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
);
132 int y
= (x
& (x
- 1));
142 x
-= ((x
>> 1) & 0x55555555);
143 x
= (((x
>> 2) & 0x33333333) + (x
& 0x33333333));
144 x
= (((x
>> 4) + x
) & 0x0f0f0f0f);
152 std::string
stringf(const char *fmt
, ...)
158 string
= vstringf(fmt
, ap
);
164 std::string
vstringf(const char *fmt
, va_list ap
)
174 str
= (char*)realloc(str
, sz
);
175 rc
= vsnprintf(str
, sz
, fmt
, apc
);
177 if (rc
>= 0 && rc
< sz
)
182 if (vasprintf(&str
, fmt
, ap
) < 0)
194 int readsome(std::istream
&f
, char *s
, int n
)
196 int rc
= f
.readsome(s
, n
);
198 // f.readsome() sometimes returns 0 on a non-empty stream..
210 std::string
next_token(std::string
&text
, const char *sep
, bool long_strings
)
212 size_t pos_begin
= text
.find_first_not_of(sep
);
214 if (pos_begin
== std::string::npos
)
215 pos_begin
= text
.size();
217 if (long_strings
&& pos_begin
!= text
.size() && text
[pos_begin
] == '"') {
218 string sep_string
= sep
;
219 for (size_t i
= pos_begin
+1; i
< text
.size(); i
++)
220 if (text
[i
] == '"' && (i
+1 == text
.size() || sep_string
.find(text
[i
+1]) != std::string::npos
)) {
221 std::string token
= text
.substr(pos_begin
, i
-pos_begin
+1);
222 text
= text
.substr(i
+1);
227 size_t pos_end
= text
.find_first_of(sep
, pos_begin
);
229 if (pos_end
== std::string::npos
)
230 pos_end
= text
.size();
232 std::string token
= text
.substr(pos_begin
, pos_end
-pos_begin
);
233 text
= text
.substr(pos_end
);
237 std::vector
<std::string
> split_tokens(const std::string
&text
, const char *sep
)
239 std::vector
<std::string
> tokens
;
240 std::string current_token
;
241 for (char c
: text
) {
242 if (strchr(sep
, c
)) {
243 if (!current_token
.empty()) {
244 tokens
.push_back(current_token
);
245 current_token
.clear();
250 if (!current_token
.empty()) {
251 tokens
.push_back(current_token
);
252 current_token
.clear();
257 // this is very similar to fnmatch(). the exact rules used by this
260 // ? matches any character except
261 // * matches any sequence of characters
262 // [...] matches any of the characters in the list
263 // [!..] matches any of the characters not in the list
265 // a backslash may be used to escape the next characters in the
266 // pattern. each special character can also simply match itself.
268 bool patmatch(const char *pattern
, const char *string
)
273 if (*pattern
== '\\') {
274 if (pattern
[1] == string
[0] && patmatch(pattern
+2, string
+1))
278 if (*pattern
== '?') {
281 return patmatch(pattern
+1, string
+1);
284 if (*pattern
== '*') {
286 if (patmatch(pattern
+1, string
++))
289 return pattern
[1] == 0;
292 if (*pattern
== '[') {
293 bool found_match
= false;
294 bool inverted_list
= pattern
[1] == '!';
295 const char *p
= pattern
+ (inverted_list
? 1 : 0);
299 if (found_match
!= inverted_list
&& patmatch(p
+1, string
+1))
313 if (*pattern
== *string
)
314 return patmatch(pattern
+1, string
+1);
319 int run_command(const std::string
&command
, std::function
<void(const std::string
&)> process_line
)
322 return system(command
.c_str());
324 FILE *f
= popen(command
.c_str(), "r");
330 while (fgets(logbuf
, 128, f
) != NULL
) {
332 if (!line
.empty() && line
.back() == '\n')
333 process_line(line
), line
.clear();
344 return WEXITSTATUS(ret
);
348 std::string
make_temp_file(std::string template_str
)
351 if (template_str
.rfind("/tmp/", 0) == 0) {
353 char longpath
[MAX_PATH
+ 1];
354 char shortpath
[MAX_PATH
+ 1];
356 WCHAR longpath
[MAX_PATH
+ 1];
357 TCHAR shortpath
[MAX_PATH
+ 1];
359 if (!GetTempPath(MAX_PATH
+1, longpath
))
360 log_error("GetTempPath() failed.\n");
361 if (!GetShortPathName(longpath
, shortpath
, MAX_PATH
+ 1))
362 log_error("GetShortPathName() failed.\n");
364 for (int i
= 0; shortpath
[i
]; i
++)
365 path
+= char(shortpath
[i
]);
366 template_str
= stringf("%s\\%s", path
.c_str(), template_str
.c_str() + 5);
369 size_t pos
= template_str
.rfind("XXXXXX");
370 log_assert(pos
!= std::string::npos
);
373 for (int i
= 0; i
< 6; i
++) {
374 static std::string y
= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
375 static uint32_t x
= 314159265 ^ uint32_t(time(NULL
));
376 x
^= x
<< 13, x
^= x
>> 17, x
^= x
<< 5;
377 template_str
[pos
+i
] = y
[x
% y
.size()];
379 if (_access(template_str
.c_str(), 0) != 0)
383 size_t pos
= template_str
.rfind("XXXXXX");
384 log_assert(pos
!= std::string::npos
);
386 int suffixlen
= GetSize(template_str
) - pos
- 6;
388 char *p
= strdup(template_str
.c_str());
389 close(mkstemps(p
, suffixlen
));
397 std::string
make_temp_dir(std::string template_str
)
400 template_str
= make_temp_file(template_str
);
401 mkdir(template_str
.c_str());
405 size_t pos
= template_str
.rfind("XXXXXX");
406 log_assert(pos
!= std::string::npos
);
408 int suffixlen
= GetSize(template_str
) - pos
- 6;
409 log_assert(suffixlen
== 0);
412 char *p
= strdup(template_str
.c_str());
414 log_assert(p
!= NULL
);
423 bool check_file_exists(std::string filename
, bool)
425 return _access(filename
.c_str(), 0) == 0;
428 bool check_file_exists(std::string filename
, bool is_exec
)
430 return access(filename
.c_str(), is_exec
? X_OK
: F_OK
) == 0;
434 bool is_absolute_path(std::string filename
)
437 return filename
[0] == '/' || filename
[0] == '\\' || (filename
[0] != 0 && filename
[1] == ':');
439 return filename
[0] == '/';
443 void remove_directory(std::string dirname
)
446 run_command(stringf("rmdir /s /q \"%s\"", dirname
.c_str()));
449 struct dirent
**namelist
;
450 int n
= scandir(dirname
.c_str(), &namelist
, nullptr, alphasort
);
452 for (int i
= 0; i
< n
; i
++) {
453 if (strcmp(namelist
[i
]->d_name
, ".") && strcmp(namelist
[i
]->d_name
, "..")) {
454 std::string buffer
= stringf("%s/%s", dirname
.c_str(), namelist
[i
]->d_name
);
455 if (!stat(buffer
.c_str(), &stbuf
) && S_ISREG(stbuf
.st_mode
)) {
456 remove(buffer
.c_str());
458 remove_directory(buffer
);
463 rmdir(dirname
.c_str());
467 int GetSize(RTLIL::Wire
*wire
)
474 // if there are already IdString objects then we have a global initialization order bug
476 log_assert(empty_id
.index_
== 0);
477 IdString::get_reference(empty_id
.index_
);
479 Pass::init_register();
480 yosys_design
= new RTLIL::Design
;
481 yosys_celltypes
.setup();
485 void yosys_shutdown()
492 for (auto f
: log_files
)
498 Pass::done_register();
499 yosys_celltypes
.clear();
501 #ifdef YOSYS_ENABLE_TCL
502 if (yosys_tcl_interp
!= NULL
) {
503 Tcl_DeleteInterp(yosys_tcl_interp
);
505 yosys_tcl_interp
= NULL
;
509 #ifdef YOSYS_ENABLE_PLUGINS
510 for (auto &it
: loaded_plugins
)
513 loaded_plugins
.clear();
514 loaded_plugin_aliases
.clear();
518 IdString::put_reference(empty_id
.index_
);
521 RTLIL::IdString
new_id(std::string file
, int line
, std::string func
)
524 size_t pos
= file
.find_last_of("/\\");
526 size_t pos
= file
.find_last_of('/');
528 if (pos
!= std::string::npos
)
529 file
= file
.substr(pos
+1);
531 pos
= func
.find_last_of(':');
532 if (pos
!= std::string::npos
)
533 func
= func
.substr(pos
+1);
535 return stringf("$auto$%s:%d:%s$%d", file
.c_str(), line
, func
.c_str(), autoidx
++);
538 RTLIL::Design
*yosys_get_design()
543 const char *create_prompt(RTLIL::Design
*design
, int recursion_counter
)
545 static char buffer
[100];
546 std::string str
= "\n";
547 if (recursion_counter
> 1)
548 str
+= stringf("(%d) ", recursion_counter
);
550 if (!design
->selected_active_module
.empty())
551 str
+= stringf(" [%s]", RTLIL::unescape_id(design
->selected_active_module
).c_str());
552 if (!design
->selection_stack
.empty() && !design
->selection_stack
.back().full_selection
) {
553 if (design
->selected_active_module
.empty())
555 else if (design
->selection_stack
.back().selected_modules
.size() != 1 || design
->selection_stack
.back().selected_members
.size() != 0 ||
556 design
->selection_stack
.back().selected_modules
.count(design
->selected_active_module
) == 0)
559 snprintf(buffer
, 100, "%s> ", str
.c_str());
563 void rewrite_filename(std::string
&filename
)
565 if (filename
.substr(0, 1) == "\"" && filename
.substr(GetSize(filename
)-1) == "\"")
566 filename
= filename
.substr(1, GetSize(filename
)-2);
567 if (filename
.substr(0, 2) == "+/")
568 filename
= proc_share_dirname() + filename
.substr(2);
571 #ifdef YOSYS_ENABLE_TCL
572 static int tcl_yosys_cmd(ClientData
, Tcl_Interp
*interp
, int argc
, const char *argv
[])
574 std::vector
<std::string
> args
;
575 for (int i
= 1; i
< argc
; i
++)
576 args
.push_back(argv
[i
]);
578 if (args
.size() >= 1 && args
[0] == "-import") {
579 for (auto &it
: pass_register
) {
580 std::string tcl_command_name
= it
.first
;
581 if (tcl_command_name
== "proc")
582 tcl_command_name
= "procs";
584 if (Tcl_GetCommandInfo(interp
, tcl_command_name
.c_str(), &info
) != 0) {
585 log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it
.first
.c_str());
587 std::string tcl_script
= stringf("proc %s args { yosys %s {*}$args }", tcl_command_name
.c_str(), it
.first
.c_str());
588 Tcl_Eval(interp
, tcl_script
.c_str());
594 if (args
.size() == 1) {
595 Pass::call(yosys_get_design(), args
[0]);
599 Pass::call(yosys_get_design(), args
);
603 extern Tcl_Interp
*yosys_get_tcl_interp()
605 if (yosys_tcl_interp
== NULL
) {
606 yosys_tcl_interp
= Tcl_CreateInterp();
607 Tcl_CreateCommand(yosys_tcl_interp
, "yosys", tcl_yosys_cmd
, NULL
, NULL
);
609 return yosys_tcl_interp
;
612 struct TclPass
: public Pass
{
613 TclPass() : Pass("tcl", "execute a TCL script file") { }
614 virtual void help() {
616 log(" tcl <filename>\n");
618 log("This command executes the tcl commands in the specified file.\n");
619 log("Use 'yosys cmd' to run the yosys command 'cmd' from tcl.\n");
621 log("The tcl command 'yosys -import' can be used to import all yosys\n");
622 log("commands directly as tcl commands to the tcl shell. The yosys\n");
623 log("command 'proc' is wrapped using the tcl command 'procs' in order\n");
624 log("to avoid a name collision with the tcl builtin command 'proc'.\n");
627 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
629 log_cmd_error("Missing script file.\n");
631 extra_args(args
, 1, design
, false);
632 if (Tcl_EvalFile(yosys_get_tcl_interp(), args
[1].c_str()) != TCL_OK
)
633 log_cmd_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
638 #if defined(__linux__)
639 std::string
proc_self_dirname()
642 ssize_t buflen
= readlink("/proc/self/exe", path
, sizeof(path
));
644 log_error("readlink(\"/proc/self/exe\") failed: %s\n", strerror(errno
));
646 while (buflen
> 0 && path
[buflen
-1] != '/')
648 return std::string(path
, buflen
);
650 #elif defined(__APPLE__)
651 std::string
proc_self_dirname()
655 while (_NSGetExecutablePath(path
, &buflen
) != 0)
656 path
= (char *) realloc((void *) path
, buflen
);
657 while (buflen
> 0 && path
[buflen
-1] != '/')
659 return std::string(path
, buflen
);
661 #elif defined(_WIN32)
662 std::string
proc_self_dirname()
666 char longpath
[MAX_PATH
+ 1];
667 char shortpath
[MAX_PATH
+ 1];
669 WCHAR longpath
[MAX_PATH
+ 1];
670 TCHAR shortpath
[MAX_PATH
+ 1];
672 if (!GetModuleFileName(0, longpath
, MAX_PATH
+1))
673 log_error("GetModuleFileName() failed.\n");
674 if (!GetShortPathName(longpath
, shortpath
, MAX_PATH
+1))
675 log_error("GetShortPathName() failed.\n");
676 while (shortpath
[i
] != 0)
678 while (i
> 0 && shortpath
[i
-1] != '/' && shortpath
[i
-1] != '\\')
681 for (i
= 0; shortpath
[i
]; i
++)
682 path
+= char(shortpath
[i
]);
685 #elif defined(EMSCRIPTEN)
686 std::string
proc_self_dirname()
691 #error Dont know how to determine process executable base path!
695 std::string
proc_share_dirname()
700 std::string
proc_share_dirname()
702 std::string proc_self_path
= proc_self_dirname();
704 std::string proc_share_path
= proc_self_path
+ "share\\";
705 if (check_file_exists(proc_share_path
, true))
706 return proc_share_path
;
707 proc_share_path
= proc_self_path
+ "..\\share\\";
708 if (check_file_exists(proc_share_path
, true))
709 return proc_share_path
;
711 std::string proc_share_path
= proc_self_path
+ "share/";
712 if (check_file_exists(proc_share_path
, true))
713 return proc_share_path
;
714 proc_share_path
= proc_self_path
+ "../share/yosys/";
715 if (check_file_exists(proc_share_path
, true))
716 return proc_share_path
;
718 log_error("proc_share_dirname: unable to determine share/ directory!\n");
722 bool fgetline(FILE *f
, std::string
&buffer
)
727 if (fgets(block
, 4096, f
) == NULL
)
730 if (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r')) {
731 while (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r'))
732 buffer
.resize(buffer
.size()-1);
738 static void handle_label(std::string
&command
, bool &from_to_active
, const std::string
&run_from
, const std::string
&run_to
)
743 while (pos
< GetSize(command
) && (command
[pos
] == ' ' || command
[pos
] == '\t'))
746 if (pos
< GetSize(command
) && command
[pos
] == '#')
749 while (pos
< GetSize(command
) && command
[pos
] != ' ' && command
[pos
] != '\t' && command
[pos
] != '\r' && command
[pos
] != '\n')
750 label
+= command
[pos
++];
752 if (label
.back() == ':' && GetSize(label
) > 1)
754 label
= label
.substr(0, GetSize(label
)-1);
755 command
= command
.substr(pos
);
757 if (label
== run_from
)
758 from_to_active
= true;
759 else if (label
== run_to
|| (run_from
== run_to
&& !run_from
.empty()))
760 from_to_active
= false;
764 void run_frontend(std::string filename
, std::string command
, std::string
*backend_command
, std::string
*from_to_label
, RTLIL::Design
*design
)
766 if (design
== nullptr)
767 design
= yosys_design
;
769 if (command
== "auto") {
770 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
772 else if (filename
.size() > 2 && filename
.substr(filename
.size()-3) == ".sv")
773 command
= "verilog -sv";
774 else if (filename
.size() > 4 && filename
.substr(filename
.size()-5) == ".blif")
776 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
778 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".ys")
780 else if (filename
== "-")
783 log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename
.c_str());
786 if (command
== "script")
788 std::string run_from
, run_to
;
789 bool from_to_active
= true;
791 if (from_to_label
!= NULL
) {
792 size_t pos
= from_to_label
->find(':');
793 if (pos
== std::string::npos
) {
794 run_from
= *from_to_label
;
795 run_to
= *from_to_label
;
797 run_from
= from_to_label
->substr(0, pos
);
798 run_to
= from_to_label
->substr(pos
+1);
800 from_to_active
= run_from
.empty();
803 log("\n-- Executing script file `%s' --\n", filename
.c_str());
808 f
= fopen(filename
.c_str(), "r");
811 log_error("Can't open script file `%s' for reading: %s\n", filename
.c_str(), strerror(errno
));
813 FILE *backup_script_file
= Frontend::current_script_file
;
814 Frontend::current_script_file
= f
;
818 while (fgetline(f
, command
)) {
819 while (!command
.empty() && command
[command
.size()-1] == '\\') {
820 std::string next_line
;
821 if (!fgetline(f
, next_line
))
823 command
.resize(command
.size()-1);
824 command
+= next_line
;
826 handle_label(command
, from_to_active
, run_from
, run_to
);
828 Pass::call(design
, command
);
831 if (!command
.empty()) {
832 handle_label(command
, from_to_active
, run_from
, run_to
);
834 Pass::call(design
, command
);
838 Frontend::current_script_file
= backup_script_file
;
842 Frontend::current_script_file
= backup_script_file
;
847 if (backend_command
!= NULL
&& *backend_command
== "auto")
848 *backend_command
= "";
853 if (filename
== "-") {
854 log("\n-- Parsing stdin using frontend `%s' --\n", command
.c_str());
856 log("\n-- Parsing `%s' using frontend `%s' --\n", filename
.c_str(), command
.c_str());
859 Frontend::frontend_call(design
, NULL
, filename
, command
);
862 void run_frontend(std::string filename
, std::string command
, RTLIL::Design
*design
)
864 run_frontend(filename
, command
, nullptr, nullptr, design
);
867 void run_pass(std::string command
, RTLIL::Design
*design
)
869 if (design
== nullptr)
870 design
= yosys_design
;
872 log("\n-- Running command `%s' --\n", command
.c_str());
874 Pass::call(design
, command
);
877 void run_backend(std::string filename
, std::string command
, RTLIL::Design
*design
)
879 if (design
== nullptr)
880 design
= yosys_design
;
882 if (command
== "auto") {
883 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
885 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
887 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".blif")
889 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".edif")
891 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".json")
893 else if (filename
== "-")
895 else if (filename
.empty())
898 log_error("Can't guess backend for output file `%s' (missing -b option)!\n", filename
.c_str());
901 if (filename
.empty())
904 if (filename
== "-") {
905 log("\n-- Writing to stdout using backend `%s' --\n", command
.c_str());
907 log("\n-- Writing to `%s' using backend `%s' --\n", filename
.c_str(), command
.c_str());
910 Backend::backend_call(design
, NULL
, filename
, command
);
913 #ifdef YOSYS_ENABLE_READLINE
914 static char *readline_cmd_generator(const char *text
, int state
)
916 static std::map
<std::string
, Pass
*>::iterator it
;
920 it
= pass_register
.begin();
924 for (; it
!= pass_register
.end(); it
++) {
925 if (it
->first
.substr(0, len
) == text
)
926 return strdup((it
++)->first
.c_str());
931 static char *readline_obj_generator(const char *text
, int state
)
933 static std::vector
<char*> obj_names
;
941 RTLIL::Design
*design
= yosys_get_design();
942 int len
= strlen(text
);
944 if (design
->selected_active_module
.empty())
946 for (auto &it
: design
->modules_
)
947 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
948 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
951 if (design
->modules_
.count(design
->selected_active_module
) > 0)
953 RTLIL::Module
*module
= design
->modules_
.at(design
->selected_active_module
);
955 for (auto &it
: module
->wires_
)
956 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
957 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
959 for (auto &it
: module
->memories
)
960 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
961 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
963 for (auto &it
: module
->cells_
)
964 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
965 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
967 for (auto &it
: module
->processes
)
968 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
969 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
972 std::sort(obj_names
.begin(), obj_names
.end());
975 if (idx
< obj_names
.size())
976 return strdup(obj_names
[idx
++]);
983 static char **readline_completion(const char *text
, int start
, int)
986 return rl_completion_matches(text
, readline_cmd_generator
);
987 if (strncmp(rl_line_buffer
, "read_", 5) && strncmp(rl_line_buffer
, "write_", 6))
988 return rl_completion_matches(text
, readline_obj_generator
);
993 void shell(RTLIL::Design
*design
)
995 static int recursion_counter
= 0;
998 log_cmd_error_throw
= true;
1000 #ifdef YOSYS_ENABLE_READLINE
1001 rl_readline_name
= "yosys";
1002 rl_attempted_completion_function
= readline_completion
;
1003 rl_basic_word_break_characters
= " \t\n";
1006 char *command
= NULL
;
1007 #ifdef YOSYS_ENABLE_READLINE
1008 while ((command
= readline(create_prompt(design
, recursion_counter
))) != NULL
)
1011 char command_buffer
[4096];
1014 fputs(create_prompt(design
, recursion_counter
), stdout
);
1016 if ((command
= fgets(command_buffer
, 4096, stdin
)) == NULL
)
1019 if (command
[strspn(command
, " \t\r\n")] == 0)
1021 #ifdef YOSYS_ENABLE_READLINE
1022 add_history(command
);
1025 char *p
= command
+ strspn(command
, " \t\r\n");
1026 if (!strncmp(p
, "exit", 4)) {
1028 p
+= strspn(p
, " \t\r\n");
1034 log_assert(design
->selection_stack
.size() == 1);
1035 Pass::call(design
, command
);
1036 } catch (log_cmd_error_exception
) {
1037 while (design
->selection_stack
.size() > 1)
1038 design
->selection_stack
.pop_back();
1042 if (command
== NULL
)
1045 recursion_counter
--;
1046 log_cmd_error_throw
= false;
1049 struct ShellPass
: public Pass
{
1050 ShellPass() : Pass("shell", "enter interactive command mode") { }
1051 virtual void help() {
1055 log("This command enters the interactive command mode. This can be useful\n");
1056 log("in a script to interrupt the script at a certain point and allow for\n");
1057 log("interactive inspection or manual synthesis of the design at this point.\n");
1059 log("The command prompt of the interactive shell indicates the current\n");
1060 log("selection (see 'help select'):\n");
1063 log(" the entire design is selected\n");
1066 log(" only part of the design is selected\n");
1068 log(" yosys [modname]>\n");
1069 log(" the entire module 'modname' is selected using 'select -module modname'\n");
1071 log(" yosys [modname]*>\n");
1072 log(" only part of current module 'modname' is selected\n");
1074 log("When in interactive shell, some errors (e.g. invalid command arguments)\n");
1075 log("do not terminate yosys but return to the command prompt.\n");
1077 log("This command is the default action if nothing else has been specified\n");
1078 log("on the command line.\n");
1080 log("Press Ctrl-D or type 'exit' to leave the interactive shell.\n");
1083 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1084 extra_args(args
, 1, design
, false);
1089 #ifdef YOSYS_ENABLE_READLINE
1090 struct HistoryPass
: public Pass
{
1091 HistoryPass() : Pass("history", "show last interactive commands") { }
1092 virtual void help() {
1096 log("This command prints all commands in the shell history buffer. This are\n");
1097 log("all commands executed in an interactive session, but not the commands\n");
1098 log("from executed scripts.\n");
1101 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1102 extra_args(args
, 1, design
, false);
1103 for(HIST_ENTRY
**list
= history_list(); *list
!= NULL
; list
++)
1104 log("%s\n", (*list
)->line
);
1109 struct ScriptPass
: public Pass
{
1110 ScriptPass() : Pass("script", "execute commands from script file") { }
1111 virtual void help() {
1113 log(" script <filename> [<from_label>:<to_label>]\n");
1115 log("This command executes the yosys commands in the specified file.\n");
1117 log("The 2nd argument can be used to only execute the section of the\n");
1118 log("file between the specified labels. An empty from label is synonymous\n");
1119 log("for the beginning of the file and an empty to label is synonymous\n");
1120 log("for the end of the file.\n");
1122 log("If only one label is specified (without ':') then only the block\n");
1123 log("marked with that label (until the next label) is executed.\n");
1126 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1127 if (args
.size() < 2)
1128 log_cmd_error("Missing script file.\n");
1129 else if (args
.size() == 2)
1130 run_frontend(args
[1], "script", design
);
1131 else if (args
.size() == 3)
1132 run_frontend(args
[1], "script", NULL
, &args
[2], design
);
1134 extra_args(args
, 2, design
, false);