2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Claire Xenia Wolf <claire@yosyshq.com>
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_EDITLINE
29 # include <editline/readline.h>
32 #ifdef YOSYS_ENABLE_PLUGINS
39 #elif defined(__APPLE__)
40 # include <mach-o/dyld.h>
43 # include <sys/stat.h>
47 # include <sys/types.h>
48 # include <sys/stat.h>
49 # if !defined(YOSYS_DISABLE_SPAWN)
50 # include <sys/wait.h>
54 #if !defined(_WIN32) && defined(YOSYS_ENABLE_GLOB)
59 # include <sys/sysctl.h>
63 #if PY_MAJOR_VERSION >= 3
64 # define INIT_MODULE PyInit_libyosys
65 extern "C" PyObject
* INIT_MODULE();
67 # define INIT_MODULE initlibyosys
68 extern "C" void INIT_MODULE();
80 RTLIL::Design
*yosys_design
= NULL
;
81 CellTypes yosys_celltypes
;
83 #ifdef YOSYS_ENABLE_TCL
84 Tcl_Interp
*yosys_tcl_interp
= NULL
;
87 std::set
<std::string
> yosys_input_files
, yosys_output_files
;
89 bool memhasher_active
= false;
90 uint32_t memhasher_rng
= 123456;
91 std::vector
<void*> memhasher_store
;
93 std::string yosys_share_dirname
;
94 std::string yosys_abc_executable
;
96 void init_share_dirname();
97 void init_abc_executable_name();
101 #if defined(__linux__) || defined(__FreeBSD__)
102 memhasher_rng
+= time(NULL
) << 16 ^ getpid();
104 memhasher_store
.resize(0x10000);
105 memhasher_active
= true;
110 for (auto p
: memhasher_store
)
112 memhasher_store
.clear();
113 memhasher_active
= false;
118 memhasher_rng
^= memhasher_rng
<< 13;
119 memhasher_rng
^= memhasher_rng
>> 17;
120 memhasher_rng
^= memhasher_rng
<< 5;
122 int size
, index
= (memhasher_rng
>> 4) & 0xffff;
123 switch (memhasher_rng
& 7) {
124 case 0: size
= 16; break;
125 case 1: size
= 256; break;
126 case 2: size
= 1024; break;
127 case 3: size
= 4096; break;
130 if (index
< 16) size
*= 16;
131 memhasher_store
[index
] = realloc(memhasher_store
[index
], size
);
137 log(" /----------------------------------------------------------------------------\\\n");
139 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
141 log(" | Copyright (C) 2012 - 2020 Claire Xenia Wolf <claire@yosyshq.com> |\n");
143 log(" | Permission to use, copy, modify, and/or distribute this software for any |\n");
144 log(" | purpose with or without fee is hereby granted, provided that the above |\n");
145 log(" | copyright notice and this permission notice appear in all copies. |\n");
147 log(" | THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |\n");
148 log(" | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |\n");
149 log(" | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |\n");
150 log(" | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |\n");
151 log(" | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |\n");
152 log(" | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |\n");
153 log(" | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |\n");
155 log(" \\----------------------------------------------------------------------------/\n");
157 log(" %s\n", yosys_version_str
);
163 #if defined(__GNUC__)
164 return x
> 1 ? (8*sizeof(int)) - __builtin_clz(x
-1) : 0;
168 for (int i
= 0; i
< 32; i
++)
169 if (((x
-1) >> i
) == 0)
175 std::string
stringf(const char *fmt
, ...)
181 string
= vstringf(fmt
, ap
);
187 std::string
vstringf(const char *fmt
, va_list ap
)
192 #if defined(_WIN32 )|| defined(__CYGWIN__)
197 str
= (char*)realloc(str
, sz
);
198 rc
= vsnprintf(str
, sz
, fmt
, apc
);
200 if (rc
>= 0 && rc
< sz
)
205 if (vasprintf(&str
, fmt
, ap
) < 0)
217 int readsome(std::istream
&f
, char *s
, int n
)
219 int rc
= int(f
.readsome(s
, n
));
221 // f.readsome() sometimes returns 0 on a non-empty stream..
233 std::string
next_token(std::string
&text
, const char *sep
, bool long_strings
)
235 size_t pos_begin
= text
.find_first_not_of(sep
);
237 if (pos_begin
== std::string::npos
)
238 pos_begin
= text
.size();
240 if (long_strings
&& pos_begin
!= text
.size() && text
[pos_begin
] == '"') {
241 string sep_string
= sep
;
242 for (size_t i
= pos_begin
+1; i
< text
.size(); i
++) {
243 if (text
[i
] == '"' && (i
+1 == text
.size() || sep_string
.find(text
[i
+1]) != std::string::npos
)) {
244 std::string token
= text
.substr(pos_begin
, i
-pos_begin
+1);
245 text
= text
.substr(i
+1);
248 if (i
+1 < text
.size() && text
[i
] == '"' && text
[i
+1] == ';' && (i
+2 == text
.size() || sep_string
.find(text
[i
+2]) != std::string::npos
)) {
249 std::string token
= text
.substr(pos_begin
, i
-pos_begin
+1);
250 text
= text
.substr(i
+2);
256 size_t pos_end
= text
.find_first_of(sep
, pos_begin
);
258 if (pos_end
== std::string::npos
)
259 pos_end
= text
.size();
261 std::string token
= text
.substr(pos_begin
, pos_end
-pos_begin
);
262 text
= text
.substr(pos_end
);
266 std::vector
<std::string
> split_tokens(const std::string
&text
, const char *sep
)
268 std::vector
<std::string
> tokens
;
269 std::string current_token
;
270 for (char c
: text
) {
271 if (strchr(sep
, c
)) {
272 if (!current_token
.empty()) {
273 tokens
.push_back(current_token
);
274 current_token
.clear();
279 if (!current_token
.empty()) {
280 tokens
.push_back(current_token
);
281 current_token
.clear();
286 // this is very similar to fnmatch(). the exact rules used by this
289 // ? matches any character except
290 // * matches any sequence of characters
291 // [...] matches any of the characters in the list
292 // [!..] matches any of the characters not in the list
294 // a backslash may be used to escape the next characters in the
295 // pattern. each special character can also simply match itself.
297 bool patmatch(const char *pattern
, const char *string
)
302 if (*pattern
== '\\') {
303 if (pattern
[1] == string
[0] && patmatch(pattern
+2, string
+1))
307 if (*pattern
== '?') {
310 return patmatch(pattern
+1, string
+1);
313 if (*pattern
== '*') {
315 if (patmatch(pattern
+1, string
++))
318 return pattern
[1] == 0;
321 if (*pattern
== '[') {
322 bool found_match
= false;
323 bool inverted_list
= pattern
[1] == '!';
324 const char *p
= pattern
+ (inverted_list
? 1 : 0);
328 if (found_match
!= inverted_list
&& patmatch(p
+1, string
+1))
342 if (*pattern
== *string
)
343 return patmatch(pattern
+1, string
+1);
348 #if !defined(YOSYS_DISABLE_SPAWN)
349 int run_command(const std::string
&command
, std::function
<void(const std::string
&)> process_line
)
352 return system(command
.c_str());
354 FILE *f
= popen(command
.c_str(), "r");
360 while (fgets(logbuf
, 128, f
) != NULL
) {
362 if (!line
.empty() && line
.back() == '\n')
363 process_line(line
), line
.clear();
374 return WEXITSTATUS(ret
);
379 std::string
make_temp_file(std::string template_str
)
382 size_t pos
= template_str
.rfind("XXXXXX");
383 log_assert(pos
!= std::string::npos
);
384 static size_t index
= 0;
385 template_str
.replace(pos
, 6, stringf("%06zu", index
++));
386 #elif defined(_WIN32)
387 if (template_str
.rfind("/tmp/", 0) == 0) {
389 char longpath
[MAX_PATH
+ 1];
390 char shortpath
[MAX_PATH
+ 1];
392 WCHAR longpath
[MAX_PATH
+ 1];
393 TCHAR shortpath
[MAX_PATH
+ 1];
395 if (!GetTempPath(MAX_PATH
+1, longpath
))
396 log_error("GetTempPath() failed.\n");
397 if (!GetShortPathName(longpath
, shortpath
, MAX_PATH
+ 1))
398 log_error("GetShortPathName() failed.\n");
400 for (int i
= 0; shortpath
[i
]; i
++)
401 path
+= char(shortpath
[i
]);
402 template_str
= stringf("%s\\%s", path
.c_str(), template_str
.c_str() + 5);
405 size_t pos
= template_str
.rfind("XXXXXX");
406 log_assert(pos
!= std::string::npos
);
409 for (int i
= 0; i
< 6; i
++) {
410 static std::string y
= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
411 static uint32_t x
= 314159265 ^ uint32_t(time(NULL
));
412 x
^= x
<< 13, x
^= x
>> 17, x
^= x
<< 5;
413 template_str
[pos
+i
] = y
[x
% y
.size()];
415 if (_access(template_str
.c_str(), 0) != 0)
419 size_t pos
= template_str
.rfind("XXXXXX");
420 log_assert(pos
!= std::string::npos
);
422 int suffixlen
= GetSize(template_str
) - pos
- 6;
424 char *p
= strdup(template_str
.c_str());
425 close(mkstemps(p
, suffixlen
));
433 std::string
make_temp_dir(std::string template_str
)
436 template_str
= make_temp_file(template_str
);
437 mkdir(template_str
.c_str());
439 #elif defined(__wasm)
440 template_str
= make_temp_file(template_str
);
441 mkdir(template_str
.c_str(), 0777);
445 size_t pos
= template_str
.rfind("XXXXXX");
446 log_assert(pos
!= std::string::npos
);
448 int suffixlen
= GetSize(template_str
) - pos
- 6;
449 log_assert(suffixlen
== 0);
452 char *p
= strdup(template_str
.c_str());
454 log_assert(p
!= NULL
);
463 bool check_file_exists(std::string filename
, bool)
465 return _access(filename
.c_str(), 0) == 0;
468 bool check_file_exists(std::string filename
, bool is_exec
)
470 return access(filename
.c_str(), is_exec
? X_OK
: F_OK
) == 0;
474 bool is_absolute_path(std::string filename
)
477 return filename
[0] == '/' || filename
[0] == '\\' || (filename
[0] != 0 && filename
[1] == ':');
479 return filename
[0] == '/';
483 void remove_directory(std::string dirname
)
486 run_command(stringf("rmdir /s /q \"%s\"", dirname
.c_str()));
489 struct dirent
**namelist
;
490 int n
= scandir(dirname
.c_str(), &namelist
, nullptr, alphasort
);
492 for (int i
= 0; i
< n
; i
++) {
493 if (strcmp(namelist
[i
]->d_name
, ".") && strcmp(namelist
[i
]->d_name
, "..")) {
494 std::string buffer
= stringf("%s/%s", dirname
.c_str(), namelist
[i
]->d_name
);
495 if (!stat(buffer
.c_str(), &stbuf
) && S_ISREG(stbuf
.st_mode
)) {
496 remove(buffer
.c_str());
498 remove_directory(buffer
);
503 rmdir(dirname
.c_str());
507 std::string
escape_filename_spaces(const std::string
& filename
)
510 out
.reserve(filename
.size());
511 for (auto c
: filename
)
521 int GetSize(RTLIL::Wire
*wire
)
526 bool already_setup
= false;
532 already_setup
= true;
533 init_share_dirname();
534 init_abc_executable_name();
536 #define X(_id) RTLIL::ID::_id = "\\" # _id;
537 #include "kernel/constids.inc"
541 PyImport_AppendInittab((char*)"libyosys", INIT_MODULE
);
543 PyRun_SimpleString("import sys");
544 signal(SIGINT
, SIG_DFL
);
547 Pass::init_register();
548 yosys_design
= new RTLIL::Design
;
549 yosys_celltypes
.setup();
553 bool yosys_already_setup()
555 return already_setup
;
558 bool already_shutdown
= false;
560 void yosys_shutdown()
564 already_shutdown
= true;
567 Pass::done_register();
572 for (auto f
: log_files
)
578 yosys_celltypes
.clear();
580 #ifdef YOSYS_ENABLE_TCL
581 if (yosys_tcl_interp
!= NULL
) {
582 Tcl_DeleteInterp(yosys_tcl_interp
);
584 yosys_tcl_interp
= NULL
;
588 #ifdef YOSYS_ENABLE_PLUGINS
589 for (auto &it
: loaded_plugins
)
592 loaded_plugins
.clear();
594 loaded_python_plugins
.clear();
596 loaded_plugin_aliases
.clear();
604 RTLIL::IdString
new_id(std::string file
, int line
, std::string func
)
607 size_t pos
= file
.find_last_of("/\\");
609 size_t pos
= file
.find_last_of('/');
611 if (pos
!= std::string::npos
)
612 file
= file
.substr(pos
+1);
614 pos
= func
.find_last_of(':');
615 if (pos
!= std::string::npos
)
616 func
= func
.substr(pos
+1);
618 return stringf("$auto$%s:%d:%s$%d", file
.c_str(), line
, func
.c_str(), autoidx
++);
621 RTLIL::IdString
new_id_suffix(std::string file
, int line
, std::string func
, std::string suffix
)
624 size_t pos
= file
.find_last_of("/\\");
626 size_t pos
= file
.find_last_of('/');
628 if (pos
!= std::string::npos
)
629 file
= file
.substr(pos
+1);
631 pos
= func
.find_last_of(':');
632 if (pos
!= std::string::npos
)
633 func
= func
.substr(pos
+1);
635 return stringf("$auto$%s:%d:%s$%s$%d", file
.c_str(), line
, func
.c_str(), suffix
.c_str(), autoidx
++);
638 RTLIL::Design
*yosys_get_design()
643 const char *create_prompt(RTLIL::Design
*design
, int recursion_counter
)
645 static char buffer
[100];
646 std::string str
= "\n";
647 if (recursion_counter
> 1)
648 str
+= stringf("(%d) ", recursion_counter
);
650 if (!design
->selected_active_module
.empty())
651 str
+= stringf(" [%s]", RTLIL::unescape_id(design
->selected_active_module
).c_str());
652 if (!design
->selection_stack
.empty() && !design
->selection_stack
.back().full_selection
) {
653 if (design
->selected_active_module
.empty())
655 else if (design
->selection_stack
.back().selected_modules
.size() != 1 || design
->selection_stack
.back().selected_members
.size() != 0 ||
656 design
->selection_stack
.back().selected_modules
.count(design
->selected_active_module
) == 0)
659 snprintf(buffer
, 100, "%s> ", str
.c_str());
663 std::vector
<std::string
> glob_filename(const std::string
&filename_pattern
)
665 std::vector
<std::string
> results
;
667 #if defined(_WIN32) || !defined(YOSYS_ENABLE_GLOB)
668 results
.push_back(filename_pattern
);
672 int err
= glob(filename_pattern
.c_str(), 0, NULL
, &globbuf
);
675 for (size_t i
= 0; i
< globbuf
.gl_pathc
; i
++)
676 results
.push_back(globbuf
.gl_pathv
[i
]);
679 results
.push_back(filename_pattern
);
686 void rewrite_filename(std::string
&filename
)
688 if (filename
.compare(0, 1, "\"") == 0 && filename
.compare(GetSize(filename
)-1, std::string::npos
, "\"") == 0)
689 filename
= filename
.substr(1, GetSize(filename
)-2);
690 if (filename
.compare(0, 2, "+/") == 0)
691 filename
= proc_share_dirname() + filename
.substr(2);
693 if (filename
.compare(0, 2, "~/") == 0)
694 filename
= filename
.replace(0, 1, getenv("HOME"));
698 #ifdef YOSYS_ENABLE_TCL
699 static int tcl_yosys_cmd(ClientData
, Tcl_Interp
*interp
, int argc
, const char *argv
[])
701 std::vector
<std::string
> args
;
702 for (int i
= 1; i
< argc
; i
++)
703 args
.push_back(argv
[i
]);
705 if (args
.size() >= 1 && args
[0] == "-import") {
706 for (auto &it
: pass_register
) {
707 std::string tcl_command_name
= it
.first
;
708 if (tcl_command_name
== "proc")
709 tcl_command_name
= "procs";
710 else if (tcl_command_name
== "rename")
711 tcl_command_name
= "renames";
713 if (Tcl_GetCommandInfo(interp
, tcl_command_name
.c_str(), &info
) != 0) {
714 log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it
.first
.c_str());
716 std::string tcl_script
= stringf("proc %s args { yosys %s {*}$args }", tcl_command_name
.c_str(), it
.first
.c_str());
717 Tcl_Eval(interp
, tcl_script
.c_str());
723 if (args
.size() == 1) {
724 Pass::call(yosys_get_design(), args
[0]);
728 Pass::call(yosys_get_design(), args
);
732 extern Tcl_Interp
*yosys_get_tcl_interp()
734 if (yosys_tcl_interp
== NULL
) {
735 yosys_tcl_interp
= Tcl_CreateInterp();
736 if (Tcl_Init(yosys_tcl_interp
)!=TCL_OK
)
737 log("Tcl_Init() call failed\n");
738 Tcl_CreateCommand(yosys_tcl_interp
, "yosys", tcl_yosys_cmd
, NULL
, NULL
);
740 return yosys_tcl_interp
;
743 struct TclPass
: public Pass
{
744 TclPass() : Pass("tcl", "execute a TCL script file") { }
745 void help() override
{
746 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
748 log(" tcl <filename> [args]\n");
750 log("This command executes the tcl commands in the specified file.\n");
751 log("Use 'yosys cmd' to run the yosys command 'cmd' from tcl.\n");
753 log("The tcl command 'yosys -import' can be used to import all yosys\n");
754 log("commands directly as tcl commands to the tcl shell. Yosys commands\n");
755 log("'proc' and 'rename' are wrapped to tcl commands 'procs' and 'renames'\n");
756 log("in order to avoid a name collision with the built in commands.\n");
758 log("If any arguments are specified, these arguments are provided to the script via\n");
759 log("the standard $argc and $argv variables.\n");
762 void execute(std::vector
<std::string
> args
, RTLIL::Design
*) override
{
764 log_cmd_error("Missing script file.\n");
766 std::vector
<Tcl_Obj
*> script_args
;
767 for (auto it
= args
.begin() + 2; it
!= args
.end(); ++it
)
768 script_args
.push_back(Tcl_NewStringObj((*it
).c_str(), (*it
).size()));
770 Tcl_Interp
*interp
= yosys_get_tcl_interp();
771 Tcl_ObjSetVar2(interp
, Tcl_NewStringObj("argc", 4), NULL
, Tcl_NewIntObj(script_args
.size()), 0);
772 Tcl_ObjSetVar2(interp
, Tcl_NewStringObj("argv", 4), NULL
, Tcl_NewListObj(script_args
.size(), script_args
.data()), 0);
773 Tcl_ObjSetVar2(interp
, Tcl_NewStringObj("argv0", 5), NULL
, Tcl_NewStringObj(args
[1].c_str(), args
[1].size()), 0);
774 if (Tcl_EvalFile(interp
, args
[1].c_str()) != TCL_OK
)
775 log_cmd_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(interp
));
780 #if defined(__linux__) || defined(__CYGWIN__)
781 std::string
proc_self_dirname()
784 ssize_t buflen
= readlink("/proc/self/exe", path
, sizeof(path
));
786 log_error("readlink(\"/proc/self/exe\") failed: %s\n", strerror(errno
));
788 while (buflen
> 0 && path
[buflen
-1] != '/')
790 return std::string(path
, buflen
);
792 #elif defined(__FreeBSD__)
793 std::string
proc_self_dirname()
795 int mib
[4] = {CTL_KERN
, KERN_PROC
, KERN_PROC_PATHNAME
, -1};
799 if (sysctl(mib
, 4, NULL
, &buflen
, NULL
, 0) != 0)
800 log_error("sysctl failed: %s\n", strerror(errno
));
801 buffer
= (char*)malloc(buflen
);
803 log_error("malloc failed: %s\n", strerror(errno
));
804 if (sysctl(mib
, 4, buffer
, &buflen
, NULL
, 0) != 0)
805 log_error("sysctl failed: %s\n", strerror(errno
));
806 while (buflen
> 0 && buffer
[buflen
-1] != '/')
808 path
.assign(buffer
, buflen
);
812 #elif defined(__APPLE__)
813 std::string
proc_self_dirname()
817 while (_NSGetExecutablePath(path
, &buflen
) != 0)
818 path
= (char *) realloc((void *) path
, buflen
);
819 while (buflen
> 0 && path
[buflen
-1] != '/')
821 std::string
str(path
, buflen
);
825 #elif defined(_WIN32)
826 std::string
proc_self_dirname()
830 char longpath
[MAX_PATH
+ 1];
831 char shortpath
[MAX_PATH
+ 1];
833 WCHAR longpath
[MAX_PATH
+ 1];
834 TCHAR shortpath
[MAX_PATH
+ 1];
836 if (!GetModuleFileName(0, longpath
, MAX_PATH
+1))
837 log_error("GetModuleFileName() failed.\n");
838 if (!GetShortPathName(longpath
, shortpath
, MAX_PATH
+1))
839 log_error("GetShortPathName() failed.\n");
840 while (shortpath
[i
] != 0)
842 while (i
> 0 && shortpath
[i
-1] != '/' && shortpath
[i
-1] != '\\')
845 for (i
= 0; shortpath
[i
]; i
++)
846 path
+= char(shortpath
[i
]);
849 #elif defined(EMSCRIPTEN) || defined(__wasm)
850 std::string
proc_self_dirname()
855 #error "Don't know how to determine process executable base path!"
858 #if defined(EMSCRIPTEN) || defined(__wasm)
859 void init_share_dirname()
861 yosys_share_dirname
= "/share/";
864 void init_share_dirname()
866 std::string proc_self_path
= proc_self_dirname();
867 # if defined(_WIN32) && !defined(YOSYS_WIN32_UNIX_DIR)
868 std::string proc_share_path
= proc_self_path
+ "share\\";
869 if (check_file_exists(proc_share_path
, true)) {
870 yosys_share_dirname
= proc_share_path
;
873 proc_share_path
= proc_self_path
+ "..\\share\\";
874 if (check_file_exists(proc_share_path
, true)) {
875 yosys_share_dirname
= proc_share_path
;
879 std::string proc_share_path
= proc_self_path
+ "share/";
880 if (check_file_exists(proc_share_path
, true)) {
881 yosys_share_dirname
= proc_share_path
;
884 proc_share_path
= proc_self_path
+ "../share/" + proc_program_prefix()+ "yosys/";
885 if (check_file_exists(proc_share_path
, true)) {
886 yosys_share_dirname
= proc_share_path
;
890 proc_share_path
= YOSYS_DATDIR
"/";
891 if (check_file_exists(proc_share_path
, true)) {
892 yosys_share_dirname
= proc_share_path
;
900 void init_abc_executable_name()
903 std::string exe_file
;
904 if (std::getenv("ABC")) {
905 yosys_abc_executable
= std::getenv("ABC");
907 yosys_abc_executable
= ABCEXTERNAL
;
910 yosys_abc_executable
= proc_self_dirname() + proc_program_prefix()+ "yosys-abc";
914 if (!check_file_exists(yosys_abc_executable
+ ".exe") && check_file_exists(proc_self_dirname() + "..\\" + proc_program_prefix() + "yosys-abc.exe"))
915 yosys_abc_executable
= proc_self_dirname() + "..\\" + proc_program_prefix() + "yosys-abc";
920 std::string
proc_share_dirname()
922 if (yosys_share_dirname
.empty())
923 log_error("init_share_dirname: unable to determine share/ directory!\n");
924 return yosys_share_dirname
;
927 std::string
proc_program_prefix()
929 std::string program_prefix
;
930 #ifdef YOSYS_PROGRAM_PREFIX
931 program_prefix
= YOSYS_PROGRAM_PREFIX
;
933 return program_prefix
;
936 bool fgetline(FILE *f
, std::string
&buffer
)
941 if (fgets(block
, 4096, f
) == NULL
)
944 if (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r')) {
945 while (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r'))
946 buffer
.resize(buffer
.size()-1);
952 static void handle_label(std::string
&command
, bool &from_to_active
, const std::string
&run_from
, const std::string
&run_to
)
957 while (pos
< GetSize(command
) && (command
[pos
] == ' ' || command
[pos
] == '\t'))
960 if (pos
< GetSize(command
) && command
[pos
] == '#')
963 while (pos
< GetSize(command
) && command
[pos
] != ' ' && command
[pos
] != '\t' && command
[pos
] != '\r' && command
[pos
] != '\n')
964 label
+= command
[pos
++];
966 if (GetSize(label
) > 1 && label
.back() == ':')
968 label
= label
.substr(0, GetSize(label
)-1);
969 command
= command
.substr(pos
);
971 if (label
== run_from
)
972 from_to_active
= true;
973 else if (label
== run_to
|| (run_from
== run_to
&& !run_from
.empty()))
974 from_to_active
= false;
978 bool run_frontend(std::string filename
, std::string command
, RTLIL::Design
*design
, std::string
*from_to_label
)
980 if (design
== nullptr)
981 design
= yosys_design
;
983 if (command
== "auto") {
984 std::string filename_trim
= filename
;
985 if (filename_trim
.size() > 3 && filename_trim
.compare(filename_trim
.size()-3, std::string::npos
, ".gz") == 0)
986 filename_trim
.erase(filename_trim
.size()-3);
987 if (filename_trim
.size() > 2 && filename_trim
.compare(filename_trim
.size()-2, std::string::npos
, ".v") == 0)
988 command
= " -vlog2k";
989 else if (filename_trim
.size() > 2 && filename_trim
.compare(filename_trim
.size()-3, std::string::npos
, ".sv") == 0)
991 else if (filename_trim
.size() > 3 && filename_trim
.compare(filename_trim
.size()-4, std::string::npos
, ".vhd") == 0)
993 else if (filename_trim
.size() > 4 && filename_trim
.compare(filename_trim
.size()-5, std::string::npos
, ".blif") == 0)
995 else if (filename_trim
.size() > 5 && filename_trim
.compare(filename_trim
.size()-6, std::string::npos
, ".eblif") == 0)
997 else if (filename_trim
.size() > 4 && filename_trim
.compare(filename_trim
.size()-5, std::string::npos
, ".json") == 0)
999 else if (filename_trim
.size() > 3 && filename_trim
.compare(filename_trim
.size()-3, std::string::npos
, ".il") == 0)
1001 else if (filename_trim
.size() > 3 && filename_trim
.compare(filename_trim
.size()-3, std::string::npos
, ".ys") == 0)
1003 else if (filename_trim
.size() > 3 && filename_trim
.compare(filename_trim
.size()-4, std::string::npos
, ".tcl") == 0)
1005 else if (filename
== "-")
1008 log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename
.c_str());
1011 if (command
== "script")
1013 std::string run_from
, run_to
;
1014 bool from_to_active
= true;
1016 if (from_to_label
!= NULL
) {
1017 size_t pos
= from_to_label
->find(':');
1018 if (pos
== std::string::npos
) {
1019 run_from
= *from_to_label
;
1020 run_to
= *from_to_label
;
1022 run_from
= from_to_label
->substr(0, pos
);
1023 run_to
= from_to_label
->substr(pos
+1);
1025 from_to_active
= run_from
.empty();
1028 log("\n-- Executing script file `%s' --\n", filename
.c_str());
1032 if (filename
!= "-") {
1033 f
= fopen(filename
.c_str(), "r");
1034 yosys_input_files
.insert(filename
);
1038 log_error("Can't open script file `%s' for reading: %s\n", filename
.c_str(), strerror(errno
));
1040 FILE *backup_script_file
= Frontend::current_script_file
;
1041 Frontend::current_script_file
= f
;
1044 std::string command
;
1045 while (fgetline(f
, command
)) {
1046 while (!command
.empty() && command
[command
.size()-1] == '\\') {
1047 std::string next_line
;
1048 if (!fgetline(f
, next_line
))
1050 command
.resize(command
.size()-1);
1051 command
+= next_line
;
1053 handle_label(command
, from_to_active
, run_from
, run_to
);
1054 if (from_to_active
) {
1055 Pass::call(design
, command
);
1060 if (!command
.empty()) {
1061 handle_label(command
, from_to_active
, run_from
, run_to
);
1062 if (from_to_active
) {
1063 Pass::call(design
, command
);
1069 Frontend::current_script_file
= backup_script_file
;
1073 Frontend::current_script_file
= backup_script_file
;
1075 if (filename
!= "-")
1081 if (command
== "tcl") {
1082 Pass::call(design
, vector
<string
>({command
, filename
}));
1086 if (filename
== "-") {
1087 log("\n-- Parsing stdin using frontend `%s' --\n", command
.c_str());
1089 log("\n-- Parsing `%s' using frontend `%s' --\n", filename
.c_str(), command
.c_str());
1092 if (command
[0] == ' ') {
1093 auto argv
= split_tokens("read" + command
);
1094 argv
.push_back(filename
);
1095 Pass::call(design
, argv
);
1097 Frontend::frontend_call(design
, NULL
, filename
, command
);
1103 void run_pass(std::string command
, RTLIL::Design
*design
)
1105 if (design
== nullptr)
1106 design
= yosys_design
;
1108 log("\n-- Running command `%s' --\n", command
.c_str());
1110 Pass::call(design
, command
);
1113 void run_backend(std::string filename
, std::string command
, RTLIL::Design
*design
)
1115 if (design
== nullptr)
1116 design
= yosys_design
;
1118 if (command
== "auto") {
1119 if (filename
.size() > 2 && filename
.compare(filename
.size()-2, std::string::npos
, ".v") == 0)
1120 command
= "verilog";
1121 else if (filename
.size() > 3 && filename
.compare(filename
.size()-3, std::string::npos
, ".sv") == 0)
1122 command
= "verilog -sv";
1123 else if (filename
.size() > 3 && filename
.compare(filename
.size()-3, std::string::npos
, ".il") == 0)
1125 else if (filename
.size() > 3 && filename
.compare(filename
.size()-3, std::string::npos
, ".cc") == 0)
1127 else if (filename
.size() > 4 && filename
.compare(filename
.size()-4, std::string::npos
, ".aig") == 0)
1129 else if (filename
.size() > 5 && filename
.compare(filename
.size()-5, std::string::npos
, ".blif") == 0)
1131 else if (filename
.size() > 5 && filename
.compare(filename
.size()-5, std::string::npos
, ".edif") == 0)
1133 else if (filename
.size() > 5 && filename
.compare(filename
.size()-5, std::string::npos
, ".json") == 0)
1135 else if (filename
== "-")
1137 else if (filename
.empty())
1140 log_error("Can't guess backend for output file `%s' (missing -b option)!\n", filename
.c_str());
1143 if (filename
.empty())
1146 if (filename
== "-") {
1147 log("\n-- Writing to stdout using backend `%s' --\n", command
.c_str());
1149 log("\n-- Writing to `%s' using backend `%s' --\n", filename
.c_str(), command
.c_str());
1152 Backend::backend_call(design
, NULL
, filename
, command
);
1155 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1156 static char *readline_cmd_generator(const char *text
, int state
)
1158 static std::map
<std::string
, Pass
*>::iterator it
;
1162 it
= pass_register
.begin();
1166 for (; it
!= pass_register
.end(); it
++) {
1167 if (it
->first
.compare(0, len
, text
) == 0)
1168 return strdup((it
++)->first
.c_str());
1173 static char *readline_obj_generator(const char *text
, int state
)
1175 static std::vector
<char*> obj_names
;
1183 RTLIL::Design
*design
= yosys_get_design();
1184 int len
= strlen(text
);
1186 if (design
->selected_active_module
.empty())
1188 for (auto mod
: design
->modules())
1189 if (RTLIL::unescape_id(mod
->name
).compare(0, len
, text
) == 0)
1190 obj_names
.push_back(strdup(log_id(mod
->name
)));
1192 else if (design
->module(design
->selected_active_module
) != nullptr)
1194 RTLIL::Module
*module
= design
->module(design
->selected_active_module
);
1196 for (auto w
: module
->wires())
1197 if (RTLIL::unescape_id(w
->name
).compare(0, len
, text
) == 0)
1198 obj_names
.push_back(strdup(log_id(w
->name
)));
1200 for (auto &it
: module
->memories
)
1201 if (RTLIL::unescape_id(it
.first
).compare(0, len
, text
) == 0)
1202 obj_names
.push_back(strdup(log_id(it
.first
)));
1204 for (auto cell
: module
->cells())
1205 if (RTLIL::unescape_id(cell
->name
).compare(0, len
, text
) == 0)
1206 obj_names
.push_back(strdup(log_id(cell
->name
)));
1208 for (auto &it
: module
->processes
)
1209 if (RTLIL::unescape_id(it
.first
).compare(0, len
, text
) == 0)
1210 obj_names
.push_back(strdup(log_id(it
.first
)));
1213 std::sort(obj_names
.begin(), obj_names
.end());
1216 if (idx
< obj_names
.size())
1217 return strdup(obj_names
[idx
++]);
1224 static char **readline_completion(const char *text
, int start
, int)
1227 return rl_completion_matches(text
, readline_cmd_generator
);
1228 if (strncmp(rl_line_buffer
, "read_", 5) && strncmp(rl_line_buffer
, "write_", 6))
1229 return rl_completion_matches(text
, readline_obj_generator
);
1234 void shell(RTLIL::Design
*design
)
1236 static int recursion_counter
= 0;
1238 recursion_counter
++;
1239 log_cmd_error_throw
= true;
1241 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1242 rl_readline_name
= (char*)"yosys";
1243 rl_attempted_completion_function
= readline_completion
;
1244 rl_basic_word_break_characters
= (char*)" \t\n";
1247 char *command
= NULL
;
1248 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1249 while ((command
= readline(create_prompt(design
, recursion_counter
))) != NULL
)
1252 char command_buffer
[4096];
1255 fputs(create_prompt(design
, recursion_counter
), stdout
);
1257 if ((command
= fgets(command_buffer
, 4096, stdin
)) == NULL
)
1260 if (command
[strspn(command
, " \t\r\n")] == 0)
1262 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1263 add_history(command
);
1266 char *p
= command
+ strspn(command
, " \t\r\n");
1267 if (!strncmp(p
, "exit", 4)) {
1269 p
+= strspn(p
, " \t\r\n");
1275 log_assert(design
->selection_stack
.size() == 1);
1276 Pass::call(design
, command
);
1277 } catch (log_cmd_error_exception
) {
1278 while (design
->selection_stack
.size() > 1)
1279 design
->selection_stack
.pop_back();
1284 if (command
== NULL
)
1287 recursion_counter
--;
1288 log_cmd_error_throw
= false;
1291 struct ShellPass
: public Pass
{
1292 ShellPass() : Pass("shell", "enter interactive command mode") { }
1293 void help() override
{
1297 log("This command enters the interactive command mode. This can be useful\n");
1298 log("in a script to interrupt the script at a certain point and allow for\n");
1299 log("interactive inspection or manual synthesis of the design at this point.\n");
1301 log("The command prompt of the interactive shell indicates the current\n");
1302 log("selection (see 'help select'):\n");
1305 log(" the entire design is selected\n");
1308 log(" only part of the design is selected\n");
1310 log(" yosys [modname]>\n");
1311 log(" the entire module 'modname' is selected using 'select -module modname'\n");
1313 log(" yosys [modname]*>\n");
1314 log(" only part of current module 'modname' is selected\n");
1316 log("When in interactive shell, some errors (e.g. invalid command arguments)\n");
1317 log("do not terminate yosys but return to the command prompt.\n");
1319 log("This command is the default action if nothing else has been specified\n");
1320 log("on the command line.\n");
1322 log("Press Ctrl-D or type 'exit' to leave the interactive shell.\n");
1325 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) override
{
1326 extra_args(args
, 1, design
, false);
1331 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1332 struct HistoryPass
: public Pass
{
1333 HistoryPass() : Pass("history", "show last interactive commands") { }
1334 void help() override
{
1338 log("This command prints all commands in the shell history buffer. This are\n");
1339 log("all commands executed in an interactive session, but not the commands\n");
1340 log("from executed scripts.\n");
1343 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) override
{
1344 extra_args(args
, 1, design
, false);
1345 #ifdef YOSYS_ENABLE_READLINE
1346 for(HIST_ENTRY
**list
= history_list(); *list
!= NULL
; list
++)
1347 log("%s\n", (*list
)->line
);
1349 for (int i
= where_history(); history_get(i
); i
++)
1350 log("%s\n", history_get(i
)->line
);
1356 struct ScriptCmdPass
: public Pass
{
1357 ScriptCmdPass() : Pass("script", "execute commands from file or wire") { }
1358 void help() override
{
1359 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1361 log(" script <filename> [<from_label>:<to_label>]\n");
1362 log(" script -scriptwire [selection]\n");
1364 log("This command executes the yosys commands in the specified file (default\n");
1365 log("behaviour), or commands embedded in the constant text value connected to the\n");
1366 log("selected wires.\n");
1368 log("In the default (file) case, the 2nd argument can be used to only execute the\n");
1369 log("section of the file between the specified labels. An empty from label is\n");
1370 log("synonymous with the beginning of the file and an empty to label is synonymous\n");
1371 log("with the end of the file.\n");
1373 log("If only one label is specified (without ':') then only the block\n");
1374 log("marked with that label (until the next label) is executed.\n");
1376 log("In \"-scriptwire\" mode, the commands on the selected wire(s) will be executed\n");
1377 log("in the scope of (and thus, relative to) the wires' owning module(s). This\n");
1378 log("'-module' mode can be exited by using the 'cd' command.\n");
1381 void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) override
1383 bool scriptwire
= false;
1386 for (argidx
= 1; argidx
< args
.size(); argidx
++) {
1387 if (args
[argidx
] == "-scriptwire") {
1394 extra_args(args
, argidx
, design
);
1396 for (auto mod
: design
->selected_modules())
1397 for (auto &c
: mod
->connections()) {
1398 if (!c
.first
.is_wire())
1400 auto w
= c
.first
.as_wire();
1401 if (!mod
->selected(w
))
1403 if (!c
.second
.is_fully_const())
1404 log_error("RHS of selected wire %s.%s is not constant.\n", log_id(mod
), log_id(w
));
1405 auto v
= c
.second
.as_const();
1406 Pass::call_on_module(design
, mod
, v
.decode_string());
1409 else if (args
.size() < 2)
1410 log_cmd_error("Missing script file.\n");
1411 else if (args
.size() == 2)
1412 run_frontend(args
[1], "script", design
);
1413 else if (args
.size() == 3)
1414 run_frontend(args
[1], "script", design
, &args
[2]);
1416 extra_args(args
, 2, design
, false);