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>
38 # include <sys/resource.h>
39 # include <sys/types.h>
43 #if !defined(_WIN32) || defined(__MINGW32__)
47 int optind
= 1, optcur
= 1;
48 int getopt(int argc
, char **argv
, const char *optstring
)
50 if (optind
>= argc
|| argv
[optind
][0] != '-')
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] == ':')
60 if (argv
[optind
][++optcur
] == 0)
65 if (argv
[optind
][++optcur
]) {
66 optarg
= argv
[optind
++] + optcur
;
71 optarg
= argv
[++optind
];
81 # include <sys/stat.h>
82 # include <sys/types.h>
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();
93 log_files
.push_back(stdout
);
94 log_error_stderr
= true;
99 void run(const char *command
)
101 int selSize
= GetSize(yosys_get_design()->selection_stack
);
103 log_last_error
= "Internal error (see JavaScript console for details)";
107 while (GetSize(yosys_get_design()->selection_stack
) > selSize
)
108 yosys_get_design()->selection_stack
.pop_back();
115 return log_last_error
.c_str();
120 const char *p
= create_prompt(yosys_get_design(), 0);
121 while (*p
== '\n') p
++;
125 #else /* EMSCRIPTEN */
127 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
128 int yosys_history_offset
= 0;
129 std::string yosys_history_file
;
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());
141 write_history(yosys_history_file
.c_str());
143 write_history(yosys_history_file
.c_str());
148 #if defined(YOSYS_ENABLE_READLINE)
149 HIST_ENTRY
**hist_list
= history_list();
150 if (hist_list
!= NULL
)
156 int main(int argc
, char **argv
)
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;
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();
182 if (argc
== 2 && (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "-help") || !strcmp(argv
[1], "--help")))
185 printf("Usage: %s [options] [<infile> [..]]\n", argv
[0]);
188 printf(" suppress printing of banner (copyright, disclaimer, version)\n");
191 printf(" suppress printing of footer (log hash, version, timing statistics)\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");
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");
202 printf(" annotate all log messages with a time stamp\n");
205 printf(" print more detailed timing stats at exit\n");
207 printf(" -l logfile\n");
208 printf(" write log messages to the specified file\n");
210 printf(" -L logfile\n");
211 printf(" like -l but open log file in line buffered mode\n");
213 printf(" -o outfile\n");
214 printf(" write the design to the specified file on exit\n");
216 printf(" -b backend\n");
217 printf(" use this backend for the output file specified on the command line\n");
219 printf(" -f frontend\n");
220 printf(" use the specified frontend for the input files on the command line\n");
223 printf(" print the command list\n");
225 printf(" -h command\n");
226 printf(" print the help message for the specified command\n");
228 printf(" -s scriptfile\n");
229 printf(" execute the commands in the script file\n");
231 printf(" -c tcl_scriptfile\n");
232 printf(" execute the commands in the tcl script file (see 'help tcl' for details)\n");
234 printf(" -p command\n");
235 printf(" execute the commands\n");
237 printf(" -m module_file\n");
238 printf(" load the specified module (aka plugin)\n");
241 printf(" enable tracing of core data structure changes. for debugging\n");
244 printf(" will slightly randomize allocated pointer addresses. for debugging\n");
247 printf(" will call abort() at the end of the script. for debugging\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");
254 printf(" -W regex\n");
255 printf(" print a warning for all log messages matching the regex.\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");
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");
265 printf(" -E <depsfile>\n");
266 printf(" write a Makefile dependencies file with in- and output file names\n");
269 printf(" print version information and exit\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");
274 printf(" yosys -o output.blif -S input.v\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");
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");
288 while ((opt
= getopt(argc
, argv
, "MXAQTVSm:f:Hh:b:o:p:l:L:qv:tds:c:W:w:e:D:E:")) != -1)
302 print_banner
= false;
308 printf("%s\n", yosys_version_str
);
311 passes_commands
.push_back("synth");
314 plugin_filenames
.push_back(optarg
);
317 frontend_command
= optarg
;
320 passes_commands
.push_back("help");
323 passes_commands
.push_back(stringf("help %s", optarg
));
326 backend_command
= optarg
;
329 passes_commands
.push_back(optarg
);
332 output_filename
= optarg
;
333 got_output_filename
= true;
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
);
343 setvbuf(log_files
.back(), NULL
, _IOLBF
, 0);
347 if (log_errfile
== stderr
)
348 log_quiet_warnings
= true;
349 log_errfile
= stderr
;
353 log_errfile
= stderr
;
354 log_verbose_level
= atoi(optarg
);
360 timing_details
= true;
364 scriptfile_tcl
= false;
368 scriptfile_tcl
= true;
371 log_warn_regexes
.push_back(std::regex(optarg
,
372 std::regex_constants::nosubs
|
373 std::regex_constants::optimize
|
374 std::regex_constants::egrep
));
377 log_nowarn_regexes
.push_back(std::regex(optarg
,
378 std::regex_constants::nosubs
|
379 std::regex_constants::optimize
|
380 std::regex_constants::egrep
));
383 log_werror_regexes
.push_back(std::regex(optarg
,
384 std::regex_constants::nosubs
|
385 std::regex_constants::optimize
|
386 std::regex_constants::egrep
));
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");
396 log_hdump_all
= true;
398 if (!args
.empty() && !args
[0].empty() && args
[0].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");
406 log_hdump
[args
[0]].insert(args
[1]);
414 fprintf(stderr
, "Run '%s -h' for help.\n", argv
[0]);
419 if (log_errfile
== NULL
) {
420 log_files
.push_back(stdout
);
421 log_error_stderr
= true;
428 log_hasher
= new SHA1
;
430 #if defined(__linux__)
431 // set stack size to >= 128 MB
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
);
443 log_error_atexit
= yosys_atexit
;
445 for (auto &fn
: plugin_filenames
)
448 if (optind
== argc
&& passes_commands
.size() == 0 && scriptfile
.empty()) {
449 if (!got_output_filename
)
450 backend_command
= "";
454 while (optind
< argc
)
455 run_frontend(argv
[optind
++], frontend_command
, output_filename
== "-" ? &backend_command
: NULL
);
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()));
463 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
466 run_frontend(scriptfile
, "script", output_filename
== "-" ? &backend_command
: NULL
);
469 for (auto it
= passes_commands
.begin(); it
!= passes_commands
.end(); it
++)
472 if (!backend_command
.empty())
473 run_backend(output_filename
, backend_command
);
475 if (!depsfile
.empty())
477 FILE *f
= fopen(depsfile
.c_str(), "wt");
479 log_error("Can't open dependencies file for writing: %s\n", strerror(errno
));
481 for (auto fn
: yosys_output_files
) {
482 fprintf(f
, "%s%s", first
? "" : " ", fn
.c_str());
486 for (auto fn
: yosys_input_files
) {
487 if (yosys_output_files
.count(fn
) == 0)
488 fprintf(f
, " %s", fn
.c_str());
495 std::string hash
= log_hasher
->final().substr(0, 10);
497 log_hasher
= nullptr;
503 if (mode_v
&& !mode_q
)
504 log_files
.push_back(stderr
);
506 if (log_warnings_count
)
507 log("Warnings: %d unique messages, %d total\n", GetSize(log_warnings
), log_warnings_count
);
509 log("End of script. Logfile hash: %s\n", hash
.c_str());
512 std::string stats_divider
= ", ";
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";
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());
532 log("%s\n", yosys_version_str
);
534 int64_t total_ns
= 0;
535 std::set
<tuple
<int64_t, int, std::string
>> timedat
;
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
));
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());
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)) {
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));
563 log("%s\n", out_count
? "" : " no commands executed");
567 #if defined(YOSYS_ENABLE_COVER) && defined(__linux__)
568 if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
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
);
577 filename
= getenv("YOSYS_COVER_FILE");
580 f
= fopen(filename
.c_str(), "a+");
583 log_error("Can't create coverage file `%s'.\n", filename
.c_str());
585 log("<writing coverage file \"%s\">\n", filename
.c_str());
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());
601 #if defined(_MSC_VER)
603 #elif defined(_WIN32)
612 #endif /* EMSCRIPTEN */