Merge branch 'koriakin/xc7nocarrymux' into xaig
[yosys.git] / kernel / driver.cc
index 65b6c5a47660e8c21f751ebe800c4cb30ca2bb9b..f273057dd765d4872efcfa3f38a4be92a93fceca 100644 (file)
@@ -2,11 +2,11 @@
  *  yosys -- Yosys Open SYnthesis Suite
  *
  *  Copyright (C) 2012  Clifford Wolf <clifford@clifford.at>
- *  
+ *
  *  Permission to use, copy, modify, and/or distribute this software for any
  *  purpose with or without fee is hereby granted, provided that the above
  *  copyright notice and this permission notice appear in all copies.
- *  
+ *
  *  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  *  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  *  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 #  include <readline/history.h>
 #endif
 
+#ifdef YOSYS_ENABLE_EDITLINE
+#  include <editline/readline.h>
+#endif
+
 #include <stdio.h>
 #include <string.h>
 #include <limits.h>
 #include <errno.h>
 
-#ifdef __linux__
+#if defined (__linux__) || defined(__FreeBSD__)
+#  include <sys/resource.h>
 #  include <sys/types.h>
 #  include <unistd.h>
 #endif
 
+#ifdef __FreeBSD__
+#  include <sys/sysctl.h>
+#  include <sys/user.h>
+#endif
+
 #if !defined(_WIN32) || defined(__MINGW32__)
 #  include <unistd.h>
 #else
@@ -72,14 +82,113 @@ int getopt(int argc, char **argv, const char *optstring)
 
 USING_YOSYS_NAMESPACE
 
