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 lexer.l).
35 #include "verilog_frontend.h"
36 #include "kernel/log.h"
42 using namespace VERILOG_FRONTEND
;
44 static std::list
<std::string
> output_code
;
45 static std::list
<std::string
> input_buffer
;
46 static size_t input_buffer_charp
;
48 static void return_char(char ch
)
50 if (input_buffer_charp
== 0)
51 input_buffer
.push_front(std::string() + ch
);
53 input_buffer
.front()[--input_buffer_charp
] = ch
;
56 static void insert_input(std::string str
)
58 if (input_buffer_charp
!= 0) {
59 input_buffer
.front() = input_buffer
.front().substr(input_buffer_charp
);
60 input_buffer_charp
= 0;
62 input_buffer
.push_front(str
);
65 static char next_char()
67 if (input_buffer
.empty())
70 log_assert(input_buffer_charp
<= input_buffer
.front().size());
71 if (input_buffer_charp
== input_buffer
.front().size()) {
72 input_buffer_charp
= 0;
73 input_buffer
.pop_front();
77 char ch
= input_buffer
.front()[input_buffer_charp
++];
78 return ch
== '\r' ? next_char() : ch
;
81 static std::string
skip_spaces()
85 char ch
= next_char();
88 if (ch
!= ' ' && ch
!= '\t') {
97 static std::string
next_token(bool pass_newline
= false)
101 char ch
= next_char();
108 output_code
.push_back(token
);
114 if (ch
== ' ' || ch
== '\t')
116 while ((ch
= next_char()) != 0) {
117 if (ch
!= ' ' && ch
!= '\t') {
126 while ((ch
= next_char()) != 0) {
131 if ((ch
= next_char()) != 0)
135 if (token
== "\"\"" && (ch
= next_char()) != 0) {
144 if ((ch
= next_char()) != 0) {
148 while ((ch
= next_char()) != 0) {
153 if (last_ch
!= '*' || ch
!= '/') {
160 else if (ch
== '*') {
162 int newline_count
= 0;
164 while ((ch
= next_char()) != 0) {
170 if (last_ch
== '*' && ch
== '/')
174 while (newline_count
-- > 0)
183 const char *ok
= "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ$0123456789";
184 if (ch
== '`' || strchr(ok
, ch
) != NULL
)
190 if (strchr(ok
, ch
) == NULL
) {
195 } while ((ch
= next_char()) != 0);
201 static void input_file(std::istream
&f
, std::string filename
)
207 auto it
= input_buffer
.begin();
209 input_buffer
.insert(it
, "`file_push \"" + filename
+ "\"\n");
210 while ((rc
= readsome(f
, buffer
, sizeof(buffer
)-1)) > 0) {
212 input_buffer
.insert(it
, buffer
);
214 input_buffer
.insert(it
, "\n`file_pop\n");
218 static bool try_expand_macro(std::set
<std::string
> &defines_with_args
,
219 std::map
<std::string
, std::string
> &defines_map
,
224 std::string
literal("\"");
225 // Expand string literal
226 while (!input_buffer
.empty()) {
227 std::string ntok
= next_token();
229 insert_input(literal
+"\"");
231 } else if (!try_expand_macro(defines_with_args
, defines_map
, ntok
)) {
235 return false; // error - unmatched `"
236 } else if (tok
.size() > 1 && tok
[0] == '`' && defines_map
.count(tok
.substr(1)) > 0) {
237 std::string name
= tok
.substr(1);
238 // printf("expand: >>%s<< -> >>%s<<\n", name.c_str(), defines_map[name].c_str());
239 std::string skipped_spaces
= skip_spaces();
240 tok
= next_token(false);
241 if (tok
== "(" && defines_with_args
.count(name
) > 0) {
243 std::vector
<std::string
> args
;
244 args
.push_back(std::string());
247 tok
= next_token(true);
248 if (tok
== ")" || tok
== "}" || tok
== "]")
252 if (level
== 1 && tok
== ",")
253 args
.push_back(std::string());
256 if (tok
== "(" || tok
== "{" || tok
== "[")
259 for (int i
= 0; i
< GetSize(args
); i
++)
260 defines_map
[stringf("macro_%s_arg%d", name
.c_str(), i
+1)] = args
[i
];
263 insert_input(skipped_spaces
);
265 insert_input(defines_map
[name
]);
270 std::string
frontend_verilog_preproc(std::istream
&f
, std::string filename
, const std::map
<std::string
, std::string
> &pre_defines_map
,
271 dict
<std::string
, std::pair
<std::string
, bool>> &global_defines_cache
, const std::list
<std::string
> &include_dirs
)
273 std::set
<std::string
> defines_with_args
;
274 std::map
<std::string
, std::string
> defines_map(pre_defines_map
);
275 std::vector
<std::string
> filename_stack
;
276 int ifdef_fail_level
= 0;
277 bool in_elseif
= false;
280 input_buffer
.clear();
281 input_buffer_charp
= 0;
283 input_file(f
, filename
);
285 defines_map
["YOSYS"] = "1";
286 defines_map
[formal_mode
? "FORMAL" : "SYNTHESIS"] = "1";
288 for (auto &it
: pre_defines_map
)
289 defines_map
[it
.first
] = it
.second
;
291 for (auto &it
: global_defines_cache
) {
292 if (it
.second
.second
)
293 defines_with_args
.insert(it
.first
);
294 defines_map
[it
.first
] = it
.second
.first
;
297 while (!input_buffer
.empty())
299 std::string tok
= next_token();
300 // printf("token: >>%s<<\n", tok != "\n" ? tok.c_str() : "NEWLINE");
302 if (tok
== "`endif") {
303 if (ifdef_fail_level
> 0)
305 if (ifdef_fail_level
== 0)
310 if (tok
== "`else") {
311 if (ifdef_fail_level
== 0)
312 ifdef_fail_level
= 1;
313 else if (ifdef_fail_level
== 1 && !in_elseif
)
314 ifdef_fail_level
= 0;
318 if (tok
== "`elsif") {
320 std::string name
= next_token(true);
321 if (ifdef_fail_level
== 0)
322 ifdef_fail_level
= 1, in_elseif
= true;
323 else if (ifdef_fail_level
== 1 && defines_map
.count(name
) != 0)
324 ifdef_fail_level
= 0, in_elseif
= true;
328 if (tok
== "`ifdef") {
330 std::string name
= next_token(true);
331 if (ifdef_fail_level
> 0 || defines_map
.count(name
) == 0)
336 if (tok
== "`ifndef") {
338 std::string name
= next_token(true);
339 if (ifdef_fail_level
> 0 || defines_map
.count(name
) != 0)
344 if (ifdef_fail_level
> 0) {
346 output_code
.push_back(tok
);
350 if (tok
== "`include") {
352 std::string fn
= next_token(true);
353 while (try_expand_macro(defines_with_args
, defines_map
, fn
)) {
357 size_t pos
= fn
.find('"');
358 if (pos
== std::string::npos
)
363 fn
= fn
.substr(0, pos
) + fn
.substr(pos
+1);
367 std::string fixed_fn
= fn
;
368 ff
.open(fixed_fn
.c_str());
370 bool filename_path_sep_found
;
373 // Both forward and backslash are acceptable separators on Windows.
374 filename_path_sep_found
= (filename
.find_first_of("/\\") != std::string::npos
);
375 // Easier just to invert the check for an absolute path (e.g. C:\ or C:/)
376 fn_relative
= !(fn
[1] == ':' && (fn
[2] == '/' || fn
[2] == '\\'));
378 filename_path_sep_found
= (filename
.find('/') != std::string::npos
);
379 fn_relative
= (fn
[0] != '/');
382 if (ff
.fail() && fn
.size() > 0 && fn_relative
&& filename_path_sep_found
) {
383 // if the include file was not found, it is not given with an absolute path, and the
384 // currently read file is given with a path, then try again relative to its directory
387 fixed_fn
= filename
.substr(0, filename
.find_last_of("/\\")+1) + fn
;
389 fixed_fn
= filename
.substr(0, filename
.rfind('/')+1) + fn
;
393 if (ff
.fail() && fn
.size() > 0 && fn_relative
) {
394 // if the include file was not found and it is not given with an absolute path, then
395 // search it in the include path
396 for (auto incdir
: include_dirs
) {
398 fixed_fn
= incdir
+ '/' + fn
;
400 if (!ff
.fail()) break;
404 output_code
.push_back("`file_notfound " + fn
);
406 input_file(ff
, fixed_fn
);
410 if (tok
== "`file_push") {
412 std::string fn
= next_token(true);
413 if (!fn
.empty() && fn
.front() == '"' && fn
.back() == '"')
414 fn
= fn
.substr(1, fn
.size()-2);
415 output_code
.push_back(tok
+ " \"" + fn
+ "\"");
416 filename_stack
.push_back(filename
);
421 if (tok
== "`file_pop") {
422 output_code
.push_back(tok
);
423 filename
= filename_stack
.back();
424 filename_stack
.pop_back();
428 if (tok
== "`define") {
429 std::string name
, value
;
430 std::map
<std::string
, int> args
;
432 name
= next_token(true);
433 bool here_doc_mode
= false;
434 int newline_count
= 0;
436 if (skip_spaces() != "")
438 while (!tok
.empty()) {
440 if (tok
== "\"\"\"") {
441 here_doc_mode
= !here_doc_mode
;
444 if (state
== 0 && tok
== "(") {
451 else if (tok
!= ",") {
452 int arg_idx
= args
.size()+1;
469 char ch
= next_char();
474 value
+= std::string("\\");
478 if (args
.count(tok
) > 0)
479 value
+= stringf("`macro_%s_arg%d", name
.c_str(), args
.at(tok
));
484 while (newline_count
-- > 0)
486 // printf("define: >>%s<< -> >>%s<<\n", name.c_str(), value.c_str());
487 defines_map
[name
] = value
;
489 defines_with_args
.insert(name
);
491 defines_with_args
.erase(name
);
492 global_defines_cache
[name
] = std::pair
<std::string
, bool>(value
, state
== 2);
496 if (tok
== "`undef") {
499 name
= next_token(true);
500 // printf("undef: >>%s<<\n", name.c_str());
501 defines_map
.erase(name
);
502 defines_with_args
.erase(name
);
503 global_defines_cache
.erase(name
);
507 if (tok
== "`timescale") {
509 while (!tok
.empty() && tok
!= "\n")
510 tok
= next_token(true);
516 if (tok
== "`resetall") {
518 defines_with_args
.clear();
519 global_defines_cache
.clear();
523 if (try_expand_macro(defines_with_args
, defines_map
, tok
))
526 output_code
.push_back(tok
);
530 for (auto &str
: output_code
)
534 input_buffer
.clear();
535 input_buffer_charp
= 0;