1;; This is a prelude of standard definitions for ISLE, the instruction-selector
2;; DSL, as we use it bound to our interfaces.
3;;
4;; Note that all `extern` functions here are typically defined in the
5;; `isle_prelude_methods` macro defined in `src/isa/isle.rs`
6
7;;;; Primitive and External Types ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8
9;; `()`
10(type Unit (primitive Unit))
11(model Unit (type Unit))
12
13(decl pure unit () Unit)
14(extern constructor unit unit)
15
16(model bool (type Bool))
17
18(model u8 (type (bv 8)))
19(type u8 (primitive u8))
20
21(model u16 (type (bv 16)))
22(type u16 (primitive u16))
23
24(model u32 (type (bv 32)))
25(type u32 (primitive u32))
26
27(model u64 (type (bv 64)))
28(type u64 (primitive u64))
29(type u128 (primitive u128))
30
31(model usize (type (bv)))
32(type usize (primitive usize))
33
34(model i8 (type (bv 8)))
35(type i8 (primitive i8))
36
37(model i16 (type (bv 16)))
38(type i16 (primitive i16))
39
40(model i32 (type (bv 32)))
41(type i32 (primitive i32))
42
43(model i64 (type (bv 64)))
44(type i64 (primitive i64))
45(type i128 (primitive i128))
46(type isize (primitive isize))
47
48;; `cranelift-entity`-based identifiers.
49(model Type (type Int))
50(type Type (primitive Type))
51(model Value (type (bv)))
52(type Value (primitive Value))
53(type ValueList (primitive ValueList))
54(type BlockCall (primitive BlockCall))
55
56;; ISLE representation of `&[Value]`.
57(type ValueSlice (primitive ValueSlice))
58
59;; Extract the type of a `Value`.
60(spec (value_type arg) (provide (= arg (widthof result))))
61(decl value_type (Type) Value)
62(extern extractor infallible value_type value_type)
63
64;; Extractor that matches a `u32` only if non-negative.
65(decl u32_nonnegative (u32) u32)
66(extern extractor u32_nonnegative u32_nonnegative)
67
68;; Extractor that pulls apart an Offset32 into a i32 with the raw
69;; signed-32-bit twos-complement bits.
70(decl offset32 (i32) Offset32)
71(extern extractor infallible offset32 offset32)
72
73;; Pure/fallible constructor that tests if one u32 is less than or
74;; equal to another.
75(spec (u32_lteq a b)
76    (provide (= result ()))
77    (require (<= a b)
78             (= (widthof a) 32)
79             (= (widthof b) 32)))
80(decl pure partial u32_lteq (u32 u32) Unit)
81(extern constructor u32_lteq u32_lteq)
82
83;; Pure/fallible constructor that tests if one u8 is less than or
84;; equal to another.
85(decl pure partial u8_lteq (u8 u8) Unit)
86(extern constructor u8_lteq u8_lteq)
87
88;; Pure/fallible constructor that tests if one u8 is strictly less
89;;  than another.
90(decl pure partial u8_lt (u8 u8) Unit)
91(extern constructor u8_lt u8_lt)
92
93;;;; Primitive Type Conversions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94
95(decl pure u8_as_i8 (u8) i8)
96(extern constructor u8_as_i8 u8_as_i8)
97
98(spec (u8_as_u32 arg)
99    (provide (= result (zero_ext 32 arg)))
100    (require
101      (= (widthof arg) 8)
102      (= (widthof result) 32)))
103(decl pure u8_as_u32 (u8) u32)
104(extern constructor u8_as_u32 u8_as_u32)
105(convert u8 u32 u8_as_u32)
106
107(decl pure u8_as_u64 (u8) u64)
108(extern constructor u8_as_u64 u8_as_u64)
109(convert u8 u64 u8_as_u64)
110
111(decl pure u16_as_i16 (u16) i16)
112(extern constructor u16_as_i16 u16_as_i16)
113
114(decl pure u16_as_u32 (u16) u32)
115(extern constructor u16_as_u32 u16_as_u32)
116(convert u16 u32 u16_as_u32)
117
118(decl pure u16_as_u64 (u16) u64)
119(extern constructor u16_as_u64 u16_as_u64)
120(convert u16 u64 u16_as_u64)
121
122(decl pure u64_as_u8 (u64) u8)
123(extern constructor u64_as_u8 u64_as_u8)
124
125(decl pure u64_as_u16 (u64) u16)
126(extern constructor u64_as_u16 u64_as_u16)
127
128(decl pure u64_as_i64 (u64) i64)
129(extern constructor u64_as_i64 u64_as_i64)
130
131(decl pure partial u16_try_from_u64 (u64) u16)
132(extern constructor u16_try_from_u64 u16_try_from_u64)
133
134(decl pure partial u32_try_from_u64 (u64) u32)
135(extern constructor u32_try_from_u64 u32_try_from_u64)
136
137(decl pure partial i8_try_from_u64 (u64) i8)
138(extern constructor i8_try_from_u64 i8_try_from_u64)
139
140(decl pure partial i16_try_from_u64 (u64) i16)
141(extern constructor i16_try_from_u64 i16_try_from_u64)
142
143(decl pure partial i32_try_from_u64 (u64) i32)
144(extern constructor i32_try_from_u64 i32_try_from_u64)
145
146(decl pure u32_as_u64 (u32) u64)
147(extern constructor u32_as_u64 u32_as_u64)
148(convert u32 u64 u32_as_u64)
149
150(decl pure i32_as_i64 (i32) i64)
151(extern constructor i32_as_i64 i32_as_i64)
152(convert i32 i64 i32_as_i64)
153
154(spec (i64_as_u64 arg) (provide (= arg result)))
155(decl pure i64_as_u64 (i64) u64)
156(extern constructor i64_as_u64 i64_as_u64)
157
158(spec (i64_neg x) (provide (= result (bvneg x))))
159(decl pure i64_neg (i64) i64)
160(extern constructor i64_neg i64_neg)
161
162(decl pure i8_neg (i8) i8)
163(extern constructor i8_neg i8_neg)
164
165(decl u128_as_u64 (u64) u128)
166(extern extractor u128_as_u64 u128_as_u64)
167
168(decl u64_as_u32 (u32) u64)
169(extern extractor u64_as_u32 u64_as_u32)
170
171(decl u32_as_u16 (u16) u32)
172(extern extractor u32_as_u16 u32_as_u16)
173
174(decl pure u64_as_i32 (u64) i32)
175(extern constructor u64_as_i32 u64_as_i32)
176
177;;;; Primitive Arithmetic ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
178
179(decl pure u8_and (u8 u8) u8)
180(extern constructor u8_and u8_and)
181
182(decl pure u8_shl (u8 u8) u8)
183(extern constructor u8_shl u8_shl)
184
185(decl pure u8_shr (u8 u8) u8)
186(extern constructor u8_shr u8_shr)
187
188(decl pure u8_sub (u8 u8) u8)
189(extern constructor u8_sub u8_sub)
190
191(decl pure u32_add (u32 u32) u32)
192(extern constructor u32_add u32_add)
193
194(spec (u32_sub a b) (provide (= result (bvsub a b))))
195(decl pure u32_sub (u32 u32) u32)
196(extern constructor u32_sub u32_sub)
197
198(decl pure u32_and (u32 u32) u32)
199(extern constructor u32_and u32_and)
200
201(decl pure u32_shl (u32 u32) u32)
202(extern constructor u32_shl u32_shl)
203
204;; Pure/fallible constructor that tries to add two `u32`s, interpreted
205;; as signed values, and fails to match on overflow.
206(spec (s32_add_fallible x y)
207      (provide (= result (bvadd x y)))
208      (require (not (bvsaddo x y))))
209(decl pure partial s32_add_fallible (i32 i32) i32)
210(extern constructor s32_add_fallible s32_add_fallible)
211
212(decl pure u64_add (u64 u64) u64)
213(extern constructor u64_add u64_add)
214
215(spec (u64_sub a b)
216      (provide (= result (bvsub a b))))
217(decl pure u64_sub (u64 u64) u64)
218(extern constructor u64_sub u64_sub)
219
220(decl pure u64_mul (u64 u64) u64)
221(extern constructor u64_mul u64_mul)
222
223(decl pure partial u64_sdiv (u64 u64) u64)
224(extern constructor u64_sdiv u64_sdiv)
225
226(decl pure partial u64_udiv (u64 u64) u64)
227(extern constructor u64_udiv u64_udiv)
228
229(decl pure u64_and (u64 u64) u64)
230(extern constructor u64_and u64_and)
231
232(decl pure u64_or (u64 u64) u64)
233(extern constructor u64_or u64_or)
234
235(decl pure u64_xor (u64 u64) u64)
236(extern constructor u64_xor u64_xor)
237
238(decl pure u64_shl (u64 u64) u64)
239(extern constructor u64_shl u64_shl)
240
241(decl pure imm64_shl (Type Imm64 Imm64) Imm64)
242(extern constructor imm64_shl imm64_shl)
243
244(decl pure imm64_ushr (Type Imm64 Imm64) Imm64)
245(extern constructor imm64_ushr imm64_ushr)
246
247(decl pure imm64_sshr (Type Imm64 Imm64) Imm64)
248(extern constructor imm64_sshr imm64_sshr)
249
250(decl pure u64_not (u64) u64)
251(extern constructor u64_not u64_not)
252
253(decl pure u64_eq (u64 u64) bool)
254(extern constructor u64_eq u64_eq)
255
256(decl pure u64_le (u64 u64) bool)
257(extern constructor u64_le u64_le)
258
259(decl pure u64_lt (u64 u64) bool)
260(extern constructor u64_lt u64_lt)
261
262(decl pure i64_shr (i64 i64) i64)
263(extern constructor i64_shr i64_shr)
264
265(decl pure i64_ctz (i64) i64)
266(extern constructor i64_ctz i64_ctz)
267
268;; Sign extends a u64 from ty bits up to 64bits
269(decl pure i64_sextend_u64 (Type u64) i64)
270(extern constructor i64_sextend_u64 i64_sextend_u64)
271
272(spec (i64_sextend_imm64 ty a) (provide (= result (sign_ext 64 (conv_to ty a)))))
273(decl pure i64_sextend_imm64 (Type Imm64) i64)
274(extern constructor i64_sextend_imm64 i64_sextend_imm64)
275
276(decl pure u64_uextend_imm64 (Type Imm64) u64)
277(extern constructor u64_uextend_imm64 u64_uextend_imm64)
278
279(decl pure imm64_icmp (Type IntCC Imm64 Imm64) Imm64)
280(extern constructor imm64_icmp imm64_icmp)
281
282(decl u64_is_zero (bool) u64)
283(extern extractor infallible u64_is_zero u64_is_zero)
284
285(decl i64_is_zero (bool) i64)
286(extern extractor infallible i64_is_zero i64_is_zero)
287
288(decl u64_zero () u64)
289(extractor (u64_zero) (u64_is_zero true))
290
291(decl u64_nonzero (u64) u64)
292(extractor (u64_nonzero x) (and (u64_is_zero false) x))
293
294(decl i64_nonzero (i64) i64)
295(extractor (i64_nonzero x) (and (i64_is_zero false) x))
296
297;; x & 1 == 1
298(spec (u64_is_odd arg) (provide (= result (= #b1 (extract 0 0 arg)))))
299(decl pure u64_is_odd (u64) bool)
300(extern constructor u64_is_odd u64_is_odd)
301
302;; Each of these extractors tests whether the upper half of the input equals the
303;; lower half of the input
304(decl u128_replicated_u64 (u64) u128)
305(extern extractor u128_replicated_u64 u128_replicated_u64)
306(decl u64_replicated_u32 (u64) u64)
307(extern extractor u64_replicated_u32 u64_replicated_u32)
308(decl u32_replicated_u16 (u64) u64)
309(extern extractor u32_replicated_u16 u32_replicated_u16)
310(decl u16_replicated_u8 (u8) u64)
311(extern extractor u16_replicated_u8 u16_replicated_u8)
312
313;; Floating point operations
314
315(decl pure partial f16_min (Ieee16 Ieee16) Ieee16)
316(extern constructor f16_min f16_min)
317(decl pure partial f16_max (Ieee16 Ieee16) Ieee16)
318(extern constructor f16_max f16_max)
319(decl pure f16_neg (Ieee16) Ieee16)
320(extern constructor f16_neg f16_neg)
321(decl pure f16_abs (Ieee16) Ieee16)
322(extern constructor f16_abs f16_abs)
323(decl pure f16_copysign (Ieee16 Ieee16) Ieee16)
324(extern constructor f16_copysign f16_copysign)
325(decl pure partial f32_add (Ieee32 Ieee32) Ieee32)
326(extern constructor f32_add f32_add)
327(decl pure partial f32_sub (Ieee32 Ieee32) Ieee32)
328(extern constructor f32_sub f32_sub)
329(decl pure partial f32_mul (Ieee32 Ieee32) Ieee32)
330(extern constructor f32_mul f32_mul)
331(decl pure partial f32_div (Ieee32 Ieee32) Ieee32)
332(extern constructor f32_div f32_div)
333(decl pure partial f32_sqrt (Ieee32) Ieee32)
334(extern constructor f32_sqrt f32_sqrt)
335(decl pure partial f32_ceil (Ieee32) Ieee32)
336(extern constructor f32_ceil f32_ceil)
337(decl pure partial f32_floor (Ieee32) Ieee32)
338(extern constructor f32_floor f32_floor)
339(decl pure partial f32_trunc (Ieee32) Ieee32)
340(extern constructor f32_trunc f32_trunc)
341(decl pure partial f32_nearest (Ieee32) Ieee32)
342(extern constructor f32_nearest f32_nearest)
343(decl pure partial f32_min (Ieee32 Ieee32) Ieee32)
344(extern constructor f32_min f32_min)
345(decl pure partial f32_max (Ieee32 Ieee32) Ieee32)
346(extern constructor f32_max f32_max)
347(decl pure f32_neg (Ieee32) Ieee32)
348(extern constructor f32_neg f32_neg)
349(decl pure f32_abs (Ieee32) Ieee32)
350(extern constructor f32_abs f32_abs)
351(decl pure f32_copysign (Ieee32 Ieee32) Ieee32)
352(extern constructor f32_copysign f32_copysign)
353(decl pure partial f64_add (Ieee64 Ieee64) Ieee64)
354(extern constructor f64_add f64_add)
355(decl pure partial f64_sub (Ieee64 Ieee64) Ieee64)
356(extern constructor f64_sub f64_sub)
357(decl pure partial f64_mul (Ieee64 Ieee64) Ieee64)
358(extern constructor f64_mul f64_mul)
359(decl pure partial f64_div (Ieee64 Ieee64) Ieee64)
360(extern constructor f64_div f64_div)
361(decl pure partial f64_sqrt (Ieee64) Ieee64)
362(extern constructor f64_sqrt f64_sqrt)
363(decl pure partial f64_ceil (Ieee64) Ieee64)
364(extern constructor f64_ceil f64_ceil)
365(decl pure partial f64_floor (Ieee64) Ieee64)
366(extern constructor f64_floor f64_floor)
367(decl pure partial f64_trunc (Ieee64) Ieee64)
368(extern constructor f64_trunc f64_trunc)
369(decl pure partial f64_nearest (Ieee64) Ieee64)
370(extern constructor f64_nearest f64_nearest)
371(decl pure partial f64_min (Ieee64 Ieee64) Ieee64)
372(extern constructor f64_min f64_min)
373(decl pure partial f64_max (Ieee64 Ieee64) Ieee64)
374(extern constructor f64_max f64_max)
375(decl pure f64_neg (Ieee64) Ieee64)
376(extern constructor f64_neg f64_neg)
377(decl pure f64_abs (Ieee64) Ieee64)
378(extern constructor f64_abs f64_abs)
379(decl pure f64_copysign (Ieee64 Ieee64) Ieee64)
380(extern constructor f64_copysign f64_copysign)
381(decl pure partial f128_min (Ieee128 Ieee128) Ieee128)
382(extern constructor f128_min f128_min)
383(decl pure partial f128_max (Ieee128 Ieee128) Ieee128)
384(extern constructor f128_max f128_max)
385(decl pure f128_neg (Ieee128) Ieee128)
386(extern constructor f128_neg f128_neg)
387(decl pure f128_abs (Ieee128) Ieee128)
388(extern constructor f128_abs f128_abs)
389(decl pure f128_copysign (Ieee128 Ieee128) Ieee128)
390(extern constructor f128_copysign f128_copysign)
391(type Ieee128 (primitive Ieee128))
392
393;;;; `cranelift_codegen::ir::Type` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
394
395(extern const $I8 Type)
396(extern const $I16 Type)
397(extern const $I32 Type)
398(extern const $I64 Type)
399(extern const $I128 Type)
400
401(extern const $F16 Type)
402(extern const $F32 Type)
403(extern const $F64 Type)
404(extern const $F128 Type)
405
406(extern const $I8X8 Type)
407(extern const $I8X16 Type)
408(extern const $I16X4 Type)
409(extern const $I16X8 Type)
410(extern const $I32X2 Type)
411(extern const $I32X4 Type)
412(extern const $I64X2 Type)
413
414(extern const $F32X4 Type)
415(extern const $F64X2 Type)
416
417(extern const $I32X4XN Type)
418
419;; Get the unsigned minimum value for a given type.
420;; This always zero, but is included for completeness.
421(decl pure ty_umin (Type) u64)
422(extern constructor ty_umin ty_umin)
423
424;; Get the unsigned maximum value for a given type.
425(decl pure ty_umax (Type) u64)
426(extern constructor ty_umax ty_umax)
427
428;; Get the signed minimum value for a given type.
429(decl pure ty_smin (Type) u64)
430(extern constructor ty_smin ty_smin)
431
432;; Get the signed maximum value for a given type.
433(decl pure ty_smax (Type) u64)
434(extern constructor ty_smax ty_smax)
435
436;; Get the bit width of a given type.
437(spec (ty_bits x) (provide (= result (int2bv 8 x))))
438(decl pure ty_bits (Type) u8)
439(extern constructor ty_bits ty_bits)
440
441;; Get the bit width of a given type.
442(spec (ty_bits_u16 x)
443      (provide (= result (int2bv 16 x))))
444(decl pure ty_bits_u16 (Type) u16)
445(extern constructor ty_bits_u16 ty_bits_u16)
446
447;; Get the bit width of a given type.
448(decl pure ty_bits_u64 (Type) u64)
449(extern constructor ty_bits_u64 ty_bits_u64)
450
451;; Get a mask for the width of a given type.
452(decl pure ty_mask (Type) u64)
453(extern constructor ty_mask ty_mask)
454
455;; Get a mask that is set for each lane in a given type.
456(decl pure ty_lane_mask (Type) u64)
457(extern constructor ty_lane_mask ty_lane_mask)
458
459;; Get the number of lanes for a given type.
460(decl pure ty_lane_count (Type) u64)
461(extern constructor ty_lane_count ty_lane_count)
462
463;; Get the byte width of a given type.
464(decl pure ty_bytes (Type) u16)
465(extern constructor ty_bytes ty_bytes)
466
467;; Get the type of each lane in the given type.
468(decl pure lane_type (Type) Type)
469(extern constructor lane_type lane_type)
470
471;; Get a type with the same element type, but half the number of lanes.
472(decl pure partial ty_half_lanes (Type) Type)
473(extern constructor ty_half_lanes ty_half_lanes)
474
475;; Get a type with the same number of lanes but a lane type that is half as small.
476(decl pure partial ty_half_width (Type) Type)
477(extern constructor ty_half_width ty_half_width)
478
479;; Generate a mask for the maximum shift amount for a given type. i.e 31 for I32.
480(decl pure ty_shift_mask (Type) u64)
481(rule (ty_shift_mask ty) (u64_sub (ty_bits (lane_type ty)) 1))
482
483;; Compare two types for equality.
484(decl pure ty_equal (Type Type) bool)
485(extern constructor ty_equal ty_equal)
486
487;;;; `cranelift_codegen::ir::MemFlags ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
488
489;; Provide model for the MemFlags type (declared in generated clif_lower.isle).
490(model MemFlags (type (bv 16)))
491
492;; `MemFlags::trusted`
493(spec (mem_flags_trusted)
494      (provide (= result #x0003)))
495(decl pure mem_flags_trusted () MemFlags)
496(extern constructor mem_flags_trusted mem_flags_trusted)
497
498;;;; Helpers for Working with Flags ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
499
500;; Swap args of an IntCC flag.
501(decl intcc_swap_args (IntCC) IntCC)
502(extern constructor intcc_swap_args intcc_swap_args)
503
504;; Complement an IntCC flag.
505(decl intcc_complement (IntCC) IntCC)
506(extern constructor intcc_complement intcc_complement)
507
508;; This is a direct import of `IntCC::without_equal`.
509;; Get the corresponding IntCC with the equal component removed.
510;; For conditions without a zero component, this is a no-op.
511(decl pure intcc_without_eq (IntCC) IntCC)
512(extern constructor intcc_without_eq intcc_without_eq)
513
514;; Swap args of a FloatCC flag.
515(decl floatcc_swap_args (FloatCC) FloatCC)
516(extern constructor floatcc_swap_args floatcc_swap_args)
517
518;; Complement a FloatCC flag.
519(decl floatcc_complement (FloatCC) FloatCC)
520(extern constructor floatcc_complement floatcc_complement)
521
522;; True when this FloatCC involves an unordered comparison.
523(decl pure floatcc_unordered (FloatCC) bool)
524(extern constructor floatcc_unordered floatcc_unordered)
525
526;;;; Helper Clif Extractors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
527
528(decl eq (Type Value Value) Value)
529(extractor (eq ty x y) (icmp ty (IntCC.Equal) x y))
530
531(decl ne (Type Value Value) Value)
532(extractor (ne ty x y) (icmp ty (IntCC.NotEqual) x y))
533
534(decl ult (Type Value Value) Value)
535(extractor (ult ty x y) (icmp ty (IntCC.UnsignedLessThan) x y))
536
537(decl ule (Type Value Value) Value)
538(extractor (ule ty x y) (icmp ty (IntCC.UnsignedLessThanOrEqual) x y))
539
540(decl ugt (Type Value Value) Value)
541(extractor (ugt ty x y) (icmp ty (IntCC.UnsignedGreaterThan) x y))
542
543(decl uge (Type Value Value) Value)
544(extractor (uge ty x y) (icmp ty (IntCC.UnsignedGreaterThanOrEqual) x y))
545
546(decl slt (Type Value Value) Value)
547(extractor (slt ty x y) (icmp ty (IntCC.SignedLessThan) x y))
548
549(decl sle (Type Value Value) Value)
550(extractor (sle ty x y) (icmp ty (IntCC.SignedLessThanOrEqual) x y))
551
552(decl sgt (Type Value Value) Value)
553(extractor (sgt ty x y) (icmp ty (IntCC.SignedGreaterThan) x y))
554
555(decl sge (Type Value Value) Value)
556(extractor (sge ty x y) (icmp ty (IntCC.SignedGreaterThanOrEqual) x y))
557
558;; An extractor that only matches types that can fit in 16 bits.
559(spec (fits_in_16 arg)
560      (provide (= result arg))
561      (require (<= arg 16)))
562(decl fits_in_16 (Type) Type)
563(extern extractor fits_in_16 fits_in_16)
564
565;; An extractor that only matches types that can fit in 32 bits.
566(spec (fits_in_32 arg)
567      (provide (= result arg))
568      (require (<= arg 32)))
569(decl fits_in_32 (Type) Type)
570(extern extractor fits_in_32 fits_in_32)
571
572;; An extractor that only matches types that can fit in 32 bits.
573(decl lane_fits_in_32 (Type) Type)
574(extern extractor lane_fits_in_32 lane_fits_in_32)
575
576;; An extractor that only matches types that can fit in 64 bits.
577(spec (fits_in_64 arg)
578      (provide (= result arg))
579      (require (<= arg 64)))
580(decl fits_in_64 (Type) Type)
581(extern extractor fits_in_64 fits_in_64)
582
583;; An extractor that only matches types that fit in exactly 32 bits.
584(decl ty_32 (Type) Type)
585(extern extractor ty_32 ty_32)
586
587;; An extractor that only matches types that fit in exactly 64 bits.
588(decl ty_64 (Type) Type)
589(extern extractor ty_64 ty_64)
590
591;; A pure constructor/extractor that only matches scalar integers, and
592;; references that can fit in 64 bits.
593(spec (ty_int_ref_scalar_64 arg)
594    (provide (= result arg))
595    (require (<= arg 64)))
596(decl pure partial ty_int_ref_scalar_64 (Type) Type)
597(extern constructor ty_int_ref_scalar_64 ty_int_ref_scalar_64)
598(extern extractor ty_int_ref_scalar_64 ty_int_ref_scalar_64_extract)
599
600;; An extractor that matches 32- and 64-bit types only.
601(spec (ty_32_or_64 arg)
602      (provide (= result arg))
603      (require (or (= arg 32) (= arg 64))))
604(decl ty_32_or_64 (Type) Type)
605(extern extractor ty_32_or_64 ty_32_or_64)
606
607;; An extractor that matches 8- and 16-bit types only.
608(decl ty_8_or_16 (Type) Type)
609(extern extractor ty_8_or_16 ty_8_or_16)
610
611;; An extractor that matches 16- and 32-bit types only.
612(decl ty_16_or_32 (Type) Type)
613(extern extractor ty_16_or_32 ty_16_or_32)
614
615;; An extractor that matches int types that fit in 32 bits.
616(decl int_fits_in_32 (Type) Type)
617(extern extractor int_fits_in_32 int_fits_in_32)
618
619;; An extractor that matches I64.
620(decl ty_int_ref_64 (Type) Type)
621(extern extractor ty_int_ref_64 ty_int_ref_64)
622
623;; An extractor that matches int or reference types bigger than 16 bits but at most 64 bits.
624(decl ty_int_ref_16_to_64 (Type) Type)
625(extern extractor ty_int_ref_16_to_64 ty_int_ref_16_to_64)
626
627;; An extractor that only matches integers.
628(spec (ty_int a) (provide (= result a)))
629(decl ty_int (Type) Type)
630(extern extractor ty_int ty_int)
631
632;; An extractor that only matches scalar types, float or int or ref's.
633(decl ty_scalar (Type) Type)
634(extern extractor ty_scalar ty_scalar)
635
636;; An extractor that only matches scalar floating-point types--F32 or F64.
637(decl ty_scalar_float (Type) Type)
638(extern extractor ty_scalar_float ty_scalar_float)
639
640;; An extractor that matches scalar floating-point types or vector types.
641(decl ty_float_or_vec (Type) Type)
642(extern extractor ty_float_or_vec ty_float_or_vec)
643
644;; A pure constructor that only matches vector floating-point types.
645(decl pure partial ty_vector_float (Type) Type)
646(extern constructor ty_vector_float ty_vector_float)
647
648;; A pure constructor that only matches vector types with lanes which
649;; are not floating-point.
650(decl pure partial ty_vector_not_float (Type) Type)
651(extern constructor ty_vector_not_float ty_vector_not_float)
652
653;; A pure constructor/extractor that only matches 64-bit vector types.
654(decl pure partial ty_vec64 (Type) Type)
655(extern constructor ty_vec64 ty_vec64_ctor)
656(extern extractor ty_vec64 ty_vec64)
657
658;; An extractor that only matches 128-bit vector types.
659(decl ty_vec128 (Type) Type)
660(extern extractor ty_vec128 ty_vec128)
661
662;; An extractor that only matches dynamic vector types with a 64-bit
663;; base type.
664(decl ty_dyn_vec64 (Type) Type)
665(extern extractor ty_dyn_vec64 ty_dyn_vec64)
666
667;; An extractor that only matches dynamic vector types with a 128-bit
668;; base type.
669(decl ty_dyn_vec128 (Type) Type)
670(extern extractor ty_dyn_vec128 ty_dyn_vec128)
671
672;; An extractor that only matches 64-bit vector types with integer
673;; lanes (I8X8, I16X4, I32X2)
674(decl ty_vec64_int (Type) Type)
675(extern extractor ty_vec64_int ty_vec64_int)
676
677;; An extractor that only matches 128-bit vector types with integer
678;; lanes (I8X16, I16X8, I32X4, I64X2).
679(decl ty_vec128_int (Type) Type)
680(extern extractor ty_vec128_int ty_vec128_int)
681
682;; An extractor that only matches types that can be a 64-bit address.
683(decl ty_addr64 (Type) Type)
684(extern extractor ty_addr64 ty_addr64)
685
686;; A pure constructor that matches everything except vectors with size 32X2.
687(decl pure partial not_vec32x2 (Type) Type)
688(extern constructor not_vec32x2 not_vec32x2)
689
690;; An extractor that matches everything except I64X2
691(decl not_i64x2 () Type)
692(extern extractor not_i64x2 not_i64x2)
693
694;; Extract a `u8` from an `Uimm8`.
695(decl u8_from_uimm8 (u8) Uimm8)
696(extern extractor infallible u8_from_uimm8 u8_from_uimm8)
697
698;; Extract a `u64` from a `bool`.
699(decl u64_from_bool (u64) bool)
700(extern extractor infallible u64_from_bool u64_from_bool)
701
702;; Extract a `u64` from an `Imm64`.
703(spec (u64_from_imm64 arg) (provide (= arg result)))
704(decl u64_from_imm64 (u64) Imm64)
705(extern extractor infallible u64_from_imm64 u64_from_imm64)
706
707;; Extract a `u64` from an `Imm64` which is not zero.
708(decl nonzero_u64_from_imm64 (u64) Imm64)
709(extern extractor nonzero_u64_from_imm64 nonzero_u64_from_imm64)
710
711;; If the given `Imm64` is a power-of-two, extract its log2 value.
712(decl imm64_power_of_two (u64) Imm64)
713(extern extractor imm64_power_of_two imm64_power_of_two)
714
715;; Create a new Imm64.
716(decl pure imm64 (u64) Imm64)
717(extern constructor imm64 imm64)
718
719;; Create a new Imm64, masked to the width of the given type.
720(decl pure imm64_masked (Type u64) Imm64)
721(extern constructor imm64_masked imm64_masked)
722
723;; Extract a `u16` from an `Ieee16`.
724(decl u16_from_ieee16 (u16) Ieee16)
725(extern extractor infallible u16_from_ieee16 u16_from_ieee16)
726
727;; Extract a `u32` from an `Ieee32`.
728(decl u32_from_ieee32 (u32) Ieee32)
729(extern extractor infallible u32_from_ieee32 u32_from_ieee32)
730
731;; Extract a `u64` from an `Ieee64`.
732(decl u64_from_ieee64 (u64) Ieee64)
733(extern extractor infallible u64_from_ieee64 u64_from_ieee64)
734
735;; Match a multi-lane type, extracting (# bits per lane, # lanes) from the given
736;; type. Will only match when there is more than one lane.
737(decl multi_lane (u32 u32) Type)
738(extern extractor multi_lane multi_lane)
739
740;; Match a dynamic-lane type, extracting (# bits per lane) from the given
741;; type.
742(decl dynamic_lane (u32 u32) Type)
743(extern extractor dynamic_lane dynamic_lane)
744
745;; An extractor that only matches 64-bit dynamic vector types with integer
746;; lanes (I8X8XN, I16X4XN, I32X2XN)
747(decl ty_dyn64_int (Type) Type)
748(extern extractor ty_dyn64_int ty_dyn64_int)
749
750;; An extractor that only matches 128-bit dynamic vector types with integer
751;; lanes (I8X16XN, I16X8XN, I32X4XN, I64X2XN).
752(decl ty_dyn128_int (Type) Type)
753(extern extractor ty_dyn128_int ty_dyn128_int)
754
755;; Convert an `Offset32` to a primitive number.
756(spec (offset32_to_i32 offset) (provide (= result offset)))
757(decl pure offset32_to_i32 (Offset32) i32)
758(extern constructor offset32_to_i32 offset32_to_i32)
759
760;; Convert a number to an `Offset32`
761(spec (i32_to_offset32 x) (provide (= result x)))
762(decl pure i32_to_offset32 (i32) Offset32)
763(extern constructor i32_to_offset32 i32_to_offset32)
764
765;; This is a direct import of `IntCC::unsigned`.
766;; Get the corresponding IntCC with the signed component removed.
767;; For conditions without a signed component, this is a no-op.
768(decl pure intcc_unsigned (IntCC) IntCC)
769(extern constructor intcc_unsigned intcc_unsigned)
770
771;; Pure constructor that only matches signed integer cond codes.
772(spec (signed_cond_code c)
773    (provide (= result c))
774    (require (and (bvuge c #x02) (bvule c #x05))))
775(decl pure partial signed_cond_code (IntCC) IntCC)
776(extern constructor signed_cond_code signed_cond_code)
777
778;;;; Helpers for Working with TrapCode ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
779
780(decl pure trap_code_division_by_zero () TrapCode)
781(extern constructor trap_code_division_by_zero trap_code_division_by_zero)
782
783(decl pure trap_code_integer_overflow () TrapCode)
784(extern constructor trap_code_integer_overflow trap_code_integer_overflow)
785
786(decl pure trap_code_bad_conversion_to_integer () TrapCode)
787(extern constructor trap_code_bad_conversion_to_integer trap_code_bad_conversion_to_integer)
788
789;;;; Helpers for tail recursion loops ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
790
791;; A range of integers to loop through.
792(type Range (primitive Range))
793
794;; Create a new range from `start` through `end` (exclusive).
795(decl pure range (usize usize) Range)
796(extern constructor range range)
797
798;; A view on the current state of the range.
799(type RangeView extern
800      (enum
801        (Empty)
802        (NonEmpty (index usize) (rest Range))))
803
804;; View the current state of the range.
805(decl range_view (RangeView) Range)
806(extern extractor infallible range_view range_view)
807
808;; Extractor to test whether a range is empty.
809(decl range_empty () Range)
810(extractor (range_empty) (range_view (RangeView.Empty)))
811
812;; Extractor to return the first value in the range, and a sub-range
813;; containing the remaining values.
814(decl range_unwrap (usize Range) Range)
815(extractor (range_unwrap index rest) (range_view (RangeView.NonEmpty index rest)))
816
817;;;; Automatic conversions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
818
819(convert Offset32 i32 offset32_to_i32)
820(convert i32 Offset32 i32_to_offset32)
821
822;;;; Common Term Signatures ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
823
824(form
825  bv_unary_8_to_64
826  ((args (bv  8)) (ret (bv  8)) (canon (bv  8)))
827  ((args (bv 16)) (ret (bv 16)) (canon (bv 16)))
828  ((args (bv 32)) (ret (bv 32)) (canon (bv 32)))
829  ((args (bv 64)) (ret (bv 64)) (canon (bv 64)))
830)
831
832(form
833  bv_binary_8_to_64
834  ((args (bv  8) (bv  8)) (ret (bv  8)) (canon (bv  8)))
835  ((args (bv 16) (bv 16)) (ret (bv 16)) (canon (bv 16)))
836  ((args (bv 32) (bv 32)) (ret (bv 32)) (canon (bv 32)))
837  ((args (bv 64) (bv 64)) (ret (bv 64)) (canon (bv 64)))
838)
839
840(form
841  bv_ternary_8_to_64
842  ((args (bv  8) (bv  8) (bv  8)) (ret (bv  8)) (canon (bv  8)))
843  ((args (bv 16) (bv 16) (bv 16)) (ret (bv 16)) (canon (bv 16)))
844  ((args (bv 32) (bv 32) (bv 32)) (ret (bv 32)) (canon (bv 32)))
845  ((args (bv 64) (bv 64) (bv 64)) (ret (bv 64)) (canon (bv 64)))
846)
847