summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/wavm/Include/WAVM/IR/OperatorSignatures.h
blob: d67085b8030bc272da6da86a4ae2fd8fda9e96a4 (plain) (blame)
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
#pragma once

#include <initializer_list>
#include "WAVM/IR/Module.h"
#include "WAVM/IR/Operators.h"
#include "WAVM/IR/Types.h"

namespace WAVM { namespace IR {

	struct OpTypeTuple
	{
		static constexpr U8 maxTypes = 5;

		constexpr OpTypeTuple(std::initializer_list<ValueType> inTypes)
		: types{inTypes.size() > 0 ? inTypes.begin()[0] : ValueType::none,
				inTypes.size() > 1 ? inTypes.begin()[1] : ValueType::none,
				inTypes.size() > 2 ? inTypes.begin()[2] : ValueType::none,
				inTypes.size() > 3 ? inTypes.begin()[3] : ValueType::none,
				inTypes.size() > 4 ? inTypes.begin()[4] : ValueType::none}
		, numTypes(U8(inTypes.size()))
		{
		}

		ValueType operator[](Uptr index) const
		{
			WAVM_ASSERT(index < numTypes);
			return types[index];
		}

		U8 size() const { return numTypes; }

		const ValueType* data() const { return types; }

		const ValueType* begin() const { return &types[0]; }
		const ValueType* end() const { return &types[numTypes]; }

	private:
		ValueType types[maxTypes];
		U8 numTypes;
	};

	struct OpSignature
	{
		OpTypeTuple params;
		OpTypeTuple results;

		constexpr OpSignature(std::initializer_list<ValueType> inResults,
							  std::initializer_list<ValueType> inParams)
		: params(inParams), results(inResults)
		{
		}
	};

	namespace OpSignatures {
		// Monomorphic signatures.
		constexpr OpSignature none_to_none{{}, {}};
		constexpr OpSignature none_to_i32{{ValueType::i32}, {}};
		constexpr OpSignature none_to_i64{{ValueType::i64}, {}};
		constexpr OpSignature none_to_f32{{ValueType::f32}, {}};
		constexpr OpSignature none_to_f64{{ValueType::f64}, {}};
		constexpr OpSignature none_to_v128{{ValueType::v128}, {}};
		constexpr OpSignature none_to_funcref{{ValueType::funcref}, {}};

		constexpr OpSignature i32_to_i32{{ValueType::i32}, {ValueType::i32}};
		constexpr OpSignature i64_to_i64{{ValueType::i64}, {ValueType::i64}};
		constexpr OpSignature f32_to_f32{{ValueType::f32}, {ValueType::f32}};
		constexpr OpSignature f64_to_f64{{ValueType::f64}, {ValueType::f64}};
		constexpr OpSignature v128_to_v128{{ValueType::v128}, {ValueType::v128}};

		constexpr OpSignature i32_to_i64{{ValueType::i64}, {ValueType::i32}};
		constexpr OpSignature i32_to_f32{{ValueType::f32}, {ValueType::i32}};
		constexpr OpSignature i32_to_f64{{ValueType::f64}, {ValueType::i32}};
		constexpr OpSignature i32_to_v128{{ValueType::v128}, {ValueType::i32}};

		constexpr OpSignature i64_to_i32{{ValueType::i32}, {ValueType::i64}};
		constexpr OpSignature i64_to_f32{{ValueType::f32}, {ValueType::i64}};
		constexpr OpSignature i64_to_f64{{ValueType::f64}, {ValueType::i64}};
		constexpr OpSignature i64_to_v128{{ValueType::v128}, {ValueType::i64}};

		constexpr OpSignature f32_to_f64{{ValueType::f64}, {ValueType::f32}};
		constexpr OpSignature f32_to_i32{{ValueType::i32}, {ValueType::f32}};
		constexpr OpSignature f32_to_i64{{ValueType::i64}, {ValueType::f32}};
		constexpr OpSignature f32_to_v128{{ValueType::v128}, {ValueType::f32}};

		constexpr OpSignature f64_to_f32{{ValueType::f32}, {ValueType::f64}};
		constexpr OpSignature f64_to_i32{{ValueType::i32}, {ValueType::f64}};
		constexpr OpSignature f64_to_i64{{ValueType::i64}, {ValueType::f64}};
		constexpr OpSignature f64_to_v128{{ValueType::v128}, {ValueType::f64}};

		constexpr OpSignature v128_to_i32{{ValueType::i32}, {ValueType::v128}};
		constexpr OpSignature v128_to_i64{{ValueType::i64}, {ValueType::v128}};
		constexpr OpSignature v128_to_f32{{ValueType::f32}, {ValueType::v128}};
		constexpr OpSignature v128_to_f64{{ValueType::f64}, {ValueType::v128}};

