Made "cover" a compile-time option (disabled by default)
[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 #if !defined(_WIN32) || defined(__MINGW32__)
34 # include <unistd.h>
35 #else
36 char *optarg;
37 int optind = 1, optcur = 1;
38 int getopt(int argc, char **argv, const char *optstring)
39 {
40 if (optind >= argc || argv[optind][0] != '-')
41 return -1;
42
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] == ':')
47 takes_arg = true;
48
49 if (!takes_arg) {
50 if (argv[optind][++optcur] == 0)
51 optind++, optcur = 1;
52 return opt;
53 }
54
55 if (argv[optind][++optcur]) {
56 optarg = argv[optind++] + optcur;
57 optcur = 1;
58 return opt;
59 }
60
61 optarg = argv[++optind];
62 optind++, optcur = 1;
63 return opt;
64 }
65 #endif
66
67
68 USING_YOSYS_NAMESPACE
69
70 int main(int argc, char **argv)
71 {
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;
83
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();
91 }
92 #endif
93
94 int opt;
95 while ((opt = getopt(argc, argv, "AQTVSm:f:Hh:b:o:p:l:qv:ts:c:")) != -1)
96 {
97 switch (opt)
98 {
99 case 'A':
100 call_abort = true;
101 break;
102 case 'Q':
103 print_banner = false;
104 break;
105 case 'T':
106 print_stats = false;
107 break;
108 case 'V':
109 printf("%s\n", yosys_version_str);
110 exit(0);
111 case 'S':
112 passes_commands.push_back("synth");
113 break;
114 case 'm':
115 plugin_filenames.push_back(optarg);
116 break;
117 case 'f':
118 frontend_command = optarg;
119 break;
120 case 'H':
121 passes_commands.push_back("help");
122 break;
123 case 'h':
124 passes_commands.push_back(stringf("help %s", optarg));
125 break;
126 case 'b':
127 backend_command = optarg;
128 break;
129 case 'p':
130 passes_commands.push_back(optarg);
131 break;
132 case 'o':
133 output_filename = optarg;
134 got_output_filename = true;
135 break;
136 case 'l':
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);
140 exit(1);
141 }
142 break;
143 case 'q':
144 log_errfile = stderr;
145 break;
146 case 'v':
147 log_errfile = stderr;
148 log_verbose_level = atoi(optarg);
149 break;
150 case 't':
151 log_time = true;
152 break;
153 case 's':
154 scriptfile = optarg;
155 scriptfile_tcl = false;
156 break;
157 case 'c':
158 scriptfile = optarg;
159 scriptfile_tcl = true;
160 break;
161 default:
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");
230 exit(1);
231 }
232 }
233
234 if (log_errfile == NULL)
235 log_files.push_back(stderr);
236
237 if (print_banner) {
238 log("\n");
239 log(" /----------------------------------------------------------------------------\\\n");
240 log(" | |\n");
241 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
242 log(" | |\n");
243 log(" | Copyright (C) 2012 Clifford Wolf <clifford@clifford.at> |\n");
244 log(" | |\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");
248 log(" | |\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");
256 log(" | |\n");
257 log(" \\----------------------------------------------------------------------------/\n");
258 log("\n");
259 log(" %s\n", yosys_version_str);
260 log("\n");
261 }
262
263 if (print_stats)
264 log_hasher = new SHA1;
265
266 yosys_setup();
267
268 for (auto &fn : plugin_filenames)
269 load_plugin(fn, {});
270
271 if (optind == argc && passes_commands.size() == 0 && scriptfile.empty()) {
272 if (!got_output_filename)
273 backend_command = "";
274 shell(yosys_design);
275 }
276
277 while (optind < argc)
278 run_frontend(argv[optind++], frontend_command, yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
279
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()));
285 #else
286 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
287 #endif
288 } else
289 run_frontend(scriptfile, "script", yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
290 }
291
292 for (auto it = passes_commands.begin(); it != passes_commands.end(); it++)
293 run_pass(*it, yosys_design);
294
295 if (!backend_command.empty())
296 run_backend(output_filename, backend_command, yosys_design);
297
298 if (print_stats)
299 {
300 std::string hash = log_hasher->final().substr(0, 10);
301 delete log_hasher;
302 log_hasher = nullptr;
303
304 log_spacer();
305 #ifdef _WIN32
306 log("End of script. Logfile hash: %s\n", hash.c_str());
307 #else
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);
313 #endif
314 log("%s\n", yosys_version_str);
315
316 int64_t total_ns = 0;
317 std::set<std::tuple<int64_t, int, std::string>> timedat;
318
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));
323 }
324
325 int out_count = 0;
326 log("Time spent:");
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)) {
329 log(", ...");
330 break;
331 }
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));
334 }
335 log("%s\n", out_count ? "" : " no commands executed");
336 }
337
338 #ifdef YOSYS_ENABLE_COVER
339 if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
340 {
341 char filename_buffer[4096];
342 FILE *f;
343
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");
347 } else {
348 snprintf(filename_buffer, 4096, "%s", getenv("YOSYS_COVER_FILE"));
349 f = fopen(filename_buffer, "a+");
350 }
351
352 if (f == NULL)
353 log_error("Can't create coverage file `%s'.\n", filename_buffer);
354
355 log("<writing coverage file \"%s\">\n", filename_buffer);
356
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());
359
360 fclose(f);
361 }
362 #endif
363
364 if (call_abort)
365 abort();
366
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());
372 } else
373 write_history(history_file.c_str());
374 }
375
376 clear_history();
377 HIST_ENTRY **hist_list = history_list();
378 if (hist_list != NULL)
379 free(hist_list);
380 #endif
381
382 yosys_shutdown();
383
384 return 0;
385 }
386