authorgravatar for ennui@langeweile.pageEnnui Langeweile <ennui@langeweile.page> 2026-07-28 23:22:15-03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-07-30 19:06:55+02:00
logd632f2a5b43e2e7adba1b742d16ce15d74a0accb
tree4d53df0b7ade302706a6a566ffc5b32a19de5ad2
parentb6361fa84a5c8f217af5771e345387752fd5c581

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.

2 files changed, 12 insertions(+), 12 deletions(-)

stage1/FuncGen.h+3-3
...@@ -52,17 +52,17 @@ static void FuncGen_free(struct FuncGen *self) {...@@ -52,17 +52,17 @@ static void FuncGen_free(struct FuncGen *self) {
52}52}
5353
54static void FuncGen_outdent(struct FuncGen *self, FILE *out) {54static void FuncGen_outdent(struct FuncGen *self, FILE *out) {
55 for (uint32_t i = 0; i < self->block_i; i += 1) fputs(" ", out);55 for (uint32_t i = 0; i < self->block_i; i += 1) fputs(" ", out);
56}56}
5757
58static void FuncGen_indent(struct FuncGen *self, FILE *out) {58static void FuncGen_indent(struct FuncGen *self, FILE *out) {
59 FuncGen_outdent(self, out);59 FuncGen_outdent(self, out);
60 fputs(" ", out);60 fputs(" ", out);
61}61}
6262
63static void FuncGen_cont(struct FuncGen *self, FILE *out) {63static void FuncGen_cont(struct FuncGen *self, FILE *out) {
64 FuncGen_indent(self, out);64 FuncGen_indent(self, out);
65 fputs(" ", out);65 fputs(" ", out);
66}66}
6767
68static uint32_t FuncGen_localAlloc(struct FuncGen *self, int8_t type) {68static uint32_t FuncGen_localAlloc(struct FuncGen *self, int8_t type) {
stage1/wasm2c.c+9-9
...@@ -518,8 +518,8 @@ int main(int argc, char **argv) {...@@ -518,8 +518,8 @@ int main(int argc, char **argv) {
518 }518 }
519 fprintf(out,519 fprintf(out,
520 ") {\n"520 ") {\n"
521 " init();\n"521 " init();\n"
522 " %sf%" PRIu32 "(",522 " %sf%" PRIu32 "(",
523 func_type->result->len > 0 ? "return " : "", idx - imports_len);523 func_type->result->len > 0 ? "return " : "", idx - imports_len);
524 for (uint32_t param_i = 0; param_i < func_type->param->len; param_i += 1) {524 for (uint32_t param_i = 0; param_i < func_type->param->len; param_i += 1) {
525 if (param_i > 0) fputs(", ", out);525 if (param_i > 0) fputs(", ", out);
...@@ -552,7 +552,7 @@ int main(int argc, char **argv) {...@@ -552,7 +552,7 @@ int main(int argc, char **argv) {
552 uint32_t segment_len = InputStream_readLeb128_u32(&in);552 uint32_t segment_len = InputStream_readLeb128_u32(&in);
553 for (uint32_t i = 0; i < segment_len; i += 1) {553 for (uint32_t i = 0; i < segment_len; i += 1) {
554 uint32_t func_id = InputStream_readLeb128_u32(&in);554 uint32_t func_id = InputStream_readLeb128_u32(&in);
555 fprintf(out, " t%" PRIu32 "[UINT32_C(%" PRIu32 ")] = (void (*)(void))&",555 fprintf(out, " t%" PRIu32 "[UINT32_C(%" PRIu32 ")] = (void (*)(void))&",
556 table_idx, offset + i);556 table_idx, offset + i);
557 if (func_id < imports_len)557 if (func_id < imports_len)
558 fprintf(out, "%s_%s", imports[func_id].mod, imports[func_id].name);558 fprintf(out, "%s_%s", imports[func_id].mod, imports[func_id].name);
...@@ -2260,9 +2260,9 @@ int main(int argc, char **argv) {...@@ -2260,9 +2260,9 @@ int main(int argc, char **argv) {
2260 uint32_t len = InputStream_readLeb128_u32(&in);2260 uint32_t len = InputStream_readLeb128_u32(&in);
2261 fputs("static void init_data(void) {\n", out);2261 fputs("static void init_data(void) {\n", out);
2262 for (uint32_t i = 0; i < mems_len; i += 1)2262 for (uint32_t i = 0; i < mems_len; i += 1)
2263 fprintf(out, " p%" PRIu32 " = UINT32_C(%" PRIu32 ");\n"2263 fprintf(out, " p%" PRIu32 " = UINT32_C(%" PRIu32 ");\n"
2264 " c%" PRIu32 " = p%" PRIu32 ";\n"2264 " c%" PRIu32 " = p%" PRIu32 ";\n"
2265 " m%" PRIu32 " = calloc(c%" PRIu32 ", UINT32_C(1) << 16);\n",2265 " m%" PRIu32 " = calloc(c%" PRIu32 ", UINT32_C(1) << 16);\n",
2266 i, mems[i].limits.min, i, i, i, i);2266 i, mems[i].limits.min, i, i, i, i);
2267 for (uint32_t segment_i = 0; segment_i < len; segment_i += 1) {2267 for (uint32_t segment_i = 0; segment_i < len; segment_i += 1) {
2268 uint32_t mem_idx;2268 uint32_t mem_idx;
...@@ -2280,15 +2280,15 @@ int main(int argc, char **argv) {...@@ -2280,15 +2280,15 @@ int main(int argc, char **argv) {
2280 uint32_t offset = evalExpr(&in);2280 uint32_t offset = evalExpr(&in);
2281 uint32_t segment_len = InputStream_readLeb128_u32(&in);2281 uint32_t segment_len = InputStream_readLeb128_u32(&in);
2282 fputc('\n', out);2282 fputc('\n', out);
2283 fprintf(out, " static const uint8_t s%" PRIu32 "[UINT32_C(%" PRIu32 ")] = {",2283 fprintf(out, " static const uint8_t s%" PRIu32 "[UINT32_C(%" PRIu32 ")] = {",
2284 segment_i, segment_len);2284 segment_i, segment_len);
2285 for (uint32_t i = 0; i < segment_len; i += 1) {2285 for (uint32_t i = 0; i < segment_len; i += 1) {
2286 if (i % 32 == 0) fputs("\n ", out);2286 if (i % 32 == 0) fputs("\n ", out);
2287 fprintf(out, " 0x%02hhX,", InputStream_readByte(&in));2287 fprintf(out, " 0x%02hhX,", InputStream_readByte(&in));
2288 }2288 }
2289 fprintf(out, "\n"2289 fprintf(out, "\n"
2290 " };\n"2290 " };\n"
2291 " memcpy(&m%" PRIu32 "[UINT32_C(0x%" PRIX32 ")], s%" PRIu32 ", UINT32_C(%" PRIu32 "));\n",2291 " memcpy(&m%" PRIu32 "[UINT32_C(0x%" PRIX32 ")], s%" PRIu32 ", UINT32_C(%" PRIu32 "));\n",
2292 mem_idx, offset, segment_i, segment_len);2292 mem_idx, offset, segment_i, segment_len);
2293 }2293 }
2294 fputs("}\n", out);2294 fputs("}\n", out);