9a81d8a4879bb8d2f2bf0804af58d9dd0a3d361e
[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(" -M\n");
155 printf(" will slightly randomize allocated pointer addresses. for debugging\n");
156 printf("\n");
157 printf(" -A\n");
158 printf(" will call abort() at the end of the script. for debugging\n");
159 printf("\n");
160 printf(" -V\n");
161 printf(" print version information and exit\n");
162 printf("\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");
165 printf("\n");
166 printf(" yosys -o output.blif -S input.v\n");
167 printf("\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");
171 printf("\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");
175 printf("\n");
176 exit(0);
177 }
178
179 int opt;
180 while ((opt = getopt(argc, argv, "MAQTVSm:f:Hh:b:o:p:l:qv:tds:c:")) != -1)
181 {
182 switch (opt)
183 {
184 case 'M':
185 memhasher_on();
186 break;
187 case 'A':
188 call_abort = true;
189 break;
190 case 'Q':
191 print_banner = false;
192 break;
193 case 'T':
194 print_stats = false;
195 break;
196 case 'V':
197 printf("%s\n", yosys_version_str);
198 exit(0);
199 case 'S':
200 passes_commands.push_back("synth");
201 break;
202 case 'm':
203 plugin_filenames.push_back(optarg);
204 break;
205 case 'f':
206 frontend_command = optarg;
207 break;
208 case 'H':
209 passes_commands.push_back("help");
210 break;
211 case 'h':
212 passes_commands.push_back(stringf("help %s", optarg));
213 break;
214 case 'b':
215 backend_command = optarg;
216 break;
217 case 'p':
218 passes_commands.push_back(optarg);
219 break;
220 case 'o':
221 output_filename = optarg;
222 got_output_filename = true;
223 break;
224 case 'l':
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);
228 exit(1);
229 }
230 break;
231 case 'q':
232 if (log_errfile == stderr)
233 log_quiet_warnings = true;
234 log_errfile = stderr;
235 break;
236 case 'v':
237 log_errfile = stderr;
238 log_verbose_level = atoi(optarg);
239 break;
240 case 't':
241 log_time = true;
242 break;
243 case 'd':
244 timing_details = true;
245 break;
246 case 's':
247 scriptfile = optarg;
248 scriptfile_tcl = false;
249 break;
250 case 'c':
251 scriptfile = optarg;
252 scriptfile_tcl = true;
253 break;
254 default:
255 fprintf(stderr, "Run '%s -h' for help.\n", argv[0]);
256 exit(1);
257 }
258 }
259
260 if (log_errfile == NULL)
261 log_files.push_back(stderr);
262
263 if (print_banner) {
264 log("\n");
265 log(" /----------------------------------------------------------------------------\\\n");
266 log(" | |\n");
267 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
268 log(" | |\n");
269 log(" | Copyright (C) 2012 Clifford Wolf <clifford@clifford.at> |\n");
270 log(" | |\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");
274 log(" | |\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");
282 log(" | |\n");
283 log(" \\----------------------------------------------------------------------------/\n");
284 log("\n");
285 log(" %s\n", yosys_version_str);
286 log("\n");
287 }
288
289 if (print_stats)
290 log_hasher = new SHA1;
291
292 yosys_setup();
293
294 for (auto &fn : plugin_filenames)
295 load_plugin(fn, {});
296
297 if (optind == argc && passes_commands.size() == 0 && scriptfile.empty()) {
298 if (!got_output_filename)
299 backend_command = "";
300 shell(yosys_design);
301 }
302
303 while (optind < argc)
304 run_frontend(argv[optind++], frontend_command, yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
305
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()));
311 #else
312 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
313 #endif
314 } else
315 run_frontend(scriptfile, "script", yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
316 }
317
318 for (auto it = passes_commands.begin(); it != passes_commands.end(); it++)
319 run_pass(*it, yosys_design);
320
321 if (!backend_command.empty())
322 run_backend(output_filename, backend_command, yosys_design);
323
324 if (print_stats)
325 {
326 std::string hash = log_hasher->final().substr(0, 10);
327 delete log_hasher;
328 log_hasher = nullptr;
329
330 log_spacer();
331 #ifdef _WIN32
332 log("End of script. Logfile hash: %s\n", hash.c_str());
333 #else
334 std::string meminfo;
335 std::string stats_divider = ", ";
336 # ifdef __linux__
337 std::ifstream statm;
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";
346 }
347 # endif
348
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());
354 #endif
355 log("%s\n", yosys_version_str);
356
357 int64_t total_ns = 0;
358 std::set<std::tuple<int64_t, int, std::string>> timedat;
359
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));
364 }
365
366 if (timing_details)
367 {
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());
372 }
373 }
374 else
375 {
376 int out_count = 0;
377 log("Time spent:");
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)) {
380 log(", ...");
381 break;
382 }
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));
385 }
386 log("%s\n", out_count ? "" : " no commands executed");
387 }
388 }
389
390 #ifdef YOSYS_ENABLE_COVER
391 if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
392 {
393 char filename_buffer[4096];
394 FILE *f;
395
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");
399 } else {
400 snprintf(filename_buffer, 4096, "%s", getenv("YOSYS_COVER_FILE"));
401 f = fopen(filename_buffer, "a+");
402 }
403
404 if (f == NULL)
405 log_error("Can't create coverage file `%s'.\n", filename_buffer);
406
407 log("<writing coverage file \"%s\">\n", filename_buffer);
408
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());
411
412 fclose(f);
413 }
414 #endif
415
416 memhasher_off();
417 if (call_abort)
418 abort();
419
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());
425 } else
426 write_history(history_file.c_str());
427 }
428
429 clear_history();
430 HIST_ENTRY **hist_list = history_list();
431 if (hist_list != NULL)
432 free(hist_list);
433 #endif
434
435 yosys_shutdown();
436
437 return 0;
438 }
439