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
|
#pragma once
#include <string.h>
#include <vector>
#include "WAVM/IR/IR.h"
#include "WAVM/IR/Types.h"
#include "WAVM/Inline/Assert.h"
#include "WAVM/Inline/BasicTypes.h"
#include "WAVM/Inline/Serialization.h"
#include "WAVM/Platform/Defines.h"
#include "OperatorTable.h"
namespace WAVM { namespace IR {
// Forward declarations.
struct Module;
// Structures for operator immediates
struct NoImm
{
};
struct MemoryImm
{
Uptr memoryIndex;
};
struct MemoryCopyImm
{
Uptr destMemoryIndex;
Uptr sourceMemoryIndex;
};
struct TableImm
{
Uptr tableIndex;
};
struct TableCopyImm
{
Uptr destTableIndex;
Uptr sourceTableIndex;
};
struct ControlStructureImm
{
IndexedBlockType type;
};
struct SelectImm
{
// ValueType::any represents a legacy select with the type inferred from the first operand
// following the condition.
ValueType type;
};
struct BranchImm
{
Uptr targetDepth;
};
struct BranchTableImm
{
Uptr defaultTargetDepth;
// An index into the FunctionDef's branchTables array.
Uptr branchTableIndex;
};
template<typename Value> struct LiteralImm
{
Value value;
};
template<bool isGlobal> struct GetOrSetVariableImm
{
Uptr variableIndex;
};
struct FunctionImm
{
Uptr functionIndex;
};
struct FunctionRefImm
{
Uptr functionIndex;
};
struct CallIndirectImm
{
IndexedFunctionType type;
Uptr tableIndex;
};
struct BaseLoadOrStoreImm
{
U8 alignmentLog2;
U64 offset;
Uptr memoryIndex;
};
template<Uptr naturalAlignmentLog2> struct LoadOrStoreImm : BaseLoadOrStoreImm
{
};
template<Uptr naturalAlignmentLog2, Uptr numLanes>
struct LoadOrStoreLaneImm : LoadOrStoreImm<naturalAlignmentLog2>
{
U8 laneIndex;
};
using LoadOrStoreI8x16LaneImm = LoadOrStoreLaneImm<0, 16>;
using LoadOrStoreI16x8LaneImm = LoadOrStoreLaneImm<1, 8>;
using LoadOrStoreI32x4LaneImm = LoadOrStoreLaneImm<2, 4>;
using LoadOrStoreI64x2LaneImm = LoadOrStoreLaneImm<3, 2>;
template<Uptr numLanes> struct LaneIndexImm
{
U8 laneIndex;
};
template<Uptr numLanes> struct ShuffleImm
{
U8 laneIndices[numLanes];
};
template<Uptr naturalAlignmentLog2> struct AtomicLoadOrStoreImm : BaseLoadOrStoreImm
{
};
enum class MemoryOrder
{
sequentiallyConsistent = 0
};
struct AtomicFenceImm
{
MemoryOrder order;
};
struct ExceptionTypeImm
{
Uptr exceptionTypeIndex;
};
struct DelegateImm
{
Uptr catchDepth;
};
struct RethrowImm
{
Uptr catchDepth;
};
struct DataSegmentAndMemImm
{
Uptr dataSegmentIndex;
Uptr memoryIndex;
};
struct DataSegmentImm
{
Uptr dataSegmentIndex;
};
struct ElemSegmentAndTableImm
{
Uptr elemSegmentIndex;
Uptr tableIndex;
};
struct ElemSegmentImm
{
Uptr elemSegmentIndex;
};
struct ReferenceTypeImm
{
ReferenceType referenceType;
};
enum class Opcode : U16
{
#define VISIT_OPCODE(opcode, name, ...) name = opcode,
WAVM_ENUM_OPERATORS(VISIT_OPCODE)
#undef VISIT_OPCODE
};
static constexpr U64 maxSingleByteOpcode = 0xdf;
template<typename Imm> struct OpcodeAndImm
{
Opcode opcode;
Imm imm;
};
// Specialize for the empty immediate struct so they don't take an extra byte of space.
template<> struct OpcodeAndImm<NoImm>
{
union
{
Opcode opcode;
NoImm imm;
};
};
// Decodes an operator from an input stream and dispatches by opcode.
struct OperatorDecoderStream
{
OperatorDecoderStream(const std::vector<U8>& codeBytes)
: nextByte(codeBytes.data()), end(codeBytes.data() + codeBytes.size())
{
}
operator bool() const { return nextByte < end; }
template<typename Visitor> typename Visitor::Result decodeOp(Visitor& visitor)
{
WAVM_ASSERT(nextByte + sizeof(Opcode) <= end);
Opcode opcode;
memcpy(&opcode, nextByte, sizeof(Opcode));
switch(opcode)
{
#define VISIT_OPCODE(opcode, name, nameString, Imm, ...) \
case Opcode::name: { \
WAVM_ASSERT(nextByte + sizeof(OpcodeAndImm<Imm>) <= end); \
OpcodeAndImm<Imm> encodedOperator; \
memcpy(&encodedOperator, nextByte, sizeof(OpcodeAndImm<Imm>)); \
nextByte += sizeof(OpcodeAndImm<Imm>); \
return visitor.name(encodedOperator.imm); \
}
WAVM_ENUM_OPERATORS(VISIT_OPCODE)
#undef VISIT_OPCODE
default: WAVM_UNREACHABLE();
}
}
template<typename Visitor> typename Visitor::Result decodeOpWithoutConsume(Visitor& visitor)
{
const U8* savedNextByte = nextByte;
typename Visitor::Result result = decodeOp(visitor);
nextByte = savedNextByte;
return result;
}
private:
const U8* nextByte;
const U8* end;
};
// Encodes an operator to an output stream.
struct OperatorEncoderStream
{
OperatorEncoderStream(Serialization::OutputStream& inByteStream) : byteStream(inByteStream)
{
}
#define VISIT_OPCODE(_, name, nameString, Imm, ...) \
void name(Imm imm = {}) \
{ \
OpcodeAndImm<Imm> encodedOperator; \
encodedOperator.opcode = Opcode::name; \
encodedOperator.imm = imm; \
memcpy((OpcodeAndImm<Imm>*)byteStream.advance(sizeof(OpcodeAndImm<Imm>)), \
&encodedOperator, \
sizeof(OpcodeAndImm<Imm>)); \
}
WAVM_ENUM_OPERATORS(VISIT_OPCODE)
#undef VISIT_OPCODE
private:
Serialization::OutputStream& byteStream;
};
WAVM_API const char* getOpcodeName(Opcode opcode);
}}
|