abc9: -reintegrate recover type from existing cell, check against boxid
[yosys.git] / frontends / verilog / verilog_frontend.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
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.
9 *
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.
17 *
18 * ---
19 *
20 * The Verilog frontend.
21 *
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.
26 *
27 */
28
29 #include "verilog_frontend.h"
30 #include "kernel/yosys.h"
31 #include "libs/sha1/sha1.h"
32 #include <stdarg.h>
33
34 YOSYS_NAMESPACE_BEGIN
35 using namespace VERILOG_FRONTEND;
36
37 // use the Verilog bison/flex parser to generate an AST and use AST::process() to convert it to RTLIL
38
39 static std::vector<std::string> verilog_defaults;
40 static std::list<std::vector<std::string>> verilog_defaults_stack;
41
42 static void error_on_dpi_function(AST::AstNode *node)
43 {
44 if (node->type == AST::AST_DPI_FUNCTION)
45 log_file_error(node->filename, node->linenum, "Found DPI function %s.\n", node->str.c_str());
46 for (auto child : node->children)
47 error_on_dpi_function(child);
48 }
49
50 struct VerilogFrontend : public Frontend {
51 VerilogFrontend() : Frontend("verilog", "read modules from Verilog file") { }
52 void help() YS_OVERRIDE
53 {
54 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
55 log("\n");
56 log(" read_verilog [options] [filename]\n");
57 log("\n");
58 log("Load modules from a Verilog file to the current design. A large subset of\n");
59 log("Verilog-2005 is supported.\n");
60 log("\n");
61 log(" -sv\n");
62 log(" enable support for SystemVerilog features. (only a small subset\n");
63 log(" of SystemVerilog is supported)\n");
64 log("\n");
65 log(" -formal\n");
66 log(" enable support for SystemVerilog assertions and some Yosys extensions\n");
67 log(" replace the implicit -D SYNTHESIS with -D FORMAL\n");
68 log("\n");
69 log(" -noassert\n");
70 log(" ignore assert() statements\n");
71 log("\n");
72 log(" -noassume\n");
73 log(" ignore assume() statements\n");
74 log("\n");
75 log(" -norestrict\n");
76 log(" ignore restrict() statements\n");
77 log("\n");
78 log(" -assume-asserts\n");
79 log(" treat all assert() statements like assume() statements\n");
80 log("\n");
81 log(" -assert-assumes\n");
82 log(" treat all assume() statements like assert() statements\n");
83 log("\n");
84 log(" -debug\n");
85 log(" alias for -dump_ast1 -dump_ast2 -dump_vlog1 -dump_vlog2 -yydebug\n");
86 log("\n");
87 log(" -dump_ast1\n");
88 log(" dump abstract syntax tree (before simplification)\n");
89 log("\n");
90 log(" -dump_ast2\n");
91 log(" dump abstract syntax tree (after simplification)\n");
92 log("\n");
93 log(" -no_dump_ptr\n");
94 log(" do not include hex memory addresses in dump (easier to diff dumps)\n");
95 log("\n");
96 log(" -dump_vlog1\n");
97 log(" dump ast as Verilog code (before simplification)\n");
98 log("\n");
99 log(" -dump_vlog2\n");
100 log(" dump ast as Verilog code (after simplification)\n");
101 log("\n");
102 log(" -dump_rtlil\n");
103 log(" dump generated RTLIL netlist\n");
104 log("\n");
105 log(" -yydebug\n");
106 log(" enable parser debug output\n");
107 log("\n");
108 log(" -nolatches\n");
109 log(" usually latches are synthesized into logic loops\n");
110 log(" this option prohibits this and sets the output to 'x'\n");
111 log(" in what would be the latches hold condition\n");
112 log("\n");
113 log(" this behavior can also be achieved by setting the\n");
114 log(" 'nolatches' attribute on the respective module or\n");
115 log(" always block.\n");
116 log("\n");
117 log(" -nomem2reg\n");
118 log(" under certain conditions memories are converted to registers\n");
119 log(" early during simplification to ensure correct handling of\n");
120 log(" complex corner cases. this option disables this behavior.\n");
121 log("\n");
122 log(" this can also be achieved by setting the 'nomem2reg'\n");
123 log(" attribute on the respective module or register.\n");
124 log("\n");
125 log(" This is potentially dangerous. Usually the front-end has good\n");
126 log(" reasons for converting an array to a list of registers.\n");
127 log(" Prohibiting this step will likely result in incorrect synthesis\n");
128 log(" results.\n");
129 log("\n");
130 log(" -mem2reg\n");
131 log(" always convert memories to registers. this can also be\n");
132 log(" achieved by setting the 'mem2reg' attribute on the respective\n");
133 log(" module or register.\n");
134 log("\n");
135 log(" -nomeminit\n");
136 log(" do not infer $meminit cells and instead convert initialized\n");
137 log(" memories to registers directly in the front-end.\n");
138 log("\n");
139 log(" -ppdump\n");
140 log(" dump Verilog code after pre-processor\n");
141 log("\n");
142 log(" -nopp\n");
143 log(" do not run the pre-processor\n");
144 log("\n");
145 log(" -nodpi\n");
146 log(" disable DPI-C support\n");
147 log("\n");
148 log(" -noblackbox\n");
149 log(" do not automatically add a (* blackbox *) attribute to an\n");
150 log(" empty module.\n");
151 log("\n");
152 log(" -lib\n");
153 log(" only create empty blackbox modules. This implies -DBLACKBOX.\n");
154 log(" modules with the (* whitebox *) attribute will be preserved.\n");
155 log(" (* lib_whitebox *) will be treated like (* whitebox *).\n");
156 log("\n");
157 log(" -nowb\n");
158 log(" delete (* whitebox *) and (* lib_whitebox *) attributes from\n");
159 log(" all modules.\n");
160 log("\n");
161 log(" -specify\n");
162 log(" parse and import specify blocks\n");
163 log("\n");
164 log(" -noopt\n");
165 log(" don't perform basic optimizations (such as const folding) in the\n");
166 log(" high-level front-end.\n");
167 log("\n");
168 log(" -icells\n");
169 log(" interpret cell types starting with '$' as internal cell types\n");
170 log("\n");
171 log(" -pwires\n");
172 log(" add a wire for each module parameter\n");
173 log("\n");
174 log(" -nooverwrite\n");
175 log(" ignore re-definitions of modules. (the default behavior is to\n");
176 log(" create an error message if the existing module is not a black box\n");
177 log(" module, and overwrite the existing module otherwise.)\n");
178 log("\n");
179 log(" -overwrite\n");
180 log(" overwrite existing modules with the same name\n");
181 log("\n");
182 log(" -defer\n");
183 log(" only read the abstract syntax tree and defer actual compilation\n");
184 log(" to a later 'hierarchy' command. Useful in cases where the default\n");
185 log(" parameters of modules yield invalid or not synthesizable code.\n");
186 log("\n");
187 log(" -noautowire\n");
188 log(" make the default of `default_nettype be \"none\" instead of \"wire\".\n");
189 log("\n");
190 log(" -setattr <attribute_name>\n");
191 log(" set the specified attribute (to the value 1) on all loaded modules\n");
192 log("\n");
193 log(" -Dname[=definition]\n");
194 log(" define the preprocessor symbol 'name' and set its optional value\n");
195 log(" 'definition'\n");
196 log("\n");
197 log(" -Idir\n");
198 log(" add 'dir' to the directories which are used when searching include\n");
199 log(" files\n");
200 log("\n");
201 log("The command 'verilog_defaults' can be used to register default options for\n");
202 log("subsequent calls to 'read_verilog'.\n");
203 log("\n");
204 log("Note that the Verilog frontend does a pretty good job of processing valid\n");
205 log("verilog input, but has not very good error reporting. It generally is\n");
206 log("recommended to use a simulator (for example Icarus Verilog) for checking\n");
207 log("the syntax of the code, rather than to rely on read_verilog for that.\n");
208 log("\n");
209 log("Depending on if read_verilog is run in -formal mode, either the macro\n");
210 log("SYNTHESIS or FORMAL is defined automatically. In addition, read_verilog\n");
211 log("always defines the macro YOSYS.\n");
212 log("\n");
213 log("See the Yosys README file for a list of non-standard Verilog features\n");
214 log("supported by the Yosys Verilog front-end.\n");
215 log("\n");
216 }
217 void execute(std::istream *&f, std::string filename, std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
218 {
219 bool flag_dump_ast1 = false;
220 bool flag_dump_ast2 = false;
221 bool flag_no_dump_ptr = false;
222 bool flag_dump_vlog1 = false;
223 bool flag_dump_vlog2 = false;
224 bool flag_dump_rtlil = false;
225 bool flag_nolatches = false;
226 bool flag_nomeminit = false;
227 bool flag_nomem2reg = false;
228 bool flag_mem2reg = false;
229 bool flag_ppdump = false;
230 bool flag_nopp = false;
231 bool flag_nodpi = false;
232 bool flag_noopt = false;
233 bool flag_icells = false;
234 bool flag_pwires = false;
235 bool flag_nooverwrite = false;
236 bool flag_overwrite = false;
237 bool flag_defer = false;
238 bool flag_noblackbox = false;
239 bool flag_nowb = false;
240 std::map<std::string, std::string> defines_map;
241 std::list<std::string> include_dirs;
242 std::list<std::string> attributes;
243
244 frontend_verilog_yydebug = false;
245 sv_mode = false;
246 formal_mode = false;
247 norestrict_mode = false;
248 assume_asserts_mode = false;
249 lib_mode = false;
250 specify_mode = false;
251 default_nettype_wire = true;
252
253 args.insert(args.begin()+1, verilog_defaults.begin(), verilog_defaults.end());
254
255 size_t argidx;
256 for (argidx = 1; argidx < args.size(); argidx++) {
257 std::string arg = args[argidx];
258 if (arg == "-sv") {
259 sv_mode = true;
260 continue;
261 }
262 if (arg == "-formal") {
263 formal_mode = true;
264 continue;
265 }
266 if (arg == "-noassert") {
267 noassert_mode = true;
268 continue;
269 }
270 if (arg == "-noassume") {
271 noassume_mode = true;
272 continue;
273 }
274 if (arg == "-norestrict") {
275 norestrict_mode = true;
276 continue;
277 }
278 if (arg == "-assume-asserts") {
279 assume_asserts_mode = true;
280 continue;
281 }
282 if (arg == "-assert-assumes") {
283 assert_assumes_mode = true;
284 continue;
285 }
286 if (arg == "-debug") {
287 flag_dump_ast1 = true;
288 flag_dump_ast2 = true;
289 flag_dump_vlog1 = true;
290 flag_dump_vlog2 = true;
291 frontend_verilog_yydebug = true;
292 continue;
293 }
294 if (arg == "-dump_ast1") {
295 flag_dump_ast1 = true;
296 continue;
297 }
298 if (arg == "-dump_ast2") {
299 flag_dump_ast2 = true;
300 continue;
301 }
302 if (arg == "-no_dump_ptr") {
303 flag_no_dump_ptr = true;
304 continue;
305 }
306 if (arg == "-dump_vlog1") {
307 flag_dump_vlog1 = true;
308 continue;
309 }
310 if (arg == "-dump_vlog2") {
311 flag_dump_vlog2 = true;
312 continue;
313 }
314 if (arg == "-dump_rtlil") {
315 flag_dump_rtlil = true;
316 continue;
317 }
318 if (arg == "-yydebug") {
319 frontend_verilog_yydebug = true;
320 continue;
321 }
322 if (arg == "-nolatches") {
323 flag_nolatches = true;
324 continue;
325 }
326 if (arg == "-nomeminit") {
327 flag_nomeminit = true;
328 continue;
329 }
330 if (arg == "-nomem2reg") {
331 flag_nomem2reg = true;
332 continue;
333 }
334 if (arg == "-mem2reg") {
335 flag_mem2reg = true;
336 continue;
337 }
338 if (arg == "-ppdump") {
339 flag_ppdump = true;
340 continue;
341 }
342 if (arg == "-nopp") {
343 flag_nopp = true;
344 continue;
345 }
346 if (arg == "-nodpi") {
347 flag_nodpi = true;
348 continue;
349 }
350 if (arg == "-noblackbox") {
351 flag_noblackbox = true;
352 continue;
353 }
354 if (arg == "-lib") {
355 lib_mode = true;
356 defines_map["BLACKBOX"] = string();
357 continue;
358 }
359 if (arg == "-nowb") {
360 flag_nowb = true;
361 continue;
362 }
363 if (arg == "-specify") {
364 specify_mode = true;
365 continue;
366 }
367 if (arg == "-noopt") {
368 flag_noopt = true;
369 continue;
370 }
371 if (arg == "-icells") {
372 flag_icells = true;
373 continue;
374 }
375 if (arg == "-pwires") {
376 flag_pwires = true;
377 continue;
378 }
379 if (arg == "-ignore_redef" || arg == "-nooverwrite") {
380 flag_nooverwrite = true;
381 flag_overwrite = false;
382 continue;
383 }
384 if (arg == "-overwrite") {
385 flag_nooverwrite = false;
386 flag_overwrite = true;
387 continue;
388 }
389 if (arg == "-defer") {
390 flag_defer = true;
391 continue;
392 }
393 if (arg == "-noautowire") {
394 default_nettype_wire = false;
395 continue;
396 }
397 if (arg == "-setattr" && argidx+1 < args.size()) {
398 attributes.push_back(RTLIL::escape_id(args[++argidx]));
399 continue;
400 }
401 if (arg == "-D" && argidx+1 < args.size()) {
402 std::string name = args[++argidx], value;
403 size_t equal = name.find('=');
404 if (equal != std::string::npos) {
405 value = name.substr(equal+1);
406 name = name.substr(0, equal);
407 }
408 defines_map[name] = value;
409 continue;
410 }
411 if (arg.compare(0, 2, "-D") == 0) {
412 size_t equal = arg.find('=', 2);
413 std::string name = arg.substr(2, equal-2);
414 std::string value;
415 if (equal != std::string::npos)
416 value = arg.substr(equal+1);
417 defines_map[name] = value;
418 continue;
419 }
420 if (arg == "-I" && argidx+1 < args.size()) {
421 include_dirs.push_back(args[++argidx]);
422 continue;
423 }
424 if (arg.compare(0, 2, "-I") == 0) {
425 include_dirs.push_back(arg.substr(2));
426 continue;
427 }
428 break;
429 }
430 extra_args(f, filename, args, argidx);
431
432 log_header(design, "Executing Verilog-2005 frontend: %s\n", filename.c_str());
433
434 log("Parsing %s%s input from `%s' to AST representation.\n",
435 formal_mode ? "formal " : "", sv_mode ? "SystemVerilog" : "Verilog", filename.c_str());
436
437 AST::current_filename = filename;
438 AST::set_line_num = &frontend_verilog_yyset_lineno;
439 AST::get_line_num = &frontend_verilog_yyget_lineno;
440
441 current_ast = new AST::AstNode(AST::AST_DESIGN);
442
443 lexin = f;
444 std::string code_after_preproc;
445
446 if (!flag_nopp) {
447 code_after_preproc = frontend_verilog_preproc(*f, filename, defines_map, design->verilog_defines, include_dirs);
448 if (flag_ppdump)
449 log("-- Verilog code after preprocessor --\n%s-- END OF DUMP --\n", code_after_preproc.c_str());
450 lexin = new std::istringstream(code_after_preproc);
451 }
452
453 frontend_verilog_yyset_lineno(1);
454 frontend_verilog_yyrestart(NULL);
455 frontend_verilog_yyparse();
456 frontend_verilog_yylex_destroy();
457
458 for (auto &child : current_ast->children) {
459 if (child->type == AST::AST_MODULE)
460 for (auto &attr : attributes)
461 if (child->attributes.count(attr) == 0)
462 child->attributes[attr] = AST::AstNode::mkconst_int(1, false);
463 }
464
465 if (flag_nodpi)
466 error_on_dpi_function(current_ast);
467
468 AST::process(design, current_ast, flag_dump_ast1, flag_dump_ast2, flag_no_dump_ptr, flag_dump_vlog1, flag_dump_vlog2, flag_dump_rtlil, flag_nolatches,
469 flag_nomeminit, flag_nomem2reg, flag_mem2reg, flag_noblackbox, lib_mode, flag_nowb, flag_noopt, flag_icells, flag_pwires, flag_nooverwrite, flag_overwrite, flag_defer, default_nettype_wire);
470
471 if (!flag_nopp)
472 delete lexin;
473
474 delete current_ast;
475 current_ast = NULL;
476
477 log("Successfully finished Verilog frontend.\n");
478 }
479 } VerilogFrontend;
480
481 struct VerilogDefaults : public Pass {
482 VerilogDefaults() : Pass("verilog_defaults", "set default options for read_verilog") { }
483 void help() YS_OVERRIDE
484 {
485 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
486 log("\n");
487 log(" verilog_defaults -add [options]\n");
488 log("\n");
489 log("Add the specified options to the list of default options to read_verilog.\n");
490 log("\n");
491 log("\n");
492 log(" verilog_defaults -clear\n");
493 log("\n");
494 log("Clear the list of Verilog default options.\n");
495 log("\n");
496 log("\n");
497 log(" verilog_defaults -push\n");
498 log(" verilog_defaults -pop\n");
499 log("\n");
500 log("Push or pop the list of default options to a stack. Note that -push does\n");
501 log("not imply -clear.\n");
502 log("\n");
503 }
504 void execute(std::vector<std::string> args, RTLIL::Design*) YS_OVERRIDE
505 {
506 if (args.size() < 2)
507 cmd_error(args, 1, "Missing argument.");
508
509 if (args[1] == "-add") {
510 verilog_defaults.insert(verilog_defaults.end(), args.begin()+2, args.end());
511 return;
512 }
513
514 if (args.size() != 2)
515 cmd_error(args, 2, "Extra argument.");
516
517 if (args[1] == "-clear") {
518 verilog_defaults.clear();
519 return;
520 }
521
522 if (args[1] == "-push") {
523 verilog_defaults_stack.push_back(verilog_defaults);
524 return;
525 }
526
527 if (args[1] == "-pop") {
528 if (verilog_defaults_stack.empty()) {
529 verilog_defaults.clear();
530 } else {
531 verilog_defaults.swap(verilog_defaults_stack.back());
532 verilog_defaults_stack.pop_back();
533 }
534 return;
535 }
536 }
537 } VerilogDefaults;
538
539 struct VerilogDefines : public Pass {
540 VerilogDefines() : Pass("verilog_defines", "define and undefine verilog defines") { }
541 void help() YS_OVERRIDE
542 {
543 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
544 log("\n");
545 log(" verilog_defines [options]\n");
546 log("\n");
547 log("Define and undefine verilog preprocessor macros.\n");
548 log("\n");
549 log(" -Dname[=definition]\n");
550 log(" define the preprocessor symbol 'name' and set its optional value\n");
551 log(" 'definition'\n");
552 log("\n");
553 log(" -Uname[=definition]\n");
554 log(" undefine the preprocessor symbol 'name'\n");
555 log("\n");
556 log(" -reset\n");
557 log(" clear list of defined preprocessor symbols\n");
558 log("\n");
559 log(" -list\n");
560 log(" list currently defined preprocessor symbols\n");
561 log("\n");
562 }
563 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
564 {
565 size_t argidx;
566 for (argidx = 1; argidx < args.size(); argidx++) {
567 std::string arg = args[argidx];
568 if (arg == "-D" && argidx+1 < args.size()) {
569 std::string name = args[++argidx], value;
570 size_t equal = name.find('=');
571 if (equal != std::string::npos) {
572 value = name.substr(equal+1);
573 name = name.substr(0, equal);
574 }
575 design->verilog_defines[name] = std::pair<std::string, bool>(value, false);
576 continue;
577 }
578 if (arg.compare(0, 2, "-D") == 0) {
579 size_t equal = arg.find('=', 2);
580 std::string name = arg.substr(2, equal-2);
581 std::string value;
582 if (equal != std::string::npos)
583 value = arg.substr(equal+1);
584 design->verilog_defines[name] = std::pair<std::string, bool>(value, false);
585 continue;
586 }
587 if (arg == "-U" && argidx+1 < args.size()) {
588 std::string name = args[++argidx];
589 design->verilog_defines.erase(name);
590 continue;
591 }
592 if (arg.compare(0, 2, "-U") == 0) {
593 std::string name = arg.substr(2);
594 design->verilog_defines.erase(name);
595 continue;
596 }
597 if (arg == "-reset") {
598 design->verilog_defines.clear();
599 continue;
600 }
601 if (arg == "-list") {
602 for (auto &it : design->verilog_defines) {
603 log("`define %s%s %s\n", it.first.c_str(), it.second.second ? "()" : "", it.second.first.c_str());
604 }
605 continue;
606 }
607 break;
608 }
609
610 if (args.size() != argidx)
611 cmd_error(args, argidx, "Extra argument.");
612 }
613 } VerilogDefines;
614
615 YOSYS_NAMESPACE_END
616
617 // the yyerror function used by bison to report parser errors
618 void frontend_verilog_yyerror(char const *fmt, ...)
619 {
620 va_list ap;
621 char buffer[1024];
622 char *p = buffer;
623 va_start(ap, fmt);
624 p += vsnprintf(p, buffer + sizeof(buffer) - p, fmt, ap);
625 va_end(ap);
626 p += snprintf(p, buffer + sizeof(buffer) - p, "\n");
627 YOSYS_NAMESPACE_PREFIX log_file_error(YOSYS_NAMESPACE_PREFIX AST::current_filename, frontend_verilog_yyget_lineno(),
628 "%s", buffer);
629 exit(1);
630 }