1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/*
* Copyright 2016 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef WABT_INTERPRETER_H_
#define WABT_INTERPRETER_H_
#include <stdint.h>
#include <memory>
#include <vector>
#include "common.h"
#include "binding-hash.h"
#include "opcode.h"
#include "writer.h"
namespace wabt {
class Stream;
#define FOREACH_INTERPRETER_RESULT(V) \
V(Ok, "ok") \
/* returned from the top-most function */ \
V(Returned, "returned") \
/* memory access is out of bounds */ \
V(TrapMemoryAccessOutOfBounds, "out of bounds memory access") \
/* converting from float -> int would overflow int */ \
V(TrapIntegerOverflow, "integer overflow") \
/* dividend is zero in integer divide */ \
V(TrapIntegerDivideByZero, "integer divide by zero") \
/* converting from float -> int where float is nan */ \
V(TrapInvalidConversionToInteger, "invalid conversion to integer") \
/* function table index is out of bounds */ \
V(TrapUndefinedTableIndex, "undefined table index") \
/* function table element is uninitialized */ \
V(TrapUninitializedTableElement, "uninitialized table element") \
/* unreachable instruction executed */ \
V(TrapUnreachable, "unreachable executed") \
/* call indirect signature doesn't match function table signature */ \
V(TrapIndirectCallSignatureMismatch, "indirect call signature mismatch") \
/* ran out of call stack frames (probably infinite recursion) */ \
V(TrapCallStackExhausted, "call stack exhausted") \
/* ran out of value stack space */ \
V(TrapValueStackExhausted, "value stack exhausted") \
/* we called a host function, but the return value didn't match the */ \
/* expected type */ \
V(TrapHostResultTypeMismatch, "host result type mismatch") \
/* we called an import function, but it didn't complete succesfully */ \
V(TrapHostTrapped, "host function trapped") \
/* we attempted to call a function with the an argument list that doesn't \
* match the function signature */ \
V(ArgumentTypeMismatch, "argument type mismatch") \
/* we tried to get an export by name that doesn't exist */ \
V(UnknownExport, "unknown export") \
/* the expected export kind doesn't match. */ \
V(ExportKindMismatch, "export kind mismatch")
enum class InterpreterResult {
#define V(Name, str) Name,
FOREACH_INTERPRETER_RESULT(V)
#undef V
};
#define WABT_INVALID_INDEX static_cast<uint32_t>(~0)
#define WABT_INVALID_OFFSET static_cast<uint32_t>(~0)
#define WABT_TABLE_ENTRY_SIZE (sizeof(uint32_t) * 2 + sizeof(uint8_t))
#define WABT_TABLE_ENTRY_OFFSET_OFFSET 0
#define WABT_TABLE_ENTRY_DROP_OFFSET sizeof(uint32_t)
#define WABT_TABLE_ENTRY_KEEP_OFFSET (sizeof(uint32_t) * 2)
enum class InterpreterOpcode {
/* push space on the value stack for N entries */
#define WABT_OPCODE(rtype, type1, type2, mem_size, code, Name, text) Name = code,
#include "interpreter-opcode.def"
#undef WABT_OPCODE
First = static_cast<int>(Opcode::First),
Last = DropKeep,
};
static const int kInterpreterOpcodeCount = WABT_ENUM_COUNT(InterpreterOpcode);
struct InterpreterFuncSignature {
std::vector<Type> param_types;
std::vector<Type> result_types;
};
struct InterpreterTable {
explicit InterpreterTable(const Limits& limits)
: limits(limits), func_indexes(limits.initial, WABT_INVALID_INDEX) {}
Limits limits;
std::vector<uint32_t> func_indexes;
};
struct InterpreterMemory {
InterpreterMemory() {
WABT_ZERO_MEMORY(page_limits);
}
explicit InterpreterMemory(const Limits& limits)
: page_limits(limits), data(limits.initial * WABT_PAGE_SIZE) {}
Limits page_limits;
std::vector<char> data;
};
union InterpreterValue {
uint32_t i32;
uint64_t i64;
uint32_t f32_bits;
uint64_t f64_bits;
};
struct InterpreterTypedValue {
InterpreterTypedValue() {}
explicit InterpreterTypedValue(Type type): type(type) {}
InterpreterTypedValue(Type type, const InterpreterValue& value)
: type(type), value(value) {}
Type type;
InterpreterValue value;
};
struct InterpreterGlobal {
InterpreterGlobal() : mutable_(false), import_index(WABT_INVALID_INDEX) {}
InterpreterGlobal(const InterpreterTypedValue& typed_value, bool mutable_)
: typed_value(typed_value), mutable_(mutable_) {}
InterpreterTypedValue typed_value;
bool mutable_;
uint32_t import_index; /* or INVALID_INDEX if not imported */
};
struct InterpreterImport {
InterpreterImport();
InterpreterImport(InterpreterImport&&);
InterpreterImport& operator=(InterpreterImport&&);
~InterpreterImport();
StringSlice module_name;
StringSlice field_name;
ExternalKind kind;
union {
struct {
uint32_t sig_index;
} func;
struct {
Limits limits;
} table, memory;
struct {
Type type;
bool mutable_;
} global;
};
};
struct InterpreterFunc;
typedef Result (*InterpreterHostFuncCallback)(
const struct HostInterpreterFunc* func,
const InterpreterFuncSignature* sig,
uint32_t num_args,
InterpreterTypedValue* args,
uint32_t num_results,
InterpreterTypedValue* out_results,
void* user_data);
struct InterpreterFunc {
WABT_DISALLOW_COPY_AND_ASSIGN(InterpreterFunc);
InterpreterFunc(uint32_t sig_index, bool is_host)
: sig_index(sig_index), is_host(is_host) {}
virtual ~InterpreterFunc() {}
inline struct DefinedInterpreterFunc* as_defined();
inline struct HostInterpreterFunc* as_host();
uint32_t sig_index;
bool is_host;
};
struct DefinedInterpreterFunc : InterpreterFunc {
DefinedInterpreterFunc(uint32_t sig_index)
: InterpreterFunc(sig_index, false),
offset(WABT_INVALID_INDEX),
local_decl_count(0),
local_count(0) {}
uint32_t offset;
uint32_t local_decl_count;
uint32_t local_count;
std::vector<Type> param_and_local_types;
};
struct HostInterpreterFunc : InterpreterFunc {
HostInterpreterFunc(const StringSlice& module_name,
const StringSlice& field_name,
uint32_t sig_index)
: InterpreterFunc(sig_index, true),
module_name(module_name),
field_name(field_name) {}
StringSlice module_name;
StringSlice field_name;
InterpreterHostFuncCallback callback;
void* user_data;
};
DefinedInterpreterFunc* InterpreterFunc::as_defined() {
assert(!is_host);
return static_cast<DefinedInterpreterFunc*>(this);
}
HostInterpreterFunc* InterpreterFunc::as_host() {
assert(is_host);
return static_cast<HostInterpreterFunc*>(this);
}
struct InterpreterExport {
InterpreterExport(const StringSlice& name, ExternalKind kind, uint32_t index)
: name(name), kind(kind), index(index) {}
InterpreterExport(InterpreterExport&&);
InterpreterExport& operator=(InterpreterExport&&);
~InterpreterExport();
StringSlice name;
ExternalKind kind;
uint32_t index;
};
struct PrintErrorCallback {
void* user_data;
void (*print_error)(const char* msg, void* user_data);
};
struct InterpreterHostImportDelegate {
void* user_data;
Result (*import_func)(InterpreterImport*,
InterpreterFunc*,
InterpreterFuncSignature*,
PrintErrorCallback,
void* user_data);
Result (*import_table)(InterpreterImport*,
InterpreterTable*,
PrintErrorCallback,
void* user_data);
Result (*import_memory)(InterpreterImport*,
InterpreterMemory*,
PrintErrorCallback,
void* user_data);
Result (*import_global)(InterpreterImport*,
InterpreterGlobal*,
PrintErrorCallback,
void* user_data);
};
struct InterpreterModule {
WABT_DISALLOW_COPY_AND_ASSIGN(InterpreterModule);
explicit InterpreterModule(bool is_host);
InterpreterModule(const StringSlice& name, bool is_host);
virtual ~InterpreterModule();
inline struct DefinedInterpreterModule* as_defined();
inline struct HostInterpreterModule* as_host();
StringSlice name;
std::vector<InterpreterExport> exports;
BindingHash export_bindings;
uint32_t memory_index; /* INVALID_INDEX if not defined */
uint32_t table_index; /* INVALID_INDEX if not defined */
bool is_host;
};
struct DefinedInterpreterModule : InterpreterModule {
explicit DefinedInterpreterModule(size_t istream_start);
std::vector<InterpreterImport> imports;
uint32_t start_func_index; /* INVALID_INDEX if not defined */
size_t istream_start;
size_t istream_end;
};
struct HostInterpreterModule : InterpreterModule {
HostInterpreterModule(const StringSlice& name);
InterpreterHostImportDelegate import_delegate;
};
DefinedInterpreterModule* InterpreterModule::as_defined() {
assert(!is_host);
return static_cast<DefinedInterpreterModule*>(this);
}
HostInterpreterModule* InterpreterModule::as_host() {
assert(is_host);
return static_cast<HostInterpreterModule*>(this);
}
/* Used to track and reset the state of the environment. */
struct InterpreterEnvironmentMark {
size_t modules_size;
size_t sigs_size;
size_t funcs_size;
size_t memories_size;
size_t tables_size;
size_t globals_size;
size_t istream_size;
};
struct InterpreterEnvironment {
InterpreterEnvironment();
std::vector<std::unique_ptr<InterpreterModule>> modules;
std::vector<InterpreterFuncSignature> sigs;
std::vector<std::unique_ptr<InterpreterFunc>> funcs;
std::vector<InterpreterMemory> memories;
std::vector<InterpreterTable> tables;
std::vector<InterpreterGlobal> globals;
std::unique_ptr<OutputBuffer> istream;
BindingHash module_bindings;
BindingHash registered_module_bindings;
};
struct InterpreterThread {
InterpreterThread();
InterpreterEnvironment* env;
std::vector<InterpreterValue> value_stack;
std::vector<uint32_t> call_stack;
InterpreterValue* value_stack_top;
InterpreterValue* value_stack_end;
uint32_t* call_stack_top;
uint32_t* call_stack_end;
uint32_t pc;
};
#define WABT_INTERPRETER_THREAD_OPTIONS_DEFAULT \
{ 512 * 1024 / sizeof(InterpreterValue), 64 * 1024, WABT_INVALID_OFFSET }
struct InterpreterThreadOptions {
uint32_t value_stack_size;
uint32_t call_stack_size;
uint32_t pc;
};
bool is_canonical_nan_f32(uint32_t f32_bits);
bool is_canonical_nan_f64(uint64_t f64_bits);
bool is_arithmetic_nan_f32(uint32_t f32_bits);
bool is_arithmetic_nan_f64(uint64_t f64_bits);
bool func_signatures_are_equal(InterpreterEnvironment* env,
uint32_t sig_index_0,
uint32_t sig_index_1);
void destroy_interpreter_environment(InterpreterEnvironment* env);
InterpreterEnvironmentMark mark_interpreter_environment(
InterpreterEnvironment* env);
void reset_interpreter_environment_to_mark(InterpreterEnvironment* env,
InterpreterEnvironmentMark mark);
HostInterpreterModule* append_host_module(InterpreterEnvironment* env,
StringSlice name);
void init_interpreter_thread(InterpreterEnvironment* env,
InterpreterThread* thread,
InterpreterThreadOptions* options);
InterpreterResult push_thread_value(InterpreterThread* thread,
InterpreterValue value);
void destroy_interpreter_thread(InterpreterThread* thread);
InterpreterResult call_host(InterpreterThread* thread,
HostInterpreterFunc* func);
InterpreterResult run_interpreter(InterpreterThread* thread,
uint32_t num_instructions,
uint32_t* call_stack_return_top);
void trace_pc(InterpreterThread* thread, Stream* stream);
void disassemble(InterpreterEnvironment* env,
Stream* stream,
uint32_t from,
uint32_t to);
void disassemble_module(InterpreterEnvironment* env,
Stream* stream,
InterpreterModule* module);
InterpreterExport* get_interpreter_export_by_name(InterpreterModule* module,
const StringSlice* name);
} // namespace wabt
#endif /* WABT_INTERPRETER_H_ */
|