Add "yosys -e regex" for turning warnings into errors
[yosys.git] / kernel / driver.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 #include "kernel/yosys.h"
21 #include "libs/sha1/sha1.h"
22
23 #ifdef YOSYS_ENABLE_READLINE
24 # include <readline/readline.h>
25 # include <readline/history.h>
26 #endif
27
28 #ifdef YOSYS_ENABLE_EDITLINE
29 # include <editline/readline.h>
30 #endif
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <errno.h>
36
37 #ifdef __linux__
38 # include <sys/resource.h>
39 # include <sys/types.h>
40 # include <unistd.h>
41 #endif
42
43 #if !defined(_WIN32) || defined(__MINGW32__)
44 # include <unistd.h>
45 #else
46 char *optarg;
47 int optind = 1, optcur = 1;
48 int getopt(int argc, char **argv, const char *optstring)
49 {
50 if (optind >= argc || argv[optind][0] != '-')
51 return -1;
52
53 bool takes_arg = false;
54 int opt = argv[optind][optcur];
55 for (int i = 0; optstring[i]; i++)
56 if (opt == optstring[i] && optstring[i + 1] == ':')
57 takes_arg = true;
58
59 if (!takes_arg) {
60 if (argv[optind][++optcur] == 0)
61 optind++, optcur = 1;
62 return opt;
63 }
64
65 if (argv[optind][++optcur]) {
66 optarg = argv[optind++] + optcur;
67 optcur = 1;
68 return opt;
69 }
70
71 optarg = argv[++optind];
72 optind++, optcur = 1;
73 return opt;
74 }
75 #endif
76
77
78 USING_YOSYS_NAMESPACE
79
80 #ifdef EMSCRIPTEN
81 # include <sys/stat.h>
82 # include <sys/types.h>
83
84 extern "C" int main(int, char**);
85 extern "C" void run(const char*);
86 extern "C" const char *errmsg();
87 extern "C" const char *prompt();
88
89 int main(int, char**)
90 {
91 mkdir("/work", 0777);
92 chdir("/work");
93 log_files.push_back(stdout);
94 log_error_stderr = true;
95 yosys_banner();
96 yosys_setup();
97 }
98
99 void run(const char *command)
100 {
101 int selSize = GetSize(yosys_get_design()->selection_stack);
102 try {
103 log_last_error = "Internal error (see JavaScript console for details)";
104 run_pass(command);
105 log_last_error = "";
106 } catch (...) {
107 while (GetSize(yosys_get_design()->selection_stack) > selSize)
108 yosys_get_design()->selection_stack.pop_back();
109 throw;
110 }
111 }
112
113 const char *errmsg()
114 {
115 return log_last_error.c_str();
116 }
117
118 const char *prompt()
119 {
120 const char *p = create_prompt(yosys_get_design(), 0);
121 while (*p == '\n') p++;
122 return p;
123 }
124
125 #else /* EMSCRIPTEN */
126
127 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
128 int yosys_history_offset = 0;
129 std::string yosys_history_file;
130 #endif
131
132 void yosys_atexit()
133 {
134 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
135 if (!yosys_history_file.empty()) {
136 #if defined(YOSYS_ENABLE_READLINE)
137 if (yosys_history_offset > 0) {
138 history_truncate_file(yosys_history_file.c_str(), 100);
139 append_history(where_history() - yosys_history_offset, yosys_history_file.c_str());
140 } else
141 write_history(yosys_history_file.c_str());
142 #else
143 write_history(yosys_history_file.c_str());
144 #endif
145 }
146
147 clear_history();
148 #if defined(YOSYS_ENABLE_READLINE)
149 HIST_ENTRY **hist_list = history_list();
150 if (hist_list != NULL)
151 free(hist_list);
152 #endif
153 #endif
154 }
155
156 int main(int argc, char **argv)
157 {
158 std::string frontend_command = "auto";
159 std::string backend_command = "auto";
160 std::vector<std::string> passes_commands;
161 std::vector<std::string> plugin_filenames;
162 std::string output_filename = "";
163 std::string scriptfile = "";
164 std::string depsfile = "";
165 bool scriptfile_tcl = false;
166 bool got_output_filename = false;
167 bool print_banner = true;
168 bool print_stats = true;
169 bool call_abort = false;
170 bool timing_details = false;
171 bool mode_v = false;
172 bool mode_q = false;
173
174 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
175 if (getenv("HOME") != NULL) {
176 yosys_history_file = stringf("%s/.yosys_history", getenv("HOME"));
177 read_history(yosys_history_file.c_str());
178 yosys_history_offset = where_history();
179 }
180 #endif
181
182 if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "-help") || !strcmp(argv[1], "--help")))
183 {
184 printf("\n");
185 printf("Usage: %s [options] [<infile> [..]]\n", argv[0]);
186 printf("\n");
187 printf(" -Q\n");
188 printf(" suppress printing of banner (copyright, disclaimer, version)\n");
189 printf("\n");
190 printf(" -T\n");
191 printf(" suppress printing of footer (log hash, version, timing statistics)\n");
192 printf("\n");
193 printf(" -q\n");
194 printf(" quiet operation. only write warnings and error messages to console\n");
195 printf(" use this option twice to also quiet warning messages\n");
196 printf("\n");
197 printf(" -v <level>\n");
198 printf(" print log headers up to level <level> to the console. (this\n");
199 printf(" implies -q for everything except the 'End of script.' message.)\n");
200 printf("\n");
201 printf(" -t\n");
202 printf(" annotate all log messages with a time stamp\n");
203 printf("\n");
204 printf(" -d\n");
205 printf(" print more detailed timing stats at exit\n");
206 printf("\n");
207 printf(" -l logfile\n");
208 printf(" write log messages to the specified file\n");
209 printf("\n");
210 printf(" -L logfile\n");
211 printf(" like -l but open log file in line buffered mode\n");
212 printf("\n");
213 printf(" -o outfile\n");
214 printf(" write the design to the specified file on exit\n");
215 printf("\n");
216 printf(" -b backend\n");
217 printf(" use this backend for the output file specified on the command line\n");
218 printf("\n");
219 printf(" -f frontend\n");
220 printf(" use the specified frontend for the input files on the command line\n");
221 printf("\n");
222 printf(" -H\n");
223 printf(" print the command list\n");
224 printf("\n");
225 printf(" -h command\n");
226 printf(" print the help message for the specified command\n");
227 printf("\n");
228 printf(" -s scriptfile\n");
229 printf(" execute the commands in the script file\n");
230 printf("\n");
231 printf(" -c tcl_scriptfile\n");
232 printf(" execute the commands in the tcl script file (see 'help tcl' for details)\n");
233 printf("\n");
234 printf(" -p command\n");
235 printf(" execute the commands\n");
236 printf("\n");
237 printf(" -m module_file\n");
238 printf(" load the specified module (aka plugin)\n");
239 printf("\n");
240 printf(" -X\n");
241 printf(" enable tracing of core data structure changes. for debugging\n");
242 printf("\n");
243 printf(" -M\n");
244 printf(" will slightly randomize allocated pointer addresses. for debugging\n");
245 printf("\n");
246 printf(" -A\n");
247 printf(" will call abort() at the end of the script. for debugging\n");
248 printf("\n");
249 printf(" -D <header_id>[:<filename>]\n");
250 printf(" dump the design when printing the specified log header to a file.\n");
251 printf(" yosys_dump_<header_id>.il is used as filename if none is specified.\n");
252 printf(" Use 'ALL' as <header_id> to dump at every header.\n");
253 printf("\n");
254 printf(" -W regex\n");
255 printf(" print a warning for all log messages matching the regex.\n");
256 printf("\n");
257 printf(" -w regex\n");
258 printf(" if a warning message matches the regex, it is printed as regular\n");
259 printf(" message instead.\n");
260 printf("\n");
261 printf(" -e regex\n");
262 printf(" if a warning message matches the regex, it is printed as error\n");
263 printf(" message instead and the tool terminates with a nonzero return code.\n");
264 printf("\n");
265 printf(" -E <depsfile>\n");
266 printf(" write a Makefile dependencies file with in- and output file names\n");
267 printf("\n");
268 printf(" -V\n");
269 printf(" print version information and exit\n");
270 printf("\n");
271 printf("The option -S is an shortcut for calling the \"synth\" command, a default\n");
272 printf("script for transforming the Verilog input to a gate-level netlist. For example:\n");
273 printf("\n");
274 printf(" yosys -o output.blif -S input.v\n");
275 printf("\n");
276 printf("For more complex synthesis jobs it is recommended to use the read_* and write_*\n");
277 printf("commands in a script file instead of specifying input and output files on the\n");
278 printf("command line.\n");
279 printf("\n");
280 printf("When no commands, script files or input files are specified on the command\n");
281 printf("line, yosys automatically enters the interactive command mode. Use the 'help'\n");
282 printf("command to get information on the individual commands.\n");
283 printf("\n");
284 exit(0);
285 }
286
287 int opt;
288 while ((opt = getopt(argc, argv, "MXAQTVSm:f:Hh:b:o:p:l:L:qv:tds:c:W:w:e:D:E:")) != -1)
289 {
290 switch (opt)
291 {
292 case 'M':
293 memhasher_on();
294 break;
295 case 'X':
296 yosys_xtrace++;
297 break;
298 case 'A':
299 call_abort = true;
300 break;
301 case 'Q':
302 print_banner = false;
303 break;
304 case 'T':
305 print_stats = false;
306 break;
307 case 'V':
308 printf("%s\n", yosys_version_str);
309 exit(0);
310 case 'S':
311 passes_commands.push_back("synth");
312 break;
313 case 'm':
314 plugin_filenames.push_back(optarg);
315 break;
316 case 'f':
317 frontend_command = optarg;
318 break;
319 case 'H':
320 passes_commands.push_back("help");
321 break;
322 case 'h':
323 passes_commands.push_back(stringf("help %s", optarg));
324 break;
325 case 'b':
326 backend_command = optarg;
327 break;
328 case 'p':
329 passes_commands.push_back(optarg);
330 break;
331 case 'o':
332 output_filename = optarg;
333 got_output_filename = true;
334 break;
335 case 'l':
336 case 'L':
337 log_files.push_back(fopen(optarg, "wt"));
338 if (log_files.back() == NULL) {
339 fprintf(stderr, "Can't open log file `%s' for writing!\n", optarg);
340 exit(1);
341 }
342 if (opt == 'L')
343 setvbuf(log_files.back(), NULL, _IOLBF, 0);
344 break;
345 case 'q':
346 mode_q = true;
347 if (log_errfile == stderr)
348 log_quiet_warnings = true;
349 log_errfile = stderr;
350 break;
351 case 'v':
352 mode_v = true;
353 log_errfile = stderr;
354 log_verbose_level = atoi(optarg);
355 break;
356 case 't':
357 log_time = true;
358 break;
359 case 'd':
360 timing_details = true;
361 break;
362 case 's':
363 scriptfile = optarg;
364 scriptfile_tcl = false;
365 break;
366 case 'c':
367 scriptfile = optarg;
368 scriptfile_tcl = true;
369 break;
370 case 'W':
371 log_warn_regexes.push_back(std::regex(optarg,
372 std::regex_constants::nosubs |
373 std::regex_constants::optimize |
374 std::regex_constants::egrep));
375 break;
376 case 'w':
377 log_nowarn_regexes.push_back(std::regex(optarg,
378 std::regex_constants::nosubs |
379 std::regex_constants::optimize |
380 std::regex_constants::egrep));
381 break;
382 case 'e':
383 log_werror_regexes.push_back(std::regex(optarg,
384 std::regex_constants::nosubs |
385 std::regex_constants::optimize |
386 std::regex_constants::egrep));
387 break;
388 case 'D':
389 {
390 auto args = split_tokens(optarg, ":");
391 if (!args.empty() && args[0] == "ALL") {
392 if (GetSize(args) != 1) {
393 fprintf(stderr, "Invalid number of tokens in -D ALL.\n");
394 exit(1);
395 }
396 log_hdump_all = true;
397 } else {
398 if (!args.empty() && !args[0].empty() && args[0].back() == '.')
399 args[0].pop_back();
400 if (GetSize(args) == 1)
401 args.push_back("yosys_dump_" + args[0] + ".il");
402 if (GetSize(args) != 2) {
403 fprintf(stderr, "Invalid number of tokens in -D.\n");
404 exit(1);
405 }
406 log_hdump[args[0]].insert(args[1]);
407 }
408 }
409 break;
410 case 'E':
411 depsfile = optarg;
412 break;
413 default:
414 fprintf(stderr, "Run '%s -h' for help.\n", argv[0]);
415 exit(1);
416 }
417 }
418
419 if (log_errfile == NULL) {
420 log_files.push_back(stdout);
421 log_error_stderr = true;
422 }
423
424 if (print_banner)
425 yosys_banner();
426
427 if (print_stats)
428 log_hasher = new SHA1;
429
430 #if defined(__linux__)
431 // set stack size to >= 128 MB
432 {
433 struct rlimit rl;
434 const rlim_t stack_size = 128L * 1024L * 1024L;
435 if (getrlimit(RLIMIT_STACK, &rl) == 0 && rl.rlim_cur < stack_size) {
436 rl.rlim_cur = stack_size;
437 setrlimit(RLIMIT_STACK, &rl);
438 }
439 }
440 #endif
441
442 yosys_setup();
443 log_error_atexit = yosys_atexit;
444
445 for (auto &fn : plugin_filenames)
446 load_plugin(fn, {});
447
448 if (optind == argc && passes_commands.size() == 0 && scriptfile.empty()) {
449 if (!got_output_filename)
450 backend_command = "";
451 shell(yosys_design);
452 }
453
454 while (optind < argc)
455 run_frontend(argv[optind++], frontend_command, output_filename == "-" ? &backend_command : NULL);
456
457 if (!scriptfile.empty()) {
458 if (scriptfile_tcl) {
459 #ifdef YOSYS_ENABLE_TCL
460 if (Tcl_EvalFile(yosys_get_tcl_interp(), scriptfile.c_str()) != TCL_OK)
461 log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
462 #else
463 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
464 #endif
465 } else
466 run_frontend(scriptfile, "script", output_filename == "-" ? &backend_command : NULL);
467 }
468
469 for (auto it = passes_commands.begin(); it != passes_commands.end(); it++)
470 run_pass(*it);
471
472 if (!backend_command.empty())
473 run_backend(output_filename, backend_command);
474
475 if (!depsfile.empty())
476 {
477 FILE *f = fopen(depsfile.c_str(), "wt");
478 if (f == nullptr)
479 log_error("Can't open dependencies file for writing: %s\n", strerror(errno));
480 bool first = true;
481 for (auto fn : yosys_output_files) {
482 fprintf(f, "%s%s", first ? "" : " ", fn.c_str());
483 first = false;
484 }
485 fprintf(f, ":");
486 for (auto fn : yosys_input_files) {
487 if (yosys_output_files.count(fn) == 0)
488 fprintf(f, " %s", fn.c_str());
489 }
490 fprintf(f, "\n");
491 }
492
493 if (print_stats)
494 {
495 std::string hash = log_hasher->final().substr(0, 10);
496 delete log_hasher;
497 log_hasher = nullptr;
498
499 log_time = false;
500 yosys_xtrace = 0;
501 log_spacer();
502
503 if (mode_v && !mode_q)
504 log_files.push_back(stderr);
505
506 if (log_warnings_count)
507 log("Warnings: %d unique messages, %d total\n", GetSize(log_warnings), log_warnings_count);
508 #ifdef _WIN32
509 log("End of script. Logfile hash: %s\n", hash.c_str());
510 #else
511 std::string meminfo;
512 std::string stats_divider = ", ";
513 # ifdef __linux__
514 std::ifstream statm;
515 statm.open(stringf("/proc/%lld/statm", (long long)getpid()));
516 if (statm.is_open()) {
517 int sz_total, sz_resident;
518 statm >> sz_total >> sz_resident;
519 meminfo = stringf(", MEM: %.2f MB total, %.2f MB resident",
520 sz_total * (getpagesize() / 1024.0 / 1024.0),
521 sz_resident * (getpagesize() / 1024.0 / 1024.0));
522 stats_divider = "\n";
523 }
524 # endif
525
526 struct rusage ru_buffer;
527 getrusage(RUSAGE_SELF, &ru_buffer);
528 log("End of script. Logfile hash: %s%sCPU: user %.2fs system %.2fs%s\n", hash.c_str(),
529 stats_divider.c_str(), ru_buffer.ru_utime.tv_sec + 1e-6 * ru_buffer.ru_utime.tv_usec,
530 ru_buffer.ru_stime.tv_sec + 1e-6 * ru_buffer.ru_stime.tv_usec, meminfo.c_str());
531 #endif
532 log("%s\n", yosys_version_str);
533
534 int64_t total_ns = 0;
535 std::set<tuple<int64_t, int, std::string>> timedat;
536
537 for (auto &it : pass_register)
538 if (it.second->call_counter) {
539 total_ns += it.second->runtime_ns + 1;
540 timedat.insert(make_tuple(it.second->runtime_ns + 1, it.second->call_counter, it.first));
541 }
542
543 if (timing_details)
544 {
545 log("Time spent:\n");
546 for (auto it = timedat.rbegin(); it != timedat.rend(); it++) {
547 log("%5d%% %5d calls %8.3f sec %s\n", int(100*std::get<0>(*it) / total_ns),
548 std::get<1>(*it), std::get<0>(*it) / 1000000000.0, std::get<2>(*it).c_str());
549 }
550 }
551 else
552 {
553 int out_count = 0;
554 log("Time spent:");
555 for (auto it = timedat.rbegin(); it != timedat.rend() && out_count < 4; it++, out_count++) {
556 if (out_count >= 2 && (std::get<0>(*it) < 1000000000 || int(100*std::get<0>(*it) / total_ns) < 20)) {
557 log(", ...");
558 break;
559 }
560 log("%s %d%% %dx %s (%d sec)", out_count ? "," : "", int(100*std::get<0>(*it) / total_ns),
561 std::get<1>(*it), std::get<2>(*it).c_str(), int(std::get<0>(*it) / 1000000000));
562 }
563 log("%s\n", out_count ? "" : " no commands executed");
564 }
565 }
566
567 #if defined(YOSYS_ENABLE_COVER) && defined(__linux__)
568 if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
569 {
570 string filename;
571 FILE *f;
572
573 if (getenv("YOSYS_COVER_DIR")) {
574 filename = stringf("%s/yosys_cover_%d_XXXXXX.txt", getenv("YOSYS_COVER_DIR"), getpid());
575 filename = make_temp_file(filename);
576 } else {
577 filename = getenv("YOSYS_COVER_FILE");
578 }
579
580 f = fopen(filename.c_str(), "a+");
581
582 if (f == NULL)
583 log_error("Can't create coverage file `%s'.\n", filename.c_str());
584
585 log("<writing coverage file \"%s\">\n", filename.c_str());
586
587 for (auto &it : get_coverage_data())
588 fprintf(f, "%-60s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str());
589
590 fclose(f);
591 }
592 #endif
593
594 yosys_atexit();
595
596 memhasher_off();
597 if (call_abort)
598 abort();
599
600 log_flush();
601 #if defined(_MSC_VER)
602 _exit(0);
603 #elif defined(_WIN32)
604 _Exit(0);
605 #endif
606
607 yosys_shutdown();
608
609 return 0;
610 }
611
612 #endif /* EMSCRIPTEN */
613