master
  1///! Contains all constants and types representing the wasm
  2///! binary format, as specified by:
  3///! https://webassembly.github.io/spec/core/
  4const std = @import("std.zig");
  5const testing = std.testing;
  6
  7/// Wasm instruction opcodes
  8///
  9/// All instructions are defined as per spec:
 10/// https://webassembly.github.io/spec/core/appendix/index-instructions.html
 11pub const Opcode = enum(u8) {
 12    @"unreachable" = 0x00,
 13    nop = 0x01,
 14    block = 0x02,
 15    loop = 0x03,
 16    @"if" = 0x04,
 17    @"else" = 0x05,
 18    end = 0x0B,
 19    br = 0x0C,
 20    br_if = 0x0D,
 21    br_table = 0x0E,
 22    @"return" = 0x0F,
 23    call = 0x10,
 24    call_indirect = 0x11,
 25    drop = 0x1A,
 26    select = 0x1B,
 27    local_get = 0x20,
 28    local_set = 0x21,
 29    local_tee = 0x22,
 30    global_get = 0x23,
 31    global_set = 0x24,
 32    i32_load = 0x28,
 33    i64_load = 0x29,
 34    f32_load = 0x2A,
 35    f64_load = 0x2B,
 36    i32_load8_s = 0x2C,
 37    i32_load8_u = 0x2D,
 38    i32_load16_s = 0x2E,
 39    i32_load16_u = 0x2F,
 40    i64_load8_s = 0x30,
 41    i64_load8_u = 0x31,
 42    i64_load16_s = 0x32,
 43    i64_load16_u = 0x33,
 44    i64_load32_s = 0x34,
 45    i64_load32_u = 0x35,
 46    i32_store = 0x36,
 47    i64_store = 0x37,
 48    f32_store = 0x38,
 49    f64_store = 0x39,
 50    i32_store8 = 0x3A,
 51    i32_store16 = 0x3B,
 52    i64_store8 = 0x3C,
 53    i64_store16 = 0x3D,
 54    i64_store32 = 0x3E,
 55    memory_size = 0x3F,
 56    memory_grow = 0x40,
 57    i32_const = 0x41,
 58    i64_const = 0x42,
 59    f32_const = 0x43,
 60    f64_const = 0x44,
 61    i32_eqz = 0x45,
 62    i32_eq = 0x46,
 63    i32_ne = 0x47,
 64    i32_lt_s = 0x48,
 65    i32_lt_u = 0x49,
 66    i32_gt_s = 0x4A,
 67    i32_gt_u = 0x4B,
 68    i32_le_s = 0x4C,
 69    i32_le_u = 0x4D,
 70    i32_ge_s = 0x4E,
 71    i32_ge_u = 0x4F,
 72    i64_eqz = 0x50,
 73    i64_eq = 0x51,
 74    i64_ne = 0x52,
 75    i64_lt_s = 0x53,
 76    i64_lt_u = 0x54,
 77    i64_gt_s = 0x55,
 78    i64_gt_u = 0x56,
 79    i64_le_s = 0x57,
 80    i64_le_u = 0x58,
 81    i64_ge_s = 0x59,
 82    i64_ge_u = 0x5A,
 83    f32_eq = 0x5B,
 84    f32_ne = 0x5C,
 85    f32_lt = 0x5D,
 86    f32_gt = 0x5E,
 87    f32_le = 0x5F,
 88    f32_ge = 0x60,
 89    f64_eq = 0x61,
 90    f64_ne = 0x62,
 91    f64_lt = 0x63,
 92    f64_gt = 0x64,
 93    f64_le = 0x65,
 94    f64_ge = 0x66,
 95    i32_clz = 0x67,
 96    i32_ctz = 0x68,
 97    i32_popcnt = 0x69,
 98    i32_add = 0x6A,
 99    i32_sub = 0x6B,
100    i32_mul = 0x6C,
101    i32_div_s = 0x6D,
102    i32_div_u = 0x6E,
103    i32_rem_s = 0x6F,
104    i32_rem_u = 0x70,
105    i32_and = 0x71,
106    i32_or = 0x72,
107    i32_xor = 0x73,
108    i32_shl = 0x74,
109    i32_shr_s = 0x75,
110    i32_shr_u = 0x76,
111    i32_rotl = 0x77,
112    i32_rotr = 0x78,
113    i64_clz = 0x79,
114    i64_ctz = 0x7A,
115    i64_popcnt = 0x7B,
116    i64_add = 0x7C,
117    i64_sub = 0x7D,
118    i64_mul = 0x7E,
119    i64_div_s = 0x7F,
120    i64_div_u = 0x80,
121    i64_rem_s = 0x81,
122    i64_rem_u = 0x82,
123    i64_and = 0x83,
124    i64_or = 0x84,
125    i64_xor = 0x85,
126    i64_shl = 0x86,
127    i64_shr_s = 0x87,
128    i64_shr_u = 0x88,
129    i64_rotl = 0x89,
130    i64_rotr = 0x8A,
131    f32_abs = 0x8B,
132    f32_neg = 0x8C,
133    f32_ceil = 0x8D,
134    f32_floor = 0x8E,
135    f32_trunc = 0x8F,
136    f32_nearest = 0x90,
137    f32_sqrt = 0x91,
138    f32_add = 0x92,
139    f32_sub = 0x93,
140    f32_mul = 0x94,
141    f32_div = 0x95,
142    f32_min = 0x96,
143    f32_max = 0x97,
144    f32_copysign = 0x98,
145    f64_abs = 0x99,
146    f64_neg = 0x9A,
147    f64_ceil = 0x9B,
148    f64_floor = 0x9C,
149    f64_trunc = 0x9D,
150    f64_nearest = 0x9E,
151    f64_sqrt = 0x9F,
152    f64_add = 0xA0,
153    f64_sub = 0xA1,
154    f64_mul = 0xA2,
155    f64_div = 0xA3,
156    f64_min = 0xA4,
157    f64_max = 0xA5,
158    f64_copysign = 0xA6,
159    i32_wrap_i64 = 0xA7,
160    i32_trunc_f32_s = 0xA8,
161    i32_trunc_f32_u = 0xA9,
162    i32_trunc_f64_s = 0xAA,
163    i32_trunc_f64_u = 0xAB,
164    i64_extend_i32_s = 0xAC,
165    i64_extend_i32_u = 0xAD,
166    i64_trunc_f32_s = 0xAE,
167    i64_trunc_f32_u = 0xAF,
168    i64_trunc_f64_s = 0xB0,
169    i64_trunc_f64_u = 0xB1,
170    f32_convert_i32_s = 0xB2,
171    f32_convert_i32_u = 0xB3,
172    f32_convert_i64_s = 0xB4,
173    f32_convert_i64_u = 0xB5,
174    f32_demote_f64 = 0xB6,
175    f64_convert_i32_s = 0xB7,
176    f64_convert_i32_u = 0xB8,
177    f64_convert_i64_s = 0xB9,
178    f64_convert_i64_u = 0xBA,
179    f64_promote_f32 = 0xBB,
180    i32_reinterpret_f32 = 0xBC,
181    i64_reinterpret_f64 = 0xBD,
182    f32_reinterpret_i32 = 0xBE,
183    f64_reinterpret_i64 = 0xBF,
184    i32_extend8_s = 0xC0,
185    i32_extend16_s = 0xC1,
186    i64_extend8_s = 0xC2,
187    i64_extend16_s = 0xC3,
188    i64_extend32_s = 0xC4,
189
190    misc_prefix = 0xFC,
191    simd_prefix = 0xFD,
192    atomics_prefix = 0xFE,
193    _,
194};
195
196/// Opcodes that require a prefix `0xFC`.
197/// Each opcode represents a varuint32, meaning
198/// they are encoded as leb128 in binary.
199pub const MiscOpcode = enum(u32) {
200    i32_trunc_sat_f32_s = 0x00,
201    i32_trunc_sat_f32_u = 0x01,
202    i32_trunc_sat_f64_s = 0x02,
203    i32_trunc_sat_f64_u = 0x03,
204    i64_trunc_sat_f32_s = 0x04,
205    i64_trunc_sat_f32_u = 0x05,
206    i64_trunc_sat_f64_s = 0x06,
207    i64_trunc_sat_f64_u = 0x07,
208    memory_init = 0x08,
209    data_drop = 0x09,
210    memory_copy = 0x0A,
211    memory_fill = 0x0B,
212    table_init = 0x0C,
213    elem_drop = 0x0D,
214    table_copy = 0x0E,
215    table_grow = 0x0F,
216    table_size = 0x10,
217    table_fill = 0x11,
218    _,
219};
220
221/// Simd opcodes that require a prefix `0xFD`.
222/// Each opcode represents a varuint32, meaning
223/// they are encoded as leb128 in binary.
224pub const SimdOpcode = enum(u32) {
225    v128_load = 0x00,
226    v128_load8x8_s = 0x01,
227    v128_load8x8_u = 0x02,
228    v128_load16x4_s = 0x03,
229    v128_load16x4_u = 0x04,
230    v128_load32x2_s = 0x05,
231    v128_load32x2_u = 0x06,
232    v128_load8_splat = 0x07,
233    v128_load16_splat = 0x08,
234    v128_load32_splat = 0x09,
235    v128_load64_splat = 0x0A,
236    v128_store = 0x0B,
237    v128_const = 0x0C,
238    i8x16_shuffle = 0x0D,
239    i8x16_swizzle = 0x0E,
240    i8x16_splat = 0x0F,
241    i16x8_splat = 0x10,
242    i32x4_splat = 0x11,
243    i64x2_splat = 0x12,
244    f32x4_splat = 0x13,
245    f64x2_splat = 0x14,
246    i8x16_extract_lane_s = 0x15,
247    i8x16_extract_lane_u = 0x16,
248    i8x16_replace_lane = 0x17,
249    i16x8_extract_lane_s = 0x18,
250    i16x8_extract_lane_u = 0x19,
251    i16x8_replace_lane = 0x1A,
252    i32x4_extract_lane = 0x1B,
253    i32x4_replace_lane = 0x1C,
254    i64x2_extract_lane = 0x1D,
255    i64x2_replace_lane = 0x1E,
256    f32x4_extract_lane = 0x1F,
257    f32x4_replace_lane = 0x20,
258    f64x2_extract_lane = 0x21,
259    f64x2_replace_lane = 0x22,
260    i8x16_eq = 0x23,
261    i16x8_eq = 0x2D,
262    i32x4_eq = 0x37,
263    i8x16_ne = 0x24,
264    i16x8_ne = 0x2E,
265    i32x4_ne = 0x38,
266    i8x16_lt_s = 0x25,
267    i16x8_lt_s = 0x2F,
268    i32x4_lt_s = 0x39,
269    i8x16_lt_u = 0x26,
270    i16x8_lt_u = 0x30,
271    i32x4_lt_u = 0x3A,
272    i8x16_gt_s = 0x27,
273    i16x8_gt_s = 0x31,
274    i32x4_gt_s = 0x3B,
275    i8x16_gt_u = 0x28,
276    i16x8_gt_u = 0x32,
277    i32x4_gt_u = 0x3C,
278    i8x16_le_s = 0x29,
279    i16x8_le_s = 0x33,
280    i32x4_le_s = 0x3D,
281    i8x16_le_u = 0x2A,
282    i16x8_le_u = 0x34,
283    i32x4_le_u = 0x3E,
284    i8x16_ge_s = 0x2B,
285    i16x8_ge_s = 0x35,
286    i32x4_ge_s = 0x3F,
287    i8x16_ge_u = 0x2C,
288    i16x8_ge_u = 0x36,
289    i32x4_ge_u = 0x40,
290    f32x4_eq = 0x41,
291    f64x2_eq = 0x47,
292    f32x4_ne = 0x42,
293    f64x2_ne = 0x48,
294    f32x4_lt = 0x43,
295    f64x2_lt = 0x49,
296    f32x4_gt = 0x44,
297    f64x2_gt = 0x4A,
298    f32x4_le = 0x45,
299    f64x2_le = 0x4B,
300    f32x4_ge = 0x46,
301    f64x2_ge = 0x4C,
302    v128_not = 0x4D,
303    v128_and = 0x4E,
304    v128_andnot = 0x4F,
305    v128_or = 0x50,
306    v128_xor = 0x51,
307    v128_bitselect = 0x52,
308    v128_any_true = 0x53,
309    v128_load8_lane = 0x54,
310    v128_load16_lane = 0x55,
311    v128_load32_lane = 0x56,
312    v128_load64_lane = 0x57,
313    v128_store8_lane = 0x58,
314    v128_store16_lane = 0x59,
315    v128_store32_lane = 0x5A,
316    v128_store64_lane = 0x5B,
317    v128_load32_zero = 0x5C,
318    v128_load64_zero = 0x5D,
319    f32x4_demote_f64x2_zero = 0x5E,
320    f64x2_promote_low_f32x4 = 0x5F,
321    i8x16_abs = 0x60,
322    i16x8_abs = 0x80,
323    i32x4_abs = 0xA0,
324    i64x2_abs = 0xC0,
325    i8x16_neg = 0x61,
326    i16x8_neg = 0x81,
327    i32x4_neg = 0xA1,
328    i64x2_neg = 0xC1,
329    i8x16_popcnt = 0x62,
330    i16x8_q15mulr_sat_s = 0x82,
331    i8x16_all_true = 0x63,
332    i16x8_all_true = 0x83,
333    i32x4_all_true = 0xA3,
334    i64x2_all_true = 0xC3,
335    i8x16_bitmask = 0x64,
336    i16x8_bitmask = 0x84,
337    i32x4_bitmask = 0xA4,
338    i64x2_bitmask = 0xC4,
339    i8x16_narrow_i16x8_s = 0x65,
340    i16x8_narrow_i32x4_s = 0x85,
341    i8x16_narrow_i16x8_u = 0x66,
342    i16x8_narrow_i32x4_u = 0x86,
343    f32x4_ceil = 0x67,
344    i16x8_extend_low_i8x16_s = 0x87,
345    i32x4_extend_low_i16x8_s = 0xA7,
346    i64x2_extend_low_i32x4_s = 0xC7,
347    f32x4_floor = 0x68,
348    i16x8_extend_high_i8x16_s = 0x88,
349    i32x4_extend_high_i16x8_s = 0xA8,
350    i64x2_extend_high_i32x4_s = 0xC8,
351    f32x4_trunc = 0x69,
352    i16x8_extend_low_i8x16_u = 0x89,
353    i32x4_extend_low_i16x8_u = 0xA9,
354    i64x2_extend_low_i32x4_u = 0xC9,
355    f32x4_nearest = 0x6A,
356    i16x8_extend_high_i8x16_u = 0x8A,
357    i32x4_extend_high_i16x8_u = 0xAA,
358    i64x2_extend_high_i32x4_u = 0xCA,
359    i8x16_shl = 0x6B,
360    i16x8_shl = 0x8B,
361    i32x4_shl = 0xAB,
362    i64x2_shl = 0xCB,
363    i8x16_shr_s = 0x6C,
364    i16x8_shr_s = 0x8C,
365    i32x4_shr_s = 0xAC,
366    i64x2_shr_s = 0xCC,
367    i8x16_shr_u = 0x6D,
368    i16x8_shr_u = 0x8D,
369    i32x4_shr_u = 0xAD,
370    i64x2_shr_u = 0xCD,
371    i8x16_add = 0x6E,
372    i16x8_add = 0x8E,
373    i32x4_add = 0xAE,
374    i64x2_add = 0xCE,
375    i8x16_add_sat_s = 0x6F,
376    i16x8_add_sat_s = 0x8F,
377    i8x16_add_sat_u = 0x70,
378    i16x8_add_sat_u = 0x90,
379    i8x16_sub = 0x71,
380    i16x8_sub = 0x91,
381    i32x4_sub = 0xB1,
382    i64x2_sub = 0xD1,
383    i8x16_sub_sat_s = 0x72,
384    i16x8_sub_sat_s = 0x92,
385    i8x16_sub_sat_u = 0x73,
386    i16x8_sub_sat_u = 0x93,
387    f64x2_ceil = 0x74,
388    f64x2_nearest = 0x94,
389    f64x2_floor = 0x75,
390    i16x8_mul = 0x95,
391    i32x4_mul = 0xB5,
392    i64x2_mul = 0xD5,
393    i8x16_min_s = 0x76,
394    i16x8_min_s = 0x96,
395    i32x4_min_s = 0xB6,
396    i64x2_eq = 0xD6,
397    i8x16_min_u = 0x77,
398    i16x8_min_u = 0x97,
399    i32x4_min_u = 0xB7,
400    i64x2_ne = 0xD7,
401    i8x16_max_s = 0x78,
402    i16x8_max_s = 0x98,
403    i32x4_max_s = 0xB8,
404    i64x2_lt_s = 0xD8,
405    i8x16_max_u = 0x79,
406    i16x8_max_u = 0x99,
407    i32x4_max_u = 0xB9,
408    i64x2_gt_s = 0xD9,
409    f64x2_trunc = 0x7A,
410    i32x4_dot_i16x8_s = 0xBA,
411    i64x2_le_s = 0xDA,
412    i8x16_avgr_u = 0x7B,
413    i16x8_avgr_u = 0x9B,
414    i64x2_ge_s = 0xDB,
415    i16x8_extadd_pairwise_i8x16_s = 0x7C,
416    i16x8_extmul_low_i8x16_s = 0x9C,
417    i32x4_extmul_low_i16x8_s = 0xBC,
418    i64x2_extmul_low_i32x4_s = 0xDC,
419    i16x8_extadd_pairwise_i8x16_u = 0x7D,
420    i16x8_extmul_high_i8x16_s = 0x9D,
421    i32x4_extmul_high_i16x8_s = 0xBD,
422    i64x2_extmul_high_i32x4_s = 0xDD,
423    i32x4_extadd_pairwise_i16x8_s = 0x7E,
424    i16x8_extmul_low_i8x16_u = 0x9E,
425    i32x4_extmul_low_i16x8_u = 0xBE,
426    i64x2_extmul_low_i32x4_u = 0xDE,
427    i32x4_extadd_pairwise_i16x8_u = 0x7F,
428    i16x8_extmul_high_i8x16_u = 0x9F,
429    i32x4_extmul_high_i16x8_u = 0xBF,
430    i64x2_extmul_high_i32x4_u = 0xDF,
431    f32x4_abs = 0xE0,
432    f64x2_abs = 0xEC,
433    f32x4_neg = 0xE1,
434    f64x2_neg = 0xED,
435    f32x4_sqrt = 0xE3,
436    f64x2_sqrt = 0xEF,
437    f32x4_add = 0xE4,
438    f64x2_add = 0xF0,
439    f32x4_sub = 0xE5,
440    f64x2_sub = 0xF1,
441    f32x4_mul = 0xE6,
442    f64x2_mul = 0xF2,
443    f32x4_div = 0xE7,
444    f64x2_div = 0xF3,
445    f32x4_min = 0xE8,
446    f64x2_min = 0xF4,
447    f32x4_max = 0xE9,
448    f64x2_max = 0xF5,
449    f32x4_pmin = 0xEA,
450    f64x2_pmin = 0xF6,
451    f32x4_pmax = 0xEB,
452    f64x2_pmax = 0xF7,
453    i32x4_trunc_sat_f32x4_s = 0xF8,
454    i32x4_trunc_sat_f32x4_u = 0xF9,
455    f32x4_convert_i32x4_s = 0xFA,
456    f32x4_convert_i32x4_u = 0xFB,
457    i32x4_trunc_sat_f64x2_s_zero = 0xFC,
458    i32x4_trunc_sat_f64x2_u_zero = 0xFD,
459    f64x2_convert_low_i32x4_s = 0xFE,
460    f64x2_convert_low_i32x4_u = 0xFF,
461
462    // relaxed-simd opcodes
463    i8x16_relaxed_swizzle = 0x100,
464    i32x4_relaxed_trunc_f32x4_s = 0x101,
465    i32x4_relaxed_trunc_f32x4_u = 0x102,
466    i32x4_relaxed_trunc_f64x2_s_zero = 0x103,
467    i32x4_relaxed_trunc_f64x2_u_zero = 0x104,
468    f32x4_relaxed_madd = 0x105,
469    f32x4_relaxed_nmadd = 0x106,
470    f64x2_relaxed_madd = 0x107,
471    f64x2_relaxed_nmadd = 0x108,
472    i8x16_relaxed_laneselect = 0x109,
473    i16x8_relaxed_laneselect = 0x10a,
474    i32x4_relaxed_laneselect = 0x10b,
475    i64x2_relaxed_laneselect = 0x10c,
476    f32x4_relaxed_min = 0x10d,
477    f32x4_relaxed_max = 0x10e,
478    f64x2_relaxed_min = 0x10f,
479    f64x2_relaxed_max = 0x110,
480    i16x8_relaxed_q15mulr_s = 0x111,
481    i16x8_relaxed_dot_i8x16_i7x16_s = 0x112,
482    i32x4_relaxed_dot_i8x16_i7x16_add_s = 0x113,
483    f32x4_relaxed_dot_bf16x8_add_f32x4 = 0x114,
484};
485
486/// Atomic opcodes that require a prefix `0xFE`.
487/// Each opcode represents a varuint32, meaning
488/// they are encoded as leb128 in binary.
489pub const AtomicsOpcode = enum(u32) {
490    memory_atomic_notify = 0x00,
491    memory_atomic_wait32 = 0x01,
492    memory_atomic_wait64 = 0x02,
493    atomic_fence = 0x03,
494    i32_atomic_load = 0x10,
495    i64_atomic_load = 0x11,
496    i32_atomic_load8_u = 0x12,
497    i32_atomic_load16_u = 0x13,
498    i64_atomic_load8_u = 0x14,
499    i64_atomic_load16_u = 0x15,
500    i64_atomic_load32_u = 0x16,
501    i32_atomic_store = 0x17,
502    i64_atomic_store = 0x18,
503    i32_atomic_store8 = 0x19,
504    i32_atomic_store16 = 0x1A,
505    i64_atomic_store8 = 0x1B,
506    i64_atomic_store16 = 0x1C,
507    i64_atomic_store32 = 0x1D,
508    i32_atomic_rmw_add = 0x1E,
509    i64_atomic_rmw_add = 0x1F,
510    i32_atomic_rmw8_add_u = 0x20,
511    i32_atomic_rmw16_add_u = 0x21,
512    i64_atomic_rmw8_add_u = 0x22,
513    i64_atomic_rmw16_add_u = 0x23,
514    i64_atomic_rmw32_add_u = 0x24,
515    i32_atomic_rmw_sub = 0x25,
516    i64_atomic_rmw_sub = 0x26,
517    i32_atomic_rmw8_sub_u = 0x27A,
518    i32_atomic_rmw16_sub_u = 0x28A,
519    i64_atomic_rmw8_sub_u = 0x29A,
520    i64_atomic_rmw16_sub_u = 0x2A,
521    i64_atomic_rmw32_sub_u = 0x2B,
522    i32_atomic_rmw_and = 0x2C,
523    i64_atomic_rmw_and = 0x2D,
524    i32_atomic_rmw8_and_u = 0x2E,
525    i32_atomic_rmw16_and_u = 0x2F,
526    i64_atomic_rmw8_and_u = 0x30,
527    i64_atomic_rmw16_and_u = 0x31,
528    i64_atomic_rmw32_and_u = 0x32,
529    i32_atomic_rmw_or = 0x33,
530    i64_atomic_rmw_or = 0x34,
531    i32_atomic_rmw8_or_u = 0x35,
532    i32_atomic_rmw16_or_u = 0x36,
533    i64_atomic_rmw8_or_u = 0x37,
534    i64_atomic_rmw16_or_u = 0x38,
535    i64_atomic_rmw32_or_u = 0x39,
536    i32_atomic_rmw_xor = 0x3A,
537    i64_atomic_rmw_xor = 0x3B,
538    i32_atomic_rmw8_xor_u = 0x3C,
539    i32_atomic_rmw16_xor_u = 0x3D,
540    i64_atomic_rmw8_xor_u = 0x3E,
541    i64_atomic_rmw16_xor_u = 0x3F,
542    i64_atomic_rmw32_xor_u = 0x40,
543    i32_atomic_rmw_xchg = 0x41,
544    i64_atomic_rmw_xchg = 0x42,
545    i32_atomic_rmw8_xchg_u = 0x43,
546    i32_atomic_rmw16_xchg_u = 0x44,
547    i64_atomic_rmw8_xchg_u = 0x45,
548    i64_atomic_rmw16_xchg_u = 0x46,
549    i64_atomic_rmw32_xchg_u = 0x47,
550
551    i32_atomic_rmw_cmpxchg = 0x48,
552    i64_atomic_rmw_cmpxchg = 0x49,
553    i32_atomic_rmw8_cmpxchg_u = 0x4A,
554    i32_atomic_rmw16_cmpxchg_u = 0x4B,
555    i64_atomic_rmw8_cmpxchg_u = 0x4C,
556    i64_atomic_rmw16_cmpxchg_u = 0x4D,
557    i64_atomic_rmw32_cmpxchg_u = 0x4E,
558};
559
560/// Enum representing all Wasm value types as per spec:
561/// https://webassembly.github.io/spec/core/binary/types.html
562pub const Valtype = enum(u8) {
563    i32 = 0x7F,
564    i64 = 0x7E,
565    f32 = 0x7D,
566    f64 = 0x7C,
567    v128 = 0x7B,
568};
569
570/// Reference types, where the funcref references to a function regardless of its type
571/// and ref references an object from the embedder.
572pub const RefType = enum(u8) {
573    funcref = 0x70,
574    externref = 0x6F,
575};
576
577/// Limits classify the size range of resizeable storage associated with memory types and table types.
578pub const Limits = struct {
579    flags: Flags,
580    min: u32,
581    max: u32,
582
583    pub const Flags = packed struct(u8) {
584        has_max: bool,
585        is_shared: bool,
586        reserved: u6 = 0,
587    };
588};
589
590/// Initialization expressions are used to set the initial value on an object
591/// when a wasm module is being loaded.
592pub const InitExpression = union(enum) {
593    i32_const: i32,
594    i64_const: i64,
595    f32_const: f32,
596    f64_const: f64,
597    global_get: u32,
598};
599
600/// Describes the layout of the memory where `min` represents
601/// the minimal amount of pages, and the optional `max` represents
602/// the max pages. When `null` will allow the host to determine the
603/// amount of pages.
604pub const Memory = struct {
605    limits: Limits,
606};
607
608/// Wasm module sections as per spec:
609/// https://webassembly.github.io/spec/core/binary/modules.html
610pub const Section = enum(u8) {
611    custom,
612    type,
613    import,
614    function,
615    table,
616    memory,
617    global,
618    @"export",
619    start,
620    element,
621    code,
622    data,
623    data_count,
624    _,
625};
626
627/// The kind of the type when importing or exporting to/from the host environment.
628/// https://webassembly.github.io/spec/core/syntax/modules.html
629pub const ExternalKind = enum(u8) {
630    function,
631    table,
632    memory,
633    global,
634};
635
636/// Defines the enum values for each subsection id for the "Names" custom section
637/// as described by:
638/// https://webassembly.github.io/spec/core/appendix/custom.html?highlight=name#name-section
639pub const NameSubsection = enum(u8) {
640    module,
641    function,
642    local,
643    label,
644    type,
645    table,
646    memory,
647    global,
648    elem_segment,
649    data_segment,
650};
651
652// type constants
653pub const element_type: u8 = 0x70;
654pub const function_type: u8 = 0x60;
655pub const result_type: u8 = 0x40;
656
657/// Represents a block which will not return a value
658pub const BlockType = enum(u8) {
659    empty = 0x40,
660    i32 = 0x7F,
661    i64 = 0x7E,
662    f32 = 0x7D,
663    f64 = 0x7C,
664    v128 = 0x7B,
665
666    pub fn fromValtype(valtype: Valtype) BlockType {
667        return @enumFromInt(@intFromEnum(valtype));
668    }
669};
670
671// binary constants
672pub const magic = [_]u8{ 0x00, 0x61, 0x73, 0x6D }; // \0asm
673pub const version = [_]u8{ 0x01, 0x00, 0x00, 0x00 }; // version 1 (MVP)
674
675// Each wasm page size is 64kB
676pub const page_size = 64 * 1024;