|
| 1 | +/* |
| 2 | + * Copyright 2017 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +// |
| 18 | +// This pass replaces all call_indirect instructions with a call to JS side |
| 19 | +// js_call_indirect_xxxx functions that can perform debug analysis of the |
| 20 | +// call in question. Must run after FuncCastEmulation pass, if that is |
| 21 | +// desired. |
| 22 | +// |
| 23 | + |
| 24 | +#include <string> |
| 25 | +#include <unordered_map> |
| 26 | +#include <unordered_set> |
| 27 | + |
| 28 | +#include <ir/element-utils.h> |
| 29 | +#include <ir/literal-utils.h> |
| 30 | +#include <pass.h> |
| 31 | +#include <wasm-builder.h> |
| 32 | +#include <wasm.h> |
| 33 | +#include <asm_v_wasm.h> |
| 34 | + |
| 35 | +namespace wasm { |
| 36 | + |
| 37 | +void setFunctionAsImported(std::unique_ptr<Function>& import, Module* m); |
| 38 | + |
| 39 | +typedef std::unordered_map<std::string, std::unique_ptr<Function>> |
| 40 | + JsCallIndirectMap; |
| 41 | + |
| 42 | +struct ParallelJsCallIndirect |
| 43 | + : public WalkerPass<PostWalker<ParallelJsCallIndirect>> { |
| 44 | + bool isFunctionParallel() override { return true; } |
| 45 | + |
| 46 | + std::unique_ptr<Pass> create() override { |
| 47 | + return std::make_unique<ParallelJsCallIndirect>(); |
| 48 | + } |
| 49 | + |
| 50 | + void visitCallIndirect(CallIndirect* curr) { |
| 51 | + Module* module = getModule(); |
| 52 | + Builder builder(*module); |
| 53 | + std::vector<Expression*> callArgs; |
| 54 | + callArgs.push_back(curr->target); |
| 55 | + callArgs.insert( |
| 56 | + callArgs.end(), curr->operands.begin(), curr->operands.end()); |
| 57 | + std::string jsCallIndirect = |
| 58 | + "js_call_indirect_" + getSigFromStructs(curr->type, curr->operands); |
| 59 | + if (module->getFunctionOrNull(jsCallIndirect)) { |
| 60 | + replaceCurrent(builder.makeCall(jsCallIndirect, callArgs, curr->type)); |
| 61 | + } else { |
| 62 | + // The code is attempting a call_indirect via a signature that there does not exist |
| 63 | + // any function in the whole program. We can abort here since that will not work at runtime. |
| 64 | + // TODO: Maybe just create on the fly a "js_call_indirect_" function for this nonexisting |
| 65 | + // signature? |
| 66 | + replaceCurrent(builder.makeCall("abort", {}, Type::none)); |
| 67 | + } |
| 68 | + } |
| 69 | +}; |
| 70 | + |
| 71 | +struct JsCallIndirect : public Pass { |
| 72 | + void run(Module* module) override { |
| 73 | + std::unordered_set<Signature> seen_sigs; |
| 74 | + for (std::unique_ptr<Function>& func : module->functions) { |
| 75 | + Signature sig = func->getSig(); |
| 76 | + std::string type_str = getSig(sig.results, sig.params); |
| 77 | + if (seen_sigs.find(sig) != seen_sigs.end()) |
| 78 | + continue; |
| 79 | + seen_sigs.insert(sig); |
| 80 | + |
| 81 | + std::vector<Type> params; |
| 82 | + params.push_back(Type::i32); |
| 83 | + for (auto t : sig.params) |
| 84 | + params.push_back(t); |
| 85 | + auto& import = |
| 86 | + Builder::makeFunction(std::string("js_call_indirect_") + type_str, |
| 87 | + Signature(Type(params), sig.results.getBasic()), |
| 88 | + {}); |
| 89 | + setFunctionAsImported(import, module); |
| 90 | + import->base = type_str; |
| 91 | + module->addFunction(std::move(import)); |
| 92 | + } |
| 93 | + |
| 94 | + // route all call_indirects to js_call_indirect_*() variants |
| 95 | + ParallelJsCallIndirect().run(getPassRunner(), module); |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +Pass* createJsCallIndirectPass() { return new JsCallIndirect(); } |
| 100 | + |
| 101 | +} // namespace wasm |
0 commit comments