+#ifdef EMSCRIPTEN
+#  include <sys/stat.h>
+#  include <sys/types.h>
+#  include <emscripten.h>
+
+extern "C" int main(int, char**);
+extern "C" void run(const char*);
+extern "C" const char *errmsg();
+extern "C" const char *prompt();
+
+int main(int argc, char **argv)
+{
+       EM_ASM(
+               if (ENVIRONMENT_IS_NODE)
+               {
+                       FS.mkdir('/hostcwd');
+                       FS.mount(NODEFS, { root: '.' }, '/hostcwd');
+                       FS.mkdir('/hostfs');
+                       FS.mount(NODEFS, { root: '/' }, '/hostfs');
+               }
+       );
+
+       mkdir("/work", 0777);
+       chdir("/work");
+       log_files.push_back(stdout);
+       log_error_stderr = true;
+       yosys_banner();
+       yosys_setup();
+#ifdef WITH_PYTHON
+       PyRun_SimpleString(("sys.path.append(\""+proc_self_dirname()+"\")").c_str());
+       PyRun_SimpleString(("sys.path.append(\""+proc_share_dirname()+"plugins\")").c_str());
+#endif
+
+       if (argc == 2)
+       {
+               // Run the first argument as a script file
+               run_frontend(argv[1], "script", 0, 0, 0);
+       }
+}
+
+void run(const char *command)
+{
+       int selSize = GetSize(yosys_get_design()->selection_stack);
+       try {
+               log_last_error = "Internal error (see JavaScript console for details)";
+               run_pass(command);
+               log_last_error = "";
+       } catch (...) {
+               while (GetSize(yosys_get_design()->selection_stack) > selSize)
+                       yosys_get_design()->selection_stack.pop_back();
+               throw;
+       }
+}
+
+const char *errmsg()
+{
+       return log_last_error.c_str();
+}
+
+const char *prompt()
+{
+       const char *p = create_prompt(yosys_get_design(), 0);
+       while (*p == '\n') p++;
+       return p;
+}
+
+#else /* EMSCRIPTEN */
+
+#if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
+int yosys_history_offset = 0;
+std::string yosys_history_file;
+#endif
+
+void yosys_atexit()
+{
+#if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
+       if (!yosys_history_file.empty()) {
+#if defined(YOSYS_ENABLE_READLINE)
+               if (yosys_history_offset > 0) {
+                       history_truncate_file(yosys_history_file.c_str(), 100);
+                       append_history(where_history() - yosys_history_offset, yosys_history_file.c_str());
+               } else
+                       write_history(yosys_history_file.c_str());
+#else
+               write_history(yosys_history_file.c_str());
+#endif
+       }
+
+       clear_history();
+#if defined(YOSYS_ENABLE_READLINE)
+       HIST_ENTRY **hist_list = history_list();
+       if (hist_list != NULL)
+               free(hist_list);
+#endif
+#endif
+}
+
 int main(int argc, char **argv)
 {
        std::string frontend_command = "auto";
        std::string backend_command = "auto";
+       std::vector<std::string> vlog_defines;
        std::vector<std::string> passes_commands;
        std::vector<std::string> plugin_filenames;
        std::string output_filename = "";
        std::string scriptfile = "";
+       std::string depsfile = "";
        bool scriptfile_tcl = false;
        bool got_output_filename = false;
        bool print_banner = true;
@@ -89,13 +198,11 @@ int main(int argc, char **argv)
        bool mode_v = false;
        bool mode_q = false;
 
-#ifdef YOSYS_ENABLE_READLINE
-       int history_offset = 0;
-       std::string history_file;
+#if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
        if (getenv("HOME") != NULL) {
-               history_file = stringf("%s/.yosys_history", getenv("HOME"));
-               read_history(history_file.c_str());
-               history_offset = where_history();
+               yosys_history_file = stringf("%s/.yosys_history", getenv("HOME"));
+               read_history(yosys_history_file.c_str());
+               yosys_history_offset = where_history();
        }
 #endif
 
@@ -127,14 +234,17 @@ int main(int argc, char **argv)
                printf("    -l logfile\n");
                printf("        write log messages to the specified file\n");
                printf("\n");
+               printf("    -L logfile\n");
+               printf("        like -l but open log file in line buffered mode\n");
+               printf("\n");
                printf("    -o outfile\n");
                printf("        write the design to the specified file on exit\n");
                printf("\n");
                printf("    -b backend\n");
                printf("        use this backend for the output file specified on the command line\n");
                printf("\n");
-               printf("    -f backend\n");
-               printf("        use the specified front for the input files on the command line\n");
+               printf("    -f frontend\n");
+               printf("        use the specified frontend for the input files on the command line\n");
                printf("\n");
                printf("    -H\n");
                printf("        print the command list\n");
@@ -163,11 +273,36 @@ int main(int argc, char **argv)
                printf("    -A\n");
                printf("        will call abort() at the end of the script. for debugging\n");
                printf("\n");
+               printf("    -D <macro>[=<value>]\n");
+               printf("        set the specified Verilog define (via \"read -define\")\n");
+               printf("\n");
+               printf("    -P <header_id>[:<filename>]\n");
+               printf("        dump the design when printing the specified log header to a file.\n");
+               printf("        yosys_dump_<header_id>.il is used as filename if none is specified.\n");
+               printf("        Use 'ALL' as <header_id> to dump at every header.\n");
+               printf("\n");
+               printf("    -W regex\n");
+               printf("        print a warning for all log messages matching the regex.\n");
+               printf("\n");
+               printf("    -w regex\n");
+               printf("        if a warning message matches the regex, it is printed as regular\n");
+               printf("        message instead.\n");
+               printf("\n");
+               printf("    -e regex\n");
+               printf("        if a warning message matches the regex, it is printed as error\n");
+               printf("        message instead and the tool terminates with a nonzero return code.\n");
+               printf("\n");
+               printf("    -E <depsfile>\n");
+               printf("        write a Makefile dependencies file with in- and output file names\n");
+               printf("\n");
+               printf("    -g\n");
+               printf("        globally enable debug log messages\n");
+               printf("\n");
                printf("    -V\n");
                printf("        print version information and exit\n");
                printf("\n");
                printf("The option -S is an shortcut for calling the \"synth\" command, a default\n");
-               printf("script for transforming the verilog input to a gate-level netlist. For example:\n");
+               printf("script for transforming the Verilog input to a gate-level netlist. For example:\n");
                printf("\n");
                printf("    yosys -o output.blif -S input.v\n");
                printf("\n");
@@ -183,7 +318,7 @@ int main(int argc, char **argv)
        }
 
        int opt;
