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) {
5252}
5353
5454static 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);
5656}
5757
5858static void FuncGen_indent(struct FuncGen *self, FILE *out) {
5959 FuncGen_outdent(self, out);
60 fputs(" ", out);
60 fputs(" ", out);
6161}
6262
6363static void FuncGen_cont(struct FuncGen *self, FILE *out) {
6464 FuncGen_indent(self, out);
65 fputs(" ", out);
65 fputs(" ", out);
6666}
6767
6868static 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) {
518518 }
519519 fprintf(out,
520520 ") {\n"
521 " init();\n"
522 " %sf%" PRIu32 "(",
521 " init();\n"
522 " %sf%" PRIu32 "(",
523523 func_type->result->len > 0 ? "return " : "", idx - imports_len);
524524 for (uint32_t param_i = 0; param_i < func_type->param->len; param_i += 1) {
525525 if (param_i > 0) fputs(", ", out);
......@@ -552,7 +552,7 @@ int main(int argc, char **argv) {
552552 uint32_t segment_len = InputStream_readLeb128_u32(&in);
553553 for (uint32_t i = 0; i < segment_len; i += 1) {
554554 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))&",
556556 table_idx, offset + i);
557557 if (func_id < imports_len)
558558 fprintf(out, "%s_%s", imports[func_id].mod, imports[func_id].name);
......@@ -2260,9 +2260,9 @@ int main(int argc, char **argv) {
22602260 uint32_t len = InputStream_readLeb128_u32(&in);
22612261 fputs("static void init_data(void) {\n", out);
22622262 for (uint32_t i = 0; i < mems_len; i += 1)
2263 fprintf(out, " p%" PRIu32 " = UINT32_C(%" PRIu32 ");\n"
2264 " c%" PRIu32 " = p%" PRIu32 ";\n"
2265 " m%" PRIu32 " = calloc(c%" PRIu32 ", UINT32_C(1) << 16);\n",
2263 fprintf(out, " p%" PRIu32 " = UINT32_C(%" PRIu32 ");\n"
2264 " c%" PRIu32 " = p%" PRIu32 ";\n"
2265 " m%" PRIu32 " = calloc(c%" PRIu32 ", UINT32_C(1) << 16);\n",
22662266 i, mems[i].limits.min, i, i, i, i);
22672267 for (uint32_t segment_i = 0; segment_i < len; segment_i += 1) {
22682268 uint32_t mem_idx;
......@@ -2280,15 +2280,15 @@ int main(int argc, char **argv) {
22802280 uint32_t offset = evalExpr(&in);
22812281 uint32_t segment_len = InputStream_readLeb128_u32(&in);
22822282 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 ")] = {",
22842284 segment_i, segment_len);
22852285 for (uint32_t i = 0; i < segment_len; i += 1) {
22862286 if (i % 32 == 0) fputs("\n ", out);
22872287 fprintf(out, " 0x%02hhX,", InputStream_readByte(&in));
22882288 }
22892289 fprintf(out, "\n"
2290 " };\n"
2291 " memcpy(&m%" PRIu32 "[UINT32_C(0x%" PRIX32 ")], s%" PRIu32 ", UINT32_C(%" PRIu32 "));\n",
2290 " };\n"
2291 " memcpy(&m%" PRIu32 "[UINT32_C(0x%" PRIX32 ")], s%" PRIu32 ", UINT32_C(%" PRIu32 "));\n",
22922292 mem_idx, offset, segment_i, segment_len);
22932293 }
22942294 fputs("}\n", out);