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