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()...@@ -731,9 +731,6 @@ else()
731 set(ZIG_WASM2C_COMPILE_FLAGS "-std=c99 -O2")731 set(ZIG_WASM2C_COMPILE_FLAGS "-std=c99 -O2")
732 set(ZIG1_COMPILE_FLAGS "-std=c99 -Os")732 set(ZIG1_COMPILE_FLAGS "-std=c99 -Os")
733 set(ZIG2_COMPILE_FLAGS "-std=c99 -O0")733 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()
737 if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang")734 if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
738 set(ZIG2_LINK_FLAGS "-Wl,-stack_size,0x10000000")735 set(ZIG2_LINK_FLAGS "-Wl,-stack_size,0x10000000")
739 else()736 else()
stage1/FuncGen.h+15-5
...@@ -119,9 +119,15 @@ static void FuncGen_blockBegin(struct FuncGen *self, FILE *out, enum WasmOpcode...@@ -119,9 +119,15 @@ static void FuncGen_blockBegin(struct FuncGen *self, FILE *out, enum WasmOpcode
119 if (self->block == NULL) panic("out of memory");119 if (self->block == NULL) panic("out of memory");
120 }120 }
121 uint32_t label = FuncGen_localAlloc(self, type < 0 ? ~(int8_t)kind : (int8_t)kind);121 uint32_t label = FuncGen_localAlloc(self, type < 0 ? ~(int8_t)kind : (int8_t)kind);
122 FuncGen_indent(self, out);122
123 if (kind == WasmOpcode_if) fprintf(out, "if (l%" PRIu32 ") ", FuncGen_stackPop(self));123 if (kind == WasmOpcode_if) {
124 fputs("{\n", out);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
125 self->block[self->block_i].type = type < 0 ? ~type : type;131 self->block[self->block_i].type = type < 0 ? ~type : type;
126 self->block[self->block_i].label = label;132 self->block[self->block_i].label = label;
127 self->block[self->block_i].stack_i = self->stack_i;133 self->block[self->block_i].stack_i = self->stack_i;
...@@ -148,8 +154,12 @@ static void FuncGen_blockEnd(struct FuncGen *self, FILE *out) {...@@ -148,8 +154,12 @@ static void FuncGen_blockEnd(struct FuncGen *self, FILE *out) {
148 uint32_t label = FuncGen_blockLabel(self, 0);154 uint32_t label = FuncGen_blockLabel(self, 0);
149 if (kind != WasmOpcode_loop) FuncGen_label(self, out, label);155 if (kind != WasmOpcode_loop) FuncGen_label(self, out, label);
150 self->block_i -= 1;156 self->block_i -= 1;
151 FuncGen_indent(self, out);157
152 fputs("}\n", out);158 if (EXTRA_BRACES || kind == WasmOpcode_if) {
159 FuncGen_indent(self, out);
160 fputs("}\n", out);
161 }
162
153 if (self->stack_i != self->block[self->block_i].stack_i) {163 if (self->stack_i != self->block[self->block_i].stack_i) {
154 FuncGen_indent(self, out);164 FuncGen_indent(self, out);
155 fprintf(out, "// stack mismatch %u != %u\n", self->stack_i, self->block[self->block_i].stack_i);165 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,3 +1,5 @@
1#define EXTRA_BRACES 0
2
1#include "FuncGen.h"3#include "FuncGen.h"
2#include "InputStream.h"4#include "InputStream.h"
3#include "panic.h"5#include "panic.h"
...@@ -800,10 +802,14 @@ int main(int argc, char **argv) {...@@ -800,10 +802,14 @@ int main(int argc, char **argv) {
800 FuncType_blockType(types, FuncGen_blockType(&fg, label_idx));802 FuncType_blockType(types, FuncGen_blockType(&fg, label_idx));
801 uint32_t label = FuncGen_blockLabel(&fg, label_idx);803 uint32_t label = FuncGen_blockLabel(&fg, label_idx);
802804
803 FuncGen_indent(&fg, out);805 if (opcode == WasmOpcode_br_if) {
804 if (opcode == WasmOpcode_br_if)806 FuncGen_indent(&fg, out);
805 fprintf(out, "if (l%" PRIu32 ") ", FuncGen_stackPop(&fg));807 fprintf(out, "if (l%" PRIu32 ") {\n", FuncGen_stackPop(&fg));
806 fputs("{\n", out);808 } else if (EXTRA_BRACES) {
809 FuncGen_indent(&fg, out);
810 fputs("{\n", out);
811 }
812
807 const struct ResultType *label_type;813 const struct ResultType *label_type;
808 uint32_t lhs;814 uint32_t lhs;
809 switch (kind) {815 switch (kind) {
...@@ -833,8 +839,10 @@ int main(int argc, char **argv) {...@@ -833,8 +839,10 @@ int main(int argc, char **argv) {
833 }839 }
834 FuncGen_cont(&fg, out);840 FuncGen_cont(&fg, out);
835 fprintf(out, "goto l%" PRIu32 ";\n", label);841 fprintf(out, "goto l%" PRIu32 ";\n", label);
836 FuncGen_indent(&fg, out);842 if (EXTRA_BRACES || opcode == WasmOpcode_br_if) {
837 fprintf(out, "}\n");843 FuncGen_indent(&fg, out);
844 fputs("}\n", out);
845 }
838 if (opcode == WasmOpcode_br) unreachable_depth += 1;846 if (opcode == WasmOpcode_br) unreachable_depth += 1;
839 }847 }
840 break;848 break;