authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-12-05 03:44:35-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-06 12:15:04-07:00
log9f4ef4de23927f724413efa6ef54c2cd08a05976
tree8463c0e7dbbb4d1c4410a8a568b620e46cb0800b
parentfdb98c5ce121e80582aee9854bbe2543af725a74

wasm2c: remove unnecessary brackets to reduce max bracket depth

This avoids the need to pass `-fbracket-depth=512` to clang.

3 files changed, 29 insertions(+), 14 deletions(-)

CMakeLists.txt-3
......@@ -731,9 +731,6 @@ else()
731731 set(ZIG_WASM2C_COMPILE_FLAGS "-std=c99 -O2")
732732 set(ZIG1_COMPILE_FLAGS "-std=c99 -Os")
733733 set(ZIG2_COMPILE_FLAGS "-std=c99 -O0")
734 if(CMAKE_C_COMPILER_ID MATCHES "Clang")
735 set(ZIG1_COMPILE_FLAGS "${ZIG1_COMPILE_FLAGS} -fbracket-depth=512")
736 endif()
737734 if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
738735 set(ZIG2_LINK_FLAGS "-Wl,-stack_size,0x10000000")
739736 else()
stage1/FuncGen.h+15-5
......@@ -119,9 +119,15 @@ static void FuncGen_blockBegin(struct FuncGen *self, FILE *out, enum WasmOpcode
119119 if (self->block == NULL) panic("out of memory");
120120 }
121121 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);
122
123 if (kind == WasmOpcode_if) {
124 FuncGen_indent(self, out);
125 fprintf(out, "if (l%" PRIu32 ") {\n", FuncGen_stackPop(self));
126 } else if (EXTRA_BRACES) {
127 FuncGen_indent(self, out);
128 fputs("{\n", out);
129 }
130
125131 self->block[self->block_i].type = type < 0 ? ~type : type;
126132 self->block[self->block_i].label = label;
127133 self->block[self->block_i].stack_i = self->stack_i;
......@@ -148,8 +154,12 @@ static void FuncGen_blockEnd(struct FuncGen *self, FILE *out) {
148154 uint32_t label = FuncGen_blockLabel(self, 0);
149155 if (kind != WasmOpcode_loop) FuncGen_label(self, out, label);
150156 self->block_i -= 1;
151 FuncGen_indent(self, out);
152 fputs("}\n", out);
157
158 if (EXTRA_BRACES || kind == WasmOpcode_if) {
159 FuncGen_indent(self, out);
160 fputs("}\n", out);
161 }
162
153163 if (self->stack_i != self->block[self->block_i].stack_i) {
154164 FuncGen_indent(self, out);
155165 fprintf(out, "// stack mismatch %u != %u\n", self->stack_i, self->block[self->block_i].stack_i);
stage1/wasm2c.c+14-6
......@@ -1,3 +1,5 @@
1#define EXTRA_BRACES 0
2
13#include "FuncGen.h"
24#include "InputStream.h"
35#include "panic.h"
......@@ -800,10 +802,14 @@ int main(int argc, char **argv) {
800802 FuncType_blockType(types, FuncGen_blockType(&fg, label_idx));
801803 uint32_t label = FuncGen_blockLabel(&fg, label_idx);
802804
803 FuncGen_indent(&fg, out);
804 if (opcode == WasmOpcode_br_if)
805 fprintf(out, "if (l%" PRIu32 ") ", FuncGen_stackPop(&fg));
806 fputs("{\n", out);
805 if (opcode == WasmOpcode_br_if) {
806 FuncGen_indent(&fg, out);
807 fprintf(out, "if (l%" PRIu32 ") {\n", FuncGen_stackPop(&fg));
808 } else if (EXTRA_BRACES) {
809 FuncGen_indent(&fg, out);
810 fputs("{\n", out);
811 }
812
807813 const struct ResultType *label_type;
808814 uint32_t lhs;
809815 switch (kind) {
......@@ -833,8 +839,10 @@ int main(int argc, char **argv) {
833839 }
834840 FuncGen_cont(&fg, out);
835841 fprintf(out, "goto l%" PRIu32 ";\n", label);
836 FuncGen_indent(&fg, out);
837 fprintf(out, "}\n");
842 if (EXTRA_BRACES || opcode == WasmOpcode_br_if) {
843 FuncGen_indent(&fg, out);
844 fputs("}\n", out);
845 }
838846 if (opcode == WasmOpcode_br) unreachable_depth += 1;
839847 }
840848 break;