# CONFIG := gcc-4.8
 # CONFIG := afl-gcc
 # CONFIG := emcc
+# CONFIG := wasi
 # CONFIG := mxe
 # CONFIG := msys2
 # CONFIG := msys2-64
 LINK_CURSES := 0
 LINK_TERMCAP := 0
 LINK_ABC := 0
-# Needed for environments that don't have proper thread support (i.e. emscripten)
+# Needed for environments that can't run executables (i.e. emscripten, wasm)
+DISABLE_SPAWN := 0
+# Needed for environments that don't have proper thread support (i.e. emscripten, wasm--for now)
 DISABLE_ABC_THREADS := 0
 
 # clang sanitizers
 # SANITIZER = undefined
 # SANITIZER = cfi
 
-PROGRAM_PREFIX := 
+PROGRAM_PREFIX :=
 
 OS := $(shell uname -s)
 PREFIX ?= /usr/local
 LDLIBS =
 EXE = .js
 
+DISABLE_SPAWN := 1
+
 TARGETS := $(filter-out $(PROGRAM_PREFIX)yosys-config,$(TARGETS))
 EXTRA_TARGETS += yosysjs-$(YOSYS_VER).zip
 
 yosys.html: misc/yosys.html
        $(P) cp misc/yosys.html yosys.html
 
+else ifeq ($(CONFIG),wasi)
+ifeq ($(WASI_SDK),)
+CXX = clang++
+LD = clang++
+AR = llvm-ar
+RANLIB = llvm-ranlib
+WASIFLAGS := -target wasm32-wasi --sysroot $(WASI_SYSROOT) $(WASIFLAGS)
+else
+CXX = $(WASI_SDK)/bin/clang++
+LD = $(WASI_SDK)/bin/clang++
+AR = $(WASI_SDK)/bin/ar
+RANLIB = $(WASI_SDK)/bin/ranlib
+WASIFLAGS := --sysroot $(WASI_SDK)/share/wasi-sysroot $(WASIFLAGS)
+endif
+CXXFLAGS := $(WASIFLAGS) -std=c++11 -Os $(filter-out -fPIC,$(CXXFLAGS))
+LDFLAGS := $(WASIFLAGS) -Wl,-z,stack-size=1048576 $(filter-out -rdynamic,$(LDFLAGS))
+LDLIBS := $(filter-out -lrt,$(LDLIBS))
+ABCMKARGS += AR="$(AR)" RANLIB="$(RANLIB)"
+ABCMKARGS += ARCHFLAGS="$(WASIFLAGS) -DABC_USE_STDINT_H -DABC_NO_DYNAMIC_LINKING"
+ABCMKARGS += OPTFLAGS="-Os"
+EXE = .wasm
+
+DISABLE_SPAWN := 1
+
+ifeq ($(ENABLE_ABC),1)
+LINK_ABC := 1
+DISABLE_ABC_THREADS := 1
+endif
+
 else ifeq ($(CONFIG),mxe)
 PKG_CONFIG = /usr/local/src/mxe/usr/bin/i686-w64-mingw32.static-pkg-config
 CXX = /usr/local/src/mxe/usr/bin/i686-w64-mingw32.static-g++
 ABCMKARGS += "ABC_USE_NO_PTHREADS=1"
 endif
 
+ifeq ($(DISABLE_SPAWN),1)
+CXXFLAGS += -DYOSYS_DISABLE_SPAWN
+endif
+
 ifeq ($(ENABLE_PLUGINS),1)
 CXXFLAGS += $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKG_CONFIG) --silence-errors --cflags libffi) -DYOSYS_ENABLE_PLUGINS
 LDLIBS += $(shell PKG_CONFIG_PATH=$(PKG_CONFIG_PATH) $(PKG_CONFIG) --silence-errors --libs libffi || echo -lffi)
        echo 'ENABLE_READLINE := 0' >> Makefile.conf
        echo 'ENABLE_ZLIB := 0' >> Makefile.conf
 
+config-wasi: clean
+       echo 'CONFIG := wasi' > Makefile.conf
+       echo 'ENABLE_TCL := 0' >> Makefile.conf
+       echo 'ENABLE_ABC := 0' >> Makefile.conf
+       echo 'ENABLE_PLUGINS := 0' >> Makefile.conf
+       echo 'ENABLE_READLINE := 0' >> Makefile.conf
+       echo 'ENABLE_ZLIB := 0' >> Makefile.conf
+
 config-mxe: clean
        echo 'CONFIG := mxe' > Makefile.conf
        echo 'ENABLE_PLUGINS := 0' >> Makefile.conf
 
