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>
89 extern "C" int main(int, char**);
90 extern "C" void run(const char*);
91 extern "C" const char *errmsg();
92 extern "C" const char *prompt();
98 log_files
.push_back(stdout
);
99 log_error_stderr
= true;
104 void run(const char *command
)
106 int selSize
= GetSize(yosys_get_design()->selection_stack
);
108 log_last_error
= "Internal error (see JavaScript console for details)";
112 while (GetSize(yosys_get_design()->selection_stack
) > selSize
)
113 yosys_get_design()->selection_stack
.pop_back();
120 return log_last_error
.c_str();
125 const char *p
= create_prompt(yosys_get_design(), 0);
126 while (*p
== '\n') p
++;
130 #else /* EMSCRIPTEN */
132 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
133 int yosys_history_offset
= 0;
134 std::string yosys_history_file
;
139 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
140 if (!yosys_history_file
.empty()) {
141 #if defined(YOSYS_ENABLE_READLINE)
142 if (yosys_history_offset
> 0) {
143 history_truncate_file(yosys_history_file
.c_str(), 100);
144 append_history(where_history() - yosys_history_offset
, yosys_history_file
.c_str());
146 write_history(yosys_history_file
.c_str());
148 write_history(yosys_history_file
.c_str());
153 #if defined(YOSYS_ENABLE_READLINE)
154 HIST_ENTRY
**hist_list
= history_list();
155 if (hist_list
!= NULL
)
161 int main(int argc
, char **argv
)
163 std::string frontend_command
= "auto";
164 std::string backend_command
= "auto";
165 std::vector
<std::string
> passes_commands
;
166 std::vector
<std::string
> plugin_filenames
;
167 std::string output_filename
= "";
168 std::string scriptfile
= "";
169 std::string depsfile
= "";
170 bool scriptfile_tcl
= false;
171 bool got_output_filename
= false;
172 bool print_banner
= true;
173 bool print_stats
= true;
174 bool call_abort
= false;
175 bool timing_details
= false;
179 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
180 if (getenv("HOME") != NULL
) {
181 yosys_history_file
= stringf("%s/.yosys_history", getenv("HOME"));
182 read_history(yosys_history_file
.c_str());
183 yosys_history_offset
= where_history();
187 if (argc
== 2 && (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "-help") || !strcmp(argv
[1], "--help")))
190 printf("Usage: %s [options] [<infile> [..]]\n", argv
[0]);
193 printf(" suppress printing of banner (copyright, disclaimer, version)\n");
196 printf(" suppress printing of footer (log hash, version, timing statistics)\n");
199 printf(" quiet operation. only write warnings and error messages to console\n");
200 printf(" use this option twice to also quiet warning messages\n");
202 printf(" -v <level>\n");
203 printf(" print log headers up to level <level> to the console. (this\n");
204 printf(" implies -q for everything except the 'End of script.' message.)\n");
207 printf(" annotate all log messages with a time stamp\n");
210 printf(" print more detailed timing stats at exit\n");
212 printf(" -l logfile\n");
213 printf(" write log messages to the specified file\n");
215 printf(" -L logfile\n");
216 printf(" like -l but open log file in line buffered mode\n");
218 printf(" -o outfile\n");
219 printf(" write the design to the specified file on exit\n");
221 printf(" -b backend\n");
222 printf(" use this backend for the output file specified on the command line\n");
224 printf(" -f frontend\n");
225 printf(" use the specified frontend for the input files on the command line\n");
228 printf(" print the command list\n");
230 printf(" -h command\n");
231 printf(" print the help message for the specified command\n");
233 printf(" -s scriptfile\n");
234 printf(" execute the commands in the script file\n");
236 printf(" -c tcl_scriptfile\n");
237 printf(" execute the commands in the tcl script file (see 'help tcl' for details)\n");
239 printf(" -p command\n");
240 printf(" execute the commands\n");
242 printf(" -m module_file\n");
243 printf(" load the specified module (aka plugin)\n");
246 printf(" enable tracing of core data structure changes. for debugging\n");
249 printf(" will slightly randomize allocated pointer addresses. for debugging\n");
252 printf(" will call abort() at the end of the script. for debugging\n");
254 printf(" -D <header_id>[:<filename>]\n");
255 printf(" dump the design when printing the specified log header to a file.\n");
256 printf(" yosys_dump_<header_id>.il is used as filename if none is specified.\n");
257 printf(" Use 'ALL' as <header_id> to dump at every header.\n");
259 printf(" -W regex\n");
260 printf(" print a warning for all log messages matching the regex.\n");
262 printf(" -w regex\n");
263 printf(" if a warning message matches the regex, it is printed as regular\n");
264 printf(" message instead.\n");
266 printf(" -e regex\n");
267 printf(" if a warning message matches the regex, it is printed as error\n");
268 printf(" message instead and the tool terminates with a nonzero return code.\n");
270 printf(" -E <depsfile>\n");
271 printf(" write a Makefile dependencies file with in- and output file names\n");
274 printf(" print version information and exit\n");
276 printf("The option -S is an shortcut for calling the \"synth\" command, a default\n");
277 printf("script for transforming the Verilog input to a gate-level netlist. For example:\n");
279 printf(" yosys -o output.blif -S input.v\n");
281 printf("For more complex synthesis jobs it is recommended to use the read_* and write_*\n");
282 printf("commands in a script file instead of specifying input and output files on the\n");
283 printf("command line.\n");
285 printf("When no commands, script files or input files are specified on the command\n");
286 printf("line, yosys automatically enters the interactive command mode. Use the 'help'\n");
287 printf("command to get information on the individual commands.\n");
293 while ((opt
= getopt(argc
, argv
, "MXAQTVSm:f:Hh:b:o:p:l:L:qv:tds:c:W:w:e:D:E:")) != -1)
307 print_banner
= false;
313 printf("%s\n", yosys_version_str
);
316 passes_commands
.push_back("synth");
319 plugin_filenames
.push_back(optarg
);
322 frontend_command
= optarg
;
325 passes_commands
.push_back("help");
328 passes_commands
.push_back(stringf("help %s", optarg
));
331 backend_command
= optarg
;
334 passes_commands
.push_back(optarg
);
337 output_filename
= optarg
;
338 got_output_filename
= true;
342 log_files
.push_back(fopen(optarg
, "wt"));
343 if (log_files
.back() == NULL
) {
344 fprintf(stderr
, "Can't open log file `%s' for writing!\n", optarg
);
348 setvbuf(log_files
.back(), NULL
, _IOLBF
, 0);
352 if (log_errfile
== stderr
)
353 log_quiet_warnings
= true;
354 log_errfile
= stderr
;
358 log_errfile
= stderr
;
359 log_verbose_level
= atoi(optarg
);
365 timing_details
= true;
369 scriptfile_tcl
= false;
373 scriptfile_tcl
= true;
376 log_warn_regexes
.push_back(std::regex(optarg
,
377 std::regex_constants::nosubs
|
378 std::regex_constants::optimize
|
379 std::regex_constants::egrep
));
382 log_nowarn_regexes
.push_back(std::regex(optarg
,
383 std::regex_constants::nosubs
|
384 std::regex_constants::optimize
|
385 std::regex_constants::egrep
));
388 log_werror_regexes
.push_back(std::regex(optarg
,
389 std::regex_constants::nosubs
|
390 std::regex_constants::optimize
|
391 std::regex_constants::egrep
));
395 auto args
= split_tokens(optarg
, ":");
396 if (!args
.empty() && args
[0] == "ALL") {
397 if (GetSize(args
) != 1) {
398 fprintf(stderr
, "Invalid number of tokens in -D ALL.\n");
401 log_hdump_all
= true;
403 if (!args
.empty() && !args
[0].empty() && args
[0].back() == '.')
405 if (GetSize(args
) == 1)
406 args
.push_back("yosys_dump_" + args
[0] + ".il");
407 if (GetSize(args
) != 2) {
408 fprintf(stderr
, "Invalid number of tokens in -D.\n");
411 log_hdump
[args
[0]].insert(args
[1]);
419 fprintf(stderr
, "Run '%s -h' for help.\n", argv
[0]);
424 if (log_errfile
== NULL
) {
425 log_files
.push_back(stdout
);
426 log_error_stderr
= true;
433 log_hasher
= new SHA1
;
435 #if defined(__linux__)
436 // set stack size to >= 128 MB
439 const rlim_t stack_size
= 128L * 1024L * 1024L;
440 if (getrlimit(RLIMIT_STACK
, &rl
) == 0 && rl
.rlim_cur
< stack_size
) {
441 rl
.rlim_cur
= stack_size
;
442 setrlimit(RLIMIT_STACK
, &rl
);
448 log_error_atexit
= yosys_atexit
;
450 for (auto &fn
: plugin_filenames
)
453 if (optind
== argc
&& passes_commands
.size() == 0 && scriptfile
.empty()) {
454 if (!got_output_filename
)
455 backend_command
= "";
459 while (optind
< argc
)
460 run_frontend(argv
[optind
++], frontend_command
, output_filename
== "-" ? &backend_command
: NULL
);
462 if (!scriptfile
.empty()) {
463 if (scriptfile_tcl
) {
464 #ifdef YOSYS_ENABLE_TCL
465 if (Tcl_EvalFile(yosys_get_tcl_interp(), scriptfile
.c_str()) != TCL_OK
)
466 log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
468 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
471 run_frontend(scriptfile
, "script", output_filename
== "-" ? &backend_command
: NULL
);
474 for (auto it
= passes_commands
.begin(); it
!= passes_commands
.end(); it
++)
477 if (!backend_command
.empty())
478 run_backend(output_filename
, backend_command
);
480 if (!depsfile
.empty())
482 FILE *f
= fopen(depsfile
.c_str(), "wt");
484 log_error("Can't open dependencies file for writing: %s\n", strerror(errno
));
486 for (auto fn
: yosys_output_files
) {
487 fprintf(f
, "%s%s", first
? "" : " ", fn
.c_str());
491 for (auto fn
: yosys_input_files
) {
492 if (yosys_output_files
.count(fn
) == 0)
493 fprintf(f
, " %s", fn
.c_str());
500 std::string hash
= log_hasher
->final().substr(0, 10);
502 log_hasher
= nullptr;
508 if (mode_v
&& !mode_q
)
509 log_files
.push_back(stderr
);
511 if (log_warnings_count
)
512 log("Warnings: %d unique messages, %d total\n", GetSize(log_warnings
), log_warnings_count
);
514 log("End of script. Logfile hash: %s\n", hash
.c_str());
517 std::string stats_divider
= ", ";
518 # if defined(__linux__)
520 statm
.open(stringf("/proc/%lld/statm", (long long)getpid()));
521 if (statm
.is_open()) {
522 int sz_total
, sz_resident
;
523 statm
>> sz_total
>> sz_resident
;
524 meminfo
= stringf(", MEM: %.2f MB total, %.2f MB resident",
525 sz_total
* (getpagesize() / 1024.0 / 1024.0),
526 sz_resident
* (getpagesize() / 1024.0 / 1024.0));
527 stats_divider
= "\n";
529 # elif defined(__FreeBSD__)
530 pid_t pid
= getpid();
531 int mib
[4] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PID
, (int)pid
};
532 struct kinfo_proc kip
;
533 size_t kip_len
= sizeof(kip
);
534 if (sysctl(mib
, 4, &kip
, &kip_len
, NULL
, 0) == 0) {
535 vm_size_t sz_total
= kip
.ki_size
;
536 segsz_t sz_resident
= kip
.ki_rssize
;
537 meminfo
= stringf(", MEM: %.2f MB total, %.2f MB resident",
538 (int)sz_total
/ 1024.0 / 1024.0,
539 (int)sz_resident
* (getpagesize() / 1024.0 / 1024.0));
540 stats_divider
= "\n";
544 struct rusage ru_buffer
;
545 getrusage(RUSAGE_SELF
, &ru_buffer
);
546 log("End of script. Logfile hash: %s%sCPU: user %.2fs system %.2fs%s\n", hash
.c_str(),
547 stats_divider
.c_str(), ru_buffer
.ru_utime
.tv_sec
+ 1e-6 * ru_buffer
.ru_utime
.tv_usec
,
548 ru_buffer
.ru_stime
.tv_sec
+ 1e-6 * ru_buffer
.ru_stime
.tv_usec
, meminfo
.c_str());
550 log("%s\n", yosys_version_str
);
552 int64_t total_ns
= 0;
553 std::set
<tuple
<int64_t, int, std::string
>> timedat
;
555 for (auto &it
: pass_register
)
556 if (it
.second
->call_counter
) {
557 total_ns
+= it
.second
->runtime_ns
+ 1;
558 timedat
.insert(make_tuple(it
.second
->runtime_ns
+ 1, it
.second
->call_counter
, it
.first
));
563 log("Time spent:\n");
564 for (auto it
= timedat
.rbegin(); it
!= timedat
.rend(); it
++) {
565 log("%5d%% %5d calls %8.3f sec %s\n", int(100*std::get
<0>(*it
) / total_ns
),
566 std::get
<1>(*it
), std::get
<0>(*it
) / 1000000000.0, std::get
<2>(*it
).c_str());
573 for (auto it
= timedat
.rbegin(); it
!= timedat
.rend() && out_count
< 4; it
++, out_count
++) {
574 if (out_count
>= 2 && (std::get
<0>(*it
) < 1000000000 || int(100*std::get
<0>(*it
) / total_ns
) < 20)) {
578 log("%s %d%% %dx %s (%d sec)", out_count
? "," : "", int(100*std::get
<0>(*it
) / total_ns
),
579 std::get
<1>(*it
), std::get
<2>(*it
).c_str(), int(std::get
<0>(*it
) / 1000000000));
581 log("%s\n", out_count
? "" : " no commands executed");
585 #if defined(YOSYS_ENABLE_COVER) && (defined(__linux__) || defined(__FreeBSD__))
586 if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
591 if (getenv("YOSYS_COVER_DIR")) {
592 filename
= stringf("%s/yosys_cover_%d_XXXXXX.txt", getenv("YOSYS_COVER_DIR"), getpid());
593 filename
= make_temp_file(filename
);
595 filename
= getenv("YOSYS_COVER_FILE");
598 f
= fopen(filename
.c_str(), "a+");
601 log_error("Can't create coverage file `%s'.\n", filename
.c_str());
603 log("<writing coverage file \"%s\">\n", filename
.c_str());
605 for (auto &it
: get_coverage_data())
606 fprintf(f
, "%-60s %10d %s\n", it
.second
.first
.c_str(), it
.second
.second
, it
.first
.c_str());
619 #if defined(_MSC_VER)
621 #elif defined(_WIN32)
630 #endif /* EMSCRIPTEN */