diff options
Diffstat (limited to 'src/wasm2asm.h')
-rw-r--r-- | src/wasm2asm.h | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/wasm2asm.h b/src/wasm2asm.h index 346c1c677..ca67b0aa8 100644 --- a/src/wasm2asm.h +++ b/src/wasm2asm.h @@ -211,6 +211,7 @@ private: void addImport(Ref ast, Import* import); void addTables(Ref ast, Module* wasm); void addExports(Ref ast, Module* wasm); + void addGlobal(Ref ast, Global* global); void addWasmCompatibilityFuncs(Module* wasm); void setNeedsAlmostASM(const char *reason); void addMemoryGrowthFuncs(Ref ast); @@ -418,6 +419,10 @@ Ref Wasm2AsmBuilder::processWasm(Module* wasm) { pow2ed <<= 1; } tableSize = pow2ed; + // globals + for (auto& global : wasm->globals) { + addGlobal(asmFunc[3], global.get()); + } // functions for (auto& func : wasm->functions) { asmFunc[3]->push_back(processFunction(func.get())); @@ -578,6 +583,39 @@ void Wasm2AsmBuilder::addExports(Ref ast, Module* wasm) { ast->push_back(ValueBuilder::makeStatement(ValueBuilder::makeReturn(exports))); } +void Wasm2AsmBuilder::addGlobal(Ref ast, Global* global) { + if (auto* const_ = global->init->dynCast<Const>()) { + Ref theValue; + switch (const_->type) { + case Type::i32: { + theValue = ValueBuilder::makeInt(const_->value.geti32()); + break; + } + case Type::f32: { + theValue = ValueBuilder::makeCall(MATH_FROUND, + makeAsmCoercion(ValueBuilder::makeDouble(const_->value.getf32()), ASM_DOUBLE) + ); + break; + } + case Type::f64: { + theValue = makeAsmCoercion(ValueBuilder::makeDouble(const_->value.getf64()), ASM_DOUBLE); + break; + } + default: { + assert(false && "Global const type not supported"); + } + } + Ref theVar = ValueBuilder::makeVar(); + ast->push_back(theVar); + ValueBuilder::appendToVar(theVar, + fromName(global->name), + theValue + ); + } else { + assert(false && "Global init type not supported"); + } +} + Ref Wasm2AsmBuilder::processFunction(Function* func) { if (flags.debug) { static int fns = 0; |