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>
33 #if !defined(_WIN32) || defined(__MINGW32__)
37 int optind
= 1, optcur
= 1;
38 int getopt(int argc
, char **argv
, const char *optstring
)
40 if (optind
>= argc
|| argv
[optind
][0] != '-')
43 bool takes_arg
= false;
44 int opt
= argv
[optind
][optcur
];
45 for (int i
= 0; optstring
[i
]; i
++)
46 if (opt
== optstring
[i
] && optstring
[i
+ 1] == ':')
50 if (argv
[optind
][++optcur
] == 0)
55 if (argv
[optind
][++optcur
]) {
56 optarg
= argv
[optind
++] + optcur
;
61 optarg
= argv
[++optind
];
70 int main(int argc
, char **argv
)
72 std::string frontend_command
= "auto";
73 std::string backend_command
= "auto";
74 std::vector
<std::string
> passes_commands
;
75 std::vector
<std::string
> plugin_filenames
;
76 std::string output_filename
= "";
77 std::string scriptfile
= "";
78 bool scriptfile_tcl
= false;
79 bool got_output_filename
= false;
80 bool print_banner
= true;
81 bool print_stats
= true;
82 bool call_abort
= false;
84 #ifdef YOSYS_ENABLE_READLINE
85 int history_offset
= 0;
86 std::string history_file
;
87 if (getenv("HOME") != NULL
) {
88 history_file
= stringf("%s/.yosys_history", getenv("HOME"));
89 read_history(history_file
.c_str());
90 history_offset
= where_history();
95 while ((opt
= getopt(argc
, argv
, "AQTVSm:f:Hh:b:o:p:l:qv:ts:c:")) != -1)
103 print_banner
= false;
109 printf("%s\n", yosys_version_str
);
112 passes_commands
.push_back("synth");
115 plugin_filenames
.push_back(optarg
);
118 frontend_command
= optarg
;
121 passes_commands
.push_back("help");
124 passes_commands
.push_back(stringf("help %s", optarg
));
127 backend_command
= optarg
;
130 passes_commands
.push_back(optarg
);
133 output_filename
= optarg
;
134 got_output_filename
= true;
137 log_files
.push_back(fopen(optarg
, "wt"));
138 if (log_files
.back() == NULL
) {
139 fprintf(stderr
, "Can't open log file `%s' for writing!\n", optarg
);
144 log_errfile
= stderr
;
147 log_errfile
= stderr
;
148 log_verbose_level
= atoi(optarg
);
155 scriptfile_tcl
= false;
159 scriptfile_tcl
= true;
162 fprintf(stderr
, "\n");
163 fprintf(stderr
, "Usage: %s [-V -S -Q -T -q] [-v <level>[-t] [-l <logfile>] [-o <outfile>] [-f <frontend>] [-h cmd] \\\n", argv
[0]);
164 fprintf(stderr
, " %*s[{-s|-c} <scriptfile>] [-p <pass> [-p ..]] [-b <backend>] [-m <module_file>] [<infile> [..]]\n", int(strlen(argv
[0])+1), "");
165 fprintf(stderr
, "\n");
166 fprintf(stderr
, " -Q\n");
167 fprintf(stderr
, " suppress printing of banner (copyright, disclaimer, version)\n");
168 fprintf(stderr
, "\n");
169 fprintf(stderr
, " -T\n");
170 fprintf(stderr
, " suppress printing of footer (log hash, version, timing statistics)\n");
171 fprintf(stderr
, "\n");
172 fprintf(stderr
, " -q\n");
173 fprintf(stderr
, " quiet operation. only write error messages to console\n");
174 fprintf(stderr
, "\n");
175 fprintf(stderr
, " -v <level>\n");
176 fprintf(stderr
, " print log headers up to level <level> to the console. (implies -q)\n");
177 fprintf(stderr
, "\n");
178 fprintf(stderr
, " -t\n");
179 fprintf(stderr
, " annotate all log messages with a time stamp\n");
180 fprintf(stderr
, "\n");
181 fprintf(stderr
, " -l logfile\n");
182 fprintf(stderr
, " write log messages to the specified file\n");
183 fprintf(stderr
, "\n");
184 fprintf(stderr
, " -o outfile\n");
185 fprintf(stderr
, " write the design to the specified file on exit\n");
186 fprintf(stderr
, "\n");
187 fprintf(stderr
, " -b backend\n");
188 fprintf(stderr
, " use this backend for the output file specified on the command line\n");
189 fprintf(stderr
, "\n");
190 fprintf(stderr
, " -f backend\n");
191 fprintf(stderr
, " use the specified front for the input files on the command line\n");
192 fprintf(stderr
, "\n");
193 fprintf(stderr
, " -H\n");
194 fprintf(stderr
, " print the command list\n");
195 fprintf(stderr
, "\n");
196 fprintf(stderr
, " -h command\n");
197 fprintf(stderr
, " print the help message for the specified command\n");
198 fprintf(stderr
, "\n");
199 fprintf(stderr
, " -s scriptfile\n");
200 fprintf(stderr
, " execute the commands in the script file\n");
201 fprintf(stderr
, "\n");
202 fprintf(stderr
, " -c tcl_scriptfile\n");
203 fprintf(stderr
, " execute the commands in the tcl script file (see 'help tcl' for details)\n");
204 fprintf(stderr
, "\n");
205 fprintf(stderr
, " -p command\n");
206 fprintf(stderr
, " execute the commands\n");
207 fprintf(stderr
, "\n");
208 fprintf(stderr
, " -m module_file\n");
209 fprintf(stderr
, " load the specified module (aka plugin)\n");
210 fprintf(stderr
, "\n");
211 fprintf(stderr
, " -A\n");
212 fprintf(stderr
, " will call abort() at the end of the script. useful for debugging\n");
213 fprintf(stderr
, "\n");
214 fprintf(stderr
, " -V\n");
215 fprintf(stderr
, " print version information and exit\n");
216 fprintf(stderr
, "\n");
217 fprintf(stderr
, "The option -S is an shortcut for calling the \"synth\" command, a default\n");
218 fprintf(stderr
, "script for transforming the verilog input to a gate-level netlist. For example:\n");
219 fprintf(stderr
, "\n");
220 fprintf(stderr
, " yosys -o output.blif -S input.v\n");
221 fprintf(stderr
, "\n");
222 fprintf(stderr
, "For more complex synthesis jobs it is recommended to use the read_* and write_*\n");
223 fprintf(stderr
, "commands in a script file instead of specifying input and output files on the\n");
224 fprintf(stderr
, "command line.\n");
225 fprintf(stderr
, "\n");
226 fprintf(stderr
, "When no commands, script files or input files are specified on the command\n");
227 fprintf(stderr
, "line, yosys automatically enters the interactive command mode. Use the 'help'\n");
228 fprintf(stderr
, "command to get information on the individual commands.\n");
229 fprintf(stderr
, "\n");
234 if (log_errfile
== NULL
)
235 log_files
.push_back(stderr
);
239 log(" /----------------------------------------------------------------------------\\\n");
241 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
243 log(" | Copyright (C) 2012 Clifford Wolf <clifford@clifford.at> |\n");
245 log(" | Permission to use, copy, modify, and/or distribute this software for any |\n");
246 log(" | purpose with or without fee is hereby granted, provided that the above |\n");
247 log(" | copyright notice and this permission notice appear in all copies. |\n");
249 log(" | THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |\n");
250 log(" | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |\n");
251 log(" | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |\n");
252 log(" | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |\n");
253 log(" | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |\n");
254 log(" | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |\n");
255 log(" | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |\n");
257 log(" \\----------------------------------------------------------------------------/\n");
259 log(" %s\n", yosys_version_str
);
264 log_hasher
= new SHA1
;
268 for (auto &fn
: plugin_filenames
)
271 if (optind
== argc
&& passes_commands
.size() == 0 && scriptfile
.empty()) {
272 if (!got_output_filename
)
273 backend_command
= "";
277 while (optind
< argc
)
278 run_frontend(argv
[optind
++], frontend_command
, yosys_design
, output_filename
== "-" ? &backend_command
: NULL
, NULL
);
280 if (!scriptfile
.empty()) {
281 if (scriptfile_tcl
) {
282 #ifdef YOSYS_ENABLE_TCL
283 if (Tcl_EvalFile(yosys_get_tcl_interp(), scriptfile
.c_str()) != TCL_OK
)
284 log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
286 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
289 run_frontend(scriptfile
, "script", yosys_design
, output_filename
== "-" ? &backend_command
: NULL
, NULL
);
292 for (auto it
= passes_commands
.begin(); it
!= passes_commands
.end(); it
++)
293 run_pass(*it
, yosys_design
);
295 if (!backend_command
.empty())
296 run_backend(output_filename
, backend_command
, yosys_design
);
300 std::string hash
= log_hasher
->final().substr(0, 10);
302 log_hasher
= nullptr;
306 log("End of script. Logfile hash: %s\n", hash
.c_str());
308 struct rusage ru_buffer
;
309 getrusage(RUSAGE_SELF
, &ru_buffer
);
310 log("End of script. Logfile hash: %s, CPU: user %.2fs system %.2fs\n", hash
.c_str(),
311 ru_buffer
.ru_utime
.tv_sec
+ 1e-6 * ru_buffer
.ru_utime
.tv_usec
,
312 ru_buffer
.ru_stime
.tv_sec
+ 1e-6 * ru_buffer
.ru_stime
.tv_usec
);
314 log("%s\n", yosys_version_str
);
316 int64_t total_ns
= 0;
317 std::set
<std::tuple
<int64_t, int, std::string
>> timedat
;
319 for (auto &it
: pass_register
)
320 if (it
.second
->call_counter
) {
321 total_ns
+= it
.second
->runtime_ns
+ 1;
322 timedat
.insert(make_tuple(it
.second
->runtime_ns
+ 1, it
.second
->call_counter
, it
.first
));
327 for (auto it
= timedat
.rbegin(); it
!= timedat
.rend() && out_count
< 4; it
++, out_count
++) {
328 if (out_count
>= 2 && (std::get
<0>(*it
) < 1000000000 || int(100*std::get
<0>(*it
) / total_ns
) < 20)) {
332 log("%s %d%% %dx %s (%d sec)", out_count
? "," : "", int(100*std::get
<0>(*it
) / total_ns
),
333 std::get
<1>(*it
), std::get
<2>(*it
).c_str(), int(std::get
<0>(*it
) / 1000000000));
335 log("%s\n", out_count
? "" : " no commands executed");
338 #ifdef YOSYS_ENABLE_COVER
339 if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
341 char filename_buffer
[4096];
344 if (getenv("YOSYS_COVER_DIR")) {
345 snprintf(filename_buffer
, 4096, "%s/yosys_cover_%d_XXXXXX.txt", getenv("YOSYS_COVER_DIR"), getpid());
346 f
= fdopen(mkstemps(filename_buffer
, 4), "w");
348 snprintf(filename_buffer
, 4096, "%s", getenv("YOSYS_COVER_FILE"));
349 f
= fopen(filename_buffer
, "a+");
353 log_error("Can't create coverage file `%s'.\n", filename_buffer
);
355 log("<writing coverage file \"%s\">\n", filename_buffer
);
357 for (auto &it
: get_coverage_data())
358 fprintf(f
, "%-60s %10d %s\n", it
.second
.first
.c_str(), it
.second
.second
, it
.first
.c_str());
367 #ifdef YOSYS_ENABLE_READLINE
368 if (!history_file
.empty()) {
369 if (history_offset
> 0) {
370 history_truncate_file(history_file
.c_str(), 100);
371 append_history(where_history() - history_offset
, history_file
.c_str());
373 write_history(history_file
.c_str());
377 HIST_ENTRY
**hist_list
= history_list();
378 if (hist_list
!= NULL
)