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>
34 # include <sys/types.h>
38 #if !defined(_WIN32) || defined(__MINGW32__)
42 int optind
= 1, optcur
= 1;
43 int getopt(int argc
, char **argv
, const char *optstring
)
45 if (optind
>= argc
|| argv
[optind
][0] != '-')
48 bool takes_arg
= false;
49 int opt
= argv
[optind
][optcur
];
50 for (int i
= 0; optstring
[i
]; i
++)
51 if (opt
== optstring
[i
] && optstring
[i
+ 1] == ':')
55 if (argv
[optind
][++optcur
] == 0)
60 if (argv
[optind
][++optcur
]) {
61 optarg
= argv
[optind
++] + optcur
;
66 optarg
= argv
[++optind
];
76 # include <sys/stat.h>
77 # include <sys/types.h>
79 extern "C" int main(int, char**);
80 extern "C" void run(const char*);
81 extern "C" const char *errmsg();
82 extern "C" const char *prompt();
88 log_files
.push_back(stdout
);
89 log_error_stderr
= true;
94 void run(const char *command
)
96 int selSize
= GetSize(yosys_get_design()->selection_stack
);
98 log_last_error
= "Internal error (see JavaScript console for details)";
102 while (GetSize(yosys_get_design()->selection_stack
) > selSize
)
103 yosys_get_design()->selection_stack
.pop_back();
110 return log_last_error
.c_str();
115 const char *p
= create_prompt(yosys_get_design(), 0);
116 while (*p
== '\n') p
++;
120 #else /* EMSCRIPTEN */
122 int main(int argc
, char **argv
)
124 std::string frontend_command
= "auto";
125 std::string backend_command
= "auto";
126 std::vector
<std::string
> passes_commands
;
127 std::vector
<std::string
> plugin_filenames
;
128 std::string output_filename
= "";
129 std::string scriptfile
= "";
130 bool scriptfile_tcl
= false;
131 bool got_output_filename
= false;
132 bool print_banner
= true;
133 bool print_stats
= true;
134 bool call_abort
= false;
135 bool timing_details
= false;
139 #ifdef YOSYS_ENABLE_READLINE
140 int history_offset
= 0;
141 std::string history_file
;
142 if (getenv("HOME") != NULL
) {
143 history_file
= stringf("%s/.yosys_history", getenv("HOME"));
144 read_history(history_file
.c_str());
145 history_offset
= where_history();
149 if (argc
== 2 && (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "-help") || !strcmp(argv
[1], "--help")))
152 printf("Usage: %s [options] [<infile> [..]]\n", argv
[0]);
155 printf(" suppress printing of banner (copyright, disclaimer, version)\n");
158 printf(" suppress printing of footer (log hash, version, timing statistics)\n");
161 printf(" quiet operation. only write warnings and error messages to console\n");
162 printf(" use this option twice to also quiet warning messages\n");
164 printf(" -v <level>\n");
165 printf(" print log headers up to level <level> to the console. (this\n");
166 printf(" implies -q for everything except the 'End of script.' message.)\n");
169 printf(" annotate all log messages with a time stamp\n");
172 printf(" print more detailed timing stats at exit\n");
174 printf(" -l logfile\n");
175 printf(" write log messages to the specified file\n");
177 printf(" -L logfile\n");
178 printf(" like -l but open log file in line buffered mode\n");
180 printf(" -o outfile\n");
181 printf(" write the design to the specified file on exit\n");
183 printf(" -b backend\n");
184 printf(" use this backend for the output file specified on the command line\n");
186 printf(" -f frontend\n");
187 printf(" use the specified frontend for the input files on the command line\n");
190 printf(" print the command list\n");
192 printf(" -h command\n");
193 printf(" print the help message for the specified command\n");
195 printf(" -s scriptfile\n");
196 printf(" execute the commands in the script file\n");
198 printf(" -c tcl_scriptfile\n");
199 printf(" execute the commands in the tcl script file (see 'help tcl' for details)\n");
201 printf(" -p command\n");
202 printf(" execute the commands\n");
204 printf(" -m module_file\n");
205 printf(" load the specified module (aka plugin)\n");
208 printf(" enable tracing of core data structure changes. for debugging\n");
211 printf(" will slightly randomize allocated pointer addresses. for debugging\n");
214 printf(" will call abort() at the end of the script. for debugging\n");
216 printf(" -D <header_id>[:<filename>]\n");
217 printf(" dump the design when printing the specified log header to a file.\n");
218 printf(" yosys_dump_<header_id>.il is used as filename if none is specified.\n");
219 printf(" Use 'ALL' as <header_id> to dump at every header.\n");
221 printf(" -W regex\n");
222 printf(" print a warning for all log messages matching the regex.\n");
224 printf(" -w regex\n");
225 printf(" if a warning message matches the regex, it is printes as regular\n");
226 printf(" message instead.\n");
229 printf(" print version information and exit\n");
231 printf("The option -S is an shortcut for calling the \"synth\" command, a default\n");
232 printf("script for transforming the Verilog input to a gate-level netlist. For example:\n");
234 printf(" yosys -o output.blif -S input.v\n");
236 printf("For more complex synthesis jobs it is recommended to use the read_* and write_*\n");
237 printf("commands in a script file instead of specifying input and output files on the\n");
238 printf("command line.\n");
240 printf("When no commands, script files or input files are specified on the command\n");
241 printf("line, yosys automatically enters the interactive command mode. Use the 'help'\n");
242 printf("command to get information on the individual commands.\n");
248 while ((opt
= getopt(argc
, argv
, "MXAQTVSm:f:Hh:b:o:p:l:L:qv:tds:c:W:w:D:")) != -1)
262 print_banner
= false;
268 printf("%s\n", yosys_version_str
);
271 passes_commands
.push_back("synth");
274 plugin_filenames
.push_back(optarg
);
277 frontend_command
= optarg
;
280 passes_commands
.push_back("help");
283 passes_commands
.push_back(stringf("help %s", optarg
));
286 backend_command
= optarg
;
289 passes_commands
.push_back(optarg
);
292 output_filename
= optarg
;
293 got_output_filename
= true;
297 log_files
.push_back(fopen(optarg
, "wt"));
298 if (log_files
.back() == NULL
) {
299 fprintf(stderr
, "Can't open log file `%s' for writing!\n", optarg
);
303 setvbuf(log_files
.back(), NULL
, _IOLBF
, 0);
307 if (log_errfile
== stderr
)
308 log_quiet_warnings
= true;
309 log_errfile
= stderr
;
313 log_errfile
= stderr
;
314 log_verbose_level
= atoi(optarg
);
320 timing_details
= true;
324 scriptfile_tcl
= false;
328 scriptfile_tcl
= true;
331 log_warn_regexes
.push_back(std::regex(optarg
,
332 std::regex_constants::nosubs
|
333 std::regex_constants::optimize
|
334 std::regex_constants::egrep
));
337 log_nowarn_regexes
.push_back(std::regex(optarg
,
338 std::regex_constants::nosubs
|
339 std::regex_constants::optimize
|
340 std::regex_constants::egrep
));
344 auto args
= split_tokens(optarg
, ":");
345 if (!args
.empty() && args
[0] == "ALL") {
346 if (GetSize(args
) != 1) {
347 fprintf(stderr
, "Invalid number of tokens in -D ALL.\n");
350 log_hdump_all
= true;
352 if (!args
.empty() && !args
[0].empty() && args
[0].back() == '.')
354 if (GetSize(args
) == 1)
355 args
.push_back("yosys_dump_" + args
[0] + ".il");
356 if (GetSize(args
) != 2) {
357 fprintf(stderr
, "Invalid number of tokens in -D.\n");
360 log_hdump
[args
[0]].insert(args
[1]);
365 fprintf(stderr
, "Run '%s -h' for help.\n", argv
[0]);
370 if (log_errfile
== NULL
) {
371 log_files
.push_back(stdout
);
372 log_error_stderr
= true;
379 log_hasher
= new SHA1
;
383 for (auto &fn
: plugin_filenames
)
386 if (optind
== argc
&& passes_commands
.size() == 0 && scriptfile
.empty()) {
387 if (!got_output_filename
)
388 backend_command
= "";
392 while (optind
< argc
)
393 run_frontend(argv
[optind
++], frontend_command
, output_filename
== "-" ? &backend_command
: NULL
);
395 if (!scriptfile
.empty()) {
396 if (scriptfile_tcl
) {
397 #ifdef YOSYS_ENABLE_TCL
398 if (Tcl_EvalFile(yosys_get_tcl_interp(), scriptfile
.c_str()) != TCL_OK
)
399 log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
401 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
404 run_frontend(scriptfile
, "script", output_filename
== "-" ? &backend_command
: NULL
);
407 for (auto it
= passes_commands
.begin(); it
!= passes_commands
.end(); it
++)
410 if (!backend_command
.empty())
411 run_backend(output_filename
, backend_command
);
415 std::string hash
= log_hasher
->final().substr(0, 10);
417 log_hasher
= nullptr;
423 if (mode_v
&& !mode_q
)
424 log_files
.push_back(stderr
);
427 log("End of script. Logfile hash: %s\n", hash
.c_str());
430 std::string stats_divider
= ", ";
433 statm
.open(stringf("/proc/%lld/statm", (long long)getpid()));
434 if (statm
.is_open()) {
435 int sz_total
, sz_resident
;
436 statm
>> sz_total
>> sz_resident
;
437 meminfo
= stringf(", MEM: %.2f MB total, %.2f MB resident",
438 sz_total
* (getpagesize() / 1024.0 / 1024.0),
439 sz_resident
* (getpagesize() / 1024.0 / 1024.0));
440 stats_divider
= "\n";
444 struct rusage ru_buffer
;
445 getrusage(RUSAGE_SELF
, &ru_buffer
);
446 log("End of script. Logfile hash: %s%sCPU: user %.2fs system %.2fs%s\n", hash
.c_str(),
447 stats_divider
.c_str(), ru_buffer
.ru_utime
.tv_sec
+ 1e-6 * ru_buffer
.ru_utime
.tv_usec
,
448 ru_buffer
.ru_stime
.tv_sec
+ 1e-6 * ru_buffer
.ru_stime
.tv_usec
, meminfo
.c_str());
450 log("%s\n", yosys_version_str
);
452 int64_t total_ns
= 0;
453 std::set
<tuple
<int64_t, int, std::string
>> timedat
;
455 for (auto &it
: pass_register
)
456 if (it
.second
->call_counter
) {
457 total_ns
+= it
.second
->runtime_ns
+ 1;
458 timedat
.insert(make_tuple(it
.second
->runtime_ns
+ 1, it
.second
->call_counter
, it
.first
));
463 log("Time spent:\n");
464 for (auto it
= timedat
.rbegin(); it
!= timedat
.rend(); it
++) {
465 log("%5d%% %5d calls %8.3f sec %s\n", int(100*std::get
<0>(*it
) / total_ns
),
466 std::get
<1>(*it
), std::get
<0>(*it
) / 1000000000.0, std::get
<2>(*it
).c_str());
473 for (auto it
= timedat
.rbegin(); it
!= timedat
.rend() && out_count
< 4; it
++, out_count
++) {
474 if (out_count
>= 2 && (std::get
<0>(*it
) < 1000000000 || int(100*std::get
<0>(*it
) / total_ns
) < 20)) {
478 log("%s %d%% %dx %s (%d sec)", out_count
? "," : "", int(100*std::get
<0>(*it
) / total_ns
),
479 std::get
<1>(*it
), std::get
<2>(*it
).c_str(), int(std::get
<0>(*it
) / 1000000000));
481 log("%s\n", out_count
? "" : " no commands executed");
485 #if defined(YOSYS_ENABLE_COVER) && defined(__linux__)
486 if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
491 if (getenv("YOSYS_COVER_DIR")) {
492 filename
= stringf("%s/yosys_cover_%d_XXXXXX.txt", getenv("YOSYS_COVER_DIR"), getpid());
493 filename
= make_temp_file(filename
);
495 filename
= getenv("YOSYS_COVER_FILE");
498 f
= fopen(filename
.c_str(), "a+");
501 log_error("Can't create coverage file `%s'.\n", filename
.c_str());
503 log("<writing coverage file \"%s\">\n", filename
.c_str());
505 for (auto &it
: get_coverage_data())
506 fprintf(f
, "%-60s %10d %s\n", it
.second
.first
.c_str(), it
.second
.second
, it
.first
.c_str());
516 #ifdef YOSYS_ENABLE_READLINE
517 if (!history_file
.empty()) {
518 if (history_offset
> 0) {
519 history_truncate_file(history_file
.c_str(), 100);
520 append_history(where_history() - history_offset
, history_file
.c_str());
522 write_history(history_file
.c_str());
526 HIST_ENTRY
**hist_list
= history_list();
527 if (hist_list
!= NULL
)
532 #if defined(_MSC_VER)
534 #elif defined(_WIN32)
543 #endif /* EMSCRIPTEN */