Added "yosys -X"
[yosys.git] / kernel / driver.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
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.
9 *
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.
17 *
18 */
19
20 #include "kernel/yosys.h"
21 #include "libs/sha1/sha1.h"
22
23 #ifdef YOSYS_ENABLE_READLINE
24 # include <readline/readline.h>
25 # include <readline/history.h>
26 #endif
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <errno.h>
32
33 #ifdef __linux__
34 # include <sys/types.h>
35 # include <unistd.h>
36 #endif
37
38 #if !defined(_WIN32) || defined(__MINGW32__)
39 # include <unistd.h>
40 #else
41 char *optarg;
42 int optind = 1, optcur = 1;
43 int getopt(int argc, char **argv, const char *optstring)
44 {
45 if (optind >= argc || argv[optind][0] != '-')
46 return -1;
47
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] == ':')
52 takes_arg = true;
53
54 if (!takes_arg) {
55 if (argv[optind][++optcur] == 0)
56 optind++, optcur = 1;
57 return opt;
58 }
59
60 if (argv[optind][++optcur]) {
61 optarg = argv[optind++] + optcur;
62 optcur = 1;
63 return opt;
64 }
65
66 optarg = argv[++optind];
67 optind++, optcur = 1;
68 return opt;
69 }
70 #endif
71
72
73 USING_YOSYS_NAMESPACE
74
75 int main(int argc, char **argv)
76 {
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;
89
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();
97 }
98 #endif
99
100 if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "-help") || !strcmp(argv[1], "--help")))
101 {
102 printf("\n");
103 printf("Usage: %s [options] [<infile> [..]]\n", argv[0]);
104 printf("\n");
105 printf(" -Q\n");
106 printf(" suppress printing of banner (copyright, disclaimer, version)\n");
107 printf("\n");
108 printf(" -T\n");
109 printf(" suppress printing of footer (log hash, version, timing statistics)\n");
110 printf("\n");
111 printf(" -q\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");
114 printf("\n");
115 printf(" -v <level>\n");
116 printf(" print log headers up to level <level> to the console. (implies -q)\n");
117 printf("\n");
118 printf(" -t\n");
119 printf(" annotate all log messages with a time stamp\n");
120 printf("\n");
121 printf(" -d\n");
122 printf(" print more detailed timing stats at exit\n");
123 printf("\n");
124 printf(" -l logfile\n");
125 printf(" write log messages to the specified file\n");
126 printf("\n");
127 printf(" -o outfile\n");
128 printf(" write the design to the specified file on exit\n");
129 printf("\n");
130 printf(" -b backend\n");
131 printf(" use this backend for the output file specified on the command line\n");
132 printf("\n");
133 printf(" -f backend\n");
134 printf(" use the specified front for the input files on the command line\n");
135 printf("\n");
136 printf(" -H\n");
137 printf(" print the command list\n");
138 printf("\n");
139 printf(" -h command\n");
140 printf(" print the help message for the specified command\n");
141 printf("\n");
142 printf(" -s scriptfile\n");
143 printf(" execute the commands in the script file\n");
144 printf("\n");
145 printf(" -c tcl_scriptfile\n");
146 printf(" execute the commands in the tcl script file (see 'help tcl' for details)\n");
147 printf("\n");
148 printf(" -p command\n");
149 printf(" execute the commands\n");
150 printf("\n");
151 printf(" -m module_file\n");
152 printf(" load the specified module (aka plugin)\n");
153 printf("\n");
154 printf(" -X\n");
155 printf(" enable tracing of core data structure changes. for debugging\n");
156 printf("\n");
157 printf(" -M\n");
158 printf(" will slightly randomize allocated pointer addresses. for debugging\n");
159 printf("\n");
160 printf(" -A\n");
161 printf(" will call abort() at the end of the script. for debugging\n");
162 printf("\n");
163 printf(" -V\n");
164 printf(" print version information and exit\n");
165 printf("\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");
168 printf("\n");
169 printf(" yosys -o output.blif -S input.v\n");
170 printf("\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");
174 printf("\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");
178 printf("\n");
179 exit(0);
180 }
181
182 int opt;
183 while ((opt = getopt(argc, argv, "MXAQTVSm:f:Hh:b:o:p:l:qv:tds:c:")) != -1)
184 {
185 switch (opt)
186 {
187 case 'M':
188 memhasher_on();
189 break;
190 case 'X':
191 yosys_xtrace++;
192 break;
193 case 'A':
194 call_abort = true;
195 break;
196 case 'Q':
197 print_banner = false;
198 break;
199 case 'T':
200 print_stats = false;
201 break;
202 case 'V':
203 printf("%s\n", yosys_version_str);
204 exit(0);
205 case 'S':
206 passes_commands.push_back("synth");
207 break;
208 case 'm':
209 plugin_filenames.push_back(optarg);
210 break;
211 case 'f':
212 frontend_command = optarg;
213 break;
214 case 'H':
215 passes_commands.push_back("help");
216 break;
217 case 'h':
218 passes_commands.push_back(stringf("help %s", optarg));
219 break;
220 case 'b':
221 backend_command = optarg;
222 break;
223 case 'p':
224 passes_commands.push_back(optarg);
225 break;
226 case 'o':
227 output_filename = optarg;
228 got_output_filename = true;
229 break;
230 case 'l':
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);
234 exit(1);
235 }
236 break;
237 case 'q':
238 if (log_errfile == stderr)
239 log_quiet_warnings = true;
240 log_errfile = stderr;
241 break;
242 case 'v':
243 log_errfile = stderr;
244 log_verbose_level = atoi(optarg);
245 break;
246 case 't':
247 log_time = true;
248 break;
249 case 'd':
250 timing_details = true;
251 break;
252 case 's':
253 scriptfile = optarg;
254 scriptfile_tcl = false;
255 break;
256 case 'c':
257 scriptfile = optarg;
258 scriptfile_tcl = true;
259 break;
260 default:
261 fprintf(stderr, "Run '%s -h' for help.\n", argv[0]);
262 exit(1);
263 }
264 }
265
266 if (log_errfile == NULL)
267 log_files.push_back(stderr);
268
269 if (print_banner) {
270 log("\n");
271 log(" /----------------------------------------------------------------------------\\\n");
272 log(" | |\n");
273 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
274 log(" | |\n");
275 log(" | Copyright (C) 2012 Clifford Wolf <clifford@clifford.at> |\n");
276 log(" | |\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");
280 log(" | |\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");
288 log(" | |\n");
289 log(" \\----------------------------------------------------------------------------/\n");
290 log("\n");
291 log(" %s\n", yosys_version_str);
292 log("\n");
293 }
294
295 if (print_stats)
296 log_hasher = new SHA1;
297
298 yosys_setup();
299
300 for (auto &fn : plugin_filenames)
301 load_plugin(fn, {});
302
303 if (optind == argc && passes_commands.size() == 0 && scriptfile.empty()) {
304 if (!got_output_filename)
305 backend_command = "";
306 shell(yosys_design);
307 }
308
309 while (optind < argc)
310 run_frontend(argv[optind++], frontend_command, yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
311
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()));
317 #else
318 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
319 #endif
320 } else
321 run_frontend(scriptfile, "script", yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
322 }
323
324 for (auto it = passes_commands.begin(); it != passes_commands.end(); it++)
325 run_pass(*it, yosys_design);
326
327 if (!backend_command.empty())
328 run_backend(output_filename, backend_command, yosys_design);
329
330 if (print_stats)
331 {
332 std::string hash = log_hasher->final().substr(0, 10);
333 delete log_hasher;
334 log_hasher = nullptr;
335
336 log_spacer();
337 #ifdef _WIN32
338 log("End of script. Logfile hash: %s\n", hash.c_str());
339 #else
340 std::string meminfo;
341 std::string stats_divider = ", ";
342 # ifdef __linux__
343 std::ifstream statm;
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";
352 }
353 # endif
354
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());
360 #endif
361 log("%s\n", yosys_version_str);
362
363 int64_t total_ns = 0;
364 std::set<std::tuple<int64_t, int, std::string>> timedat;
365
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));
370 }
371
372 if (timing_details)
373 {
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());
378 }
379 }
380 else
381 {
382 int out_count = 0;
383 log("Time spent:");
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)) {
386 log(", ...");
387 break;
388 }
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));
391 }
392 log("%s\n", out_count ? "" : " no commands executed");
393 }
394 }
395
396 #ifdef YOSYS_ENABLE_COVER
397 if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
398 {
399 char filename_buffer[4096];
400 FILE *f;
401
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");
405 } else {
406 snprintf(filename_buffer, 4096, "%s", getenv("YOSYS_COVER_FILE"));
407 f = fopen(filename_buffer, "a+");
408 }
409
410 if (f == NULL)
411 log_error("Can't create coverage file `%s'.\n", filename_buffer);
412
413 log("<writing coverage file \"%s\">\n", filename_buffer);
414
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());
417
418 fclose(f);
419 }
420 #endif
421
422 memhasher_off();
423 if (call_abort)
424 abort();
425
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());
431 } else
432 write_history(history_file.c_str());
433 }
434
435 clear_history();
436 HIST_ENTRY **hist_list = history_list();
437 if (hist_list != NULL)
438 free(hist_list);
439 #endif
440
441 yosys_shutdown();
442
443 return 0;
444 }
445