-ifneq ($(CONFIG),emcc)
+ifeq ($(DISABLE_SPAWN),0)
 OBJS += frontends/rpc/rpc_frontend.o
 endif
 
 std::string yosys_history_file;
 #endif
 
+#if defined(__wasm)
+extern "C" {
+       // FIXME: WASI does not currently support exceptions.
+       void* __cxa_allocate_exception(size_t thrown_size) throw() {
+               return malloc(thrown_size);
+       }
+       bool __cxa_uncaught_exception() throw();
+       void __cxa_throw(void* thrown_exception, struct std::type_info * tinfo, void (*dest)(void*)) {
+               std::terminate();
+       }
+}
+#endif
+
 void yosys_atexit()
 {
 #if defined(YOSYS_ENABLE_READLINE) || defined(YOSYS_ENABLE_EDITLINE)
                        ru_buffer.ru_utime.tv_usec += ru_buffer_children.ru_utime.tv_usec;
                        ru_buffer.ru_stime.tv_sec += ru_buffer_children.ru_stime.tv_sec;
                        ru_buffer.ru_stime.tv_usec += ru_buffer_children.ru_stime.tv_usec;
+#if defined(__linux__) || defined(__FreeBSD__)
                        ru_buffer.ru_maxrss = std::max(ru_buffer.ru_maxrss, ru_buffer_children.ru_maxrss);
+#endif
                }
-#  if defined(__linux__) || defined(__FreeBSD__)
+#if defined(__linux__) || defined(__FreeBSD__)
                meminfo = stringf(", MEM: %.2f MB peak",
                                ru_buffer.ru_maxrss / 1024.0);
 #endif
 
                return;
 
        if (tok[0] == '!') {
+#if !defined(YOSYS_DISABLE_SPAWN)
                cmd_buf = command.substr(command.find('!') + 1);
                while (!cmd_buf.empty() && (cmd_buf.back() == ' ' || cmd_buf.back() == '\t' ||
                                cmd_buf.back() == '\r' || cmd_buf.back() == '\n'))
                if (retCode != 0)
                        log_cmd_error("Shell command returned error code %d.\n", retCode);
                return;
+#else
+               log_cmd_error("Shell is not available.\n");
+#endif
        }
 
        while (!tok.empty()) {
 
 #  include <unistd.h>
 #  include <dirent.h>
 #  include <sys/types.h>
-#  include <sys/wait.h>
 #  include <sys/stat.h>
+#  if !defined(YOSYS_DISABLE_SPAWN)
+#    include <sys/wait.h>
+#  endif
 #endif
 
 #if !defined(_WIN32) && defined(YOSYS_ENABLE_GLOB)
        return false;
 }
 
+#if !defined(YOSYS_DISABLE_SPAWN)
 int run_command(const std::string &command, std::function<void(const std::string&)> process_line)
 {
        if (!process_line)
                return system(command.c_str());
 
-#ifdef EMSCRIPTEN
-       FILE *f = nullptr;
-#else
        FILE *f = popen(command.c_str(), "r");
-#endif
        if (f == nullptr)
                return -1;
 
        return WEXITSTATUS(ret);
 #endif
 }
