Commit 9f4ef4de23

Jacob Young <jacobly0@users.noreply.github.com>
2022-12-05 09:44:35
wasm2c: remove unnecessary brackets to reduce max bracket depth
This avoids the need to pass `-fbracket-depth=512` to clang.
1 parent fdb98c5
Changed files (3)
stage1/FuncGen.h
@@ -119,9 +119,15 @@ static void FuncGen_blockBegin(struct FuncGen *self, FILE *out, enum WasmOpcode
         if (self->block == NULL) panic("out of memory");
     }
     uint32_t label = FuncGen_localAlloc(self, type < 0 ? ~(int8_t)kind : (int8_t)kind);
-    FuncGen_indent(self, out);
-    if (kind == WasmOpcode_if) fprintf(out, "if (l%" PRIu32 ") ", FuncGen_stackPop(self));
-    fputs("{\n", out);
+
+    if (kind == WasmOpcode_if) {
+        FuncGen_indent(self, out);
+        fprintf(out, "if (l%" PRIu32 ") {\n", FuncGen_stackPop(self));
+    } else if (EXTRA_BRACES) {
+        FuncGen_indent(self, out);
+        fputs("{\n", out);
+    }
+
     self->block[self->block_i].type = type < 0 ? ~type : type;
     self->block[self->block_i].label = label;
     self->block[self->block_i].stack_i = self->stack_i;
@@ -148,8 +154,12 @@ static void FuncGen_blockEnd(struct FuncGen *self, FILE *out) {
     uint32_t label = FuncGen_blockLabel(self, 0);
     if (kind != WasmOpcode_loop) FuncGen_label(self, out, label);
     self->block_i -= 1;
-    FuncGen_indent(self, out);
-    fputs("}\n", out);
+
+    if (EXTRA_BRACES || kind == WasmOpcode_if) {
+        FuncGen_indent(self, out);
+        fputs("}\n", out);
+    }
+
     if (self->stack_i != self->block[self->block_i].stack_i) {
         FuncGen_indent(self, out);
         fprintf(out, "// stack mismatch %u != %u\n", self->stack_i, self->block[self->block_i].stack_i);
stage1/wasm2c.c
@@ -1,3 +1,5 @@
+#define EXTRA_BRACES 0
+
 #include "FuncGen.h"
 #include "InputStream.h"
 #include "panic.h"
@@ -800,10 +802,14 @@ int main(int argc, char **argv) {
                                 FuncType_blockType(types, FuncGen_blockType(&fg, label_idx));
                             uint32_t label = FuncGen_blockLabel(&fg, label_idx);
 
-                            FuncGen_indent(&fg, out);
-                            if (opcode == WasmOpcode_br_if)
-                                fprintf(out, "if (l%" PRIu32 ") ", FuncGen_stackPop(&fg));
-                            fputs("{\n", out);
+                            if (opcode == WasmOpcode_br_if) {
+                                FuncGen_indent(&fg, out);
+                                fprintf(out, "if (l%" PRIu32 ") {\n", FuncGen_stackPop(&fg));
+                            } else if (EXTRA_BRACES) {
+                                FuncGen_indent(&fg, out);
+                                fputs("{\n", out);
+                            }
+
                             const struct ResultType *label_type;
                             uint32_t lhs;
                             switch (kind) {
@@ -833,8 +839,10 @@ int main(int argc, char **argv) {
                             }
                             FuncGen_cont(&fg, out);
                             fprintf(out, "goto l%" PRIu32 ";\n", label);
-                            FuncGen_indent(&fg, out);
-                            fprintf(out, "}\n");
+                            if (EXTRA_BRACES || opcode == WasmOpcode_br_if) {
+                                FuncGen_indent(&fg, out);
+                                fputs("}\n", out);
+                            }
                             if (opcode == WasmOpcode_br) unreachable_depth += 1;
                         }
                         break;
CMakeLists.txt
@@ -731,9 +731,6 @@ else()
   set(ZIG_WASM2C_COMPILE_FLAGS "-std=c99 -O2")
   set(ZIG1_COMPILE_FLAGS "-std=c99 -Os")
   set(ZIG2_COMPILE_FLAGS "-std=c99 -O0")
-  if(CMAKE_C_COMPILER_ID MATCHES "Clang")
-    set(ZIG1_COMPILE_FLAGS "${ZIG1_COMPILE_FLAGS} -fbracket-depth=512")
-  endif()
   if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang")
     set(ZIG2_LINK_FLAGS "-Wl,-stack_size,0x10000000")
   else()