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 #include "kernel/yosys.h"
21 #include "libs/sha1/sha1.h"
23 #ifdef YOSYS_ENABLE_READLINE
24 # include <readline/readline.h>
25 # include <readline/history.h>
28 #ifdef YOSYS_ENABLE_EDITLINE
29 # include <editline/readline.h>
37 #if defined (__linux__) || defined(__FreeBSD__)
38 # include <sys/resource.h>
39 # include <sys/types.h>
44 # include <sys/sysctl.h>
45 # include <sys/user.h>
48 #if !defined(_WIN32) || defined(__MINGW32__)
52 int optind
= 1, optcur
= 1;
53 int getopt(int argc
, char **argv
, const char *optstring
)
55 if (optind
>= argc
|| argv
[optind
][0] != '-')
58 bool takes_arg
= false;
59 int opt
= argv
[optind
][optcur
];
60 for (int i
= 0; optstring
[i
]; i
++)
61 if (opt
== optstring
[i
] && optstring
[i
+ 1] == ':')
65 if (argv
[optind
][++optcur
] == 0)
70 if (argv
[optind
][++optcur
]) {
71 optarg
= argv
[optind
++] + optcur
;
76 optarg
= argv
[++optind
];
86 # include <sys/stat.h>
87 # include <sys/types.h>
88 # include <emscripten.h>
90 extern "C" int main(int, char**);
91 extern "C" void run(const char*);
92 extern "C" const char *errmsg();
93 extern "C" const char *prompt();
95 int main(int argc
, char **argv
)
98 if (ENVIRONMENT_IS_NODE
)
100 FS
.mkdir('/hostcwd');
101 FS
.mount(NODEFS
, { root
: '.' }, '/hostcwd');
103 FS
.mount(NODEFS
, { root
: '/' }, '/hostfs');
107 mkdir("/work", 0777);
109 log_files
.push_back(stdout
);
110 log_error_stderr
= true;
114 PyRun_SimpleString(("sys.path.append(\""+proc_self_dirname()+"\")").c_str());
115 PyRun_SimpleString(("sys.path.append(\""+proc_share_dirname()+"plugins\")").c_str());
120 // Run the first argument as a script file
121 run_frontend(argv
[1], "script", 0, 0, 0);
125 void run(const char *command
)
127 int selSize
= GetSize(yosys_get_design()->selection_stack
);
129 log_last_error
= "Internal error (see JavaScript console for details)";
133 while (GetSize(yosys_get_design()->selection_stack
) > selSize
)
134 yosys_get_design()->selection_stack
.pop_back();
141 return log_last_error
.c_str();
146 const char *p
= create_prompt(yosys_get_design(), 0);
147 while (*p
== '\n') p
++;
151 #else /* EMSCRIPTEN */
153 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
154 int yosys_history_offset
= 0;
155 std::string yosys_history_file
;
160 // FIXME: WASI does not currently support exceptions.
161 void* __cxa_allocate_exception(size_t thrown_size
) throw() {
162 return malloc(thrown_size
);
164 bool __cxa_uncaught_exception() throw();
165 void __cxa_throw(void* thrown_exception
, struct std::type_info
* tinfo
, void (*dest
)(void*)) {
173 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
174 if (!yosys_history_file
.empty()) {
175 #if defined(YOSYS_ENABLE_READLINE)
176 if (yosys_history_offset
> 0) {
177 history_truncate_file(yosys_history_file
.c_str(), 100);
178 append_history(where_history() - yosys_history_offset
, yosys_history_file
.c_str());
180 write_history(yosys_history_file
.c_str());
182 write_history(yosys_history_file
.c_str());
187 #if defined(YOSYS_ENABLE_READLINE)
188 HIST_ENTRY
**hist_list
= history_list();
189 if (hist_list
!= NULL
)
195 int main(int argc
, char **argv
)
197 std::string frontend_command
= "auto";
198 std::string backend_command
= "auto";
199 std::vector
<std::string
> vlog_defines
;
200 std::vector
<std::string
> passes_commands
;
201 std::vector
<std::string
> plugin_filenames
;
202 std::string output_filename
= "";
203 std::string scriptfile
= "";
204 std::string depsfile
= "";
205 bool scriptfile_tcl
= false;
206 bool got_output_filename
= false;
207 bool print_banner
= true;
208 bool print_stats
= true;
209 bool call_abort
= false;
210 bool timing_details
= false;
214 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
215 if (getenv("HOME") != NULL
) {
216 yosys_history_file
= stringf("%s/.yosys_history", getenv("HOME"));
217 read_history(yosys_history_file
.c_str());
218 yosys_history_offset
= where_history();
222 if (argc
== 2 && (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "-help") || !strcmp(argv
[1], "--help")))
225 printf("Usage: %s [options] [<infile> [..]]\n", argv
[0]);
228 printf(" suppress printing of banner (copyright, disclaimer, version)\n");
231 printf(" suppress printing of footer (log hash, version, timing statistics)\n");
234 printf(" quiet operation. only write warnings and error messages to console\n");
235 printf(" use this option twice to also quiet warning messages\n");
237 printf(" -v <level>\n");
238 printf(" print log headers up to level <level> to the console. (this\n");
239 printf(" implies -q for everything except the 'End of script.' message.)\n");
242 printf(" annotate all log messages with a time stamp\n");
245 printf(" print more detailed timing stats at exit\n");
247 printf(" -l logfile\n");
248 printf(" write log messages to the specified file\n");
250 printf(" -L logfile\n");
251 printf(" like -l but open log file in line buffered mode\n");
253 printf(" -o outfile\n");
254 printf(" write the design to the specified file on exit\n");
256 printf(" -b backend\n");
257 printf(" use this backend for the output file specified on the command line\n");
259 printf(" -f frontend\n");
260 printf(" use the specified frontend for the input files on the command line\n");
263 printf(" print the command list\n");
265 printf(" -h command\n");
266 printf(" print the help message for the specified command\n");
268 printf(" -s scriptfile\n");
269 printf(" execute the commands in the script file\n");
270 #ifdef YOSYS_ENABLE_TCL
272 printf(" -c tcl_scriptfile\n");
273 printf(" execute the commands in the tcl script file (see 'help tcl' for details)\n");
276 printf(" -p command\n");
277 printf(" execute the commands\n");
279 printf(" -m module_file\n");
280 printf(" load the specified module (aka plugin)\n");
283 printf(" enable tracing of core data structure changes. for debugging\n");
286 printf(" will slightly randomize allocated pointer addresses. for debugging\n");
289 printf(" will call abort() at the end of the script. for debugging\n");
291 printf(" -D <macro>[=<value>]\n");
292 printf(" set the specified Verilog define (via \"read -define\")\n");
294 printf(" -P <header_id>[:<filename>]\n");
295 printf(" dump the design when printing the specified log header to a file.\n");
296 printf(" yosys_dump_<header_id>.il is used as filename if none is specified.\n");
297 printf(" Use 'ALL' as <header_id> to dump at every header.\n");
299 printf(" -W regex\n");
300 printf(" print a warning for all log messages matching the regex.\n");
302 printf(" -w regex\n");
303 printf(" if a warning message matches the regex, it is printed as regular\n");
304 printf(" message instead.\n");
306 printf(" -e regex\n");
307 printf(" if a warning message matches the regex, it is printed as error\n");
308 printf(" message instead and the tool terminates with a nonzero return code.\n");
310 printf(" -E <depsfile>\n");
311 printf(" write a Makefile dependencies file with in- and output file names\n");
313 printf(" -x <feature>\n");
314 printf(" do not print warnings for the specified experimental feature\n");
317 printf(" globally enable debug log messages\n");
320 printf(" print version information and exit\n");
322 printf("The option -S is an shortcut for calling the \"synth\" command, a default\n");
323 printf("script for transforming the Verilog input to a gate-level netlist. For example:\n");
325 printf(" yosys -o output.blif -S input.v\n");
327 printf("For more complex synthesis jobs it is recommended to use the read_* and write_*\n");
328 printf("commands in a script file instead of specifying input and output files on the\n");
329 printf("command line.\n");
331 printf("When no commands, script files or input files are specified on the command\n");
332 printf("line, yosys automatically enters the interactive command mode. Use the 'help'\n");
333 printf("command to get information on the individual commands.\n");
338 if (argc
== 2 && (!strcmp(argv
[1], "-V") || !strcmp(argv
[1], "-version") || !strcmp(argv
[1], "--version")))
340 printf("%s\n", yosys_version_str
);
345 while ((opt
= getopt(argc
, argv
, "MXAQTVSgm:f:Hh:b:o:p:l:L:qv:tds:c:W:w:e:D:P:E:x:")) != -1)
359 print_banner
= false;
365 printf("%s\n", yosys_version_str
);
368 passes_commands
.push_back("synth");
374 plugin_filenames
.push_back(optarg
);
377 frontend_command
= optarg
;
380 passes_commands
.push_back("help");
383 passes_commands
.push_back(stringf("help %s", optarg
));
386 backend_command
= optarg
;
389 passes_commands
.push_back(optarg
);
392 output_filename
= optarg
;
393 got_output_filename
= true;
397 log_files
.push_back(fopen(optarg
, "wt"));
398 if (log_files
.back() == NULL
) {
399 fprintf(stderr
, "Can't open log file `%s' for writing!\n", optarg
);
403 setvbuf(log_files
.back(), NULL
, _IOLBF
, 0);
407 if (log_errfile
== stderr
)
408 log_quiet_warnings
= true;
409 log_errfile
= stderr
;
413 log_errfile
= stderr
;
414 log_verbose_level
= atoi(optarg
);
420 timing_details
= true;
424 scriptfile_tcl
= false;
428 scriptfile_tcl
= true;
431 log_warn_regexes
.push_back(YS_REGEX_COMPILE(optarg
));
434 log_nowarn_regexes
.push_back(YS_REGEX_COMPILE(optarg
));
437 log_werror_regexes
.push_back(YS_REGEX_COMPILE(optarg
));
440 vlog_defines
.push_back(optarg
);
444 auto args
= split_tokens(optarg
, ":");
445 if (!args
.empty() && args
[0] == "ALL") {
446 if (GetSize(args
) != 1) {
447 fprintf(stderr
, "Invalid number of tokens in -D ALL.\n");
450 log_hdump_all
= true;
452 if (!args
.empty() && !args
[0].empty() && args
[0].back() == '.')
454 if (GetSize(args
) == 1)
455 args
.push_back("yosys_dump_" + args
[0] + ".il");
456 if (GetSize(args
) != 2) {
457 fprintf(stderr
, "Invalid number of tokens in -D.\n");
460 log_hdump
[args
[0]].insert(args
[1]);
468 log_experimentals_ignored
.insert(optarg
);
471 fprintf(stderr
, "Run '%s -h' for help.\n", argv
[0]);
476 if (log_errfile
== NULL
) {
477 log_files
.push_back(stdout
);
478 log_error_stderr
= true;
485 log_hasher
= new SHA1
;
487 #if defined(__linux__)
488 // set stack size to >= 128 MB
491 const rlim_t stack_size
= 128L * 1024L * 1024L;
492 if (getrlimit(RLIMIT_STACK
, &rl
) == 0 && rl
.rlim_cur
< stack_size
) {
493 rl
.rlim_cur
= stack_size
;
494 setrlimit(RLIMIT_STACK
, &rl
);
501 PyRun_SimpleString(("sys.path.append(\""+proc_self_dirname()+"\")").c_str());
502 PyRun_SimpleString(("sys.path.append(\""+proc_share_dirname()+"plugins\")").c_str());
504 log_error_atexit
= yosys_atexit
;
506 for (auto &fn
: plugin_filenames
)
509 if (optind
== argc
&& passes_commands
.size() == 0 && scriptfile
.empty()) {
510 if (!got_output_filename
)
511 backend_command
= "";
515 if (!vlog_defines
.empty()) {
516 std::string vdef_cmd
= "read -define";
517 for (auto vdef
: vlog_defines
)
518 vdef_cmd
+= " " + vdef
;
522 while (optind
< argc
)
523 run_frontend(argv
[optind
++], frontend_command
, output_filename
== "-" ? &backend_command
: NULL
);
525 if (!scriptfile
.empty()) {
526 if (scriptfile_tcl
) {
527 #ifdef YOSYS_ENABLE_TCL
528 if (Tcl_EvalFile(yosys_get_tcl_interp(), scriptfile
.c_str()) != TCL_OK
)
529 log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
531 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
534 run_frontend(scriptfile
, "script", output_filename
== "-" ? &backend_command
: NULL
);
537 for (auto it
= passes_commands
.begin(); it
!= passes_commands
.end(); it
++)
540 if (!backend_command
.empty())
541 run_backend(output_filename
, backend_command
);
543 yosys_design
->check();
544 for (auto it
: saved_designs
)
546 for (auto it
: pushed_designs
)
549 if (!depsfile
.empty())
551 FILE *f
= fopen(depsfile
.c_str(), "wt");
553 log_error("Can't open dependencies file for writing: %s\n", strerror(errno
));
555 for (auto fn
: yosys_output_files
) {
556 fprintf(f
, "%s%s", first
? "" : " ", escape_filename_spaces(fn
).c_str());
560 for (auto fn
: yosys_input_files
) {
561 if (yosys_output_files
.count(fn
) == 0)
562 fprintf(f
, " %s", escape_filename_spaces(fn
).c_str());
567 if (log_expect_no_warnings
&& log_warnings_count_noexpect
)
568 log_error("Unexpected warnings found: %d unique messages, %d total, %d expected\n", GetSize(log_warnings
),
569 log_warnings_count
, log_warnings_count
- log_warnings_count_noexpect
);
573 std::string hash
= log_hasher
->final().substr(0, 10);
575 log_hasher
= nullptr;
581 if (mode_v
&& !mode_q
)
582 log_files
.push_back(stderr
);
584 if (log_warnings_count
)
585 log("Warnings: %d unique messages, %d total\n", GetSize(log_warnings
), log_warnings_count
);
587 if (!log_experimentals
.empty())
588 log("Warnings: %d experimental features used (not excluded with -x).\n", GetSize(log_experimentals
));
591 log("End of script. Logfile hash: %s\n", hash
.c_str());
594 std::string stats_divider
= ", ";
596 struct rusage ru_buffer
;
597 getrusage(RUSAGE_SELF
, &ru_buffer
);
598 if (yosys_design
->scratchpad_get_bool("print_stats.include_children")) {
599 struct rusage ru_buffer_children
;
600 getrusage(RUSAGE_CHILDREN
, &ru_buffer_children
);
601 ru_buffer
.ru_utime
.tv_sec
+= ru_buffer_children
.ru_utime
.tv_sec
;
602 ru_buffer
.ru_utime
.tv_usec
+= ru_buffer_children
.ru_utime
.tv_usec
;
603 ru_buffer
.ru_stime
.tv_sec
+= ru_buffer_children
.ru_stime
.tv_sec
;
604 ru_buffer
.ru_stime
.tv_usec
+= ru_buffer_children
.ru_stime
.tv_usec
;
605 #if defined(__linux__) || defined(__FreeBSD__)
606 ru_buffer
.ru_maxrss
= std::max(ru_buffer
.ru_maxrss
, ru_buffer_children
.ru_maxrss
);
609 #if defined(__linux__) || defined(__FreeBSD__)
610 meminfo
= stringf(", MEM: %.2f MB peak",
611 ru_buffer
.ru_maxrss
/ 1024.0);
613 log("End of script. Logfile hash: %s%sCPU: user %.2fs system %.2fs%s\n", hash
.c_str(),
614 stats_divider
.c_str(), ru_buffer
.ru_utime
.tv_sec
+ 1e-6 * ru_buffer
.ru_utime
.tv_usec
,
615 ru_buffer
.ru_stime
.tv_sec
+ 1e-6 * ru_buffer
.ru_stime
.tv_usec
, meminfo
.c_str());
617 log("%s\n", yosys_version_str
);
619 int64_t total_ns
= 0;
620 std::set
<tuple
<int64_t, int, std::string
>> timedat
;
622 for (auto &it
: pass_register
)
623 if (it
.second
->call_counter
) {
624 total_ns
+= it
.second
->runtime_ns
+ 1;
625 timedat
.insert(make_tuple(it
.second
->runtime_ns
+ 1, it
.second
->call_counter
, it
.first
));
630 log("Time spent:\n");
631 for (auto it
= timedat
.rbegin(); it
!= timedat
.rend(); it
++) {
632 log("%5d%% %5d calls %8.3f sec %s\n", int(100*std::get
<0>(*it
) / total_ns
),
633 std::get
<1>(*it
), std::get
<0>(*it
) / 1000000000.0, std::get
<2>(*it
).c_str());
640 for (auto it
= timedat
.rbegin(); it
!= timedat
.rend() && out_count
< 4; it
++, out_count
++) {
641 if (out_count
>= 2 && (std::get
<0>(*it
) < 1000000000 || int(100*std::get
<0>(*it
) / total_ns
) < 20)) {
645 log("%s %d%% %dx %s (%d sec)", out_count
? "," : "", int(100*std::get
<0>(*it
) / total_ns
),
646 std::get
<1>(*it
), std::get
<2>(*it
).c_str(), int(std::get
<0>(*it
) / 1000000000));
648 log("%s\n", out_count
? "" : " no commands executed");
652 #if defined(YOSYS_ENABLE_COVER) && (defined(__linux__) || defined(__FreeBSD__))
653 if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
658 if (getenv("YOSYS_COVER_DIR")) {
659 filename
= stringf("%s/yosys_cover_%d_XXXXXX.txt", getenv("YOSYS_COVER_DIR"), getpid());
660 filename
= make_temp_file(filename
);
662 filename
= getenv("YOSYS_COVER_FILE");
665 f
= fopen(filename
.c_str(), "a+");
668 log_error("Can't create coverage file `%s'.\n", filename
.c_str());
670 log("<writing coverage file \"%s\">\n", filename
.c_str());
672 for (auto &it
: get_coverage_data())
673 fprintf(f
, "%-60s %10d %s\n", it
.second
.first
.c_str(), it
.second
.second
, it
.first
.c_str());
679 log_check_expected();
688 #if defined(_MSC_VER)
690 #elif defined(_WIN32)
699 #endif /* EMSCRIPTEN */