|
| 1 | +/* |
| 2 | + * Copyright 2025 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 | +#include "interpreter/interpreter.h" |
| 18 | +#include "interpreter/expression-iterator.h" |
| 19 | +#include "interpreter/store.h" |
| 20 | +#include "wasm-traversal.h" |
| 21 | + |
| 22 | +namespace wasm { |
| 23 | + |
| 24 | +using namespace interpreter; |
| 25 | + |
| 26 | +// Provides access to the interpreter's private store. |
| 27 | +class InterpreterImpl { |
| 28 | +public: |
| 29 | + static WasmStore& getStore(Interpreter& interpreter) { |
| 30 | + return interpreter.store; |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +namespace { |
| 35 | + |
| 36 | +struct Branch { |
| 37 | + Name label; |
| 38 | +}; |
| 39 | + |
| 40 | +// TODO: Handle other forms of control flow. |
| 41 | +struct Flow : std::variant<std::monostate, Branch> { |
| 42 | + operator bool() { return !std::get_if<std::monostate>(this); } |
| 43 | +}; |
| 44 | + |
| 45 | +struct ExpressionInterpreter : OverriddenVisitor<ExpressionInterpreter, Flow> { |
| 46 | + Interpreter& parent; |
| 47 | + ExpressionInterpreter(Interpreter& parent) : parent(parent) {} |
| 48 | + |
| 49 | + WasmStore& store() { return InterpreterImpl::getStore(parent); } |
| 50 | + void push(Literal val) { store().push(val); } |
| 51 | + Literal pop() { return store().pop(); } |
| 52 | + |
| 53 | + Flow visitNop(Nop* curr) { WASM_UNREACHABLE("TODO"); } |
| 54 | + Flow visitBlock(Block* curr) { WASM_UNREACHABLE("TODO"); } |
| 55 | + Flow visitIf(If* curr) { WASM_UNREACHABLE("TODO"); } |
| 56 | + Flow visitLoop(Loop* curr) { WASM_UNREACHABLE("TODO"); } |
| 57 | + Flow visitBreak(Break* curr) { WASM_UNREACHABLE("TODO"); } |
| 58 | + Flow visitSwitch(Switch* curr) { WASM_UNREACHABLE("TODO"); } |
| 59 | + Flow visitCall(Call* curr) { WASM_UNREACHABLE("TODO"); } |
| 60 | + Flow visitCallIndirect(CallIndirect* curr) { WASM_UNREACHABLE("TODO"); } |
| 61 | + Flow visitLocalGet(LocalGet* curr) { WASM_UNREACHABLE("TODO"); } |
| 62 | + Flow visitLocalSet(LocalSet* curr) { WASM_UNREACHABLE("TODO"); } |
| 63 | + Flow visitGlobalGet(GlobalGet* curr) { WASM_UNREACHABLE("TODO"); } |
| 64 | + Flow visitGlobalSet(GlobalSet* curr) { WASM_UNREACHABLE("TODO"); } |
| 65 | + Flow visitLoad(Load* curr) { WASM_UNREACHABLE("TODO"); } |
| 66 | + Flow visitStore(Store* curr) { WASM_UNREACHABLE("TODO"); } |
| 67 | + Flow visitAtomicRMW(AtomicRMW* curr) { WASM_UNREACHABLE("TODO"); } |
| 68 | + Flow visitAtomicCmpxchg(AtomicCmpxchg* curr) { WASM_UNREACHABLE("TODO"); } |
| 69 | + Flow visitAtomicWait(AtomicWait* curr) { WASM_UNREACHABLE("TODO"); } |
| 70 | + Flow visitAtomicNotify(AtomicNotify* curr) { WASM_UNREACHABLE("TODO"); } |
| 71 | + Flow visitAtomicFence(AtomicFence* curr) { WASM_UNREACHABLE("TODO"); } |
| 72 | + Flow visitSIMDExtract(SIMDExtract* curr) { WASM_UNREACHABLE("TODO"); } |
| 73 | + Flow visitSIMDReplace(SIMDReplace* curr) { WASM_UNREACHABLE("TODO"); } |
| 74 | + Flow visitSIMDShuffle(SIMDShuffle* curr) { WASM_UNREACHABLE("TODO"); } |
| 75 | + Flow visitSIMDTernary(SIMDTernary* curr) { WASM_UNREACHABLE("TODO"); } |
| 76 | + Flow visitSIMDShift(SIMDShift* curr) { WASM_UNREACHABLE("TODO"); } |
| 77 | + Flow visitSIMDLoad(SIMDLoad* curr) { WASM_UNREACHABLE("TODO"); } |
| 78 | + Flow visitSIMDLoadStoreLane(SIMDLoadStoreLane* curr) { |
| 79 | + WASM_UNREACHABLE("TODO"); |
| 80 | + } |
| 81 | + Flow visitMemoryInit(MemoryInit* curr) { WASM_UNREACHABLE("TODO"); } |
| 82 | + Flow visitDataDrop(DataDrop* curr) { WASM_UNREACHABLE("TODO"); } |
| 83 | + Flow visitMemoryCopy(MemoryCopy* curr) { WASM_UNREACHABLE("TODO"); } |
| 84 | + Flow visitMemoryFill(MemoryFill* curr) { WASM_UNREACHABLE("TODO"); } |
| 85 | + Flow visitConst(Const* curr) { |
| 86 | + push(curr->value); |
| 87 | + return {}; |
| 88 | + } |
| 89 | + Flow visitUnary(Unary* curr) { WASM_UNREACHABLE("TODO"); } |
| 90 | + Flow visitBinary(Binary* curr) { |
| 91 | + auto rhs = pop(); |
| 92 | + auto lhs = pop(); |
| 93 | + // TODO: switch-case over all operations. |
| 94 | + if (curr->op == AddInt32) { |
| 95 | + push(lhs.add(rhs)); |
| 96 | + return {}; |
| 97 | + } |
| 98 | + WASM_UNREACHABLE("TODO"); |
| 99 | + } |
| 100 | + Flow visitSelect(Select* curr) { WASM_UNREACHABLE("TODO"); } |
| 101 | + Flow visitDrop(Drop* curr) { WASM_UNREACHABLE("TODO"); } |
| 102 | + Flow visitReturn(Return* curr) { WASM_UNREACHABLE("TODO"); } |
| 103 | + Flow visitMemorySize(MemorySize* curr) { WASM_UNREACHABLE("TODO"); } |
| 104 | + Flow visitMemoryGrow(MemoryGrow* curr) { WASM_UNREACHABLE("TODO"); } |
| 105 | + Flow visitUnreachable(Unreachable* curr) { WASM_UNREACHABLE("TODO"); } |
| 106 | + Flow visitPop(Pop* curr) { WASM_UNREACHABLE("TODO"); } |
| 107 | + Flow visitRefNull(RefNull* curr) { WASM_UNREACHABLE("TODO"); } |
| 108 | + Flow visitRefIsNull(RefIsNull* curr) { WASM_UNREACHABLE("TODO"); } |
| 109 | + Flow visitRefFunc(RefFunc* curr) { WASM_UNREACHABLE("TODO"); } |
| 110 | + Flow visitRefEq(RefEq* curr) { WASM_UNREACHABLE("TODO"); } |
| 111 | + Flow visitTableGet(TableGet* curr) { WASM_UNREACHABLE("TODO"); } |
| 112 | + Flow visitTableSet(TableSet* curr) { WASM_UNREACHABLE("TODO"); } |
| 113 | + Flow visitTableSize(TableSize* curr) { WASM_UNREACHABLE("TODO"); } |
| 114 | + Flow visitTableGrow(TableGrow* curr) { WASM_UNREACHABLE("TODO"); } |
| 115 | + Flow visitTableFill(TableFill* curr) { WASM_UNREACHABLE("TODO"); } |
| 116 | + Flow visitTableCopy(TableCopy* curr) { WASM_UNREACHABLE("TODO"); } |
| 117 | + Flow visitTableInit(TableInit* curr) { WASM_UNREACHABLE("TODO"); } |
| 118 | + Flow visitTry(Try* curr) { WASM_UNREACHABLE("TODO"); } |
| 119 | + Flow visitTryTable(TryTable* curr) { WASM_UNREACHABLE("TODO"); } |
| 120 | + Flow visitThrow(Throw* curr) { WASM_UNREACHABLE("TODO"); } |
| 121 | + Flow visitRethrow(Rethrow* curr) { WASM_UNREACHABLE("TODO"); } |
| 122 | + Flow visitThrowRef(ThrowRef* curr) { WASM_UNREACHABLE("TODO"); } |
| 123 | + Flow visitTupleMake(TupleMake* curr) { WASM_UNREACHABLE("TODO"); } |
| 124 | + Flow visitTupleExtract(TupleExtract* curr) { WASM_UNREACHABLE("TODO"); } |
| 125 | + Flow visitRefI31(RefI31* curr) { WASM_UNREACHABLE("TODO"); } |
| 126 | + Flow visitI31Get(I31Get* curr) { WASM_UNREACHABLE("TODO"); } |
| 127 | + Flow visitCallRef(CallRef* curr) { WASM_UNREACHABLE("TODO"); } |
| 128 | + Flow visitRefTest(RefTest* curr) { WASM_UNREACHABLE("TODO"); } |
| 129 | + Flow visitRefCast(RefCast* curr) { WASM_UNREACHABLE("TODO"); } |
| 130 | + Flow visitBrOn(BrOn* curr) { WASM_UNREACHABLE("TODO"); } |
| 131 | + Flow visitStructNew(StructNew* curr) { WASM_UNREACHABLE("TODO"); } |
| 132 | + Flow visitStructGet(StructGet* curr) { WASM_UNREACHABLE("TODO"); } |
| 133 | + Flow visitStructSet(StructSet* curr) { WASM_UNREACHABLE("TODO"); } |
| 134 | + Flow visitStructRMW(StructRMW* curr) { WASM_UNREACHABLE("TODO"); } |
| 135 | + Flow visitStructCmpxchg(StructCmpxchg* curr) { WASM_UNREACHABLE("TODO"); } |
| 136 | + Flow visitArrayNew(ArrayNew* curr) { WASM_UNREACHABLE("TODO"); } |
| 137 | + Flow visitArrayNewData(ArrayNewData* curr) { WASM_UNREACHABLE("TODO"); } |
| 138 | + Flow visitArrayNewElem(ArrayNewElem* curr) { WASM_UNREACHABLE("TODO"); } |
| 139 | + Flow visitArrayNewFixed(ArrayNewFixed* curr) { WASM_UNREACHABLE("TODO"); } |
| 140 | + Flow visitArrayGet(ArrayGet* curr) { WASM_UNREACHABLE("TODO"); } |
| 141 | + Flow visitArraySet(ArraySet* curr) { WASM_UNREACHABLE("TODO"); } |
| 142 | + Flow visitArrayLen(ArrayLen* curr) { WASM_UNREACHABLE("TODO"); } |
| 143 | + Flow visitArrayCopy(ArrayCopy* curr) { WASM_UNREACHABLE("TODO"); } |
| 144 | + Flow visitArrayFill(ArrayFill* curr) { WASM_UNREACHABLE("TODO"); } |
| 145 | + Flow visitArrayInitData(ArrayInitData* curr) { WASM_UNREACHABLE("TODO"); } |
| 146 | + Flow visitArrayInitElem(ArrayInitElem* curr) { WASM_UNREACHABLE("TODO"); } |
| 147 | + Flow visitRefAs(RefAs* curr) { WASM_UNREACHABLE("TODO"); } |
| 148 | + Flow visitStringNew(StringNew* curr) { WASM_UNREACHABLE("TODO"); } |
| 149 | + Flow visitStringConst(StringConst* curr) { WASM_UNREACHABLE("TODO"); } |
| 150 | + Flow visitStringMeasure(StringMeasure* curr) { WASM_UNREACHABLE("TODO"); } |
| 151 | + Flow visitStringEncode(StringEncode* curr) { WASM_UNREACHABLE("TODO"); } |
| 152 | + Flow visitStringConcat(StringConcat* curr) { WASM_UNREACHABLE("TODO"); } |
| 153 | + Flow visitStringEq(StringEq* curr) { WASM_UNREACHABLE("TODO"); } |
| 154 | + Flow visitStringWTF16Get(StringWTF16Get* curr) { WASM_UNREACHABLE("TODO"); } |
| 155 | + Flow visitStringSliceWTF(StringSliceWTF* curr) { WASM_UNREACHABLE("TODO"); } |
| 156 | + Flow visitContBind(ContBind* curr) { WASM_UNREACHABLE("TODO"); } |
| 157 | + Flow visitContNew(ContNew* curr) { WASM_UNREACHABLE("TODO"); } |
| 158 | + Flow visitResume(Resume* curr) { WASM_UNREACHABLE("TODO"); } |
| 159 | + Flow visitSuspend(Suspend* curr) { WASM_UNREACHABLE("TODO"); } |
| 160 | +}; |
| 161 | + |
| 162 | +} // anonymous namespace |
| 163 | + |
| 164 | +std::vector<Literal> Interpreter::run(Expression* root) { |
| 165 | + // Create a fresh store and execution frame, then run the expression to |
| 166 | + // completion. |
| 167 | + store = WasmStore(); |
| 168 | + store.callStack.emplace_back(); |
| 169 | + store.callStack.back().exprs = ExpressionIterator(root); |
| 170 | + |
| 171 | + ExpressionInterpreter interpreter(*this); |
| 172 | + while (auto& it = store.callStack.back().exprs) { |
| 173 | + if (auto flow = interpreter.visit(*it)) { |
| 174 | + // TODO: Handle control flow transfers. |
| 175 | + } else { |
| 176 | + ++it; |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + return store.callStack.back().valueStack; |
| 181 | +} |
| 182 | + |
| 183 | +} // namespace wasm |
0 commit comments