-       while ((opt = getopt(argc, argv, "MXAQTVSm:f:Hh:b:o:p:l:qv:tds:c:")) != -1)
+       while ((opt = getopt(argc, argv, "MXAQTVSgm:f:Hh:b:o:p:l:L:qv:tds:c:W:w:e:D:P:E:")) != -1)
        {
                switch (opt)
                {
@@ -208,6 +343,9 @@ int main(int argc, char **argv)
                case 'S':
                        passes_commands.push_back("synth");
                        break;
+               case 'g':
+                       log_force_debug++;
+                       break;
                case 'm':
                        plugin_filenames.push_back(optarg);
                        break;
@@ -231,11 +369,14 @@ int main(int argc, char **argv)
                        got_output_filename = true;
                        break;
                case 'l':
+               case 'L':
                        log_files.push_back(fopen(optarg, "wt"));
                        if (log_files.back() == NULL) {
                                fprintf(stderr, "Can't open log file `%s' for writing!\n", optarg);
                                exit(1);
                        }
+                       if (opt == 'L')
+                               setvbuf(log_files.back(), NULL, _IOLBF, 0);
                        break;
                case 'q':
                        mode_q = true;
@@ -262,6 +403,52 @@ int main(int argc, char **argv)
                        scriptfile = optarg;
                        scriptfile_tcl = true;
                        break;
+               case 'W':
+                       log_warn_regexes.push_back(std::regex(optarg,
+                                       std::regex_constants::nosubs |
+                                       std::regex_constants::optimize |
+                                       std::regex_constants::egrep));
+                       break;
+               case 'w':
+                       log_nowarn_regexes.push_back(std::regex(optarg,
+                                       std::regex_constants::nosubs |
+                                       std::regex_constants::optimize |
+                                       std::regex_constants::egrep));
+                       break;
+               case 'e':
+                       log_werror_regexes.push_back(std::regex(optarg,
+                                       std::regex_constants::nosubs |
+                                       std::regex_constants::optimize |
+                                       std::regex_constants::egrep));
+                       break;
+               case 'D':
+                       vlog_defines.push_back(optarg);
+                       break;
+               case 'P':
+                       {
+                               auto args = split_tokens(optarg, ":");
+                               if (!args.empty() && args[0] == "ALL") {
+                                       if (GetSize(args) != 1) {
+                                               fprintf(stderr, "Invalid number of tokens in -D ALL.\n");
+                                               exit(1);
+                                       }
+                                       log_hdump_all = true;
+                               } else {
+                                       if (!args.empty() && !args[0].empty() && args[0].back() == '.')
+                                               args[0].pop_back();
+                                       if (GetSize(args) == 1)
+                                               args.push_back("yosys_dump_" + args[0] + ".il");
+                                       if (GetSize(args) != 2) {
+                                               fprintf(stderr, "Invalid number of tokens in -D.\n");
+                                               exit(1);
+                                       }
+                                       log_hdump[args[0]].insert(args[1]);
+                               }
+                       }
+                       break;
+               case 'E':
+                       depsfile = optarg;
+                       break;
                default:
                        fprintf(stderr, "Run '%s -h' for help.\n", argv[0]);
                        exit(1);
@@ -279,7 +466,24 @@ int main(int argc, char **argv)
        if (print_stats)
                log_hasher = new SHA1;
 
+#if defined(__linux__)
+       // set stack size to >= 128 MB
+       {
+               struct rlimit rl;
+               const rlim_t stack_size = 128L * 1024L * 1024L;
+               if (getrlimit(RLIMIT_STACK, &rl) == 0 && rl.rlim_cur < stack_size) {
+                       rl.rlim_cur = stack_size;
+                       setrlimit(RLIMIT_STACK, &rl);
+               }
+       }
+#endif
+
        yosys_setup();
+#ifdef WITH_PYTHON
+       PyRun_SimpleString(("sys.path.append(\""+proc_self_dirname()+"\")").c_str());
+       PyRun_SimpleString(("sys.path.append(\""+proc_share_dirname()+"plugins\")").c_str());
+#endif
+       log_error_atexit = yosys_atexit;
 
        for (auto &fn : plugin_filenames)
                load_plugin(fn, {});
@@ -290,8 +494,15 @@ int main(int argc, char **argv)
                shell(yosys_design);
        }
 
+       if (!vlog_defines.empty()) {
+               std::string vdef_cmd = "read -define";
+               for (auto vdef : vlog_defines)
+                       vdef_cmd += " " + vdef;
+               run_pass(vdef_cmd);
+       }
+
        while (optind < argc)
-               run_frontend(argv[optind++], frontend_command, yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
+               run_frontend(argv[optind++], frontend_command, output_filename == "-" ? &backend_command : NULL);
 
        if (!scriptfile.empty()) {
                if (scriptfile_tcl) {
@@ -302,14 +513,32 @@ int main(int argc, char **argv)
                        log_error("Can't exectue TCL script: this version of yosys is not built with TCL support enabled.\n");
 #endif
                } else
-                       run_frontend(scriptfile, "script", yosys_design, output_filename == "-" ? &backend_command : NULL, NULL);
+                       run_frontend(scriptfile, "script", output_filename == "-" ? &backend_command : NULL);
        }
 
        for (auto it = passes_commands.begin(); it != passes_commands.end(); it++)
-               run_pass(*it, yosys_design);
+               run_pass(*it);
 
        if (!backend_command.empty())
-               run_backend(output_filename, backend_command, yosys_design);
+               run_backend(output_filename, backend_command);
+
+       if (!depsfile.empty())
+       {
+               FILE *f = fopen(depsfile.c_str(), "wt");
+               if (f == nullptr)
+                       log_error("Can't open dependencies file for writing: %s\n", strerror(errno));
+               bool first = true;
+               for (auto fn : yosys_output_files) {
+                       fprintf(f, "%s%s", first ? "" : " ", escape_filename_spaces(fn).c_str());
+                       first = false;
+               }
+               fprintf(f, ":");
+               for (auto fn : yosys_input_files) {
+                       if (yosys_output_files.count(fn) == 0)
+                               fprintf(f, " %s", escape_filename_spaces(fn).c_str());
+               }
+               fprintf(f, "\n");
+       }
 
        if (print_stats)
        {
@@ -324,12 +553,14 @@ int main(int argc, char **argv)
                if (mode_v && !mode_q)
                        log_files.push_back(stderr);
 
+               if (log_warnings_count)
+                       log("Warnings: %d unique messages, %d total\n", GetSize(log_warnings), log_warnings_count);
 #ifdef _WIN32
                log("End of script. Logfile hash: %s\n", hash.c_str());
 #else
                std::string meminfo;
                std::string stats_divider = ", ";
-#  ifdef __linux__
+#  if defined(__linux__)
                std::ifstream statm;
                statm.open(stringf("/proc/%lld/statm", (long long)getpid()));
                if (statm.is_open()) {
@@ -340,6 +571,19 @@ int main(int argc, char **argv)
                                        sz_resident * (getpagesize() / 1024.0 / 1024.0));
                        stats_divider = "\n";
                }
+#  elif defined(__FreeBSD__)
+               pid_t pid = getpid();
+               int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, (int)pid};
+               struct kinfo_proc kip;
+               size_t kip_len = sizeof(kip);
+               if (sysctl(mib, 4, &kip, &kip_len, NULL, 0) == 0) {
+                       vm_size_t sz_total = kip.ki_size;
+                       segsz_t sz_resident = kip.ki_rssize;
+                       meminfo = stringf(", MEM: %.2f MB total, %.2f MB resident",
+                               (int)sz_total / 1024.0 / 1024.0,
+                               (int)sz_resident * (getpagesize() / 1024.0 / 1024.0));
+                       stats_divider = "\n";
+               }
 #  endif
 
                struct rusage ru_buffer;
@@ -351,7 +595,7 @@ int main(int argc, char **argv)
                log("%s\n", yosys_version_str);
 
                int64_t total_ns = 0;
-               std::set<std::tuple<int64_t, int, std::string>> timedat;
+               std::set<tuple<int64_t, int, std::string>> timedat;
 
                for (auto &it : pass_register)
                        if (it.second->call_counter) {
@@ -383,7 +627,7 @@ int main(int argc, char **argv)
                }
        }
 
