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>
44 # include <sys/types.h>
45 # include <sys/stat.h>
56 RTLIL::Design
*yosys_design
= NULL
;
57 CellTypes yosys_celltypes
;
59 #ifdef YOSYS_ENABLE_TCL
60 Tcl_Interp
*yosys_tcl_interp
= NULL
;
63 bool memhasher_active
= false;
64 uint32_t memhasher_rng
= 123456;
65 std::vector
<void*> memhasher_store
;
70 memhasher_rng
+= time(NULL
) << 16 ^ getpid();
72 memhasher_store
.resize(0x10000);
73 memhasher_active
= true;
78 for (auto p
: memhasher_store
)
80 memhasher_store
.clear();
81 memhasher_active
= false;
86 memhasher_rng
^= memhasher_rng
<< 13;
87 memhasher_rng
^= memhasher_rng
>> 17;
88 memhasher_rng
^= memhasher_rng
<< 5;
90 int size
, index
= (memhasher_rng
>> 4) & 0xffff;
91 switch (memhasher_rng
& 7) {
92 case 0: size
= 16; break;
93 case 1: size
= 256; break;
94 case 2: size
= 1024; break;
95 case 3: size
= 4096; break;
98 if (index
< 16) size
*= 16;
99 memhasher_store
[index
] = realloc(memhasher_store
[index
], size
);
105 log(" /----------------------------------------------------------------------------\\\n");
107 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
109 log(" | Copyright (C) 2012 - 2016 Clifford Wolf <clifford@clifford.at> |\n");
111 log(" | Permission to use, copy, modify, and/or distribute this software for any |\n");
112 log(" | purpose with or without fee is hereby granted, provided that the above |\n");
113 log(" | copyright notice and this permission notice appear in all copies. |\n");
115 log(" | THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |\n");
116 log(" | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |\n");
117 log(" | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |\n");
118 log(" | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |\n");
119 log(" | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |\n");
120 log(" | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |\n");
121 log(" | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |\n");
123 log(" \\----------------------------------------------------------------------------/\n");
125 log(" %s\n", yosys_version_str
);
134 for (int i
= 0; i
< 32; i
++)
135 if (((x
-1) >> i
) == 0)
141 std::string
stringf(const char *fmt
, ...)
147 string
= vstringf(fmt
, ap
);
153 std::string
vstringf(const char *fmt
, va_list ap
)
163 str
= (char*)realloc(str
, sz
);
164 rc
= vsnprintf(str
, sz
, fmt
, apc
);
166 if (rc
>= 0 && rc
< sz
)
171 if (vasprintf(&str
, fmt
, ap
) < 0)
183 int readsome(std::istream
&f
, char *s
, int n
)
185 int rc
= int(f
.readsome(s
, n
));
187 // f.readsome() sometimes returns 0 on a non-empty stream..
199 std::string
next_token(std::string
&text
, const char *sep
, bool long_strings
)
201 size_t pos_begin
= text
.find_first_not_of(sep
);
203 if (pos_begin
== std::string::npos
)
204 pos_begin
= text
.size();
206 if (long_strings
&& pos_begin
!= text
.size() && text
[pos_begin
] == '"') {
207 string sep_string
= sep
;
208 for (size_t i
= pos_begin
+1; i
< text
.size(); i
++)
209 if (text
[i
] == '"' && (i
+1 == text
.size() || sep_string
.find(text
[i
+1]) != std::string::npos
)) {
210 std::string token
= text
.substr(pos_begin
, i
-pos_begin
+1);
211 text
= text
.substr(i
+1);
216 size_t pos_end
= text
.find_first_of(sep
, pos_begin
);
218 if (pos_end
== std::string::npos
)
219 pos_end
= text
.size();
221 std::string token
= text
.substr(pos_begin
, pos_end
-pos_begin
);
222 text
= text
.substr(pos_end
);
226 std::vector
<std::string
> split_tokens(const std::string
&text
, const char *sep
)
228 std::vector
<std::string
> tokens
;
229 std::string current_token
;
230 for (char c
: text
) {
231 if (strchr(sep
, c
)) {
232 if (!current_token
.empty()) {
233 tokens
.push_back(current_token
);
234 current_token
.clear();
239 if (!current_token
.empty()) {
240 tokens
.push_back(current_token
);
241 current_token
.clear();
246 // this is very similar to fnmatch(). the exact rules used by this
249 // ? matches any character except
250 // * matches any sequence of characters
251 // [...] matches any of the characters in the list
252 // [!..] matches any of the characters not in the list
254 // a backslash may be used to escape the next characters in the
255 // pattern. each special character can also simply match itself.
257 bool patmatch(const char *pattern
, const char *string
)
262 if (*pattern
== '\\') {
263 if (pattern
[1] == string
[0] && patmatch(pattern
+2, string
+1))
267 if (*pattern
== '?') {
270 return patmatch(pattern
+1, string
+1);
273 if (*pattern
== '*') {
275 if (patmatch(pattern
+1, string
++))
278 return pattern
[1] == 0;
281 if (*pattern
== '[') {
282 bool found_match
= false;
283 bool inverted_list
= pattern
[1] == '!';
284 const char *p
= pattern
+ (inverted_list
? 1 : 0);
288 if (found_match
!= inverted_list
&& patmatch(p
+1, string
+1))
302 if (*pattern
== *string
)
303 return patmatch(pattern
+1, string
+1);
308 int run_command(const std::string
&command
, std::function
<void(const std::string
&)> process_line
)
311 return system(command
.c_str());
313 FILE *f
= popen(command
.c_str(), "r");
319 while (fgets(logbuf
, 128, f
) != NULL
) {
321 if (!line
.empty() && line
.back() == '\n')
322 process_line(line
), line
.clear();
333 return WEXITSTATUS(ret
);
337 std::string
make_temp_file(std::string template_str
)
340 if (template_str
.rfind("/tmp/", 0) == 0) {
342 char longpath
[MAX_PATH
+ 1];
343 char shortpath
[MAX_PATH
+ 1];
345 WCHAR longpath
[MAX_PATH
+ 1];
346 TCHAR shortpath
[MAX_PATH
+ 1];
348 if (!GetTempPath(MAX_PATH
+1, longpath
))
349 log_error("GetTempPath() failed.\n");
350 if (!GetShortPathName(longpath
, shortpath
, MAX_PATH
+ 1))
351 log_error("GetShortPathName() failed.\n");
353 for (int i
= 0; shortpath
[i
]; i
++)
354 path
+= char(shortpath
[i
]);
355 template_str
= stringf("%s\\%s", path
.c_str(), template_str
.c_str() + 5);
358 size_t pos
= template_str
.rfind("XXXXXX");
359 log_assert(pos
!= std::string::npos
);
362 for (int i
= 0; i
< 6; i
++) {
363 static std::string y
= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
364 static uint32_t x
= 314159265 ^ uint32_t(time(NULL
));
365 x
^= x
<< 13, x
^= x
>> 17, x
^= x
<< 5;
366 template_str
[pos
+i
] = y
[x
% y
.size()];
368 if (_access(template_str
.c_str(), 0) != 0)
372 size_t pos
= template_str
.rfind("XXXXXX");
373 log_assert(pos
!= std::string::npos
);
375 int suffixlen
= GetSize(template_str
) - pos
- 6;
377 char *p
= strdup(template_str
.c_str());
378 close(mkstemps(p
, suffixlen
));
386 std::string
make_temp_dir(std::string template_str
)
389 template_str
= make_temp_file(template_str
);
390 mkdir(template_str
.c_str());
394 size_t pos
= template_str
.rfind("XXXXXX");
395 log_assert(pos
!= std::string::npos
);
397 int suffixlen
= GetSize(template_str
) - pos
- 6;
398 log_assert(suffixlen
== 0);
401 char *p
= strdup(template_str
.c_str());
403 log_assert(p
!= NULL
);
412 bool check_file_exists(std::string filename
, bool)
414 return _access(filename
.c_str(), 0) == 0;
417 bool check_file_exists(std::string filename
, bool is_exec
)
419 return access(filename
.c_str(), is_exec
? X_OK
: F_OK
) == 0;
423 bool is_absolute_path(std::string filename
)
426 return filename
[0] == '/' || filename
[0] == '\\' || (filename
[0] != 0 && filename
[1] == ':');
428 return filename
[0] == '/';
432 void remove_directory(std::string dirname
)
435 run_command(stringf("rmdir /s /q \"%s\"", dirname
.c_str()));
438 struct dirent
**namelist
;
439 int n
= scandir(dirname
.c_str(), &namelist
, nullptr, alphasort
);
441 for (int i
= 0; i
< n
; i
++) {
442 if (strcmp(namelist
[i
]->d_name
, ".") && strcmp(namelist
[i
]->d_name
, "..")) {
443 std::string buffer
= stringf("%s/%s", dirname
.c_str(), namelist
[i
]->d_name
);
444 if (!stat(buffer
.c_str(), &stbuf
) && S_ISREG(stbuf
.st_mode
)) {
445 remove(buffer
.c_str());
447 remove_directory(buffer
);
452 rmdir(dirname
.c_str());
456 int GetSize(RTLIL::Wire
*wire
)
463 // if there are already IdString objects then we have a global initialization order bug
465 log_assert(empty_id
.index_
== 0);
466 IdString::get_reference(empty_id
.index_
);
468 Pass::init_register();
469 yosys_design
= new RTLIL::Design
;
470 yosys_celltypes
.setup();
474 void yosys_shutdown()
481 for (auto f
: log_files
)
487 Pass::done_register();
488 yosys_celltypes
.clear();
490 #ifdef YOSYS_ENABLE_TCL
491 if (yosys_tcl_interp
!= NULL
) {
492 Tcl_DeleteInterp(yosys_tcl_interp
);
494 yosys_tcl_interp
= NULL
;
498 #ifdef YOSYS_ENABLE_PLUGINS
499 for (auto &it
: loaded_plugins
)
502 loaded_plugins
.clear();
503 loaded_plugin_aliases
.clear();
507 IdString::put_reference(empty_id
.index_
);
510 RTLIL::IdString
new_id(std::string file
, int line
, std::string func
)
513 size_t pos
= file
.find_last_of("/\\");
515 size_t pos
= file
.find_last_of('/');
517 if (pos
!= std::string::npos
)
518 file
= file
.substr(pos
+1);
520 pos
= func
.find_last_of(':');
521 if (pos
!= std::string::npos
)
522 func
= func
.substr(pos
+1);
524 return stringf("$auto$%s:%d:%s$%d", file
.c_str(), line
, func
.c_str(), autoidx
++);
527 RTLIL::Design
*yosys_get_design()
532 const char *create_prompt(RTLIL::Design
*design
, int recursion_counter
)
534 static char buffer
[100];
535 std::string str
= "\n";
536 if (recursion_counter
> 1)
537 str
+= stringf("(%d) ", recursion_counter
);
539 if (!design
->selected_active_module
.empty())
540 str
+= stringf(" [%s]", RTLIL::unescape_id(design
->selected_active_module
).c_str());
541 if (!design
->selection_stack
.empty() && !design
->selection_stack
.back().full_selection
) {
542 if (design
->selected_active_module
.empty())
544 else if (design
->selection_stack
.back().selected_modules
.size() != 1 || design
->selection_stack
.back().selected_members
.size() != 0 ||
545 design
->selection_stack
.back().selected_modules
.count(design
->selected_active_module
) == 0)
548 snprintf(buffer
, 100, "%s> ", str
.c_str());
552 std::vector
<std::string
> glob_filename(const std::string
&filename_pattern
)
554 std::vector
<std::string
> results
;
557 results
.push_back(filename_pattern
);
561 int err
= glob(filename_pattern
.c_str(), 0, NULL
, &globbuf
);
564 for (size_t i
= 0; i
< globbuf
.gl_pathc
; i
++)
565 results
.push_back(globbuf
.gl_pathv
[i
]);
568 results
.push_back(filename_pattern
);
575 void rewrite_filename(std::string
&filename
)
577 if (filename
.substr(0, 1) == "\"" && filename
.substr(GetSize(filename
)-1) == "\"")
578 filename
= filename
.substr(1, GetSize(filename
)-2);
579 if (filename
.substr(0, 2) == "+/")
580 filename
= proc_share_dirname() + filename
.substr(2);
583 #ifdef YOSYS_ENABLE_TCL
584 static int tcl_yosys_cmd(ClientData
, Tcl_Interp
*interp
, int argc
, const char *argv
[])
586 std::vector
<std::string
> args
;
587 for (int i
= 1; i
< argc
; i
++)
588 args
.push_back(argv
[i
]);
590 if (args
.size() >= 1 && args
[0] == "-import") {
591 for (auto &it
: pass_register
) {
592 std::string tcl_command_name
= it
.first
;
593 if (tcl_command_name
== "proc")
594 tcl_command_name
= "procs";
596 if (Tcl_GetCommandInfo(interp
, tcl_command_name
.c_str(), &info
) != 0) {
597 log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it
.first
.c_str());
599 std::string tcl_script
= stringf("proc %s args { yosys %s {*}$args }", tcl_command_name
.c_str(), it
.first
.c_str());
600 Tcl_Eval(interp
, tcl_script
.c_str());
606 if (args
.size() == 1) {
607 Pass::call(yosys_get_design(), args
[0]);
611 Pass::call(yosys_get_design(), args
);
615 extern Tcl_Interp
*yosys_get_tcl_interp()
617 if (yosys_tcl_interp
== NULL
) {
618 yosys_tcl_interp
= Tcl_CreateInterp();
619 Tcl_CreateCommand(yosys_tcl_interp
, "yosys", tcl_yosys_cmd
, NULL
, NULL
);
621 return yosys_tcl_interp
;
624 struct TclPass
: public Pass
{
625 TclPass() : Pass("tcl", "execute a TCL script file") { }
626 virtual void help() {
628 log(" tcl <filename>\n");
630 log("This command executes the tcl commands in the specified file.\n");
631 log("Use 'yosys cmd' to run the yosys command 'cmd' from tcl.\n");
633 log("The tcl command 'yosys -import' can be used to import all yosys\n");
634 log("commands directly as tcl commands to the tcl shell. The yosys\n");
635 log("command 'proc' is wrapped using the tcl command 'procs' in order\n");
636 log("to avoid a name collision with the tcl builtin command 'proc'.\n");
639 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
641 log_cmd_error("Missing script file.\n");
643 extra_args(args
, 1, design
, false);
644 if (Tcl_EvalFile(yosys_get_tcl_interp(), args
[1].c_str()) != TCL_OK
)
645 log_cmd_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
650 #if defined(__linux__) || defined(__CYGWIN__)
651 std::string
proc_self_dirname()
654 ssize_t buflen
= readlink("/proc/self/exe", path
, sizeof(path
));
656 log_error("readlink(\"/proc/self/exe\") failed: %s\n", strerror(errno
));
658 while (buflen
> 0 && path
[buflen
-1] != '/')
660 return std::string(path
, buflen
);
662 #elif defined(__APPLE__)
663 std::string
proc_self_dirname()
667 while (_NSGetExecutablePath(path
, &buflen
) != 0)
668 path
= (char *) realloc((void *) path
, buflen
);
669 while (buflen
> 0 && path
[buflen
-1] != '/')
671 return std::string(path
, buflen
);
673 #elif defined(_WIN32)
674 std::string
proc_self_dirname()
678 char longpath
[MAX_PATH
+ 1];
679 char shortpath
[MAX_PATH
+ 1];
681 WCHAR longpath
[MAX_PATH
+ 1];
682 TCHAR shortpath
[MAX_PATH
+ 1];
684 if (!GetModuleFileName(0, longpath
, MAX_PATH
+1))
685 log_error("GetModuleFileName() failed.\n");
686 if (!GetShortPathName(longpath
, shortpath
, MAX_PATH
+1))
687 log_error("GetShortPathName() failed.\n");
688 while (shortpath
[i
] != 0)
690 while (i
> 0 && shortpath
[i
-1] != '/' && shortpath
[i
-1] != '\\')
693 for (i
= 0; shortpath
[i
]; i
++)
694 path
+= char(shortpath
[i
]);
697 #elif defined(EMSCRIPTEN)
698 std::string
proc_self_dirname()
703 #error Dont know how to determine process executable base path!
707 std::string
proc_share_dirname()
712 std::string
proc_share_dirname()
714 std::string proc_self_path
= proc_self_dirname();
715 # if defined(_WIN32) && !defined(YOSYS_WIN32_UNIX_DIR)
716 std::string proc_share_path
= proc_self_path
+ "share\\";
717 if (check_file_exists(proc_share_path
, true))
718 return proc_share_path
;
719 proc_share_path
= proc_self_path
+ "..\\share\\";
720 if (check_file_exists(proc_share_path
, true))
721 return proc_share_path
;
723 std::string proc_share_path
= proc_self_path
+ "share/";
724 if (check_file_exists(proc_share_path
, true))
725 return proc_share_path
;
726 proc_share_path
= proc_self_path
+ "../share/yosys/";
727 if (check_file_exists(proc_share_path
, true))
728 return proc_share_path
;
730 proc_share_path
= YOSYS_DATDIR
"/";
731 if (check_file_exists(proc_share_path
, true))
732 return proc_share_path
;
735 log_error("proc_share_dirname: unable to determine share/ directory!\n");
739 bool fgetline(FILE *f
, std::string
&buffer
)
744 if (fgets(block
, 4096, f
) == NULL
)
747 if (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r')) {
748 while (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r'))
749 buffer
.resize(buffer
.size()-1);
755 static void handle_label(std::string
&command
, bool &from_to_active
, const std::string
&run_from
, const std::string
&run_to
)
760 while (pos
< GetSize(command
) && (command
[pos
] == ' ' || command
[pos
] == '\t'))
763 if (pos
< GetSize(command
) && command
[pos
] == '#')
766 while (pos
< GetSize(command
) && command
[pos
] != ' ' && command
[pos
] != '\t' && command
[pos
] != '\r' && command
[pos
] != '\n')
767 label
+= command
[pos
++];
769 if (label
.back() == ':' && GetSize(label
) > 1)
771 label
= label
.substr(0, GetSize(label
)-1);
772 command
= command
.substr(pos
);
774 if (label
== run_from
)
775 from_to_active
= true;
776 else if (label
== run_to
|| (run_from
== run_to
&& !run_from
.empty()))
777 from_to_active
= false;
781 void run_frontend(std::string filename
, std::string command
, std::string
*backend_command
, std::string
*from_to_label
, RTLIL::Design
*design
)
783 if (design
== nullptr)
784 design
= yosys_design
;
786 if (command
== "auto") {
787 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
789 else if (filename
.size() > 2 && filename
.substr(filename
.size()-3) == ".sv")
790 command
= "verilog -sv";
791 else if (filename
.size() > 2 && filename
.substr(filename
.size()-4) == ".vhd")
793 else if (filename
.size() > 4 && filename
.substr(filename
.size()-5) == ".blif")
795 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
797 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".ys")
799 else if (filename
.size() > 2 && filename
.substr(filename
.size()-4) == ".tcl")
801 else if (filename
== "-")
804 log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename
.c_str());
807 if (command
== "script")
809 std::string run_from
, run_to
;
810 bool from_to_active
= true;
812 if (from_to_label
!= NULL
) {
813 size_t pos
= from_to_label
->find(':');
814 if (pos
== std::string::npos
) {
815 run_from
= *from_to_label
;
816 run_to
= *from_to_label
;
818 run_from
= from_to_label
->substr(0, pos
);
819 run_to
= from_to_label
->substr(pos
+1);
821 from_to_active
= run_from
.empty();
824 log("\n-- Executing script file `%s' --\n", filename
.c_str());
829 f
= fopen(filename
.c_str(), "r");
832 log_error("Can't open script file `%s' for reading: %s\n", filename
.c_str(), strerror(errno
));
834 FILE *backup_script_file
= Frontend::current_script_file
;
835 Frontend::current_script_file
= f
;
839 while (fgetline(f
, command
)) {
840 while (!command
.empty() && command
[command
.size()-1] == '\\') {
841 std::string next_line
;
842 if (!fgetline(f
, next_line
))
844 command
.resize(command
.size()-1);
845 command
+= next_line
;
847 handle_label(command
, from_to_active
, run_from
, run_to
);
849 Pass::call(design
, command
);
852 if (!command
.empty()) {
853 handle_label(command
, from_to_active
, run_from
, run_to
);
855 Pass::call(design
, command
);
859 Frontend::current_script_file
= backup_script_file
;
863 Frontend::current_script_file
= backup_script_file
;
868 if (backend_command
!= NULL
&& *backend_command
== "auto")
869 *backend_command
= "";
874 if (filename
== "-") {
875 log("\n-- Parsing stdin using frontend `%s' --\n", command
.c_str());
877 log("\n-- Parsing `%s' using frontend `%s' --\n", filename
.c_str(), command
.c_str());
880 if (command
== "tcl")
881 Pass::call(design
, vector
<string
>({command
, filename
}));
883 Frontend::frontend_call(design
, NULL
, filename
, command
);
886 void run_frontend(std::string filename
, std::string command
, RTLIL::Design
*design
)
888 run_frontend(filename
, command
, nullptr, nullptr, design
);
891 void run_pass(std::string command
, RTLIL::Design
*design
)
893 if (design
== nullptr)
894 design
= yosys_design
;
896 log("\n-- Running command `%s' --\n", command
.c_str());
898 Pass::call(design
, command
);
901 void run_backend(std::string filename
, std::string command
, RTLIL::Design
*design
)
903 if (design
== nullptr)
904 design
= yosys_design
;
906 if (command
== "auto") {
907 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
909 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
911 else if (filename
.size() > 4 && filename
.substr(filename
.size()-4) == ".aig")
913 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".blif")
915 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".edif")
917 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".json")
919 else if (filename
== "-")
921 else if (filename
.empty())
924 log_error("Can't guess backend for output file `%s' (missing -b option)!\n", filename
.c_str());
927 if (filename
.empty())
930 if (filename
== "-") {
931 log("\n-- Writing to stdout using backend `%s' --\n", command
.c_str());
933 log("\n-- Writing to `%s' using backend `%s' --\n", filename
.c_str(), command
.c_str());
936 Backend::backend_call(design
, NULL
, filename
, command
);
939 #ifdef YOSYS_ENABLE_READLINE
940 static char *readline_cmd_generator(const char *text
, int state
)
942 static std::map
<std::string
, Pass
*>::iterator it
;
946 it
= pass_register
.begin();
950 for (; it
!= pass_register
.end(); it
++) {
951 if (it
->first
.substr(0, len
) == text
)
952 return strdup((it
++)->first
.c_str());
957 static char *readline_obj_generator(const char *text
, int state
)
959 static std::vector
<char*> obj_names
;
967 RTLIL::Design
*design
= yosys_get_design();
968 int len
= strlen(text
);
970 if (design
->selected_active_module
.empty())
972 for (auto &it
: design
->modules_
)
973 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
974 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
977 if (design
->modules_
.count(design
->selected_active_module
) > 0)
979 RTLIL::Module
*module
= design
->modules_
.at(design
->selected_active_module
);
981 for (auto &it
: module
->wires_
)
982 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
983 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
985 for (auto &it
: module
->memories
)
986 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
987 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
989 for (auto &it
: module
->cells_
)
990 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
991 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
993 for (auto &it
: module
->processes
)
994 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
995 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
998 std::sort(obj_names
.begin(), obj_names
.end());
1001 if (idx
< obj_names
.size())
1002 return strdup(obj_names
[idx
++]);
1009 static char **readline_completion(const char *text
, int start
, int)
1012 return rl_completion_matches(text
, readline_cmd_generator
);
1013 if (strncmp(rl_line_buffer
, "read_", 5) && strncmp(rl_line_buffer
, "write_", 6))
1014 return rl_completion_matches(text
, readline_obj_generator
);
1019 void shell(RTLIL::Design
*design
)
1021 static int recursion_counter
= 0;
1023 recursion_counter
++;
1024 log_cmd_error_throw
= true;
1026 #ifdef YOSYS_ENABLE_READLINE
1027 rl_readline_name
= "yosys";
1028 rl_attempted_completion_function
= readline_completion
;
1029 rl_basic_word_break_characters
= " \t\n";
1032 char *command
= NULL
;
1033 #ifdef YOSYS_ENABLE_READLINE
1034 while ((command
= readline(create_prompt(design
, recursion_counter
))) != NULL
)
1037 char command_buffer
[4096];
1040 fputs(create_prompt(design
, recursion_counter
), stdout
);
1042 if ((command
= fgets(command_buffer
, 4096, stdin
)) == NULL
)
1045 if (command
[strspn(command
, " \t\r\n")] == 0)
1047 #ifdef YOSYS_ENABLE_READLINE
1048 add_history(command
);
1051 char *p
= command
+ strspn(command
, " \t\r\n");
1052 if (!strncmp(p
, "exit", 4)) {
1054 p
+= strspn(p
, " \t\r\n");
1060 log_assert(design
->selection_stack
.size() == 1);
1061 Pass::call(design
, command
);
1062 } catch (log_cmd_error_exception
) {
1063 while (design
->selection_stack
.size() > 1)
1064 design
->selection_stack
.pop_back();
1068 if (command
== NULL
)
1071 recursion_counter
--;
1072 log_cmd_error_throw
= false;
1075 struct ShellPass
: public Pass
{
1076 ShellPass() : Pass("shell", "enter interactive command mode") { }
1077 virtual void help() {
1081 log("This command enters the interactive command mode. This can be useful\n");
1082 log("in a script to interrupt the script at a certain point and allow for\n");
1083 log("interactive inspection or manual synthesis of the design at this point.\n");
1085 log("The command prompt of the interactive shell indicates the current\n");
1086 log("selection (see 'help select'):\n");
1089 log(" the entire design is selected\n");
1092 log(" only part of the design is selected\n");
1094 log(" yosys [modname]>\n");
1095 log(" the entire module 'modname' is selected using 'select -module modname'\n");
1097 log(" yosys [modname]*>\n");
1098 log(" only part of current module 'modname' is selected\n");
1100 log("When in interactive shell, some errors (e.g. invalid command arguments)\n");
1101 log("do not terminate yosys but return to the command prompt.\n");
1103 log("This command is the default action if nothing else has been specified\n");
1104 log("on the command line.\n");
1106 log("Press Ctrl-D or type 'exit' to leave the interactive shell.\n");
1109 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1110 extra_args(args
, 1, design
, false);
1115 #ifdef YOSYS_ENABLE_READLINE
1116 struct HistoryPass
: public Pass
{
1117 HistoryPass() : Pass("history", "show last interactive commands") { }
1118 virtual void help() {
1122 log("This command prints all commands in the shell history buffer. This are\n");
1123 log("all commands executed in an interactive session, but not the commands\n");
1124 log("from executed scripts.\n");
1127 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1128 extra_args(args
, 1, design
, false);
1129 for(HIST_ENTRY
**list
= history_list(); *list
!= NULL
; list
++)
1130 log("%s\n", (*list
)->line
);
1135 struct ScriptCmdPass
: public Pass
{
1136 ScriptCmdPass() : Pass("script", "execute commands from script file") { }
1137 virtual void help() {
1139 log(" script <filename> [<from_label>:<to_label>]\n");
1141 log("This command executes the yosys commands in the specified file.\n");
1143 log("The 2nd argument can be used to only execute the section of the\n");
1144 log("file between the specified labels. An empty from label is synonymous\n");
1145 log("for the beginning of the file and an empty to label is synonymous\n");
1146 log("for the end of the file.\n");
1148 log("If only one label is specified (without ':') then only the block\n");
1149 log("marked with that label (until the next label) is executed.\n");
1152 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1153 if (args
.size() < 2)
1154 log_cmd_error("Missing script file.\n");
1155 else if (args
.size() == 2)
1156 run_frontend(args
[1], "script", design
);
1157 else if (args
.size() == 3)
1158 run_frontend(args
[1], "script", NULL
, &args
[2], design
);
1160 extra_args(args
, 2, design
, false);