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) {...@@ -91,7 +91,7 @@ int main(int argc, char **argv) {
91 InputStream_readByte(&in) != 'm') panic("input is not a zstd-compressed wasm file");91 InputStream_readByte(&in) != 'm') panic("input is not a zstd-compressed wasm file");
92 if (InputStream_readLittle_u32(&in) != 1) panic("unsupported wasm version");92 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");
95 if (out == NULL) panic("unable to open output file");95 if (out == NULL) panic("unable to open output file");
96 fputs("#include <math.h>\n"96 fputs("#include <math.h>\n"
97 "#include <stdint.h>\n"97 "#include <stdint.h>\n"
...@@ -225,14 +225,22 @@ int main(int argc, char **argv) {...@@ -225,14 +225,22 @@ int main(int argc, char **argv) {
225 " return dst;\n"225 " return dst;\n"
226 "}\n"226 "}\n"
227 "\n"227 "\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"
229 " uint32_t r = *p;\n"230 " uint32_t r = *p;\n"
230 " uint32_t new_p = r + n;\n"231 " uint32_t new_p = r + n;\n"
231 " uint8_t *new_m = realloc(*m, new_p << 16);\n"232 " if (new_p > UINT32_C(0x10000)) return UINT32_C(0xFFFFFFF);\n"
232 " if (new_m == NULL) return UINT32_C(0xFFFFFFF);\n"233 " uint32_t new_c = *c;\n"
233 " memset(&new_m[r << 16], 0, n << 16);\n"234 " if (new_c < new_p) {\n"
234 " *m = new_m;\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"
235 " *p = new_p;\n"242 " *p = new_p;\n"
243 " memset(&new_m[r << 16], 0, n << 16);\n"
236 " return r;\n"244 " return r;\n"
237 "}\n"245 "}\n"
238 "\n"246 "\n"
...@@ -369,7 +377,8 @@ int main(int argc, char **argv) {...@@ -369,7 +377,8 @@ int main(int argc, char **argv) {
369 for (uint32_t i = 0; i < mems_len; i += 1) {377 for (uint32_t i = 0; i < mems_len; i += 1) {
370 mems[i].limits = InputStream_readLimits(&in);378 mems[i].limits = InputStream_readLimits(&in);
371 fprintf(out, "static uint8_t *m%" PRIu32 ";\n"379 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);
373 }382 }
374 fputc('\n', out);383 fputc('\n', out);
375 }384 }
...@@ -1319,8 +1328,8 @@ int main(int argc, char **argv) {...@@ -1319,8 +1328,8 @@ int main(int argc, char **argv) {
1319 if (unreachable_depth == 0) {1328 if (unreachable_depth == 0) {
1320 uint32_t pages = FuncGen_stackPop(&fg);1329 uint32_t pages = FuncGen_stackPop(&fg);
1321 FuncGen_stackPush(&fg, out, WasmValType_i32);1330 FuncGen_stackPush(&fg, out, WasmValType_i32);
1322 fprintf(out, "memory_grow(&m%" PRIu32 ", &p%" PRIu32 ", l%" PRIu32 ");\n",1331 fprintf(out, "memory_grow(&m%" PRIu32 ", &p%" PRIu32 ", &c%" PRIu32
1323 mem_idx, mem_idx, pages);1332 ", l%" PRIu32 ");\n", mem_idx, mem_idx, mem_idx, pages);
1324 }1333 }
1325 break;1334 break;
1326 }1335 }
...@@ -2449,8 +2458,9 @@ int main(int argc, char **argv) {...@@ -2449,8 +2458,9 @@ int main(int argc, char **argv) {
2449 fputs("static void init_data(void) {\n", out);2458 fputs("static void init_data(void) {\n", out);
2450 for (uint32_t i = 0; i < mems_len; i += 1)2459 for (uint32_t i = 0; i < mems_len; i += 1)
2451 fprintf(out, " p%" PRIu32 " = UINT32_C(%" PRIu32 ");\n"2460 fprintf(out, " p%" PRIu32 " = UINT32_C(%" PRIu32 ");\n"
2452 " m%" PRIu32 " = calloc(p%" PRIu32 ", UINT32_C(1) << 16);\n",2461 " c%" PRIu32 " = p%" PRIu32 ";\n"
2453 i, mems[i].limits.min, i, i);2462 " m%" PRIu32 " = calloc(c%" PRIu32 ", UINT32_C(1) << 16);\n",
2463 i, mems[i].limits.min, i, i, i, i);
2454 for (uint32_t segment_i = 0; segment_i < len; segment_i += 1) {2464 for (uint32_t segment_i = 0; segment_i < len; segment_i += 1) {
2455 uint32_t mem_idx;2465 uint32_t mem_idx;
2456 switch (InputStream_readLeb128_u32(&in)) {2466 switch (InputStream_readLeb128_u32(&in)) {