summaryrefslogtreecommitdiff
path: root/src/wasm2asm.h
diff options
context:
space:
mode:
authorDaniel Wirtz <dcode@dcode.io>2018-02-20 23:59:12 +0100
committerAlon Zakai <alonzakai@gmail.com>2018-02-20 14:59:12 -0800
commiteacd9a987750ae3984c225a73e9567931277d6a0 (patch)
treea070ee03496b50387a0a0a2d1251213421950fb9 /src/wasm2asm.h
parent27000a9bfa616133c9368214386fd0416f398dfe (diff)
downloadbinaryen-eacd9a987750ae3984c225a73e9567931277d6a0.tar.gz
binaryen-eacd9a987750ae3984c225a73e9567931277d6a0.tar.bz2
binaryen-eacd9a987750ae3984c225a73e9567931277d6a0.zip
Add global initializers to wasm2asm (#1434)
Adds support for (constant i32/f32/f64) global variable initializers, which were previously ignored though get_global/set_global are supported.
Diffstat (limited to 'src/wasm2asm.h')
-rw-r--r--src/wasm2asm.h38
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;