diff options
author | Ben Smith <binjimin@gmail.com> | 2018-01-08 17:35:01 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-08 17:35:01 -0800 |
commit | 6fc756c4f1a5b7c0209c5dad0c0ec51b3b504df7 (patch) | |
tree | 80cc03a9efaf5fa05e81f4ad1e0f6c9833f17788 /test/utils.py | |
parent | 1f735e8f5125f84abba2a91d4f44bc8ee30544a8 (diff) | |
download | wabt-6fc756c4f1a5b7c0209c5dad0c0ec51b3b504df7.tar.gz wabt-6fc756c4f1a5b7c0209c5dad0c0ec51b3b504df7.tar.bz2 wabt-6fc756c4f1a5b7c0209c5dad0c0ec51b3b504df7.zip |
Add wasm2c tool (#710)
Add `wasm2c`, a new tool that reads a `.wasm` file and generates a C
source file and its accompanying header file. The C output currently
only supports gcc/clang compilers, since it uses builtins for some
functionality.
The resulting C code is not standalone; there are runtime functions that
must be provided, as well as pointers to all imports.
The C runtime symbols that must be provided are as follows:
* `void wasm_rt_trap(wasm_rt_trap_t code)`:
Called when the WebAssembly code traps. This function must not return.
* `u32 wasm_rt_register_func_type(u32 param_count, u32 result_count, ...)`:
Register a function type with the given signature. This function must
check whether this signature has already been registered and return
the original index.
* `void wasm_rt_allocate_memory(wasm_rt_memory_t*, u32 initial, u32 max)`:
Allocate the memory buffer for the given memory object, given the
number of pages. The memory must be zeroed before returning.
* `u32 wasm_rt_grow_memory(wasm_rt_memory_t*, u32 delta)`:
Grow memory by the given number of pages. If allocation fails, or the
new pages size is larger than the maximum, return -1. Otherwise return
the previous number of pages. The newly allocated memory must be
zeroed.
* `void wasm_rt_allocate_table(wasm_rt_table_t*, u32 initial, u32 max)`:
Allocate the buffer for the given table object. The buffer must be
zeroed before returning.
* `u32 wasm_rt_call_stack_depth`:
A symbol that tracks the current call stack depth. If this value
exceeds `WASM_RT_MAX_CALL_STACK_DEPTH` then a trap occurs. This value
defaults to 500, but can redefined.
An example implementation can be found in `spec-wasm2c-prefix.c`.
All functionality from the WebAssembly MVP is supported, and the
generated code passes all of the core spec tests. There is a new test
tool called `run-spec-wasm2c.py` which runs the following:
* `wast2json` to convert the spec test to json and wasm files
* `wasm2c` to convert the wasm to C source and headers
* a C compiler (default `cc`) to compile and link all C source files,
including a C test runner (`spec-wasm2c-prefix.c`)
* Finally, the resulting executable to produce output
Diffstat (limited to 'test/utils.py')
-rw-r--r-- | test/utils.py | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/test/utils.py b/test/utils.py index d2bf1c2c..00ece562 100644 --- a/test/utils.py +++ b/test/utils.py @@ -46,8 +46,13 @@ class Executable(object): self.error_cmdline = kwargs.get('error_cmdline', True) self.clean_stdout = kwargs.get('clean_stdout') self.clean_stderr = kwargs.get('clean_stderr') + self.stdout_handle = self._ForwardHandle(kwargs.get('forward_stdout')) + self.stderr_handle = self._ForwardHandle(kwargs.get('forward_stderr')) self.verbose = False + def _ForwardHandle(self, forward): + return None if forward else subprocess.PIPE + def _RunWithArgsInternal(self, *args, **kwargs): cmd = [self.exe] + self.before_args + list(args) + self.after_args cmd_str = ' '.join(cmd) @@ -63,11 +68,13 @@ class Executable(object): stderr = '' error = None try: - process = subprocess.Popen(cmd, stdout=subprocess.PIPE, - stderr=subprocess.PIPE, **kwargs) + process = subprocess.Popen(cmd, stdout=self.stdout_handle, + stderr=self.stderr_handle, **kwargs) stdout, stderr = process.communicate() - stdout = stdout.decode('utf-8', 'ignore') - stderr = stderr.decode('utf-8', 'ignore') + if stdout: + stdout = stdout.decode('utf-8', 'ignore') + if stderr: + stderr = stderr.decode('utf-8', 'ignore') if self.clean_stdout: stdout = self.clean_stdout(stdout) if self.clean_stderr: @@ -91,7 +98,8 @@ class Executable(object): def RunWithArgs(self, *args, **kwargs): stdout, stderr, error = self._RunWithArgsInternal(*args, **kwargs) - sys.stdout.write(stdout) + if stdout: + sys.stdout.write(stdout) if error: raise error |