6f97642381b8dcbcb018bd9d9388442c6750c915
[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 #include <readline/readline.h>
24 #include <readline/history.h>
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <libgen.h>
30 #include <dlfcn.h>
31 #include <limits.h>
32 #include <errno.h>
33
34 USING_YOSYS_NAMESPACE
35
36 int main(int argc, char **argv)
37 {
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;
49
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();
56 }
57
58 int opt;
59 while ((opt = getopt(argc, argv, "AQTVSm:f:Hh:b:o:p:l:qv:ts:c:")) != -1)
60 {
61 switch (opt)
62 {
63 case 'A':
64 call_abort = true;
65 break;
66 case 'Q':
67 print_banner = false;
68 break;
69 case 'T':
70 print_stats = false;
71 break;
72 case 'V':
73 printf("%s\n", yosys_version_str);
74 exit(0);
75 case 'S':
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");
83 break;
84 case 'm':
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());
88 exit(1);
89 }
90 break;
91 case 'f':
92 frontend_command = optarg;
93 break;
94 case 'H':
95 passes_commands.push_back("help");
96 break;
97 case 'h':
98 passes_commands.push_back(stringf("help %s", optarg));
99 break;
100 case 'b':
101 backend_command = optarg;
102 break;
103 case 'p':
104 passes_commands.push_back(optarg);
105 break;
106 case 'o':
107 output_filename = optarg;
108 got_output_filename = true;
109 break;
110 case 'l':
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);
114 exit(1);
115 }
116 break;
117 case 'q':
118 log_errfile = stderr;
119 break;
120 case 'v':
121 log_errfile = stderr;
122 log_verbose_level = atoi(optarg);
123 break;
124 case 't':
125 log_time = true;
126 break;
127 case 's':
128 scriptfile = optarg;
129 scriptfile_tcl = false;
130 break;
131 case 'c':
132 scriptfile = optarg;
133 scriptfile_tcl = true;
134 break;
135 default:
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");
204 exit(1);
205 }
206 }
207
208 if (log_errfile == NULL)
209 log_files.push_back(stderr);
210
211 if (print_banner) {
212 log("\n");
213 log(" /-----------------------------------------------------------------------------\\\n");
214 log(" | |\n");
215 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
216 log(" | |\n");
217 log(" | Copyright (C) 2012 Clifford Wolf <clifford@clifford.at> |\n");
218 log(" | |\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");
222 log(" | |\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");
230 log(" | |\n");
231 log(" \\-----------------------------------------------------------------------------/\n");
232 log("\n");
233 log(" %s\n", yosys_version_str);
234 log("\n");
235 }
236
237 if (print_stats)
238 log_hasher = new SHA1;
239
240 yosys_setup();
241
242 if (optind == argc && passes_commands.size() == 0 && scriptfile.empty()) {
243 if (!got_output_filename)
244 backend_command = "";
245 shell(yosys_design);
246 }
247
248 while (optind < argc)
249 run_frontend(argv[optind++], frontend_command, yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
250
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()));
256 #else
257 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
258 #endif
259 } else
260 run_frontend(scriptfile, "script", yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
261 }
262
263 for (auto it = passes_commands.begin(); it != passes_commands.end(); it++)
264 run_pass(*it, yosys_design);
265
266 if (!backend_command.empty())
267 run_backend(output_filename, backend_command, yosys_design);
268
269 if (print_stats)
270 {
271 std::string hash = log_hasher->final().substr(0, 10);
272 delete log_hasher;
273 log_hasher = nullptr;
274
275 struct rusage ru_buffer;
276 getrusage(RUSAGE_SELF, &ru_buffer);
277 log("\nEnd of script. Logfile hash: %s, CPU: user %.2fs system %.2fs\n", hash.c_str(),
278 ru_buffer.ru_utime.tv_sec + 1e-6 * ru_buffer.ru_utime.tv_usec,
279 ru_buffer.ru_stime.tv_sec + 1e-6 * ru_buffer.ru_stime.tv_usec);
280 log("%s\n", yosys_version_str);
281
282 int64_t total_ns = 0;
283 std::set<std::tuple<int64_t, int, std::string>> timedat;
284
285 for (auto &it : pass_register)
286 if (it.second->call_counter) {
287 total_ns += it.second->runtime_ns + 1;
288 timedat.insert(make_tuple(it.second->runtime_ns + 1, it.second->call_counter, it.first));
289 }
290
291 int out_count = 0;
292 log("Time spent:");
293 for (auto it = timedat.rbegin(); it != timedat.rend() && out_count < 4; it++, out_count++) {
294 if (out_count >= 2 && (std::get<0>(*it) < 1000000000 || int(100*std::get<0>(*it) / total_ns) < 20)) {
295 log(", ...");
296 break;
297 }
298 log("%s %d%% %dx %s (%d sec)", out_count ? "," : "", int(100*std::get<0>(*it) / total_ns),
299 std::get<1>(*it), std::get<2>(*it).c_str(), int(std::get<0>(*it) / 1000000000));
300 }
301 log("%s\n", out_count ? "" : " no commands executed");
302 }
303
304 #ifdef COVER_ACTIVE
305 if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
306 {
307 char filename_buffer[4096];
308 FILE *f;
309
310 if (getenv("YOSYS_COVER_DIR")) {
311 snprintf(filename_buffer, 4096, "%s/yosys_cover_%d_XXXXXX.txt", getenv("YOSYS_COVER_DIR"), getpid());
312 f = fdopen(mkstemps(filename_buffer, 4), "w");
313 } else {
314 snprintf(filename_buffer, 4096, "%s", getenv("YOSYS_COVER_FILE"));
315 f = fopen(filename_buffer, "a+");
316 }
317
318 if (f == NULL)
319 log_error("Can't create coverage file `%s'.\n", filename_buffer);
320
321 log("<writing coverage file \"%s\">\n", filename_buffer);
322
323 for (auto &it : get_coverage_data())
324 fprintf(f, "%-60s %10d %s\n", it.second.first.c_str(), it.second.second, it.first.c_str());
325
326 fclose(f);
327 }
328 #endif
329
330 if (call_abort)
331 abort();
332
333 if (!history_file.empty()) {
334 if (history_offset > 0) {
335 history_truncate_file(history_file.c_str(), 100);
336 append_history(where_history() - history_offset, history_file.c_str());
337 } else
338 write_history(history_file.c_str());
339 }
340
341 clear_history();
342 HIST_ENTRY **hist_list = history_list();
343 if (hist_list != NULL)
344 free(hist_list);
345
346 yosys_shutdown();
347
348 for (auto mod : loaded_modules)
349 dlclose(mod);
350
351 return 0;
352 }
353