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