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 #include <readline/readline.h>
24 #include <readline/history.h>
36 int main(int argc
, char **argv
)
38 std::string frontend_command
= "auto";
39 std::string backend_command
= "auto";
40 std::vector
<std::string
> passes_commands
;
41 std::vector
<void*> loaded_modules
;
42 std::string output_filename
= "";
43 std::string scriptfile
= "";
44 bool scriptfile_tcl
= false;
45 bool got_output_filename
= false;
46 bool print_banner
= true;
47 bool print_stats
= true;
48 bool call_abort
= false;
50 int history_offset
= 0;
51 std::string history_file
;
52 if (getenv("HOME") != NULL
) {
53 history_file
= stringf("%s/.yosys_history", getenv("HOME"));
54 read_history(history_file
.c_str());
55 history_offset
= where_history();
59 while ((opt
= getopt(argc
, argv
, "AQTVSm:f:Hh:b:o:p:l:qv:ts:c:")) != -1)
73 printf("%s\n", yosys_version_str
);
76 passes_commands
.push_back("hierarchy");
77 passes_commands
.push_back("proc");
78 passes_commands
.push_back("opt");
79 passes_commands
.push_back("memory");
80 passes_commands
.push_back("opt");
81 passes_commands
.push_back("techmap");
82 passes_commands
.push_back("opt");
85 loaded_modules
.push_back(dlopen(optarg
, RTLD_LAZY
|RTLD_GLOBAL
));
86 if (loaded_modules
.back() == NULL
) {
87 fprintf(stderr
, "Can't load module `%s': %s\n", optarg
, dlerror());
92 frontend_command
= optarg
;
95 passes_commands
.push_back("help");
98 passes_commands
.push_back(stringf("help %s", optarg
));
101 backend_command
= optarg
;
104 passes_commands
.push_back(optarg
);
107 output_filename
= optarg
;
108 got_output_filename
= true;
111 log_files
.push_back(fopen(optarg
, "wt"));
112 if (log_files
.back() == NULL
) {
113 fprintf(stderr
, "Can't open log file `%s' for writing!\n", optarg
);
118 log_errfile
= stderr
;
121 log_errfile
= stderr
;
122 log_verbose_level
= atoi(optarg
);
129 scriptfile_tcl
= false;
133 scriptfile_tcl
= true;
136 fprintf(stderr
, "\n");
137 fprintf(stderr
, "Usage: %s [-V -S -Q -T -q] [-v <level>[-t] [-l <logfile>] [-o <outfile>] [-f <frontend>] [-h cmd] \\\n", argv
[0]);
138 fprintf(stderr
, " %*s[{-s|-c} <scriptfile>] [-p <pass> [-p ..]] [-b <backend>] [-m <module_file>] [<infile> [..]]\n", int(strlen(argv
[0])+1), "");
139 fprintf(stderr
, "\n");
140 fprintf(stderr
, " -Q\n");
141 fprintf(stderr
, " suppress printing of banner (copyright, disclaimer, version)\n");
142 fprintf(stderr
, "\n");
143 fprintf(stderr
, " -T\n");
144 fprintf(stderr
, " suppress printing of footer (log hash, version, timing statistics)\n");
145 fprintf(stderr
, "\n");
146 fprintf(stderr
, " -q\n");
147 fprintf(stderr
, " quiet operation. only write error messages to console\n");
148 fprintf(stderr
, "\n");
149 fprintf(stderr
, " -v <level>\n");
150 fprintf(stderr
, " print log headers up to level <level> to the console. (implies -q)\n");
151 fprintf(stderr
, "\n");
152 fprintf(stderr
, " -t\n");
153 fprintf(stderr
, " annotate all log messages with a time stamp\n");
154 fprintf(stderr
, "\n");
155 fprintf(stderr
, " -l logfile\n");
156 fprintf(stderr
, " write log messages to the specified file\n");
157 fprintf(stderr
, "\n");
158 fprintf(stderr
, " -o outfile\n");
159 fprintf(stderr
, " write the design to the specified file on exit\n");
160 fprintf(stderr
, "\n");
161 fprintf(stderr
, " -b backend\n");
162 fprintf(stderr
, " use this backend for the output file specified on the command line\n");
163 fprintf(stderr
, "\n");
164 fprintf(stderr
, " -f backend\n");
165 fprintf(stderr
, " use the specified front for the input files on the command line\n");
166 fprintf(stderr
, "\n");
167 fprintf(stderr
, " -H\n");
168 fprintf(stderr
, " print the command list\n");
169 fprintf(stderr
, "\n");
170 fprintf(stderr
, " -h command\n");
171 fprintf(stderr
, " print the help message for the specified command\n");
172 fprintf(stderr
, "\n");
173 fprintf(stderr
, " -s scriptfile\n");
174 fprintf(stderr
, " execute the commands in the script file\n");
175 fprintf(stderr
, "\n");
176 fprintf(stderr
, " -c tcl_scriptfile\n");
177 fprintf(stderr
, " execute the commands in the tcl script file (see 'help tcl' for details)\n");
178 fprintf(stderr
, "\n");
179 fprintf(stderr
, " -p command\n");
180 fprintf(stderr
, " execute the commands\n");
181 fprintf(stderr
, "\n");
182 fprintf(stderr
, " -m module_file\n");
183 fprintf(stderr
, " load the specified module (aka plugin)\n");
184 fprintf(stderr
, "\n");
185 fprintf(stderr
, " -A\n");
186 fprintf(stderr
, " will call abort() at the end of the script. useful for debugging\n");
187 fprintf(stderr
, "\n");
188 fprintf(stderr
, " -V\n");
189 fprintf(stderr
, " print version information and exit\n");
190 fprintf(stderr
, "\n");
191 fprintf(stderr
, "The option -S is an alias for the following options that perform a simple\n");
192 fprintf(stderr
, "transformation of the input to a gate-level netlist.\n");
193 fprintf(stderr
, "\n");
194 fprintf(stderr
, " -p hierarchy -p proc -p opt -p memory -p opt -p techmap -p opt\n");
195 fprintf(stderr
, "\n");
196 fprintf(stderr
, "For more complex synthesis jobs it is recommended to use the read_* and write_*\n");
197 fprintf(stderr
, "commands in a script file instead of specifying input and output files on the\n");
198 fprintf(stderr
, "command line.\n");
199 fprintf(stderr
, "\n");
200 fprintf(stderr
, "When no commands, script files or input files are specified on the command\n");
201 fprintf(stderr
, "line, yosys automatically enters the interactive command mode. Use the 'help'\n");
202 fprintf(stderr
, "command to get information on the individual commands.\n");
203 fprintf(stderr
, "\n");
208 if (log_errfile
== NULL
)
209 log_files
.push_back(stderr
);
213 log(" /-----------------------------------------------------------------------------\\\n");
215 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
217 log(" | Copyright (C) 2012 Clifford Wolf <clifford@clifford.at> |\n");
219 log(" | Permission to use, copy, modify, and/or distribute this software for any |\n");
220 log(" | purpose with or without fee is hereby granted, provided that the above |\n");
221 log(" | copyright notice and this permission notice appear in all copies. |\n");
223 log(" | THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |\n");
224 log(" | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |\n");
225 log(" | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |\n");
226 log(" | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |\n");
227 log(" | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |\n");
228 log(" | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |\n");
229 log(" | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |\n");
231 log(" \\-----------------------------------------------------------------------------/\n");
233 log(" %s\n", yosys_version_str
);
238 log_hasher
= new SHA1
;
242 if (optind
== argc
&& passes_commands
.size() == 0 && scriptfile
.empty()) {
243 if (!got_output_filename
)
244 backend_command
= "";
248 while (optind
< argc
)
249 run_frontend(argv
[optind
++], frontend_command
, yosys_design
, output_filename
== "-" ? &backend_command
: NULL
, NULL
);
251 if (!scriptfile
.empty()) {
252 if (scriptfile_tcl
) {
253 #ifdef YOSYS_ENABLE_TCL
254 if (Tcl_EvalFile(yosys_get_tcl_interp(), scriptfile
.c_str()) != TCL_OK
)
255 log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
257 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
260 run_frontend(scriptfile
, "script", yosys_design
, output_filename
== "-" ? &backend_command
: NULL
, NULL
);
263 for (auto it
= passes_commands
.begin(); it
!= passes_commands
.end(); it
++)
264 run_pass(*it
, yosys_design
);
266 if (!backend_command
.empty())
267 run_backend(output_filename
, backend_command
, yosys_design
);
271 std::string hash
= log_hasher
->final().substr(0, 10);
273 log_hasher
= nullptr;
275 struct rusage ru_buffer
;
276 getrusage(RUSAGE_SELF
, &ru_buffer
);
278 log("End of script. Logfile hash: %s, CPU: user %.2fs system %.2fs\n", hash
.c_str(),
279 ru_buffer
.ru_utime
.tv_sec
+ 1e-6 * ru_buffer
.ru_utime
.tv_usec
,
280 ru_buffer
.ru_stime
.tv_sec
+ 1e-6 * ru_buffer
.ru_stime
.tv_usec
);
281 log("%s\n", yosys_version_str
);
283 int64_t total_ns
= 0;
284 std::set
<std::tuple
<int64_t, int, std::string
>> timedat
;
286 for (auto &it
: pass_register
)
287 if (it
.second
->call_counter
) {
288 total_ns
+= it
.second
->runtime_ns
+ 1;
289 timedat
.insert(make_tuple(it
.second
->runtime_ns
+ 1, it
.second
->call_counter
, it
.first
));
294 for (auto it
= timedat
.rbegin(); it
!= timedat
.rend() && out_count
< 4; it
++, out_count
++) {
295 if (out_count
>= 2 && (std::get
<0>(*it
) < 1000000000 || int(100*std::get
<0>(*it
) / total_ns
) < 20)) {
299 log("%s %d%% %dx %s (%d sec)", out_count
? "," : "", int(100*std::get
<0>(*it
) / total_ns
),
300 std::get
<1>(*it
), std::get
<2>(*it
).c_str(), int(std::get
<0>(*it
) / 1000000000));
302 log("%s\n", out_count
? "" : " no commands executed");
306 if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
308 char filename_buffer
[4096];
311 if (getenv("YOSYS_COVER_DIR")) {
312 snprintf(filename_buffer
, 4096, "%s/yosys_cover_%d_XXXXXX.txt", getenv("YOSYS_COVER_DIR"), getpid());
313 f
= fdopen(mkstemps(filename_buffer
, 4), "w");
315 snprintf(filename_buffer
, 4096, "%s", getenv("YOSYS_COVER_FILE"));
316 f
= fopen(filename_buffer
, "a+");
320 log_error("Can't create coverage file `%s'.\n", filename_buffer
);
322 log("<writing coverage file \"%s\">\n", filename_buffer
);
324 for (auto &it
: get_coverage_data())
325 fprintf(f
, "%-60s %10d %s\n", it
.second
.first
.c_str(), it
.second
.second
, it
.first
.c_str());
334 if (!history_file
.empty()) {
335 if (history_offset
> 0) {
336 history_truncate_file(history_file
.c_str(), 100);
337 append_history(where_history() - history_offset
, history_file
.c_str());
339 write_history(history_file
.c_str());
343 HIST_ENTRY
**hist_list
= history_list();
344 if (hist_list
!= NULL
)
349 for (auto mod
: loaded_modules
)