+#endif
 
 std::string make_temp_file(std::string template_str)
 {
-#ifdef _WIN32
+#if defined(__wasm)
+       size_t pos = template_str.rfind("XXXXXX");
+       log_assert(pos != std::string::npos);
+       static size_t index = 0;
+       template_str.replace(pos, 6, stringf("%06zu", index++));
+#elif defined(_WIN32)
        if (template_str.rfind("/tmp/", 0) == 0) {
 #  ifdef __MINGW32__
                char longpath[MAX_PATH + 1];
 
 std::string make_temp_dir(std::string template_str)
 {
-#ifdef _WIN32
+#if defined(_WIN32)
        template_str = make_temp_file(template_str);
        mkdir(template_str.c_str());
        return template_str;
+#elif defined(__wasm)
+       template_str = make_temp_file(template_str);
+       mkdir(template_str.c_str(), 0777);
+       return template_str;
 #else
 #  ifndef NDEBUG
        size_t pos = template_str.rfind("XXXXXX");
                path += char(shortpath[i]);
        return path;
 }
-#elif defined(EMSCRIPTEN)
+#elif defined(EMSCRIPTEN) || defined(__wasm)
 std::string proc_self_dirname()
 {
        return "/";
        #error "Don't know how to determine process executable base path!"
 #endif
 
-#ifdef EMSCRIPTEN
+#if defined(EMSCRIPTEN) || defined(__wasm)
 std::string proc_share_dirname()
 {
        return "/share/";
 
 std::string next_token(std::string &text, const char *sep = " \t\r\n", bool long_strings = false);
 std::vector<std::string> split_tokens(const std::string &text, const char *sep = " \t\r\n");
 bool patmatch(const char *pattern, const char *string);
+#if !defined(YOSYS_DISABLE_SPAWN)
 int run_command(const std::string &command, std::function<void(const std::string&)> process_line = std::function<void(const std::string&)>());
+#endif
 std::string make_temp_file(std::string template_str = "/tmp/yosys_XXXXXX");
 std::string make_temp_dir(std::string template_str = "/tmp/yosys_XXXXXX");
 bool check_file_exists(std::string filename, bool is_exec = false);
 
 
 #include <limits.h>
 #include <stdint.h>
-#include <csignal>
 #include <cinttypes>
 
-#ifndef _WIN32
+#if !defined(_WIN32) && !defined(__wasm)
+#  include <csignal>
 #  include <unistd.h>
+#  define HAS_ALARM
 #endif
 
 #include "../minisat/Solver.h"
 }
 #endif
 
-#ifndef _WIN32
+#if defined(HAS_ALARM)
 ezMiniSAT *ezMiniSAT::alarmHandlerThis = NULL;
 clock_t ezMiniSAT::alarmHandlerTimeout = 0;
 
 #endif
        }
 
-#ifndef _WIN32
+#if defined(HAS_ALARM)
        struct sigaction sig_action;
        struct sigaction old_sig_action;
        int old_alarm_timeout = 0;
 
        bool foundSolution = minisatSolver->solve(assumps);
 
-#ifndef _WIN32
+#if defined(HAS_ALARM)
        if (solverTimeout > 0) {
                if (alarmHandlerTimeout == 0)
                        solverTimoutStatus = true;
 
--- /dev/null
+--- System.cc
++++ System.cc
+@@ -101,7 +101,7 @@ double Minisat::memUsedPeak(bool) { return 0; }
+ #endif
+
+
+-#if !defined(_MSC_VER) && !defined(__MINGW32__)
++#if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__wasm)
+ void Minisat::limitMemory(uint64_t max_mem_mb)
+ {
+ // FIXME: OpenBSD does not support RLIMIT_AS. Not sure how well RLIMIT_DATA works instead.
+@@ -133,7 +133,7 @@ void Minisat::limitMemory(uint64_t /*max_mem_mb*/)
+ #endif
+
+
+-#if !defined(_MSC_VER) && !defined(__MINGW32__)
++#if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__wasm)
+ void Minisat::limitTime(uint32_t max_cpu_time)
+ {
+     if (max_cpu_time != 0){
+@@ -156,9 +156,13 @@ void Minisat::limitTime(uint32_t /*max_cpu_time*/)
+
+ void Minisat::sigTerm(void handler(int))
+ {
++#if defined(__wasm)
++    (void)handler;
++#else
+     signal(SIGINT, handler);
+     signal(SIGTERM,handler);
+ #ifdef SIGXCPU
+     signal(SIGXCPU,handler);
+ #endif
++#endif
+ }
 
 patch -p0 < 00_PATCH_remove_zlib.patch
 patch -p0 < 00_PATCH_no_fpu_control.patch
 patch -p0 < 00_PATCH_typofixes.patch
-
+patch -p0 < 00_PATCH_wasm.patch
 
 #endif
 
 
-#if !defined(_MSC_VER) && !defined(__MINGW32__)
+#if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__wasm)
 void Minisat::limitMemory(uint64_t max_mem_mb)
 {
 // FIXME: OpenBSD does not support RLIMIT_AS. Not sure how well RLIMIT_DATA works instead.
 #endif
 
 
-#if !defined(_MSC_VER) && !defined(__MINGW32__)
+#if !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__wasm)
 void Minisat::limitTime(uint32_t max_cpu_time)
 {
     if (max_cpu_time != 0){
 
 void Minisat::sigTerm(void handler(int))
 {
+#if defined(__wasm)
+    (void)handler;
+#else
     signal(SIGINT, handler);
     signal(SIGTERM,handler);
 #ifdef SIGXCPU
     signal(SIGXCPU,handler);
 #endif
+#endif
 }
 
 
+ifeq ($(DISABLE_SPAWN),0)
 OBJS += passes/cmds/exec.o
+endif
 OBJS += passes/cmds/add.o
 OBJS += passes/cmds/delete.o
 OBJS += passes/cmds/design.o
 OBJS += passes/cmds/chtype.o
 OBJS += passes/cmds/blackbox.o
 OBJS += passes/cmds/ltp.o
+ifeq ($(DISABLE_SPAWN),0)
 OBJS += passes/cmds/bugpoint.o
+endif
 OBJS += passes/cmds/scratchpad.o
 OBJS += passes/cmds/logger.o
 
                                const std::string &filename = args[++argidx];
                                FILE *f = nullptr;
                                if (args[argidx-1] == "-d") {
-                       #ifdef _WIN32
-                                       log_cmd_error("The 'cover -d' option is not supported on win32.\n");
+                       #if defined(_WIN32) || defined(__wasm)
+                                       log_cmd_error("The 'cover -d' option is not supported on this platform.\n");
                        #else
                                        char filename_buffer[4096];
                                        snprintf(filename_buffer, 4096, "%s/yosys_cover_%d_XXXXXX.txt", filename.c_str(), getpid());
 
                std::vector<std::pair<std::string, RTLIL::Selection>> color_selections;
                std::vector<std::pair<std::string, RTLIL::Selection>> label_selections;
 
-#if defined(EMSCRIPTEN) || defined(_WIN32)
+#if defined(_WIN32) || defined(YOSYS_DISABLE_SPAWN)
                std::string format = "dot";
                std::string prefix = "show";
 #else
                        std::string cmd = stringf(DOT_CMD, format.c_str(), dot_file.c_str(), out_file.c_str(), out_file.c_str(), out_file.c_str());
                        #undef DOT_CMD
                        log("Exec: %s\n", cmd.c_str());
-                       if (run_command(cmd) != 0)
-                               log_cmd_error("Shell command failed!\n");
+                       #if !defined(YOSYS_DISABLE_SPAWN)
+                               if (run_command(cmd) != 0)
+                                       log_cmd_error("Shell command failed!\n");
+                       #endif
                }
 
+               #if defined(YOSYS_DISABLE_SPAWN)
+                       log_assert(viewer_exe.empty() && !format.empty());
+               #else
                if (!viewer_exe.empty()) {
                        #ifdef _WIN32
                                // system()/cmd.exe does not understand single quotes nor
                        if (run_command(cmd) != 0)
                                log_cmd_error("Shell command failed!\n");
                }
+               #endif
 
                if (flag_pause) {
                #ifdef YOSYS_ENABLE_READLINE
 
 OBJS += passes/sat/mutate.o
 OBJS += passes/sat/cutpoint.o
 OBJS += passes/sat/fminit.o
+ifeq ($(DISABLE_SPAWN),0)
 OBJS += passes/sat/qbfsat.o
-
+endif
 
 
 passes/techmap/techmap.o: passes/techmap/techmap.inc
 
-ifneq ($(CONFIG),emcc)
+ifeq ($(DISABLE_SPAWN),0)
 TARGETS += $(PROGRAM_PREFIX)yosys-filterlib$(EXE)
 EXTRA_OBJS += passes/techmap/filterlib.o
 
 
                if (abc_script[i] == ';' && abc_script[i+1] == ' ')
                        abc_script[i+1] = '\n';
 
-       FILE *f = fopen(stringf("%s/abc.script", tempdir_name.c_str()).c_str(), "wt");
+       std::string buffer = stringf("%s/abc.script", tempdir_name.c_str());
+       FILE *f = fopen(buffer.c_str(), "wt");
+       if (f == nullptr)
+               log_error("Opening %s for writing failed: %s\n", buffer.c_str(), strerror(errno));
        fprintf(f, "%s\n", abc_script.c_str());
        fclose(f);
 
 
        handle_loops();
 
-       std::string buffer = stringf("%s/input.blif", tempdir_name.c_str());
+       buffer = stringf("%s/input.blif", tempdir_name.c_str());
        f = fopen(buffer.c_str(), "wt");
        if (f == nullptr)
                log_error("Opening %s for writing failed: %s\n", buffer.c_str(), strerror(errno));
 
                size_t argidx, g_argidx;
                bool g_arg_from_cmd = false;
+#if defined(__wasm)
+               const char *pwd = ".";
+#else
                char pwd [PATH_MAX];
                if (!getcwd(pwd, sizeof(pwd))) {
                        log_cmd_error("getcwd failed: %s\n", strerror(errno));
                        log_abort();
                }
+#endif
                for (argidx = 1; argidx < args.size(); argidx++) {
                        std::string arg = args[argidx];
                        if (arg == "-exe" && argidx+1 < args.size()) {
 
                }
 
                size_t argidx;
+#if defined(__wasm)
+               const char *pwd = ".";
+#else
                char pwd [PATH_MAX];
                if (!getcwd(pwd, sizeof(pwd))) {
                        log_cmd_error("getcwd failed: %s\n", strerror(errno));
                        log_abort();
                }
+#endif
                for (argidx = 1; argidx < args.size(); argidx++) {
                        std::string arg = args[argidx];
                        if (arg == "-exe" && argidx+1 < args.size()) {