From b6361fa84a5c8f217af5771e345387752fd5c581 Mon Sep 17 00:00:00 2001 From: Ennui Langeweile Date: Tue, 28 Jul 2026 22:51:49 -0300 Subject: [PATCH 1/2] Enable `extended-const` extension in stage1 Considering Debian 12 has Binaryen 108, we can now remove the explicit exclusion of `extended-const` (v107). --- build.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.zig b/build.zig index 0bd1abe03359bdd5c84086c95286568022a53bc4..74ef42a775985c90dd8a5b6b80a0926e4bc6012b 100644 --- a/build.zig +++ b/build.zig @@ -763,9 +763,8 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void { .optimize = .ReleaseSmall, .target = b.resolveTargetQuery(std.Target.Query.parse(.{ .arch_os_abi = "wasm32-wasi", - // * `extended_const` is not supported by the `wasm-opt` version in CI. // * `nontrapping_bulk_memory_len0` is supported by `wasm2c`. - .cpu_features = "baseline-extended_const+nontrapping_bulk_memory_len0", + .cpu_features = "baseline+nontrapping_bulk_memory_len0", }) catch unreachable), }); @@ -809,6 +808,7 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void { "-Oz", "--enable-bulk-memory", "--enable-mutable-globals", + "--enable-extended-const", "--enable-nontrapping-float-to-int", "--enable-sign-ext", }); -- 2.54.0 From d632f2a5b43e2e7adba1b742d16ce15d74a0accb Mon Sep 17 00:00:00 2001 From: Ennui Langeweile Date: Tue, 28 Jul 2026 23:22:15 -0300 Subject: [PATCH 2/2] Use single-space indentation for wasm2c's generated code This pretty much halves the file size of the generated C source code. I have decided to not touch the initial code since I don't believe it would be that beneficial. --- stage1/FuncGen.h | 6 +++--- stage1/wasm2c.c | 18 +++++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/stage1/FuncGen.h b/stage1/FuncGen.h index 494dd202b224fdb927efc3119f670fafa1def207..5f62b249862ac29656d7d1231d55504b93746fd4 100644 --- a/stage1/FuncGen.h +++ b/stage1/FuncGen.h @@ -52,17 +52,17 @@ static void FuncGen_free(struct FuncGen *self) { } static void FuncGen_outdent(struct FuncGen *self, FILE *out) { - for (uint32_t i = 0; i < self->block_i; i += 1) fputs(" ", out); + for (uint32_t i = 0; i < self->block_i; i += 1) fputs(" ", out); } static void FuncGen_indent(struct FuncGen *self, FILE *out) { FuncGen_outdent(self, out); - fputs(" ", out); + fputs(" ", out); } static void FuncGen_cont(struct FuncGen *self, FILE *out) { FuncGen_indent(self, out); - fputs(" ", out); + fputs(" ", out); } static uint32_t FuncGen_localAlloc(struct FuncGen *self, int8_t type) { diff --git a/stage1/wasm2c.c b/stage1/wasm2c.c index adbf8667e5fbc8d6a4a0c56c26d78ea2185543bb..8363a332b4063ae37bafa10f53832aea91e56c03 100644 --- a/stage1/wasm2c.c +++ b/stage1/wasm2c.c @@ -518,8 +518,8 @@ int main(int argc, char **argv) { } fprintf(out, ") {\n" - " init();\n" - " %sf%" PRIu32 "(", + " init();\n" + " %sf%" PRIu32 "(", func_type->result->len > 0 ? "return " : "", idx - imports_len); for (uint32_t param_i = 0; param_i < func_type->param->len; param_i += 1) { if (param_i > 0) fputs(", ", out); @@ -552,7 +552,7 @@ int main(int argc, char **argv) { uint32_t segment_len = InputStream_readLeb128_u32(&in); for (uint32_t i = 0; i < segment_len; i += 1) { uint32_t func_id = InputStream_readLeb128_u32(&in); - fprintf(out, " t%" PRIu32 "[UINT32_C(%" PRIu32 ")] = (void (*)(void))&", + fprintf(out, " t%" PRIu32 "[UINT32_C(%" PRIu32 ")] = (void (*)(void))&", table_idx, offset + i); if (func_id < imports_len) fprintf(out, "%s_%s", imports[func_id].mod, imports[func_id].name); @@ -2260,9 +2260,9 @@ int main(int argc, char **argv) { uint32_t len = InputStream_readLeb128_u32(&in); fputs("static void init_data(void) {\n", out); for (uint32_t i = 0; i < mems_len; i += 1) - fprintf(out, " p%" PRIu32 " = UINT32_C(%" PRIu32 ");\n" - " c%" PRIu32 " = p%" PRIu32 ";\n" - " m%" PRIu32 " = calloc(c%" PRIu32 ", UINT32_C(1) << 16);\n", + fprintf(out, " p%" PRIu32 " = UINT32_C(%" PRIu32 ");\n" + " c%" PRIu32 " = p%" PRIu32 ";\n" + " m%" PRIu32 " = calloc(c%" PRIu32 ", UINT32_C(1) << 16);\n", i, mems[i].limits.min, i, i, i, i); for (uint32_t segment_i = 0; segment_i < len; segment_i += 1) { uint32_t mem_idx; @@ -2280,15 +2280,15 @@ int main(int argc, char **argv) { uint32_t offset = evalExpr(&in); uint32_t segment_len = InputStream_readLeb128_u32(&in); fputc('\n', out); - fprintf(out, " static const uint8_t s%" PRIu32 "[UINT32_C(%" PRIu32 ")] = {", + fprintf(out, " static const uint8_t s%" PRIu32 "[UINT32_C(%" PRIu32 ")] = {", segment_i, segment_len); for (uint32_t i = 0; i < segment_len; i += 1) { if (i % 32 == 0) fputs("\n ", out); fprintf(out, " 0x%02hhX,", InputStream_readByte(&in)); } fprintf(out, "\n" - " };\n" - " memcpy(&m%" PRIu32 "[UINT32_C(0x%" PRIX32 ")], s%" PRIu32 ", UINT32_C(%" PRIu32 "));\n", + " };\n" + " memcpy(&m%" PRIu32 "[UINT32_C(0x%" PRIX32 ")], s%" PRIu32 ", UINT32_C(%" PRIu32 "));\n", mem_idx, offset, segment_i, segment_len); } fputs("}\n", out); -- 2.54.0