Coverage Report

Created: 2025-07-01 06:18

/src/WasmEdge/lib/executor/instantiate/function.cpp
Line
Count
Source (jump to first uncovered line)
1
// SPDX-License-Identifier: Apache-2.0
2
// SPDX-FileCopyrightText: 2019-2024 Second State INC
3
4
#include "executor/executor.h"
5
6
#include <cstdint>
7
#include <utility>
8
9
namespace WasmEdge {
10
namespace Executor {
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
0
                                   const AST::CodeSection &CodeSec) {
16
17
  // Get the function type indices.
18
0
  auto TypeIdxs = FuncSec.getContent();
19
0
  auto CodeSegs = CodeSec.getContent();
20
21
0
  if (CodeSegs.size() == 0) {
22
0
    return {};
23
0
  }
24
  // The module will always choose the `for` loop in `else` case under
25
  // interpreter mode. Instead, if we do branch in the `for` loop which might
26
  // cause meaningless branch misses. Therefore we should check the first item
27
  // and dispatch it into different cases to reduce branch misses.
28
0
  if (CodeSegs[0].getSymbol() != false) {
29
0
    for (uint32_t I = 0; I < CodeSegs.size(); ++I) {
30
0
      auto Symbol = CodeSegs[I].getSymbol();
31
0
      ModInst.addFunc(
32
0
          TypeIdxs[I],
33
0
          (*ModInst.getType(TypeIdxs[I]))->getCompositeType().getFuncType(),
34
0
          std::move(Symbol));
35
0
    }
36
0
  } else {
37
    // Iterate through the code segments to instantiate function instances.
38
0
    for (uint32_t I = 0; I < CodeSegs.size(); ++I) {
39
      // Create and add the function instance into the module instance.
40
0
      ModInst.addFunc(
41
0
          TypeIdxs[I],
42
0
          (*ModInst.getType(TypeIdxs[I]))->getCompositeType().getFuncType(),
43
0
          CodeSegs[I].getLocals(), CodeSegs[I].getExpr().getInstrs());
44
0
    }
45
0
  }
46
0
  return {};
47
0
}
48
49
} // namespace Executor
50
} // namespace WasmEdge