Instantiate function instance. See "include/executor/executor.h".
| 11 | |
| 12 | // Instantiate function instance. See "include/executor/executor.h". |
| 13 | Expect<void> Executor::instantiate(Runtime::Instance::ModuleInstance &ModInst, |
| 14 | const AST::FunctionSection &FuncSec, |
| 15 | const AST::CodeSection &CodeSec) { |
| 16 | |
| 17 | // Get the function type indices. |
| 18 | auto TypeIdxs = FuncSec.getContent(); |
| 19 | auto CodeSegs = CodeSec.getContent(); |
| 20 | |
| 21 | if (CodeSegs.size() == 0) { |
| 22 | return {}; |
| 23 | } |
| 24 | // Under interpreter mode, the module always chooses the `for` loop in the |
| 25 | // `else` case. Branching in the `for` loop might cause meaningless branch |
| 26 | // misses, so check the first item and dispatch it into different cases to |
| 27 | // reduce branch misses. |
| 28 | if (CodeSegs[0].getSymbol() != false) { |
| 29 | for (uint32_t I = 0; I < CodeSegs.size(); ++I) { |
| 30 | auto Symbol = CodeSegs[I].getSymbol(); |
| 31 | ModInst.addFunc( |
| 32 | TypeIdxs[I], |
| 33 | (*ModInst.getType(TypeIdxs[I]))->getCompositeType().getFuncType(), |
| 34 | std::move(Symbol)); |
| 35 | } |
| 36 | } else { |
| 37 | // Iterate through the code segments to instantiate function instances. |
| 38 | for (uint32_t I = 0; I < CodeSegs.size(); ++I) { |
| 39 | // Create and add the function instance to the module instance. |
| 40 | ModInst.addFunc( |
| 41 | TypeIdxs[I], |
| 42 | (*ModInst.getType(TypeIdxs[I]))->getCompositeType().getFuncType(), |
| 43 | CodeSegs[I].getLocals(), CodeSegs[I].getExpr().getInstrs()); |
| 44 | } |
| 45 | } |
| 46 | return {}; |
| 47 | } |
| 48 | |
| 49 | } // namespace Executor |
| 50 | } // namespace WasmEdge |