yosys-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
yosys.cc
Go to the documentation of this file.
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 
22 #ifdef YOSYS_ENABLE_READLINE
23 # include <readline/readline.h>
24 # include <readline/history.h>
25 #endif
26 
27 #ifdef YOSYS_ENABLE_PLUGINS
28 # include <dlfcn.h>
29 #endif
30 
31 #ifdef _WIN32
32 # include <windows.h>
33 # include <io.h>
34 #elif defined(__APPLE__)
35 # include <mach-o/dyld.h>
36 # include <unistd.h>
37 # include <dirent.h>
38 # include <sys/stat.h>
39 #else
40 # include <unistd.h>
41 # include <dirent.h>
42 # include <sys/types.h>
43 # include <sys/stat.h>
44 #endif
45 
46 #include <limits.h>
47 #include <errno.h>
48 
50 
51 int autoidx = 1;
53 
54 #ifdef YOSYS_ENABLE_TCL
55 Tcl_Interp *yosys_tcl_interp = NULL;
56 #endif
57 
58 std::string stringf(const char *fmt, ...)
59 {
60  std::string string;
61  va_list ap;
62 
63  va_start(ap, fmt);
64  string = vstringf(fmt, ap);
65  va_end(ap);
66 
67  return string;
68 }
69 
70 std::string vstringf(const char *fmt, va_list ap)
71 {
72  std::string string;
73  char *str = NULL;
74 
75 #ifdef _WIN32
76  int sz = 64, rc;
77  while (1) {
78  va_list apc;
79  va_copy(apc, ap);
80  str = (char*)realloc(str, sz);
81  rc = vsnprintf(str, sz, fmt, apc);
82  va_end(apc);
83  if (rc >= 0 && rc < sz)
84  break;
85  sz *= 2;
86  }
87 #else
88  if (vasprintf(&str, fmt, ap) < 0)
89  str = NULL;
90 #endif
91 
92  if (str != NULL) {
93  string = str;
94  free(str);
95  }
96 
97  return string;
98 }
99 
100 int readsome(std::istream &f, char *s, int n)
101 {
102  int rc = f.readsome(s, n);
103 
104  // f.readsome() sometimes returns 0 on a non-empty stream..
105  if (rc == 0) {
106  int c = f.get();
107  if (c != EOF) {
108  *s = c;
109  rc = 1;
110  }
111  }
112 
113  return rc;
114 }
115 
116 std::string next_token(std::string &text, const char *sep)
117 {
118  size_t pos_begin = text.find_first_not_of(sep);
119 
120  if (pos_begin == std::string::npos)
121  pos_begin = text.size();
122 
123  size_t pos_end = text.find_first_of(sep, pos_begin);
124 
125  if (pos_end == std::string::npos)
126  pos_end = text.size();
127 
128  std::string token = text.substr(pos_begin, pos_end-pos_begin);
129  text = text.substr(pos_end);
130  return token;
131 }
132 
133 // this is very similar to fnmatch(). the exact rules used by this
134 // function are:
135 //
136 // ? matches any character except
137 // * matches any sequence of characters
138 // [...] matches any of the characters in the list
139 // [!..] matches any of the characters not in the list
140 //
141 // a backslash may be used to escape the next characters in the
142 // pattern. each special character can also simply match itself.
143 //
144 bool patmatch(const char *pattern, const char *string)
145 {
146  if (*pattern == 0)
147  return *string == 0;
148 
149  if (*pattern == '\\') {
150  if (pattern[1] == string[0] && patmatch(pattern+2, string+1))
151  return true;
152  }
153 
154  if (*pattern == '?') {
155  if (*string == 0)
156  return false;
157  return patmatch(pattern+1, string+1);
158  }
159 
160  if (*pattern == '*') {
161  while (*string) {
162  if (patmatch(pattern+1, string++))
163  return true;
164  }
165  return pattern[1] == 0;
166  }
167 
168  if (*pattern == '[') {
169  bool found_match = false;
170  bool inverted_list = pattern[1] == '!';
171  const char *p = pattern + (inverted_list ? 1 : 0);
172 
173  while (*++p) {
174  if (*p == ']') {
175  if (found_match != inverted_list && patmatch(p+1, string+1))
176  return true;
177  break;
178  }
179 
180  if (*p == '\\') {
181  if (*++p == *string)
182  found_match = true;
183  } else
184  if (*p == *string)
185  found_match = true;
186  }
187  }
188 
189  if (*pattern == *string)
190  return patmatch(pattern+1, string+1);
191 
192  return false;
193 }
194 
195 int run_command(const std::string &command, std::function<void(const std::string&)> process_line)
196 {
197  if (!process_line)
198  return system(command.c_str());
199 
200  FILE *f = popen(command.c_str(), "r");
201  if (f == nullptr)
202  return -1;
203 
204  std::string line;
205  char logbuf[128];
206  while (fgets(logbuf, 128, f) != NULL) {
207  line += logbuf;
208  if (!line.empty() && line.back() == '\n')
209  process_line(line), line.clear();
210  }
211  if (!line.empty())
212  process_line(line);
213 
214  int ret = pclose(f);
215  if (ret < 0)
216  return -1;
217 #ifdef _WIN32
218  return ret;
219 #else
220  return WEXITSTATUS(ret);
221 #endif
222 }
223 
224 std::string make_temp_file(std::string template_str)
225 {
226 #ifdef _WIN32
227  if (template_str.rfind("/tmp/", 0) == 0) {
228 # ifdef __MINGW32__
229  char longpath[MAX_PATH + 1];
230  char shortpath[MAX_PATH + 1];
231 # else
232  WCHAR longpath[MAX_PATH + 1];
233  TCHAR shortpath[MAX_PATH + 1];
234 # endif
235  if (!GetTempPath(MAX_PATH+1, longpath))
236  log_error("GetTempPath() failed.\n");
237  if (!GetShortPathName(longpath, shortpath, MAX_PATH + 1))
238  log_error("GetShortPathName() failed.\n");
239  std::string path;
240  for (int i = 0; shortpath[i]; i++)
241  path += char(shortpath[i]);
242  template_str = stringf("%s\\%s", path.c_str(), template_str.c_str() + 5);
243  }
244 
245  size_t pos = template_str.rfind("XXXXXX");
246  log_assert(pos != std::string::npos);
247 
248  while (1) {
249  for (int i = 0; i < 6; i++) {
250  static std::string y = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
251  static uint32_t x = 314159265 ^ uint32_t(time(NULL));
252  x ^= x << 13, x ^= x >> 17, x ^= x << 5;
253  template_str[pos+i] = y[x % y.size()];
254  }
255  if (_access(template_str.c_str(), 0) != 0)
256  break;
257  }
258 #else
259  size_t pos = template_str.rfind("XXXXXX");
260  log_assert(pos != std::string::npos);
261 
262  int suffixlen = GetSize(template_str) - pos - 6;
263 
264  char *p = strdup(template_str.c_str());
265  close(mkstemps(p, suffixlen));
266  template_str = p;
267  free(p);
268 #endif
269 
270  return template_str;
271 }
272 
273 std::string make_temp_dir(std::string template_str)
274 {
275 #ifdef _WIN32
276  template_str = make_temp_file(template_str);
277  mkdir(template_str.c_str());
278  return template_str;
279 #else
280  size_t pos = template_str.rfind("XXXXXX");
281  log_assert(pos != std::string::npos);
282 
283  int suffixlen = GetSize(template_str) - pos - 6;
284  log_assert(suffixlen == 0);
285 
286  char *p = strdup(template_str.c_str());
287  p = mkdtemp(p);
288  log_assert(p != NULL);
289  template_str = p;
290  free(p);
291 
292  return template_str;
293 #endif
294 }
295 
296 #ifdef _WIN32
297 bool check_file_exists(std::string filename, bool)
298 {
299  return _access(filename.c_str(), 0) == 0;
300 }
301 #else
302 bool check_file_exists(std::string filename, bool is_exec)
303 {
304  return access(filename.c_str(), is_exec ? X_OK : F_OK) == 0;
305 }
306 #endif
307 
308 void remove_directory(std::string dirname)
309 {
310 #ifdef _WIN32
311  run_command(stringf("rmdir /s /q \"%s\"", dirname.c_str()));
312 #else
313  struct stat stbuf;
314  struct dirent **namelist;
315  int n = scandir(dirname.c_str(), &namelist, nullptr, alphasort);
316  log_assert(n >= 0);
317  for (int i = 0; i < n; i++) {
318  if (strcmp(namelist[i]->d_name, ".") && strcmp(namelist[i]->d_name, "..")) {
319  std::string buffer = stringf("%s/%s", dirname.c_str(), namelist[i]->d_name);
320  if (!stat(buffer.c_str(), &stbuf) && S_ISREG(stbuf.st_mode)) {
321  log("Removing `%s'.\n", buffer.c_str());
322  remove(buffer.c_str());
323  } else
324  remove_directory(buffer);
325  }
326  free(namelist[i]);
327  }
328  free(namelist);
329  log("Removing `%s'.\n", dirname.c_str());
330  rmdir(dirname.c_str());
331 #endif
332 }
333 
335 {
336  return wire->width;
337 }
338 
340 {
342  yosys_design = new RTLIL::Design;
343  log_push();
344 }
345 
347 {
348  log_pop();
349 
350  delete yosys_design;
351  yosys_design = NULL;
352 
353  for (auto f : log_files)
354  if (f != stderr)
355  fclose(f);
356  log_errfile = NULL;
357  log_files.clear();
358 
360 
361 #ifdef YOSYS_ENABLE_TCL
362  if (yosys_tcl_interp != NULL) {
363  Tcl_DeleteInterp(yosys_tcl_interp);
364  Tcl_Finalize();
365  yosys_tcl_interp = NULL;
366  }
367 #endif
368 
369 #ifdef YOSYS_ENABLE_PLUGINS
370  for (auto &it : loaded_plugins)
371  dlclose(it.second);
372 
373  loaded_plugins.clear();
374  loaded_plugin_aliases.clear();
375 #endif
376 }
377 
378 RTLIL::IdString new_id(std::string file, int line, std::string func)
379 {
380 #ifdef _WIN32
381  size_t pos = file.find_last_of("/\\");
382 #else
383  size_t pos = file.find_last_of('/');
384 #endif
385  if (pos != std::string::npos)
386  file = file.substr(pos+1);
387 
388  pos = func.find_last_of(':');
389  if (pos != std::string::npos)
390  func = func.substr(pos+1);
391 
392  return stringf("$auto$%s:%d:%s$%d", file.c_str(), line, func.c_str(), autoidx++);
393 }
394 
396 {
397  return yosys_design;
398 }
399 
400 const char *create_prompt(RTLIL::Design *design, int recursion_counter)
401 {
402  static char buffer[100];
403  std::string str = "\n";
404  if (recursion_counter > 1)
405  str += stringf("(%d) ", recursion_counter);
406  str += "yosys";
407  if (!design->selected_active_module.empty())
408  str += stringf(" [%s]", RTLIL::unescape_id(design->selected_active_module).c_str());
409  if (!design->selection_stack.empty() && !design->selection_stack.back().full_selection) {
410  if (design->selected_active_module.empty())
411  str += "*";
412  else if (design->selection_stack.back().selected_modules.size() != 1 || design->selection_stack.back().selected_members.size() != 0 ||
413  design->selection_stack.back().selected_modules.count(design->selected_active_module) == 0)
414  str += "*";
415  }
416  snprintf(buffer, 100, "%s> ", str.c_str());
417  return buffer;
418 }
419 
420 #ifdef YOSYS_ENABLE_TCL
421 static int tcl_yosys_cmd(ClientData, Tcl_Interp *interp, int argc, const char *argv[])
422 {
423  std::vector<std::string> args;
424  for (int i = 1; i < argc; i++)
425  args.push_back(argv[i]);
426 
427  if (args.size() >= 1 && args[0] == "-import") {
428  for (auto &it : pass_register) {
429  std::string tcl_command_name = it.first;
430  if (tcl_command_name == "proc")
431  tcl_command_name = "procs";
432  Tcl_CmdInfo info;
433  if (Tcl_GetCommandInfo(interp, tcl_command_name.c_str(), &info) != 0) {
434  log("[TCL: yosys -import] Command name collision: found pre-existing command `%s' -> skip.\n", it.first.c_str());
435  } else {
436  std::string tcl_script = stringf("proc %s args { yosys %s {*}$args }", tcl_command_name.c_str(), it.first.c_str());
437  Tcl_Eval(interp, tcl_script.c_str());
438  }
439  }
440  return TCL_OK;
441  }
442 
443  if (args.size() == 1) {
444  Pass::call(yosys_get_design(), args[0]);
445  return TCL_OK;
446  }
447 
448  Pass::call(yosys_get_design(), args);
449  return TCL_OK;
450 }
451 
452 extern Tcl_Interp *yosys_get_tcl_interp()
453 {
454  if (yosys_tcl_interp == NULL) {
455  yosys_tcl_interp = Tcl_CreateInterp();
456  Tcl_CreateCommand(yosys_tcl_interp, "yosys", tcl_yosys_cmd, NULL, NULL);
457  }
458  return yosys_tcl_interp;
459 }
460 
461 struct TclPass : public Pass {
462  TclPass() : Pass("tcl", "execute a TCL script file") { }
463  virtual void help() {
464  log("\n");
465  log(" tcl <filename>\n");
466  log("\n");
467  log("This command executes the tcl commands in the specified file.\n");
468  log("Use 'yosys cmd' to run the yosys command 'cmd' from tcl.\n");
469  log("\n");
470  log("The tcl command 'yosys -import' can be used to import all yosys\n");
471  log("commands directly as tcl commands to the tcl shell. The yosys\n");
472  log("command 'proc' is wrapped using the tcl command 'procs' in order\n");
473  log("to avoid a name collision with the tcl builting command 'proc'.\n");
474  log("\n");
475  }
476  virtual void execute(std::vector<std::string> args, RTLIL::Design *design) {
477  if (args.size() < 2)
478  log_cmd_error("Missing script file.\n");
479  if (args.size() > 2)
480  extra_args(args, 1, design, false);
481  if (Tcl_EvalFile(yosys_get_tcl_interp(), args[1].c_str()) != TCL_OK)
482  log_cmd_error("TCL interpreter returned an error: %s\n", Tcl_GetStringResult(yosys_get_tcl_interp()));
483  }
484 } TclPass;
485 #endif
486 
487 #if defined(__linux__)
488 std::string proc_self_dirname()
489 {
490  char path[PATH_MAX];
491  ssize_t buflen = readlink("/proc/self/exe", path, sizeof(path));
492  if (buflen < 0) {
493  log_error("readlink(\"/proc/self/exe\") failed: %s\n", strerror(errno));
494  }
495  while (buflen > 0 && path[buflen-1] != '/')
496  buflen--;
497  return std::string(path, buflen);
498 }
499 #elif defined(__APPLE__)
500 std::string proc_self_dirname()
501 {
502  char *path = NULL;
503  uint32_t buflen = 0;
504  while (_NSGetExecutablePath(path, &buflen) != 0)
505  path = (char *) realloc((void *) path, buflen);
506  while (buflen > 0 && path[buflen-1] != '/')
507  buflen--;
508  return std::string(path, buflen);
509 }
510 #elif defined(_WIN32)
511 std::string proc_self_dirname()
512 {
513  int i = 0;
514 # ifdef __MINGW32__
515  char longpath[MAX_PATH + 1];
516  char shortpath[MAX_PATH + 1];
517 # else
518  WCHAR longpath[MAX_PATH + 1];
519  TCHAR shortpath[MAX_PATH + 1];
520 # endif
521  if (!GetModuleFileName(0, longpath, MAX_PATH+1))
522  log_error("GetModuleFileName() failed.\n");
523  if (!GetShortPathName(longpath, shortpath, MAX_PATH+1))
524  log_error("GetShortPathName() failed.\n");
525  while (shortpath[i] != 0)
526  i++;
527  while (i > 0 && shortpath[i-1] != '/' && shortpath[i-1] != '\\')
528  shortpath[--i] = 0;
529  std::string path;
530  for (i = 0; shortpath[i]; i++)
531  path += char(shortpath[i]);
532  return path;
533 }
534 #elif defined(EMSCRIPTEN)
535 std::string proc_self_dirname()
536 {
537  return "/";
538 }
539 #else
540  #error Dont know how to determine process executable base path!
541 #endif
542 
543 std::string proc_share_dirname()
544 {
545  std::string proc_self_path = proc_self_dirname();
546 #ifdef _WIN32
547  std::string proc_share_path = proc_self_path + "share\\";
548  if (check_file_exists(proc_share_path, true))
549  return proc_share_path;
550  proc_share_path = proc_self_path + "..\\share\\";
551  if (check_file_exists(proc_share_path, true))
552  return proc_share_path;
553 #else
554  std::string proc_share_path = proc_self_path + "share/";
555  if (check_file_exists(proc_share_path, true))
556  return proc_share_path;
557  proc_share_path = proc_self_path + "../share/yosys/";
558  if (check_file_exists(proc_share_path, true))
559  return proc_share_path;
560 #endif
561  log_error("proc_share_dirname: unable to determine share/ directory!\n");
562 }
563 
564 bool fgetline(FILE *f, std::string &buffer)
565 {
566  buffer = "";
567  char block[4096];
568  while (1) {
569  if (fgets(block, 4096, f) == NULL)
570  return false;
571  buffer += block;
572  if (buffer.size() > 0 && (buffer[buffer.size()-1] == '\n' || buffer[buffer.size()-1] == '\r')) {
573  while (buffer.size() > 0 && (buffer[buffer.size()-1] == '\n' || buffer[buffer.size()-1] == '\r'))
574  buffer.resize(buffer.size()-1);
575  return true;
576  }
577  }
578 }
579 
580 static void handle_label(std::string &command, bool &from_to_active, const std::string &run_from, const std::string &run_to)
581 {
582  int pos = 0;
583  std::string label;
584 
585  while (pos < GetSize(command) && (command[pos] == ' ' || command[pos] == '\t'))
586  pos++;
587 
588  while (pos < GetSize(command) && command[pos] != ' ' && command[pos] != '\t' && command[pos] != '\r' && command[pos] != '\n')
589  label += command[pos++];
590 
591  if (label.back() == ':' && GetSize(label) > 1)
592  {
593  label = label.substr(0, GetSize(label)-1);
594  command = command.substr(pos);
595 
596  if (label == run_from)
597  from_to_active = true;
598  else if (label == run_to || (run_from == run_to && !run_from.empty()))
599  from_to_active = false;
600  }
601 }
602 
603 void run_frontend(std::string filename, std::string command, RTLIL::Design *design, std::string *backend_command, std::string *from_to_label)
604 {
605  if (command == "auto") {
606  if (filename.size() > 2 && filename.substr(filename.size()-2) == ".v")
607  command = "verilog";
608  else if (filename.size() > 2 && filename.substr(filename.size()-3) == ".sv")
609  command = "verilog -sv";
610  else if (filename.size() > 3 && filename.substr(filename.size()-3) == ".il")
611  command = "ilang";
612  else if (filename.size() > 3 && filename.substr(filename.size()-3) == ".ys")
613  command = "script";
614  else if (filename == "-")
615  command = "script";
616  else
617  log_error("Can't guess frontend for input file `%s' (missing -f option)!\n", filename.c_str());
618  }
619 
620  if (command == "script")
621  {
622  std::string run_from, run_to;
623  bool from_to_active = true;
624 
625  if (from_to_label != NULL) {
626  size_t pos = from_to_label->find(':');
627  if (pos == std::string::npos) {
628  run_from = *from_to_label;
629  run_to = *from_to_label;
630  } else {
631  run_from = from_to_label->substr(0, pos);
632  run_to = from_to_label->substr(pos+1);
633  }
634  from_to_active = run_from.empty();
635  }
636 
637  log("\n-- Executing script file `%s' --\n", filename.c_str());
638 
639  FILE *f = stdin;
640 
641  if (filename != "-")
642  f = fopen(filename.c_str(), "r");
643 
644  if (f == NULL)
645  log_error("Can't open script file `%s' for reading: %s\n", filename.c_str(), strerror(errno));
646 
647  FILE *backup_script_file = Frontend::current_script_file;
649 
650  try {
651  std::string command;
652  while (fgetline(f, command)) {
653  while (!command.empty() && command[command.size()-1] == '\\') {
654  std::string next_line;
655  if (!fgetline(f, next_line))
656  break;
657  command.resize(command.size()-1);
658  command += next_line;
659  }
660  handle_label(command, from_to_active, run_from, run_to);
661  if (from_to_active)
662  Pass::call(design, command);
663  }
664 
665  if (!command.empty()) {
666  handle_label(command, from_to_active, run_from, run_to);
667  if (from_to_active)
668  Pass::call(design, command);
669  }
670  }
671  catch (log_cmd_error_exception) {
672  Frontend::current_script_file = backup_script_file;
673  throw log_cmd_error_exception();
674  }
675 
676  Frontend::current_script_file = backup_script_file;
677 
678  if (filename != "-")
679  fclose(f);
680 
681  if (backend_command != NULL && *backend_command == "auto")
682  *backend_command = "";
683 
684  return;
685  }
686 
687  if (filename == "-") {
688  log("\n-- Parsing stdin using frontend `%s' --\n", command.c_str());
689  } else {
690  log("\n-- Parsing `%s' using frontend `%s' --\n", filename.c_str(), command.c_str());
691  }
692 
693  Frontend::frontend_call(design, NULL, filename, command);
694 }
695 
696 void run_pass(std::string command, RTLIL::Design *design)
697 {
698  log("\n-- Running pass `%s' --\n", command.c_str());
699 
700  Pass::call(design, command);
701 }
702 
703 void run_backend(std::string filename, std::string command, RTLIL::Design *design)
704 {
705  if (command == "auto") {
706  if (filename.size() > 2 && filename.substr(filename.size()-2) == ".v")
707  command = "verilog";
708  else if (filename.size() > 3 && filename.substr(filename.size()-3) == ".il")
709  command = "ilang";
710  else if (filename.size() > 5 && filename.substr(filename.size()-5) == ".blif")
711  command = "blif";
712  else if (filename == "-")
713  command = "ilang";
714  else if (filename.empty())
715  return;
716  else
717  log_error("Can't guess backend for output file `%s' (missing -b option)!\n", filename.c_str());
718  }
719 
720  if (filename.empty())
721  filename = "-";
722 
723  if (filename == "-") {
724  log("\n-- Writing to stdout using backend `%s' --\n", command.c_str());
725  } else {
726  log("\n-- Writing to `%s' using backend `%s' --\n", filename.c_str(), command.c_str());
727  }
728 
729  Backend::backend_call(design, NULL, filename, command);
730 }
731 
732 #ifdef YOSYS_ENABLE_READLINE
733 static char *readline_cmd_generator(const char *text, int state)
734 {
735  static std::map<std::string, Pass*>::iterator it;
736  static int len;
737 
738  if (!state) {
739  it = pass_register.begin();
740  len = strlen(text);
741  }
742 
743  for (; it != pass_register.end(); it++) {
744  if (it->first.substr(0, len) == text)
745  return strdup((it++)->first.c_str());
746  }
747  return NULL;
748 }
749 
750 static char *readline_obj_generator(const char *text, int state)
751 {
752  static std::vector<char*> obj_names;
753  static size_t idx;
754 
755  if (!state)
756  {
757  idx = 0;
758  obj_names.clear();
759 
760  RTLIL::Design *design = yosys_get_design();
761  int len = strlen(text);
762 
763  if (design->selected_active_module.empty())
764  {
765  for (auto &it : design->modules_)
766  if (RTLIL::unescape_id(it.first).substr(0, len) == text)
767  obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
768  }
769  else
770  if (design->modules_.count(design->selected_active_module) > 0)
771  {
772  RTLIL::Module *module = design->modules_.at(design->selected_active_module);
773 
774  for (auto &it : module->wires_)
775  if (RTLIL::unescape_id(it.first).substr(0, len) == text)
776  obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
777 
778  for (auto &it : module->memories)
779  if (RTLIL::unescape_id(it.first).substr(0, len) == text)
780  obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
781 
782  for (auto &it : module->cells_)
783  if (RTLIL::unescape_id(it.first).substr(0, len) == text)
784  obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
785 
786  for (auto &it : module->processes)
787  if (RTLIL::unescape_id(it.first).substr(0, len) == text)
788  obj_names.push_back(strdup(RTLIL::id2cstr(it.first)));
789  }
790 
791  std::sort(obj_names.begin(), obj_names.end());
792  }
793 
794  if (idx < obj_names.size())
795  return strdup(obj_names[idx++]);
796 
797  idx = 0;
798  obj_names.clear();
799  return NULL;
800 }
801 
802 static char **readline_completion(const char *text, int start, int)
803 {
804  if (start == 0)
805  return rl_completion_matches(text, readline_cmd_generator);
806  if (strncmp(rl_line_buffer, "read_", 5) && strncmp(rl_line_buffer, "write_", 6))
807  return rl_completion_matches(text, readline_obj_generator);
808  return NULL;
809 }
810 #endif
811 
812 void shell(RTLIL::Design *design)
813 {
814  static int recursion_counter = 0;
815 
816  recursion_counter++;
817  log_cmd_error_throw = true;
818 
819 #ifdef YOSYS_ENABLE_READLINE
820  rl_readline_name = "yosys";
821  rl_attempted_completion_function = readline_completion;
822  rl_basic_word_break_characters = " \t\n";
823 #endif
824 
825  char *command = NULL;
826 #ifdef YOSYS_ENABLE_READLINE
827  while ((command = readline(create_prompt(design, recursion_counter))) != NULL)
828  {
829 #else
830  char command_buffer[4096];
831  while (1)
832  {
833  fputs(create_prompt(design, recursion_counter), stdout);
834  fflush(stdout);
835  if ((command = fgets(command_buffer, 4096, stdin)) == NULL)
836  break;
837 #endif
838  if (command[strspn(command, " \t\r\n")] == 0)
839  continue;
840 #ifdef YOSYS_ENABLE_READLINE
841  add_history(command);
842 #endif
843 
844  char *p = command + strspn(command, " \t\r\n");
845  if (!strncmp(p, "exit", 4)) {
846  p += 4;
847  p += strspn(p, " \t\r\n");
848  if (*p == 0)
849  break;
850  }
851 
852  try {
853  log_assert(design->selection_stack.size() == 1);
854  Pass::call(design, command);
855  } catch (log_cmd_error_exception) {
856  while (design->selection_stack.size() > 1)
857  design->selection_stack.pop_back();
858  log_reset_stack();
859  }
860  }
861  if (command == NULL)
862  printf("exit\n");
863 
864  recursion_counter--;
865  log_cmd_error_throw = false;
866 }
867 
868 struct ShellPass : public Pass {
869  ShellPass() : Pass("shell", "enter interactive command mode") { }
870  virtual void help() {
871  log("\n");
872  log(" shell\n");
873  log("\n");
874  log("This command enters the interactive command mode. This can be useful\n");
875  log("in a script to interrupt the script at a certain point and allow for\n");
876  log("interactive inspection or manual synthesis of the design at this point.\n");
877  log("\n");
878  log("The command prompt of the interactive shell indicates the current\n");
879  log("selection (see 'help select'):\n");
880  log("\n");
881  log(" yosys>\n");
882  log(" the entire design is selected\n");
883  log("\n");
884  log(" yosys*>\n");
885  log(" only part of the design is selected\n");
886  log("\n");
887  log(" yosys [modname]>\n");
888  log(" the entire module 'modname' is selected using 'select -module modname'\n");
889  log("\n");
890  log(" yosys [modname]*>\n");
891  log(" only part of current module 'modname' is selected\n");
892  log("\n");
893  log("When in interactive shell, some errors (e.g. invalid command arguments)\n");
894  log("do not terminate yosys but return to the command prompt.\n");
895  log("\n");
896  log("This command is the default action if nothing else has been specified\n");
897  log("on the command line.\n");
898  log("\n");
899  log("Press Ctrl-D or type 'exit' to leave the interactive shell.\n");
900  log("\n");
901  }
902  virtual void execute(std::vector<std::string> args, RTLIL::Design *design) {
903  extra_args(args, 1, design, false);
904  shell(design);
905  }
906 } ShellPass;
907 
908 #ifdef YOSYS_ENABLE_READLINE
909 struct HistoryPass : public Pass {
910  HistoryPass() : Pass("history", "show last interactive commands") { }
911  virtual void help() {
912  log("\n");
913  log(" history\n");
914  log("\n");
915  log("This command prints all commands in the shell history buffer. This are\n");
916  log("all commands executed in an interactive session, but not the commands\n");
917  log("from executed scripts.\n");
918  log("\n");
919  }
920  virtual void execute(std::vector<std::string> args, RTLIL::Design *design) {
921  extra_args(args, 1, design, false);
922  for(HIST_ENTRY **list = history_list(); *list != NULL; list++)
923  log("%s\n", (*list)->line);
924  }
925 } HistoryPass;
926 #endif
927 
928 struct ScriptPass : public Pass {
929  ScriptPass() : Pass("script", "execute commands from script file") { }
930  virtual void help() {
931  log("\n");
932  log(" script <filename> [<from_label>:<to_label>]\n");
933  log("\n");
934  log("This command executes the yosys commands in the specified file.\n");
935  log("\n");
936  log("The 2nd argument can be used to only execute the section of the\n");
937  log("file between the specified labels. An empty from label is synonymous\n");
938  log("for the beginning of the file and an empty to label is synonymous\n");
939  log("for the end of the file.\n");
940  log("\n");
941  log("If only one label is specified (without ':') then only the block\n");
942  log("marked with that label (until the next label) is executed.\n");
943  log("\n");
944  }
945  virtual void execute(std::vector<std::string> args, RTLIL::Design *design) {
946  if (args.size() < 2)
947  log_cmd_error("Missing script file.\n");
948  else if (args.size() == 2)
949  run_frontend(args[1], "script", design, NULL, NULL);
950  else if (args.size() == 3)
951  run_frontend(args[1], "script", design, NULL, &args[2]);
952  else
953  extra_args(args, 2, design, false);
954  }
955 } ScriptPass;
956 
958 
static void handle_label(std::string &command, bool &from_to_active, const std::string &run_from, const std::string &run_to)
Definition: yosys.cc:580
std::string next_token(std::string &text, const char *sep)
Definition: yosys.cc:116
std::vector< RTLIL::Selection > selection_stack
Definition: rtlil.h:509
std::string stringf(const char *fmt,...)
Definition: yosys.cc:58
const char * create_prompt(RTLIL::Design *design, int recursion_counter)
Definition: yosys.cc:400
void remove_directory(std::string dirname)
Definition: yosys.cc:308
void sort(T *array, int size, LessThan lt)
Definition: Sort.h:57
void free(void *)
virtual void help()
Definition: yosys.cc:930
RTLIL::Design * yosys_design
Definition: yosys.cc:52
#define YOSYS_NAMESPACE_END
Definition: yosys.h:100
void yosys_setup()
Definition: yosys.cc:339
std::map< RTLIL::IdString, RTLIL::Wire * > wires_
Definition: rtlil.h:595
static std::string unescape_id(std::string str)
Definition: rtlil.h:257
static void frontend_call(RTLIL::Design *design, std::istream *f, std::string filename, std::string command)
Definition: register.cc:375
static std::string idx(std::string str)
Definition: test_autotb.cc:57
int width
Definition: rtlil.h:826
std::map< RTLIL::IdString, RTLIL::Memory * > memories
Definition: rtlil.h:601
ScriptPass ScriptPass
FILE * log_errfile
Definition: log.cc:39
void log_error(const char *format,...)
Definition: log.cc:204
RTLIL::Module * module
Definition: abc.cc:94
RTLIL::IdString new_id(std::string file, int line, std::string func)
Definition: yosys.cc:378
void log_pop()
Definition: log.cc:237
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)=0
ShellPass ShellPass
ScriptPass()
Definition: yosys.cc:929
std::string make_temp_file(std::string template_str)
Definition: yosys.cc:224
static FILE * current_script_file
Definition: register.h:69
tuple n
Definition: fsm/generate.py:59
int run_command(const std::string &command, std::function< void(const std::string &)> process_line)
Definition: yosys.cc:195
virtual void help()
Definition: register.cc:93
YOSYS_NAMESPACE_BEGIN std::vector< FILE * > log_files
Definition: log.cc:37
std::string vstringf(const char *fmt, va_list ap)
Definition: yosys.cc:70
bool patmatch(const char *pattern, const char *string)
Definition: yosys.cc:144
static void backend_call(RTLIL::Design *design, std::ostream *f, std::string filename, std::string command)
Definition: register.cc:479
std::string proc_self_dirname()
int GetSize(RTLIL::Wire *wire)
Definition: yosys.cc:334
#define log_assert(_assert_expr_)
Definition: log.h:85
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)
Definition: yosys.cc:945
static void done_register()
Definition: register.cc:62
std::string make_temp_dir(std::string template_str)
Definition: yosys.cc:273
Definition: register.h:27
static const char * id2cstr(const RTLIL::IdString &str)
Definition: rtlil.h:267
void log_cmd_error(const char *format,...)
Definition: log.cc:211
int readsome(std::istream &f, char *s, int n)
Definition: yosys.cc:100
bool check_file_exists(std::string filename, bool is_exec)
Definition: yosys.cc:302
bool log_cmd_error_throw
Definition: log.cc:43
std::map< RTLIL::IdString, RTLIL::Process * > processes
Definition: rtlil.h:602
void log_reset_stack()
Definition: log.cc:246
std::string substr(size_t pos=0, size_t len=std::string::npos) const
Definition: rtlil.h:208
RTLIL::Design * yosys_get_design()
Definition: yosys.cc:395
virtual void execute(std::vector< std::string > args, RTLIL::Design *design)
Definition: yosys.cc:902
std::map< RTLIL::IdString, RTLIL::Module * > modules_
Definition: rtlil.h:507
std::map< std::string, std::string > loaded_plugin_aliases
Definition: plugin.cc:29
#define NULL
std::map< RTLIL::IdString, RTLIL::Cell * > cells_
Definition: rtlil.h:596
#define YOSYS_NAMESPACE_BEGIN
Definition: yosys.h:99
void shell(RTLIL::Design *design)
Definition: yosys.cc:812
void log(const char *format,...)
Definition: log.cc:180
void run_backend(std::string filename, std::string command, RTLIL::Design *design)
Definition: yosys.cc:703
void log_push()
Definition: log.cc:232
std::map< std::string, Pass * > pass_register
Definition: register.cc:35
std::string proc_share_dirname()
Definition: yosys.cc:543
std::string selected_active_module
Definition: rtlil.h:511
static void init_register()
Definition: register.cc:54
void yosys_shutdown()
Definition: yosys.cc:346
void extra_args(std::vector< std::string > args, size_t argidx, RTLIL::Design *design, bool select=true)
Definition: register.cc:128
ShellPass()
Definition: yosys.cc:869
static void call(RTLIL::Design *design, std::string command)
Definition: register.cc:146
void run_pass(std::string command, RTLIL::Design *design)
Definition: yosys.cc:696
virtual void help()
Definition: yosys.cc:870
YOSYS_NAMESPACE_BEGIN int autoidx
Definition: yosys.cc:51
bool fgetline(FILE *f, std::string &buffer)
Definition: yosys.cc:564
void run_frontend(std::string filename, std::string command, RTLIL::Design *design, std::string *backend_command, std::string *from_to_label)
Definition: yosys.cc:603
std::map< std::string, void * > loaded_plugins
Definition: plugin.cc:28