9a81d8a4879bb8d2f2bf0804af58d9dd0a3d361e
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(" will slightly randomize allocated pointer addresses. for debugging\n");
158 printf(" will call abort() at the end of the script. for debugging\n");
161 printf(" print version information and exit\n");
163 printf("The option -S is an shortcut for calling the \"synth\" command, a default\n");
164 printf("script for transforming the verilog input to a gate-level netlist. For example:\n");
166 printf(" yosys -o output.blif -S input.v\n");
168 printf("For more complex synthesis jobs it is recommended to use the read_* and write_*\n");
169 printf("commands in a script file instead of specifying input and output files on the\n");
170 printf("command line.\n");
172 printf("When no commands, script files or input files are specified on the command\n");
173 printf("line, yosys automatically enters the interactive command mode. Use the 'help'\n");
174 printf("command to get information on the individual commands.\n");
180 while ((opt
= getopt(argc
, argv
, "MAQTVSm:f:Hh:b:o:p:l:qv:tds:c:")) != -1)
191 print_banner
= false;
197 printf("%s\n", yosys_version_str
);
200 passes_commands
.push_back("synth");
203 plugin_filenames
.push_back(optarg
);
206 frontend_command
= optarg
;
209 passes_commands
.push_back("help");
212 passes_commands
.push_back(stringf("help %s", optarg
));
215 backend_command
= optarg
;
218 passes_commands
.push_back(optarg
);
221 output_filename
= optarg
;
222 got_output_filename
= true;
225 log_files
.push_back(fopen(optarg
, "wt"));
226 if (log_files
.back() == NULL
) {
227 fprintf(stderr
, "Can't open log file `%s' for writing!\n", optarg
);
232 if (log_errfile
== stderr
)
233 log_quiet_warnings
= true;
234 log_errfile
= stderr
;
237 log_errfile
= stderr
;
238 log_verbose_level
= atoi(optarg
);
244 timing_details
= true;
248 scriptfile_tcl
= false;
252 scriptfile_tcl
= true;
255 fprintf(stderr
, "Run '%s -h' for help.\n", argv
[0]);
260 if (log_errfile
== NULL
)
261 log_files
.push_back(stderr
);
265 log(" /----------------------------------------------------------------------------\\\n");
267 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
269 log(" | Copyright (C) 2012 Clifford Wolf <clifford@clifford.at> |\n");
271 log(" | Permission to use, copy, modify, and/or distribute this software for any |\n");
272 log(" | purpose with or without fee is hereby granted, provided that the above |\n");
273 log(" | copyright notice and this permission notice appear in all copies. |\n");
275 log(" | THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |\n");
276 log(" | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |\n");
277 log(" | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |\n");
278 log(" | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |\n");
279 log(" | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |\n");
280 log(" | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |\n");
281 log(" | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |\n");
283 log(" \\----------------------------------------------------------------------------/\n");
285 log(" %s\n", yosys_version_str
);
290 log_hasher
= new SHA1
;
294 for (auto &fn
: plugin_filenames
)
297 if (optind
== argc
&& passes_commands
.size() == 0 && scriptfile
.empty()) {
298 if (!got_output_filename
)
299 backend_command
= "";
303 while (optind
< argc
)
304 run_frontend(argv
[optind
++], frontend_command
, yosys_design
, output_filename
== "-" ? &backend_command
: NULL
, NULL
);
306 if (!scriptfile
.empty()) {
307 if (scriptfile_tcl
) {
308 #ifdef YOSYS_ENABLE_TCL
309 if (Tcl_EvalFile(yosys_get_tcl_interp(), scriptfile
.c_str()) != TCL_OK
)
310 log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
312 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
315 run_frontend(scriptfile
, "script", yosys_design
, output_filename
== "-" ? &backend_command
: NULL
, NULL
);
318 for (auto it
= passes_commands
.begin(); it
!= passes_commands
.end(); it
++)
319 run_pass(*it
, yosys_design
);
321 if (!backend_command
.empty())
322 run_backend(output_filename
, backend_command
, yosys_design
);
326 std::string hash
= log_hasher
->final().substr(0, 10);
328 log_hasher
= nullptr;
332 log("End of script. Logfile hash: %s\n", hash
.c_str());
335 std::string stats_divider
= ", ";
338 statm
.open(stringf("/proc/%lld/statm", (long long)getpid()));
339 if (statm
.is_open()) {
340 int sz_total
, sz_resident
;
341 statm
>> sz_total
>> sz_resident
;
342 meminfo
= stringf(", MEM: %.2f MB total, %.2f MB resident",
343 sz_total
* (getpagesize() / 1024.0 / 1024.0),
344 sz_resident
* (getpagesize() / 1024.0 / 1024.0));
345 stats_divider
= "\n";
349 struct rusage ru_buffer
;
350 getrusage(RUSAGE_SELF
, &ru_buffer
);
351 log("End of script. Logfile hash: %s%sCPU: user %.2fs system %.2fs%s\n", hash
.c_str(),
352 stats_divider
.c_str(), ru_buffer
.ru_utime
.tv_sec
+ 1e-6 * ru_buffer
.ru_utime
.tv_usec
,
353 ru_buffer
.ru_stime
.tv_sec
+ 1e-6 * ru_buffer
.ru_stime
.tv_usec
, meminfo
.c_str());
355 log("%s\n", yosys_version_str
);
357 int64_t total_ns
= 0;
358 std::set
<std::tuple
<int64_t, int, std::string
>> timedat
;
360 for (auto &it
: pass_register
)
361 if (it
.second
->call_counter
) {
362 total_ns
+= it
.second
->runtime_ns
+ 1;
363 timedat
.insert(make_tuple(it
.second
->runtime_ns
+ 1, it
.second
->call_counter
, it
.first
));
368 log("Time spent:\n");
369 for (auto it
= timedat
.rbegin(); it
!= timedat
.rend(); it
++) {
370 log("%5d%% %5d calls %8.3f sec %s\n", int(100*std::get
<0>(*it
) / total_ns
),
371 std::get
<1>(*it
), std::get
<0>(*it
) / 1000000000.0, std::get
<2>(*it
).c_str());
378 for (auto it
= timedat
.rbegin(); it
!= timedat
.rend() && out_count
< 4; it
++, out_count
++) {
379 if (out_count
>= 2 && (std::get
<0>(*it
) < 1000000000 || int(100*std::get
<0>(*it
) / total_ns
) < 20)) {
383 log("%s %d%% %dx %s (%d sec)", out_count
? "," : "", int(100*std::get
<0>(*it
) / total_ns
),
384 std::get
<1>(*it
), std::get
<2>(*it
).c_str(), int(std::get
<0>(*it
) / 1000000000));
386 log("%s\n", out_count
? "" : " no commands executed");
390 #ifdef YOSYS_ENABLE_COVER
391 if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
393 char filename_buffer
[4096];
396 if (getenv("YOSYS_COVER_DIR")) {
397 snprintf(filename_buffer
, 4096, "%s/yosys_cover_%d_XXXXXX.txt", getenv("YOSYS_COVER_DIR"), getpid());
398 f
= fdopen(mkstemps(filename_buffer
, 4), "w");
400 snprintf(filename_buffer
, 4096, "%s", getenv("YOSYS_COVER_FILE"));
401 f
= fopen(filename_buffer
, "a+");
405 log_error("Can't create coverage file `%s'.\n", filename_buffer
);
407 log("<writing coverage file \"%s\">\n", filename_buffer
);
409 for (auto &it
: get_coverage_data())
410 fprintf(f
, "%-60s %10d %s\n", it
.second
.first
.c_str(), it
.second
.second
, it
.first
.c_str());
420 #ifdef YOSYS_ENABLE_READLINE
421 if (!history_file
.empty()) {
422 if (history_offset
> 0) {
423 history_truncate_file(history_file
.c_str(), 100);
424 append_history(where_history() - history_offset
, history_file
.c_str());
426 write_history(history_file
.c_str());
430 HIST_ENTRY
**hist_list
= history_list();
431 if (hist_list
!= NULL
)