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 - 2016 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
);
132 for (int i
= 0; i
< 32; i
++)
133 if (((x
-1) >> i
) == 0)
139 std::string
stringf(const char *fmt
, ...)
145 string
= vstringf(fmt
, ap
);
151 std::string
vstringf(const char *fmt
, va_list ap
)
161 str
= (char*)realloc(str
, sz
);
162 rc
= vsnprintf(str
, sz
, fmt
, apc
);
164 if (rc
>= 0 && rc
< sz
)
169 if (vasprintf(&str
, fmt
, ap
) < 0)
181 int readsome(std::istream
&f
, char *s
, int n
)
183 int rc
= int(f
.readsome(s
, n
));
185 // f.readsome() sometimes returns 0 on a non-empty stream..
197 std::string
next_token(std::string
&text
, const char *sep
, bool long_strings
)
199 size_t pos_begin
= text
.find_first_not_of(sep
);
201 if (pos_begin
== std::string::npos
)
202 pos_begin
= text
.size();
204 if (long_strings
&& pos_begin
!= text
.size() && text
[pos_begin
] == '"') {
205 string sep_string
= sep
;
206 for (size_t i
= pos_begin
+1; i
< text
.size(); i
++)
207 if (text
[i
] == '"' && (i
+1 == text
.size() || sep_string
.find(text
[i
+1]) != std::string::npos
)) {
208 std::string token
= text
.substr(pos_begin
, i
-pos_begin
+1);
209 text
= text
.substr(i
+1);
214 size_t pos_end
= text
.find_first_of(sep
, pos_begin
);
216 if (pos_end
== std::string::npos
)
217 pos_end
= text
.size();
219 std::string token
= text
.substr(pos_begin
, pos_end
-pos_begin
);
220 text
= text
.substr(pos_end
);
224 std::vector
<std::string
> split_tokens(const std::string
&text
, const char *sep
)
226 std::vector
<std::string
> tokens
;
227 std::string current_token
;
228 for (char c
: text
) {
229 if (strchr(sep
, c
)) {
230 if (!current_token
.empty()) {
231 tokens
.push_back(current_token
);
232 current_token
.clear();
237 if (!current_token
.empty()) {
238 tokens
.push_back(current_token
);
239 current_token
.clear();
244 // this is very similar to fnmatch(). the exact rules used by this
247 // ? matches any character except
248 // * matches any sequence of characters
249 // [...] matches any of the characters in the list
250 // [!..] matches any of the characters not in the list
252 // a backslash may be used to escape the next characters in the
253 // pattern. each special character can also simply match itself.
255 bool patmatch(const char *pattern
, const char *string
)
260 if (*pattern
== '\\') {
261 if (pattern
[1] == string
[0] && patmatch(pattern
+2, string
+1))
265 if (*pattern
== '?') {
268 return patmatch(pattern
+1, string
+1);
271 if (*pattern
== '*') {
273 if (patmatch(pattern
+1, string
++))
276 return pattern
[1] == 0;
279 if (*pattern
== '[') {
280 bool found_match
= false;
281 bool inverted_list
= pattern
[1] == '!';
282 const char *p
= pattern
+ (inverted_list
? 1 : 0);
286 if (found_match
!= inverted_list
&& patmatch(p
+1, string
+1))
300 if (*pattern
== *string
)
301 return patmatch(pattern
+1, string
+1);
306 int run_command(const std::string
&command
, std::function
<void(const std::string
&)> process_line
)
309 return system(command
.c_str());
311 FILE *f
= popen(command
.c_str(), "r");
317 while (fgets(logbuf
, 128, f
) != NULL
) {
319 if (!line
.empty() && line
.back() == '\n')
320 process_line(line
), line
.clear();
331 return WEXITSTATUS(ret
);
335 std::string
make_temp_file(std::string template_str
)
338 if (template_str
.rfind("/tmp/", 0) == 0) {
340 char longpath
[MAX_PATH
+ 1];
341 char shortpath
[MAX_PATH
+ 1];
343 WCHAR longpath
[MAX_PATH
+ 1];
344 TCHAR shortpath
[MAX_PATH
+ 1];
346 if (!GetTempPath(MAX_PATH
+1, longpath
))
347 log_error("GetTempPath() failed.\n");
348 if (!GetShortPathName(longpath
, shortpath
, MAX_PATH
+ 1))
349 log_error("GetShortPathName() failed.\n");
351 for (int i
= 0; shortpath
[i
]; i
++)
352 path
+= char(shortpath
[i
]);
353 template_str
= stringf("%s\\%s", path
.c_str(), template_str
.c_str() + 5);
356 size_t pos
= template_str
.rfind("XXXXXX");
357 log_assert(pos
!= std::string::npos
);
360 for (int i
= 0; i
< 6; i
++) {
361 static std::string y
= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
362 static uint32_t x
= 314159265 ^ uint32_t(time(NULL
));
363 x
^= x
<< 13, x
^= x
>> 17, x
^= x
<< 5;
364 template_str
[pos
+i
] = y
[x
% y
.size()];
366 if (_access(template_str
.c_str(), 0) != 0)
370 size_t pos
= template_str
.rfind("XXXXXX");
371 log_assert(pos
!= std::string::npos
);
373 int suffixlen
= GetSize(template_str
) - pos
- 6;
375 char *p
= strdup(template_str
.c_str());
376 close(mkstemps(p
, suffixlen
));
384 std::string
make_temp_dir(std::string template_str
)
387 template_str
= make_temp_file(template_str
);
388 mkdir(template_str
.c_str());
392 size_t pos
= template_str
.rfind("XXXXXX");
393 log_assert(pos
!= std::string::npos
);
395 int suffixlen
= GetSize(template_str
) - pos
- 6;
396 log_assert(suffixlen
== 0);
399 char *p
= strdup(template_str
.c_str());
401 log_assert(p
!= NULL
);
410 bool check_file_exists(std::string filename
, bool)
412 return _access(filename
.c_str(), 0) == 0;
415 bool check_file_exists(std::string filename
, bool is_exec
)
417 return access(filename
.c_str(), is_exec
? X_OK
: F_OK
) == 0;
421 bool is_absolute_path(std::string filename
)
424 return filename
[0] == '/' || filename
[0] == '\\' || (filename
[0] != 0 && filename
[1] == ':');
426 return filename
[0] == '/';
430 void remove_directory(std::string dirname
)
433 run_command(stringf("rmdir /s /q \"%s\"", dirname
.c_str()));
436 struct dirent
**namelist
;
437 int n
= scandir(dirname
.c_str(), &namelist
, nullptr, alphasort
);
439 for (int i
= 0; i
< n
; i
++) {
440 if (strcmp(namelist
[i
]->d_name
, ".") && strcmp(namelist
[i
]->d_name
, "..")) {
441 std::string buffer
= stringf("%s/%s", dirname
.c_str(), namelist
[i
]->d_name
);
442 if (!stat(buffer
.c_str(), &stbuf
) && S_ISREG(stbuf
.st_mode
)) {
443 remove(buffer
.c_str());
445 remove_directory(buffer
);
450 rmdir(dirname
.c_str());
454 int GetSize(RTLIL::Wire
*wire
)
461 // if there are already IdString objects then we have a global initialization order bug
463 log_assert(empty_id
.index_
== 0);
464 IdString::get_reference(empty_id
.index_
);
466 Pass::init_register();
467 yosys_design
= new RTLIL::Design
;
468 yosys_celltypes
.setup();
472 void yosys_shutdown()
479 for (auto f
: log_files
)
485 Pass::done_register();
486 yosys_celltypes
.clear();
488 #ifdef YOSYS_ENABLE_TCL
489 if (yosys_tcl_interp
!= NULL
) {
490 Tcl_DeleteInterp(yosys_tcl_interp
);
492 yosys_tcl_interp
= NULL
;
496 #ifdef YOSYS_ENABLE_PLUGINS
497 for (auto &it
: loaded_plugins
)
500 loaded_plugins
.clear();
501 loaded_plugin_aliases
.clear();
505 IdString::put_reference(empty_id
.index_
);
508 RTLIL::IdString
new_id(std::string file
, int line
, std::string func
)
511 size_t pos
= file
.find_last_of("/\\");
513 size_t pos
= file
.find_last_of('/');
515 if (pos
!= std::string::npos
)
516 file
= file
.substr(pos
+1);
518 pos
= func
.find_last_of(':');
519 if (pos
!= std::string::npos
)
520 func
= func
.substr(pos
+1);
522 return stringf("$auto$%s:%d:%s$%d", file
.c_str(), line
, func
.c_str(), autoidx
++);
525 RTLIL::Design
*yosys_get_design()
530 const char *create_prompt(RTLIL::Design
*design
, int recursion_counter
)
532 static char buffer
[100];
533 std::string str
= "\n";
534 if (recursion_counter
> 1)
535 str
+= stringf("(%d) ", recursion_counter
);
537 if (!design
->selected_active_module
.empty())
538 str
+= stringf(" [%s]", RTLIL::unescape_id(design
->selected_active_module
).c_str());
539 if (!design
->selection_stack
.empty() && !design
->selection_stack
.back().full_selection
) {
540 if (design
->selected_active_module
.empty())
542 else if (design
->selection_stack
.back().selected_modules
.size() != 1 || design
->selection_stack
.back().selected_members
.size() != 0 ||
543 design
->selection_stack
.back().selected_modules
.count(design
->selected_active_module
) == 0)
546 snprintf(buffer
, 100, "%s> ", str
.c_str());
550 void rewrite_filename(std::string
&filename
)
552 if (filename
.substr(0, 1) == "\"" && filename
.substr(GetSize(filename
)-1) == "\"")
553 filename
= filename
.substr(1, GetSize(filename
)-2);
554 if (filename
.substr(0, 2) == "+/")
555 filename
= proc_share_dirname() + filename
.substr(2);
558 #ifdef YOSYS_ENABLE_TCL
559 static int tcl_yosys_cmd(ClientData
, Tcl_Interp
*interp
, int argc
, const char *argv
[])
561 std::vector
<std::string
> args
;
562 for (int i
= 1; i
< argc
; i
++)
563 args
.push_back(argv
[i
]);
565 if (args
.size() >= 1 && args
[0] == "-import") {
566 for (auto &it
: pass_register
) {
567 std::string tcl_command_name
= it
.first
;
568 if (tcl_command_name
== "proc")
569 tcl_command_name
= "procs";
571 if (Tcl_GetCommandInfo(interp
, tcl_command_name
.c_str(), &info
) != 0) {
572 log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it
.first
.c_str());
574 std::string tcl_script
= stringf("proc %s args { yosys %s {*}$args }", tcl_command_name
.c_str(), it
.first
.c_str());
575 Tcl_Eval(interp
, tcl_script
.c_str());
581 if (args
.size() == 1) {
582 Pass::call(yosys_get_design(), args
[0]);
586 Pass::call(yosys_get_design(), args
);
590 extern Tcl_Interp
*yosys_get_tcl_interp()
592 if (yosys_tcl_interp
== NULL
) {
593 yosys_tcl_interp
= Tcl_CreateInterp();
594 Tcl_CreateCommand(yosys_tcl_interp
, "yosys", tcl_yosys_cmd
, NULL
, NULL
);
596 return yosys_tcl_interp
;
599 struct TclPass
: public Pass
{
600 TclPass() : Pass("tcl", "execute a TCL script file") { }
601 virtual void help() {
603 log(" tcl <filename>\n");
605 log("This command executes the tcl commands in the specified file.\n");
606 log("Use 'yosys cmd' to run the yosys command 'cmd' from tcl.\n");
608 log("The tcl command 'yosys -import' can be used to import all yosys\n");
609 log("commands directly as tcl commands to the tcl shell. The yosys\n");
610 log("command 'proc' is wrapped using the tcl command 'procs' in order\n");
611 log("to avoid a name collision with the tcl builtin command 'proc'.\n");
614 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
616 log_cmd_error("Missing script file.\n");
618 extra_args(args
, 1, design
, false);
619 if (Tcl_EvalFile(yosys_get_tcl_interp(), args
[1].c_str()) != TCL_OK
)
620 log_cmd_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
625 #if defined(__linux__) || defined(__CYGWIN__)
626 std::string
proc_self_dirname()
629 ssize_t buflen
= readlink("/proc/self/exe", path
, sizeof(path
));
631 log_error("readlink(\"/proc/self/exe\") failed: %s\n", strerror(errno
));
633 while (buflen
> 0 && path
[buflen
-1] != '/')
635 return std::string(path
, buflen
);
637 #elif defined(__APPLE__)
638 std::string
proc_self_dirname()
642 while (_NSGetExecutablePath(path
, &buflen
) != 0)
643 path
= (char *) realloc((void *) path
, buflen
);
644 while (buflen
> 0 && path
[buflen
-1] != '/')
646 return std::string(path
, buflen
);
648 #elif defined(_WIN32)
649 std::string
proc_self_dirname()
653 char longpath
[MAX_PATH
+ 1];
654 char shortpath
[MAX_PATH
+ 1];
656 WCHAR longpath
[MAX_PATH
+ 1];
657 TCHAR shortpath
[MAX_PATH
+ 1];
659 if (!GetModuleFileName(0, longpath
, MAX_PATH
+1))
660 log_error("GetModuleFileName() failed.\n");
661 if (!GetShortPathName(longpath
, shortpath
, MAX_PATH
+1))
662 log_error("GetShortPathName() failed.\n");
663 while (shortpath
[i
] != 0)
665 while (i
> 0 && shortpath
[i
-1] != '/' && shortpath
[i
-1] != '\\')
668 for (i
= 0; shortpath
[i
]; i
++)
669 path
+= char(shortpath
[i
]);
672 #elif defined(EMSCRIPTEN)
673 std::string
proc_self_dirname()
678 #error Dont know how to determine process executable base path!
682 std::string
proc_share_dirname()
687 std::string
proc_share_dirname()
689 std::string proc_self_path
= proc_self_dirname();
690 # if defined(_WIN32) && !defined(YOSYS_WIN32_UNIX_DIR)
691 std::string proc_share_path
= proc_self_path
+ "share\\";
692 if (check_file_exists(proc_share_path
, true))
693 return proc_share_path
;
694 proc_share_path
= proc_self_path
+ "..\\share\\";
695 if (check_file_exists(proc_share_path
, true))
696 return proc_share_path
;
698 std::string proc_share_path
= proc_self_path
+ "share/";
699 if (check_file_exists(proc_share_path
, true))
700 return proc_share_path
;
701 proc_share_path
= proc_self_path
+ "../share/yosys/";
702 if (check_file_exists(proc_share_path
, true))
703 return proc_share_path
;
705 proc_share_path
= YOSYS_DATDIR
"/";
706 if (check_file_exists(proc_share_path
, true))
707 return proc_share_path
;
710 log_error("proc_share_dirname: unable to determine share/ directory!\n");
714 bool fgetline(FILE *f
, std::string
&buffer
)
719 if (fgets(block
, 4096, f
) == NULL
)
722 if (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r')) {
723 while (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r'))
724 buffer
.resize(buffer
.size()-1);
730 static void handle_label(std::string
&command
, bool &from_to_active
, const std::string
&run_from
, const std::string
&run_to
)
735 while (pos
< GetSize(command
) && (command
[pos
] == ' ' || command
[pos
] == '\t'))
738 if (pos
< GetSize(command
) && command
[pos
] == '#')
741 while (pos
< GetSize(command
) && command
[pos
] != ' ' && command
[pos
] != '\t' && command
[pos
] != '\r' && command
[pos
] != '\n')
742 label
+= command
[pos
++];
744 if (label
.back() == ':' && GetSize(label
) > 1)
746 label
= label
.substr(0, GetSize(label
)-1);
747 command
= command
.substr(pos
);
749 if (label
== run_from
)
750 from_to_active
= true;
751 else if (label
== run_to
|| (run_from
== run_to
&& !run_from
.empty()))
752 from_to_active
= false;
756 void run_frontend(std::string filename
, std::string command
, std::string
*backend_command
, std::string
*from_to_label
, RTLIL::Design
*design
)
758 if (design
== nullptr)
759 design
= yosys_design
;
761 if (command
== "auto") {
762 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
764 else if (filename
.size() > 2 && filename
.substr(filename
.size()-3) == ".sv")
765 command
= "verilog -sv";
766 else if (filename
.size() > 2 && filename
.substr(filename
.size()-4) == ".vhd")
768 else if (filename
.size() > 4 && filename
.substr(filename
.size()-5) == ".blif")
770 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
772 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".ys")
774 else if (filename
== "-")
777 log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename
.c_str());
780 if (command
== "script")
782 std::string run_from
, run_to
;
783 bool from_to_active
= true;
785 if (from_to_label
!= NULL
) {
786 size_t pos
= from_to_label
->find(':');
787 if (pos
== std::string::npos
) {
788 run_from
= *from_to_label
;
789 run_to
= *from_to_label
;
791 run_from
= from_to_label
->substr(0, pos
);
792 run_to
= from_to_label
->substr(pos
+1);
794 from_to_active
= run_from
.empty();
797 log("\n-- Executing script file `%s' --\n", filename
.c_str());
802 f
= fopen(filename
.c_str(), "r");
805 log_error("Can't open script file `%s' for reading: %s\n", filename
.c_str(), strerror(errno
));
807 FILE *backup_script_file
= Frontend::current_script_file
;
808 Frontend::current_script_file
= f
;
812 while (fgetline(f
, command
)) {
813 while (!command
.empty() && command
[command
.size()-1] == '\\') {
814 std::string next_line
;
815 if (!fgetline(f
, next_line
))
817 command
.resize(command
.size()-1);
818 command
+= next_line
;
820 handle_label(command
, from_to_active
, run_from
, run_to
);
822 Pass::call(design
, command
);
825 if (!command
.empty()) {
826 handle_label(command
, from_to_active
, run_from
, run_to
);
828 Pass::call(design
, command
);
832 Frontend::current_script_file
= backup_script_file
;
836 Frontend::current_script_file
= backup_script_file
;
841 if (backend_command
!= NULL
&& *backend_command
== "auto")
842 *backend_command
= "";
847 if (filename
== "-") {
848 log("\n-- Parsing stdin using frontend `%s' --\n", command
.c_str());
850 log("\n-- Parsing `%s' using frontend `%s' --\n", filename
.c_str(), command
.c_str());
853 Frontend::frontend_call(design
, NULL
, filename
, command
);
856 void run_frontend(std::string filename
, std::string command
, RTLIL::Design
*design
)
858 run_frontend(filename
, command
, nullptr, nullptr, design
);
861 void run_pass(std::string command
, RTLIL::Design
*design
)
863 if (design
== nullptr)
864 design
= yosys_design
;
866 log("\n-- Running command `%s' --\n", command
.c_str());
868 Pass::call(design
, command
);
871 void run_backend(std::string filename
, std::string command
, RTLIL::Design
*design
)
873 if (design
== nullptr)
874 design
= yosys_design
;
876 if (command
== "auto") {
877 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
879 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
881 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".blif")
883 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".edif")
885 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".json")
887 else if (filename
== "-")
889 else if (filename
.empty())
892 log_error("Can't guess backend for output file `%s' (missing -b option)!\n", filename
.c_str());
895 if (filename
.empty())
898 if (filename
== "-") {
899 log("\n-- Writing to stdout using backend `%s' --\n", command
.c_str());
901 log("\n-- Writing to `%s' using backend `%s' --\n", filename
.c_str(), command
.c_str());
904 Backend::backend_call(design
, NULL
, filename
, command
);
907 #ifdef YOSYS_ENABLE_READLINE
908 static char *readline_cmd_generator(const char *text
, int state
)
910 static std::map
<std::string
, Pass
*>::iterator it
;
914 it
= pass_register
.begin();
918 for (; it
!= pass_register
.end(); it
++) {
919 if (it
->first
.substr(0, len
) == text
)
920 return strdup((it
++)->first
.c_str());
925 static char *readline_obj_generator(const char *text
, int state
)
927 static std::vector
<char*> obj_names
;
935 RTLIL::Design
*design
= yosys_get_design();
936 int len
= strlen(text
);
938 if (design
->selected_active_module
.empty())
940 for (auto &it
: design
->modules_
)
941 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
942 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
945 if (design
->modules_
.count(design
->selected_active_module
) > 0)
947 RTLIL::Module
*module
= design
->modules_
.at(design
->selected_active_module
);
949 for (auto &it
: module
->wires_
)
950 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
951 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
953 for (auto &it
: module
->memories
)
954 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
955 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
957 for (auto &it
: module
->cells_
)
958 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
959 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
961 for (auto &it
: module
->processes
)
962 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
963 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
966 std::sort(obj_names
.begin(), obj_names
.end());
969 if (idx
< obj_names
.size())
970 return strdup(obj_names
[idx
++]);
977 static char **readline_completion(const char *text
, int start
, int)
980 return rl_completion_matches(text
, readline_cmd_generator
);
981 if (strncmp(rl_line_buffer
, "read_", 5) && strncmp(rl_line_buffer
, "write_", 6))
982 return rl_completion_matches(text
, readline_obj_generator
);
987 void shell(RTLIL::Design
*design
)
989 static int recursion_counter
= 0;
992 log_cmd_error_throw
= true;
994 #ifdef YOSYS_ENABLE_READLINE
995 rl_readline_name
= "yosys";
996 rl_attempted_completion_function
= readline_completion
;
997 rl_basic_word_break_characters
= " \t\n";
1000 char *command
= NULL
;
1001 #ifdef YOSYS_ENABLE_READLINE
1002 while ((command
= readline(create_prompt(design
, recursion_counter
))) != NULL
)
1005 char command_buffer
[4096];
1008 fputs(create_prompt(design
, recursion_counter
), stdout
);
1010 if ((command
= fgets(command_buffer
, 4096, stdin
)) == NULL
)
1013 if (command
[strspn(command
, " \t\r\n")] == 0)
1015 #ifdef YOSYS_ENABLE_READLINE
1016 add_history(command
);
1019 char *p
= command
+ strspn(command
, " \t\r\n");
1020 if (!strncmp(p
, "exit", 4)) {
1022 p
+= strspn(p
, " \t\r\n");
1028 log_assert(design
->selection_stack
.size() == 1);
1029 Pass::call(design
, command
);
1030 } catch (log_cmd_error_exception
) {
1031 while (design
->selection_stack
.size() > 1)
1032 design
->selection_stack
.pop_back();
1036 if (command
== NULL
)
1039 recursion_counter
--;
1040 log_cmd_error_throw
= false;
1043 struct ShellPass
: public Pass
{
1044 ShellPass() : Pass("shell", "enter interactive command mode") { }
1045 virtual void help() {
1049 log("This command enters the interactive command mode. This can be useful\n");
1050 log("in a script to interrupt the script at a certain point and allow for\n");
1051 log("interactive inspection or manual synthesis of the design at this point.\n");
1053 log("The command prompt of the interactive shell indicates the current\n");
1054 log("selection (see 'help select'):\n");
1057 log(" the entire design is selected\n");
1060 log(" only part of the design is selected\n");
1062 log(" yosys [modname]>\n");
1063 log(" the entire module 'modname' is selected using 'select -module modname'\n");
1065 log(" yosys [modname]*>\n");
1066 log(" only part of current module 'modname' is selected\n");
1068 log("When in interactive shell, some errors (e.g. invalid command arguments)\n");
1069 log("do not terminate yosys but return to the command prompt.\n");
1071 log("This command is the default action if nothing else has been specified\n");
1072 log("on the command line.\n");
1074 log("Press Ctrl-D or type 'exit' to leave the interactive shell.\n");
1077 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1078 extra_args(args
, 1, design
, false);
1083 #ifdef YOSYS_ENABLE_READLINE
1084 struct HistoryPass
: public Pass
{
1085 HistoryPass() : Pass("history", "show last interactive commands") { }
1086 virtual void help() {
1090 log("This command prints all commands in the shell history buffer. This are\n");
1091 log("all commands executed in an interactive session, but not the commands\n");
1092 log("from executed scripts.\n");
1095 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1096 extra_args(args
, 1, design
, false);
1097 for(HIST_ENTRY
**list
= history_list(); *list
!= NULL
; list
++)
1098 log("%s\n", (*list
)->line
);
1103 struct ScriptCmdPass
: public Pass
{
1104 ScriptCmdPass() : Pass("script", "execute commands from script file") { }
1105 virtual void help() {
1107 log(" script <filename> [<from_label>:<to_label>]\n");
1109 log("This command executes the yosys commands in the specified file.\n");
1111 log("The 2nd argument can be used to only execute the section of the\n");
1112 log("file between the specified labels. An empty from label is synonymous\n");
1113 log("for the beginning of the file and an empty to label is synonymous\n");
1114 log("for the end of the file.\n");
1116 log("If only one label is specified (without ':') then only the block\n");
1117 log("marked with that label (until the next label) is executed.\n");
1120 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1121 if (args
.size() < 2)
1122 log_cmd_error("Missing script file.\n");
1123 else if (args
.size() == 2)
1124 run_frontend(args
[1], "script", design
);
1125 else if (args
.size() == 3)
1126 run_frontend(args
[1], "script", NULL
, &args
[2], design
);
1128 extra_args(args
, 2, design
, false);