2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
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.
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.
21 #include <readline/readline.h>
22 #include <readline/history.h>
30 #include "kernel/rtlil.h"
31 #include "kernel/register.h"
32 #include "kernel/log.h"
34 bool fgetline(FILE *f
, std::string
&buffer
)
39 if (fgets(block
, 4096, f
) == NULL
)
42 if (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r'))
47 static void run_frontend(std::string filename
, std::string command
, RTLIL::Design
*design
, std::string
*backend_command
)
49 if (command
== "auto") {
50 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
52 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
54 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".ys")
56 else if (filename
== "-")
59 log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename
.c_str());
62 if (command
== "script") {
63 log("\n-- Executing script file `%s' --\n", filename
.c_str());
66 f
= fopen(filename
.c_str(), "r");
68 log_error("Can't open script file `%s' for reading: %s\n", filename
.c_str(), strerror(errno
));
70 while (fgetline(f
, command
)) {
71 Pass::call(design
, command
);
74 if (!command
.empty()) {
75 Pass::call(design
, command
);
80 if (backend_command
!= NULL
&& *backend_command
== "auto")
81 *backend_command
= "";
85 if (filename
== "-") {
86 log("\n-- Parsing stdin using frontend `%s' --\n", command
.c_str());
88 log("\n-- Parsing `%s' using frontend `%s' --\n", filename
.c_str(), command
.c_str());
91 Frontend::frontend_call(design
, NULL
, filename
, command
);
95 static void run_pass(std::string command
, RTLIL::Design
*design
)
97 log("\n-- Running pass `%s' --\n", command
.c_str());
99 Pass::call(design
, command
);
103 static void run_backend(std::string filename
, std::string command
, RTLIL::Design
*design
)
105 if (command
== "auto") {
106 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
108 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
110 else if (filename
== "-")
112 else if (filename
.empty())
115 log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename
.c_str());
118 if (filename
.empty())
121 if (filename
== "-") {
122 log("\n-- Writing to stdout using backend `%s' --\n", command
.c_str());
124 log("\n-- Writing to `%s' using backend `%s' --\n", filename
.c_str(), command
.c_str());
127 Backend::backend_call(design
, NULL
, filename
, command
);
131 static char *readline_cmd_generator(const char *text
, int state
)
133 static std::map
<std::string
, Pass
*>::iterator it
;
137 it
= REGISTER_INTERN::pass_register
.begin();
141 for (; it
!= REGISTER_INTERN::pass_register
.end(); it
++) {
142 if (it
->first
.substr(0, len
) == text
)
143 return strdup((it
++)->first
.c_str());
148 static char *readline_obj_generator(const char *text
, int state
)
150 static std::vector
<char*> obj_names
;
158 RTLIL::Design
*design
= yosys_get_design();
159 int len
= strlen(text
);
161 if (design
->selected_active_module
.empty())
163 for (auto &it
: design
->modules
)
164 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
165 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
.c_str())));
168 if (design
->modules
.count(design
->selected_active_module
) > 0)
170 RTLIL::Module
*module
= design
->modules
.at(design
->selected_active_module
);
172 for (auto &it
: module
->wires
)
173 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
174 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
.c_str())));
176 for (auto &it
: module
->memories
)
177 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
178 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
.c_str())));
180 for (auto &it
: module
->cells
)
181 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
182 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
.c_str())));
184 for (auto &it
: module
->processes
)
185 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
186 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
.c_str())));
189 std::sort(obj_names
.begin(), obj_names
.end());
192 if (idx
< obj_names
.size())
193 return strdup(obj_names
[idx
++]);
200 static char **readline_completion(const char *text
, int start
, int)
203 return rl_completion_matches(text
, readline_cmd_generator
);
204 if (strncmp(rl_line_buffer
, "read_", 5) && strncmp(rl_line_buffer
, "write_", 6))
205 return rl_completion_matches(text
, readline_obj_generator
);
209 static const char *create_prompt(RTLIL::Design
*design
, int recursion_counter
)
211 static char buffer
[100];
212 std::string str
= "\n";
213 if (recursion_counter
> 1)
214 str
+= stringf("(%d) ", recursion_counter
);
216 if (!design
->selected_active_module
.empty())
217 str
+= stringf(" [%s]", design
->selected_active_module
.c_str());
218 if (!design
->selection_stack
.back().full_selection
) {
219 if (design
->selected_active_module
.empty())
221 else if (design
->selection_stack
.back().selected_modules
.size() != 1 || design
->selection_stack
.back().selected_members
.size() != 0 ||
222 design
->selection_stack
.back().selected_modules
.count(design
->selected_active_module
) == 0)
225 snprintf(buffer
, 100, "%s> ", str
.c_str());
229 static void shell(RTLIL::Design
*design
)
231 static int recursion_counter
= 0;
234 log_cmd_error_throw
= true;
236 rl_readline_name
= "yosys";
237 rl_attempted_completion_function
= readline_completion
;
239 char *command
= NULL
;
240 while ((command
= readline(create_prompt(design
, recursion_counter
))) != NULL
)
242 if (command
[strspn(command
, " \t\r\n")] == 0)
244 add_history(command
);
246 char *p
= command
+ strspn(command
, " \t\r\n");
247 if (!strncmp(p
, "exit", 4)) {
249 p
+= strspn(p
, " \t\r\n");
255 assert(design
->selection_stack
.size() == 1);
256 Pass::call(design
, command
);
258 while (design
->selection_stack
.size() > 1)
259 design
->selection_stack
.pop_back();
267 log_cmd_error_throw
= false;
270 struct ShellPass
: public Pass
{
271 ShellPass() : Pass("shell", "enter interactive command mode") { }
272 virtual void help() {
276 log("This command enters the interactive command mode. This can be useful\n");
277 log("in a script to interrupt the script at a certain point and allow for\n");
278 log("interactive inspection or manual synthesis of the design at this point.\n");
280 log("The command prompt of the interactive shell indicates the current\n");
281 log("selection (see 'help select'):\n");
284 log(" the entire design is selected\n");
287 log(" only part of the design is selected\n");
289 log(" yosys [modname]>\n");
290 log(" the entire module 'modname' is selected using 'select -module modname'\n");
292 log(" yosys [modname]*>\n");
293 log(" only part of current module 'modname' is selected\n");
295 log("When in interactive shell, some errors (e.g. invalid command arguments)\n");
296 log("do not terminate yosys but return to the command prompt.\n");
298 log("This command is the default action if nothing else has been specified\n");
299 log("on the command line.\n");
301 log("Press Ctrl-D or type 'exit' to leave the interactive shell.\n");
304 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
305 extra_args(args
, 1, design
, false);
310 struct ScriptPass
: public Pass
{
311 ScriptPass() : Pass("script", "execute commands from script file") { }
312 virtual void help() {
314 log(" script <filename>\n");
316 log("This command executes the yosys commands in the specified file.\n");
319 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
321 log_cmd_error("Missing script file.\n");
323 extra_args(args
, 1, design
, false);
324 run_frontend(args
[1], "script", design
, NULL
);
328 #ifdef YOSYS_ENABLE_TCL
329 static Tcl_Interp
*yosys_tcl_interp
= NULL
;
331 static int tcl_yosys_cmd(ClientData
, Tcl_Interp
*interp
, int argc
, const char *argv
[])
333 std::vector
<std::string
> args
;
334 for (int i
= 1; i
< argc
; i
++)
335 args
.push_back(argv
[i
]);
337 if (args
.size() >= 1 && args
[0] == "-import") {
338 for (auto &it
: REGISTER_INTERN::pass_register
) {
339 std::string tcl_command_name
= it
.first
;
340 if (tcl_command_name
== "proc")
341 tcl_command_name
= "procs";
343 if (Tcl_GetCommandInfo(interp
, tcl_command_name
.c_str(), &info
) != 0) {
344 log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it
.first
.c_str());
346 std::string tcl_script
= stringf("proc %s args { yosys %s {*}$args }", tcl_command_name
.c_str(), it
.first
.c_str());
347 Tcl_Eval(interp
, tcl_script
.c_str());
353 if (args
.size() == 1) {
354 Pass::call(yosys_get_design(), args
[0]);
358 Pass::call(yosys_get_design(), args
);
362 extern Tcl_Interp
*yosys_get_tcl_interp()
364 if (yosys_tcl_interp
== NULL
) {
365 yosys_tcl_interp
= Tcl_CreateInterp();
366 Tcl_CreateCommand(yosys_tcl_interp
, "yosys", tcl_yosys_cmd
, NULL
, NULL
);
368 return yosys_tcl_interp
;
371 struct TclPass
: public Pass
{
372 TclPass() : Pass("tcl", "execute a TCL script file") { }
373 virtual void help() {
375 log(" tcl <filename>\n");
377 log("This command executes the tcl commands in the specified file.\n");
378 log("Use 'yosys cmd' to run the yosys command 'cmd' from tcl.\n");
380 log("The tcl command 'yosys -import' can be used to import all yosys\n");
381 log("commands directly as tcl commands to the tcl shell. The yosys\n");
382 log("command 'proc' is wrapped using the tcl command 'procs' in order\n");
383 log("to avoid a name collision with the tcl builting command 'proc'.\n");
386 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
388 log_cmd_error("Missing script file.\n");
390 extra_args(args
, 1, design
, false);
391 if (Tcl_EvalFile(yosys_get_tcl_interp(), args
[1].c_str()) != TCL_OK
)
392 log_cmd_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
397 static RTLIL::Design
*yosys_design
= NULL
;
399 extern RTLIL::Design
*yosys_get_design()
404 std::string
rewrite_yosys_exe(std::string exe
)
407 ssize_t buflen
= readlink("/proc/self/exe", buffer
, sizeof(buffer
)-1);
413 std::string newexe
= stringf("%s/%s", dirname(buffer
), exe
.c_str());
414 if (access(newexe
.c_str(), X_OK
) == 0)
420 int main(int argc
, char **argv
)
422 std::string frontend_command
= "auto";
423 std::string backend_command
= "auto";
424 std::vector
<std::string
> passes_commands
;
425 std::vector
<void*> loaded_modules
;
426 std::string output_filename
= "";
427 std::string scriptfile
= "";
428 bool scriptfile_tcl
= false;
429 bool got_output_filename
= false;
432 while ((opt
= getopt(argc
, argv
, "Sm:f:b:o:p:l:qts:c:")) != -1)
437 backend_command
= "verilog -noattr";
438 passes_commands
.push_back("hierarchy");
439 passes_commands
.push_back("proc");
440 passes_commands
.push_back("opt");
441 passes_commands
.push_back("memory");
442 passes_commands
.push_back("techmap");
443 passes_commands
.push_back("opt");
446 loaded_modules
.push_back(dlopen(optarg
, RTLD_LAZY
|RTLD_GLOBAL
));
447 if (loaded_modules
.back() == NULL
) {
448 fprintf(stderr
, "Can't load module `%s': %s\n", optarg
, dlerror());
453 frontend_command
= optarg
;
456 backend_command
= optarg
;
459 passes_commands
.push_back(optarg
);
462 output_filename
= optarg
;
463 got_output_filename
= true;
466 log_files
.push_back(fopen(optarg
, "wt"));
467 if (log_files
.back() == NULL
) {
468 fprintf(stderr
, "Can't open log file `%s' for writing!\n", optarg
);
473 log_errfile
= stderr
;
480 scriptfile_tcl
= false;
484 scriptfile_tcl
= true;
487 fprintf(stderr
, "\n");
488 fprintf(stderr
, "Usage: %s [-S] [-q] [-t] [-l logfile] [-o <outfile>] [-f <frontend>] [{-s|-c} <scriptfile>]\n", argv
[0]);
489 fprintf(stderr
, " %*s[-p <pass> [-p ..]] [-b <backend>] [-m <module_file>] [<infile> [..]]\n", int(strlen(argv
[0])+1), "");
490 fprintf(stderr
, "\n");
491 fprintf(stderr
, " -q\n");
492 fprintf(stderr
, " quiet operation. only write error messages to console\n");
493 fprintf(stderr
, "\n");
494 fprintf(stderr
, " -t\n");
495 fprintf(stderr
, " annotate all log messages with a time stamp\n");
496 fprintf(stderr
, "\n");
497 fprintf(stderr
, " -l logfile\n");
498 fprintf(stderr
, " write log messages to the specified file\n");
499 fprintf(stderr
, "\n");
500 fprintf(stderr
, " -o outfile\n");
501 fprintf(stderr
, " write the design to the specified file on exit\n");
502 fprintf(stderr
, "\n");
503 fprintf(stderr
, " -b backend\n");
504 fprintf(stderr
, " use this backend for the output file specified on the command line\n");
505 fprintf(stderr
, "\n");
506 fprintf(stderr
, " -f backend\n");
507 fprintf(stderr
, " use the specified front for the input files on the command line\n");
508 fprintf(stderr
, "\n");
509 fprintf(stderr
, " -s scriptfile\n");
510 fprintf(stderr
, " execute the commands in the script file\n");
511 fprintf(stderr
, "\n");
512 fprintf(stderr
, " -c tcl_scriptfile\n");
513 fprintf(stderr
, " execute the commands in the tcl script file (see 'help tcl' for details)\n");
514 fprintf(stderr
, "\n");
515 fprintf(stderr
, " -p command\n");
516 fprintf(stderr
, " execute the commands\n");
517 fprintf(stderr
, "\n");
518 fprintf(stderr
, " -m module_file\n");
519 fprintf(stderr
, " load the specified module (aka plugin)\n");
520 fprintf(stderr
, "\n");
521 fprintf(stderr
, "The option -S is an alias for the following options that perform a simple\n");
522 fprintf(stderr
, "transformation of the input to a gate-level netlist. This can be helpful when\n");
523 fprintf(stderr
, "e.g. using yosys as a pre-processor for a tool that can't understand full verilog.\n");
524 fprintf(stderr
, "\n");
525 fprintf(stderr
, " -b 'verilog -noattr' -p hierarchy -p proc -p opt -p memory -p techmap -p opt\n");
526 fprintf(stderr
, "\n");
527 fprintf(stderr
, "For more complex synthesis jobs it is recommended to use the read_* and write_*\n");
528 fprintf(stderr
, "commands in a script file instead of specifying input and output files on the\n");
529 fprintf(stderr
, "command line.\n");
530 fprintf(stderr
, "\n");
531 fprintf(stderr
, "When no commands, script files and input files are specified on the command\n");
532 fprintf(stderr
, "line, yosys automatically enters the interactive command mode. Use the 'help'\n");
533 fprintf(stderr
, "command to get information on the individual commands.\n");
534 fprintf(stderr
, "\n");
539 if (log_errfile
== NULL
)
540 log_files
.push_back(stderr
);
543 log(" /-----------------------------------------------------------------------------\\\n");
545 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
547 log(" | Copyright (C) 2012 Clifford Wolf <clifford@clifford.at> |\n");
549 log(" | Permission to use, copy, modify, and/or distribute this software for any |\n");
550 log(" | purpose with or without fee is hereby granted, provided that the above |\n");
551 log(" | copyright notice and this permission notice appear in all copies. |\n");
553 log(" | THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |\n");
554 log(" | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |\n");
555 log(" | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |\n");
556 log(" | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |\n");
557 log(" | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |\n");
558 log(" | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |\n");
559 log(" | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |\n");
561 log(" \\-----------------------------------------------------------------------------/\n");
564 Pass::init_register();
566 yosys_design
= new RTLIL::Design
;
567 yosys_design
->selection_stack
.push_back(RTLIL::Selection());
570 if (optind
== argc
&& passes_commands
.size() == 0 && scriptfile
.empty()) {
571 if (!got_output_filename
)
572 backend_command
= "";
576 while (optind
< argc
)
577 run_frontend(argv
[optind
++], frontend_command
, yosys_design
, output_filename
== "-" ? &backend_command
: NULL
);
579 if (!scriptfile
.empty()) {
580 if (scriptfile_tcl
) {
581 #ifdef YOSYS_ENABLE_TCL
582 if (Tcl_EvalFile(yosys_get_tcl_interp(), scriptfile
.c_str()) != TCL_OK
)
583 log_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
585 log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
588 run_frontend(scriptfile
, "script", yosys_design
, output_filename
== "-" ? &backend_command
: NULL
);
591 for (auto it
= passes_commands
.begin(); it
!= passes_commands
.end(); it
++)
592 run_pass(*it
, yosys_design
);
594 if (!backend_command
.empty())
595 run_backend(output_filename
, backend_command
, yosys_design
);
603 for (auto f
: log_files
)
609 Pass::done_register();
611 for (auto mod
: loaded_modules
)
614 #ifdef YOSYS_ENABLE_TCL
615 if (yosys_tcl_interp
!= NULL
) {
616 Tcl_DeleteInterp(yosys_tcl_interp
);
618 yosys_tcl_interp
= NULL
;