authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-12-06 02:16:29-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-06 12:27:28-07:00
log3683602226f6675edae8c38e2a43b0626783267b
treeeffd89ed4983f09e3102094a447e838324f4be3c
parent3686787f6751867dc2bc64c4088b56e11d06fd03

wasm2c: improve amortized speed of memory.grow


1 files changed, 21 insertions(+), 11 deletions(-)

stage1/wasm2c.c+21-11
......@@ -91,7 +91,7 @@ int main(int argc, char **argv) {
9191 InputStream_readByte(&in) != 'm') panic("input is not a zstd-compressed wasm file");
9292 if (InputStream_readLittle_u32(&in) != 1) panic("unsupported wasm version");
9393
94 FILE *out = fopen(argv[2], "w");
94 FILE *out = fopen(argv[2], "wb");
9595 if (out == NULL) panic("unable to open output file");
9696 fputs("#include <math.h>\n"
9797 "#include <stdint.h>\n"
......@@ -225,14 +225,22 @@ int main(int argc, char **argv) {
225225 " return dst;\n"
226226 "}\n"
227227 "\n"
228 "static uint32_t memory_grow(uint8_t **m, uint32_t *p, uint32_t n) {\n"
228 "static uint32_t memory_grow(uint8_t **m, uint32_t *p, uint32_t *c, uint32_t n) {\n"
229 " uint8_t *new_m = *m;\n"
229230 " uint32_t r = *p;\n"
230231 " uint32_t new_p = r + n;\n"
231 " uint8_t *new_m = realloc(*m, new_p << 16);\n"
232 " if (new_m == NULL) return UINT32_C(0xFFFFFFF);\n"
233 " memset(&new_m[r << 16], 0, n << 16);\n"
234 " *m = new_m;\n"
232 " if (new_p > UINT32_C(0x10000)) return UINT32_C(0xFFFFFFF);\n"
233 " uint32_t new_c = *c;\n"
234 " if (new_c < new_p) {\n"
235 " do new_c += new_c / 2 + 8; while (new_c < new_p);\n"
236 " if (new_c > UINT32_C(0x10000)) new_c = UINT32_C(0x10000);\n"
237 " new_m = realloc(new_m, new_c << 16);\n"
238 " if (new_m == NULL) return UINT32_C(0xFFFFFFF);\n"
239 " *m = new_m;\n"
240 " *c = new_c;\n"
241 " }\n"
235242 " *p = new_p;\n"
243 " memset(&new_m[r << 16], 0, n << 16);\n"
236244 " return r;\n"
237245 "}\n"
238246 "\n"
......@@ -369,7 +377,8 @@ int main(int argc, char **argv) {
369377 for (uint32_t i = 0; i < mems_len; i += 1) {
370378 mems[i].limits = InputStream_readLimits(&in);
371379 fprintf(out, "static uint8_t *m%" PRIu32 ";\n"
372 "static uint32_t p%" PRIu32 ";\n", i, i);
380 "static uint32_t p%" PRIu32 ";\n"
381 "static uint32_t c%" PRIu32 ";\n", i, i, i);
373382 }
374383 fputc('\n', out);
375384 }
......@@ -1319,8 +1328,8 @@ int main(int argc, char **argv) {
13191328 if (unreachable_depth == 0) {
13201329 uint32_t pages = FuncGen_stackPop(&fg);
13211330 FuncGen_stackPush(&fg, out, WasmValType_i32);
1322 fprintf(out, "memory_grow(&m%" PRIu32 ", &p%" PRIu32 ", l%" PRIu32 ");\n",
1323 mem_idx, mem_idx, pages);
1331 fprintf(out, "memory_grow(&m%" PRIu32 ", &p%" PRIu32 ", &c%" PRIu32
1332 ", l%" PRIu32 ");\n", mem_idx, mem_idx, mem_idx, pages);
13241333 }
13251334 break;
13261335 }
......@@ -2449,8 +2458,9 @@ int main(int argc, char **argv) {
24492458 fputs("static void init_data(void) {\n", out);
24502459 for (uint32_t i = 0; i < mems_len; i += 1)
24512460 fprintf(out, " p%" PRIu32 " = UINT32_C(%" PRIu32 ");\n"
2452 " m%" PRIu32 " = calloc(p%" PRIu32 ", UINT32_C(1) << 16);\n",
2453 i, mems[i].limits.min, i, i);
2461 " c%" PRIu32 " = p%" PRIu32 ";\n"
2462 " m%" PRIu32 " = calloc(c%" PRIu32 ", UINT32_C(1) << 16);\n",
2463 i, mems[i].limits.min, i, i, i, i);
24542464 for (uint32_t segment_i = 0; segment_i < len; segment_i += 1) {
24552465 uint32_t mem_idx;
24562466 switch (InputStream_readLeb128_u32(&in)) {