2 * yosys -- Yosys Open SYnthesis Suite
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "kernel/yosys.h"
21 #include "kernel/celltypes.h"
23 #ifdef YOSYS_ENABLE_READLINE
24 # include <readline/readline.h>
25 # include <readline/history.h>
28 #ifdef YOSYS_ENABLE_PLUGINS
35 #elif defined(__APPLE__)
36 # include <mach-o/dyld.h>
39 # include <sys/stat.h>
43 # include <sys/types.h>
44 # include <sys/stat.h>
54 RTLIL::Design
*yosys_design
= NULL
;
55 CellTypes yosys_celltypes
;
57 #ifdef YOSYS_ENABLE_TCL
58 Tcl_Interp
*yosys_tcl_interp
= NULL
;
61 bool memhasher_active
= false;
62 uint32_t memhasher_rng
= 123456;
63 std::vector
<void*> memhasher_store
;
68 memhasher_rng
+= time(NULL
) << 16 ^ getpid();
70 memhasher_store
.resize(0x10000);
71 memhasher_active
= true;
76 for (auto p
: memhasher_store
)
78 memhasher_store
.clear();
79 memhasher_active
= false;
84 memhasher_rng
^= memhasher_rng
<< 13;
85 memhasher_rng
^= memhasher_rng
>> 17;
86 memhasher_rng
^= memhasher_rng
<< 5;
88 int size
, index
= (memhasher_rng
>> 4) & 0xffff;
89 switch (memhasher_rng
& 7) {
90 case 0: size
= 16; break;
91 case 1: size
= 256; break;
92 case 2: size
= 1024; break;
93 case 3: size
= 4096; break;
96 if (index
< 16) size
*= 16;
97 memhasher_store
[index
] = realloc(memhasher_store
[index
], size
);
103 log(" /----------------------------------------------------------------------------\\\n");
105 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
107 log(" | Copyright (C) 2012 - 2015 Clifford Wolf <clifford@clifford.at> |\n");
109 log(" | Permission to use, copy, modify, and/or distribute this software for any |\n");
110 log(" | purpose with or without fee is hereby granted, provided that the above |\n");
111 log(" | copyright notice and this permission notice appear in all copies. |\n");
113 log(" | THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |\n");
114 log(" | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |\n");
115 log(" | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |\n");
116 log(" | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |\n");
117 log(" | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |\n");
118 log(" | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |\n");
119 log(" | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |\n");
121 log(" \\----------------------------------------------------------------------------/\n");
123 log(" %s\n", yosys_version_str
);
127 std::string
stringf(const char *fmt
, ...)
133 string
= vstringf(fmt
, ap
);
139 std::string
vstringf(const char *fmt
, va_list ap
)
149 str
= (char*)realloc(str
, sz
);
150 rc
= vsnprintf(str
, sz
, fmt
, apc
);
152 if (rc
>= 0 && rc
< sz
)
157 if (vasprintf(&str
, fmt
, ap
) < 0)
169 int readsome(std::istream
&f
, char *s
, int n
)
171 int rc
= f
.readsome(s
, n
);
173 // f.readsome() sometimes returns 0 on a non-empty stream..
185 std::string
next_token(std::string
&text
, const char *sep
)
187 size_t pos_begin
= text
.find_first_not_of(sep
);
189 if (pos_begin
== std::string::npos
)
190 pos_begin
= text
.size();
192 size_t pos_end
= text
.find_first_of(sep
, pos_begin
);
194 if (pos_end
== std::string::npos
)
195 pos_end
= text
.size();
197 std::string token
= text
.substr(pos_begin
, pos_end
-pos_begin
);
198 text
= text
.substr(pos_end
);
202 // this is very similar to fnmatch(). the exact rules used by this
205 // ? matches any character except
206 // * matches any sequence of characters
207 // [...] matches any of the characters in the list
208 // [!..] matches any of the characters not in the list
210 // a backslash may be used to escape the next characters in the
211 // pattern. each special character can also simply match itself.
213 bool patmatch(const char *pattern
, const char *string
)
218 if (*pattern
== '\\') {
219 if (pattern
[1] == string
[0] && patmatch(pattern
+2, string
+1))
223 if (*pattern
== '?') {
226 return patmatch(pattern
+1, string
+1);
229 if (*pattern
== '*') {
231 if (patmatch(pattern
+1, string
++))
234 return pattern
[1] == 0;
237 if (*pattern
== '[') {
238 bool found_match
= false;
239 bool inverted_list
= pattern
[1] == '!';
240 const char *p
= pattern
+ (inverted_list
? 1 : 0);
244 if (found_match
!= inverted_list
&& patmatch(p
+1, string
+1))
258 if (*pattern
== *string
)
259 return patmatch(pattern
+1, string
+1);
264 int run_command(const std::string
&command
, std::function
<void(const std::string
&)> process_line
)
267 return system(command
.c_str());
269 FILE *f
= popen(command
.c_str(), "r");
275 while (fgets(logbuf
, 128, f
) != NULL
) {
277 if (!line
.empty() && line
.back() == '\n')
278 process_line(line
), line
.clear();
289 return WEXITSTATUS(ret
);
293 std::string
make_temp_file(std::string template_str
)
296 if (template_str
.rfind("/tmp/", 0) == 0) {
298 char longpath
[MAX_PATH
+ 1];
299 char shortpath
[MAX_PATH
+ 1];
301 WCHAR longpath
[MAX_PATH
+ 1];
302 TCHAR shortpath
[MAX_PATH
+ 1];
304 if (!GetTempPath(MAX_PATH
+1, longpath
))
305 log_error("GetTempPath() failed.\n");
306 if (!GetShortPathName(longpath
, shortpath
, MAX_PATH
+ 1))
307 log_error("GetShortPathName() failed.\n");
309 for (int i
= 0; shortpath
[i
]; i
++)
310 path
+= char(shortpath
[i
]);
311 template_str
= stringf("%s\\%s", path
.c_str(), template_str
.c_str() + 5);
314 size_t pos
= template_str
.rfind("XXXXXX");
315 log_assert(pos
!= std::string::npos
);
318 for (int i
= 0; i
< 6; i
++) {
319 static std::string y
= "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
320 static uint32_t x
= 314159265 ^ uint32_t(time(NULL
));
321 x
^= x
<< 13, x
^= x
>> 17, x
^= x
<< 5;
322 template_str
[pos
+i
] = y
[x
% y
.size()];
324 if (_access(template_str
.c_str(), 0) != 0)
328 size_t pos
= template_str
.rfind("XXXXXX");
329 log_assert(pos
!= std::string::npos
);
331 int suffixlen
= GetSize(template_str
) - pos
- 6;
333 char *p
= strdup(template_str
.c_str());
334 close(mkstemps(p
, suffixlen
));
342 std::string
make_temp_dir(std::string template_str
)
345 template_str
= make_temp_file(template_str
);
346 mkdir(template_str
.c_str());
350 size_t pos
= template_str
.rfind("XXXXXX");
351 log_assert(pos
!= std::string::npos
);
353 int suffixlen
= GetSize(template_str
) - pos
- 6;
354 log_assert(suffixlen
== 0);
357 char *p
= strdup(template_str
.c_str());
359 log_assert(p
!= NULL
);
368 bool check_file_exists(std::string filename
, bool)
370 return _access(filename
.c_str(), 0) == 0;
373 bool check_file_exists(std::string filename
, bool is_exec
)
375 return access(filename
.c_str(), is_exec
? X_OK
: F_OK
) == 0;
379 bool is_absolute_path(std::string filename
)
382 return filename
[0] == '/' || filename
[0] == '\\' || (filename
[0] != 0 && filename
[1] == ':');
384 return filename
[0] == '/';
388 void remove_directory(std::string dirname
)
391 run_command(stringf("rmdir /s /q \"%s\"", dirname
.c_str()));
394 struct dirent
**namelist
;
395 int n
= scandir(dirname
.c_str(), &namelist
, nullptr, alphasort
);
397 for (int i
= 0; i
< n
; i
++) {
398 if (strcmp(namelist
[i
]->d_name
, ".") && strcmp(namelist
[i
]->d_name
, "..")) {
399 std::string buffer
= stringf("%s/%s", dirname
.c_str(), namelist
[i
]->d_name
);
400 if (!stat(buffer
.c_str(), &stbuf
) && S_ISREG(stbuf
.st_mode
)) {
401 remove(buffer
.c_str());
403 remove_directory(buffer
);
408 rmdir(dirname
.c_str());
412 int GetSize(RTLIL::Wire
*wire
)
419 // if there are already IdString objects then we have a global initialization order bug
421 log_assert(empty_id
.index_
== 0);
422 IdString::get_reference(empty_id
.index_
);
424 Pass::init_register();
425 yosys_design
= new RTLIL::Design
;
426 yosys_celltypes
.setup();
430 void yosys_shutdown()
437 for (auto f
: log_files
)
443 Pass::done_register();
444 yosys_celltypes
.clear();
446 #ifdef YOSYS_ENABLE_TCL
447 if (yosys_tcl_interp
!= NULL
) {
448 Tcl_DeleteInterp(yosys_tcl_interp
);
450 yosys_tcl_interp
= NULL
;
454 #ifdef YOSYS_ENABLE_PLUGINS
455 for (auto &it
: loaded_plugins
)
458 loaded_plugins
.clear();
459 loaded_plugin_aliases
.clear();
463 IdString::put_reference(empty_id
.index_
);
466 RTLIL::IdString
new_id(std::string file
, int line
, std::string func
)
469 size_t pos
= file
.find_last_of("/\\");
471 size_t pos
= file
.find_last_of('/');
473 if (pos
!= std::string::npos
)
474 file
= file
.substr(pos
+1);
476 pos
= func
.find_last_of(':');
477 if (pos
!= std::string::npos
)
478 func
= func
.substr(pos
+1);
480 return stringf("$auto$%s:%d:%s$%d", file
.c_str(), line
, func
.c_str(), autoidx
++);
483 RTLIL::Design
*yosys_get_design()
488 const char *create_prompt(RTLIL::Design
*design
, int recursion_counter
)
490 static char buffer
[100];
491 std::string str
= "\n";
492 if (recursion_counter
> 1)
493 str
+= stringf("(%d) ", recursion_counter
);
495 if (!design
->selected_active_module
.empty())
496 str
+= stringf(" [%s]", RTLIL::unescape_id(design
->selected_active_module
).c_str());
497 if (!design
->selection_stack
.empty() && !design
->selection_stack
.back().full_selection
) {
498 if (design
->selected_active_module
.empty())
500 else if (design
->selection_stack
.back().selected_modules
.size() != 1 || design
->selection_stack
.back().selected_members
.size() != 0 ||
501 design
->selection_stack
.back().selected_modules
.count(design
->selected_active_module
) == 0)
504 snprintf(buffer
, 100, "%s> ", str
.c_str());
508 #ifdef YOSYS_ENABLE_TCL
509 static int tcl_yosys_cmd(ClientData
, Tcl_Interp
*interp
, int argc
, const char *argv
[])
511 std::vector
<std::string
> args
;
512 for (int i
= 1; i
< argc
; i
++)
513 args
.push_back(argv
[i
]);
515 if (args
.size() >= 1 && args
[0] == "-import") {
516 for (auto &it
: pass_register
) {
517 std::string tcl_command_name
= it
.first
;
518 if (tcl_command_name
== "proc")
519 tcl_command_name
= "procs";
521 if (Tcl_GetCommandInfo(interp
, tcl_command_name
.c_str(), &info
) != 0) {
522 log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it
.first
.c_str());
524 std::string tcl_script
= stringf("proc %s args { yosys %s {*}$args }", tcl_command_name
.c_str(), it
.first
.c_str());
525 Tcl_Eval(interp
, tcl_script
.c_str());
531 if (args
.size() == 1) {
532 Pass::call(yosys_get_design(), args
[0]);
536 Pass::call(yosys_get_design(), args
);
540 extern Tcl_Interp
*yosys_get_tcl_interp()
542 if (yosys_tcl_interp
== NULL
) {
543 yosys_tcl_interp
= Tcl_CreateInterp();
544 Tcl_CreateCommand(yosys_tcl_interp
, "yosys", tcl_yosys_cmd
, NULL
, NULL
);
546 return yosys_tcl_interp
;
549 struct TclPass
: public Pass
{
550 TclPass() : Pass("tcl", "execute a TCL script file") { }
551 virtual void help() {
553 log(" tcl <filename>\n");
555 log("This command executes the tcl commands in the specified file.\n");
556 log("Use 'yosys cmd' to run the yosys command 'cmd' from tcl.\n");
558 log("The tcl command 'yosys -import' can be used to import all yosys\n");
559 log("commands directly as tcl commands to the tcl shell. The yosys\n");
560 log("command 'proc' is wrapped using the tcl command 'procs' in order\n");
561 log("to avoid a name collision with the tcl builting command 'proc'.\n");
564 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
566 log_cmd_error("Missing script file.\n");
568 extra_args(args
, 1, design
, false);
569 if (Tcl_EvalFile(yosys_get_tcl_interp(), args
[1].c_str()) != TCL_OK
)
570 log_cmd_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
575 #if defined(__linux__)
576 std::string
proc_self_dirname()
579 ssize_t buflen
= readlink("/proc/self/exe", path
, sizeof(path
));
581 log_error("readlink(\"/proc/self/exe\") failed: %s\n", strerror(errno
));
583 while (buflen
> 0 && path
[buflen
-1] != '/')
585 return std::string(path
, buflen
);
587 #elif defined(__APPLE__)
588 std::string
proc_self_dirname()
592 while (_NSGetExecutablePath(path
, &buflen
) != 0)
593 path
= (char *) realloc((void *) path
, buflen
);
594 while (buflen
> 0 && path
[buflen
-1] != '/')
596 return std::string(path
, buflen
);
598 #elif defined(_WIN32)
599 std::string
proc_self_dirname()
603 char longpath
[MAX_PATH
+ 1];
604 char shortpath
[MAX_PATH
+ 1];
606 WCHAR longpath
[MAX_PATH
+ 1];
607 TCHAR shortpath
[MAX_PATH
+ 1];
609 if (!GetModuleFileName(0, longpath
, MAX_PATH
+1))
610 log_error("GetModuleFileName() failed.\n");
611 if (!GetShortPathName(longpath
, shortpath
, MAX_PATH
+1))
612 log_error("GetShortPathName() failed.\n");
613 while (shortpath
[i
] != 0)
615 while (i
> 0 && shortpath
[i
-1] != '/' && shortpath
[i
-1] != '\\')
618 for (i
= 0; shortpath
[i
]; i
++)
619 path
+= char(shortpath
[i
]);
622 #elif defined(EMSCRIPTEN)
623 std::string
proc_self_dirname()
628 #error Dont know how to determine process executable base path!
632 std::string
proc_share_dirname()
637 std::string
proc_share_dirname()
639 std::string proc_self_path
= proc_self_dirname();
641 std::string proc_share_path
= proc_self_path
+ "share\\";
642 if (check_file_exists(proc_share_path
, true))
643 return proc_share_path
;
644 proc_share_path
= proc_self_path
+ "..\\share\\";
645 if (check_file_exists(proc_share_path
, true))
646 return proc_share_path
;
648 std::string proc_share_path
= proc_self_path
+ "share/";
649 if (check_file_exists(proc_share_path
, true))
650 return proc_share_path
;
651 proc_share_path
= proc_self_path
+ "../share/yosys/";
652 if (check_file_exists(proc_share_path
, true))
653 return proc_share_path
;
655 log_error("proc_share_dirname: unable to determine share/ directory!\n");
659 bool fgetline(FILE *f
, std::string
&buffer
)
664 if (fgets(block
, 4096, f
) == NULL
)
667 if (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r')) {
668 while (buffer
.size() > 0 && (buffer
[buffer
.size()-1] == '\n' || buffer
[buffer
.size()-1] == '\r'))
669 buffer
.resize(buffer
.size()-1);
675 static void handle_label(std::string
&command
, bool &from_to_active
, const std::string
&run_from
, const std::string
&run_to
)
680 while (pos
< GetSize(command
) && (command
[pos
] == ' ' || command
[pos
] == '\t'))
683 while (pos
< GetSize(command
) && command
[pos
] != ' ' && command
[pos
] != '\t' && command
[pos
] != '\r' && command
[pos
] != '\n')
684 label
+= command
[pos
++];
686 if (label
.back() == ':' && GetSize(label
) > 1)
688 label
= label
.substr(0, GetSize(label
)-1);
689 command
= command
.substr(pos
);
691 if (label
== run_from
)
692 from_to_active
= true;
693 else if (label
== run_to
|| (run_from
== run_to
&& !run_from
.empty()))
694 from_to_active
= false;
698 void run_frontend(std::string filename
, std::string command
, std::string
*backend_command
, std::string
*from_to_label
, RTLIL::Design
*design
)
700 if (design
== nullptr)
701 design
= yosys_design
;
703 if (command
== "auto") {
704 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
706 else if (filename
.size() > 2 && filename
.substr(filename
.size()-3) == ".sv")
707 command
= "verilog -sv";
708 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
710 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".ys")
712 else if (filename
== "-")
715 log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename
.c_str());
718 if (command
== "script")
720 std::string run_from
, run_to
;
721 bool from_to_active
= true;
723 if (from_to_label
!= NULL
) {
724 size_t pos
= from_to_label
->find(':');
725 if (pos
== std::string::npos
) {
726 run_from
= *from_to_label
;
727 run_to
= *from_to_label
;
729 run_from
= from_to_label
->substr(0, pos
);
730 run_to
= from_to_label
->substr(pos
+1);
732 from_to_active
= run_from
.empty();
735 log("\n-- Executing script file `%s' --\n", filename
.c_str());
740 f
= fopen(filename
.c_str(), "r");
743 log_error("Can't open script file `%s' for reading: %s\n", filename
.c_str(), strerror(errno
));
745 FILE *backup_script_file
= Frontend::current_script_file
;
746 Frontend::current_script_file
= f
;
750 while (fgetline(f
, command
)) {
751 while (!command
.empty() && command
[command
.size()-1] == '\\') {
752 std::string next_line
;
753 if (!fgetline(f
, next_line
))
755 command
.resize(command
.size()-1);
756 command
+= next_line
;
758 handle_label(command
, from_to_active
, run_from
, run_to
);
760 Pass::call(design
, command
);
763 if (!command
.empty()) {
764 handle_label(command
, from_to_active
, run_from
, run_to
);
766 Pass::call(design
, command
);
770 Frontend::current_script_file
= backup_script_file
;
774 Frontend::current_script_file
= backup_script_file
;
779 if (backend_command
!= NULL
&& *backend_command
== "auto")
780 *backend_command
= "";
785 if (filename
== "-") {
786 log("\n-- Parsing stdin using frontend `%s' --\n", command
.c_str());
788 log("\n-- Parsing `%s' using frontend `%s' --\n", filename
.c_str(), command
.c_str());
791 Frontend::frontend_call(design
, NULL
, filename
, command
);
794 void run_frontend(std::string filename
, std::string command
, RTLIL::Design
*design
)
796 run_frontend(filename
, command
, nullptr, nullptr, design
);
799 void run_pass(std::string command
, RTLIL::Design
*design
)
801 if (design
== nullptr)
802 design
= yosys_design
;
804 log("\n-- Running command `%s' --\n", command
.c_str());
806 Pass::call(design
, command
);
809 void run_backend(std::string filename
, std::string command
, RTLIL::Design
*design
)
811 if (design
== nullptr)
812 design
= yosys_design
;
814 if (command
== "auto") {
815 if (filename
.size() > 2 && filename
.substr(filename
.size()-2) == ".v")
817 else if (filename
.size() > 3 && filename
.substr(filename
.size()-3) == ".il")
819 else if (filename
.size() > 5 && filename
.substr(filename
.size()-5) == ".blif")
821 else if (filename
== "-")
823 else if (filename
.empty())
826 log_error("Can't guess backend for output file `%s' (missing -b option)!\n", filename
.c_str());
829 if (filename
.empty())
832 if (filename
== "-") {
833 log("\n-- Writing to stdout using backend `%s' --\n", command
.c_str());
835 log("\n-- Writing to `%s' using backend `%s' --\n", filename
.c_str(), command
.c_str());
838 Backend::backend_call(design
, NULL
, filename
, command
);
841 #ifdef YOSYS_ENABLE_READLINE
842 static char *readline_cmd_generator(const char *text
, int state
)
844 static std::map
<std::string
, Pass
*>::iterator it
;
848 it
= pass_register
.begin();
852 for (; it
!= pass_register
.end(); it
++) {
853 if (it
->first
.substr(0, len
) == text
)
854 return strdup((it
++)->first
.c_str());
859 static char *readline_obj_generator(const char *text
, int state
)
861 static std::vector
<char*> obj_names
;
869 RTLIL::Design
*design
= yosys_get_design();
870 int len
= strlen(text
);
872 if (design
->selected_active_module
.empty())
874 for (auto &it
: design
->modules_
)
875 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
876 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
879 if (design
->modules_
.count(design
->selected_active_module
) > 0)
881 RTLIL::Module
*module
= design
->modules_
.at(design
->selected_active_module
);
883 for (auto &it
: module
->wires_
)
884 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
885 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
887 for (auto &it
: module
->memories
)
888 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
889 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
891 for (auto &it
: module
->cells_
)
892 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
893 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
895 for (auto &it
: module
->processes
)
896 if (RTLIL::unescape_id(it
.first
).substr(0, len
) == text
)
897 obj_names
.push_back(strdup(RTLIL::id2cstr(it
.first
)));
900 std::sort(obj_names
.begin(), obj_names
.end());
903 if (idx
< obj_names
.size())
904 return strdup(obj_names
[idx
++]);
911 static char **readline_completion(const char *text
, int start
, int)
914 return rl_completion_matches(text
, readline_cmd_generator
);
915 if (strncmp(rl_line_buffer
, "read_", 5) && strncmp(rl_line_buffer
, "write_", 6))
916 return rl_completion_matches(text
, readline_obj_generator
);
921 void shell(RTLIL::Design
*design
)
923 static int recursion_counter
= 0;
926 log_cmd_error_throw
= true;
928 #ifdef YOSYS_ENABLE_READLINE
929 rl_readline_name
= "yosys";
930 rl_attempted_completion_function
= readline_completion
;
931 rl_basic_word_break_characters
= " \t\n";
934 char *command
= NULL
;
935 #ifdef YOSYS_ENABLE_READLINE
936 while ((command
= readline(create_prompt(design
, recursion_counter
))) != NULL
)
939 char command_buffer
[4096];
942 fputs(create_prompt(design
, recursion_counter
), stdout
);
944 if ((command
= fgets(command_buffer
, 4096, stdin
)) == NULL
)
947 if (command
[strspn(command
, " \t\r\n")] == 0)
949 #ifdef YOSYS_ENABLE_READLINE
950 add_history(command
);
953 char *p
= command
+ strspn(command
, " \t\r\n");
954 if (!strncmp(p
, "exit", 4)) {
956 p
+= strspn(p
, " \t\r\n");
962 log_assert(design
->selection_stack
.size() == 1);
963 Pass::call(design
, command
);
964 } catch (log_cmd_error_exception
) {
965 while (design
->selection_stack
.size() > 1)
966 design
->selection_stack
.pop_back();
974 log_cmd_error_throw
= false;
977 struct ShellPass
: public Pass
{
978 ShellPass() : Pass("shell", "enter interactive command mode") { }
979 virtual void help() {
983 log("This command enters the interactive command mode. This can be useful\n");
984 log("in a script to interrupt the script at a certain point and allow for\n");
985 log("interactive inspection or manual synthesis of the design at this point.\n");
987 log("The command prompt of the interactive shell indicates the current\n");
988 log("selection (see 'help select'):\n");
991 log(" the entire design is selected\n");
994 log(" only part of the design is selected\n");
996 log(" yosys [modname]>\n");
997 log(" the entire module 'modname' is selected using 'select -module modname'\n");
999 log(" yosys [modname]*>\n");
1000 log(" only part of current module 'modname' is selected\n");
1002 log("When in interactive shell, some errors (e.g. invalid command arguments)\n");
1003 log("do not terminate yosys but return to the command prompt.\n");
1005 log("This command is the default action if nothing else has been specified\n");
1006 log("on the command line.\n");
1008 log("Press Ctrl-D or type 'exit' to leave the interactive shell.\n");
1011 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1012 extra_args(args
, 1, design
, false);
1017 #ifdef YOSYS_ENABLE_READLINE
1018 struct HistoryPass
: public Pass
{
1019 HistoryPass() : Pass("history", "show last interactive commands") { }
1020 virtual void help() {
1024 log("This command prints all commands in the shell history buffer. This are\n");
1025 log("all commands executed in an interactive session, but not the commands\n");
1026 log("from executed scripts.\n");
1029 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1030 extra_args(args
, 1, design
, false);
1031 for(HIST_ENTRY
**list
= history_list(); *list
!= NULL
; list
++)
1032 log("%s\n", (*list
)->line
);
1037 struct ScriptPass
: public Pass
{
1038 ScriptPass() : Pass("script", "execute commands from script file") { }
1039 virtual void help() {
1041 log(" script <filename> [<from_label>:<to_label>]\n");
1043 log("This command executes the yosys commands in the specified file.\n");
1045 log("The 2nd argument can be used to only execute the section of the\n");
1046 log("file between the specified labels. An empty from label is synonymous\n");
1047 log("for the beginning of the file and an empty to label is synonymous\n");
1048 log("for the end of the file.\n");
1050 log("If only one label is specified (without ':') then only the block\n");
1051 log("marked with that label (until the next label) is executed.\n");
1054 virtual void execute(std::vector
<std::string
> args
, RTLIL::Design
*design
) {
1055 if (args
.size() < 2)
1056 log_cmd_error("Missing script file.\n");
1057 else if (args
.size() == 2)
1058 run_frontend(args
[1], "script", design
);
1059 else if (args
.size() == 3)
1060 run_frontend(args
[1], "script", NULL
, &args
[2], design
);
1062 extra_args(args
, 2, design
, false);