| ... | @@ -20,6 +20,8 @@ | ... | @@ -20,6 +20,8 @@ |
| 20 | #include <sys/random.h> | 20 | #include <sys/random.h> |
| 21 | #endif | 21 | #endif |
| 22 | | 22 | |
| | 23 | #include <zstd.h> |
| | 24 | |
| 23 | enum wasi_errno_t { | 25 | enum wasi_errno_t { |
| 24 | WASI_ESUCCESS = 0, | 26 | WASI_ESUCCESS = 0, |
| 25 | WASI_E2BIG = 1, | 27 | WASI_E2BIG = 1, |
| ... | @@ -4122,7 +4124,12 @@ int main(int argc, char **argv) { | ... | @@ -4122,7 +4124,12 @@ int main(int argc, char **argv) { |
| 4122 | | 4124 | |
| 4123 | new_argv[new_argv_i] = NULL; | 4125 | new_argv[new_argv_i] = NULL; |
| 4124 | | 4126 | |
| 4125 | const struct ByteSlice mod = read_file_alloc(wasm_file); | 4127 | const struct ByteSlice compressed_bytes = read_file_alloc(wasm_file); |
| | 4128 | |
| | 4129 | const size_t max_uncompressed_size = 2500000; |
| | 4130 | char *mod_ptr = arena_alloc(max_uncompressed_size); |
| | 4131 | size_t mod_len = ZSTD_decompress(mod_ptr, max_uncompressed_size, |
| | 4132 | compressed_bytes.ptr, compressed_bytes.len); |
| 4126 | | 4133 | |
| 4127 | int cwd = err_wrap("opening cwd", open(".", O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_PATH)); | 4134 | int cwd = err_wrap("opening cwd", open(".", O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_PATH)); |
| 4128 | int zig_lib_dir = err_wrap("opening zig lib dir", open(zig_lib_dir_path, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_PATH)); | 4135 | int zig_lib_dir = err_wrap("opening zig lib dir", open(zig_lib_dir_path, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_PATH)); |
| ... | @@ -4136,22 +4143,22 @@ int main(int argc, char **argv) { | ... | @@ -4136,22 +4143,22 @@ int main(int argc, char **argv) { |
| 4136 | | 4143 | |
| 4137 | uint32_t i = 0; | 4144 | uint32_t i = 0; |
| 4138 | | 4145 | |
| 4139 | if (mod.ptr[0] != 0 || mod.ptr[1] != 'a' || mod.ptr[2] != 's' || mod.ptr[3] != 'm') { | 4146 | if (mod_ptr[0] != 0 || mod_ptr[1] != 'a' || mod_ptr[2] != 's' || mod_ptr[3] != 'm') { |
| 4140 | panic("bad magic"); | 4147 | panic("bad magic"); |
| 4141 | } | 4148 | } |
| 4142 | i += 4; | 4149 | i += 4; |
| 4143 | | 4150 | |
| 4144 | uint32_t version = read_u32_le(mod.ptr + i); | 4151 | uint32_t version = read_u32_le(mod_ptr + i); |
| 4145 | i += 4; | 4152 | i += 4; |
| 4146 | if (version != 1) panic("bad wasm version"); | 4153 | if (version != 1) panic("bad wasm version"); |
| 4147 | | 4154 | |
| 4148 | uint32_t section_starts[13]; | 4155 | uint32_t section_starts[13]; |
| 4149 | memset(&section_starts, 0, sizeof(uint32_t) * 13); | 4156 | memset(&section_starts, 0, sizeof(uint32_t) * 13); |
| 4150 | | 4157 | |
| 4151 | while (i < mod.len) { | 4158 | while (i < mod_len) { |
| 4152 | uint8_t section_id = mod.ptr[i]; | 4159 | uint8_t section_id = mod_ptr[i]; |
| 4153 | i += 1; | 4160 | i += 1; |
| 4154 | uint32_t section_len = read32_uleb128(mod.ptr, &i); | 4161 | uint32_t section_len = read32_uleb128(mod_ptr, &i); |
| 4155 | section_starts[section_id] = i; | 4162 | section_starts[section_id] = i; |
| 4156 | i += section_len; | 4163 | i += section_len; |
| 4157 | } | 4164 | } |
| ... | @@ -4160,18 +4167,18 @@ int main(int argc, char **argv) { | ... | @@ -4160,18 +4167,18 @@ int main(int argc, char **argv) { |
| 4160 | struct TypeInfo *types; | 4167 | struct TypeInfo *types; |
| 4161 | { | 4168 | { |
| 4162 | i = section_starts[Section_type]; | 4169 | i = section_starts[Section_type]; |
| 4163 | uint32_t types_len = read32_uleb128(mod.ptr, &i); | 4170 | uint32_t types_len = read32_uleb128(mod_ptr, &i); |
| 4164 | types = arena_alloc(sizeof(struct TypeInfo) * types_len); | 4171 | types = arena_alloc(sizeof(struct TypeInfo) * types_len); |
| 4165 | for (size_t type_i = 0; type_i < types_len; type_i += 1) { | 4172 | for (size_t type_i = 0; type_i < types_len; type_i += 1) { |
| 4166 | struct TypeInfo *info = &types[type_i]; | 4173 | struct TypeInfo *info = &types[type_i]; |
| 4167 | if (mod.ptr[i] != 0x60) panic("bad type byte"); | 4174 | if (mod_ptr[i] != 0x60) panic("bad type byte"); |
| 4168 | i += 1; | 4175 | i += 1; |
| 4169 | | 4176 | |
| 4170 | info->param_count = read32_uleb128(mod.ptr, &i); | 4177 | info->param_count = read32_uleb128(mod_ptr, &i); |
| 4171 | if (info->param_count > 32) panic("found a type with over 32 parameters"); | 4178 | if (info->param_count > 32) panic("found a type with over 32 parameters"); |
| 4172 | info->param_types = 0; | 4179 | info->param_types = 0; |
| 4173 | for (uint32_t param_i = 0; param_i < info->param_count; param_i += 1) { | 4180 | for (uint32_t param_i = 0; param_i < info->param_count; param_i += 1) { |
| 4174 | int64_t param_type = read64_ileb128(mod.ptr, &i); | 4181 | int64_t param_type = read64_ileb128(mod_ptr, &i); |
| 4175 | switch (param_type) { | 4182 | switch (param_type) { |
| 4176 | case -1: case -3: bs_unset(&info->param_types, param_i); break; | 4183 | case -1: case -3: bs_unset(&info->param_types, param_i); break; |
| 4177 | case -2: case -4: bs_set(&info->param_types, param_i); break; | 4184 | case -2: case -4: bs_set(&info->param_types, param_i); break; |
| ... | @@ -4179,10 +4186,10 @@ int main(int argc, char **argv) { | ... | @@ -4179,10 +4186,10 @@ int main(int argc, char **argv) { |
| 4179 | } | 4186 | } |
| 4180 | } | 4187 | } |
| 4181 | | 4188 | |
| 4182 | info->result_count = read32_uleb128(mod.ptr, &i); | 4189 | info->result_count = read32_uleb128(mod_ptr, &i); |
| 4183 | info->result_types = 0; | 4190 | info->result_types = 0; |
| 4184 | for (uint32_t result_i = 0; result_i < info->result_count; result_i += 1) { | 4191 | for (uint32_t result_i = 0; result_i < info->result_count; result_i += 1) { |
| 4185 | int64_t result_type = read64_ileb128(mod.ptr, &i); | 4192 | int64_t result_type = read64_ileb128(mod_ptr, &i); |
| 4186 | switch (result_type) { | 4193 | switch (result_type) { |
| 4187 | case -1: case -3: bs_unset(&info->result_types, result_i); break; | 4194 | case -1: case -3: bs_unset(&info->result_types, result_i); break; |
| 4188 | case -2: case -4: bs_set(&info->result_types, result_i); break; | 4195 | case -2: case -4: bs_set(&info->result_types, result_i); break; |
| ... | @@ -4197,18 +4204,18 @@ int main(int argc, char **argv) { | ... | @@ -4197,18 +4204,18 @@ int main(int argc, char **argv) { |
| 4197 | uint32_t imports_len; | 4204 | uint32_t imports_len; |
| 4198 | { | 4205 | { |
| 4199 | i = section_starts[Section_import]; | 4206 | i = section_starts[Section_import]; |
| 4200 | imports_len = read32_uleb128(mod.ptr, &i); | 4207 | imports_len = read32_uleb128(mod_ptr, &i); |
| 4201 | imports = arena_alloc(sizeof(struct Import) * imports_len); | 4208 | imports = arena_alloc(sizeof(struct Import) * imports_len); |
| 4202 | for (size_t imp_i = 0; imp_i < imports_len; imp_i += 1) { | 4209 | for (size_t imp_i = 0; imp_i < imports_len; imp_i += 1) { |
| 4203 | struct Import *imp = &imports[imp_i]; | 4210 | struct Import *imp = &imports[imp_i]; |
| 4204 | | 4211 | |
| 4205 | struct ByteSlice mod_name = read_name(mod.ptr, &i); | 4212 | struct ByteSlice mod_name = read_name(mod_ptr, &i); |
| 4206 | if (mod_name.len == strlen("wasi_snapshot_preview1") && | 4213 | if (mod_name.len == strlen("wasi_snapshot_preview1") && |
| 4207 | memcmp(mod_name.ptr, "wasi_snapshot_preview1", mod_name.len) == 0) { | 4214 | memcmp(mod_name.ptr, "wasi_snapshot_preview1", mod_name.len) == 0) { |
| 4208 | imp->mod = ImpMod_wasi_snapshot_preview1; | 4215 | imp->mod = ImpMod_wasi_snapshot_preview1; |
| 4209 | } else panic("unknown import module"); | 4216 | } else panic("unknown import module"); |
| 4210 | | 4217 | |
| 4211 | struct ByteSlice sym_name = read_name(mod.ptr, &i); | 4218 | struct ByteSlice sym_name = read_name(mod_ptr, &i); |
| 4212 | if (sym_name.len == strlen("args_get") && | 4219 | if (sym_name.len == strlen("args_get") && |
| 4213 | memcmp(sym_name.ptr, "args_get", sym_name.len) == 0) { | 4220 | memcmp(sym_name.ptr, "args_get", sym_name.len) == 0) { |
| 4214 | imp->name = ImpName_args_get; | 4221 | imp->name = ImpName_args_get; |
| ... | @@ -4292,9 +4299,9 @@ int main(int argc, char **argv) { | ... | @@ -4292,9 +4299,9 @@ int main(int argc, char **argv) { |
| 4292 | imp->name = ImpName_random_get; | 4299 | imp->name = ImpName_random_get; |
| 4293 | } else panic("unknown import name"); | 4300 | } else panic("unknown import name"); |
| 4294 | | 4301 | |
| 4295 | uint32_t desc = read32_uleb128(mod.ptr, &i); | 4302 | uint32_t desc = read32_uleb128(mod_ptr, &i); |
| 4296 | if (desc != 0) panic("external kind not function"); | 4303 | if (desc != 0) panic("external kind not function"); |
| 4297 | imp->type_idx = read32_uleb128(mod.ptr, &i); | 4304 | imp->type_idx = read32_uleb128(mod_ptr, &i); |
| 4298 | } | 4305 | } |
| 4299 | } | 4306 | } |
| 4300 | | 4307 | |
| ... | @@ -4302,11 +4309,11 @@ int main(int argc, char **argv) { | ... | @@ -4302,11 +4309,11 @@ int main(int argc, char **argv) { |
| 4302 | uint32_t start_fn_idx; | 4309 | uint32_t start_fn_idx; |
| 4303 | { | 4310 | { |
| 4304 | i = section_starts[Section_export]; | 4311 | i = section_starts[Section_export]; |
| 4305 | uint32_t count = read32_uleb128(mod.ptr, &i); | 4312 | uint32_t count = read32_uleb128(mod_ptr, &i); |
| 4306 | for (; count > 0; count -= 1) { | 4313 | for (; count > 0; count -= 1) { |
| 4307 | struct ByteSlice name = read_name(mod.ptr, &i); | 4314 | struct ByteSlice name = read_name(mod_ptr, &i); |
| 4308 | uint32_t desc = read32_uleb128(mod.ptr, &i); | 4315 | uint32_t desc = read32_uleb128(mod_ptr, &i); |
| 4309 | start_fn_idx = read32_uleb128(mod.ptr, &i); | 4316 | start_fn_idx = read32_uleb128(mod_ptr, &i); |
| 4310 | if (desc == 0 && name.len == strlen("_start") && | 4317 | if (desc == 0 && name.len == strlen("_start") && |
| 4311 | memcmp(name.ptr, "_start", name.len) == 0) | 4318 | memcmp(name.ptr, "_start", name.len) == 0) |
| 4312 | { | 4319 | { |
| ... | @@ -4321,11 +4328,11 @@ int main(int argc, char **argv) { | ... | @@ -4321,11 +4328,11 @@ int main(int argc, char **argv) { |
| 4321 | uint32_t functions_len; | 4328 | uint32_t functions_len; |
| 4322 | { | 4329 | { |
| 4323 | i = section_starts[Section_function]; | 4330 | i = section_starts[Section_function]; |
| 4324 | functions_len = read32_uleb128(mod.ptr, &i); | 4331 | functions_len = read32_uleb128(mod_ptr, &i); |
| 4325 | functions = arena_alloc(sizeof(struct Function) * functions_len); | 4332 | functions = arena_alloc(sizeof(struct Function) * functions_len); |
| 4326 | for (size_t func_i = 0; func_i < functions_len; func_i += 1) { | 4333 | for (size_t func_i = 0; func_i < functions_len; func_i += 1) { |
| 4327 | struct Function *func = &functions[func_i]; | 4334 | struct Function *func = &functions[func_i]; |
| 4328 | func->type_idx = read32_uleb128(mod.ptr, &i); | 4335 | func->type_idx = read32_uleb128(mod_ptr, &i); |
| 4329 | } | 4336 | } |
| 4330 | } | 4337 | } |
| 4331 | | 4338 | |
| ... | @@ -4333,18 +4340,18 @@ int main(int argc, char **argv) { | ... | @@ -4333,18 +4340,18 @@ int main(int argc, char **argv) { |
| 4333 | uint64_t *globals; | 4340 | uint64_t *globals; |
| 4334 | { | 4341 | { |
| 4335 | i = section_starts[Section_global]; | 4342 | i = section_starts[Section_global]; |
| 4336 | uint32_t globals_len = read32_uleb128(mod.ptr, &i); | 4343 | uint32_t globals_len = read32_uleb128(mod_ptr, &i); |
| 4337 | globals = arena_alloc(sizeof(uint64_t) * globals_len); | 4344 | globals = arena_alloc(sizeof(uint64_t) * globals_len); |
| 4338 | for (size_t glob_i = 0; glob_i < globals_len; glob_i += 1) { | 4345 | for (size_t glob_i = 0; glob_i < globals_len; glob_i += 1) { |
| 4339 | uint64_t *global = &globals[glob_i]; | 4346 | uint64_t *global = &globals[glob_i]; |
| 4340 | uint32_t content_type = read32_uleb128(mod.ptr, &i); | 4347 | uint32_t content_type = read32_uleb128(mod_ptr, &i); |
| 4341 | uint32_t mutability = read32_uleb128(mod.ptr, &i); | 4348 | uint32_t mutability = read32_uleb128(mod_ptr, &i); |
| 4342 | if (mutability != 1) panic("expected mutable global"); | 4349 | if (mutability != 1) panic("expected mutable global"); |
| 4343 | if (content_type != 0x7f) panic("unexpected content type"); | 4350 | if (content_type != 0x7f) panic("unexpected content type"); |
| 4344 | uint8_t opcode = mod.ptr[i]; | 4351 | uint8_t opcode = mod_ptr[i]; |
| 4345 | i += 1; | 4352 | i += 1; |
| 4346 | if (opcode != WasmOp_i32_const) panic("expected i32_const op"); | 4353 | if (opcode != WasmOp_i32_const) panic("expected i32_const op"); |
| 4347 | uint32_t init = read32_ileb128(mod.ptr, &i); | 4354 | uint32_t init = read32_ileb128(mod_ptr, &i); |
| 4348 | *global = (uint32_t)init; | 4355 | *global = (uint32_t)init; |
| 4349 | } | 4356 | } |
| 4350 | } | 4357 | } |
| ... | @@ -4353,26 +4360,26 @@ int main(int argc, char **argv) { | ... | @@ -4353,26 +4360,26 @@ int main(int argc, char **argv) { |
| 4353 | uint32_t memory_len; | 4360 | uint32_t memory_len; |
| 4354 | { | 4361 | { |
| 4355 | i = section_starts[Section_memory]; | 4362 | i = section_starts[Section_memory]; |
| 4356 | uint32_t memories_len = read32_uleb128(mod.ptr, &i); | 4363 | uint32_t memories_len = read32_uleb128(mod_ptr, &i); |
| 4357 | if (memories_len != 1) panic("unexpected memory count"); | 4364 | if (memories_len != 1) panic("unexpected memory count"); |
| 4358 | uint32_t flags = read32_uleb128(mod.ptr, &i); | 4365 | uint32_t flags = read32_uleb128(mod_ptr, &i); |
| 4359 | (void)flags; | 4366 | (void)flags; |
| 4360 | memory_len = read32_uleb128(mod.ptr, &i) * wasm_page_size; | 4367 | memory_len = read32_uleb128(mod_ptr, &i) * wasm_page_size; |
| 4361 | | 4368 | |
| 4362 | i = section_starts[Section_data]; | 4369 | i = section_starts[Section_data]; |
| 4363 | uint32_t datas_count = read32_uleb128(mod.ptr, &i); | 4370 | uint32_t datas_count = read32_uleb128(mod_ptr, &i); |
| 4364 | for (; datas_count > 0; datas_count -= 1) { | 4371 | for (; datas_count > 0; datas_count -= 1) { |
| 4365 | uint32_t mode = read32_uleb128(mod.ptr, &i); | 4372 | uint32_t mode = read32_uleb128(mod_ptr, &i); |
| 4366 | if (mode != 0) panic("expected mode 0"); | 4373 | if (mode != 0) panic("expected mode 0"); |
| 4367 | enum WasmOp opcode = mod.ptr[i]; | 4374 | enum WasmOp opcode = mod_ptr[i]; |
| 4368 | i += 1; | 4375 | i += 1; |
| 4369 | if (opcode != WasmOp_i32_const) panic("expected opcode i32_const"); | 4376 | if (opcode != WasmOp_i32_const) panic("expected opcode i32_const"); |
| 4370 | uint32_t offset = read32_uleb128(mod.ptr, &i); | 4377 | uint32_t offset = read32_uleb128(mod_ptr, &i); |
| 4371 | enum WasmOp end = mod.ptr[i]; | 4378 | enum WasmOp end = mod_ptr[i]; |
| 4372 | if (end != WasmOp_end) panic("expected end opcode"); | 4379 | if (end != WasmOp_end) panic("expected end opcode"); |
| 4373 | i += 1; | 4380 | i += 1; |
| 4374 | uint32_t bytes_len = read32_uleb128(mod.ptr, &i); | 4381 | uint32_t bytes_len = read32_uleb128(mod_ptr, &i); |
| 4375 | memcpy(memory + offset, mod.ptr + i, bytes_len); | 4382 | memcpy(memory + offset, mod_ptr + i, bytes_len); |
| 4376 | i += bytes_len; | 4383 | i += bytes_len; |
| 4377 | } | 4384 | } |
| 4378 | } | 4385 | } |
| ... | @@ -4380,37 +4387,37 @@ int main(int argc, char **argv) { | ... | @@ -4380,37 +4387,37 @@ int main(int argc, char **argv) { |
| 4380 | uint32_t *table = NULL; | 4387 | uint32_t *table = NULL; |
| 4381 | { | 4388 | { |
| 4382 | i = section_starts[Section_table]; | 4389 | i = section_starts[Section_table]; |
| 4383 | uint32_t table_count = read32_uleb128(mod.ptr, &i); | 4390 | uint32_t table_count = read32_uleb128(mod_ptr, &i); |
| 4384 | if (table_count > 1) { | 4391 | if (table_count > 1) { |
| 4385 | panic("expected only one table section"); | 4392 | panic("expected only one table section"); |
| 4386 | } else if (table_count == 1) { | 4393 | } else if (table_count == 1) { |
| 4387 | uint32_t element_type = read32_uleb128(mod.ptr, &i); | 4394 | uint32_t element_type = read32_uleb128(mod_ptr, &i); |
| 4388 | (void)element_type; | 4395 | (void)element_type; |
| 4389 | uint32_t has_max = read32_uleb128(mod.ptr, &i); | 4396 | uint32_t has_max = read32_uleb128(mod_ptr, &i); |
| 4390 | if (has_max != 1) panic("expected has_max==1"); | 4397 | if (has_max != 1) panic("expected has_max==1"); |
| 4391 | uint32_t initial = read32_uleb128(mod.ptr, &i); | 4398 | uint32_t initial = read32_uleb128(mod_ptr, &i); |
| 4392 | (void)initial; | 4399 | (void)initial; |
| 4393 | uint32_t maximum = read32_uleb128(mod.ptr, &i); | 4400 | uint32_t maximum = read32_uleb128(mod_ptr, &i); |
| 4394 | | 4401 | |
| 4395 | i = section_starts[Section_element]; | 4402 | i = section_starts[Section_element]; |
| 4396 | uint32_t element_section_count = read32_uleb128(mod.ptr, &i); | 4403 | uint32_t element_section_count = read32_uleb128(mod_ptr, &i); |
| 4397 | if (element_section_count != 1) panic("expected one element section"); | 4404 | if (element_section_count != 1) panic("expected one element section"); |
| 4398 | uint32_t flags = read32_uleb128(mod.ptr, &i); | 4405 | uint32_t flags = read32_uleb128(mod_ptr, &i); |
| 4399 | (void)flags; | 4406 | (void)flags; |
| 4400 | enum WasmOp opcode = mod.ptr[i]; | 4407 | enum WasmOp opcode = mod_ptr[i]; |
| 4401 | i += 1; | 4408 | i += 1; |
| 4402 | if (opcode != WasmOp_i32_const) panic("expected op i32_const"); | 4409 | if (opcode != WasmOp_i32_const) panic("expected op i32_const"); |
| 4403 | uint32_t offset = read32_uleb128(mod.ptr, &i); | 4410 | uint32_t offset = read32_uleb128(mod_ptr, &i); |
| 4404 | enum WasmOp end = mod.ptr[i]; | 4411 | enum WasmOp end = mod_ptr[i]; |
| 4405 | if (end != WasmOp_end) panic("expected op end"); | 4412 | if (end != WasmOp_end) panic("expected op end"); |
| 4406 | i += 1; | 4413 | i += 1; |
| 4407 | uint32_t elem_count = read32_uleb128(mod.ptr, &i); | 4414 | uint32_t elem_count = read32_uleb128(mod_ptr, &i); |
| 4408 | | 4415 | |
| 4409 | table = arena_alloc(sizeof(uint32_t) * maximum); | 4416 | table = arena_alloc(sizeof(uint32_t) * maximum); |
| 4410 | memset(table, 0, sizeof(uint32_t) * maximum); | 4417 | memset(table, 0, sizeof(uint32_t) * maximum); |
| 4411 | | 4418 | |
| 4412 | for (uint32_t elem_i = 0; elem_i < elem_count; elem_i += 1) { | 4419 | for (uint32_t elem_i = 0; elem_i < elem_count; elem_i += 1) { |
| 4413 | table[elem_i + offset] = read32_uleb128(mod.ptr, &i); | 4420 | table[elem_i + offset] = read32_uleb128(mod_ptr, &i); |
| 4414 | } | 4421 | } |
| 4415 | } | 4422 | } |
| 4416 | } | 4423 | } |
| ... | @@ -4420,7 +4427,7 @@ int main(int argc, char **argv) { | ... | @@ -4420,7 +4427,7 @@ int main(int argc, char **argv) { |
| 4420 | memset(&vm, 0xaa, sizeof(struct VirtualMachine)); // to match the zig version | 4427 | memset(&vm, 0xaa, sizeof(struct VirtualMachine)); // to match the zig version |
| 4421 | #endif | 4428 | #endif |
| 4422 | vm.stack = arena_alloc(sizeof(uint64_t) * 10000000), | 4429 | vm.stack = arena_alloc(sizeof(uint64_t) * 10000000), |
| 4423 | vm.mod_ptr = mod.ptr; | 4430 | vm.mod_ptr = mod_ptr; |
| 4424 | vm.opcodes = arena_alloc(2000000); | 4431 | vm.opcodes = arena_alloc(2000000); |
| 4425 | vm.operands = arena_alloc(sizeof(uint32_t) * 2000000); | 4432 | vm.operands = arena_alloc(sizeof(uint32_t) * 2000000); |
| 4426 | vm.stack_top = 0; | 4433 | vm.stack_top = 0; |
| ... | @@ -4436,14 +4443,14 @@ int main(int argc, char **argv) { | ... | @@ -4436,14 +4443,14 @@ int main(int argc, char **argv) { |
| 4436 | | 4443 | |
| 4437 | { | 4444 | { |
| 4438 | uint32_t code_i = section_starts[Section_code]; | 4445 | uint32_t code_i = section_starts[Section_code]; |
| 4439 | uint32_t codes_len = read32_uleb128(mod.ptr, &code_i); | 4446 | uint32_t codes_len = read32_uleb128(mod_ptr, &code_i); |
| 4440 | if (codes_len != functions_len) panic("code/function length mismatch"); | 4447 | if (codes_len != functions_len) panic("code/function length mismatch"); |
| 4441 | struct ProgramCounter pc; | 4448 | struct ProgramCounter pc; |
| 4442 | pc.opcode = 0; | 4449 | pc.opcode = 0; |
| 4443 | pc.operand = 0; | 4450 | pc.operand = 0; |
| 4444 | for (uint32_t func_i = 0; func_i < functions_len; func_i += 1) { | 4451 | for (uint32_t func_i = 0; func_i < functions_len; func_i += 1) { |
| 4445 | struct Function *func = &functions[func_i]; | 4452 | struct Function *func = &functions[func_i]; |
| 4446 | uint32_t size = read32_uleb128(mod.ptr, &code_i); | 4453 | uint32_t size = read32_uleb128(mod_ptr, &code_i); |
| 4447 | uint32_t code_begin = code_i; | 4454 | uint32_t code_begin = code_i; |
| 4448 | | 4455 | |
| 4449 | struct TypeInfo *type_info = &vm.types[func->type_idx]; | 4456 | struct TypeInfo *type_info = &vm.types[func->type_idx]; |
| ... | @@ -4451,11 +4458,11 @@ int main(int argc, char **argv) { | ... | @@ -4451,11 +4458,11 @@ int main(int argc, char **argv) { |
| 4451 | func->local_types = malloc(sizeof(uint32_t) * ((type_info->param_count + func->locals_count + 31) / 32)); | 4458 | func->local_types = malloc(sizeof(uint32_t) * ((type_info->param_count + func->locals_count + 31) / 32)); |
| 4452 | func->local_types[0] = type_info->param_types; | 4459 | func->local_types[0] = type_info->param_types; |
| 4453 | | 4460 | |
| 4454 | for (uint32_t local_sets_count = read32_uleb128(mod.ptr, &code_i); | 4461 | for (uint32_t local_sets_count = read32_uleb128(mod_ptr, &code_i); |
| 4455 | local_sets_count > 0; local_sets_count -= 1) | 4462 | local_sets_count > 0; local_sets_count -= 1) |
| 4456 | { | 4463 | { |
| 4457 | uint32_t set_count = read32_uleb128(mod.ptr, &code_i); | 4464 | uint32_t set_count = read32_uleb128(mod_ptr, &code_i); |
| 4458 | int64_t local_type = read64_ileb128(mod.ptr, &code_i); | 4465 | int64_t local_type = read64_ileb128(mod_ptr, &code_i); |
| 4459 | | 4466 | |
| 4460 | uint32_t i = type_info->param_count + func->locals_count; | 4467 | uint32_t i = type_info->param_count + func->locals_count; |
| 4461 | func->locals_count += set_count; | 4468 | func->locals_count += set_count; |