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.
20 #include "kernel/yosys.h"
22 #include <readline/readline.h>
23 #include <readline/history.h>
31 RTLIL::Design
*yosys_design
= NULL
;
33 #ifdef YOSYS_ENABLE_TCL
34 Tcl_Interp
*yosys_tcl_interp
= NULL
;
37 std::string
stringf(const char *fmt
, ...)
43 string
= vstringf(fmt
, ap
);
49 std::string
vstringf(const char *fmt
, va_list ap
)
54 if (vasprintf(&str
, fmt
, ap
) < 0)
65 int SIZE(RTLIL::Wire
*wire
)
72 Pass::init_register();
74 yosys_design
= new RTLIL::Design
;
75 yosys_design
->selection_stack
.push_back(RTLIL::Selection());
86 for (auto f
: log_files
)
92 Pass::done_register();
94 #ifdef YOSYS_ENABLE_TCL
95 if (yosys_tcl_interp
!= NULL
) {
96 Tcl_DeleteInterp(yosys_tcl_interp
);
98 yosys_tcl_interp
= NULL
;
103 RTLIL::IdString
new_id(std::string file
, int line
, std::string func
)
105 std::string str
= "$auto$";
106 size_t pos
= file
.find_last_of('/');
107 str
+= pos
!= std::string::npos
? file
.substr(pos
+1) : file
;
108 str
+= stringf(":%d:%s$%d", line
, func
.c_str(), autoidx
++);
112 RTLIL::Design
*yosys_get_design()
117 const char *create_prompt(RTLIL::Design
*design
, int recursion_counter
)
119 static char buffer
[100];
120 std::string str
= "\n";
121 if (recursion_counter
> 1)
122 str
+= stringf("(%d) ", recursion_counter
);
124 if (!design
->selected_active_module
.empty())
125 str
+= stringf(" [%s]", RTLIL::id2cstr(design
->selected_active_module
));
126 if (!design
->selection_stack
.empty() && !design
->selection_stack
.back().full_selection
) {
127 if (design
->selected_active_module
.empty())
129 else if (design
->selection_stack
.back().selected_modules
.size() != 1 || design
->selection_stack
.back().selected_members
.size() != 0 ||
130 design
->selection_stack
.back().selected_modules
.count(design
->selected_active_module
) == 0)
133 snprintf(buffer
, 100, "%s> ", str
.c_str());
137 #ifdef YOSYS_ENABLE_TCL
138 static int tcl_yosys_cmd(ClientData
, Tcl_Interp
*interp
, int argc
, const char *argv
[])
140 std::vector
<std::string
> args
;
141 for (int i
= 1; i
< argc
; i
++)
142 args
.push_back(argv
[i
]);
144 if (args
.size() >= 1 && args
[0] == "-import") {
145 for (auto &it
: pass_register
) {
146 std::string tcl_command_name
= it
.first
;
147 if (tcl_command_name
== "proc")
148 tcl_command_name
= "procs";
150 if (Tcl_GetCommandInfo(interp
, tcl_command_name
.c_str(), &info
) != 0) {
151 log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it
.first
.c_str());
153 std::string tcl_script
= stringf("proc %s args { yosys %s {*}$args }", tcl_command_name
.c_str(), it
.first
.c_str());
154 Tcl_Eval(interp
, tcl_script
.c_str());
160 if (args
.size() == 1) {
161 Pass::call(yosys_get_design(), args
[0]);
165 Pass::call(yosys_get_design(), args
);
169 extern Tcl_Interp
*yosys_get_tcl_interp()
171 if (yosys_tcl_interp
== NULL
) {
172 yosys_tcl_interp
= Tcl_CreateInterp();
173 Tcl_CreateCommand(yosys_tcl_interp
, "yosys", tcl_yosys_cmd
, NULL
, NULL
);
175 return yosys_tcl_interp
;
178 struct TclPass
: public Pass
{
179 TclPass() : Pass("tcl", "execute a TCL script file") { }
180 virtual void help() {
182 log(" tcl <filename>\n");
184 log("This command executes the tcl commands in the specified file.\n");
185 log("Use 'yosys cmd' to run the yosys command 'cmd' from tcl.\n");
187 log("The tcl command 'yosys -import' can be used to import all yosys\n");
188 log("commands directly as tcl commands to the tcl shell. The yosys\n");
189 log("command 'proc' is wrapped using the tcl command 'procs' in order\n");
190 log("to avoid a name collision with the tcl builting command 'proc'.\n");
193 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
195 log_cmd_error("Missing script file.\n");
197 extra_args(args
, 1, design
, false);
198 if (Tcl_EvalFile(yosys_get_tcl_interp(), args
[1].c_str()) != TCL_OK
)
199 log_cmd_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
204 #if defined(__linux__)
205 std::string
proc_self_dirname ()
207 char path
[PATH_MAX
];
208 ssize_t buflen
= readlink("/proc/self/exe", path
, sizeof(path
));
210 log_cmd_error("readlink(\"/proc/self/exe\") failed: %s", strerror(errno
));
213 while (buflen
> 0 && path
[buflen
-1] != '/')
215 return std::string(path
, buflen
);
217 #elif defined(__APPLE__)
218 #include <mach-o/dyld.h>
219 std::string
proc_self_dirname ()
223 while (_NSGetExecutablePath(path
, &buflen
) != 0)
224 path
= (char *) realloc((void *) path
, buflen
);
225 while (buflen
> 0 && path
[buflen
-1] != '/')
227 return std::string(path
, buflen
);
230 #error Dont know how to determine process executable base path!
233 std::string
proc_share_dirname ()
235 std::string proc_self_path
= proc_self_dirname();
236 std::string proc_share_path
= proc_self_path
+ "share/";
237 if (access(proc_share_path
.c_str(), X_OK
) == 0)
238 return proc_share_path
;
239 proc_share_path
= proc_self_path
+ "../share/yosys/";
240 if (access(proc_share_path
.c_str(), X_OK
) == 0)
241 return proc_share_path
;
242 log_cmd_error("proc_share_dirname: unable to determine share/ directory!");
246 bool fgetline(FILE *f
, std::string
&buffer
)
251 if (fgets(block
, 4096, f
) == NULL
)
254 if (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r')) {
255 while (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r'))
256 buffer
.resize(buffer
.size()-1);
262 static void handle_label(std::string
&command
, bool &from_to_active
, const std::string
&run_from
, const std::string
&run_to
)
267 while (pos
< SIZE(command
) && (command
[pos
] == ' ' || command
[pos
] == '\t'))
270 while (pos
< SIZE(command
) && command
[pos
] != ' ' && command
[pos
] != '\t' && command
[pos
] != '\r' && command
[pos
] != '\n')
271 label
+= command
[pos
++];
273 if (label
.back() == ':' && SIZE(label
) > 1)
275 label
= label
.substr(0, SIZE(label
)-1);
276 command
= command
.substr(pos
);
278 if (label
== run_from
)
279 from_to_active
= true;
280 else if (label
== run_to
|| (run_from
== run_to
&& !run_from
.empty()))
281 from_to_active
= false;
285 void run_frontend(std::string filename
, std::string command
, RTLIL::Design
*design
, std::string
*backend_command
, std::string
*from_to_label
)
287 if (command
== "auto") {
288 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
290 else if (filename
.size() > 2 && filename
.substr(filename
.size()-3) == ".sv")
291 command
= "verilog -sv";
292 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
294 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".ys")
296 else if (filename
== "-")
299 log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename
.c_str());
302 if (command
== "script")
304 std::string run_from
, run_to
;
305 bool from_to_active
= true;
307 if (from_to_label
!= NULL
) {
308 size_t pos
= from_to_label
->find(':');
309 if (pos
== std::string::npos
) {
310 run_from
= *from_to_label
;
311 run_to
= *from_to_label
;
313 run_from
= from_to_label
->substr(0, pos
);
314 run_to
= from_to_label
->substr(pos
+1);
316 from_to_active
= run_from
.empty();
319 log("\n-- Executing script file `%s' --\n", filename
.c_str());
324 f
= fopen(filename
.c_str(), "r");
327 log_error("Can't open script file `%s' for reading: %s\n", filename
.c_str(), strerror(errno
));
329 FILE *backup_script_file
= Frontend::current_script_file
;
330 Frontend::current_script_file
= f
;
334 while (fgetline(f
, command
)) {
335 while (!command
.empty() && command
[command
.size()-1] == '\\') {
336 std::string next_line
;
337 if (!fgetline(f
, next_line
))
339 command
.resize(command
.size()-1);
340 command
+= next_line
;
342 handle_label(command
, from_to_active
, run_from
, run_to
);
344 Pass::call(design
, command
);
347 if (!command
.empty()) {
348 handle_label(command
, from_to_active
, run_from
, run_to
);
350 Pass::call(design
, command
);
353 catch (log_cmd_error_expection
) {
354 Frontend::current_script_file
= backup_script_file
;
355 throw log_cmd_error_expection();
358 Frontend::current_script_file
= backup_script_file
;
363 if (backend_command
!= NULL
&& *backend_command
== "auto")
364 *backend_command
= "";
369 if (filename
== "-") {
370 log("\n-- Parsing stdin using frontend `%s' --\n", command
.c_str());
372 log("\n-- Parsing `%s' using frontend `%s' --\n", filename
.c_str(), command
.c_str());
375 Frontend::frontend_call(design
, NULL
, filename
, command
);
378 void run_pass(std::string command
, RTLIL::Design
*design
)
380 log("\n-- Running pass `%s' --\n", command
.c_str());
382 Pass::call(design
, command
);
385 void run_backend(std::string filename
, std::string command
, RTLIL::Design
*design
)
387 if (command
== "auto") {
388 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
390 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
392 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".blif")
394 else if (filename
== "-")
396 else if (filename
.empty())
399 log_error("Can't guess backend for output file `%s' (missing -b option)!\n", filename
.c_str());
402 if (filename
.empty())
405 if (filename
== "-") {
406 log("\n-- Writing to stdout using backend `%s' --\n", command
.c_str());
408 log("\n-- Writing to `%s' using backend `%s' --\n", filename
.c_str(), command
.c_str());
411 Backend::backend_call(design
, NULL
, filename
, command
);
414 static char *readline_cmd_generator(const char *text
, int state
)
416 static std::map
<std::string
, Pass
*>::iterator it
;
420 it
= pass_register
.begin();
424 for (; it
!= pass_register
.end(); it
++) {
425 if (it
->first
.substr(0, len
) == text
)
426 return strdup((it
++)->first
.c_str());
431 static char *readline_obj_generator(const char *text
, int state
)
433 static std::vector
<char*> obj_names
;
441 RTLIL::Design
*design
= yosys_get_design();
442 int len
= strlen(text
);
444 if (design
->selected_active_module
.empty())
446 for (auto &it
: design
->modules_
)
447 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
448 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
451 if (design
->modules_
.count(design
->selected_active_module
) > 0)
453 RTLIL::Module
*module
= design
->modules_
.at(design
->selected_active_module
);
455 for (auto &it
: module
->wires_
)
456 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
457 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
459 for (auto &it
: module
->memories
)
460 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
461 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
463 for (auto &it
: module
->cells_
)
464 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
465 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
467 for (auto &it
: module
->processes
)
468 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
469 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
472 std::sort(obj_names
.begin(), obj_names
.end());
475 if (idx
< obj_names
.size())
476 return strdup(obj_names
[idx
++]);
483 static char **readline_completion(const char *text
, int start
, int)
486 return rl_completion_matches(text
, readline_cmd_generator
);
487 if (strncmp(rl_line_buffer
, "read_", 5) && strncmp(rl_line_buffer
, "write_", 6))
488 return rl_completion_matches(text
, readline_obj_generator
);
492 void shell(RTLIL::Design
*design
)
494 static int recursion_counter
= 0;
497 log_cmd_error_throw
= true;
499 rl_readline_name
= "yosys";
500 rl_attempted_completion_function
= readline_completion
;
501 rl_basic_word_break_characters
= " \t\n";
503 char *command
= NULL
;
504 while ((command
= readline(create_prompt(design
, recursion_counter
))) != NULL
)
506 if (command
[strspn(command
, " \t\r\n")] == 0)
508 add_history(command
);
510 char *p
= command
+ strspn(command
, " \t\r\n");
511 if (!strncmp(p
, "exit", 4)) {
513 p
+= strspn(p
, " \t\r\n");
519 log_assert(design
->selection_stack
.size() == 1);
520 Pass::call(design
, command
);
521 } catch (log_cmd_error_expection
) {
522 while (design
->selection_stack
.size() > 1)
523 design
->selection_stack
.pop_back();
531 log_cmd_error_throw
= false;
534 struct ShellPass
: public Pass
{
535 ShellPass() : Pass("shell", "enter interactive command mode") { }
536 virtual void help() {
540 log("This command enters the interactive command mode. This can be useful\n");
541 log("in a script to interrupt the script at a certain point and allow for\n");
542 log("interactive inspection or manual synthesis of the design at this point.\n");
544 log("The command prompt of the interactive shell indicates the current\n");
545 log("selection (see 'help select'):\n");
548 log(" the entire design is selected\n");
551 log(" only part of the design is selected\n");
553 log(" yosys [modname]>\n");
554 log(" the entire module 'modname' is selected using 'select -module modname'\n");
556 log(" yosys [modname]*>\n");
557 log(" only part of current module 'modname' is selected\n");
559 log("When in interactive shell, some errors (e.g. invalid command arguments)\n");
560 log("do not terminate yosys but return to the command prompt.\n");
562 log("This command is the default action if nothing else has been specified\n");
563 log("on the command line.\n");
565 log("Press Ctrl-D or type 'exit' to leave the interactive shell.\n");
568 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
569 extra_args(args
, 1, design
, false);
574 struct HistoryPass
: public Pass
{
575 HistoryPass() : Pass("history", "show last interactive commands") { }
576 virtual void help() {
580 log("This command prints all commands in the shell history buffer. This are\n");
581 log("all commands executed in an interactive session, but not the commands\n");
582 log("from executed scripts.\n");
585 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
586 extra_args(args
, 1, design
, false);
587 for(HIST_ENTRY
**list
= history_list(); *list
!= NULL
; list
++)
588 log("%s\n", (*list
)->line
);
592 struct ScriptPass
: public Pass
{
593 ScriptPass() : Pass("script", "execute commands from script file") { }
594 virtual void help() {
596 log(" script <filename> [<from_label>:<to_label>]\n");
598 log("This command executes the yosys commands in the specified file.\n");
600 log("The 2nd argument can be used to only execute the section of the\n");
601 log("file between the specified labels. An empty from label is synonymous\n");
602 log("for the beginning of the file and an empty to label is synonymous\n");
603 log("for the end of the file.\n");
605 log("If only one label is specified (without ':') then only the block\n");
606 log("marked with that label (until the next label) is executed.\n");
609 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
611 log_cmd_error("Missing script file.\n");
612 else if (args
.size() == 2)
613 run_frontend(args
[1], "script", design
, NULL
, NULL
);
614 else if (args
.size() == 3)
615 run_frontend(args
[1], "script", design
, NULL
, &args
[2]);
617 extra_args(args
, 2, design
, false);