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
];
75 int main(int argc
, char **argv
)
77 std::string frontend_command
= "auto";
78 std::string backend_command
= "auto";
79 std::vector
<std::string
> passes_commands
;
80 std::vector
<std::string
> plugin_filenames
;
81 std::string output_filename
= "";
82 std::string scriptfile
= "";
83 bool scriptfile_tcl
= false;
84 bool got_output_filename
= false;
85 bool print_banner
= true;
86 bool print_stats
= true;
87 bool call_abort
= false;
88 bool timing_details
= false;
90 #ifdef YOSYS_ENABLE_READLINE
91 int history_offset
= 0;
92 std::string history_file
;
93 if (getenv("HOME") != NULL
) {
94 history_file
= stringf("%s/.yosys_history", getenv("HOME"));
95 read_history(history_file
.c_str());
96 history_offset
= where_history();
100 if (argc
== 2 && (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "-help") || !strcmp(argv
[1], "--help")))
103 printf("Usage: %s [options] [<infile> [..]]\n", argv
[0]);
106 printf(" suppress printing of banner (copyright, disclaimer, version)\n");
109 printf(" suppress printing of footer (log hash, version, timing statistics)\n");
112 printf(" quiet operation. only write warnings and error messages to console\n");
113 printf(" use this option twice to also quiet warning messages\n");
115 printf(" -v <level>\n");
116 printf(" print log headers up to level <level> to the console. (implies -q)\n");
119 printf(" annotate all log messages with a time stamp\n");
122 printf(" print more detailed timing stats at exit\n");
124 printf(" -l logfile\n");
125 printf(" write log messages to the specified file\n");
127 printf(" -o outfile\n");
128 printf(" write the design to the specified file on exit\n");
130 printf(" -b backend\n");
131 printf(" use this backend for the output file specified on the command line\n");
133 printf(" -f backend\n");
134 printf(" use the specified front for the input files on the command line\n");
137 printf(" print the command list\n");
139 printf(" -h command\n");
140 printf(" print the help message for the specified command\n");
142 printf(" -s scriptfile\n");
143 printf(" execute the commands in the script file\n");
145 printf(" -c tcl_scriptfile\n");
146 printf(" execute the commands in the tcl script file (see 'help tcl' for details)\n");
148 printf(" -p command\n");
149 printf(" execute the commands\n");
151 printf(" -m module_file\n");
152 printf(" load the specified module (aka plugin)\n");
155 printf(" enable tracing of core data structure changes. for debugging\n");
158 printf(" will slightly randomize allocated pointer addresses. for debugging\n");
161 printf(" will call abort() at the end of the script. for debugging\n");
164 printf(" print version information and exit\n");
166 printf("The option -S is an shortcut for calling the \"synth\" command, a default\n");
167 printf("script for transforming the verilog input to a gate-level netlist. For example:\n");
169 printf(" yosys -o output.blif -S input.v\n");
171 printf("For more complex synthesis jobs it is recommended to use the read_* and write_*\n");
172 printf("commands in a script file instead of specifying input and output files on the\n");
173 printf("command line.\n");
175 printf("When no commands, script files or input files are specified on the command\n");
176 printf("line, yosys automatically enters the interactive command mode. Use the 'help'\n");
177 printf("command to get information on the individual commands.\n");
183 while ((opt
= getopt(argc
, argv
, "MXAQTVSm:f:Hh:b:o:p:l:qv:tds:c:")) != -1)
197 print_banner
= false;
203 printf("%s\n", yosys_version_str
);
206 passes_commands
.push_back("synth");
209 plugin_filenames
.push_back(optarg
);
212 frontend_command
= optarg
;
215 passes_commands
.push_back("help");
218 passes_commands
.push_back(stringf("help %s", optarg
));
221 backend_command
= optarg
;
224 passes_commands
.push_back(optarg
);
227 output_filename
= optarg
;
228 got_output_filename
= true;
231 log_files
.push_back(fopen(optarg
, "wt"));
232 if (log_files
.back() == NULL
) {
233 fprintf(stderr
, "Can't open log file `%s' for writing!\n", optarg
);
238 if (log_errfile
== stderr
)
239 log_quiet_warnings
= true;
240 log_errfile
= stderr
;
243 log_errfile
= stderr
;
244 log_verbose_level
= atoi(optarg
);
250 timing_details
= true;
254 scriptfile_tcl
= false;
258 scriptfile_tcl
= true;
261 fprintf(stderr
, "Run '%s -h' for help.\n", argv
[0]);
266 if (log_errfile
== NULL
)
267 log_files
.push_back(stderr
);
271 log(" /----------------------------------------------------------------------------\\\n");
273 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
275 log(" | Copyright (C) 2012 Clifford Wolf <clifford@clifford.at> |\n");
277 log(" | Permission to use, copy, modify, and/or distribute this software for any |\n");
278 log(" | purpose with or without fee is hereby granted, provided that the above |\n");
279 log(" | copyright notice and this permission notice appear in all copies. |\n");
281 log(" | THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |\n");
282 log(" | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |\n");
283 log(" | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |\n");
284 log(" | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |\n");
285 log(" | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |\n");
286 log(" | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |\n");
287 log(" | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |\n");
289 log(" \\----------------------------------------------------------------------------/\n");
291 log(" %s\n", yosys_version_str
);
296 log_hasher
= new SHA1
;
300 for (auto &fn
: plugin_filenames
)
303 if (optind
== argc
&& passes_commands
.size() == 0 && scriptfile
.empty()) {
304 if (!got_output_filename
)
305 backend_command
= "";
309 while (optind
< argc
)
310 run_frontend(argv
[optind
++], frontend_command
, yosys_design
, output_filename
== "-" ? &backend_command
: NULL
, NULL
);
312 if (!scriptfile
.empty()) {
313 if (scriptfile_tcl
) {
314 #ifdef YOSYS_ENABLE_TCL
315 if (Tcl_EvalFile(yosys_get_tcl_interp(), scriptfile
.c_str()) != TCL_OK
)
316 log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
318 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
321 run_frontend(scriptfile
, "script", yosys_design
, output_filename
== "-" ? &backend_command
: NULL
, NULL
);
324 for (auto it
= passes_commands
.begin(); it
!= passes_commands
.end(); it
++)
325 run_pass(*it
, yosys_design
);
327 if (!backend_command
.empty())
328 run_backend(output_filename
, backend_command
, yosys_design
);
332 std::string hash
= log_hasher
->final().substr(0, 10);
334 log_hasher
= nullptr;
338 log("End of script. Logfile hash: %s\n", hash
.c_str());
341 std::string stats_divider
= ", ";
344 statm
.open(stringf("/proc/%lld/statm", (long long)getpid()));
345 if (statm
.is_open()) {
346 int sz_total
, sz_resident
;
347 statm
>> sz_total
>> sz_resident
;
348 meminfo
= stringf(", MEM: %.2f MB total, %.2f MB resident",
349 sz_total
* (getpagesize() / 1024.0 / 1024.0),
350 sz_resident
* (getpagesize() / 1024.0 / 1024.0));
351 stats_divider
= "\n";
355 struct rusage ru_buffer
;
356 getrusage(RUSAGE_SELF
, &ru_buffer
);
357 log("End of script. Logfile hash: %s%sCPU: user %.2fs system %.2fs%s\n", hash
.c_str(),
358 stats_divider
.c_str(), ru_buffer
.ru_utime
.tv_sec
+ 1e-6 * ru_buffer
.ru_utime
.tv_usec
,
359 ru_buffer
.ru_stime
.tv_sec
+ 1e-6 * ru_buffer
.ru_stime
.tv_usec
, meminfo
.c_str());
361 log("%s\n", yosys_version_str
);
363 int64_t total_ns
= 0;
364 std::set
<std::tuple
<int64_t, int, std::string
>> timedat
;
366 for (auto &it
: pass_register
)
367 if (it
.second
->call_counter
) {
368 total_ns
+= it
.second
->runtime_ns
+ 1;
369 timedat
.insert(make_tuple(it
.second
->runtime_ns
+ 1, it
.second
->call_counter
, it
.first
));
374 log("Time spent:\n");
375 for (auto it
= timedat
.rbegin(); it
!= timedat
.rend(); it
++) {
376 log("%5d%% %5d calls %8.3f sec %s\n", int(100*std::get
<0>(*it
) / total_ns
),
377 std::get
<1>(*it
), std::get
<0>(*it
) / 1000000000.0, std::get
<2>(*it
).c_str());
384 for (auto it
= timedat
.rbegin(); it
!= timedat
.rend() && out_count
< 4; it
++, out_count
++) {
385 if (out_count
>= 2 && (std::get
<0>(*it
) < 1000000000 || int(100*std::get
<0>(*it
) / total_ns
) < 20)) {
389 log("%s %d%% %dx %s (%d sec)", out_count
? "," : "", int(100*std::get
<0>(*it
) / total_ns
),
390 std::get
<1>(*it
), std::get
<2>(*it
).c_str(), int(std::get
<0>(*it
) / 1000000000));
392 log("%s\n", out_count
? "" : " no commands executed");
396 #ifdef YOSYS_ENABLE_COVER
397 if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
399 char filename_buffer
[4096];
402 if (getenv("YOSYS_COVER_DIR")) {
403 snprintf(filename_buffer
, 4096, "%s/yosys_cover_%d_XXXXXX.txt", getenv("YOSYS_COVER_DIR"), getpid());
404 f
= fdopen(mkstemps(filename_buffer
, 4), "w");
406 snprintf(filename_buffer
, 4096, "%s", getenv("YOSYS_COVER_FILE"));
407 f
= fopen(filename_buffer
, "a+");
411 log_error("Can't create coverage file `%s'.\n", filename_buffer
);
413 log("<writing coverage file \"%s\">\n", filename_buffer
);
415 for (auto &it
: get_coverage_data())
416 fprintf(f
, "%-60s %10d %s\n", it
.second
.first
.c_str(), it
.second
.second
, it
.first
.c_str());
426 #ifdef YOSYS_ENABLE_READLINE
427 if (!history_file
.empty()) {
428 if (history_offset
> 0) {
429 history_truncate_file(history_file
.c_str(), 100);
430 append_history(where_history() - history_offset
, history_file
.c_str());
432 write_history(history_file
.c_str());
436 HIST_ENTRY
**hist_list
= history_list();
437 if (hist_list
!= NULL
)