diff options
Diffstat (limited to 'tools/wasm_compile.h')
-rw-r--r-- | tools/wasm_compile.h | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/tools/wasm_compile.h b/tools/wasm_compile.h new file mode 100644 index 0000000..3412b2d --- /dev/null +++ b/tools/wasm_compile.h @@ -0,0 +1,76 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <stdint.h> + +#define STACK_FRAME_BACKUP_ADDR 0x0FFFFC +#define STACK_TOP_ADDR 0x0FFFFC +#define CODE_BOTTOM_ADDR 0x000000 +#define CODE_TOP_ADDR 0x000200 +#define MEMORY_BOTTOM_ADDR 0x000200 + +/* Error messages */ +#define MSG_SIZE_OVERFLOW "Number overflows size_t\n" +#define MSG_EOF "Unexpected end of bytes\n" +#define MSG_BAD_NUM_ENC "Improper number encoding\n" +#define MSG_BAD_NUM "Couldn't parse number\n" +#define MSG_BAD_SIZE "Couldn't compute size to allocate\n" +#define MSG_ALLOC_FAIL(n) "Failed to allocate %lu bytes\n", (unsigned long) (n) +#define MSG_BAD(nam, val) "Found 0x%02hhx instead of %s\n", (char) (val), nam +#define MSG_BYTES_REMAIN "Didn't use up all bytes when parsing\n" +#define MSG_SEEK_FAIL "Couldn't navigate the file\n" +#define MSG_BAD_IDX(nam) "Got %s out of range\n", nam + +#define PRERR(...) fprintf(stderr, __VA_ARGS__) + +struct functype { + uint32_t arguments_count; + char *arguments; + + char result; +}; + +struct function { + struct functype *type; + + uint32_t locals_count; + char *locals; + + struct instruction *translated_body; + uint32_t start_addr; +}; + +struct export { + char *name; + char desc; + uint32_t idx; +}; + +struct module { + uint32_t functypes_count; + struct functype *functypes; + uint32_t functions_count; + struct function *functions; + enum { + MEM_NONE = 0, + MEM_MIN, + MEM_MIN_MAX + } memory_type; + uint32_t mem_min, mem_max; + uint32_t exports_count; + struct export *exports; +}; + +int leb_u32(FILE *handle, uint32_t *result); + +void free_expr(struct instruction *expr); + +void free_module(struct module *module); + +struct module *parse_module(FILE *handle); + +int translate(FILE *handle, struct function *function, struct module *module); + +int assemble(uint32_t memory_size, uint16_t memory[memory_size], + struct module *module); |