		constexpr OpSignature i32_i32_to_i32{{ValueType::i32}, {ValueType::i32, ValueType::i32}};
		constexpr OpSignature i64_i64_to_i64{{ValueType::i64}, {ValueType::i64, ValueType::i64}};
		constexpr OpSignature f32_f32_to_f32{{ValueType::f32}, {ValueType::f32, ValueType::f32}};
		constexpr OpSignature f64_f64_to_f64{{ValueType::f64}, {ValueType::f64, ValueType::f64}};
		constexpr OpSignature v128_v128_to_v128{{ValueType::v128},
												{ValueType::v128, ValueType::v128}};

		constexpr OpSignature i64_i64_to_i32{{ValueType::i32}, {ValueType::i64, ValueType::i64}};
		constexpr OpSignature f32_f32_to_i32{{ValueType::i32}, {ValueType::f32, ValueType::f32}};
		constexpr OpSignature f64_f64_to_i32{{ValueType::i32}, {ValueType::f64, ValueType::f64}};

		constexpr OpSignature v128_v128_v128_to_v128{
			{ValueType::v128},
			{ValueType::v128, ValueType::v128, ValueType::v128}};

		constexpr OpSignature v128_i32_to_v128{{ValueType::v128},
											   {ValueType::v128, ValueType::i32}};
		constexpr OpSignature v128_i64_to_v128{{ValueType::v128},
											   {ValueType::v128, ValueType::i64}};
		constexpr OpSignature v128_f32_to_v128{{ValueType::v128},
											   {ValueType::v128, ValueType::f32}};
		constexpr OpSignature v128_f64_to_v128{{ValueType::v128},
											   {ValueType::v128, ValueType::f64}};

		// Memory/table index polymorphic signatures.
		inline OpSignature load(const Module& module,
								const BaseLoadOrStoreImm& imm,
								ValueType resultType)
		{
			return OpSignature({resultType},
							   {asValueType(module.memories.getType(imm.memoryIndex).indexType)});
		}
		inline OpSignature load_i32(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return load(module, imm, ValueType::i32);
		}
		inline OpSignature load_i64(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return load(module, imm, ValueType::i64);
		}
		inline OpSignature load_f32(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return load(module, imm, ValueType::f32);
		}
		inline OpSignature load_f64(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return load(module, imm, ValueType::f64);
		}
		inline OpSignature load_v128(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return load(module, imm, ValueType::v128);
		}
		template<Uptr naturalAlignmentLog2, Uptr numLanes>
		inline OpSignature load_v128_lane(
			const Module& module,
			const LoadOrStoreLaneImm<naturalAlignmentLog2, numLanes>& imm)
		{
			return OpSignature(
				{ValueType::v128},
				{asValueType(module.memories.getType(imm.memoryIndex).indexType), ValueType::v128});
		}

		inline OpSignature store(const Module& module,
								 const BaseLoadOrStoreImm& imm,
								 ValueType valueType)
		{
			return OpSignature(
				{}, {asValueType(module.memories.getType(imm.memoryIndex).indexType), valueType});
		}
		inline OpSignature store_i32(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return store(module, imm, ValueType::i32);
		}
		inline OpSignature store_i64(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return store(module, imm, ValueType::i64);
		}
		inline OpSignature store_f32(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return store(module, imm, ValueType::f32);
		}
		inline OpSignature store_f64(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return store(module, imm, ValueType::f64);
		}
		inline OpSignature store_v128(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return store(module, imm, ValueType::v128);
		}
		template<Uptr naturalAlignmentLog2, Uptr numLanes>
		inline OpSignature store_v128_lane(
			const Module& module,
			const LoadOrStoreLaneImm<naturalAlignmentLog2, numLanes>& imm)
		{
			return store(module, imm, ValueType::v128);
		}

		template<Uptr numVectors>
		inline OpSignature load_v128xN(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			const ValueType indexType
				= asValueType(module.memories.getType(imm.memoryIndex).indexType);
			switch(numVectors)
			{
			case 2: return OpSignature({ValueType::v128, ValueType::v128}, {indexType});
			case 3:
				return OpSignature({ValueType::v128, ValueType::v128, ValueType::v128},
								   {indexType});
			case 4:
				return OpSignature(
					{ValueType::v128, ValueType::v128, ValueType::v128, ValueType::v128},
					{indexType});
			default: WAVM_UNREACHABLE();
			};
		}
		template<Uptr numVectors>
		inline OpSignature store_v128xN(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			const ValueType indexType
				= asValueType(module.memories.getType(imm.memoryIndex).indexType);
			switch(numVectors)
			{
			case 2: return OpSignature({}, {indexType, ValueType::v128, ValueType::v128});
			case 3:
				return OpSignature({},
								   {indexType, ValueType::v128, ValueType::v128, ValueType::v128});
			case 4:
				return OpSignature({},
								   {indexType,
									ValueType::v128,
									ValueType::v128,
									ValueType::v128,
									ValueType::v128});
			default: WAVM_UNREACHABLE();
			};
		}