-#if defined(YOSYS_ENABLE_COVER) && defined(__linux__)
+#if defined(YOSYS_ENABLE_COVER) && (defined(__linux__) || defined(__FreeBSD__))
        if (getenv("YOSYS_COVER_DIR") || getenv("YOSYS_COVER_FILE"))
        {
                string filename;
@@ -410,23 +654,17 @@ int main(int argc, char **argv)
        }
 #endif
 
+       yosys_atexit();
+
        memhasher_off();
        if (call_abort)
                abort();
 
-#ifdef YOSYS_ENABLE_READLINE
-       if (!history_file.empty()) {
-               if (history_offset > 0) {
-                       history_truncate_file(history_file.c_str(), 100);
-                       append_history(where_history() - history_offset, history_file.c_str());
-               } else
-                       write_history(history_file.c_str());
-       }
-
-       clear_history();
-       HIST_ENTRY **hist_list = history_list();
-       if (hist_list != NULL)
-               free(hist_list);
+       log_flush();
+#if defined(_MSC_VER)
+       _exit(0);
+#elif defined(_WIN32)
+       _Exit(0);
 #endif
 
        yosys_shutdown();
@@ -434,3 +672,5 @@ int main(int argc, char **argv)
        return 0;
 }
 
+#endif /* EMSCRIPTEN */
+