cd6955c3fc76fb5544aedd797ff4c4ce1b31c7ee
[yosys.git] / kernel / yosys.cc
1 /*
2 * yosys -- Yosys Open SYnthesis Suite
3 *
4 * Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
5 *
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.
9 *
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.
17 *
18 */
19
20 #include "kernel/yosys.h"
21 #include "kernel/celltypes.h"
22
23 #ifdef YOSYS_ENABLE_READLINE
24 # include <readline/readline.h>
25 # include <readline/history.h>
26 #endif
27
28 #ifdef YOSYS_ENABLE_EDITLINE
29 # include <editline/readline.h>
30 #endif
31
32 #ifdef YOSYS_ENABLE_PLUGINS
33 # include <dlfcn.h>
34 #endif
35
36 #if defined(_WIN32)
37 # include <windows.h>
38 # include <io.h>
39 #elif defined(__APPLE__)
40 # include <mach-o/dyld.h>
41 # include <unistd.h>
42 # include <dirent.h>
43 # include <sys/stat.h>
44 #else
45 # include <unistd.h>
46 # include <dirent.h>
47 # include <sys/types.h>
48 # include <sys/wait.h>
49 # include <sys/stat.h>
50 #endif
51
52 #if !defined(_WIN32) && defined(YOSYS_ENABLE_GLOB)
53 # include <glob.h>
54 #endif
55
56 #ifdef __FreeBSD__
57 # include <sys/sysctl.h>
58 #endif
59
60 #ifdef WITH_PYTHON
61 #if PY_MAJOR_VERSION >= 3
62 # define INIT_MODULE PyInit_libyosys
63 extern "C" PyObject* INIT_MODULE();
64 #else
65 # define INIT_MODULE initlibyosys
66 extern "C" void INIT_MODULE();
67 #endif
68 #endif
69
70 #include <limits.h>
71 #include <errno.h>
72
73 YOSYS_NAMESPACE_BEGIN
74
75 int autoidx = 1;
76 int yosys_xtrace = 0;
77 RTLIL::Design *yosys_design = NULL;
78 CellTypes yosys_celltypes;
79
80 #ifdef YOSYS_ENABLE_TCL
81 Tcl_Interp *yosys_tcl_interp = NULL;
82 #endif
83
84 std::set<std::string> yosys_input_files, yosys_output_files;
85
86 bool memhasher_active = false;
87 uint32_t memhasher_rng = 123456;
88 std::vector<void*> memhasher_store;
89
90 void memhasher_on()
91 {
92 #if defined(__linux__) || defined(__FreeBSD__)
93 memhasher_rng += time(NULL) << 16 ^ getpid();
94 #endif
95 memhasher_store.resize(0x10000);
96 memhasher_active = true;
97 }
98
99 void memhasher_off()
100 {
101 for (auto p : memhasher_store)
102 if (p) free(p);
103 memhasher_store.clear();
104 memhasher_active = false;
105 }
106
107 void memhasher_do()
108 {
109 memhasher_rng ^= memhasher_rng << 13;
110 memhasher_rng ^= memhasher_rng >> 17;
111 memhasher_rng ^= memhasher_rng << 5;
112
113 int size, index = (memhasher_rng >> 4) & 0xffff;
114 switch (memhasher_rng & 7) {
115 case 0: size = 16; break;
116 case 1: size = 256; break;
117 case 2: size = 1024; break;
118 case 3: size = 4096; break;
119 default: size = 0;
120 }
121 if (index < 16) size *= 16;
122 memhasher_store[index] = realloc(memhasher_store[index], size);
123 }
124
125 void yosys_banner()
126 {
127 log("\n");
128 log(" /----------------------------------------------------------------------------\\\n");
129 log(" | |\n");
130 log(" | yosys -- Yosys Open SYnthesis Suite |\n");
131 log(" | |\n");
132 log(" | Copyright (C) 2012 - 2019 Clifford Wolf <clifford@clifford.at> |\n");
133 log(" | |\n");
134 log(" | Permission to use, copy, modify, and/or distribute this software for any |\n");
135 log(" | purpose with or without fee is hereby granted, provided that the above |\n");
136 log(" | copyright notice and this permission notice appear in all copies. |\n");
137 log(" | |\n");
138 log(" | THE SOFTWARE IS PROVIDED \"AS IS\" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |\n");
139 log(" | WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |\n");
140 log(" | MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |\n");
141 log(" | ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |\n");
142 log(" | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |\n");
143 log(" | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |\n");
144 log(" | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |\n");
145 log(" | |\n");
146 log(" \\----------------------------------------------------------------------------/\n");
147 log("\n");
148 log(" %s\n", yosys_version_str);
149 log("\n");
150 }
151
152 int ceil_log2(int x)
153 {
154 #if defined(__GNUC__)
155 return x > 1 ? (8*sizeof(int)) - __builtin_clz(x-1) : 0;
156 #else
157 if (x <= 0)
158 return 0;
159 for (int i = 0; i < 32; i++)
160 if (((x-1) >> i) == 0)
161 return i;
162 log_abort();
163 #endif
164 }
165
166 std::string stringf(const char *fmt, ...)
167 {
168 std::string string;
169 va_list ap;
170
171 va_start(ap, fmt);
172 string = vstringf(fmt, ap);
173 va_end(ap);
174
175 return string;
176 }
177
178 std::string vstringf(const char *fmt, va_list ap)
179 {
180 std::string string;
181 char *str = NULL;
182
183 #if defined(_WIN32 )|| defined(__CYGWIN__)
184 int sz = 64, rc;
185 while (1) {
186 va_list apc;
187 va_copy(apc, ap);
188 str = (char*)realloc(str, sz);
189 rc = vsnprintf(str, sz, fmt, apc);
190 va_end(apc);
191 if (rc >= 0 && rc < sz)
192 break;
193 sz *= 2;
194 }
195 #else
196 if (vasprintf(&str, fmt, ap) < 0)
197 str = NULL;
198 #endif
199
200 if (str != NULL) {
201 string = str;
202 free(str);
203 }
204
205 return string;
206 }
207
208 int readsome(std::istream &f, char *s, int n)
209 {
210 int rc = int(f.readsome(s, n));
211
212 // f.readsome() sometimes returns 0 on a non-empty stream..
213 if (rc == 0) {
214 int c = f.get();
215 if (c != EOF) {
216 *s = c;
217 rc = 1;
218 }
219 }
220
221 return rc;
222 }
223
224 std::string next_token(std::string &text, const char *sep, bool long_strings)
225 {
226 size_t pos_begin = text.find_first_not_of(sep);
227
228 if (pos_begin == std::string::npos)
229 pos_begin = text.size();
230
231 if (long_strings && pos_begin != text.size() && text[pos_begin] == '"') {
232 string sep_string = sep;
233 for (size_t i = pos_begin+1; i < text.size(); i++) {
234 if (text[i] == '"' && (i+1 == text.size() || sep_string.find(text[i+1]) != std::string::npos)) {
235 std::string token = text.substr(pos_begin, i-pos_begin+1);
236 text = text.substr(i+1);
237 return token;
238 }
239 if (i+1 < text.size() && text[i] == '"' && text[i+1] == ';' && (i+2 == text.size() || sep_string.find(text[i+2]) != std::string::npos)) {
240 std::string token = text.substr(pos_begin, i-pos_begin+1);
241 text = text.substr(i+2);
242 return token + ";";
243 }
244 }
245 }
246
247 size_t pos_end = text.find_first_of(sep, pos_begin);
248
249 if (pos_end == std::string::npos)
250 pos_end = text.size();
251
252 std::string token = text.substr(pos_begin, pos_end-pos_begin);
253 text = text.substr(pos_end);
254 return token;
255 }
256
257 std::vector<std::string> split_tokens(const std::string &text, const char *sep)
258 {
259 std::vector<std::string> tokens;
260 std::string current_token;
261 for (char c : text) {
262 if (strchr(sep, c)) {
263 if (!current_token.empty()) {
264 tokens.push_back(current_token);
265 current_token.clear();
266 }
267 } else
268 current_token += c;
269 }
270 if (!current_token.empty()) {
271 tokens.push_back(current_token);
272 current_token.clear();
273 }
274 return tokens;
275 }
276
277 // this is very similar to fnmatch(). the exact rules used by this
278 // function are:
279 //
280 // ? matches any character except
281 // * matches any sequence of characters
282 // [...] matches any of the characters in the list
283 // [!..] matches any of the characters not in the list
284 //
285 // a backslash may be used to escape the next characters in the
286 // pattern. each special character can also simply match itself.
287 //
288 bool patmatch(const char *pattern, const char *string)
289 {
290 if (*pattern == 0)
291 return *string == 0;
292
293 if (*pattern == '\\') {
294 if (pattern[1] == string[0] && patmatch(pattern+2, string+1))
295 return true;
296 }
297
298 if (*pattern == '?') {
299 if (*string == 0)
300 return false;
301 return patmatch(pattern+1, string+1);
302 }
303
304 if (*pattern == '*') {
305 while (*string) {
306 if (patmatch(pattern+1, string++))
307 return true;
308 }
309 return pattern[1] == 0;
310 }
311
312 if (*pattern == '[') {
313 bool found_match = false;
314 bool inverted_list = pattern[1] == '!';
315 const char *p = pattern + (inverted_list ? 1 : 0);
316
317 while (*++p) {
318 if (*p == ']') {
319 if (found_match != inverted_list && patmatch(p+1, string+1))
320 return true;
321 break;
322 }
323
324 if (*p == '\\') {
325 if (*++p == *string)
326 found_match = true;
327 } else
328 if (*p == *string)
329 found_match = true;
330 }
331 }
332
333 if (*pattern == *string)
334 return patmatch(pattern+1, string+1);
335
336 return false;
337 }
338
339 int run_command(const std::string &command, std::function<void(const std::string&)> process_line)
340 {
341 if (!process_line)
342 return system(command.c_str());
343
344 FILE *f = popen(command.c_str(), "r");
345 if (f == nullptr)
346 return -1;
347
348 std::string line;
349 char logbuf[128];
350 while (fgets(logbuf, 128, f) != NULL) {
351 line += logbuf;
352 if (!line.empty() && line.back() == '\n')
353 process_line(line), line.clear();
354 }
355 if (!line.empty())
356 process_line(line);
357
358 int ret = pclose(f);
359 if (ret < 0)
360 return -1;
361 #ifdef _WIN32
362 return ret;
363 #else
364 return WEXITSTATUS(ret);
365 #endif
366 }
367
368 std::string make_temp_file(std::string template_str)
369 {
370 #ifdef _WIN32
371 if (template_str.rfind("/tmp/", 0) == 0) {
372 # ifdef __MINGW32__
373 char longpath[MAX_PATH + 1];
374 char shortpath[MAX_PATH + 1];
375 # else
376 WCHAR longpath[MAX_PATH + 1];
377 TCHAR shortpath[MAX_PATH + 1];
378 # endif
379 if (!GetTempPath(MAX_PATH+1, longpath))
380 log_error("GetTempPath() failed.\n");
381 if (!GetShortPathName(longpath, shortpath, MAX_PATH + 1))
382 log_error("GetShortPathName() failed.\n");
383 std::string path;
384 for (int i = 0; shortpath[i]; i++)
385 path += char(shortpath[i]);
386 template_str = stringf("%s\\%s", path.c_str(), template_str.c_str() + 5);
387 }
388
389 size_t pos = template_str.rfind("XXXXXX");
390 log_assert(pos != std::string::npos);
391
392 while (1) {
393 for (int i = 0; i < 6; i++) {
394 static std::string y = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
395 static uint32_t x = 314159265 ^ uint32_t(time(NULL));
396 x ^= x << 13, x ^= x >> 17, x ^= x << 5;
397 template_str[pos+i] = y[x % y.size()];
398 }
399 if (_access(template_str.c_str(), 0) != 0)
400 break;
401 }
402 #else
403 size_t pos = template_str.rfind("XXXXXX");
404 log_assert(pos != std::string::npos);
405
406 int suffixlen = GetSize(template_str) - pos - 6;
407
408 char *p = strdup(template_str.c_str());
409 close(mkstemps(p, suffixlen));
410 template_str = p;
411 free(p);
412 #endif
413
414 return template_str;
415 }
416
417 std::string make_temp_dir(std::string template_str)
418 {
419 #ifdef _WIN32
420 template_str = make_temp_file(template_str);
421 mkdir(template_str.c_str());
422 return template_str;
423 #else
424 # ifndef NDEBUG
425 size_t pos = template_str.rfind("XXXXXX");
426 log_assert(pos != std::string::npos);
427
428 int suffixlen = GetSize(template_str) - pos - 6;
429 log_assert(suffixlen == 0);
430 # endif
431
432 char *p = strdup(template_str.c_str());
433 p = mkdtemp(p);
434 log_assert(p != NULL);
435 template_str = p;
436 free(p);
437
438 return template_str;
439 #endif
440 }
441
442 #ifdef _WIN32
443 bool check_file_exists(std::string filename, bool)
444 {
445 return _access(filename.c_str(), 0) == 0;
446 }
447 #else
448 bool check_file_exists(std::string filename, bool is_exec)
449 {
450 return access(filename.c_str(), is_exec ? X_OK : F_OK) == 0;
451 }
452 #endif
453
454 bool is_absolute_path(std::string filename)
455 {
456 #ifdef _WIN32
457 return filename[0] == '/' || filename[0] == '\\' || (filename[0] != 0 && filename[1] == ':');
458 #else
459 return filename[0] == '/';
460 #endif
461 }
462
463 void remove_directory(std::string dirname)
464 {
465 #ifdef _WIN32
466 run_command(stringf("rmdir /s /q \"%s\"", dirname.c_str()));
467 #else
468 struct stat stbuf;
469 struct dirent **namelist;
470 int n = scandir(dirname.c_str(), &namelist, nullptr, alphasort);
471 log_assert(n >= 0);
472 for (int i = 0; i < n; i++) {
473 if (strcmp(namelist[i]->d_name, ".") && strcmp(namelist[i]->d_name, "..")) {
474 std::string buffer = stringf("%s/%s", dirname.c_str(), namelist[i]->d_name);
475 if (!stat(buffer.c_str(), &stbuf) && S_ISREG(stbuf.st_mode)) {
476 remove(buffer.c_str());
477 } else
478 remove_directory(buffer);
479 }
480 free(namelist[i]);
481 }
482 free(namelist);
483 rmdir(dirname.c_str());
484 #endif
485 }
486
487 std::string escape_filename_spaces(const std::string& filename)
488 {
489 std::string out;
490 out.reserve(filename.size());
491 for (auto c : filename)
492 {
493 if (c == ' ')
494 out += "\\ ";
495 else
496 out.push_back(c);
497 }
498 return out;
499 }
500
501 int GetSize(RTLIL::Wire *wire)
502 {
503 return wire->width;
504 }
505
506 bool already_setup = false;
507
508 void yosys_setup()
509 {
510 if(already_setup)
511 return;
512 already_setup = true;
513
514 RTLIL::ID::A = "\\A";
515 RTLIL::ID::B = "\\B";
516 RTLIL::ID::Y = "\\Y";
517 RTLIL::ID::keep = "\\keep";
518 RTLIL::ID::whitebox = "\\whitebox";
519 RTLIL::ID::blackbox = "\\blackbox";
520
521 #ifdef WITH_PYTHON
522 PyImport_AppendInittab((char*)"libyosys", INIT_MODULE);
523 Py_Initialize();
524 PyRun_SimpleString("import sys");
525 #endif
526
527 RTLIL::constpad["abc9.script.default"] = "&scorr; &sweep; &dc2; &dch -f; &ps; &if {C} {W} {D} {R} -v; &mfs";
528 RTLIL::constpad["abc9.script.default.area"] = "&scorr; &sweep; &dc2; &dch -f; &ps; &if {C} {W} {D} {R} -a -v; &mfs";
529 RTLIL::constpad["abc9.script.default.fast"] = "&if {C} {W} {D} {R}";
530 // Based on ABC's &flow
531 RTLIL::constpad["abc9.script.flow"] = "&scorr; &sweep;" \
532 /* Round 1 */ \
533 "&unmap; &if {C} {W} {D} {R}; &mfs;" \
534 "&st; &dsdb;" \
535 "&unmap; &if {C} {W} {D} {R}; &mfs;" \
536 "&st; &syn2 -m -R 10; &dsdb;" \
537 "&blut -a -K 6;" \
538 "&unmap; &if {C} {W} {D} {R}; &mfs;" \
539 /* Round 2 */ \
540 "&st; &sopb;" \
541 "&unmap; &if {C} {W} {D} {R}; &mfs;" \
542 "&st; &dsdb;" \
543 "&unmap; &if {C} {W} {D} {R}; &mfs;" \
544 "&st; &syn2 -m -R 10; &dsdb;" \
545 "&blut -a -K 6;" \
546 "&unmap; &if {C} {W} {D} {R} -v; &mfs";
547 // Based on ABC's &flow2
548 RTLIL::constpad["abc9.script.flow2"] = "&scorr; &sweep;" \
549 /* Comm1 */ "&synch2 -K 6 -C 500; &if -m {C} {W} {D} {R} -v; &mfs "/*"-W 4 -M 500 -C 7000"*/"; &save;"\
550 /* Comm2 */ "&dch -C 500; &if -m {C} {W} {D} {R} -v; &mfs "/*"-W 4 -M 500 -C 7000"*/"; &save;"\
551 "&load; &st; &sopb -R 10 -C 4; " \
552 /* Comm3 */ "&synch2 -K 6 -C 500; &if -m "/*"-E 5"*/" {C} {W} {D} {R} -v; &mfs "/*"-W 4 -M 500 -C 7000"*/"; &save;"\
553 /* Comm2 */ "&dch -C 500; &if -m {C} {W} {D} {R} -v; &mfs "/*"-W 4 -M 500 -C 7000"*/"; &save; "\
554 "&load";
555 // Based on ABC's &flow3
556 RTLIL::constpad["abc9.script.flow3"] = "&scorr; &sweep;" \
557 "&if {C} {W} {D}; &save; &st; &syn2; &if {C} {W} {D} {R} -v; &save; &load;"\
558 "&st; &if {C} -g -K 6; &dch -f; &if {C} {W} {D} {R} -v; &save; &load;"\
559 "&st; &if {C} -g -K 6; &synch2; &if {C} {W} {D} {R} -v; &save; &load;"\
560 "&mfs";
561
562 Pass::init_register();
563 yosys_design = new RTLIL::Design;
564 yosys_celltypes.setup();
565 log_push();
566 }
567
568 bool yosys_already_setup()
569 {
570 return already_setup;
571 }
572
573 bool already_shutdown = false;
574
575 void yosys_shutdown()
576 {
577 if(already_shutdown)
578 return;
579 already_shutdown = true;
580 log_pop();
581
582 delete yosys_design;
583 yosys_design = NULL;
584
585 for (auto f : log_files)
586 if (f != stderr)
587 fclose(f);
588 log_errfile = NULL;
589 log_files.clear();
590
591 Pass::done_register();
592 yosys_celltypes.clear();
593
594 #ifdef YOSYS_ENABLE_TCL
595 if (yosys_tcl_interp != NULL) {
596 Tcl_DeleteInterp(yosys_tcl_interp);
597 Tcl_Finalize();
598 yosys_tcl_interp = NULL;
599 }
600 #endif
601
602 #ifdef YOSYS_ENABLE_PLUGINS
603 for (auto &it : loaded_plugins)
604 dlclose(it.second);
605
606 loaded_plugins.clear();
607 #ifdef WITH_PYTHON
608 loaded_python_plugins.clear();
609 #endif
610 loaded_plugin_aliases.clear();
611 #endif
612
613 #ifdef WITH_PYTHON
614 Py_Finalize();
615 #endif
616 }
617
618 RTLIL::IdString new_id(std::string file, int line, std::string func)
619 {
620 #ifdef _WIN32
621 size_t pos = file.find_last_of("/\\");
622 #else
623 size_t pos = file.find_last_of('/');
624 #endif
625 if (pos != std::string::npos)
626 file = file.substr(pos+1);
627
628 pos = func.find_last_of(':');
629 if (pos != std::string::npos)
630 func = func.substr(pos+1);
631
632 return stringf("$auto$%s:%d:%s$%d", file.c_str(), line, func.c_str(), autoidx++);
633 }
634
635 RTLIL::Design *yosys_get_design()
636 {
637 return yosys_design;
638 }
639
640 const char *create_prompt(RTLIL::Design *design, int recursion_counter)
641 {
642 static char buffer[100];
643 std::string str = "\n";
644 if (recursion_counter > 1)
645 str += stringf("(%d) ", recursion_counter);
646 str += "yosys";
647 if (!design->selected_active_module.empty())
648 str += stringf(" [%s]", RTLIL::unescape_id(design->selected_active_module).c_str());
649 if (!design->selection_stack.empty() && !design->selection_stack.back().full_selection) {
650 if (design->selected_active_module.empty())
651 str += "*";
652 else if (design->selection_stack.back().selected_modules.size() != 1 || design->selection_stack.back().selected_members.size() != 0 ||
653 design->selection_stack.back().selected_modules.count(design->selected_active_module) == 0)
654 str += "*";
655 }
656 snprintf(buffer, 100, "%s> ", str.c_str());
657 return buffer;
658 }
659
660 std::vector<std::string> glob_filename(const std::string &filename_pattern)
661 {
662 std::vector<std::string> results;
663
664 #if defined(_WIN32) || !defined(YOSYS_ENABLE_GLOB)
665 results.push_back(filename_pattern);
666 #else
667 glob_t globbuf;
668
669 int err = glob(filename_pattern.c_str(), 0, NULL, &globbuf);
670
671 if(err == 0) {
672 for (size_t i = 0; i < globbuf.gl_pathc; i++)
673 results.push_back(globbuf.gl_pathv[i]);
674 globfree(&globbuf);
675 } else {
676 results.push_back(filename_pattern);
677 }
678 #endif
679
680 return results;
681 }
682
683 void rewrite_filename(std::string &filename)
684 {
685 if (filename.compare(0, 1, "\"") == 0 && filename.compare(GetSize(filename)-1, std::string::npos, "\"") == 0)
686 filename = filename.substr(1, GetSize(filename)-2);
687 if (filename.compare(0, 2, "+/") == 0)
688 filename = proc_share_dirname() + filename.substr(2);
689 #ifndef _WIN32
690 if (filename.compare(0, 2, "~/") == 0)
691 filename = filename.replace(0, 1, getenv("HOME"));
692 #endif
693 }
694
695 #ifdef YOSYS_ENABLE_TCL
696 static int tcl_yosys_cmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
697 {
698 std::vector<std::string> args;
699 for (int i = 1; i < argc; i++)
700 args.push_back(argv[i]);
701
702 if (args.size() >= 1 && args[0] == "-import") {
703 for (auto &it : pass_register) {
704 std::string tcl_command_name = it.first;
705 if (tcl_command_name == "proc")
706 tcl_command_name = "procs";
707 else if (tcl_command_name == "rename")
708 tcl_command_name = "renames";
709 Tcl_CmdInfo info;
710 if (Tcl_GetCommandInfo(interp, tcl_command_name.c_str(), &info) != 0) {
711 log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it.first.c_str());
712 } else {
713 std::string tcl_script = stringf("proc %s args { yosys %s {*}$args }", tcl_command_name.c_str(), it.first.c_str());
714 Tcl_Eval(interp, tcl_script.c_str());
715 }
716 }
717 return TCL_OK;
718 }
719
720 if (args.size() == 1) {
721 Pass::call(yosys_get_design(), args[0]);
722 return TCL_OK;
723 }
724
725 Pass::call(yosys_get_design(), args);
726 return TCL_OK;
727 }
728
729 extern Tcl_Interp *yosys_get_tcl_interp()
730 {
731 if (yosys_tcl_interp == NULL) {
732 yosys_tcl_interp = Tcl_CreateInterp();
733 Tcl_CreateCommand(yosys_tcl_interp, "yosys", tcl_yosys_cmd, NULL, NULL);
734 }
735 return yosys_tcl_interp;
736 }
737
738 struct TclPass : public Pass {
739 TclPass() : Pass("tcl", "execute a TCL script file") { }
740 void help() YS_OVERRIDE {
741 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
742 log("\n");
743 log(" tcl <filename> [args]\n");
744 log("\n");
745 log("This command executes the tcl commands in the specified file.\n");
746 log("Use 'yosys cmd' to run the yosys command 'cmd' from tcl.\n");
747 log("\n");
748 log("The tcl command 'yosys -import' can be used to import all yosys\n");
749 log("commands directly as tcl commands to the tcl shell. Yosys commands\n");
750 log("'proc' and 'rename' are wrapped to tcl commands 'procs' and 'renames'\n");
751 log("in order to avoid a name collision with the built in commands.\n");
752 log("\n");
753 log("If any arguments are specified, these arguments are provided to the script via\n");
754 log("the standard $argc and $argv variables.\n");
755 log("\n");
756 }
757 void execute(std::vector<std::string> args, RTLIL::Design *) YS_OVERRIDE {
758 if (args.size() < 2)
759 log_cmd_error("Missing script file.\n");
760
761 std::vector<Tcl_Obj*> script_args;
762 for (auto it = args.begin() + 2; it != args.end(); ++it)
763 script_args.push_back(Tcl_NewStringObj((*it).c_str(), (*it).size()));
764
765 Tcl_Interp *interp = yosys_get_tcl_interp();
766 Tcl_ObjSetVar2(interp, Tcl_NewStringObj("argc", 4), NULL, Tcl_NewIntObj(script_args.size()), 0);
767 Tcl_ObjSetVar2(interp, Tcl_NewStringObj("argv", 4), NULL, Tcl_NewListObj(script_args.size(), script_args.data()), 0);
768 Tcl_ObjSetVar2(interp, Tcl_NewStringObj("argv0", 5), NULL, Tcl_NewStringObj(args[1].c_str(), args[1].size()), 0);
769 if (Tcl_EvalFile(interp, args[1].c_str()) != TCL_OK)
770 log_cmd_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(interp));
771 }
772 } TclPass;
773 #endif
774
775 #if defined(__linux__) || defined(__CYGWIN__)
776 std::string proc_self_dirname()
777 {
778 char path[PATH_MAX];
779 ssize_t buflen = readlink("/proc/self/exe", path, sizeof(path));
780 if (buflen < 0) {
781 log_error("readlink(\"/proc/self/exe\") failed: %s\n", strerror(errno));
782 }
783 while (buflen > 0 && path[buflen-1] != '/')
784 buflen--;
785 return std::string(path, buflen);
786 }
787 #elif defined(__FreeBSD__)
788 std::string proc_self_dirname()
789 {
790 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
791 size_t buflen;
792 char *buffer;
793 std::string path;
794 if (sysctl(mib, 4, NULL, &buflen, NULL, 0) != 0)
795 log_error("sysctl failed: %s\n", strerror(errno));
796 buffer = (char*)malloc(buflen);
797 if (buffer == NULL)
798 log_error("malloc failed: %s\n", strerror(errno));
799 if (sysctl(mib, 4, buffer, &buflen, NULL, 0) != 0)
800 log_error("sysctl failed: %s\n", strerror(errno));
801 while (buflen > 0 && buffer[buflen-1] != '/')
802 buflen--;
803 path.assign(buffer, buflen);
804 free(buffer);
805 return path;
806 }
807 #elif defined(__APPLE__)
808 std::string proc_self_dirname()
809 {
810 char *path = NULL;
811 uint32_t buflen = 0;
812 while (_NSGetExecutablePath(path, &buflen) != 0)
813 path = (char *) realloc((void *) path, buflen);
814 while (buflen > 0 && path[buflen-1] != '/')
815 buflen--;
816 return std::string(path, buflen);
817 }
818 #elif defined(_WIN32)
819 std::string proc_self_dirname()
820 {
821 int i = 0;
822 # ifdef __MINGW32__
823 char longpath[MAX_PATH + 1];
824 char shortpath[MAX_PATH + 1];
825 # else
826 WCHAR longpath[MAX_PATH + 1];
827 TCHAR shortpath[MAX_PATH + 1];
828 # endif
829 if (!GetModuleFileName(0, longpath, MAX_PATH+1))
830 log_error("GetModuleFileName() failed.\n");
831 if (!GetShortPathName(longpath, shortpath, MAX_PATH+1))
832 log_error("GetShortPathName() failed.\n");
833 while (shortpath[i] != 0)
834 i++;
835 while (i > 0 && shortpath[i-1] != '/' && shortpath[i-1] != '\\')
836 shortpath[--i] = 0;
837 std::string path;
838 for (i = 0; shortpath[i]; i++)
839 path += char(shortpath[i]);
840 return path;
841 }
842 #elif defined(EMSCRIPTEN)
843 std::string proc_self_dirname()
844 {
845 return "/";
846 }
847 #else
848 #error "Don't know how to determine process executable base path!"
849 #endif
850
851 #ifdef EMSCRIPTEN
852 std::string proc_share_dirname()
853 {
854 return "/share/";
855 }
856 #else
857 std::string proc_share_dirname()
858 {
859 std::string proc_self_path = proc_self_dirname();
860 # if defined(_WIN32) && !defined(YOSYS_WIN32_UNIX_DIR)
861 std::string proc_share_path = proc_self_path + "share\\";
862 if (check_file_exists(proc_share_path, true))
863 return proc_share_path;
864 proc_share_path = proc_self_path + "..\\share\\";
865 if (check_file_exists(proc_share_path, true))
866 return proc_share_path;
867 # else
868 std::string proc_share_path = proc_self_path + "share/";
869 if (check_file_exists(proc_share_path, true))
870 return proc_share_path;
871 proc_share_path = proc_self_path + "../share/yosys/";
872 if (check_file_exists(proc_share_path, true))
873 return proc_share_path;
874 # ifdef YOSYS_DATDIR
875 proc_share_path = YOSYS_DATDIR "/";
876 if (check_file_exists(proc_share_path, true))
877 return proc_share_path;
878 # endif
879 # endif
880 log_error("proc_share_dirname: unable to determine share/ directory!\n");
881 }
882 #endif
883
884 bool fgetline(FILE *f, std::string &buffer)
885 {
886 buffer = "";
887 char block[4096];
888 while (1) {
889 if (fgets(block, 4096, f) == NULL)
890 return false;
891 buffer += block;
892 if (buffer.size() > 0 && (buffer[buffer.size()-1] == '\n' || buffer[buffer.size()-1] == '\r')) {
893 while (buffer.size() > 0 && (buffer[buffer.size()-1] == '\n' || buffer[buffer.size()-1] == '\r'))
894 buffer.resize(buffer.size()-1);
895 return true;
896 }
897 }
898 }
899
900 static void handle_label(std::string &command, bool &from_to_active, const std::string &run_from, const std::string &run_to)
901 {
902 int pos = 0;
903 std::string label;
904
905 while (pos < GetSize(command) && (command[pos] == ' ' || command[pos] == '\t'))
906 pos++;
907
908 if (pos < GetSize(command) && command[pos] == '#')
909 return;
910
911 while (pos < GetSize(command) && command[pos] != ' ' && command[pos] != '\t' && command[pos] != '\r' && command[pos] != '\n')
912 label += command[pos++];
913
914 if (GetSize(label) > 1 && label.back() == ':')
915 {
916 label = label.substr(0, GetSize(label)-1);
917 command = command.substr(pos);
918
919 if (label == run_from)
920 from_to_active = true;
921 else if (label == run_to || (run_from == run_to && !run_from.empty()))
922 from_to_active = false;
923 }
924 }
925
926 void run_frontend(std::string filename, std::string command, std::string *backend_command, std::string *from_to_label, RTLIL::Design *design)
927 {
928 if (design == nullptr)
929 design = yosys_design;
930
931 if (command == "auto") {
932 std::string filename_trim = filename;
933 if (filename_trim.size() > 3 && filename_trim.compare(filename_trim.size()-3, std::string::npos, ".gz") == 0)
934 filename_trim.erase(filename_trim.size()-3);
935 if (filename_trim.size() > 2 && filename_trim.compare(filename_trim.size()-2, std::string::npos, ".v") == 0)
936 command = "verilog";
937 else if (filename_trim.size() > 2 && filename_trim.compare(filename_trim.size()-3, std::string::npos, ".sv") == 0)
938 command = "verilog -sv";
939 else if (filename_trim.size() > 3 && filename_trim.compare(filename_trim.size()-4, std::string::npos, ".vhd") == 0)
940 command = "vhdl";
941 else if (filename_trim.size() > 4 && filename_trim.compare(filename_trim.size()-5, std::string::npos, ".blif") == 0)
942 command = "blif";
943 else if (filename_trim.size() > 5 && filename_trim.compare(filename_trim.size()-6, std::string::npos, ".eblif") == 0)
944 command = "blif";
945 else if (filename_trim.size() > 4 && filename_trim.compare(filename_trim.size()-5, std::string::npos, ".json") == 0)
946 command = "json";
947 else if (filename_trim.size() > 3 && filename_trim.compare(filename_trim.size()-3, std::string::npos, ".il") == 0)
948 command = "ilang";
949 else if (filename_trim.size() > 3 && filename_trim.compare(filename_trim.size()-3, std::string::npos, ".ys") == 0)
950 command = "script";
951 else if (filename_trim.size() > 3 && filename_trim.compare(filename_trim.size()-4, std::string::npos, ".tcl") == 0)
952 command = "tcl";
953 else if (filename == "-")
954 command = "script";
955 else
956 log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename.c_str());
957 }
958
959 if (command == "script")
960 {
961 std::string run_from, run_to;
962 bool from_to_active = true;
963
964 if (from_to_label != NULL) {
965 size_t pos = from_to_label->find(':');
966 if (pos == std::string::npos) {
967 run_from = *from_to_label;
968 run_to = *from_to_label;
969 } else {
970 run_from = from_to_label->substr(0, pos);
971 run_to = from_to_label->substr(pos+1);
972 }
973 from_to_active = run_from.empty();
974 }
975
976 log("\n-- Executing script file `%s' --\n", filename.c_str());
977
978 FILE *f = stdin;
979
980 if (filename != "-") {
981 f = fopen(filename.c_str(), "r");
982 yosys_input_files.insert(filename);
983 }
984
985 if (f == NULL)
986 log_error("Can't open script file `%s' for reading: %s\n", filename.c_str(), strerror(errno));
987
988 FILE *backup_script_file = Frontend::current_script_file;
989 Frontend::current_script_file = f;
990
991 try {
992 std::string command;
993 while (fgetline(f, command)) {
994 while (!command.empty() && command[command.size()-1] == '\\') {
995 std::string next_line;
996 if (!fgetline(f, next_line))
997 break;
998 command.resize(command.size()-1);
999 command += next_line;
1000 }
1001 handle_label(command, from_to_active, run_from, run_to);
1002 if (from_to_active) {
1003 Pass::call(design, command);
1004 design->check();
1005 }
1006 }
1007
1008 if (!command.empty()) {
1009 handle_label(command, from_to_active, run_from, run_to);
1010 if (from_to_active) {
1011 Pass::call(design, command);
1012 design->check();
1013 }
1014 }
1015 }
1016 catch (...) {
1017 Frontend::current_script_file = backup_script_file;
1018 throw;
1019 }
1020
1021 Frontend::current_script_file = backup_script_file;
1022
1023 if (filename != "-")
1024 fclose(f);
1025
1026 if (backend_command != NULL && *backend_command == "auto")
1027 *backend_command = "";
1028
1029 return;
1030 }
1031
1032 if (filename == "-") {
1033 log("\n-- Parsing stdin using frontend `%s' --\n", command.c_str());
1034 } else {
1035 log("\n-- Parsing `%s' using frontend `%s' --\n", filename.c_str(), command.c_str());
1036 }
1037
1038 if (command == "tcl")
1039 Pass::call(design, vector<string>({command, filename}));
1040 else
1041 Frontend::frontend_call(design, NULL, filename, command);
1042 design->check();
1043 }
1044
1045 void run_frontend(std::string filename, std::string command, RTLIL::Design *design)
1046 {
1047 run_frontend(filename, command, nullptr, nullptr, design);
1048 }
1049
1050 void run_pass(std::string command, RTLIL::Design *design)
1051 {
1052 if (design == nullptr)
1053 design = yosys_design;
1054
1055 log("\n-- Running command `%s' --\n", command.c_str());
1056
1057 Pass::call(design, command);
1058 }
1059
1060 void run_backend(std::string filename, std::string command, RTLIL::Design *design)
1061 {
1062 if (design == nullptr)
1063 design = yosys_design;
1064
1065 if (command == "auto") {
1066 if (filename.size() > 2 && filename.compare(filename.size()-2, std::string::npos, ".v") == 0)
1067 command = "verilog";
1068 else if (filename.size() > 3 && filename.compare(filename.size()-3, std::string::npos, ".il") == 0)
1069 command = "ilang";
1070 else if (filename.size() > 4 && filename.compare(filename.size()-4, std::string::npos, ".aig") == 0)
1071 command = "aiger";
1072 else if (filename.size() > 5 && filename.compare(filename.size()-5, std::string::npos, ".blif") == 0)
1073 command = "blif";
1074 else if (filename.size() > 5 && filename.compare(filename.size()-5, std::string::npos, ".edif") == 0)
1075 command = "edif";
1076 else if (filename.size() > 5 && filename.compare(filename.size()-5, std::string::npos, ".json") == 0)
1077 command = "json";
1078 else if (filename == "-")
1079 command = "ilang";
1080 else if (filename.empty())
1081 return;
1082 else
1083 log_error("Can't guess backend for output file `%s' (missing -b option)!\n", filename.c_str());
1084 }
1085
1086 if (filename.empty())
1087 filename = "-";
1088
1089 if (filename == "-") {
1090 log("\n-- Writing to stdout using backend `%s' --\n", command.c_str());
1091 } else {
1092 log("\n-- Writing to `%s' using backend `%s' --\n", filename.c_str(), command.c_str());
1093 }
1094
1095 Backend::backend_call(design, NULL, filename, command);
1096 }
1097
1098 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1099 static char *readline_cmd_generator(const char *text, int state)
1100 {
1101 static std::map<std::string, Pass*>::iterator it;
1102 static int len;
1103
1104 if (!state) {
1105 it = pass_register.begin();
1106 len = strlen(text);
1107 }
1108
1109 for (; it != pass_register.end(); it++) {
1110 if (it->first.compare(0, len, text) == 0)
1111 return strdup((it++)->first.c_str());
1112 }
1113 return NULL;
1114 }
1115
1116 static char *readline_obj_generator(const char *text, int state)
1117 {
1118 static std::vector<char*> obj_names;
1119 static size_t idx;
1120
1121 if (!state)
1122 {
1123 idx = 0;
1124 obj_names.clear();
1125
1126 RTLIL::Design *design = yosys_get_design();
1127 int len = strlen(text);
1128
1129 if (design->selected_active_module.empty())
1130 {
1131 for (auto &it : design->modules_)
1132 if (RTLIL::unescape_id(it.first).compare(0, len, text) == 0)
1133 obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
1134 }
1135 else
1136 if (design->modules_.count(design->selected_active_module) > 0)
1137 {
1138 RTLIL::Module *module = design->modules_.at(design->selected_active_module);
1139
1140 for (auto &it : module->wires_)
1141 if (RTLIL::unescape_id(it.first).compare(0, len, text) == 0)
1142 obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
1143
1144 for (auto &it : module->memories)
1145 if (RTLIL::unescape_id(it.first).compare(0, len, text) == 0)
1146 obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
1147
1148 for (auto &it : module->cells_)
1149 if (RTLIL::unescape_id(it.first).compare(0, len, text) == 0)
1150 obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
1151
1152 for (auto &it : module->processes)
1153 if (RTLIL::unescape_id(it.first).compare(0, len, text) == 0)
1154 obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
1155 }
1156
1157 std::sort(obj_names.begin(), obj_names.end());
1158 }
1159
1160 if (idx < obj_names.size())
1161 return strdup(obj_names[idx++]);
1162
1163 idx = 0;
1164 obj_names.clear();
1165 return NULL;
1166 }
1167
1168 static char **readline_completion(const char *text, int start, int)
1169 {
1170 if (start == 0)
1171 return rl_completion_matches(text, readline_cmd_generator);
1172 if (strncmp(rl_line_buffer, "read_", 5) && strncmp(rl_line_buffer, "write_", 6))
1173 return rl_completion_matches(text, readline_obj_generator);
1174 return NULL;
1175 }
1176 #endif
1177
1178 void shell(RTLIL::Design *design)
1179 {
1180 static int recursion_counter = 0;
1181
1182 recursion_counter++;
1183 log_cmd_error_throw = true;
1184
1185 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1186 rl_readline_name = (char*)"yosys";
1187 rl_attempted_completion_function = readline_completion;
1188 rl_basic_word_break_characters = (char*)" \t\n";
1189 #endif
1190
1191 char *command = NULL;
1192 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1193 while ((command = readline(create_prompt(design, recursion_counter))) != NULL)
1194 {
1195 #else
1196 char command_buffer[4096];
1197 while (1)
1198 {
1199 fputs(create_prompt(design, recursion_counter), stdout);
1200 fflush(stdout);
1201 if ((command = fgets(command_buffer, 4096, stdin)) == NULL)
1202 break;
1203 #endif
1204 if (command[strspn(command, " \t\r\n")] == 0)
1205 continue;
1206 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1207 add_history(command);
1208 #endif
1209
1210 char *p = command + strspn(command, " \t\r\n");
1211 if (!strncmp(p, "exit", 4)) {
1212 p += 4;
1213 p += strspn(p, " \t\r\n");
1214 if (*p == 0)
1215 break;
1216 }
1217
1218 try {
1219 log_assert(design->selection_stack.size() == 1);
1220 Pass::call(design, command);
1221 } catch (log_cmd_error_exception) {
1222 while (design->selection_stack.size() > 1)
1223 design->selection_stack.pop_back();
1224 log_reset_stack();
1225 }
1226 design->check();
1227 }
1228 if (command == NULL)
1229 printf("exit\n");
1230
1231 recursion_counter--;
1232 log_cmd_error_throw = false;
1233 }
1234
1235 struct ShellPass : public Pass {
1236 ShellPass() : Pass("shell", "enter interactive command mode") { }
1237 void help() YS_OVERRIDE {
1238 log("\n");
1239 log(" shell\n");
1240 log("\n");
1241 log("This command enters the interactive command mode. This can be useful\n");
1242 log("in a script to interrupt the script at a certain point and allow for\n");
1243 log("interactive inspection or manual synthesis of the design at this point.\n");
1244 log("\n");
1245 log("The command prompt of the interactive shell indicates the current\n");
1246 log("selection (see 'help select'):\n");
1247 log("\n");
1248 log(" yosys>\n");
1249 log(" the entire design is selected\n");
1250 log("\n");
1251 log(" yosys*>\n");
1252 log(" only part of the design is selected\n");
1253 log("\n");
1254 log(" yosys [modname]>\n");
1255 log(" the entire module 'modname' is selected using 'select -module modname'\n");
1256 log("\n");
1257 log(" yosys [modname]*>\n");
1258 log(" only part of current module 'modname' is selected\n");
1259 log("\n");
1260 log("When in interactive shell, some errors (e.g. invalid command arguments)\n");
1261 log("do not terminate yosys but return to the command prompt.\n");
1262 log("\n");
1263 log("This command is the default action if nothing else has been specified\n");
1264 log("on the command line.\n");
1265 log("\n");
1266 log("Press Ctrl-D or type 'exit' to leave the interactive shell.\n");
1267 log("\n");
1268 }
1269 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE {
1270 extra_args(args, 1, design, false);
1271 shell(design);
1272 }
1273 } ShellPass;
1274
1275 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1276 struct HistoryPass : public Pass {
1277 HistoryPass() : Pass("history", "show last interactive commands") { }
1278 void help() YS_OVERRIDE {
1279 log("\n");
1280 log(" history\n");
1281 log("\n");
1282 log("This command prints all commands in the shell history buffer. This are\n");
1283 log("all commands executed in an interactive session, but not the commands\n");
1284 log("from executed scripts.\n");
1285 log("\n");
1286 }
1287 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE {
1288 extra_args(args, 1, design, false);
1289 #ifdef YOSYS_ENABLE_READLINE
1290 for(HIST_ENTRY **list = history_list(); *list != NULL; list++)
1291 log("%s\n", (*list)->line);
1292 #else
1293 for (int i = where_history(); history_get(i); i++)
1294 log("%s\n", history_get(i)->line);
1295 #endif
1296 }
1297 } HistoryPass;
1298 #endif
1299
1300 struct ScriptCmdPass : public Pass {
1301 ScriptCmdPass() : Pass("script", "execute commands from file or wire") { }
1302 void help() YS_OVERRIDE {
1303 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
1304 log("\n");
1305 log(" script <filename> [<from_label>:<to_label>]\n");
1306 log(" script -scriptwire [selection]\n");
1307 log("\n");
1308 log("This command executes the yosys commands in the specified file (default\n");
1309 log("behaviour), or commands embedded in the constant text value connected to the\n");
1310 log("selected wires.\n");
1311 log("\n");
1312 log("In the default (file) case, the 2nd argument can be used to only execute the\n");
1313 log("section of the file between the specified labels. An empty from label is\n");
1314 log("synonymous with the beginning of the file and an empty to label is synonymous\n");
1315 log("with the end of the file.\n");
1316 log("\n");
1317 log("If only one label is specified (without ':') then only the block\n");
1318 log("marked with that label (until the next label) is executed.\n");
1319 log("\n");
1320 log("In \"-scriptwire\" mode, the commands on the selected wire(s) will be executed\n");
1321 log("in the scope of (and thus, relative to) the wires' owning module(s). This\n");
1322 log("'-module' mode can be exited by using the 'cd' command.\n");
1323 log("\n");
1324 }
1325 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE
1326 {
1327 bool scriptwire = false;
1328
1329 size_t argidx;
1330 for (argidx = 1; argidx < args.size(); argidx++) {
1331 if (args[argidx] == "-scriptwire") {
1332 scriptwire = true;
1333 continue;
1334 }
1335 break;
1336 }
1337 if (scriptwire) {
1338 extra_args(args, argidx, design);
1339
1340 for (auto mod : design->selected_modules())
1341 for (auto &c : mod->connections()) {
1342 if (!c.first.is_wire())
1343 continue;
1344 auto w = c.first.as_wire();
1345 if (!mod->selected(w))
1346 continue;
1347 if (!c.second.is_fully_const())
1348 log_error("RHS of selected wire %s.%s is not constant.\n", log_id(mod), log_id(w));
1349 auto v = c.second.as_const();
1350 Pass::call_on_module(design, mod, v.decode_string());
1351 }
1352 }
1353 else if (args.size() < 2)
1354 log_cmd_error("Missing script file.\n");
1355 else if (args.size() == 2)
1356 run_frontend(args[1], "script", design);
1357 else if (args.size() == 3)
1358 run_frontend(args[1], "script", NULL, &args[2], design);
1359 else
1360 extra_args(args, 2, design, false);
1361 }
1362 } ScriptCmdPass;
1363
1364 YOSYS_NAMESPACE_END