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
)
185 while ((ch
= next_char()) != 0) {
186 if (strchr(ok
, ch
) == NULL
) {
197 static void input_file(std::istream
&f
, std::string filename
)
203 auto it
= input_buffer
.begin();
205 input_buffer
.insert(it
, "`file_push \"" + filename
+ "\"\n");
206 while ((rc
= readsome(f
, buffer
, sizeof(buffer
)-1)) > 0) {
208 input_buffer
.insert(it
, buffer
);
210 input_buffer
.insert(it
, "\n`file_pop\n");
213 std::string
frontend_verilog_preproc(std::istream
&f
, std::string filename
, const std::map
<std::string
, std::string
> &pre_defines_map
,
214 dict
<std::string
, std::pair
<std::string
, bool>> &global_defines_cache
, const std::list
<std::string
> &include_dirs
)
216 std::set
<std::string
> defines_with_args
;
217 std::map
<std::string
, std::string
> defines_map(pre_defines_map
);
218 int ifdef_fail_level
= 0;
219 bool in_elseif
= false;
222 input_buffer
.clear();
223 input_buffer_charp
= 0;
225 input_file(f
, filename
);
227 defines_map
["YOSYS"] = "1";
228 defines_map
[formal_mode
? "FORMAL" : "SYNTHESIS"] = "1";
230 for (auto &it
: pre_defines_map
)
231 defines_map
[it
.first
] = it
.second
;
233 for (auto &it
: global_defines_cache
) {
234 if (it
.second
.second
)
235 defines_with_args
.insert(it
.first
);
236 defines_map
[it
.first
] = it
.second
.first
;
239 while (!input_buffer
.empty())
241 std::string tok
= next_token();
242 // printf("token: >>%s<<\n", tok != "\n" ? tok.c_str() : "NEWLINE");
244 if (tok
== "`endif") {
245 if (ifdef_fail_level
> 0)
247 if (ifdef_fail_level
== 0)
252 if (tok
== "`else") {
253 if (ifdef_fail_level
== 0)
254 ifdef_fail_level
= 1;
255 else if (ifdef_fail_level
== 1 && !in_elseif
)
256 ifdef_fail_level
= 0;
260 if (tok
== "`elsif") {
262 std::string name
= next_token(true);
263 if (ifdef_fail_level
== 0)
264 ifdef_fail_level
= 1, in_elseif
= true;
265 else if (ifdef_fail_level
== 1 && defines_map
.count(name
) != 0)
266 ifdef_fail_level
= 0, in_elseif
= true;
270 if (tok
== "`ifdef") {
272 std::string name
= next_token(true);
273 if (ifdef_fail_level
> 0 || defines_map
.count(name
) == 0)
278 if (tok
== "`ifndef") {
280 std::string name
= next_token(true);
281 if (ifdef_fail_level
> 0 || defines_map
.count(name
) != 0)
286 if (ifdef_fail_level
> 0) {
288 output_code
.push_back(tok
);
292 if (tok
== "`include") {
294 std::string fn
= next_token(true);
296 size_t pos
= fn
.find('"');
297 if (pos
== std::string::npos
)
302 fn
= fn
.substr(0, pos
) + fn
.substr(pos
+1);
307 if (ff
.fail() && fn
.size() > 0 && fn
[0] != '/' && filename
.find('/') != std::string::npos
) {
308 // if the include file was not found, it is not given with an absolute path, and the
309 // currently read file is given with a path, then try again relative to its directory
311 ff
.open(filename
.substr(0, filename
.rfind('/')+1) + fn
);
313 if (ff
.fail() && fn
.size() > 0 && fn
[0] != '/') {
314 // if the include file was not found and it is not given with an absolute path, then
315 // search it in the include path
316 for (auto incdir
: include_dirs
) {
318 ff
.open(incdir
+ '/' + fn
);
319 if (!ff
.fail()) break;
323 output_code
.push_back("`file_notfound " + fn
);
329 if (tok
== "`define") {
330 std::string name
, value
;
331 std::map
<std::string
, int> args
;
333 name
= next_token(true);
334 bool here_doc_mode
= false;
335 int newline_count
= 0;
337 if (skip_spaces() != "")
339 while (!tok
.empty()) {
341 if (tok
== "\"\"\"") {
342 here_doc_mode
= !here_doc_mode
;
345 if (state
== 0 && tok
== "(") {
352 else if (tok
!= ",") {
353 int arg_idx
= args
.size()+1;
370 char ch
= next_char();
375 value
+= std::string("\\");
379 if (args
.count(tok
) > 0)
380 value
+= stringf("`macro_%s_arg%d", name
.c_str(), args
.at(tok
));
385 while (newline_count
-- > 0)
387 // printf("define: >>%s<< -> >>%s<<\n", name.c_str(), value.c_str());
388 defines_map
[name
] = value
;
390 defines_with_args
.insert(name
);
392 defines_with_args
.erase(name
);
393 global_defines_cache
[name
] = std::pair
<std::string
, bool>(value
, state
== 2);
397 if (tok
== "`undef") {
400 name
= next_token(true);
401 // printf("undef: >>%s<<\n", name.c_str());
402 defines_map
.erase(name
);
403 defines_with_args
.erase(name
);
404 global_defines_cache
.erase(name
);
408 if (tok
== "`timescale") {
410 while (!tok
.empty() && tok
!= "\n")
411 tok
= next_token(true);
417 if (tok
.size() > 1 && tok
[0] == '`' && defines_map
.count(tok
.substr(1)) > 0) {
418 std::string name
= tok
.substr(1);
419 // printf("expand: >>%s<< -> >>%s<<\n", name.c_str(), defines_map[name].c_str());
420 std::string skipped_spaces
= skip_spaces();
421 tok
= next_token(false);
422 if (tok
== "(" && defines_with_args
.count(name
) > 0) {
424 std::vector
<std::string
> args
;
425 args
.push_back(std::string());
428 tok
= next_token(true);
429 if (tok
== ")" || tok
== "}" || tok
== "]")
433 if (level
== 1 && tok
== ",")
434 args
.push_back(std::string());
437 if (tok
== "(" || tok
== "{" || tok
== "[")
440 for (int i
= 0; i
< GetSize(args
); i
++)
441 defines_map
[stringf("macro_%s_arg%d", name
.c_str(), i
+1)] = args
[i
];
444 insert_input(skipped_spaces
);
446 insert_input(defines_map
[name
]);
450 output_code
.push_back(tok
);
454 for (auto &str
: output_code
)
458 input_buffer
.clear();
459 input_buffer_charp
= 0;