aboutsummaryrefslogtreecommitdiff
path: root/tools/wasm_compile.c
diff options
context:
space:
mode:
authorWojciech Kosior <kwojtus@protonmail.com>2020-09-19 21:22:41 +0200
committerWojciech Kosior <kwojtus@protonmail.com>2020-09-19 21:22:41 +0200
commit605ac7f13be9f597d3cf7ab3786b71f5cda8c9fa (patch)
treeb3c7a166f33d9d1ff35cbc1ede202dd89903e81e /tools/wasm_compile.c
parent780f056e61323a41abcaf0dd53a44f99bcac197c (diff)
downloadAGH-engineering-thesis-605ac7f13be9f597d3cf7ab3786b71f5cda8c9fa.tar.gz
AGH-engineering-thesis-605ac7f13be9f597d3cf7ab3786b71f5cda8c9fa.zip
initial work towards translating wasm to stack machine (with a provisional bench)
Diffstat (limited to 'tools/wasm_compile.c')
-rw-r--r--tools/wasm_compile.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/tools/wasm_compile.c b/tools/wasm_compile.c
new file mode 100644
index 0000000..64988c8
--- /dev/null
+++ b/tools/wasm_compile.c
@@ -0,0 +1,72 @@
+#include "wasm_compile.h"
+
+void print_instructions(uint32_t count, uint16_t instructions[count])
+{
+ uint32_t i;
+ char binary[17];
+ uint16_t instruction;
+ int j;
+
+ binary[16] = '\0';
+
+ for (i = 0; i < count; i++) {
+ instruction = instructions[i];
+ j = 16;
+
+ while (j--) {
+ binary[j] = (instruction & 1) ? '1' : '0';
+ instruction >>= 1;
+ }
+
+ puts(binary);
+ }
+}
+
+int main(int argc, char **argv)
+{
+ FILE *handle = NULL;
+ struct module *module = NULL;
+ uint16_t *translated_instructions = NULL;
+ char retval = -1;
+
+ if (argc < 2) {
+ PRERR("Please provide name of the file to parse on the command line\n");
+ goto fail;
+ }
+
+ handle = fopen(argv[1], "r");
+
+ if (!handle) {
+ PRERR("Couldn't open '%s'\n", argv[1]);
+ goto fail;
+ }
+
+ module = parse_module(handle);
+
+ if (!module)
+ goto fail;
+
+ translated_instructions = calloc(1, CODE_TOP_ADDR);
+
+ if (!translated_instructions) {
+ PRERR(MSG_ALLOC_FAIL(CODE_TOP_ADDR));
+ goto fail;
+ }
+
+ if (assemble(CODE_TOP_ADDR / 2, translated_instructions, module))
+ goto fail;
+
+ print_instructions(CODE_TOP_ADDR / 2, translated_instructions);
+
+ retval = 0;
+
+fail:
+ if (handle)
+ fclose(handle);
+
+ free_module(module);
+
+ free(translated_instructions);
+
+ return retval;
+}