		inline OpSignature size(const Module& module, const MemoryImm& imm)
		{
			const ValueType indexType
				= asValueType(module.memories.getType(imm.memoryIndex).indexType);
			return OpSignature({indexType}, {});
		}
		inline OpSignature size(const Module& module, const TableImm& imm)
		{
			const ValueType indexType
				= asValueType(module.tables.getType(imm.tableIndex).indexType);
			return OpSignature({indexType}, {});
		}

		inline OpSignature grow(const Module& module, const MemoryImm& imm)
		{
			const ValueType indexType
				= asValueType(module.memories.getType(imm.memoryIndex).indexType);
			return OpSignature({indexType}, {indexType});
		}

		inline OpSignature init(const Module& module, const DataSegmentAndMemImm& imm)
		{
			const ValueType indexType
				= asValueType(module.memories.getType(imm.memoryIndex).indexType);
			return OpSignature({}, {indexType, indexType, indexType});
		}
		inline OpSignature init(const Module& module, const ElemSegmentAndTableImm& imm)
		{
			const ValueType indexType
				= asValueType(module.tables.getType(imm.tableIndex).indexType);
			return OpSignature({}, {indexType, indexType, indexType});
		}

		inline OpSignature copy(const Module& module, const MemoryCopyImm& imm)
		{
			const ValueType destIndexType
				= asValueType(module.memories.getType(imm.destMemoryIndex).indexType);
			const ValueType sourceIndexType
				= asValueType(module.memories.getType(imm.sourceMemoryIndex).indexType);
			const ValueType numBytesType
				= destIndexType == ValueType::i64 && sourceIndexType == ValueType::i64
					  ? ValueType::i64
					  : ValueType::i32;
			return OpSignature({}, {destIndexType, sourceIndexType, numBytesType});
		}
		inline OpSignature copy(const Module& module, const TableCopyImm& imm)
		{
			const ValueType destIndexType
				= asValueType(module.tables.getType(imm.destTableIndex).indexType);
			const ValueType sourceIndexType
				= asValueType(module.tables.getType(imm.sourceTableIndex).indexType);
			const ValueType numBytesType
				= destIndexType == ValueType::i64 && sourceIndexType == ValueType::i64
					  ? ValueType::i64
					  : ValueType::i32;
			return OpSignature({}, {destIndexType, sourceIndexType, numBytesType});
		}

		inline OpSignature fill(const Module& module, const MemoryImm& imm)
		{
			const ValueType indexType
				= asValueType(module.memories.getType(imm.memoryIndex).indexType);
			return OpSignature({}, {indexType, ValueType::i32, indexType});
		}
		inline OpSignature fill(const Module& module, const TableImm& imm)
		{
			const TableType& tableType = module.tables.getType(imm.tableIndex);
			const ValueType indexType = asValueType(tableType.indexType);
			const ValueType elementType = asValueType(tableType.elementType);
			return OpSignature({}, {indexType, elementType, indexType});
		}

		inline OpSignature notify(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			const ValueType indexType
				= asValueType(module.memories.getType(imm.memoryIndex).indexType);
			return OpSignature({ValueType::i32}, {indexType, ValueType::i32});
		}
		inline OpSignature wait32(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			const ValueType indexType
				= asValueType(module.memories.getType(imm.memoryIndex).indexType);
			return OpSignature({ValueType::i32}, {indexType, ValueType::i32, ValueType::i64});
		}
		inline OpSignature wait64(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			const ValueType indexType
				= asValueType(module.memories.getType(imm.memoryIndex).indexType);
			return OpSignature({ValueType::i32}, {indexType, ValueType::i64, ValueType::i64});
		}

		inline OpSignature atomicrmw(const Module& module,
									 const BaseLoadOrStoreImm& imm,
									 ValueType valueType)
		{
			const ValueType indexType
				= asValueType(module.memories.getType(imm.memoryIndex).indexType);
			return OpSignature({valueType}, {indexType, valueType});
		}
		inline OpSignature atomicrmw_i32(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return atomicrmw(module, imm, ValueType::i32);
		}
		inline OpSignature atomicrmw_i64(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return atomicrmw(module, imm, ValueType::i64);
		}

		inline OpSignature atomiccmpxchg(const Module& module,
										 const BaseLoadOrStoreImm& imm,
										 ValueType valueType)
		{
			const ValueType indexType
				= asValueType(module.memories.getType(imm.memoryIndex).indexType);
			return OpSignature({valueType}, {indexType, valueType, valueType});
		}
		inline OpSignature atomiccmpxchg_i32(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return atomiccmpxchg(module, imm, ValueType::i32);
		}
		inline OpSignature atomiccmpxchg_i64(const Module& module, const BaseLoadOrStoreImm& imm)
		{
			return atomiccmpxchg(module, imm, ValueType::i64);
		}
	};
}};