|
| 1 | +#ifndef FUNC_GEN_H |
| 2 | +#define FUNC_GEN_H |
| 3 | + |
| 4 | +#include "panic.h" |
| 5 | +#include "wasm.h" |
| 6 | + |
| 7 | +#include <inttypes.h> |
| 8 | +#include <stdbool.h> |
| 9 | +#include <stdint.h> |
| 10 | +#include <stdio.h> |
| 11 | +#include <string.h> |
| 12 | + |
| 13 | +struct Block { |
| 14 | + uint32_t type; |
| 15 | + uint32_t label; |
| 16 | + uint32_t stack_i; |
| 17 | +}; |
| 18 | + |
| 19 | +struct FuncGen { |
| 20 | + int8_t *type; |
| 21 | + uint32_t *stack; |
| 22 | + struct Block *block; |
| 23 | + uint32_t type_i; |
| 24 | + uint32_t stack_i; |
| 25 | + uint32_t block_i; |
| 26 | + uint32_t type_len; |
| 27 | + uint32_t stack_len; |
| 28 | + uint32_t block_len; |
| 29 | +}; |
| 30 | + |
| 31 | +static void FuncGen_init(struct FuncGen *self) { |
| 32 | + memset(self, 0, sizeof(struct FuncGen)); |
| 33 | +} |
| 34 | + |
| 35 | +static void FuncGen_reset(struct FuncGen *self) { |
| 36 | + self->type_i = 0; |
| 37 | + self->stack_i = 0; |
| 38 | + self->block_i = 0; |
| 39 | +} |
| 40 | + |
| 41 | +static void FuncGen_free(struct FuncGen *self) { |
| 42 | + free(self->block); |
| 43 | + free(self->stack); |
| 44 | + free(self->type); |
| 45 | +} |
| 46 | + |
| 47 | +static void FuncGen_outdent(struct FuncGen *self, FILE *out) { |
| 48 | + for (uint32_t i = 0; i < self->block_i; i += 1) fputs(" ", out); |
| 49 | +} |
| 50 | + |
| 51 | +static void FuncGen_indent(struct FuncGen *self, FILE *out) { |
| 52 | + FuncGen_outdent(self, out); |
| 53 | + fputs(" ", out); |
| 54 | +} |
| 55 | + |
| 56 | +static void FuncGen_cont(struct FuncGen *self, FILE *out) { |
| 57 | + FuncGen_indent(self, out); |
| 58 | + fputs(" ", out); |
| 59 | +} |
| 60 | + |
| 61 | +static uint32_t FuncGen_localAlloc(struct FuncGen *self, int8_t type) { |
| 62 | + if (self->type_i == self->type_len) { |
| 63 | + self->type_len += 10; |
| 64 | + self->type_len *= 2; |
| 65 | + self->type = realloc(self->type, sizeof(int8_t) * self->type_len); |
| 66 | + if (self->type == NULL) panic("out of memory"); |
| 67 | + } |
| 68 | + uint32_t local_i = self->type_i; |
| 69 | + self->type[local_i] = type; |
| 70 | + self->type_i += 1; |
| 71 | + return local_i; |
| 72 | +} |
| 73 | + |
| 74 | +static uint32_t FuncGen_localDeclare(struct FuncGen *self, FILE *out, enum WasmValType val_type) { |
| 75 | + uint32_t local_i = FuncGen_localAlloc(self, (int8_t)val_type); |
| 76 | + fprintf(out, "%s l%" PRIu32, WasmValType_toC(val_type), local_i); |
| 77 | + return local_i; |
| 78 | +} |
| 79 | + |
| 80 | +static enum WasmValType FuncGen_localType(const struct FuncGen *self, uint32_t local_idx) { |
| 81 | + return self->type[local_idx]; |
| 82 | +} |
| 83 | + |
| 84 | +static void FuncGen_stackPush(struct FuncGen *self, FILE *out, enum WasmValType val_type) { |
| 85 | + if (self->stack_i == self->stack_len) { |
| 86 | + self->stack_len += 10; |
| 87 | + self->stack_len *= 2; |
| 88 | + self->stack = realloc(self->stack, sizeof(uint32_t) * self->stack_len); |
| 89 | + if (self->stack == NULL) panic("out of memory"); |
| 90 | + } |
| 91 | + FuncGen_indent(self, out); |
| 92 | + fputs("const ", out); |
| 93 | + self->stack[self->stack_i] = FuncGen_localDeclare(self, out, val_type); |
| 94 | + self->stack_i += 1; |
| 95 | + fputs(" = ", out); |
| 96 | +} |
| 97 | + |
| 98 | +static uint32_t FuncGen_stackAt(const struct FuncGen *self, uint32_t stack_idx) { |
| 99 | + return self->stack[self->stack_i - 1 - stack_idx]; |
| 100 | +} |
| 101 | + |
| 102 | +static uint32_t FuncGen_stackPop(struct FuncGen *self) { |
| 103 | + self->stack_i -= 1; |
| 104 | + return self->stack[self->stack_i]; |
| 105 | +} |
| 106 | + |
| 107 | +static void FuncGen_label(struct FuncGen *self, FILE *out, uint32_t label) { |
| 108 | + FuncGen_indent(self, out); |
| 109 | + fprintf(out, "goto l%" PRIu32 ";\n", label); |
| 110 | + FuncGen_outdent(self, out); |
| 111 | + fprintf(out, "l%" PRIu32 ":;\n", label); |
| 112 | +} |
| 113 | + |
| 114 | +static void FuncGen_blockBegin(struct FuncGen *self, FILE *out, enum WasmOpcode kind, int64_t type) { |
| 115 | + if (self->block_i == self->block_len) { |
| 116 | + self->block_len += 10; |
| 117 | + self->block_len *= 2; |
| 118 | + self->block = realloc(self->block, sizeof(struct Block) * self->block_len); |
| 119 | + if (self->block == NULL) panic("out of memory"); |
| 120 | + } |
| 121 | + uint32_t label = FuncGen_localAlloc(self, type < 0 ? ~(int8_t)kind : (int8_t)kind); |
| 122 | + FuncGen_indent(self, out); |
| 123 | + if (kind == WasmOpcode_if) fprintf(out, "if (l%" PRIu32 ") ", FuncGen_stackPop(self)); |
| 124 | + fputs("{\n", out); |
| 125 | + self->block[self->block_i].type = type < 0 ? ~type : type; |
| 126 | + self->block[self->block_i].label = label; |
| 127 | + self->block[self->block_i].stack_i = self->stack_i; |
| 128 | + self->block_i += 1; |
| 129 | + if (kind == WasmOpcode_loop) FuncGen_label(self, out, label); |
| 130 | +} |
| 131 | + |
| 132 | +static enum WasmOpcode FuncGen_blockKind(const struct FuncGen *self, uint32_t label_idx) { |
| 133 | + int8_t kind = self->type[self->block[self->block_i - 1 - label_idx].label]; |
| 134 | + return (enum WasmOpcode)(kind < 0 ? ~kind : kind); |
| 135 | +} |
| 136 | + |
| 137 | +static int64_t FuncGen_blockType(const struct FuncGen *self, uint32_t label_idx) { |
| 138 | + struct Block *block = &self->block[self->block_i - 1 - label_idx]; |
| 139 | + return self->type[block->label] < 0 ? ~(int64_t)block->type : (int64_t)block->type; |
| 140 | +} |
| 141 | + |
| 142 | +static uint32_t FuncGen_blockLabel(const struct FuncGen *self, uint32_t label_idx) { |
| 143 | + return self->block[self->block_i - 1 - label_idx].label; |
| 144 | +} |
| 145 | + |
| 146 | +static void FuncGen_blockEnd(struct FuncGen *self, FILE *out) { |
| 147 | + enum WasmOpcode kind = FuncGen_blockKind(self, 0); |
| 148 | + uint32_t label = FuncGen_blockLabel(self, 0); |
| 149 | + if (kind != WasmOpcode_loop) FuncGen_label(self, out, label); |
| 150 | + self->block_i -= 1; |
| 151 | + FuncGen_indent(self, out); |
| 152 | + fputs("}\n", out); |
| 153 | + if (self->stack_i != self->block[self->block_i].stack_i) { |
| 154 | + FuncGen_indent(self, out); |
| 155 | + fprintf(out, "// stack mismatch %u != %u\n", self->stack_i, self->block[self->block_i].stack_i); |
| 156 | + } |
| 157 | + self->stack_i = self->block[self->block_i].stack_i; |
| 158 | +} |
| 159 | + |
| 160 | +static bool FuncGen_done(const struct FuncGen *self) { |
| 161 | + return self->block_i == 0; |
| 162 | +} |
| 163 | + |
| 164 | +#endif /* FUNC_GEN_H */ |
0 commit comments