Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(607)

Side by Side Diff: src/wasm/wasm-interpreter.h

Issue 1972153002: [wasm] Implement an interpreter for WASM. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/v8.gyp ('k') | src/wasm/wasm-interpreter.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_WASM_INTERPRETER_H_
6 #define V8_WASM_INTERPRETER_H_
7
8 #include "src/wasm/wasm-opcodes.h"
9 #include "src/zone-containers.h"
10
11 namespace v8 {
12 namespace base {
13 class AccountingAllocator;
14 }
15
16 namespace internal {
17 namespace wasm {
18
19 // forward declarations.
20 struct WasmFunction;
21 struct WasmModuleInstance;
22 class WasmInterpreterInternals;
23
24 typedef size_t pc_t;
25 typedef size_t sp_t;
26 typedef int32_t pcdiff_t;
27 typedef uint32_t spdiff_t;
28
29 // Visible for testing. A {ControlTransfer} helps the interpreter figure out
30 // the target program counter and stack manipulations for a branch.
31 struct ControlTransfer {
32 enum StackAction { kNoAction, kPopAndRepush, kPushVoid };
33 pcdiff_t pcdiff; // adjustment to the program counter (positive or negative).
34 spdiff_t spdiff; // number of elements to pop off the stack.
35 StackAction action; // action to perform on the stack.
36 };
37 typedef ZoneMap<pc_t, ControlTransfer> ControlTransferMap;
38
39 // Macro for defining union members.
40 #define FOREACH_UNION_MEMBER(V) \
41 V(i32, kAstI32, int32_t) \
42 V(u32, kAstI32, uint32_t) \
43 V(i64, kAstI64, int64_t) \
44 V(u64, kAstI64, uint64_t) \
45 V(f32, kAstF32, float) \
46 V(f64, kAstF64, double)
47
48 // Representation of values within the interpreter.
49 struct WasmVal {
50 LocalType type;
51 union {
52 #define DECLARE_FIELD(field, localtype, ctype) ctype field;
53 FOREACH_UNION_MEMBER(DECLARE_FIELD)
54 #undef DECLARE_FIELD
55 } val;
56
57 WasmVal() : type(kAstStmt) {}
58
59 #define DECLARE_CONSTRUCTOR(field, localtype, ctype) \
60 explicit WasmVal(ctype v) : type(localtype) { val.field = v; }
61 FOREACH_UNION_MEMBER(DECLARE_CONSTRUCTOR)
62 #undef DECLARE_CONSTRUCTOR
63
64 template <typename T>
65 T to() {
66 UNREACHABLE();
67 }
68 };
69
70 #define DECLARE_CAST(field, localtype, ctype) \
71 template <> \
72 inline ctype WasmVal::to() { \
73 CHECK_EQ(localtype, type); \
74 return val.field; \
75 }
76 FOREACH_UNION_MEMBER(DECLARE_CAST)
77 #undef DECLARE_CAST
78
79 template <>
80 inline void WasmVal::to() {
81 CHECK_EQ(kAstStmt, type);
82 }
83
84 // Representation of frames within the interpreter.
85 class WasmFrame {
86 public:
87 const WasmFunction* function() const { return function_; }
88 int pc() const { return pc_; }
89
90 private:
91 friend class WasmInterpreter;
92
93 WasmFrame(const WasmFunction* function, int pc, int fp, int sp)
94 : function_(function), pc_(pc), fp_(fp), sp_(sp) {}
95
96 const WasmFunction* function_;
97 int pc_;
98 int fp_;
99 int sp_;
100 };
101
102 // An interpreter capable of executing WASM.
103 class WasmInterpreter {
104 public:
105 // State machine for a Thread:
106 // +---------------Run()-----------+
107 // V |
108 // STOPPED ---Run()--> RUNNING ------Pause()-----+-> PAUSED <------+
109 // | | | / | |
110 // | | +---- Breakpoint ---+ +-- Step() --+
111 // | |
112 // | +------------ Trap --------------> TRAPPED
113 // +------------- Finish -------------> FINISHED
114 enum State { STOPPED, RUNNING, PAUSED, FINISHED, TRAPPED };
115
116 // Representation of a thread in the interpreter.
117 class Thread {
118 public:
119 // Execution control.
120 virtual State state() = 0;
121 virtual void PushFrame(const WasmFunction* function, WasmVal* args) = 0;
122 virtual State Run() = 0;
123 virtual State Step() = 0;
124 virtual void Pause() = 0;
125 virtual void Reset() = 0;
126 virtual ~Thread() {}
127
128 // Stack inspection and modification.
129 virtual int GetFrameCount() = 0;
130 virtual const WasmFrame* GetFrame(int index) = 0;
131 virtual WasmFrame* GetMutableFrame(int index) = 0;
132 virtual WasmVal GetReturnValue() = 0;
133
134 // Thread-specific breakpoints.
135 bool SetBreakpoint(const WasmFunction* function, int pc, bool enabled);
136 bool GetBreakpoint(const WasmFunction* function, int pc);
137 };
138
139 WasmInterpreter(WasmModuleInstance* instance,
140 base::AccountingAllocator* allocator);
141 ~WasmInterpreter();
142
143 //==========================================================================
144 // Execution controls.
145 //==========================================================================
146 void Run();
147 void Pause();
148
149 // Set a breakpoint at {pc} in {function} to be {enabled}. Returns the
150 // previous state of the breakpoint at {pc}.
151 bool SetBreakpoint(const WasmFunction* function, int pc, bool enabled);
152
153 // Gets the current state of the breakpoint at {function}.
154 bool GetBreakpoint(const WasmFunction* function, int pc);
155
156 // Enable or disable tracing for {function}. Return the previous state.
157 bool SetTracing(const WasmFunction* function, bool enabled);
158
159 //==========================================================================
160 // Thread iteration and inspection.
161 //==========================================================================
162 int GetThreadCount();
163 Thread& GetThread(int id);
164
165 //==========================================================================
166 // Stack frame inspection.
167 //==========================================================================
168 WasmVal GetLocalVal(const WasmFrame* frame, int index);
169 WasmVal GetExprVal(const WasmFrame* frame, int pc);
170 void SetLocalVal(WasmFrame* frame, int index, WasmVal val);
171 void SetExprVal(WasmFrame* frame, int pc, WasmVal val);
172
173 //==========================================================================
174 // Memory access.
175 //==========================================================================
176 size_t GetMemorySize();
177 WasmVal ReadMemory(size_t offset);
178 void WriteMemory(size_t offset, WasmVal val);
179
180 //==========================================================================
181 // Testing functionality.
182 //==========================================================================
183 // Manually adds a function to this interpreter, returning the index of the
184 // function.
185 int AddFunctionForTesting(const WasmFunction* function);
186 // Manually adds code to the interpreter for the given function.
187 bool SetFunctionCodeForTesting(const WasmFunction* function,
188 const byte* start, const byte* end);
189
190 // Computes the control targets for the given bytecode as {pc offset, sp
191 // offset}
192 // pairs. Used internally in the interpreter, but exposed for testing.
193 static ControlTransferMap ComputeControlTransfersForTesting(Zone* zone,
194 const byte* start,
195 const byte* end);
196
197 private:
198 Zone zone_;
199 WasmInterpreterInternals* internals_;
200 };
201
202 } // namespace wasm
203 } // namespace internal
204 } // namespace v8
205
206 #endif // V8_WASM_INTERPRETER_H_
OLDNEW
« no previous file with comments | « src/v8.gyp ('k') | src/wasm/wasm-interpreter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698