/src/WasmEdge/lib/executor/engine/tableInstr.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 | | namespace WasmEdge { |
7 | | namespace Executor { |
8 | | |
9 | | Expect<void> Executor::runTableGetOp(Runtime::StackManager &StackMgr, |
10 | | Runtime::Instance::TableInstance &TabInst, |
11 | 0 | const AST::Instruction &Instr) { |
12 | | // Pop Idx from Stack. |
13 | 0 | uint32_t Idx = StackMgr.pop().get<uint32_t>(); |
14 | | |
15 | | // Get table[Idx] and push to Stack. |
16 | 0 | return TabInst.getRefAddr(Idx) |
17 | 0 | .map_error([&Instr, &Idx](auto E) { |
18 | 0 | spdlog::error(ErrInfo::InfoInstruction(Instr.getOpCode(), |
19 | 0 | Instr.getOffset(), {Idx}, |
20 | 0 | {ValTypeFromType<uint32_t>()})); |
21 | 0 | return E; |
22 | 0 | }) |
23 | 0 | .and_then([&](auto Ref) -> Expect<void> { |
24 | 0 | StackMgr.push(Ref); |
25 | 0 | return {}; |
26 | 0 | }); |
27 | 0 | } |
28 | | |
29 | | Expect<void> Executor::runTableSetOp(Runtime::StackManager &StackMgr, |
30 | | Runtime::Instance::TableInstance &TabInst, |
31 | 0 | const AST::Instruction &Instr) { |
32 | | // Pop Ref from Stack. |
33 | 0 | RefVariant Ref = StackMgr.pop().get<RefVariant>(); |
34 | | |
35 | | // Pop Idx from Stack. |
36 | 0 | uint32_t Idx = StackMgr.pop().get<uint32_t>(); |
37 | | |
38 | | // Set table[Idx] with Ref. |
39 | 0 | return TabInst.setRefAddr(Idx, Ref).map_error([&Instr, &Idx](auto E) { |
40 | 0 | spdlog::error(ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset(), |
41 | 0 | {Idx}, |
42 | 0 | {ValTypeFromType<uint32_t>()})); |
43 | 0 | return E; |
44 | 0 | }); |
45 | 0 | } |
46 | | |
47 | | Expect<void> |
48 | | Executor::runTableInitOp(Runtime::StackManager &StackMgr, |
49 | | Runtime::Instance::TableInstance &TabInst, |
50 | | Runtime::Instance::ElementInstance &ElemInst, |
51 | 0 | const AST::Instruction &Instr) { |
52 | | // Pop the length, source, and destination from stack. |
53 | 0 | uint32_t Len = StackMgr.pop().get<uint32_t>(); |
54 | 0 | uint32_t Src = StackMgr.pop().get<uint32_t>(); |
55 | 0 | uint32_t Dst = StackMgr.pop().get<uint32_t>(); |
56 | | |
57 | | // Replace tab[Dst : Dst + Len] with elem[Src : Src + Len]. |
58 | 0 | return TabInst.setRefs(ElemInst.getRefs(), Dst, Src, Len) |
59 | 0 | .map_error([&Instr](auto E) { |
60 | 0 | spdlog::error( |
61 | 0 | ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset())); |
62 | 0 | return E; |
63 | 0 | }); |
64 | 0 | } |
65 | | |
66 | | Expect<void> |
67 | 0 | Executor::runElemDropOp(Runtime::Instance::ElementInstance &ElemInst) { |
68 | | // Clear element instance. |
69 | 0 | ElemInst.clear(); |
70 | 0 | return {}; |
71 | 0 | } |
72 | | |
73 | | Expect<void> |
74 | | Executor::runTableCopyOp(Runtime::StackManager &StackMgr, |
75 | | Runtime::Instance::TableInstance &TabInstDst, |
76 | | Runtime::Instance::TableInstance &TabInstSrc, |
77 | 0 | const AST::Instruction &Instr) { |
78 | | // Pop the length, source, and destination from stack. |
79 | 0 | uint32_t Len = StackMgr.pop().get<uint32_t>(); |
80 | 0 | uint32_t Src = StackMgr.pop().get<uint32_t>(); |
81 | 0 | uint32_t Dst = StackMgr.pop().get<uint32_t>(); |
82 | | |
83 | | // Replace tab_dst[Dst : Dst + Len] with tab_src[Src : Src + Len]. |
84 | 0 | return TabInstSrc.getRefs(0, Src + Len) |
85 | 0 | .and_then( |
86 | 0 | [&](auto Refs) { return TabInstDst.setRefs(Refs, Dst, Src, Len); }) |
87 | 0 | .map_error([&Instr](auto E) { |
88 | 0 | spdlog::error( |
89 | 0 | ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset())); |
90 | 0 | return E; |
91 | 0 | }); |
92 | 0 | } |
93 | | |
94 | | Expect<void> |
95 | | Executor::runTableGrowOp(Runtime::StackManager &StackMgr, |
96 | 0 | Runtime::Instance::TableInstance &TabInst) { |
97 | | // Pop N for growing size, Val for init ref value. |
98 | 0 | uint32_t N = StackMgr.pop().get<uint32_t>(); |
99 | 0 | ValVariant &Val = StackMgr.getTop(); |
100 | | |
101 | | // Grow size and push result. |
102 | 0 | const uint32_t CurrSize = TabInst.getSize(); |
103 | |
|
104 | 0 | if (TabInst.growTable(N, Val.get<RefVariant>())) { |
105 | 0 | Val.emplace<uint32_t>(CurrSize); |
106 | 0 | } else { |
107 | 0 | Val.emplace<int32_t>(INT32_C(-1)); |
108 | 0 | } |
109 | 0 | return {}; |
110 | 0 | } |
111 | | |
112 | | Expect<void> |
113 | | Executor::runTableSizeOp(Runtime::StackManager &StackMgr, |
114 | 0 | Runtime::Instance::TableInstance &TabInst) { |
115 | | // Push SZ = size to stack. |
116 | 0 | StackMgr.push(TabInst.getSize()); |
117 | 0 | return {}; |
118 | 0 | } |
119 | | |
120 | | Expect<void> Executor::runTableFillOp(Runtime::StackManager &StackMgr, |
121 | | Runtime::Instance::TableInstance &TabInst, |
122 | 0 | const AST::Instruction &Instr) { |
123 | | // Pop the length, ref_value, and offset from stack. |
124 | 0 | uint32_t Len = StackMgr.pop().get<uint32_t>(); |
125 | 0 | RefVariant Val = StackMgr.pop().get<RefVariant>(); |
126 | 0 | uint32_t Off = StackMgr.pop().get<uint32_t>(); |
127 | | |
128 | | // Fill refs with ref_value. |
129 | 0 | return TabInst.fillRefs(Val, Off, Len).map_error([&Instr](auto E) { |
130 | 0 | spdlog::error( |
131 | 0 | ErrInfo::InfoInstruction(Instr.getOpCode(), Instr.getOffset())); |
132 | 0 | return E; |
133 | 0 | }); |
134 | 0 | } |
135 | | |
136 | | } // namespace Executor |
137 | | } // namespace WasmEdge |