From f87de2ae0d430f8d4204df8fceb194c24e29b413 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Mon, 22 Apr 2019 11:36:50 -0700 Subject: wasm-emscripten-finalize: Handle relocatable code in AsmConstWalker (#2035) When replacing the first argument to an asm call, allow more complex expressions for expressing the address. This fixes the case where the first argument might be the result of adding a constant to __memory_base. --- src/wasm/wasm-emscripten.cpp | 49 +++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'src/wasm/wasm-emscripten.cpp') diff --git a/src/wasm/wasm-emscripten.cpp b/src/wasm/wasm-emscripten.cpp index 837dcbea1..680e145b4 100644 --- a/src/wasm/wasm-emscripten.cpp +++ b/src/wasm/wasm-emscripten.cpp @@ -525,30 +525,37 @@ void AsmConstWalker::visitCall(Call* curr) { auto baseSig = getSig(curr); auto sig = fixupNameWithSig(curr->target, baseSig); auto* arg = curr->operands[0]; - if (auto* get = arg->dynCast()) { - // The argument may be a local.get, in which case, the last set in this - // basic block has the value. - auto* set = sets[get->index]; - if (set) { - assert(set->index == get->index); - arg = set->value; - } - } else if (auto* value = arg->dynCast()) { - // In the dynamic linking case the address of the string constant - // is the result of adding its offset to __memory_base. - // In this case are only looking for the offset with the data segment so - // the RHS of the addition is just what we want. - assert(value->op == AddInt32); - arg = value->right; - } - - auto* value = arg->dynCast(); - if (!value) - Fatal() << "Unexpected arg0 type (" << getExpressionName(arg) + while (!arg->dynCast()) { + if (auto* get = arg->dynCast()) { + // The argument may be a local.get, in which case, the last set in this + // basic block has the value. + auto* set = sets[get->index]; + if (set) { + assert(set->index == get->index); + arg = set->value; + } + } else if (auto* value = arg->dynCast()) { + // In the dynamic linking case the address of the string constant + // is the result of adding its offset to __memory_base. + // In this case are only looking for the offset with the data segment so + // the RHS of the addition is just what we want. + assert(value->op == AddInt32); + arg = value->right; + } else { + if (!value) { + Fatal() << "Unexpected arg0 type (" << getExpressionName(arg) << ") in call to to: " << import->base; + } + } + } + + auto* value = arg->cast(); auto code = codeForConstAddr(wasm, segmentOffsets, value); - value->value = idLiteralForCode(code); sigsForCode[code].insert(sig); + + // Replace the first argument to the call with a Const index + Builder builder(wasm); + curr->operands[0] = builder.makeConst(idLiteralForCode(code)); } } -- cgit v1.2.3