|
33 | 33 | #include "parsing.h" |
34 | 34 | #include "ast_utils.h" |
35 | 35 | #include "ast/branch-utils.h" |
| 36 | +#include "ast/literal-utils.h" |
36 | 37 | #include "ast/trapping.h" |
37 | 38 | #include "wasm-builder.h" |
38 | 39 | #include "wasm-emscripten.h" |
@@ -392,17 +393,12 @@ class Asm2WasmBuilder { |
392 | 393 | void allocateGlobal(IString name, WasmType type) { |
393 | 394 | assert(mappedGlobals.find(name) == mappedGlobals.end()); |
394 | 395 | mappedGlobals.emplace(name, MappedGlobal(type)); |
395 | | - auto global = new Global(); |
396 | | - global->name = name; |
397 | | - global->type = type; |
398 | | - Literal value; |
399 | | - if (type == i32) value = Literal(uint32_t(0)); |
400 | | - else if (type == f32) value = Literal(float(0)); |
401 | | - else if (type == f64) value = Literal(double(0)); |
402 | | - else WASM_UNREACHABLE(); |
403 | | - global->init = wasm.allocator.alloc<Const>()->set(value); |
404 | | - global->mutable_ = true; |
405 | | - wasm.addGlobal(global); |
| 396 | + wasm.addGlobal(builder.makeGlobal( |
| 397 | + name, |
| 398 | + type, |
| 399 | + LiteralUtils::makeZero(type, wasm), |
| 400 | + Builder::Mutable |
| 401 | + )); |
406 | 402 | } |
407 | 403 |
|
408 | 404 | struct View { |
@@ -838,12 +834,12 @@ void Asm2WasmBuilder::processAsm(Ref ast) { |
838 | 834 | // import an immutable and create a mutable global initialized to its value |
839 | 835 | import->name = Name(std::string(import->name.str) + "$asm2wasm$import"); |
840 | 836 | { |
841 | | - auto global = new Global(); |
842 | | - global->name = name; |
843 | | - global->type = type; |
844 | | - global->init = builder.makeGetGlobal(import->name, type); |
845 | | - global->mutable_ = true; |
846 | | - wasm.addGlobal(global); |
| 837 | + wasm.addGlobal(builder.makeGlobal( |
| 838 | + name, |
| 839 | + type, |
| 840 | + builder.makeGetGlobal(import->name, type), |
| 841 | + Builder::Mutable |
| 842 | + )); |
847 | 843 | } |
848 | 844 | } |
849 | 845 | } else { |
@@ -1076,11 +1072,12 @@ void Asm2WasmBuilder::processAsm(Ref ast) { |
1076 | 1072 | assert(pair[1]->isNumber()); |
1077 | 1073 | assert(exported.count(key) == 0); |
1078 | 1074 | auto value = pair[1]->getInteger(); |
1079 | | - auto global = new Global(); |
1080 | | - global->name = key; |
1081 | | - global->type = i32; |
1082 | | - global->init = builder.makeConst(Literal(int32_t(value))); |
1083 | | - global->mutable_ = false; |
| 1075 | + auto* global = builder.makeGlobal( |
| 1076 | + key, |
| 1077 | + i32, |
| 1078 | + builder.makeConst(Literal(int32_t(value))), |
| 1079 | + Builder::Immutable |
| 1080 | + ); |
1084 | 1081 | wasm.addGlobal(global); |
1085 | 1082 | auto* export_ = new Export; |
1086 | 1083 | export_->name = key; |
|
0 commit comments