forked from WAVM/WAVM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypes.h
More file actions
342 lines (292 loc) · 11 KB
/
Copy pathTypes.h
File metadata and controls
342 lines (292 loc) · 11 KB
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
#pragma once
#include "Inline/BasicTypes.h"
#include "Inline/Errors.h"
#include "Inline/Floats.h"
#include "IR.h"
#include <vector>
#include <string>
namespace IR
{
// The type of a WebAssembly operand
enum class ValueType : U8
{
any = 0,
i32 = 1,
i64 = 2,
f32 = 3,
f64 = 4,
v128 = 5,
num,
max = num-1
};
template<ValueType type> struct ValueTypeInfo;
template<> struct ValueTypeInfo<ValueType::i32> { typedef I32 Value; };
template<> struct ValueTypeInfo<ValueType::i64> { typedef I64 Value; };
template<> struct ValueTypeInfo<ValueType::f32> { typedef F32 Value; };
template<> struct ValueTypeInfo<ValueType::f64> { typedef F64 Value; };
inline std::string asString(I32 value) { return std::to_string(value); }
inline std::string asString(I64 value) { return std::to_string(value); }
inline std::string asString(F32 value) { return Floats::asString(value); }
inline std::string asString(F64 value) { return Floats::asString(value); }
inline std::string asString(const V128& v128)
{
// buffer needs 44 characters:
// 0xHHHHHHHH 0xHHHHHHHH 0xHHHHHHHH 0xHHHHHHHH\0
char buffer[44];
snprintf(buffer,sizeof(buffer),"0x%.8x 0x%.8x 0x%.8x 0x%.8x",v128.u32[0],v128.u32[1],v128.u32[2],v128.u32[3]);
return std::string(buffer);
}
template<> struct ValueTypeInfo<ValueType::v128> { typedef V128 Value; };
inline U8 getTypeByteWidth(ValueType type)
{
switch(type)
{
case ValueType::i32: return 4;
case ValueType::i64: return 8;
case ValueType::f32: return 4;
case ValueType::f64: return 8;
case ValueType::v128: return 16;
default: Errors::unreachable();
};
}
inline U8 getTypeBitWidth(ValueType type)
{
return getTypeByteWidth(type) * 8;
}
inline const char* asString(ValueType type)
{
switch(type)
{
case ValueType::any: return "any";
case ValueType::i32: return "i32";
case ValueType::i64: return "i64";
case ValueType::f32: return "f32";
case ValueType::f64: return "f64";
case ValueType::v128: return "v128";
default: Errors::unreachable();
};
}
// The type of a WebAssembly operator result. Mostly the same as ValueType, but allows operators with no result (none).
enum class ResultType : U8
{
none = 0,
i32 = (U8)ValueType::i32,
i64 = (U8)ValueType::i64,
f32 = (U8)ValueType::f32,
f64 = (U8)ValueType::f64,
v128 = (U8)ValueType::v128,
num,
max = num-1,
};
inline Uptr getArity(ResultType returnType) { return returnType == ResultType::none ? 0 : 1; }
inline const char* asString(ResultType type)
{
switch(type)
{
case ResultType::i32: return "i32";
case ResultType::i64: return "i64";
case ResultType::f32: return "f32";
case ResultType::f64: return "f64";
case ResultType::v128: return "v128";
case ResultType::none: return "()";
default: Errors::unreachable();
};
}
// Conversion between ValueType and ResultType.
inline ValueType asValueType(ResultType type)
{
assert(type != ResultType::none);
return (ValueType)type;
}
inline ResultType asResultType(ValueType type)
{
assert(type != ValueType::any);
return (ResultType)type;
}
// Infer value and result types from a C type.
template<typename> constexpr IR::ValueType inferValueType();
template<> constexpr IR::ValueType inferValueType<I32>() { return IR::ValueType::i32; }
template<> constexpr IR::ValueType inferValueType<U32>() { return IR::ValueType::i32; }
template<> constexpr IR::ValueType inferValueType<I64>() { return IR::ValueType::i64; }
template<> constexpr IR::ValueType inferValueType<U64>() { return IR::ValueType::i64; }
template<> constexpr IR::ValueType inferValueType<F32>() { return IR::ValueType::f32; }
template<> constexpr IR::ValueType inferValueType<F64>() { return IR::ValueType::f64; }
template<typename T> constexpr IR::ResultType inferResultType() { return IR::ResultType(inferValueType<T>()); }
template<> constexpr IR::ResultType inferResultType<void>() { return IR::ResultType::none; }
// The type of a WebAssembly function
struct FunctionType
{
ResultType ret;
std::vector<ValueType> parameters;
IR_API static const FunctionType* get(ResultType ret,const std::initializer_list<ValueType>& parameters);
IR_API static const FunctionType* get(ResultType ret,const std::vector<ValueType>& parameters);
IR_API static const FunctionType* get(ResultType ret = ResultType::none);
private:
FunctionType(ResultType inRet,const std::vector<ValueType>& inParameters)
: ret(inRet), parameters(inParameters) {}
};
struct IndexedFunctionType
{
U32 index;
};
inline std::string asString(const std::vector<ValueType>& typeTuple)
{
std::string result = "(";
for(Uptr parameterIndex = 0;parameterIndex < typeTuple.size();++parameterIndex)
{
if(parameterIndex != 0) { result += ','; }
result += asString(typeTuple[parameterIndex]);
}
result += ")";
return result;
}
inline std::string asString(const FunctionType* functionType)
{
return asString(functionType->parameters) + "->" + asString(functionType->ret);
}
// A size constraint: a range of expected sizes for some size-constrained type.
// If max==UINT64_MAX, the maximum size is unbounded.
struct SizeConstraints
{
U64 min;
U64 max;
friend bool operator==(const SizeConstraints& left,const SizeConstraints& right) { return left.min == right.min && left.max == right.max; }
friend bool operator!=(const SizeConstraints& left,const SizeConstraints& right) { return left.min != right.min || left.max != right.max; }
friend bool isSubset(const SizeConstraints& super,const SizeConstraints& sub)
{
return sub.min >= super.min && sub.max <= super.max;
}
};
// The type of element a table contains: for now can only be anyfunc, meaning any function type.
enum class TableElementType : U8
{
anyfunc = 0x70
};
// The type of a table
struct TableType
{
TableElementType elementType;
bool isShared;
SizeConstraints size;
TableType(): elementType(TableElementType::anyfunc), isShared(false), size({0,UINT64_MAX}) {}
TableType(TableElementType inElementType,bool inIsShared,SizeConstraints inSize)
: elementType(inElementType), isShared(inIsShared), size(inSize) {}
friend bool operator==(const TableType& left,const TableType& right)
{ return left.elementType == right.elementType && left.isShared == right.isShared && left.size == right.size; }
friend bool operator!=(const TableType& left,const TableType& right)
{ return left.elementType != right.elementType || left.isShared != right.isShared || left.size != right.size; }
friend bool isSubset(const TableType& super,const TableType& sub)
{ return super.elementType == sub.elementType && super.isShared == sub.isShared && isSubset(super.size,sub.size); }
};
// The type of a memory
struct MemoryType
{
bool isShared;
SizeConstraints size;
MemoryType(): isShared(false), size({0,UINT64_MAX}) {}
MemoryType(bool inIsShared,const SizeConstraints& inSize): isShared(inIsShared), size(inSize) {}
friend bool operator==(const MemoryType& left,const MemoryType& right) { return left.isShared == right.isShared && left.size == right.size; }
friend bool operator!=(const MemoryType& left,const MemoryType& right) { return left.isShared != right.isShared || left.size != right.size; }
friend bool isSubset(const MemoryType& super,const MemoryType& sub) { return super.isShared == sub.isShared && isSubset(super.size,sub.size); }
};
// The type of a global
struct GlobalType
{
ValueType valueType;
bool isMutable;
GlobalType(): valueType(ValueType::any), isMutable(false) {}
GlobalType(ValueType inValueType,bool inIsMutable): valueType(inValueType), isMutable(inIsMutable) {}
friend bool operator==(const GlobalType& left,const GlobalType& right) { return left.valueType == right.valueType && left.isMutable == right.isMutable; }
friend bool operator!=(const GlobalType& left,const GlobalType& right) { return left.valueType != right.valueType || left.isMutable != right.isMutable; }
friend bool operator<=(const GlobalType& left,const GlobalType& right) { return left.valueType == right.valueType && left.isMutable == right.isMutable; }
friend bool operator>(const GlobalType& left,const GlobalType& right) { return !(left <= right); }
};
inline std::string asString(const GlobalType& globalType)
{
if(globalType.isMutable) { return std::string("global ") + asString(globalType.valueType); }
else { return std::string("immutable ") + asString(globalType.valueType); }
}
// The type of a tuple
struct TupleType
{
std::vector<ValueType> elements;
IR_API static const TupleType* get(const std::initializer_list<ValueType>& elements);
IR_API static const TupleType* get(const std::vector<ValueType>& elements);
private:
TupleType(const std::vector<ValueType>& inElements)
: elements(inElements) {}
};
inline std::string asString(const TupleType* tupleType)
{
return asString(tupleType->elements);
}
// The type of an object
enum class ObjectKind : U8
{
// Standard object kinds that may be imported/exported from WebAssembly modules.
function = 0,
table = 1,
memory = 2,
global = 3,
exceptionType = 4,
max = 4,
invalid = 0xff,
};
struct ObjectType
{
const ObjectKind kind;
ObjectType() : kind(ObjectKind::invalid) {}
ObjectType(const FunctionType* inFunction) : kind(ObjectKind::function), function(inFunction) {}
ObjectType(TableType inTable) : kind(ObjectKind::table), table(inTable) {}
ObjectType(MemoryType inMemory) : kind(ObjectKind::memory), memory(inMemory) {}
ObjectType(GlobalType inGlobal) : kind(ObjectKind::global), global(inGlobal) {}
ObjectType(const TupleType* inExceptionType): kind(ObjectKind::exceptionType), exceptionType(inExceptionType) {}
ObjectType(ObjectKind inKind) : kind(inKind) {}
friend const FunctionType* asFunctionType(const ObjectType& objectType)
{
assert(objectType.kind == ObjectKind::function);
return objectType.function;
}
friend TableType asTableType(const ObjectType& objectType)
{
assert(objectType.kind == ObjectKind::table);
return objectType.table;
}
friend MemoryType asMemoryType(const ObjectType& objectType)
{
assert(objectType.kind == ObjectKind::memory);
return objectType.memory;
}
friend GlobalType asGlobalType(const ObjectType& objectType)
{
assert(objectType.kind == ObjectKind::global);
return objectType.global;
}
friend const TupleType* asExceptionTypeType(const ObjectType& objectType)
{
assert(objectType.kind == ObjectKind::exceptionType);
return objectType.exceptionType;
}
private:
union
{
const FunctionType* function;
TableType table;
MemoryType memory;
GlobalType global;
const TupleType* exceptionType;
};
};
inline std::string asString(const ObjectType& objectType)
{
switch(objectType.kind)
{
case ObjectKind::function: return "func " + asString(asFunctionType(objectType));
case ObjectKind::table: return "table";
case ObjectKind::memory: return "memory";
case ObjectKind::global: return asString(asGlobalType(objectType));
case ObjectKind::exceptionType: return "exception_type " + asString(asExceptionTypeType(objectType));
default: Errors::unreachable();
};
}
}