Merge pull request #998 from mdaiter/get_bool_attribute_opts
[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 - 2018 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 // if there are already IdString objects then we have a global initialization order bug
514 IdString empty_id;
515 log_assert(empty_id.index_ == 0);
516 IdString::get_reference(empty_id.index_);
517
518 #ifdef WITH_PYTHON
519 PyImport_AppendInittab((char*)"libyosys", INIT_MODULE);
520 Py_Initialize();
521 PyRun_SimpleString("import sys");
522 #endif
523
524 Pass::init_register();
525 yosys_design = new RTLIL::Design;
526 yosys_celltypes.setup();
527 log_push();
528 }
529
530 bool yosys_already_setup()
531 {
532 return already_setup;
533 }
534
535 bool already_shutdown = false;
536
537 void yosys_shutdown()
538 {
539 if(already_shutdown)
540 return;
541 already_shutdown = true;
542 log_pop();
543
544 delete yosys_design;
545 yosys_design = NULL;
546
547 for (auto f : log_files)
548 if (f != stderr)
549 fclose(f);
550 log_errfile = NULL;
551 log_files.clear();
552
553 Pass::done_register();
554 yosys_celltypes.clear();
555
556 #ifdef YOSYS_ENABLE_TCL
557 if (yosys_tcl_interp != NULL) {
558 Tcl_DeleteInterp(yosys_tcl_interp);
559 Tcl_Finalize();
560 yosys_tcl_interp = NULL;
561 }
562 #endif
563
564 #ifdef YOSYS_ENABLE_PLUGINS
565 for (auto &it : loaded_plugins)
566 dlclose(it.second);
567
568 loaded_plugins.clear();
569 #ifdef WITH_PYTHON
570 loaded_python_plugins.clear();
571 #endif
572 loaded_plugin_aliases.clear();
573 #endif
574
575 #ifdef WITH_PYTHON
576 Py_Finalize();
577 #endif
578
579 IdString empty_id;
580 IdString::put_reference(empty_id.index_);
581 }
582
583 RTLIL::IdString new_id(std::string file, int line, std::string func)
584 {
585 #ifdef _WIN32
586 size_t pos = file.find_last_of("/\\");
587 #else
588 size_t pos = file.find_last_of('/');
589 #endif
590 if (pos != std::string::npos)
591 file = file.substr(pos+1);
592
593 pos = func.find_last_of(':');
594 if (pos != std::string::npos)
595 func = func.substr(pos+1);
596
597 return stringf("$auto$%s:%d:%s$%d", file.c_str(), line, func.c_str(), autoidx++);
598 }
599
600 RTLIL::Design *yosys_get_design()
601 {
602 return yosys_design;
603 }
604
605 const char *create_prompt(RTLIL::Design *design, int recursion_counter)
606 {
607 static char buffer[100];
608 std::string str = "\n";
609 if (recursion_counter > 1)
610 str += stringf("(%d) ", recursion_counter);
611 str += "yosys";
612 if (!design->selected_active_module.empty())
613 str += stringf(" [%s]", RTLIL::unescape_id(design->selected_active_module).c_str());
614 if (!design->selection_stack.empty() && !design->selection_stack.back().full_selection) {
615 if (design->selected_active_module.empty())
616 str += "*";
617 else if (design->selection_stack.back().selected_modules.size() != 1 || design->selection_stack.back().selected_members.size() != 0 ||
618 design->selection_stack.back().selected_modules.count(design->selected_active_module) == 0)
619 str += "*";
620 }
621 snprintf(buffer, 100, "%s> ", str.c_str());
622 return buffer;
623 }
624
625 std::vector<std::string> glob_filename(const std::string &filename_pattern)
626 {
627 std::vector<std::string> results;
628
629 #if defined(_WIN32) || !defined(YOSYS_ENABLE_GLOB)
630 results.push_back(filename_pattern);
631 #else
632 glob_t globbuf;
633
634 int err = glob(filename_pattern.c_str(), 0, NULL, &globbuf);
635
636 if(err == 0) {
637 for (size_t i = 0; i < globbuf.gl_pathc; i++)
638 results.push_back(globbuf.gl_pathv[i]);
639 globfree(&globbuf);
640 } else {
641 results.push_back(filename_pattern);
642 }
643 #endif
644
645 return results;
646 }
647
648 void rewrite_filename(std::string &filename)
649 {
650 if (filename.substr(0, 1) == "\"" && filename.substr(GetSize(filename)-1) == "\"")
651 filename = filename.substr(1, GetSize(filename)-2);
652 if (filename.substr(0, 2) == "+/")
653 filename = proc_share_dirname() + filename.substr(2);
654 }
655
656 #ifdef YOSYS_ENABLE_TCL
657 static int tcl_yosys_cmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
658 {
659 std::vector<std::string> args;
660 for (int i = 1; i < argc; i++)
661 args.push_back(argv[i]);
662
663 if (args.size() >= 1 && args[0] == "-import") {
664 for (auto &it : pass_register) {
665 std::string tcl_command_name = it.first;
666 if (tcl_command_name == "proc")
667 tcl_command_name = "procs";
668 else if (tcl_command_name == "rename")
669 tcl_command_name = "renames";
670 Tcl_CmdInfo info;
671 if (Tcl_GetCommandInfo(interp, tcl_command_name.c_str(), &info) != 0) {
672 log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it.first.c_str());
673 } else {
674 std::string tcl_script = stringf("proc %s args { yosys %s {*}$args }", tcl_command_name.c_str(), it.first.c_str());
675 Tcl_Eval(interp, tcl_script.c_str());
676 }
677 }
678 return TCL_OK;
679 }
680
681 if (args.size() == 1) {
682 Pass::call(yosys_get_design(), args[0]);
683 return TCL_OK;
684 }
685
686 Pass::call(yosys_get_design(), args);
687 return TCL_OK;
688 }
689
690 extern Tcl_Interp *yosys_get_tcl_interp()
691 {
692 if (yosys_tcl_interp == NULL) {
693 yosys_tcl_interp = Tcl_CreateInterp();
694 Tcl_CreateCommand(yosys_tcl_interp, "yosys", tcl_yosys_cmd, NULL, NULL);
695 }
696 return yosys_tcl_interp;
697 }
698
699 struct TclPass : public Pass {
700 TclPass() : Pass("tcl", "execute a TCL script file") { }
701 void help() YS_OVERRIDE {
702 // |---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|---v---|
703 log("\n");
704 log(" tcl <filename> [args]\n");
705 log("\n");
706 log("This command executes the tcl commands in the specified file.\n");
707 log("Use 'yosys cmd' to run the yosys command 'cmd' from tcl.\n");
708 log("\n");
709 log("The tcl command 'yosys -import' can be used to import all yosys\n");
710 log("commands directly as tcl commands to the tcl shell. Yosys commands\n");
711 log("'proc' and 'rename' are wrapped to tcl commands 'procs' and 'renames'\n");
712 log("in order to avoid a name collision with the built in commands.\n");
713 log("\n");
714 log("If any arguments are specified, these arguments are provided to the script via\n");
715 log("the standard $argc and $argv variables.\n");
716 log("\n");
717 }
718 void execute(std::vector<std::string> args, RTLIL::Design *) YS_OVERRIDE {
719 if (args.size() < 2)
720 log_cmd_error("Missing script file.\n");
721
722 std::vector<Tcl_Obj*> script_args;
723 for (auto it = args.begin() + 2; it != args.end(); ++it)
724 script_args.push_back(Tcl_NewStringObj((*it).c_str(), (*it).size()));
725
726 Tcl_Interp *interp = yosys_get_tcl_interp();
727 Tcl_ObjSetVar2(interp, Tcl_NewStringObj("argc", 4), NULL, Tcl_NewIntObj(script_args.size()), 0);
728 Tcl_ObjSetVar2(interp, Tcl_NewStringObj("argv", 4), NULL, Tcl_NewListObj(script_args.size(), script_args.data()), 0);
729 Tcl_ObjSetVar2(interp, Tcl_NewStringObj("argv0", 5), NULL, Tcl_NewStringObj(args[1].c_str(), args[1].size()), 0);
730 if (Tcl_EvalFile(interp, args[1].c_str()) != TCL_OK)
731 log_cmd_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(interp));
732 }
733 } TclPass;
734 #endif
735
736 #if defined(__linux__) || defined(__CYGWIN__)
737 std::string proc_self_dirname()
738 {
739 char path[PATH_MAX];
740 ssize_t buflen = readlink("/proc/self/exe", path, sizeof(path));
741 if (buflen < 0) {
742 log_error("readlink(\"/proc/self/exe\") failed: %s\n", strerror(errno));
743 }
744 while (buflen > 0 && path[buflen-1] != '/')
745 buflen--;
746 return std::string(path, buflen);
747 }
748 #elif defined(__FreeBSD__)
749 std::string proc_self_dirname()
750 {
751 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
752 size_t buflen;
753 char *buffer;
754 std::string path;
755 if (sysctl(mib, 4, NULL, &buflen, NULL, 0) != 0)
756 log_error("sysctl failed: %s\n", strerror(errno));
757 buffer = (char*)malloc(buflen);
758 if (buffer == NULL)
759 log_error("malloc failed: %s\n", strerror(errno));
760 if (sysctl(mib, 4, buffer, &buflen, NULL, 0) != 0)
761 log_error("sysctl failed: %s\n", strerror(errno));
762 while (buflen > 0 && buffer[buflen-1] != '/')
763 buflen--;
764 path.assign(buffer, buflen);
765 free(buffer);
766 return path;
767 }
768 #elif defined(__APPLE__)
769 std::string proc_self_dirname()
770 {
771 char *path = NULL;
772 uint32_t buflen = 0;
773 while (_NSGetExecutablePath(path, &buflen) != 0)
774 path = (char *) realloc((void *) path, buflen);
775 while (buflen > 0 && path[buflen-1] != '/')
776 buflen--;
777 return std::string(path, buflen);
778 }
779 #elif defined(_WIN32)
780 std::string proc_self_dirname()
781 {
782 int i = 0;
783 # ifdef __MINGW32__
784 char longpath[MAX_PATH + 1];
785 char shortpath[MAX_PATH + 1];
786 # else
787 WCHAR longpath[MAX_PATH + 1];
788 TCHAR shortpath[MAX_PATH + 1];
789 # endif
790 if (!GetModuleFileName(0, longpath, MAX_PATH+1))
791 log_error("GetModuleFileName() failed.\n");
792 if (!GetShortPathName(longpath, shortpath, MAX_PATH+1))
793 log_error("GetShortPathName() failed.\n");
794 while (shortpath[i] != 0)
795 i++;
796 while (i > 0 && shortpath[i-1] != '/' && shortpath[i-1] != '\\')
797 shortpath[--i] = 0;
798 std::string path;
799 for (i = 0; shortpath[i]; i++)
800 path += char(shortpath[i]);
801 return path;
802 }
803 #elif defined(EMSCRIPTEN)
804 std::string proc_self_dirname()
805 {
806 return "/";
807 }
808 #else
809 #error "Don't know how to determine process executable base path!"
810 #endif
811
812 #ifdef EMSCRIPTEN
813 std::string proc_share_dirname()
814 {
815 return "/share/";
816 }
817 #else
818 std::string proc_share_dirname()
819 {
820 std::string proc_self_path = proc_self_dirname();
821 # if defined(_WIN32) && !defined(YOSYS_WIN32_UNIX_DIR)
822 std::string proc_share_path = proc_self_path + "share\\";
823 if (check_file_exists(proc_share_path, true))
824 return proc_share_path;
825 proc_share_path = proc_self_path + "..\\share\\";
826 if (check_file_exists(proc_share_path, true))
827 return proc_share_path;
828 # else
829 std::string proc_share_path = proc_self_path + "share/";
830 if (check_file_exists(proc_share_path, true))
831 return proc_share_path;
832 proc_share_path = proc_self_path + "../share/yosys/";
833 if (check_file_exists(proc_share_path, true))
834 return proc_share_path;
835 # ifdef YOSYS_DATDIR
836 proc_share_path = YOSYS_DATDIR "/";
837 if (check_file_exists(proc_share_path, true))
838 return proc_share_path;
839 # endif
840 # endif
841 log_error("proc_share_dirname: unable to determine share/ directory!\n");
842 }
843 #endif
844
845 bool fgetline(FILE *f, std::string &buffer)
846 {
847 buffer = "";
848 char block[4096];
849 while (1) {
850 if (fgets(block, 4096, f) == NULL)
851 return false;
852 buffer += block;
853 if (buffer.size() > 0 && (buffer[buffer.size()-1] == '\n' || buffer[buffer.size()-1] == '\r')) {
854 while (buffer.size() > 0 && (buffer[buffer.size()-1] == '\n' || buffer[buffer.size()-1] == '\r'))
855 buffer.resize(buffer.size()-1);
856 return true;
857 }
858 }
859 }
860
861 static void handle_label(std::string &command, bool &from_to_active, const std::string &run_from, const std::string &run_to)
862 {
863 int pos = 0;
864 std::string label;
865
866 while (pos < GetSize(command) && (command[pos] == ' ' || command[pos] == '\t'))
867 pos++;
868
869 if (pos < GetSize(command) && command[pos] == '#')
870 return;
871
872 while (pos < GetSize(command) && command[pos] != ' ' && command[pos] != '\t' && command[pos] != '\r' && command[pos] != '\n')
873 label += command[pos++];
874
875 if (GetSize(label) > 1 && label.back() == ':')
876 {
877 label = label.substr(0, GetSize(label)-1);
878 command = command.substr(pos);
879
880 if (label == run_from)
881 from_to_active = true;
882 else if (label == run_to || (run_from == run_to && !run_from.empty()))
883 from_to_active = false;
884 }
885 }
886
887 void run_frontend(std::string filename, std::string command, std::string *backend_command, std::string *from_to_label, RTLIL::Design *design)
888 {
889 if (design == nullptr)
890 design = yosys_design;
891
892 if (command == "auto") {
893 if (filename.size() > 2 && filename.substr(filename.size()-2) == ".v")
894 command = "verilog";
895 else if (filename.size() > 2 && filename.substr(filename.size()-3) == ".sv")
896 command = "verilog -sv";
897 else if (filename.size() > 3 && filename.substr(filename.size()-4) == ".vhd")
898 command = "vhdl";
899 else if (filename.size() > 4 && filename.substr(filename.size()-5) == ".blif")
900 command = "blif";
901 else if (filename.size() > 5 && filename.substr(filename.size()-6) == ".eblif")
902 command = "blif";
903 else if (filename.size() > 4 && filename.substr(filename.size()-5) == ".json")
904 command = "json";
905 else if (filename.size() > 3 && filename.substr(filename.size()-3) == ".il")
906 command = "ilang";
907 else if (filename.size() > 3 && filename.substr(filename.size()-3) == ".ys")
908 command = "script";
909 else if (filename.size() > 3 && filename.substr(filename.size()-4) == ".tcl")
910 command = "tcl";
911 else if (filename == "-")
912 command = "script";
913 else
914 log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename.c_str());
915 }
916
917 if (command == "script")
918 {
919 std::string run_from, run_to;
920 bool from_to_active = true;
921
922 if (from_to_label != NULL) {
923 size_t pos = from_to_label->find(':');
924 if (pos == std::string::npos) {
925 run_from = *from_to_label;
926 run_to = *from_to_label;
927 } else {
928 run_from = from_to_label->substr(0, pos);
929 run_to = from_to_label->substr(pos+1);
930 }
931 from_to_active = run_from.empty();
932 }
933
934 log("\n-- Executing script file `%s' --\n", filename.c_str());
935
936 FILE *f = stdin;
937
938 if (filename != "-") {
939 f = fopen(filename.c_str(), "r");
940 yosys_input_files.insert(filename);
941 }
942
943 if (f == NULL)
944 log_error("Can't open script file `%s' for reading: %s\n", filename.c_str(), strerror(errno));
945
946 FILE *backup_script_file = Frontend::current_script_file;
947 Frontend::current_script_file = f;
948
949 try {
950 std::string command;
951 while (fgetline(f, command)) {
952 while (!command.empty() && command[command.size()-1] == '\\') {
953 std::string next_line;
954 if (!fgetline(f, next_line))
955 break;
956 command.resize(command.size()-1);
957 command += next_line;
958 }
959 handle_label(command, from_to_active, run_from, run_to);
960 if (from_to_active)
961 Pass::call(design, command);
962 }
963
964 if (!command.empty()) {
965 handle_label(command, from_to_active, run_from, run_to);
966 if (from_to_active)
967 Pass::call(design, command);
968 }
969 }
970 catch (...) {
971 Frontend::current_script_file = backup_script_file;
972 throw;
973 }
974
975 Frontend::current_script_file = backup_script_file;
976
977 if (filename != "-")
978 fclose(f);
979
980 if (backend_command != NULL && *backend_command == "auto")
981 *backend_command = "";
982
983 return;
984 }
985
986 if (filename == "-") {
987 log("\n-- Parsing stdin using frontend `%s' --\n", command.c_str());
988 } else {
989 log("\n-- Parsing `%s' using frontend `%s' --\n", filename.c_str(), command.c_str());
990 }
991
992 if (command == "tcl")
993 Pass::call(design, vector<string>({command, filename}));
994 else
995 Frontend::frontend_call(design, NULL, filename, command);
996 }
997
998 void run_frontend(std::string filename, std::string command, RTLIL::Design *design)
999 {
1000 run_frontend(filename, command, nullptr, nullptr, design);
1001 }
1002
1003 void run_pass(std::string command, RTLIL::Design *design)
1004 {
1005 if (design == nullptr)
1006 design = yosys_design;
1007
1008 log("\n-- Running command `%s' --\n", command.c_str());
1009
1010 Pass::call(design, command);
1011 }
1012
1013 void run_backend(std::string filename, std::string command, RTLIL::Design *design)
1014 {
1015 if (design == nullptr)
1016 design = yosys_design;
1017
1018 if (command == "auto") {
1019 if (filename.size() > 2 && filename.substr(filename.size()-2) == ".v")
1020 command = "verilog";
1021 else if (filename.size() > 3 && filename.substr(filename.size()-3) == ".il")
1022 command = "ilang";
1023 else if (filename.size() > 4 && filename.substr(filename.size()-4) == ".aig")
1024 command = "aiger";
1025 else if (filename.size() > 5 && filename.substr(filename.size()-5) == ".blif")
1026 command = "blif";
1027 else if (filename.size() > 5 && filename.substr(filename.size()-5) == ".edif")
1028 command = "edif";
1029 else if (filename.size() > 5 && filename.substr(filename.size()-5) == ".json")
1030 command = "json";
1031 else if (filename == "-")
1032 command = "ilang";
1033 else if (filename.empty())
1034 return;
1035 else
1036 log_error("Can't guess backend for output file `%s' (missing -b option)!\n", filename.c_str());
1037 }
1038
1039 if (filename.empty())
1040 filename = "-";
1041
1042 if (filename == "-") {
1043 log("\n-- Writing to stdout using backend `%s' --\n", command.c_str());
1044 } else {
1045 log("\n-- Writing to `%s' using backend `%s' --\n", filename.c_str(), command.c_str());
1046 }
1047
1048 Backend::backend_call(design, NULL, filename, command);
1049 }
1050
1051 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1052 static char *readline_cmd_generator(const char *text, int state)
1053 {
1054 static std::map<std::string, Pass*>::iterator it;
1055 static int len;
1056
1057 if (!state) {
1058 it = pass_register.begin();
1059 len = strlen(text);
1060 }
1061
1062 for (; it != pass_register.end(); it++) {
1063 if (it->first.substr(0, len) == text)
1064 return strdup((it++)->first.c_str());
1065 }
1066 return NULL;
1067 }
1068
1069 static char *readline_obj_generator(const char *text, int state)
1070 {
1071 static std::vector<char*> obj_names;
1072 static size_t idx;
1073
1074 if (!state)
1075 {
1076 idx = 0;
1077 obj_names.clear();
1078
1079 RTLIL::Design *design = yosys_get_design();
1080 int len = strlen(text);
1081
1082 if (design->selected_active_module.empty())
1083 {
1084 for (auto &it : design->modules_)
1085 if (RTLIL::unescape_id(it.first).substr(0, len) == text)
1086 obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
1087 }
1088 else
1089 if (design->modules_.count(design->selected_active_module) > 0)
1090 {
1091 RTLIL::Module *module = design->modules_.at(design->selected_active_module);
1092
1093 for (auto &it : module->wires_)
1094 if (RTLIL::unescape_id(it.first).substr(0, len) == text)
1095 obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
1096
1097 for (auto &it : module->memories)
1098 if (RTLIL::unescape_id(it.first).substr(0, len) == text)
1099 obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
1100
1101 for (auto &it : module->cells_)
1102 if (RTLIL::unescape_id(it.first).substr(0, len) == text)
1103 obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
1104
1105 for (auto &it : module->processes)
1106 if (RTLIL::unescape_id(it.first).substr(0, len) == text)
1107 obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
1108 }
1109
1110 std::sort(obj_names.begin(), obj_names.end());
1111 }
1112
1113 if (idx < obj_names.size())
1114 return strdup(obj_names[idx++]);
1115
1116 idx = 0;
1117 obj_names.clear();
1118 return NULL;
1119 }
1120
1121 static char **readline_completion(const char *text, int start, int)
1122 {
1123 if (start == 0)
1124 return rl_completion_matches(text, readline_cmd_generator);
1125 if (strncmp(rl_line_buffer, "read_", 5) && strncmp(rl_line_buffer, "write_", 6))
1126 return rl_completion_matches(text, readline_obj_generator);
1127 return NULL;
1128 }
1129 #endif
1130
1131 void shell(RTLIL::Design *design)
1132 {
1133 static int recursion_counter = 0;
1134
1135 recursion_counter++;
1136 log_cmd_error_throw = true;
1137
1138 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1139 rl_readline_name = (char*)"yosys";
1140 rl_attempted_completion_function = readline_completion;
1141 rl_basic_word_break_characters = (char*)" \t\n";
1142 #endif
1143
1144 char *command = NULL;
1145 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1146 while ((command = readline(create_prompt(design, recursion_counter))) != NULL)
1147 {
1148 #else
1149 char command_buffer[4096];
1150 while (1)
1151 {
1152 fputs(create_prompt(design, recursion_counter), stdout);
1153 fflush(stdout);
1154 if ((command = fgets(command_buffer, 4096, stdin)) == NULL)
1155 break;
1156 #endif
1157 if (command[strspn(command, " \t\r\n")] == 0)
1158 continue;
1159 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1160 add_history(command);
1161 #endif
1162
1163 char *p = command + strspn(command, " \t\r\n");
1164 if (!strncmp(p, "exit", 4)) {
1165 p += 4;
1166 p += strspn(p, " \t\r\n");
1167 if (*p == 0)
1168 break;
1169 }
1170
1171 try {
1172 log_assert(design->selection_stack.size() == 1);
1173 Pass::call(design, command);
1174 } catch (log_cmd_error_exception) {
1175 while (design->selection_stack.size() > 1)
1176 design->selection_stack.pop_back();
1177 log_reset_stack();
1178 }
1179 }
1180 if (command == NULL)
1181 printf("exit\n");
1182
1183 recursion_counter--;
1184 log_cmd_error_throw = false;
1185 }
1186
1187 struct ShellPass : public Pass {
1188 ShellPass() : Pass("shell", "enter interactive command mode") { }
1189 void help() YS_OVERRIDE {
1190 log("\n");
1191 log(" shell\n");
1192 log("\n");
1193 log("This command enters the interactive command mode. This can be useful\n");
1194 log("in a script to interrupt the script at a certain point and allow for\n");
1195 log("interactive inspection or manual synthesis of the design at this point.\n");
1196 log("\n");
1197 log("The command prompt of the interactive shell indicates the current\n");
1198 log("selection (see 'help select'):\n");
1199 log("\n");
1200 log(" yosys>\n");
1201 log(" the entire design is selected\n");
1202 log("\n");
1203 log(" yosys*>\n");
1204 log(" only part of the design is selected\n");
1205 log("\n");
1206 log(" yosys [modname]>\n");
1207 log(" the entire module 'modname' is selected using 'select -module modname'\n");
1208 log("\n");
1209 log(" yosys [modname]*>\n");
1210 log(" only part of current module 'modname' is selected\n");
1211 log("\n");
1212 log("When in interactive shell, some errors (e.g. invalid command arguments)\n");
1213 log("do not terminate yosys but return to the command prompt.\n");
1214 log("\n");
1215 log("This command is the default action if nothing else has been specified\n");
1216 log("on the command line.\n");
1217 log("\n");
1218 log("Press Ctrl-D or type 'exit' to leave the interactive shell.\n");
1219 log("\n");
1220 }
1221 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE {
1222 extra_args(args, 1, design, false);
1223 shell(design);
1224 }
1225 } ShellPass;
1226
1227 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
1228 struct HistoryPass : public Pass {
1229 HistoryPass() : Pass("history", "show last interactive commands") { }
1230 void help() YS_OVERRIDE {
1231 log("\n");
1232 log(" history\n");
1233 log("\n");
1234 log("This command prints all commands in the shell history buffer. This are\n");
1235 log("all commands executed in an interactive session, but not the commands\n");
1236 log("from executed scripts.\n");
1237 log("\n");
1238 }
1239 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE {
1240 extra_args(args, 1, design, false);
1241 #ifdef YOSYS_ENABLE_READLINE
1242 for(HIST_ENTRY **list = history_list(); *list != NULL; list++)
1243 log("%s\n", (*list)->line);
1244 #else
1245 for (int i = where_history(); history_get(i); i++)
1246 log("%s\n", history_get(i)->line);
1247 #endif
1248 }
1249 } HistoryPass;
1250 #endif
1251
1252 struct ScriptCmdPass : public Pass {
1253 ScriptCmdPass() : Pass("script", "execute commands from script file") { }
1254 void help() YS_OVERRIDE {
1255 log("\n");
1256 log(" script <filename> [<from_label>:<to_label>]\n");
1257 log("\n");
1258 log("This command executes the yosys commands in the specified file.\n");
1259 log("\n");
1260 log("The 2nd argument can be used to only execute the section of the\n");
1261 log("file between the specified labels. An empty from label is synonymous\n");
1262 log("for the beginning of the file and an empty to label is synonymous\n");
1263 log("for the end of the file.\n");
1264 log("\n");
1265 log("If only one label is specified (without ':') then only the block\n");
1266 log("marked with that label (until the next label) is executed.\n");
1267 log("\n");
1268 }
1269 void execute(std::vector<std::string> args, RTLIL::Design *design) YS_OVERRIDE {
1270 if (args.size() < 2)
1271 log_cmd_error("Missing script file.\n");
1272 else if (args.size() == 2)
1273 run_frontend(args[1], "script", design);
1274 else if (args.size() == 3)
1275 run_frontend(args[1], "script", NULL, &args[2], design);
1276 else
1277 extra_args(args, 2, design, false);
1278 }
1279 } ScriptCmdPass;
1280
1281 YOSYS_NAMESPACE_END