2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
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"
45 using namespace VERILOG_FRONTEND
;
47 static std::list
<std::string
> output_code
;
48 static std::list
<std::string
> input_buffer
;
49 static size_t input_buffer_charp
;
51 static void return_char(char ch
)
53 if (input_buffer_charp
== 0)
54 input_buffer
.push_front(std::string() + ch
);
56 input_buffer
.front()[--input_buffer_charp
] = ch
;
59 static void insert_input(std::string str
)
61 if (input_buffer_charp
!= 0) {
62 input_buffer
.front() = input_buffer
.front().substr(input_buffer_charp
);
63 input_buffer_charp
= 0;
65 input_buffer
.push_front(str
);
68 static char next_char()
70 if (input_buffer
.empty())
73 log_assert(input_buffer_charp
<= input_buffer
.front().size());
74 if (input_buffer_charp
== input_buffer
.front().size()) {
75 input_buffer_charp
= 0;
76 input_buffer
.pop_front();
80 char ch
= input_buffer
.front()[input_buffer_charp
++];
81 return ch
== '\r' ? next_char() : ch
;
84 static std::string
skip_spaces()
88 char ch
= next_char();
91 if (ch
!= ' ' && ch
!= '\t') {
100 static std::string
next_token(bool pass_newline
= false)
104 char ch
= next_char();
111 output_code
.push_back(token
);
117 if (ch
== ' ' || ch
== '\t')
119 while ((ch
= next_char()) != 0) {
120 if (ch
!= ' ' && ch
!= '\t') {
129 while ((ch
= next_char()) != 0) {
134 if ((ch
= next_char()) != 0)
138 if (token
== "\"\"" && (ch
= next_char()) != 0) {
147 while ((ch
= next_char()) != 0) {
148 if (ch
< 33 || ch
> 126) {
157 if ((ch
= next_char()) != 0) {
161 while ((ch
= next_char()) != 0) {
166 if (last_ch
!= '*' || ch
!= '/') {
173 else if (ch
== '*') {
175 int newline_count
= 0;
177 while ((ch
= next_char()) != 0) {
183 if (last_ch
== '*' && ch
== '/')
187 while (newline_count
-- > 0)
196 const char *ok
= "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ$0123456789";
197 if (ch
== '`' || strchr(ok
, ch
) != NULL
)
201 if (first
== '`' && (ch
== '"' || ch
== '`')) {
204 if (strchr(ok
, ch
) == NULL
) {
209 } while ((ch
= next_char()) != 0);
217 macro_arg_t(const std::string
&name_
, const char *default_value_
)
219 has_default(default_value_
!= nullptr),
220 default_value(default_value_
? default_value_
: "")
225 std::string default_value
;
228 static bool all_white(const std::string
&str
)
241 void add_arg(const std::string
&name
, const char *default_value
)
244 log_error("Duplicate macro arguments with name `%s'.\n", name
.c_str());
247 name_to_pos
[name
] = args
.size();
248 args
.push_back(macro_arg_t(name
, default_value
));
251 // Find an argument by name; return nullptr if it doesn't exist. If pos is not null, write
252 // the argument's position to it on success.
253 const macro_arg_t
*find(const std::string
&name
, int *pos
= nullptr) const
255 auto it
= name_to_pos
.find(name
);
256 if (it
== name_to_pos
.end())
259 if (pos
) *pos
= it
->second
;
260 return &args
[it
->second
];
263 // Construct the name for the local macro definition we use for the given argument
264 // (something like macro_foobar_arg2). This doesn't include the leading backtick.
265 static std::string
str_token(const std::string
¯o_name
, int pos
)
267 return stringf("macro_%s_arg%d", macro_name
.c_str(), pos
);
270 // Return definitions for the macro arguments (so that substituting in the macro body and
271 // then performing macro expansion will do argument substitution properly).
272 std::vector
<std::pair
<std::string
, std::string
>>
273 get_vals(const std::string
¯o_name
, const std::vector
<std::string
> &arg_vals
) const
275 std::vector
<std::pair
<std::string
, std::string
>> ret
;
276 for (int i
= 0; i
< GetSize(args
); ++ i
) {
277 // The SystemVerilog rules are:
279 // - If the call site specifies an argument and it's not whitespace, use
282 // - Otherwise, if the argument has a default value, use it.
284 // - Otherwise, if the call site specified whitespace, use that.
286 // - Otherwise, error.
287 const std::string
*dflt
= nullptr;
288 if (args
[i
].has_default
)
289 dflt
= &args
[i
].default_value
;
291 const std::string
*given
= nullptr;
292 if (i
< GetSize(arg_vals
))
293 given
= &arg_vals
[i
];
295 const std::string
*val
= nullptr;
296 if (given
&& (! (dflt
&& all_white(*given
))))
303 log_error("Cannot expand macro `%s by giving only %d argument%s "
304 "(argument %d has no default).\n",
305 macro_name
.c_str(), GetSize(arg_vals
),
306 (GetSize(arg_vals
) == 1 ? "" : "s"), i
+ 1);
309 ret
.push_back(std::make_pair(str_token(macro_name
, i
), * val
));
315 std::vector
<macro_arg_t
> args
;
316 std::map
<std::string
, int> name_to_pos
;
321 define_body_t(const std::string
&body
, const arg_map_t
*args
= nullptr)
323 has_args(args
!= nullptr),
324 args(args
? *args
: arg_map_t())
332 define_map_t::define_map_t()
337 // We must define this destructor here (rather than relying on the default), because we need to
338 // define it somewhere we've got a complete definition of define_body_t.
339 define_map_t::~define_map_t()
343 define_map_t::add(const std::string
&name
, const std::string
&txt
, const arg_map_t
*args
)
345 defines
[name
] = std::unique_ptr
<define_body_t
>(new define_body_t(txt
, args
));
348 void define_map_t::add(const std::string
&name
, const define_body_t
&body
)
350 defines
[name
] = std::unique_ptr
<define_body_t
>(new define_body_t(body
));
353 void define_map_t::merge(const define_map_t
&map
)
355 for (const auto &pr
: map
.defines
) {
356 // These contortions are so that we take a copy of each definition body in
358 defines
[pr
.first
] = std::unique_ptr
<define_body_t
>(new define_body_t(*pr
.second
));
362 const define_body_t
*define_map_t::find(const std::string
&name
) const
364 auto it
= defines
.find(name
);
365 return (it
== defines
.end()) ? nullptr : it
->second
.get();
368 void define_map_t::erase(const std::string
&name
)
373 void define_map_t::clear()
378 void define_map_t::log() const
380 for (auto &it
: defines
) {
381 const std::string
&name
= it
.first
;
382 const define_body_t
&body
= *it
.second
;
383 Yosys::log("`define %s%s %s\n",
384 name
.c_str(), body
.has_args
? "()" : "", body
.body
.c_str());
388 static void input_file(std::istream
&f
, std::string filename
)
394 auto it
= input_buffer
.begin();
396 input_buffer
.insert(it
, "`file_push \"" + filename
+ "\"\n");
397 while ((rc
= readsome(f
, buffer
, sizeof(buffer
)-1)) > 0) {
399 input_buffer
.insert(it
, buffer
);
401 input_buffer
.insert(it
, "\n`file_pop\n");
404 // Read tokens to get one argument (either a macro argument at a callsite or a default argument in a
405 // macro definition). Writes the argument to dest. Returns true if we finished with ')' (the end of
406 // the argument list); false if we finished with ','.
407 static bool read_argument(std::string
&dest
)
410 std::vector
<char> openers
;
412 std::string tok
= next_token(true);
414 if (openers
.empty()) {
415 while (dest
.size() && (dest
.back() == ' ' || dest
.back() == '\t'))
416 dest
= dest
.substr(0, dest
.size() - 1);
419 if (openers
.back() != '(')
420 log_error("Mismatched brackets in macro argument: %c and %c.\n",
421 openers
.back(), tok
[0]);
428 char opener
= openers
.empty() ? '(' : openers
.back();
430 log_error("Mismatched brackets in macro argument: %c and %c.\n",
438 char opener
= openers
.empty() ? '(' : openers
.back();
440 log_error("Mismatched brackets in macro argument: %c and %c.\n",
448 if (tok
== "," && openers
.empty()) {
452 if (tok
== "(" || tok
== "[" || tok
== "{")
453 openers
.push_back(tok
[0]);
459 using macro_arg_stack_t
= std::stack
<std::pair
<std::string
, define_body_t
>>;
461 static void restore_macro_arg(define_map_t
&defines
, macro_arg_stack_t
¯o_arg_stack
)
463 log_assert(!macro_arg_stack
.empty());
464 auto &overwritten_arg
= macro_arg_stack
.top();
465 defines
.add(overwritten_arg
.first
, overwritten_arg
.second
);
466 macro_arg_stack
.pop();
469 static bool try_expand_macro(define_map_t
&defines
, macro_arg_stack_t
¯o_arg_stack
, std::string
&tok
)
472 std::string
literal("\"");
473 // Expand string literal
474 while (!input_buffer
.empty()) {
475 std::string ntok
= next_token();
477 insert_input(literal
+"\"");
479 } else if (!try_expand_macro(defines
, macro_arg_stack
, ntok
)) {
483 return false; // error - unmatched `"
487 // Swallow `` in macro expansion
491 if (tok
.size() <= 1 || tok
[0] != '`')
494 // This token looks like a macro name (`foo).
495 std::string macro_name
= tok
.substr(1);
496 const define_body_t
*body
= defines
.find(tok
.substr(1));
499 // Apparently not a name we know.
503 std::string name
= tok
.substr(1);
504 std::string skipped_spaces
= skip_spaces();
505 tok
= next_token(false);
506 if (body
->has_args
) {
508 if (tok
.size() == 1 && iscntrl(tok
[0])) {
510 snprintf(buf
, sizeof(buf
), "\\x%02x", tok
[0]);
513 log_error("Expected to find '(' to begin macro arguments for '%s', but instead found '%s'\n",
514 name
.c_str(), tok
.c_str());
516 std::vector
<std::string
> args
;
520 done
= read_argument(arg
);
523 for (const auto &pr
: body
->args
.get_vals(name
, args
)) {
524 if (const define_body_t
*existing
= defines
.find(pr
.first
)) {
525 macro_arg_stack
.push({pr
.first
, *existing
});
526 insert_input("`__restore_macro_arg ");
528 defines
.add(pr
.first
, pr
.second
);
532 insert_input(skipped_spaces
);
534 insert_input(body
->body
);
538 // Read the arguments for a `define preprocessor directive with formal arguments. This is called
539 // just after reading the token containing "(". Returns the number of newlines to emit afterwards to
540 // keep line numbers in sync, together with the map from argument name to data (pos and default
542 static std::pair
<int, arg_map_t
>
545 // Each argument looks like one of the following:
548 // identifier = default_text
551 // The first example is an argument with no default value. The second is an argument whose
552 // default value is default_text. The third is an argument with default value the empty
555 int newline_count
= 0;
560 // 0: At start of identifier
561 // 1: After identifier (stored in arg_name)
562 // 2: After closing paren
565 std::string arg_name
, default_val
;
570 // We've read the closing paren.
573 std::string tok
= next_token();
575 // Cope with escaped EOLs
577 char ch
= next_char();
579 // Eat the \, the \n and any trailing space and keep going.
583 // There aren't any other situations where a backslash makes sense.
584 log_error("Backslash in macro arguments (not at end of line).\n");
590 // At start of argument. If the token is ')', we've presumably just seen
591 // something like "`define foo() ...". Set state to 2 to finish. Otherwise,
592 // the token should be a valid simple identifier, but we'll allow anything
604 // After argument. The token should either be an equals sign or a comma or
607 std::string default_val
;
608 //Read an argument into default_val and set state to 2 if we're at
609 // the end; 0 if we hit a comma.
610 state
= read_argument(default_val
) ? 2 : 0;
611 args
.add_arg(arg_name
, default_val
.c_str());
616 // Take the identifier as an argument with no default value.
617 args
.add_arg(arg_name
, nullptr);
623 // As with comma, but set state to 2 (end of args)
624 args
.add_arg(arg_name
, nullptr);
629 log_error("Trailing contents after identifier in macro argument `%s': "
630 "expected '=', ',' or ')'.\n",
634 // The only FSM states are 0-2 and we dealt with 2 at the start of the loop.
639 return std::make_pair(newline_count
, args
);
642 // Read a `define preprocessor directive. This is called just after reading the token containing
645 read_define(const std::string
&filename
,
646 define_map_t
&defines_map
,
647 define_map_t
&global_defines_cache
)
649 std::string name
, value
;
653 name
= next_token(true);
655 bool here_doc_mode
= false;
656 int newline_count
= 0;
658 // The FSM state starts at 0. If it sees space (or enters here_doc_mode), it assumes this is
659 // a macro without formal arguments and jumps to state 1.
661 // In state 0, if it sees an opening parenthesis, it assumes this is a macro with formal
662 // arguments. It reads the arguments with read_define_args() and then jumps to state 2.
664 // In states 1 or 2, the FSM reads tokens to the end of line (or end of here_doc): this is
665 // the body of the macro definition.
668 if (skip_spaces() != "")
672 std::string tok
= next_token();
676 // printf("define-tok: >>%s<<\n", tok != "\n" ? tok.c_str() : "NEWLINE");
678 if (tok
== "\"\"\"") {
679 here_doc_mode
= !here_doc_mode
;
683 if (state
== 0 && tok
== "(") {
684 auto pr
= read_define_args();
685 newline_count
+= pr
.first
;
692 // This token isn't an opening parenthesis immediately following the macro name, so
693 // it's presumably at or after the start of the macro body. If state isn't already 2
694 // (which would mean we'd parsed an argument list), set it to 1.
711 char ch
= next_char();
716 value
+= std::string("\\");
722 // Is this token the name of a macro argument? If so, replace it with a magic symbol
723 // that we'll replace with the argument value.
725 if (args
.find(tok
, &arg_pos
)) {
726 value
+= '`' + args
.str_token(name
, arg_pos
);
730 // This token is nothing special. Insert it verbatim into the macro body.
734 // Append some newlines so that we don't mess up line counts in error messages.
735 while (newline_count
-- > 0)
738 if (strchr("abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ$0123456789", name
[0])) {
739 // printf("define: >>%s<< -> >>%s<<\n", name.c_str(), value.c_str());
740 defines_map
.add(name
, value
, (state
== 2) ? &args
: nullptr);
741 global_defines_cache
.add(name
, value
, (state
== 2) ? &args
: nullptr);
743 log_file_error(filename
, 0, "Invalid name for macro definition: >>%s<<.\n", name
.c_str());
748 frontend_verilog_preproc(std::istream
&f
,
749 std::string filename
,
750 const define_map_t
&pre_defines
,
751 define_map_t
&global_defines_cache
,
752 const std::list
<std::string
> &include_dirs
)
754 define_map_t defines
;
755 defines
.merge(pre_defines
);
756 defines
.merge(global_defines_cache
);
758 macro_arg_stack_t macro_arg_stack
;
759 std::vector
<std::string
> filename_stack
;
760 // We are inside pass_level levels of satisfied ifdefs, and then within
761 // fail_level levels of unsatisfied ifdefs. The unsatisfied ones are
762 // always within satisfied ones — even if some condition within is true,
763 // the parent condition failing renders it moot.
764 int ifdef_fail_level
= 0;
765 int ifdef_pass_level
= 0;
766 // For the outermost unsatisfied ifdef, true iff that ifdef already
767 // had a satisfied branch, and further elsif/else branches should be
768 // considered unsatisfied even if the condition is true.
769 // Meaningless if ifdef_fail_level == 0.
770 bool ifdef_already_satisfied
= false;
773 input_buffer
.clear();
774 input_buffer_charp
= 0;
776 input_file(f
, filename
);
778 while (!input_buffer
.empty())
780 std::string tok
= next_token();
781 // printf("token: >>%s<<\n", tok != "\n" ? tok.c_str() : "NEWLINE");
783 if (tok
== "`endif") {
784 if (ifdef_fail_level
> 0)
786 else if (ifdef_pass_level
> 0)
789 log_error("Found %s outside of macro conditional branch!\n", tok
.c_str());
793 if (tok
== "`else") {
794 if (ifdef_fail_level
== 0) {
795 if (ifdef_pass_level
== 0)
796 log_error("Found %s outside of macro conditional branch!\n", tok
.c_str());
798 ifdef_fail_level
= 1;
799 ifdef_already_satisfied
= true;
800 } else if (ifdef_fail_level
== 1 && !ifdef_already_satisfied
) {
801 ifdef_fail_level
= 0;
803 ifdef_already_satisfied
= true;
808 if (tok
== "`elsif") {
810 std::string name
= next_token(true);
811 if (ifdef_fail_level
== 0) {
812 if (ifdef_pass_level
== 0)
813 log_error("Found %s outside of macro conditional branch!\n", tok
.c_str());
815 ifdef_fail_level
= 1;
816 ifdef_already_satisfied
= true;
817 } else if (ifdef_fail_level
== 1 && !ifdef_already_satisfied
&& defines
.find(name
)) {
818 ifdef_fail_level
= 0;
820 ifdef_already_satisfied
= true;
825 if (tok
== "`ifdef") {
827 std::string name
= next_token(true);
828 if (ifdef_fail_level
> 0 || !defines
.find(name
)) {
832 ifdef_already_satisfied
= true;
834 if (ifdef_fail_level
== 1)
835 ifdef_already_satisfied
= false;
839 if (tok
== "`ifndef") {
841 std::string name
= next_token(true);
842 if (ifdef_fail_level
> 0 || defines
.find(name
)) {
846 ifdef_already_satisfied
= true;
848 if (ifdef_fail_level
== 1)
849 ifdef_already_satisfied
= false;
853 if (ifdef_fail_level
> 0) {
855 output_code
.push_back(tok
);
859 if (tok
== "`include") {
861 std::string fn
= next_token(true);
862 while (try_expand_macro(defines
, macro_arg_stack
, fn
)) {
866 size_t pos
= fn
.find('"');
867 if (pos
== std::string::npos
)
872 fn
= fn
.substr(0, pos
) + fn
.substr(pos
+1);
876 std::string fixed_fn
= fn
;
877 ff
.open(fixed_fn
.c_str());
879 bool filename_path_sep_found
;
882 // Both forward and backslash are acceptable separators on Windows.
883 filename_path_sep_found
= (filename
.find_first_of("/\\") != std::string::npos
);
884 // Easier just to invert the check for an absolute path (e.g. C:\ or C:/)
885 fn_relative
= !(fn
[1] == ':' && (fn
[2] == '/' || fn
[2] == '\\'));
887 filename_path_sep_found
= (filename
.find('/') != std::string::npos
);
888 fn_relative
= (fn
[0] != '/');
891 if (ff
.fail() && fn
.size() > 0 && fn_relative
&& filename_path_sep_found
) {
892 // if the include file was not found, it is not given with an absolute path, and the
893 // currently read file is given with a path, then try again relative to its directory
896 fixed_fn
= filename
.substr(0, filename
.find_last_of("/\\")+1) + fn
;
898 fixed_fn
= filename
.substr(0, filename
.rfind('/')+1) + fn
;
902 if (ff
.fail() && fn
.size() > 0 && fn_relative
) {
903 // if the include file was not found and it is not given with an absolute path, then
904 // search it in the include path
905 for (auto incdir
: include_dirs
) {
907 fixed_fn
= incdir
+ '/' + fn
;
909 if (!ff
.fail()) break;
913 output_code
.push_back("`file_notfound " + fn
);
915 input_file(ff
, fixed_fn
);
916 yosys_input_files
.insert(fixed_fn
);
921 if (tok
== "`file_push") {
923 std::string fn
= next_token(true);
924 if (!fn
.empty() && fn
.front() == '"' && fn
.back() == '"')
925 fn
= fn
.substr(1, fn
.size()-2);
926 output_code
.push_back(tok
+ " \"" + fn
+ "\"");
927 filename_stack
.push_back(filename
);
932 if (tok
== "`file_pop") {
933 output_code
.push_back(tok
);
934 filename
= filename_stack
.back();
935 filename_stack
.pop_back();
939 if (tok
== "`define") {
940 read_define(filename
, defines
, global_defines_cache
);
944 if (tok
== "`undef") {
947 name
= next_token(true);
948 // printf("undef: >>%s<<\n", name.c_str());
950 global_defines_cache
.erase(name
);
954 if (tok
== "`timescale") {
956 while (!tok
.empty() && tok
!= "\n")
957 tok
= next_token(true);
963 if (tok
== "`resetall") {
965 global_defines_cache
.clear();
969 if (tok
== "`__restore_macro_arg") {
970 restore_macro_arg(defines
, macro_arg_stack
);
974 if (try_expand_macro(defines
, macro_arg_stack
, tok
))
977 output_code
.push_back(tok
);
980 if (ifdef_fail_level
> 0 || ifdef_pass_level
> 0) {
981 log_error("Unterminated preprocessor conditional!\n");
985 for (auto &str
: output_code
)
989 input_buffer
.clear();
990 input_buffer_charp
= 0;