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"
21 #include "kernel/celltypes.h"
23 #ifdef YOSYS_ENABLE_READLINE
24 # include <readline/readline.h>
25 # include <readline/history.h>
28 #ifdef YOSYS_ENABLE_PLUGINS
35 #elif defined(__APPLE__)
36 # include <mach-o/dyld.h>
39 # include <sys/stat.h>
43 # include <sys/types.h>
44 # include <sys/stat.h>
54 RTLIL::Design
*yosys_design
= NULL
;
55 CellTypes yosys_celltypes
;
57 #ifdef YOSYS_ENABLE_TCL
58 Tcl_Interp
*yosys_tcl_interp
= NULL
;
61 bool memhasher_active
= false;
62 uint32_t memhasher_rng
= 123456;
63 std::vector
<void*> memhasher_store
;
68 memhasher_rng
+= time(NULL
) << 16 ^ getpid();
70 memhasher_store
.resize(0x10000);
71 memhasher_active
= true;
76 for (auto p
: memhasher_store
)
78 memhasher_store
.clear();
79 memhasher_active
= false;
84 memhasher_rng
^= memhasher_rng
<< 13;
85 memhasher_rng
^= memhasher_rng
>> 17;
86 memhasher_rng
^= memhasher_rng
<< 5;
88 int size
, index
= (memhasher_rng
>> 4) & 0xffff;
89 switch (memhasher_rng
& 7) {
90 case 0: size
= 16; break;
91 case 1: size
= 256; break;
92 case 2: size
= 1024; break;
93 case 3: size
= 4096; break;
96 if (index
< 16) size
*= 16;
97 memhasher_store
[index
] = realloc(memhasher_store
[index
], size
);
103 log(" /----------------------------------------------------------------------------\\\n");
105 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
107 log(" | Copyright (C) 2012 - 2015 Clifford Wolf <clifford@clifford.at> |\n");
109 log(" | Permission to use, copy, modify, and/or distribute this software for any |\n");
110 log(" | purpose with or without fee is hereby granted, provided that the above |\n");
111 log(" | copyright notice and this permission notice appear in all copies. |\n");
113 log(" | THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |\n");
114 log(" | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |\n");
115 log(" | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |\n");
116 log(" | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |\n");
117 log(" | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |\n");
118 log(" | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |\n");
119 log(" | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |\n");
121 log(" \\----------------------------------------------------------------------------/\n");
123 log(" %s\n", yosys_version_str
);
127 std::string
stringf(const char *fmt
, ...)
133 string
= vstringf(fmt
, ap
);
139 std::string
vstringf(const char *fmt
, va_list ap
)
149 str
= (char*)realloc(str
, sz
);
150 rc
= vsnprintf(str
, sz
, fmt
, apc
);
152 if (rc
>= 0 && rc
< sz
)
157 if (vasprintf(&str
, fmt
, ap
) < 0)
169 int readsome(std::istream
&f
, char *s
, int n
)
171 int rc
= f
.readsome(s
, n
);
173 // f.readsome() sometimes returns 0 on a non-empty stream..
185 std::string
next_token(std::string
&text
, const char *sep
, bool long_strings
)
187 size_t pos_begin
= text
.find_first_not_of(sep
);
189 if (pos_begin
== std::string::npos
)
190 pos_begin
= text
.size();
192 if (long_strings
&& pos_begin
!= text
.size() && text
[pos_begin
] == '"') {
193 string sep_string
= sep
;
194 for (size_t i
= pos_begin
+1; i
< text
.size(); i
++)
195 if (text
[i
] == '"' && (i
+1 == text
.size() || sep_string
.find(text
[i
+1]) != std::string::npos
)) {
196 std::string token
= text
.substr(pos_begin
, i
-pos_begin
+1);
197 text
= text
.substr(i
+1);
202 size_t pos_end
= text
.find_first_of(sep
, pos_begin
);
204 if (pos_end
== std::string::npos
)
205 pos_end
= text
.size();
207 std::string token
= text
.substr(pos_begin
, pos_end
-pos_begin
);
208 text
= text
.substr(pos_end
);
212 // this is very similar to fnmatch(). the exact rules used by this
215 // ? matches any character except
216 // * matches any sequence of characters
217 // [...] matches any of the characters in the list
218 // [!..] matches any of the characters not in the list
220 // a backslash may be used to escape the next characters in the
221 // pattern. each special character can also simply match itself.
223 bool patmatch(const char *pattern
, const char *string
)
228 if (*pattern
== '\\') {
229 if (pattern
[1] == string
[0] && patmatch(pattern
+2, string
+1))
233 if (*pattern
== '?') {
236 return patmatch(pattern
+1, string
+1);
239 if (*pattern
== '*') {
241 if (patmatch(pattern
+1, string
++))
244 return pattern
[1] == 0;
247 if (*pattern
== '[') {
248 bool found_match
= false;
249 bool inverted_list
= pattern
[1] == '!';
250 const char *p
= pattern
+ (inverted_list
? 1 : 0);
254 if (found_match
!= inverted_list
&& patmatch(p
+1, string
+1))
268 if (*pattern
== *string
)
269 return patmatch(pattern
+1, string
+1);
274 int run_command(const std::string
&command
, std::function
<void(const std::string
&)> process_line
)
277 return system(command
.c_str());
279 FILE *f
= popen(command
.c_str(), "r");
285 while (fgets(logbuf
, 128, f
) != NULL
) {
287 if (!line
.empty() && line
.back() == '\n')
288 process_line(line
), line
.clear();
299 return WEXITSTATUS(ret
);
303 std::string
make_temp_file(std::string template_str
)
306 if (template_str
.rfind("/tmp/", 0) == 0) {
308 char longpath
[MAX_PATH
+ 1];
309 char shortpath
[MAX_PATH
+ 1];
311 WCHAR longpath
[MAX_PATH
+ 1];
312 TCHAR shortpath
[MAX_PATH
+ 1];
314 if (!GetTempPath(MAX_PATH
+1, longpath
))
315 log_error("GetTempPath() failed.\n");
316 if (!GetShortPathName(longpath
, shortpath
, MAX_PATH
+ 1))
317 log_error("GetShortPathName() failed.\n");
319 for (int i
= 0; shortpath
[i
]; i
++)
320 path
+= char(shortpath
[i
]);
321 template_str
= stringf("%s\\%s", path
.c_str(), template_str
.c_str() + 5);
324 size_t pos
= template_str
.rfind("XXXXXX");
325 log_assert(pos
!= std::string::npos
);
328 for (int i
= 0; i
< 6; i
++) {
329 static std::string y
= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
330 static uint32_t x
= 314159265 ^ uint32_t(time(NULL
));
331 x
^= x
<< 13, x
^= x
>> 17, x
^= x
<< 5;
332 template_str
[pos
+i
] = y
[x
% y
.size()];
334 if (_access(template_str
.c_str(), 0) != 0)
338 size_t pos
= template_str
.rfind("XXXXXX");
339 log_assert(pos
!= std::string::npos
);
341 int suffixlen
= GetSize(template_str
) - pos
- 6;
343 char *p
= strdup(template_str
.c_str());
344 close(mkstemps(p
, suffixlen
));
352 std::string
make_temp_dir(std::string template_str
)
355 template_str
= make_temp_file(template_str
);
356 mkdir(template_str
.c_str());
360 size_t pos
= template_str
.rfind("XXXXXX");
361 log_assert(pos
!= std::string::npos
);
363 int suffixlen
= GetSize(template_str
) - pos
- 6;
364 log_assert(suffixlen
== 0);
367 char *p
= strdup(template_str
.c_str());
369 log_assert(p
!= NULL
);
378 bool check_file_exists(std::string filename
, bool)
380 return _access(filename
.c_str(), 0) == 0;
383 bool check_file_exists(std::string filename
, bool is_exec
)
385 return access(filename
.c_str(), is_exec
? X_OK
: F_OK
) == 0;
389 bool is_absolute_path(std::string filename
)
392 return filename
[0] == '/' || filename
[0] == '\\' || (filename
[0] != 0 && filename
[1] == ':');
394 return filename
[0] == '/';
398 void remove_directory(std::string dirname
)
401 run_command(stringf("rmdir /s /q \"%s\"", dirname
.c_str()));
404 struct dirent
**namelist
;
405 int n
= scandir(dirname
.c_str(), &namelist
, nullptr, alphasort
);
407 for (int i
= 0; i
< n
; i
++) {
408 if (strcmp(namelist
[i
]->d_name
, ".") && strcmp(namelist
[i
]->d_name
, "..")) {
409 std::string buffer
= stringf("%s/%s", dirname
.c_str(), namelist
[i
]->d_name
);
410 if (!stat(buffer
.c_str(), &stbuf
) && S_ISREG(stbuf
.st_mode
)) {
411 remove(buffer
.c_str());
413 remove_directory(buffer
);
418 rmdir(dirname
.c_str());
422 int GetSize(RTLIL::Wire
*wire
)
429 // if there are already IdString objects then we have a global initialization order bug
431 log_assert(empty_id
.index_
== 0);
432 IdString::get_reference(empty_id
.index_
);
434 Pass::init_register();
435 yosys_design
= new RTLIL::Design
;
436 yosys_celltypes
.setup();
440 void yosys_shutdown()
447 for (auto f
: log_files
)
453 Pass::done_register();
454 yosys_celltypes
.clear();
456 #ifdef YOSYS_ENABLE_TCL
457 if (yosys_tcl_interp
!= NULL
) {
458 Tcl_DeleteInterp(yosys_tcl_interp
);
460 yosys_tcl_interp
= NULL
;
464 #ifdef YOSYS_ENABLE_PLUGINS
465 for (auto &it
: loaded_plugins
)
468 loaded_plugins
.clear();
469 loaded_plugin_aliases
.clear();
473 IdString::put_reference(empty_id
.index_
);
476 RTLIL::IdString
new_id(std::string file
, int line
, std::string func
)
479 size_t pos
= file
.find_last_of("/\\");
481 size_t pos
= file
.find_last_of('/');
483 if (pos
!= std::string::npos
)
484 file
= file
.substr(pos
+1);
486 pos
= func
.find_last_of(':');
487 if (pos
!= std::string::npos
)
488 func
= func
.substr(pos
+1);
490 return stringf("$auto$%s:%d:%s$%d", file
.c_str(), line
, func
.c_str(), autoidx
++);
493 RTLIL::Design
*yosys_get_design()
498 const char *create_prompt(RTLIL::Design
*design
, int recursion_counter
)
500 static char buffer
[100];
501 std::string str
= "\n";
502 if (recursion_counter
> 1)
503 str
+= stringf("(%d) ", recursion_counter
);
505 if (!design
->selected_active_module
.empty())
506 str
+= stringf(" [%s]", RTLIL::unescape_id(design
->selected_active_module
).c_str());
507 if (!design
->selection_stack
.empty() && !design
->selection_stack
.back().full_selection
) {
508 if (design
->selected_active_module
.empty())
510 else if (design
->selection_stack
.back().selected_modules
.size() != 1 || design
->selection_stack
.back().selected_members
.size() != 0 ||
511 design
->selection_stack
.back().selected_modules
.count(design
->selected_active_module
) == 0)
514 snprintf(buffer
, 100, "%s> ", str
.c_str());
518 void rewrite_filename(std::string
&filename
)
520 if (filename
.substr(0, 1) == "\"" && filename
.substr(GetSize(filename
)-1) == "\"")
521 filename
= filename
.substr(1, GetSize(filename
)-2);
522 if (filename
.substr(0, 2) == "+/")
523 filename
= proc_share_dirname() + filename
.substr(2);
526 #ifdef YOSYS_ENABLE_TCL
527 static int tcl_yosys_cmd(ClientData
, Tcl_Interp
*interp
, int argc
, const char *argv
[])
529 std::vector
<std::string
> args
;
530 for (int i
= 1; i
< argc
; i
++)
531 args
.push_back(argv
[i
]);
533 if (args
.size() >= 1 && args
[0] == "-import") {
534 for (auto &it
: pass_register
) {
535 std::string tcl_command_name
= it
.first
;
536 if (tcl_command_name
== "proc")
537 tcl_command_name
= "procs";
539 if (Tcl_GetCommandInfo(interp
, tcl_command_name
.c_str(), &info
) != 0) {
540 log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it
.first
.c_str());
542 std::string tcl_script
= stringf("proc %s args { yosys %s {*}$args }", tcl_command_name
.c_str(), it
.first
.c_str());
543 Tcl_Eval(interp
, tcl_script
.c_str());
549 if (args
.size() == 1) {
550 Pass::call(yosys_get_design(), args
[0]);
554 Pass::call(yosys_get_design(), args
);
558 extern Tcl_Interp
*yosys_get_tcl_interp()
560 if (yosys_tcl_interp
== NULL
) {
561 yosys_tcl_interp
= Tcl_CreateInterp();
562 Tcl_CreateCommand(yosys_tcl_interp
, "yosys", tcl_yosys_cmd
, NULL
, NULL
);
564 return yosys_tcl_interp
;
567 struct TclPass
: public Pass
{
568 TclPass() : Pass("tcl", "execute a TCL script file") { }
569 virtual void help() {
571 log(" tcl <filename>\n");
573 log("This command executes the tcl commands in the specified file.\n");
574 log("Use 'yosys cmd' to run the yosys command 'cmd' from tcl.\n");
576 log("The tcl command 'yosys -import' can be used to import all yosys\n");
577 log("commands directly as tcl commands to the tcl shell. The yosys\n");
578 log("command 'proc' is wrapped using the tcl command 'procs' in order\n");
579 log("to avoid a name collision with the tcl builting command 'proc'.\n");
582 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
584 log_cmd_error("Missing script file.\n");
586 extra_args(args
, 1, design
, false);
587 if (Tcl_EvalFile(yosys_get_tcl_interp(), args
[1].c_str()) != TCL_OK
)
588 log_cmd_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
593 #if defined(__linux__)
594 std::string
proc_self_dirname()
597 ssize_t buflen
= readlink("/proc/self/exe", path
, sizeof(path
));
599 log_error("readlink(\"/proc/self/exe\") failed: %s\n", strerror(errno
));
601 while (buflen
> 0 && path
[buflen
-1] != '/')
603 return std::string(path
, buflen
);
605 #elif defined(__APPLE__)
606 std::string
proc_self_dirname()
610 while (_NSGetExecutablePath(path
, &buflen
) != 0)
611 path
= (char *) realloc((void *) path
, buflen
);
612 while (buflen
> 0 && path
[buflen
-1] != '/')
614 return std::string(path
, buflen
);
616 #elif defined(_WIN32)
617 std::string
proc_self_dirname()
621 char longpath
[MAX_PATH
+ 1];
622 char shortpath
[MAX_PATH
+ 1];
624 WCHAR longpath
[MAX_PATH
+ 1];
625 TCHAR shortpath
[MAX_PATH
+ 1];
627 if (!GetModuleFileName(0, longpath
, MAX_PATH
+1))
628 log_error("GetModuleFileName() failed.\n");
629 if (!GetShortPathName(longpath
, shortpath
, MAX_PATH
+1))
630 log_error("GetShortPathName() failed.\n");
631 while (shortpath
[i
] != 0)
633 while (i
> 0 && shortpath
[i
-1] != '/' && shortpath
[i
-1] != '\\')
636 for (i
= 0; shortpath
[i
]; i
++)
637 path
+= char(shortpath
[i
]);
640 #elif defined(EMSCRIPTEN)
641 std::string
proc_self_dirname()
646 #error Dont know how to determine process executable base path!
650 std::string
proc_share_dirname()
655 std::string
proc_share_dirname()
657 std::string proc_self_path
= proc_self_dirname();
659 std::string proc_share_path
= proc_self_path
+ "share\\";
660 if (check_file_exists(proc_share_path
, true))
661 return proc_share_path
;
662 proc_share_path
= proc_self_path
+ "..\\share\\";
663 if (check_file_exists(proc_share_path
, true))
664 return proc_share_path
;
666 std::string proc_share_path
= proc_self_path
+ "share/";
667 if (check_file_exists(proc_share_path
, true))
668 return proc_share_path
;
669 proc_share_path
= proc_self_path
+ "../share/yosys/";
670 if (check_file_exists(proc_share_path
, true))
671 return proc_share_path
;
673 log_error("proc_share_dirname: unable to determine share/ directory!\n");
677 bool fgetline(FILE *f
, std::string
&buffer
)
682 if (fgets(block
, 4096, f
) == NULL
)
685 if (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r')) {
686 while (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r'))
687 buffer
.resize(buffer
.size()-1);
693 static void handle_label(std::string
&command
, bool &from_to_active
, const std::string
&run_from
, const std::string
&run_to
)
698 while (pos
< GetSize(command
) && (command
[pos
] == ' ' || command
[pos
] == '\t'))
701 if (pos
< GetSize(command
) && command
[pos
] == '#')
704 while (pos
< GetSize(command
) && command
[pos
] != ' ' && command
[pos
] != '\t' && command
[pos
] != '\r' && command
[pos
] != '\n')
705 label
+= command
[pos
++];
707 if (label
.back() == ':' && GetSize(label
) > 1)
709 label
= label
.substr(0, GetSize(label
)-1);
710 command
= command
.substr(pos
);
712 if (label
== run_from
)
713 from_to_active
= true;
714 else if (label
== run_to
|| (run_from
== run_to
&& !run_from
.empty()))
715 from_to_active
= false;
719 void run_frontend(std::string filename
, std::string command
, std::string
*backend_command
, std::string
*from_to_label
, RTLIL::Design
*design
)
721 if (design
== nullptr)
722 design
= yosys_design
;
724 if (command
== "auto") {
725 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
727 else if (filename
.size() > 2 && filename
.substr(filename
.size()-3) == ".sv")
728 command
= "verilog -sv";
729 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
731 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".ys")
733 else if (filename
== "-")
736 log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename
.c_str());
739 if (command
== "script")
741 std::string run_from
, run_to
;
742 bool from_to_active
= true;
744 if (from_to_label
!= NULL
) {
745 size_t pos
= from_to_label
->find(':');
746 if (pos
== std::string::npos
) {
747 run_from
= *from_to_label
;
748 run_to
= *from_to_label
;
750 run_from
= from_to_label
->substr(0, pos
);
751 run_to
= from_to_label
->substr(pos
+1);
753 from_to_active
= run_from
.empty();
756 log("\n-- Executing script file `%s' --\n", filename
.c_str());
761 f
= fopen(filename
.c_str(), "r");
764 log_error("Can't open script file `%s' for reading: %s\n", filename
.c_str(), strerror(errno
));
766 FILE *backup_script_file
= Frontend::current_script_file
;
767 Frontend::current_script_file
= f
;
771 while (fgetline(f
, command
)) {
772 while (!command
.empty() && command
[command
.size()-1] == '\\') {
773 std::string next_line
;
774 if (!fgetline(f
, next_line
))
776 command
.resize(command
.size()-1);
777 command
+= next_line
;
779 handle_label(command
, from_to_active
, run_from
, run_to
);
781 Pass::call(design
, command
);
784 if (!command
.empty()) {
785 handle_label(command
, from_to_active
, run_from
, run_to
);
787 Pass::call(design
, command
);
791 Frontend::current_script_file
= backup_script_file
;
795 Frontend::current_script_file
= backup_script_file
;
800 if (backend_command
!= NULL
&& *backend_command
== "auto")
801 *backend_command
= "";
806 if (filename
== "-") {
807 log("\n-- Parsing stdin using frontend `%s' --\n", command
.c_str());
809 log("\n-- Parsing `%s' using frontend `%s' --\n", filename
.c_str(), command
.c_str());
812 Frontend::frontend_call(design
, NULL
, filename
, command
);
815 void run_frontend(std::string filename
, std::string command
, RTLIL::Design
*design
)
817 run_frontend(filename
, command
, nullptr, nullptr, design
);
820 void run_pass(std::string command
, RTLIL::Design
*design
)
822 if (design
== nullptr)
823 design
= yosys_design
;
825 log("\n-- Running command `%s' --\n", command
.c_str());
827 Pass::call(design
, command
);
830 void run_backend(std::string filename
, std::string command
, RTLIL::Design
*design
)
832 if (design
== nullptr)
833 design
= yosys_design
;
835 if (command
== "auto") {
836 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
838 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
840 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".blif")
842 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".edif")
844 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".json")
846 else if (filename
== "-")
848 else if (filename
.empty())
851 log_error("Can't guess backend for output file `%s' (missing -b option)!\n", filename
.c_str());
854 if (filename
.empty())
857 if (filename
== "-") {
858 log("\n-- Writing to stdout using backend `%s' --\n", command
.c_str());
860 log("\n-- Writing to `%s' using backend `%s' --\n", filename
.c_str(), command
.c_str());
863 Backend::backend_call(design
, NULL
, filename
, command
);
866 #ifdef YOSYS_ENABLE_READLINE
867 static char *readline_cmd_generator(const char *text
, int state
)
869 static std::map
<std::string
, Pass
*>::iterator it
;
873 it
= pass_register
.begin();
877 for (; it
!= pass_register
.end(); it
++) {
878 if (it
->first
.substr(0, len
) == text
)
879 return strdup((it
++)->first
.c_str());
884 static char *readline_obj_generator(const char *text
, int state
)
886 static std::vector
<char*> obj_names
;
894 RTLIL::Design
*design
= yosys_get_design();
895 int len
= strlen(text
);
897 if (design
->selected_active_module
.empty())
899 for (auto &it
: design
->modules_
)
900 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
901 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
904 if (design
->modules_
.count(design
->selected_active_module
) > 0)
906 RTLIL::Module
*module
= design
->modules_
.at(design
->selected_active_module
);
908 for (auto &it
: module
->wires_
)
909 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
910 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
912 for (auto &it
: module
->memories
)
913 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
914 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
916 for (auto &it
: module
->cells_
)
917 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
918 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
920 for (auto &it
: module
->processes
)
921 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
922 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
925 std::sort(obj_names
.begin(), obj_names
.end());
928 if (idx
< obj_names
.size())
929 return strdup(obj_names
[idx
++]);
936 static char **readline_completion(const char *text
, int start
, int)
939 return rl_completion_matches(text
, readline_cmd_generator
);
940 if (strncmp(rl_line_buffer
, "read_", 5) && strncmp(rl_line_buffer
, "write_", 6))
941 return rl_completion_matches(text
, readline_obj_generator
);
946 void shell(RTLIL::Design
*design
)
948 static int recursion_counter
= 0;
951 log_cmd_error_throw
= true;
953 #ifdef YOSYS_ENABLE_READLINE
954 rl_readline_name
= "yosys";
955 rl_attempted_completion_function
= readline_completion
;
956 rl_basic_word_break_characters
= " \t\n";
959 char *command
= NULL
;
960 #ifdef YOSYS_ENABLE_READLINE
961 while ((command
= readline(create_prompt(design
, recursion_counter
))) != NULL
)
964 char command_buffer
[4096];
967 fputs(create_prompt(design
, recursion_counter
), stdout
);
969 if ((command
= fgets(command_buffer
, 4096, stdin
)) == NULL
)
972 if (command
[strspn(command
, " \t\r\n")] == 0)
974 #ifdef YOSYS_ENABLE_READLINE
975 add_history(command
);
978 char *p
= command
+ strspn(command
, " \t\r\n");
979 if (!strncmp(p
, "exit", 4)) {
981 p
+= strspn(p
, " \t\r\n");
987 log_assert(design
->selection_stack
.size() == 1);
988 Pass::call(design
, command
);
989 } catch (log_cmd_error_exception
) {
990 while (design
->selection_stack
.size() > 1)
991 design
->selection_stack
.pop_back();
999 log_cmd_error_throw
= false;
1002 struct ShellPass
: public Pass
{
1003 ShellPass() : Pass("shell", "enter interactive command mode") { }
1004 virtual void help() {
1008 log("This command enters the interactive command mode. This can be useful\n");
1009 log("in a script to interrupt the script at a certain point and allow for\n");
1010 log("interactive inspection or manual synthesis of the design at this point.\n");
1012 log("The command prompt of the interactive shell indicates the current\n");
1013 log("selection (see 'help select'):\n");
1016 log(" the entire design is selected\n");
1019 log(" only part of the design is selected\n");
1021 log(" yosys [modname]>\n");
1022 log(" the entire module 'modname' is selected using 'select -module modname'\n");
1024 log(" yosys [modname]*>\n");
1025 log(" only part of current module 'modname' is selected\n");
1027 log("When in interactive shell, some errors (e.g. invalid command arguments)\n");
1028 log("do not terminate yosys but return to the command prompt.\n");
1030 log("This command is the default action if nothing else has been specified\n");
1031 log("on the command line.\n");
1033 log("Press Ctrl-D or type 'exit' to leave the interactive shell.\n");
1036 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1037 extra_args(args
, 1, design
, false);
1042 #ifdef YOSYS_ENABLE_READLINE
1043 struct HistoryPass
: public Pass
{
1044 HistoryPass() : Pass("history", "show last interactive commands") { }
1045 virtual void help() {
1049 log("This command prints all commands in the shell history buffer. This are\n");
1050 log("all commands executed in an interactive session, but not the commands\n");
1051 log("from executed scripts.\n");
1054 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1055 extra_args(args
, 1, design
, false);
1056 for(HIST_ENTRY
**list
= history_list(); *list
!= NULL
; list
++)
1057 log("%s\n", (*list
)->line
);
1062 struct ScriptPass
: public Pass
{
1063 ScriptPass() : Pass("script", "execute commands from script file") { }
1064 virtual void help() {
1066 log(" script <filename> [<from_label>:<to_label>]\n");
1068 log("This command executes the yosys commands in the specified file.\n");
1070 log("The 2nd argument can be used to only execute the section of the\n");
1071 log("file between the specified labels. An empty from label is synonymous\n");
1072 log("for the beginning of the file and an empty to label is synonymous\n");
1073 log("for the end of the file.\n");
1075 log("If only one label is specified (without ':') then only the block\n");
1076 log("marked with that label (until the next label) is executed.\n");
1079 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1080 if (args
.size() < 2)
1081 log_cmd_error("Missing script file.\n");
1082 else if (args
.size() == 2)
1083 run_frontend(args
[1], "script", design
);
1084 else if (args
.size() == 3)
1085 run_frontend(args
[1], "script", NULL
, &args
[2], design
);
1087 extra_args(args
, 2, design
, false);