Added support for scripts with labels
[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 <stdio.h>
21 #include <readline/readline.h>
22 #include <readline/history.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <libgen.h>
26 #include <dlfcn.h>
27 #include <limits.h>
28 #include <errno.h>
29
30 #include <algorithm>
31
32 #include "kernel/rtlil.h"
33 #include "kernel/register.h"
34 #include "kernel/log.h"
35
36 bool fgetline(FILE *f, std::string &buffer)
37 {
38 buffer = "";
39 char block[4096];
40 while (1) {
41 if (fgets(block, 4096, f) == NULL)
42 return false;
43 buffer += block;
44 if (buffer.size() > 0 && (buffer[buffer.size()-1] == '\n' || buffer[buffer.size()-1] == '\r')) {
45 while (buffer.size() > 0 && (buffer[buffer.size()-1] == '\n' || buffer[buffer.size()-1] == '\r'))
46 buffer.resize(buffer.size()-1);
47 return true;
48 }
49 }
50 }
51
52 static void handle_label(std::string &command, bool &from_to_active, const std::string &run_from, const std::string &run_to)
53 {
54 int pos = 0;
55 std::string label;
56
57 while (pos < SIZE(command) && (command[pos] == ' ' || command[pos] == '\t'))
58 pos++;
59
60 while (pos < SIZE(command) && command[pos] != ' ' && command[pos] != '\t' && command[pos] != '\r' && command[pos] != '\n')
61 label += command[pos++];
62
63 if (label.back() == ':' && SIZE(label) > 1)
64 {
65 label = label.substr(0, SIZE(label)-1);
66 command = command.substr(pos);
67
68 if (label == run_from)
69 from_to_active = true;
70 else if (label == run_to || (run_from == run_to && !run_from.empty()))
71 from_to_active = false;
72 }
73 }
74
75 static void run_frontend(std::string filename, std::string command, RTLIL::Design *design, std::string *backend_command, std::string *from_to_label)
76 {
77 if (command == "auto") {
78 if (filename.size() > 2 && filename.substr(filename.size()-2) == ".v")
79 command = "verilog";
80 else if (filename.size() > 2 && filename.substr(filename.size()-3) == ".sv")
81 command = "verilog -sv";
82 else if (filename.size() > 3 && filename.substr(filename.size()-3) == ".il")
83 command = "ilang";
84 else if (filename.size() > 3 && filename.substr(filename.size()-3) == ".ys")
85 command = "script";
86 else if (filename == "-")
87 command = "script";
88 else
89 log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename.c_str());
90 }
91
92 if (command == "script")
93 {
94 std::string run_from, run_to;
95 bool from_to_active = true;
96
97 if (from_to_label != NULL) {
98 size_t pos = from_to_label->find(':');
99 if (pos == std::string::npos) {
100 run_from = *from_to_label;
101 run_to = *from_to_label;
102 } else {
103 run_from = from_to_label->substr(0, pos);
104 run_to = from_to_label->substr(pos+1);
105 }
106 from_to_active = run_from.empty();
107 }
108
109 log("\n-- Executing script file `%s' --\n", filename.c_str());
110
111 FILE *f = stdin;
112
113 if (filename != "-")
114 f = fopen(filename.c_str(), "r");
115
116 if (f == NULL)
117 log_error("Can't open script file `%s' for reading: %s\n", filename.c_str(), strerror(errno));
118
119 std::string command;
120 while (fgetline(f, command)) {
121 while (!command.empty() && command[command.size()-1] == '\\') {
122 std::string next_line;
123 if (!fgetline(f, next_line))
124 break;
125 command.resize(command.size()-1);
126 command += next_line;
127 }
128 handle_label(command, from_to_active, run_from, run_to);
129 if (from_to_active)
130 Pass::call(design, command);
131 }
132
133 if (!command.empty()) {
134 handle_label(command, from_to_active, run_from, run_to);
135 if (from_to_active)
136 Pass::call(design, command);
137 }
138
139 if (filename != "-")
140 fclose(f);
141
142 if (backend_command != NULL && *backend_command == "auto")
143 *backend_command = "";
144
145 return;
146 }
147
148 if (filename == "-") {
149 log("\n-- Parsing stdin using frontend `%s' --\n", command.c_str());
150 } else {
151 log("\n-- Parsing `%s' using frontend `%s' --\n", filename.c_str(), command.c_str());
152 }
153
154 Frontend::frontend_call(design, NULL, filename, command);
155 }
156
157 static void run_pass(std::string command, RTLIL::Design *design)
158 {
159 log("\n-- Running pass `%s' --\n", command.c_str());
160
161 Pass::call(design, command);
162 }
163
164 static void run_backend(std::string filename, std::string command, RTLIL::Design *design)
165 {
166 if (command == "auto") {
167 if (filename.size() > 2 && filename.substr(filename.size()-2) == ".v")
168 command = "verilog";
169 else if (filename.size() > 3 && filename.substr(filename.size()-3) == ".il")
170 command = "ilang";
171 else if (filename.size() > 5 && filename.substr(filename.size()-5) == ".blif")
172 command = "blif";
173 else if (filename == "-")
174 command = "ilang";
175 else if (filename.empty())
176 return;
177 else
178 log_error("Can't guess backend for output file `%s' (missing -b option)!\n", filename.c_str());
179 }
180
181 if (filename.empty())
182 filename = "-";
183
184 if (filename == "-") {
185 log("\n-- Writing to stdout using backend `%s' --\n", command.c_str());
186 } else {
187 log("\n-- Writing to `%s' using backend `%s' --\n", filename.c_str(), command.c_str());
188 }
189
190 Backend::backend_call(design, NULL, filename, command);
191 }
192
193 static char *readline_cmd_generator(const char *text, int state)
194 {
195 static std::map<std::string, Pass*>::iterator it;
196 static int len;
197
198 if (!state) {
199 it = REGISTER_INTERN::pass_register.begin();
200 len = strlen(text);
201 }
202
203 for (; it != REGISTER_INTERN::pass_register.end(); it++) {
204 if (it->first.substr(0, len) == text)
205 return strdup((it++)->first.c_str());
206 }
207 return NULL;
208 }
209
210 static char *readline_obj_generator(const char *text, int state)
211 {
212 static std::vector<char*> obj_names;
213 static size_t idx;
214
215 if (!state)
216 {
217 idx = 0;
218 obj_names.clear();
219
220 RTLIL::Design *design = yosys_get_design();
221 int len = strlen(text);
222
223 if (design->selected_active_module.empty())
224 {
225 for (auto &it : design->modules)
226 if (RTLIL::unescape_id(it.first).substr(0, len) == text)
227 obj_names.push_back(strdup(RTLIL::id2cstr(it.first.c_str())));
228 }
229 else
230 if (design->modules.count(design->selected_active_module) > 0)
231 {
232 RTLIL::Module *module = design->modules.at(design->selected_active_module);
233
234 for (auto &it : module->wires)
235 if (RTLIL::unescape_id(it.first).substr(0, len) == text)
236 obj_names.push_back(strdup(RTLIL::id2cstr(it.first.c_str())));
237
238 for (auto &it : module->memories)
239 if (RTLIL::unescape_id(it.first).substr(0, len) == text)
240 obj_names.push_back(strdup(RTLIL::id2cstr(it.first.c_str())));
241
242 for (auto &it : module->cells)
243 if (RTLIL::unescape_id(it.first).substr(0, len) == text)
244 obj_names.push_back(strdup(RTLIL::id2cstr(it.first.c_str())));
245
246 for (auto &it : module->processes)
247 if (RTLIL::unescape_id(it.first).substr(0, len) == text)
248 obj_names.push_back(strdup(RTLIL::id2cstr(it.first.c_str())));
249 }
250
251 std::sort(obj_names.begin(), obj_names.end());
252 }
253
254 if (idx < obj_names.size())
255 return strdup(obj_names[idx++]);
256
257 idx = 0;
258 obj_names.clear();
259 return NULL;
260 }
261
262 static char **readline_completion(const char *text, int start, int)
263 {
264 if (start == 0)
265 return rl_completion_matches(text, readline_cmd_generator);
266 if (strncmp(rl_line_buffer, "read_", 5) && strncmp(rl_line_buffer, "write_", 6))
267 return rl_completion_matches(text, readline_obj_generator);
268 return NULL;
269 }
270
271 const char *create_prompt(RTLIL::Design *design, int recursion_counter)
272 {
273 static char buffer[100];
274 std::string str = "\n";
275 if (recursion_counter > 1)
276 str += stringf("(%d) ", recursion_counter);
277 str += "yosys";
278 if (!design->selected_active_module.empty())
279 str += stringf(" [%s]", RTLIL::id2cstr(design->selected_active_module));
280 if (!design->selection_stack.back().full_selection) {
281 if (design->selected_active_module.empty())
282 str += "*";
283 else if (design->selection_stack.back().selected_modules.size() != 1 || design->selection_stack.back().selected_members.size() != 0 ||
284 design->selection_stack.back().selected_modules.count(design->selected_active_module) == 0)
285 str += "*";
286 }
287 snprintf(buffer, 100, "%s> ", str.c_str());
288 return buffer;
289 }
290
291 static void shell(RTLIL::Design *design)
292 {
293 static int recursion_counter = 0;
294
295 recursion_counter++;
296 log_cmd_error_throw = true;
297
298 rl_readline_name = "yosys";
299 rl_attempted_completion_function = readline_completion;
300 rl_basic_word_break_characters = " \t\n";
301
302 char *command = NULL;
303 while ((command = readline(create_prompt(design, recursion_counter))) != NULL)
304 {
305 if (command[strspn(command, " \t\r\n")] == 0)
306 continue;
307 add_history(command);
308
309 char *p = command + strspn(command, " \t\r\n");
310 if (!strncmp(p, "exit", 4)) {
311 p += 4;
312 p += strspn(p, " \t\r\n");
313 if (*p == 0)
314 break;
315 }
316
317 try {
318 assert(design->selection_stack.size() == 1);
319 Pass::call(design, command);
320 } catch (int) {
321 while (design->selection_stack.size() > 1)
322 design->selection_stack.pop_back();
323 log_reset_stack();
324 }
325 }
326 if (command == NULL)
327 printf("exit\n");
328
329 recursion_counter--;
330 log_cmd_error_throw = false;
331 }
332
333 struct ShellPass : public Pass {
334 ShellPass() : Pass("shell", "enter interactive command mode") { }
335 virtual void help() {
336 log("\n");
337 log(" shell\n");
338 log("\n");
339 log("This command enters the interactive command mode. This can be useful\n");
340 log("in a script to interrupt the script at a certain point and allow for\n");
341 log("interactive inspection or manual synthesis of the design at this point.\n");
342 log("\n");
343 log("The command prompt of the interactive shell indicates the current\n");
344 log("selection (see 'help select'):\n");
345 log("\n");
346 log(" yosys>\n");
347 log(" the entire design is selected\n");
348 log("\n");
349 log(" yosys*>\n");
350 log(" only part of the design is selected\n");
351 log("\n");
352 log(" yosys [modname]>\n");
353 log(" the entire module 'modname' is selected using 'select -module modname'\n");
354 log("\n");
355 log(" yosys [modname]*>\n");
356 log(" only part of current module 'modname' is selected\n");
357 log("\n");
358 log("When in interactive shell, some errors (e.g. invalid command arguments)\n");
359 log("do not terminate yosys but return to the command prompt.\n");
360 log("\n");
361 log("This command is the default action if nothing else has been specified\n");
362 log("on the command line.\n");
363 log("\n");
364 log("Press Ctrl-D or type 'exit' to leave the interactive shell.\n");
365 log("\n");
366 }
367 virtual void execute(std::vector<std::string> args, RTLIL::Design *design) {
368 extra_args(args, 1, design, false);
369 shell(design);
370 }
371 } ShellPass;
372
373 struct HistoryPass : public Pass {
374 HistoryPass() : Pass("history", "show last interactive commands") { }
375 virtual void help() {
376 log("\n");
377 log(" history\n");
378 log("\n");
379 log("This command prints all commands in the shell history buffer. This are\n");
380 log("all commands executed in an interactive session, but not the commands\n");
381 log("from executed scripts.\n");
382 log("\n");
383 }
384 virtual void execute(std::vector<std::string> args, RTLIL::Design *design) {
385 extra_args(args, 1, design, false);
386 for(HIST_ENTRY **list = history_list(); *list != NULL; list++)
387 log("%s\n", (*list)->line);
388 }
389 } HistoryPass;
390
391 struct ScriptPass : public Pass {
392 ScriptPass() : Pass("script", "execute commands from script file") { }
393 virtual void help() {
394 log("\n");
395 log(" script <filename> [<from_label>:<to_label>]\n");
396 log("\n");
397 log("This command executes the yosys commands in the specified file.\n");
398 log("\n");
399 log("The 2nd argument can be used to only execute the section of the\n");
400 log("file between the specified labels. An empty from label is synonymous\n");
401 log("for the beginning of the file and an empty to label is synonymous\n");
402 log("for the end of the file.\n");
403 log("\n");
404 log("If only one label is specified (without ':') then only the block\n");
405 log("marked with that label (until the next label) is executed.\n");
406 log("\n");
407 }
408 virtual void execute(std::vector<std::string> args, RTLIL::Design *design) {
409 if (args.size() < 2)
410 log_cmd_error("Missing script file.\n");
411 else if (args.size() == 2)
412 run_frontend(args[1], "script", design, NULL, NULL);
413 else if (args.size() == 3)
414 run_frontend(args[1], "script", design, NULL, &args[2]);
415 else
416 extra_args(args, 2, design, false);
417 }
418 } ScriptPass;
419
420 #ifdef YOSYS_ENABLE_TCL
421 static Tcl_Interp *yosys_tcl_interp = NULL;
422
423 static int tcl_yosys_cmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
424 {
425 std::vector<std::string> args;
426 for (int i = 1; i < argc; i++)
427 args.push_back(argv[i]);
428
429 if (args.size() >= 1 && args[0] == "-import") {
430 for (auto &it : REGISTER_INTERN::pass_register) {
431 std::string tcl_command_name = it.first;
432 if (tcl_command_name == "proc")
433 tcl_command_name = "procs";
434 Tcl_CmdInfo info;
435 if (Tcl_GetCommandInfo(interp, tcl_command_name.c_str(), &info) != 0) {
436 log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it.first.c_str());
437 } else {
438 std::string tcl_script = stringf("proc %s args { yosys %s {*}$args }", tcl_command_name.c_str(), it.first.c_str());
439 Tcl_Eval(interp, tcl_script.c_str());
440 }
441 }
442 return TCL_OK;
443 }
444
445 if (args.size() == 1) {
446 Pass::call(yosys_get_design(), args[0]);
447 return TCL_OK;
448 }
449
450 Pass::call(yosys_get_design(), args);
451 return TCL_OK;
452 }
453
454 extern Tcl_Interp *yosys_get_tcl_interp()
455 {
456 if (yosys_tcl_interp == NULL) {
457 yosys_tcl_interp = Tcl_CreateInterp();
458 Tcl_CreateCommand(yosys_tcl_interp, "yosys", tcl_yosys_cmd, NULL, NULL);
459 }
460 return yosys_tcl_interp;
461 }
462
463 struct TclPass : public Pass {
464 TclPass() : Pass("tcl", "execute a TCL script file") { }
465 virtual void help() {
466 log("\n");
467 log(" tcl <filename>\n");
468 log("\n");
469 log("This command executes the tcl commands in the specified file.\n");
470 log("Use 'yosys cmd' to run the yosys command 'cmd' from tcl.\n");
471 log("\n");
472 log("The tcl command 'yosys -import' can be used to import all yosys\n");
473 log("commands directly as tcl commands to the tcl shell. The yosys\n");
474 log("command 'proc' is wrapped using the tcl command 'procs' in order\n");
475 log("to avoid a name collision with the tcl builting command 'proc'.\n");
476 log("\n");
477 }
478 virtual void execute(std::vector<std::string> args, RTLIL::Design *design) {
479 if (args.size() < 2)
480 log_cmd_error("Missing script file.\n");
481 if (args.size() > 2)
482 extra_args(args, 1, design, false);
483 if (Tcl_EvalFile(yosys_get_tcl_interp(), args[1].c_str()) != TCL_OK)
484 log_cmd_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
485 }
486 } TclPass;
487 #endif
488
489 static RTLIL::Design *yosys_design = NULL;
490
491 extern RTLIL::Design *yosys_get_design()
492 {
493 return yosys_design;
494 }
495
496 #if defined(__linux__)
497 std::string proc_self_dirname ()
498 {
499 char path [PATH_MAX];
500 ssize_t buflen = readlink("/proc/self/exe", path, sizeof(path));
501 if (buflen < 0) {
502 log_cmd_error("readlink(\"/proc/self/exe\") failed: %s", strerror(errno));
503 log_abort();
504 }
505 while (buflen > 0 && path[buflen-1] != '/')
506 buflen--;
507 return std::string(path, buflen);
508 }
509 #elif defined(__APPLE__)
510 #include <mach-o/dyld.h>
511 std::string proc_self_dirname ()
512 {
513 char * path = NULL;
514 uint32_t buflen = 0;
515 while (_NSGetExecutablePath(path, &buflen) != 0)
516 path = (char *) realloc((void *) path, buflen);
517 while (buflen > 0 && path[buflen-1] != '/')
518 buflen--;
519 return std::string(path, buflen);
520 }
521 #else
522 #error Dont know how to determine process executable base path!
523 #endif
524
525 std::string proc_share_dirname ()
526 {
527 std::string proc_self_path = proc_self_dirname();
528 std::string proc_share_path = proc_self_path + "share/";
529 if (access(proc_share_path.c_str(), X_OK) == 0)
530 return proc_share_path;
531 proc_share_path = proc_self_path + "../share/yosys/";
532 if (access(proc_share_path.c_str(), X_OK) == 0)
533 return proc_share_path;
534 log_cmd_error("proc_share_dirname: unable to determine share/ directory!");
535 log_abort();
536 }
537
538 int main(int argc, char **argv)
539 {
540 std::string frontend_command = "auto";
541 std::string backend_command = "auto";
542 std::vector<std::string> passes_commands;
543 std::vector<void*> loaded_modules;
544 std::string output_filename = "";
545 std::string scriptfile = "";
546 bool scriptfile_tcl = false;
547 bool got_output_filename = false;
548
549 int history_offset = 0;
550 std::string history_file;
551 if (getenv("HOME") != NULL) {
552 history_file = stringf("%s/.yosys_history", getenv("HOME"));
553 read_history(history_file.c_str());
554 history_offset = where_history();
555 }
556
557 int opt;
558 while ((opt = getopt(argc, argv, "VSm:f:Hh:b:o:p:l:qv:ts:c:")) != -1)
559 {
560 switch (opt)
561 {
562 case 'V':
563 printf("%s\n", yosys_version_str);
564 exit(0);
565 case 'S':
566 passes_commands.push_back("hierarchy");
567 passes_commands.push_back("proc");
568 passes_commands.push_back("opt");
569 passes_commands.push_back("memory");
570 passes_commands.push_back("opt");
571 passes_commands.push_back("techmap");
572 passes_commands.push_back("opt");
573 break;
574 case 'm':
575 loaded_modules.push_back(dlopen(optarg, RTLD_LAZY|RTLD_GLOBAL));
576 if (loaded_modules.back() == NULL) {
577 fprintf(stderr, "Can't load module `%s': %s\n", optarg, dlerror());
578 exit(1);
579 }
580 break;
581 case 'f':
582 frontend_command = optarg;
583 break;
584 case 'H':
585 passes_commands.push_back("help");
586 break;
587 case 'h':
588 passes_commands.push_back(stringf("help %s", optarg));
589 break;
590 case 'b':
591 backend_command = optarg;
592 break;
593 case 'p':
594 passes_commands.push_back(optarg);
595 break;
596 case 'o':
597 output_filename = optarg;
598 got_output_filename = true;
599 break;
600 case 'l':
601 log_files.push_back(fopen(optarg, "wt"));
602 if (log_files.back() == NULL) {
603 fprintf(stderr, "Can't open log file `%s' for writing!\n", optarg);
604 exit(1);
605 }
606 break;
607 case 'q':
608 log_errfile = stderr;
609 break;
610 case 'v':
611 log_errfile = stderr;
612 log_verbose_level = atoi(optarg);
613 break;
614 case 't':
615 log_time = true;
616 break;
617 case 's':
618 scriptfile = optarg;
619 scriptfile_tcl = false;
620 break;
621 case 'c':
622 scriptfile = optarg;
623 scriptfile_tcl = true;
624 break;
625 default:
626 fprintf(stderr, "\n");
627 fprintf(stderr, "Usage: %s [-V] [-S] [-q] [-v <level>[-t] [-l <logfile>] [-o <outfile>] [-f <frontend>] [-h cmd] \\\n", argv[0]);
628 fprintf(stderr, " %*s[{-s|-c} <scriptfile>] [-p <pass> [-p ..]] [-b <backend>] [-m <module_file>] [<infile> [..]]\n", int(strlen(argv[0])+1), "");
629 fprintf(stderr, "\n");
630 fprintf(stderr, " -q\n");
631 fprintf(stderr, " quiet operation. only write error messages to console\n");
632 fprintf(stderr, "\n");
633 fprintf(stderr, " -v <level>\n");
634 fprintf(stderr, " print log headers up to level <level> to the console. (implies -q)\n");
635 fprintf(stderr, "\n");
636 fprintf(stderr, " -t\n");
637 fprintf(stderr, " annotate all log messages with a time stamp\n");
638 fprintf(stderr, "\n");
639 fprintf(stderr, " -l logfile\n");
640 fprintf(stderr, " write log messages to the specified file\n");
641 fprintf(stderr, "\n");
642 fprintf(stderr, " -o outfile\n");
643 fprintf(stderr, " write the design to the specified file on exit\n");
644 fprintf(stderr, "\n");
645 fprintf(stderr, " -b backend\n");
646 fprintf(stderr, " use this backend for the output file specified on the command line\n");
647 fprintf(stderr, "\n");
648 fprintf(stderr, " -f backend\n");
649 fprintf(stderr, " use the specified front for the input files on the command line\n");
650 fprintf(stderr, "\n");
651 fprintf(stderr, " -H\n");
652 fprintf(stderr, " print the command list\n");
653 fprintf(stderr, "\n");
654 fprintf(stderr, " -h command\n");
655 fprintf(stderr, " print the help message for the specified command\n");
656 fprintf(stderr, "\n");
657 fprintf(stderr, " -s scriptfile\n");
658 fprintf(stderr, " execute the commands in the script file\n");
659 fprintf(stderr, "\n");
660 fprintf(stderr, " -c tcl_scriptfile\n");
661 fprintf(stderr, " execute the commands in the tcl script file (see 'help tcl' for details)\n");
662 fprintf(stderr, "\n");
663 fprintf(stderr, " -p command\n");
664 fprintf(stderr, " execute the commands\n");
665 fprintf(stderr, "\n");
666 fprintf(stderr, " -m module_file\n");
667 fprintf(stderr, " load the specified module (aka plugin)\n");
668 fprintf(stderr, "\n");
669 fprintf(stderr, " -V\n");
670 fprintf(stderr, " print version information and exit\n");
671 fprintf(stderr, "\n");
672 fprintf(stderr, "The option -S is an alias for the following options that perform a simple\n");
673 fprintf(stderr, "transformation of the input to a gate-level netlist.\n");
674 fprintf(stderr, "\n");
675 fprintf(stderr, " -p hierarchy -p proc -p opt -p memory -p opt -p techmap -p opt\n");
676 fprintf(stderr, "\n");
677 fprintf(stderr, "For more complex synthesis jobs it is recommended to use the read_* and write_*\n");
678 fprintf(stderr, "commands in a script file instead of specifying input and output files on the\n");
679 fprintf(stderr, "command line.\n");
680 fprintf(stderr, "\n");
681 fprintf(stderr, "When no commands, script files or input files are specified on the command\n");
682 fprintf(stderr, "line, yosys automatically enters the interactive command mode. Use the 'help'\n");
683 fprintf(stderr, "command to get information on the individual commands.\n");
684 fprintf(stderr, "\n");
685 exit(1);
686 }
687 }
688
689 if (log_errfile == NULL)
690 log_files.push_back(stderr);
691
692 log("\n");
693 log(" /-----------------------------------------------------------------------------\\\n");
694 log(" | |\n");
695 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
696 log(" | |\n");
697 log(" | Copyright (C) 2012 Clifford Wolf <clifford@clifford.at> |\n");
698 log(" | |\n");
699 log(" | Permission to use, copy, modify, and/or distribute this software for any |\n");
700 log(" | purpose with or without fee is hereby granted, provided that the above |\n");
701 log(" | copyright notice and this permission notice appear in all copies. |\n");
702 log(" | |\n");
703 log(" | THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |\n");
704 log(" | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |\n");
705 log(" | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |\n");
706 log(" | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |\n");
707 log(" | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |\n");
708 log(" | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |\n");
709 log(" | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |\n");
710 log(" | |\n");
711 log(" \\-----------------------------------------------------------------------------/\n");
712 log("\n");
713 log(" %s\n", yosys_version_str);
714 log("\n");
715
716 Pass::init_register();
717
718 yosys_design = new RTLIL::Design;
719 yosys_design->selection_stack.push_back(RTLIL::Selection());
720 log_push();
721
722 if (optind == argc && passes_commands.size() == 0 && scriptfile.empty()) {
723 if (!got_output_filename)
724 backend_command = "";
725 shell(yosys_design);
726 }
727
728 while (optind < argc)
729 run_frontend(argv[optind++], frontend_command, yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
730
731 if (!scriptfile.empty()) {
732 if (scriptfile_tcl) {
733 #ifdef YOSYS_ENABLE_TCL
734 if (Tcl_EvalFile(yosys_get_tcl_interp(), scriptfile.c_str()) != TCL_OK)
735 log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
736 #else
737 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
738 #endif
739 } else
740 run_frontend(scriptfile, "script", yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
741 }
742
743 for (auto it = passes_commands.begin(); it != passes_commands.end(); it++)
744 run_pass(*it, yosys_design);
745
746 if (!backend_command.empty())
747 run_backend(output_filename, backend_command, yosys_design);
748
749 delete yosys_design;
750 yosys_design = NULL;
751
752 log("\nREADY.\n");
753 log_pop();
754
755 if (!history_file.empty()) {
756 if (history_offset > 0) {
757 history_truncate_file(history_file.c_str(), 100);
758 append_history(where_history() - history_offset, history_file.c_str());
759 } else
760 write_history(history_file.c_str());
761 }
762
763 clear_history();
764 HIST_ENTRY **hist_list = history_list();
765 if (hist_list != NULL)
766 free(hist_list);
767
768 for (auto f : log_files)
769 if (f != stderr)
770 fclose(f);
771 log_errfile = NULL;
772 log_files.clear();
773
774 Pass::done_register();
775
776 for (auto mod : loaded_modules)
777 dlclose(mod);
778
779 #ifdef YOSYS_ENABLE_TCL
780 if (yosys_tcl_interp != NULL) {
781 Tcl_DeleteInterp(yosys_tcl_interp);
782 Tcl_Finalize();
783 yosys_tcl_interp = NULL;
784 }
785 #endif
786
787 return 0;
788 }
789