Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
opcodes_def.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4
5/* clang-format off */
6#define SYS_VM_CONTROL_FLOW_OPS(opcode_macro) \
7 opcode_macro(unreachable, 0x00) \
8 opcode_macro(nop, 0x01) \
9 opcode_macro(block, 0x02) \
10 opcode_macro(loop, 0x03) \
11 opcode_macro(if_, 0x04) \
12 opcode_macro(else_, 0x05) \
13 opcode_macro(padding_cf_0, 0x06) \
14 opcode_macro(padding_cf_1, 0x07) \
15 opcode_macro(padding_cf_2, 0x08) \
16 opcode_macro(padding_cf_3, 0x09) \
17 opcode_macro(padding_cf_4, 0x0A) \
18 opcode_macro(end, 0x0B) \
19 opcode_macro(br, 0x0C) \
20 opcode_macro(br_if, 0x0D)
21#define SYS_VM_BR_TABLE_OP(opcode_macro) \
22 opcode_macro(br_table, 0x0E)
23#define SYS_VM_RETURN_OP(opcode_macro) \
24 opcode_macro(return_, 0x0F)
25#define SYS_VM_CALL_OPS(opcode_macro) \
26 opcode_macro(call, 0x10) \
27 opcode_macro(call_indirect, 0x11) \
28 opcode_macro(br_table_data, 0x12)
29#define SYS_VM_CALL_IMM_OPS(opcode_macro) \
30 opcode_macro(call_imm, 0x13) \
31 opcode_macro(call_indirect_imm, 0x14) \
32 opcode_macro(padding_call_1, 0x15) \
33 opcode_macro(padding_call_2, 0x16) \
34 opcode_macro(padding_call_3, 0x17) \
35 opcode_macro(padding_call_4, 0x18) \
36 opcode_macro(padding_call_5, 0x19)
37#define SYS_VM_PARAMETRIC_OPS(opcode_macro) \
38 opcode_macro(drop, 0x1A) \
39 opcode_macro(select, 0x1B) \
40 opcode_macro(padding_param_0, 0x1C) \
41 opcode_macro(padding_param_1, 0x1D) \
42 opcode_macro(padding_param_2, 0x1E) \
43 opcode_macro(padding_param_3, 0x1F)
44#define SYS_VM_VARIABLE_ACCESS_OPS(opcode_macro)\
45 opcode_macro(get_local, 0x20) \
46 opcode_macro(set_local, 0x21) \
47 opcode_macro(tee_local, 0x22) \
48 opcode_macro(get_global, 0x23) \
49 opcode_macro(set_global, 0x24) \
50 opcode_macro(padding_va_0, 0x25) \
51 opcode_macro(padding_va_1, 0x26) \
52 opcode_macro(padding_va_2, 0x27)
53#define SYS_VM_MEMORY_OPS(opcode_macro) \
54 opcode_macro(i32_load, 0x28) \
55 opcode_macro(i64_load, 0x29) \
56 opcode_macro(f32_load, 0x2A) \
57 opcode_macro(f64_load, 0x2B) \
58 opcode_macro(i32_load8_s, 0x2C) \
59 opcode_macro(i32_load8_u, 0x2D) \
60 opcode_macro(i32_load16_s, 0x2E) \
61 opcode_macro(i32_load16_u, 0x2F) \
62 opcode_macro(i64_load8_s, 0x30) \
63 opcode_macro(i64_load8_u, 0x31) \
64 opcode_macro(i64_load16_s, 0x32) \
65 opcode_macro(i64_load16_u, 0x33) \
66 opcode_macro(i64_load32_s, 0x34) \
67 opcode_macro(i64_load32_u, 0x35) \
68 opcode_macro(i32_store, 0x36) \
69 opcode_macro(i64_store, 0x37) \
70 opcode_macro(f32_store, 0x38) \
71 opcode_macro(f64_store, 0x39) \
72 opcode_macro(i32_store8, 0x3A) \
73 opcode_macro(i32_store16, 0x3B) \
74 opcode_macro(i64_store8, 0x3C) \
75 opcode_macro(i64_store16, 0x3D) \
76 opcode_macro(i64_store32, 0x3E) \
77 opcode_macro(current_memory, 0x3F) \
78 opcode_macro(grow_memory, 0x40)
79#define SYS_VM_I32_CONSTANT_OPS(opcode_macro) \
80 opcode_macro(i32_const, 0x41)
81#define SYS_VM_I64_CONSTANT_OPS(opcode_macro) \
82 opcode_macro(i64_const, 0x42)
83#define SYS_VM_F32_CONSTANT_OPS(opcode_macro) \
84 opcode_macro(f32_const, 0x43)
85#define SYS_VM_F64_CONSTANT_OPS(opcode_macro) \
86 opcode_macro(f64_const, 0x44)
87#define SYS_VM_COMPARISON_OPS(opcode_macro) \
88 opcode_macro(i32_eqz, 0x45) \
89 opcode_macro(i32_eq, 0x46) \
90 opcode_macro(i32_ne, 0x47) \
91 opcode_macro(i32_lt_s, 0x48) \
92 opcode_macro(i32_lt_u, 0x49) \
93 opcode_macro(i32_gt_s, 0x4A) \
94 opcode_macro(i32_gt_u, 0x4B) \
95 opcode_macro(i32_le_s, 0x4C) \
96 opcode_macro(i32_le_u, 0x4D) \
97 opcode_macro(i32_ge_s, 0x4E) \
98 opcode_macro(i32_ge_u, 0x4F) \
99 opcode_macro(i64_eqz, 0x50) \
100 opcode_macro(i64_eq, 0x51) \
101 opcode_macro(i64_ne, 0x52) \
102 opcode_macro(i64_lt_s, 0x53) \
103 opcode_macro(i64_lt_u, 0x54) \
104 opcode_macro(i64_gt_s, 0x55) \
105 opcode_macro(i64_gt_u, 0x56) \
106 opcode_macro(i64_le_s, 0x57) \
107 opcode_macro(i64_le_u, 0x58) \
108 opcode_macro(i64_ge_s, 0x59) \
109 opcode_macro(i64_ge_u, 0x5A) \
110 opcode_macro(f32_eq, 0x5B) \
111 opcode_macro(f32_ne, 0x5C) \
112 opcode_macro(f32_lt, 0x5D) \
113 opcode_macro(f32_gt, 0x5E) \
114 opcode_macro(f32_le, 0x5F) \
115 opcode_macro(f32_ge, 0x60) \
116 opcode_macro(f64_eq, 0x61) \
117 opcode_macro(f64_ne, 0x62) \
118 opcode_macro(f64_lt, 0x63) \
119 opcode_macro(f64_gt, 0x64) \
120 opcode_macro(f64_le, 0x65) \
121 opcode_macro(f64_ge, 0x66)
122#define SYS_VM_NUMERIC_OPS(opcode_macro) \
123 opcode_macro(i32_clz, 0x67) \
124 opcode_macro(i32_ctz, 0x68) \
125 opcode_macro(i32_popcnt, 0x69) \
126 opcode_macro(i32_add, 0x6A) \
127 opcode_macro(i32_sub, 0x6B) \
128 opcode_macro(i32_mul, 0x6C) \
129 opcode_macro(i32_div_s, 0x6D) \
130 opcode_macro(i32_div_u, 0x6E) \
131 opcode_macro(i32_rem_s, 0x6F) \
132 opcode_macro(i32_rem_u, 0x70) \
133 opcode_macro(i32_and, 0x71) \
134 opcode_macro(i32_or, 0x72) \
135 opcode_macro(i32_xor, 0x73) \
136 opcode_macro(i32_shl, 0x74) \
137 opcode_macro(i32_shr_s, 0x75) \
138 opcode_macro(i32_shr_u, 0x76) \
139 opcode_macro(i32_rotl, 0x77) \
140 opcode_macro(i32_rotr, 0x78) \
141 opcode_macro(i64_clz, 0x79) \
142 opcode_macro(i64_ctz, 0x7A) \
143 opcode_macro(i64_popcnt, 0x7B) \
144 opcode_macro(i64_add, 0x7C) \
145 opcode_macro(i64_sub, 0x7D) \
146 opcode_macro(i64_mul, 0x7E) \
147 opcode_macro(i64_div_s, 0x7F) \
148 opcode_macro(i64_div_u, 0x80) \
149 opcode_macro(i64_rem_s, 0x81) \
150 opcode_macro(i64_rem_u, 0x82) \
151 opcode_macro(i64_and, 0x83) \
152 opcode_macro(i64_or, 0x84) \
153 opcode_macro(i64_xor, 0x85) \
154 opcode_macro(i64_shl, 0x86) \
155 opcode_macro(i64_shr_s, 0x87) \
156 opcode_macro(i64_shr_u, 0x88) \
157 opcode_macro(i64_rotl, 0x89) \
158 opcode_macro(i64_rotr, 0x8A) \
159 opcode_macro(f32_abs, 0x8B) \
160 opcode_macro(f32_neg, 0x8C) \
161 opcode_macro(f32_ceil, 0x8D) \
162 opcode_macro(f32_floor, 0x8E) \
163 opcode_macro(f32_trunc, 0x8F) \
164 opcode_macro(f32_nearest, 0x90) \
165 opcode_macro(f32_sqrt, 0x91) \
166 opcode_macro(f32_add, 0x92) \
167 opcode_macro(f32_sub, 0x93) \
168 opcode_macro(f32_mul, 0x94) \
169 opcode_macro(f32_div, 0x95) \
170 opcode_macro(f32_min, 0x96) \
171 opcode_macro(f32_max, 0x97) \
172 opcode_macro(f32_copysign, 0x98) \
173 opcode_macro(f64_abs, 0x99) \
174 opcode_macro(f64_neg, 0x9A) \
175 opcode_macro(f64_ceil, 0x9B) \
176 opcode_macro(f64_floor, 0x9C) \
177 opcode_macro(f64_trunc, 0x9D) \
178 opcode_macro(f64_nearest, 0x9E) \
179 opcode_macro(f64_sqrt, 0x9F) \
180 opcode_macro(f64_add, 0xA0) \
181 opcode_macro(f64_sub, 0xA1) \
182 opcode_macro(f64_mul, 0xA2) \
183 opcode_macro(f64_div, 0xA3) \
184 opcode_macro(f64_min, 0xA4) \
185 opcode_macro(f64_max, 0xA5) \
186 opcode_macro(f64_copysign, 0xA6)
187#define SYS_VM_CONVERSION_OPS(opcode_macro) \
188 opcode_macro(i32_wrap_i64, 0xA7) \
189 opcode_macro(i32_trunc_s_f32, 0xA8) \
190 opcode_macro(i32_trunc_u_f32, 0xA9) \
191 opcode_macro(i32_trunc_s_f64, 0xAA) \
192 opcode_macro(i32_trunc_u_f64, 0xAB) \
193 opcode_macro(i64_extend_s_i32, 0xAC) \
194 opcode_macro(i64_extend_u_i32, 0xAD) \
195 opcode_macro(i64_trunc_s_f32, 0xAE) \
196 opcode_macro(i64_trunc_u_f32, 0xAF) \
197 opcode_macro(i64_trunc_s_f64, 0xB0) \
198 opcode_macro(i64_trunc_u_f64, 0xB1) \
199 opcode_macro(f32_convert_s_i32, 0xB2) \
200 opcode_macro(f32_convert_u_i32, 0xB3) \
201 opcode_macro(f32_convert_s_i64, 0xB4) \
202 opcode_macro(f32_convert_u_i64, 0xB5) \
203 opcode_macro(f32_demote_f64, 0xB6) \
204 opcode_macro(f64_convert_s_i32, 0xB7) \
205 opcode_macro(f64_convert_u_i32, 0xB8) \
206 opcode_macro(f64_convert_s_i64, 0xB9) \
207 opcode_macro(f64_convert_u_i64, 0xBA) \
208 opcode_macro(f64_promote_f32, 0xBB) \
209 opcode_macro(i32_reinterpret_f32, 0xBC) \
210 opcode_macro(i64_reinterpret_f64, 0xBD) \
211 opcode_macro(f32_reinterpret_i32, 0xBE) \
212 opcode_macro(f64_reinterpret_i64, 0xBF)
213#define SYS_VM_EXIT_OP(opcode_macro) \
214 opcode_macro(exit, 0xC0)
215#define SYS_VM_EMPTY_OPS(opcode_macro) \
216 opcode_macro(empty0xC1, 0xC1) \
217 opcode_macro(empty0xC2, 0xC2) \
218 opcode_macro(empty0xC3, 0xC3) \
219 opcode_macro(empty0xC4, 0xC4) \
220 opcode_macro(empty0xC5, 0xC5) \
221 opcode_macro(empty0xC6, 0xC6) \
222 opcode_macro(empty0xC7, 0xC7) \
223 opcode_macro(empty0xC8, 0xC8) \
224 opcode_macro(empty0xC9, 0xC9) \
225 opcode_macro(empty0xCA, 0xCA) \
226 opcode_macro(empty0xCB, 0xCB) \
227 opcode_macro(empty0xCC, 0xCC) \
228 opcode_macro(empty0xCD, 0xCD) \
229 opcode_macro(empty0xCE, 0xCE) \
230 opcode_macro(empty0xCF, 0xCF) \
231 opcode_macro(empty0xD0, 0xD0) \
232 opcode_macro(empty0xD1, 0xD1) \
233 opcode_macro(empty0xD2, 0xD2) \
234 opcode_macro(empty0xD3, 0xD3) \
235 opcode_macro(empty0xD4, 0xD4) \
236 opcode_macro(empty0xD5, 0xD5) \
237 opcode_macro(empty0xD6, 0xD6) \
238 opcode_macro(empty0xD7, 0xD7) \
239 opcode_macro(empty0xD8, 0xD8) \
240 opcode_macro(empty0xD9, 0xD9) \
241 opcode_macro(empty0xDA, 0xDA) \
242 opcode_macro(empty0xDB, 0xDB) \
243 opcode_macro(empty0xDC, 0xDC) \
244 opcode_macro(empty0xDD, 0xDD) \
245 opcode_macro(empty0xDE, 0xDE) \
246 opcode_macro(empty0xDF, 0xDF) \
247 opcode_macro(empty0xE0, 0xE0) \
248 opcode_macro(empty0xE1, 0xE1) \
249 opcode_macro(empty0xE2, 0xE2) \
250 opcode_macro(empty0xE3, 0xE3) \
251 opcode_macro(empty0xE4, 0xE4) \
252 opcode_macro(empty0xE5, 0xE5) \
253 opcode_macro(empty0xE6, 0xE6) \
254 opcode_macro(empty0xE7, 0xE7) \
255 opcode_macro(empty0xE8, 0xE8) \
256 opcode_macro(empty0xE9, 0xE9) \
257 opcode_macro(empty0xEA, 0xEA) \
258 opcode_macro(empty0xEB, 0xEB) \
259 opcode_macro(empty0xEC, 0xEC) \
260 opcode_macro(empty0xED, 0xED) \
261 opcode_macro(empty0xEE, 0xEE) \
262 opcode_macro(empty0xEF, 0xEF) \
263 opcode_macro(empty0xF0, 0xF0) \
264 opcode_macro(empty0xF1, 0xF1) \
265 opcode_macro(empty0xF2, 0xF2) \
266 opcode_macro(empty0xF3, 0xF3) \
267 opcode_macro(empty0xF4, 0xF4) \
268 opcode_macro(empty0xF5, 0xF5) \
269 opcode_macro(empty0xF6, 0xF6) \
270 opcode_macro(empty0xF7, 0xF7) \
271 opcode_macro(empty0xF8, 0xF8) \
272 opcode_macro(empty0xF9, 0xF9) \
273 opcode_macro(empty0xFA, 0xFA) \
274 opcode_macro(empty0xFB, 0xFB) \
275 opcode_macro(empty0xFC, 0xFC) \
276 opcode_macro(empty0xFD, 0xFD) \
277 opcode_macro(empty0xFE, 0xFE)
278#define SYS_VM_ERROR_OPS(opcode_macro) \
279 opcode_macro(error, 0xFF)
280
281/* clang-format on */
282
283#define SYS_VM_CREATE_ENUM(name, code) name = code,
284
285#define SYS_VM_CREATE_STRINGS(name, code) #name,
286
287#define SYS_VM_CREATE_MAP(name, code) { code, #name },
288
289#define SYS_VM_OPCODE_NAME_if_
290#define SYS_VM_OPCODE_NAME_else_
291#define SYS_VM_OPCODE_NAME_return_
292
293#define SYS_VM_OPCODE_NAME_TEST() 1
294#define SYS_VM_OPCODE_NAME_TEST_SYS_VM_OPCODE_NAME_TEST 0,
295#define SYS_VM_OPCODE_NAME_TEST_1 1, ignore
296#define SYS_VM_EXPAND(x) x
297#define SYS_VM_CAT2(x, y) x ## y
298#define SYS_VM_CAT(x, y) SYS_VM_CAT2(x, y)
299#define SYS_VM_APPLY(f, args) f args
300#define SYS_VM_FIX_OPCODE_NAME_0(name) name ## _t
301#define SYS_VM_FIX_OPCODE_NAME_1(name) name ## t
302#define SYS_VM_FIX_OPCODE_NAME(iskeyword, garbage) SYS_VM_FIX_OPCODE_NAME_ ## iskeyword
303
304#define SYS_VM_OPCODE_T(name) \
305 SYS_VM_APPLY(SYS_VM_FIX_OPCODE_NAME, \
306 (SYS_VM_CAT(SYS_VM_OPCODE_NAME_TEST_, \
307 SYS_VM_EXPAND(SYS_VM_OPCODE_NAME_TEST SYS_VM_OPCODE_NAME_ ## name ()))))(name)
308
309#define SYS_VM_CREATE_EXIT_TYPE(name, code) \
310 struct SYS_VM_OPCODE_T(name) { \
311 SYS_VM_OPCODE_T(name)() = default; \
312 uint32_t pc; \
313 static constexpr uint8_t opcode = code; \
314 };
315
316#define SYS_VM_CREATE_CONTROL_FLOW_TYPES(name, code) \
317 struct SYS_VM_OPCODE_T(name) { \
318 SYS_VM_OPCODE_T(name)() {} \
319 SYS_VM_OPCODE_T(name)(uint32_t data) : data(data) {} \
320 SYS_VM_OPCODE_T(name)(uint32_t d, uint32_t pc, uint16_t i, uint16_t oi) \
321 : data(d), pc(pc), index(i), op_index(oi) {} \
322 uint32_t data = 0; \
323 uint32_t pc = 0; \
324 uint16_t index = 0; \
325 uint16_t op_index = 0; \
326 static constexpr uint8_t opcode = code; \
327 };
328
329#define SYS_VM_CREATE_BR_TABLE_TYPE(name, code) \
330 struct SYS_VM_OPCODE_T(name) { \
331 SYS_VM_OPCODE_T(name)() = default; \
332 struct elem_t { uint32_t pc; uint32_t stack_pop; }; \
333 elem_t* table; \
334 uint32_t size; \
335 uint32_t offset; \
336 static constexpr uint8_t opcode = code; \
337 };
338
339#define SYS_VM_CREATE_TYPES(name, code) \
340 struct SYS_VM_OPCODE_T(name) { \
341 SYS_VM_OPCODE_T(name)() = default; \
342 static constexpr uint8_t opcode = code; \
343 };
344
345#define SYS_VM_CREATE_CALL_TYPES(name, code) \
346 struct SYS_VM_OPCODE_T(name) { \
347 SYS_VM_OPCODE_T(name)() = default; \
348 uint32_t index; \
349 static constexpr uint8_t opcode = code; \
350 };
351
352#define SYS_VM_CREATE_CALL_IMM_TYPES(name, code) \
353 struct SYS_VM_OPCODE_T(name) { \
354 SYS_VM_OPCODE_T(name)() = default; \
355 uint32_t index; \
356 uint16_t locals; \
357 uint16_t return_type; \
358 static constexpr uint8_t opcode = code; \
359 };
360
361#define SYS_VM_CREATE_VARIABLE_ACCESS_TYPES(name, code) \
362 struct SYS_VM_OPCODE_T(name) { \
363 SYS_VM_OPCODE_T(name)() = default; \
364 uint32_t index; \
365 static constexpr uint8_t opcode = code; \
366 };
367
368#define SYS_VM_CREATE_MEMORY_TYPES(name, code) \
369 struct SYS_VM_OPCODE_T(name) { \
370 SYS_VM_OPCODE_T(name)() = default; \
371 uint32_t flags_align; \
372 uint32_t offset; \
373 static constexpr uint8_t opcode = code; \
374 };
375
376#define SYS_VM_CREATE_I32_CONSTANT_TYPE(name, code) \
377 struct SYS_VM_OPCODE_T(name) { \
378 SYS_VM_OPCODE_T(name)() = default; \
379 explicit SYS_VM_OPCODE_T(name)(uint32_t n) { data.ui = n; } \
380 explicit SYS_VM_OPCODE_T(name)(int32_t n) { data.i = n; } \
381 union { \
382 uint32_t ui; \
383 int32_t i; \
384 } data; \
385 static constexpr uint8_t opcode = code; \
386 };
387
388#define SYS_VM_CREATE_I64_CONSTANT_TYPE(name, code) \
389 struct SYS_VM_OPCODE_T(name) { \
390 SYS_VM_OPCODE_T(name)() = default; \
391 explicit SYS_VM_OPCODE_T(name)(uint64_t n) { data.ui = n; } \
392 explicit SYS_VM_OPCODE_T(name)(int64_t n) { data.i = n; } \
393 union { \
394 uint64_t ui; \
395 int64_t i; \
396 } data; \
397 static constexpr uint8_t opcode = code; \
398 };
399
400#define SYS_VM_CREATE_F32_CONSTANT_TYPE(name, code) \
401 struct SYS_VM_OPCODE_T(name) { \
402 SYS_VM_OPCODE_T(name)() = default; \
403 explicit SYS_VM_OPCODE_T(name)(uint32_t n) { data.ui = n; } \
404 explicit SYS_VM_OPCODE_T(name)(float n) { data.f = n; } \
405 union { \
406 uint32_t ui; \
407 float f; \
408 } data; \
409 static constexpr uint8_t opcode = code; \
410 };
411
412#define SYS_VM_CREATE_F64_CONSTANT_TYPE(name, code) \
413 struct SYS_VM_OPCODE_T(name) { \
414 SYS_VM_OPCODE_T(name)() = default; \
415 explicit SYS_VM_OPCODE_T(name)(uint64_t n) { data.ui = n; } \
416 explicit SYS_VM_OPCODE_T(name)(double n) { data.f = n; } \
417 union { \
418 uint64_t ui; \
419 double f; \
420 } data; \
421 static constexpr uint8_t opcode = code; \
422 };
423
424#define SYS_VM_IDENTITY(name, code) sysio::vm::SYS_VM_OPCODE_T(name),
425#define SYS_VM_IDENTITY_END(name, code) sysio::vm::SYS_VM_OPCODE_T(name)