2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 * The Verilog frontend.
22 * This frontend is using the AST frontend library (see frontends/ast/).
23 * Thus this frontend does not generate RTLIL code directly but creates an
24 * AST directly from the Verilog parse tree and then passes this AST to
25 * the AST frontend library.
29 * Ad-hoc implementation of a Verilog preprocessor. The directives `define,
30 * `include, `ifdef, `ifndef, `else and `endif are handled here. All other
31 * directives are handled by the lexer (see verilog_lexer.l).
36 #include "verilog_frontend.h"
37 #include "kernel/log.h"
44 using namespace VERILOG_FRONTEND
;
46 static std::list
<std::string
> output_code
;
47 static std::list
<std::string
> input_buffer
;
48 static size_t input_buffer_charp
;
50 static void return_char(char ch
)
52 if (input_buffer_charp
== 0)
53 input_buffer
.push_front(std::string() + ch
);
55 input_buffer
.front()[--input_buffer_charp
] = ch
;
58 static void insert_input(std::string str
)
60 if (input_buffer_charp
!= 0) {
61 input_buffer
.front() = input_buffer
.front().substr(input_buffer_charp
);
62 input_buffer_charp
= 0;
64 input_buffer
.push_front(str
);
67 static char next_char()
69 if (input_buffer
.empty())
72 log_assert(input_buffer_charp
<= input_buffer
.front().size());
73 if (input_buffer_charp
== input_buffer
.front().size()) {
74 input_buffer_charp
= 0;
75 input_buffer
.pop_front();
79 char ch
= input_buffer
.front()[input_buffer_charp
++];
80 return ch
== '\r' ? next_char() : ch
;
83 static std::string
skip_spaces()
87 char ch
= next_char();
90 if (ch
!= ' ' && ch
!= '\t') {
99 static std::string
next_token(bool pass_newline
= false)
103 char ch
= next_char();
110 output_code
.push_back(token
);
116 if (ch
== ' ' || ch
== '\t')
118 while ((ch
= next_char()) != 0) {
119 if (ch
!= ' ' && ch
!= '\t') {
128 while ((ch
= next_char()) != 0) {
133 if ((ch
= next_char()) != 0)
137 if (token
== "\"\"" && (ch
= next_char()) != 0) {
146 if ((ch
= next_char()) != 0) {
150 while ((ch
= next_char()) != 0) {
155 if (last_ch
!= '*' || ch
!= '/') {
162 else if (ch
== '*') {
164 int newline_count
= 0;
166 while ((ch
= next_char()) != 0) {
172 if (last_ch
== '*' && ch
== '/')
176 while (newline_count
-- > 0)
185 const char *ok
= "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ$0123456789";
186 if (ch
== '`' || strchr(ok
, ch
) != NULL
)
190 if (first
== '`' && (ch
== '"' || ch
== '`')) {
193 if (strchr(ok
, ch
) == NULL
) {
198 } while ((ch
= next_char()) != 0);
206 macro_arg_t(const std::string
&name_
, const char *default_value_
)
208 has_default(default_value_
!= nullptr),
209 default_value(default_value_
? default_value_
: "")
214 std::string default_value
;
217 static bool all_white(const std::string
&str
)
230 void add_arg(const std::string
&name
, const char *default_value
)
233 log_error("Duplicate macro arguments with name `%s'.\n", name
.c_str());
236 name_to_pos
[name
] = args
.size();
237 args
.push_back(macro_arg_t(name
, default_value
));
240 // Find an argument by name; return nullptr if it doesn't exist. If pos is not null, write
241 // the argument's position to it on success.
242 const macro_arg_t
*find(const std::string
&name
, int *pos
= nullptr) const
244 auto it
= name_to_pos
.find(name
);
245 if (it
== name_to_pos
.end())
248 if (pos
) *pos
= it
->second
;
249 return &args
[it
->second
];
252 // Construct the name for the local macro definition we use for the given argument
253 // (something like macro_foobar_arg2). This doesn't include the leading backtick.
254 static std::string
str_token(const std::string
¯o_name
, int pos
)
256 return stringf("macro_%s_arg%d", macro_name
.c_str(), pos
);
259 // Return definitions for the macro arguments (so that substituting in the macro body and
260 // then performing macro expansion will do argument substitution properly).
261 std::vector
<std::pair
<std::string
, std::string
>>
262 get_vals(const std::string
¯o_name
, const std::vector
<std::string
> &arg_vals
) const
264 std::vector
<std::pair
<std::string
, std::string
>> ret
;
265 for (int i
= 0; i
< GetSize(args
); ++ i
) {
266 // The SystemVerilog rules are:
268 // - If the call site specifies an argument and it's not whitespace, use
271 // - Otherwise, if the argument has a default value, use it.
273 // - Otherwise, if the call site specified whitespace, use that.
275 // - Otherwise, error.
276 const std::string
*dflt
= nullptr;
277 if (args
[i
].has_default
)
278 dflt
= &args
[i
].default_value
;
280 const std::string
*given
= nullptr;
281 if (i
< GetSize(arg_vals
))
282 given
= &arg_vals
[i
];
284 const std::string
*val
= nullptr;
285 if (given
&& (! (dflt
&& all_white(*given
))))
292 log_error("Cannot expand macro `%s by giving only %d argument%s "
293 "(argument %d has no default).\n",
294 macro_name
.c_str(), GetSize(arg_vals
),
295 (GetSize(arg_vals
) == 1 ? "" : "s"), i
+ 1);
298 ret
.push_back(std::make_pair(str_token(macro_name
, i
), * val
));
304 std::vector
<macro_arg_t
> args
;
305 std::map
<std::string
, int> name_to_pos
;
310 define_body_t(const std::string
&body
, const arg_map_t
*args
= nullptr)
312 has_args(args
!= nullptr),
313 args(args
? *args
: arg_map_t())
321 define_map_t::define_map_t()
324 add(formal_mode
? "FORMAL" : "SYNTHESIS", "1");
327 // We must define this destructor here (rather than relying on the default), because we need to
328 // define it somewhere we've got a complete definition of define_body_t.
329 define_map_t::~define_map_t()
333 define_map_t::add(const std::string
&name
, const std::string
&txt
, const arg_map_t
*args
)
335 defines
[name
] = std::unique_ptr
<define_body_t
>(new define_body_t(txt
, args
));
338 void define_map_t::merge(const define_map_t
&map
)
340 for (const auto &pr
: map
.defines
) {
341 // These contortions are so that we take a copy of each definition body in
343 defines
[pr
.first
] = std::unique_ptr
<define_body_t
>(new define_body_t(*pr
.second
));
347 const define_body_t
*define_map_t::find(const std::string
&name
) const
349 auto it
= defines
.find(name
);
350 return (it
== defines
.end()) ? nullptr : it
->second
.get();
353 void define_map_t::erase(const std::string
&name
)
358 void define_map_t::clear()
363 void define_map_t::log() const
365 for (auto &it
: defines
) {
366 const std::string
&name
= it
.first
;
367 const define_body_t
&body
= *it
.second
;
368 Yosys::log("`define %s%s %s\n",
369 name
.c_str(), body
.has_args
? "()" : "", body
.body
.c_str());
373 static void input_file(std::istream
&f
, std::string filename
)
379 auto it
= input_buffer
.begin();
381 input_buffer
.insert(it
, "`file_push \"" + filename
+ "\"\n");
382 while ((rc
= readsome(f
, buffer
, sizeof(buffer
)-1)) > 0) {
384 input_buffer
.insert(it
, buffer
);
386 input_buffer
.insert(it
, "\n`file_pop\n");
389 // Read tokens to get one argument (either a macro argument at a callsite or a default argument in a
390 // macro definition). Writes the argument to dest. Returns true if we finished with ')' (the end of
391 // the argument list); false if we finished with ','.
392 static bool read_argument(std::string
&dest
)
394 std::vector
<char> openers
;
397 std::string tok
= next_token(true);
401 if (openers
.back() != '(')
402 log_error("Mismatched brackets in macro argument: %c and %c.\n",
403 openers
.back(), tok
[0]);
410 char opener
= openers
.empty() ? '(' : openers
.back();
412 log_error("Mismatched brackets in macro argument: %c and %c.\n",
420 char opener
= openers
.empty() ? '(' : openers
.back();
422 log_error("Mismatched brackets in macro argument: %c and %c.\n",
430 if (tok
== "," && openers
.empty()) {
434 if (tok
== "(" || tok
== "[" || tok
== "{")
435 openers
.push_back(tok
[0]);
441 static bool try_expand_macro(define_map_t
&defines
, std::string
&tok
)
444 std::string
literal("\"");
445 // Expand string literal
446 while (!input_buffer
.empty()) {
447 std::string ntok
= next_token();
449 insert_input(literal
+"\"");
451 } else if (!try_expand_macro(defines
, ntok
)) {
455 return false; // error - unmatched `"
459 // Swallow `` in macro expansion
463 if (tok
.size() <= 1 || tok
[0] != '`')
466 // This token looks like a macro name (`foo).
467 std::string macro_name
= tok
.substr(1);
468 const define_body_t
*body
= defines
.find(tok
.substr(1));
471 // Apparently not a name we know.
475 std::string name
= tok
.substr(1);
476 std::string skipped_spaces
= skip_spaces();
477 tok
= next_token(false);
478 if (tok
== "(" && body
->has_args
) {
479 std::vector
<std::string
> args
;
483 done
= read_argument(arg
);
486 for (const auto &pr
: body
->args
.get_vals(name
, args
)) {
487 defines
.add(pr
.first
, pr
.second
);
491 insert_input(skipped_spaces
);
493 insert_input(body
->body
);
497 // Read the arguments for a `define preprocessor directive with formal arguments. This is called
498 // just after reading the token containing "(". Returns the number of newlines to emit afterwards to
499 // keep line numbers in sync, together with the map from argument name to data (pos and default
501 static std::pair
<int, arg_map_t
>
504 // Each argument looks like one of the following:
507 // identifier = default_text
510 // The first example is an argument with no default value. The second is an argument whose
511 // default value is default_text. The third is an argument with default value the empty
514 int newline_count
= 0;
519 // 0: At start of identifier
520 // 1: After identifier (stored in arg_name)
521 // 2: After closing paren
524 std::string arg_name
, default_val
;
529 // We've read the closing paren.
532 std::string tok
= next_token();
534 // Cope with escaped EOLs
536 char ch
= next_char();
538 // Eat the \, the \n and any trailing space and keep going.
542 // There aren't any other situations where a backslash makes sense.
543 log_error("Backslash in macro arguments (not at end of line).\n");
549 // At start of argument. If the token is ')', we've presumably just seen
550 // something like "`define foo() ...". Set state to 2 to finish. Otherwise,
551 // the token should be a valid simple identifier, but we'll allow anything
563 // After argument. The token should either be an equals sign or a comma or
566 std::string default_val
;
567 //Read an argument into default_val and set state to 2 if we're at
568 // the end; 0 if we hit a comma.
569 state
= read_argument(default_val
) ? 2 : 0;
570 args
.add_arg(arg_name
, default_val
.c_str());
575 // Take the identifier as an argument with no default value.
576 args
.add_arg(arg_name
, nullptr);
582 // As with comma, but set state to 2 (end of args)
583 args
.add_arg(arg_name
, nullptr);
588 log_error("Trailing contents after identifier in macro argument `%s': "
589 "expected '=', ',' or ')'.\n",
593 // The only FSM states are 0-2 and we dealt with 2 at the start of the loop.
594 __builtin_unreachable();
598 return std::make_pair(newline_count
, args
);
601 // Read a `define preprocessor directive. This is called just after reading the token containing
604 read_define(const std::string
&filename
,
605 define_map_t
&defines_map
,
606 define_map_t
&global_defines_cache
)
608 std::string name
, value
;
612 name
= next_token(true);
614 bool here_doc_mode
= false;
615 int newline_count
= 0;
617 // The FSM state starts at 0. If it sees space (or enters here_doc_mode), it assumes this is
618 // a macro without formal arguments and jumps to state 1.
620 // In state 0, if it sees an opening parenthesis, it assumes this is a macro with formal
621 // arguments. It reads the arguments with read_define_args() and then jumps to state 2.
623 // In states 1 or 2, the FSM reads tokens to the end of line (or end of here_doc): this is
624 // the body of the macro definition.
627 if (skip_spaces() != "")
631 std::string tok
= next_token();
635 // printf("define-tok: >>%s<<\n", tok != "\n" ? tok.c_str() : "NEWLINE");
637 if (tok
== "\"\"\"") {
638 here_doc_mode
= !here_doc_mode
;
642 if (state
== 0 && tok
== "(") {
643 auto pr
= read_define_args();
644 newline_count
+= pr
.first
;
651 // This token isn't an opening parenthesis immediately following the macro name, so
652 // it's presumably at or after the start of the macro body. If state isn't already 2
653 // (which would mean we'd parsed an argument list), set it to 1.
670 char ch
= next_char();
675 value
+= std::string("\\");
681 // Is this token the name of a macro argument? If so, replace it with a magic symbol
682 // that we'll replace with the argument value.
684 if (args
.find(tok
, &arg_pos
)) {
685 value
+= '`' + args
.str_token(name
, arg_pos
);
689 // This token is nothing special. Insert it verbatim into the macro body.
693 // Append some newlines so that we don't mess up line counts in error messages.
694 while (newline_count
-- > 0)
697 if (strchr("abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ$0123456789", name
[0])) {
698 // printf("define: >>%s<< -> >>%s<<\n", name.c_str(), value.c_str());
699 defines_map
.add(name
, value
, (state
== 2) ? &args
: nullptr);
700 global_defines_cache
.add(name
, value
, (state
== 2) ? &args
: nullptr);
702 log_file_error(filename
, 0, "Invalid name for macro definition: >>%s<<.\n", name
.c_str());
707 frontend_verilog_preproc(std::istream
&f
,
708 std::string filename
,
709 const define_map_t
&pre_defines
,
710 define_map_t
&global_defines_cache
,
711 const std::list
<std::string
> &include_dirs
)
713 define_map_t defines
;
714 defines
.merge(pre_defines
);
715 defines
.merge(global_defines_cache
);
717 std::vector
<std::string
> filename_stack
;
718 int ifdef_fail_level
= 0;
719 bool in_elseif
= false;
722 input_buffer
.clear();
723 input_buffer_charp
= 0;
725 input_file(f
, filename
);
727 while (!input_buffer
.empty())
729 std::string tok
= next_token();
730 // printf("token: >>%s<<\n", tok != "\n" ? tok.c_str() : "NEWLINE");
732 if (tok
== "`endif") {
733 if (ifdef_fail_level
> 0)
735 if (ifdef_fail_level
== 0)
740 if (tok
== "`else") {
741 if (ifdef_fail_level
== 0)
742 ifdef_fail_level
= 1;
743 else if (ifdef_fail_level
== 1 && !in_elseif
)
744 ifdef_fail_level
= 0;
748 if (tok
== "`elsif") {
750 std::string name
= next_token(true);
751 if (ifdef_fail_level
== 0)
752 ifdef_fail_level
= 1, in_elseif
= true;
753 else if (ifdef_fail_level
== 1 && defines
.find(name
))
754 ifdef_fail_level
= 0, in_elseif
= true;
758 if (tok
== "`ifdef") {
760 std::string name
= next_token(true);
761 if (ifdef_fail_level
> 0 || !defines
.find(name
))
766 if (tok
== "`ifndef") {
768 std::string name
= next_token(true);
769 if (ifdef_fail_level
> 0 || defines
.find(name
))
774 if (ifdef_fail_level
> 0) {
776 output_code
.push_back(tok
);
780 if (tok
== "`include") {
782 std::string fn
= next_token(true);
783 while (try_expand_macro(defines
, fn
)) {
787 size_t pos
= fn
.find('"');
788 if (pos
== std::string::npos
)
793 fn
= fn
.substr(0, pos
) + fn
.substr(pos
+1);
797 std::string fixed_fn
= fn
;
798 ff
.open(fixed_fn
.c_str());
800 bool filename_path_sep_found
;
803 // Both forward and backslash are acceptable separators on Windows.
804 filename_path_sep_found
= (filename
.find_first_of("/\\") != std::string::npos
);
805 // Easier just to invert the check for an absolute path (e.g. C:\ or C:/)
806 fn_relative
= !(fn
[1] == ':' && (fn
[2] == '/' || fn
[2] == '\\'));
808 filename_path_sep_found
= (filename
.find('/') != std::string::npos
);
809 fn_relative
= (fn
[0] != '/');
812 if (ff
.fail() && fn
.size() > 0 && fn_relative
&& filename_path_sep_found
) {
813 // if the include file was not found, it is not given with an absolute path, and the
814 // currently read file is given with a path, then try again relative to its directory
817 fixed_fn
= filename
.substr(0, filename
.find_last_of("/\\")+1) + fn
;
819 fixed_fn
= filename
.substr(0, filename
.rfind('/')+1) + fn
;
823 if (ff
.fail() && fn
.size() > 0 && fn_relative
) {
824 // if the include file was not found and it is not given with an absolute path, then
825 // search it in the include path
826 for (auto incdir
: include_dirs
) {
828 fixed_fn
= incdir
+ '/' + fn
;
830 if (!ff
.fail()) break;
834 output_code
.push_back("`file_notfound " + fn
);
836 input_file(ff
, fixed_fn
);
837 yosys_input_files
.insert(fixed_fn
);
842 if (tok
== "`file_push") {
844 std::string fn
= next_token(true);
845 if (!fn
.empty() && fn
.front() == '"' && fn
.back() == '"')
846 fn
= fn
.substr(1, fn
.size()-2);
847 output_code
.push_back(tok
+ " \"" + fn
+ "\"");
848 filename_stack
.push_back(filename
);
853 if (tok
== "`file_pop") {
854 output_code
.push_back(tok
);
855 filename
= filename_stack
.back();
856 filename_stack
.pop_back();
860 if (tok
== "`define") {
861 read_define(filename
, defines
, global_defines_cache
);
865 if (tok
== "`undef") {
868 name
= next_token(true);
869 // printf("undef: >>%s<<\n", name.c_str());
871 global_defines_cache
.erase(name
);
875 if (tok
== "`timescale") {
877 while (!tok
.empty() && tok
!= "\n")
878 tok
= next_token(true);
884 if (tok
== "`resetall") {
886 global_defines_cache
.clear();
890 if (try_expand_macro(defines
, tok
))
893 output_code
.push_back(tok
);
897 for (auto &str
: output_code
)
901 input_buffer
.clear();
902 input_buffer_charp
= 0;