Merge branch 'koriakin/xc7nocarrymux' into xaig
[yosys.git] / kernel / driver.cc
index 7a1dce4970b2a1ad33371cbbdf8593f2f8453d2a..f273057dd765d4872efcfa3f38a4be92a93fceca 100644 (file)
 #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
@@ -80,20 +85,41 @@ 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, char**)
+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)
@@ -157,6 +183,7 @@ 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 = "";
@@ -246,7 +273,10 @@ 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 <header_id>[:<filename>]\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");
@@ -265,6 +295,9 @@ int main(int argc, char **argv)
                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");
@@ -285,7 +318,7 @@ int main(int argc, char **argv)
        }
 
        int opt;
-       while ((opt = getopt(argc, argv, "MXAQTVSm:f:Hh:b:o:p:l:L:qv:tds:c:W:w:e:D:E:")) != -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)
                {
@@ -310,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;
@@ -386,6 +422,9 @@ int main(int argc, char **argv)
                                        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") {
@@ -440,6 +479,10 @@ int main(int argc, char **argv)
 #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)
@@ -451,6 +494,13 @@ 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, output_filename == "-" ? &backend_command : NULL);
 
@@ -479,13 +529,13 @@ int main(int argc, char **argv)
                        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 ? "" : " ", fn.c_str());
+                       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", fn.c_str());
+                               fprintf(f, " %s", escape_filename_spaces(fn).c_str());
                }
                fprintf(f, "\n");
        }
@@ -510,7 +560,7 @@ int main(int argc, char **argv)
 #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()) {
@@ -521,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;
@@ -564,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;