summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/wasm2js.h67
1 files changed, 19 insertions, 48 deletions
diff --git a/src/wasm2js.h b/src/wasm2js.h
index 034212b79..34260547d 100644
--- a/src/wasm2js.h
+++ b/src/wasm2js.h
@@ -192,7 +192,10 @@ public:
// The second pass on an expression: process it fully, generating
// JS
- Ref processFunctionBody(Module* m, Function* func, bool standalone);
+ Ref processExpression(Expression* curr,
+ Module* m,
+ Function* func = nullptr,
+ bool standalone = false);
Index getDataIndex(Name segment) {
auto it = dataIndices.find(segment);
@@ -323,7 +326,7 @@ private:
void addTable(Ref ast, Module* wasm);
void addStart(Ref ast, Module* wasm);
void addExports(Ref ast, Module* wasm);
- void addGlobal(Ref ast, Global* global);
+ void addGlobal(Ref ast, Global* global, Module* module);
void addMemoryFuncs(Ref ast, Module* wasm);
void addMemoryGrowFunc(Ref ast, Module* wasm);
@@ -503,7 +506,7 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) {
// globals
bool generateFetchHighBits = false;
ModuleUtils::iterDefinedGlobals(*wasm, [&](Global* global) {
- addGlobal(asmFunc[3], global);
+ addGlobal(asmFunc[3], global, wasm);
if (flags.allowAsserts && global->name == INT64_TO_32_HIGH_BITS) {
generateFetchHighBits = true;
}
@@ -845,46 +848,12 @@ void Wasm2JSBuilder::addExports(Ref ast, Module* wasm) {
ValueBuilder::makeStatement(ValueBuilder::makeReturn(exports)));
}
-void Wasm2JSBuilder::addGlobal(Ref ast, Global* global) {
+void Wasm2JSBuilder::addGlobal(Ref ast, Global* global, Module* module) {
Ref theVar = ValueBuilder::makeVar();
ast->push_back(theVar);
-
- auto* init = global->init;
- Ref value;
-
- if (auto* const_ = init->dynCast<Const>()) {
- TODO_SINGLE_COMPOUND(const_->type);
- switch (const_->type.getBasic()) {
- case Type::i32: {
- value = ValueBuilder::makeInt(const_->value.geti32());
- break;
- }
- case Type::f32: {
- value = ValueBuilder::makeCall(
- MATH_FROUND,
- makeJsCoercion(ValueBuilder::makeDouble(const_->value.getf32()),
- JS_DOUBLE));
- break;
- }
- case Type::f64: {
- value = makeJsCoercion(ValueBuilder::makeDouble(const_->value.getf64()),
- JS_DOUBLE);
- break;
- }
- default: {
- assert(false && "Top const type not supported");
- }
- }
- } else if (auto* get = init->dynCast<GlobalGet>()) {
- value = ValueBuilder::makeName(fromName(get->name, NameScope::Top));
- } else if (init->is<RefNull>()) {
- value = ValueBuilder::makeName(NULL_);
- } else {
- assert(false && "Top init type not supported");
- }
-
+ Ref init = processExpression(global->init, module);
ValueBuilder::appendToVar(
- theVar, fromName(global->name, NameScope::Top), value);
+ theVar, fromName(global->name, NameScope::Top), init);
}
Ref Wasm2JSBuilder::processFunction(Module* m,
@@ -934,7 +903,8 @@ Ref Wasm2JSBuilder::processFunction(Module* m,
size_t theVarIndex = ret[3]->size();
ret[3]->push_back(theVar);
// body
- flattenAppend(ret, processFunctionBody(m, func, standaloneFunction));
+ flattenAppend(ret,
+ processExpression(func->body, m, func, standaloneFunction));
// vars, including new temp vars
for (Index i = func->getVarIndexBase(); i < func->getNumLocals(); i++) {
ValueBuilder::appendToVar(
@@ -952,9 +922,10 @@ Ref Wasm2JSBuilder::processFunction(Module* m,
return ret;
}
-Ref Wasm2JSBuilder::processFunctionBody(Module* m,
- Function* func,
- bool standaloneFunction) {
+Ref Wasm2JSBuilder::processExpression(Expression* curr,
+ Module* m,
+ Function* func,
+ bool standaloneFunction) {
// Switches are tricky to handle - in wasm they often come with
// massively-nested "towers" of blocks, which if naively translated
// to JS may exceed parse recursion limits of VMs. Therefore even when
@@ -1092,9 +1063,9 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m,
: parent(parent), func(func), module(m),
standaloneFunction(standaloneFunction) {}
- Ref process() {
- switchProcessor.walk(func->body);
- return visit(func->body, NO_RESULT);
+ Ref process(Expression* curr) {
+ switchProcessor.walk(curr);
+ return visit(curr, NO_RESULT);
}
// A scoped temporary variable.
@@ -2460,7 +2431,7 @@ Ref Wasm2JSBuilder::processFunctionBody(Module* m,
}
};
- return ExpressionProcessor(this, m, func, standaloneFunction).process();
+ return ExpressionProcessor(this, m, func, standaloneFunction).process(curr);
}
void Wasm2JSBuilder::addMemoryFuncs(Ref ast, Module* wasm) {