forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinst.isle
1568 lines (1256 loc) · 41.7 KB
/
inst.isle
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; Instruction formats.
(type MInst
(enum
(Nop)
;; Label to output at the beginning of a block
(Label
(imm usize))
(LoadConst32
(rd WritableReg)
(imm u32))
(LoadConst64
(rd WritableReg)
(imm u64))
;; An ALU operation with two register sources and a register destination.
(AluRRR
(alu_op AluOPRRR)
(rd WritableReg)
(rs1 Reg)
(rs2 Reg))
;;Multiplication via Arith
(MulArith
(rd WritableReg)
(rs1 Reg)
(rs2 Reg))
(Shl64
(rd WritableReg)
(rs1 Reg)
(rs2 Reg))
(Shru64
(rd WritableReg)
(rs1 Reg)
(rs2 Reg))
;;Division via Arith
(DivArith
(rd WritableReg)
(rs1 Reg)
(rs2 Reg))
(UDivArith
(rd WritableReg)
(rs1 Reg)
(rs2 Reg))
(RemArith
(rd WritableReg)
(rs1 Reg)
(rs2 Reg))
(URemArith
(rd WritableReg)
(rs1 Reg)
(rs2 Reg))
(Ineg
(rd WritableReg)
(rs1 Reg))
(Bnot
(rd WritableReg)
(rs1 Reg))
;; An load
(Load
(rd WritableReg)
(op LoadOP)
(flags MemFlags)
(from AMode))
;; An Store
(Store
(to AMode)
(op StoreOP)
(flags MemFlags)
(src Reg))
;; A pseudo-instruction that captures register arguments in vregs.
(Args
(args VecArgPair))
(Ret (rets VecRetPair)
(stack_bytes_to_pop u64))
(Extend
(rd WritableReg)
(rn Reg)
(signed bool)
(from_bits u8)
(to_bits u8))
;; Adjust the stack pointer as necessary.
(ReserveSp
(amount u64))
(ReleaseSp
(amount u64))
(Call
(info BoxCallInfo))
;; A machine indirect-call instruction.
(CallInd
(info BoxCallIndInfo))
;; A direct return-call macro instruction.
(ReturnCall
(callee BoxExternalName)
(info BoxReturnCallInfo))
;; An indirect return-call macro instruction.
(ReturnCallInd
(callee Reg)
(info BoxReturnCallInfo))
(TrapIf
(test Reg)
(trap_code TrapCode))
;; use a simple compare to decide to cause trap or not.
(TrapIfC
(rs1 Reg)
(rs2 Reg)
(cc IntCC)
(trap_code TrapCode))
(Jal
;; (rd WritableReg) don't use
(dest BranchTarget))
(CondBr
(taken BranchTarget)
(not_taken BranchTarget)
(kind IntegerCompare))
;; Load an inline symbol reference.
(LoadExtName
(rd WritableReg)
(name BoxExternalName)
(offset i64))
;; Load address referenced by `mem` into `rd`.
(LoadAddr
(rd WritableReg)
(mem AMode))
;; Marker, no-op in generated code: SP "virtual offset" is adjusted. This
;; controls how AMode::NominalSPOffset args are lowered.
(VirtualSPOffsetAdj
(amount i64))
;; A MOV instruction. These are encoded as OrR's (AluRRR form) but we
;; keep them separate at the `Inst` level for better pretty-printing
;; and faster `is_move()` logic.
(Mov
(rd WritableReg)
(rm Reg)
(ty Type))
;; A MOV instruction, but where the source register is a non-allocatable
;; PReg. It's important that the register be non-allocatable, as regalloc2
;; will not see it as used.
(MovFromPReg
(rd WritableReg)
(rm PReg))
(ECall)
(EBreak)
;; An instruction guaranteed to always be undefined and to trigger an illegal instruction at
;; runtime.
(Udf
(trap_code TrapCode))
;; a jump and link register operation
(Jalr
;;Plain unconditional jumps (assembler pseudo-op J) are encoded as a JAL with rd=x0.
(rd WritableReg)
(base Reg)
(offset Imm32))
;; select x or y base on condition
(Select
(dst VecWritableReg)
(ty Type)
(condition Reg)
(x ValueRegs)
(y ValueRegs))
(BrTable
(index Reg)
(tmp1 WritableReg)
(tmp2 WritableReg)
(targets VecBranchTarget))
;; select x or y base on op_code
(IntSelect
(op IntSelectOP)
(dst VecWritableReg)
(x ValueRegs)
(y ValueRegs)
(ty Type))
;; an integer compare.
(Icmp
(cc IntCC)
(rd WritableReg)
(a ValueRegs)
(b ValueRegs)
(ty Type))
;; select a reg base on condition.
;; very useful because in lowering stage we can not have condition branch.
(SelectReg
(rd WritableReg)
(rs1 Reg)
(rs2 Reg)
(condition IntegerCompare))
(RawData (data VecU8))
;; An unwind pseudo-instruction.
(Unwind
(inst UnwindInst))
;; A dummy use, useful to keep a value alive.
(DummyUse
(reg Reg))
;; popcnt if target doesn't support extension B
;; use iteration to implement.
(Popcnt
(sum WritableReg)
(step WritableReg)
(tmp WritableReg)
(rs Reg)
(ty Type))
;;; counting leading or trailing zeros.
(Cltz
;; leading or trailing.
(leading bool)
(sum WritableReg)
(step WritableReg)
(tmp WritableReg)
(rs Reg)
(ty Type))
;; Byte-reverse register
(Rev8
(rs Reg)
(step WritableReg)
(tmp WritableReg)
(rd WritableReg))
;;
(Brev8
(rs Reg)
(ty Type)
(step WritableReg)
(tmp WritableReg)
(tmp2 WritableReg)
(rd WritableReg))
(StackProbeLoop
(guard_size u32)
(probe_count u32)
(tmp WritableReg))
;; An addition with 2 32-bit immediates.
(AddImm32
(rd WritableReg)
(src1 Imm32)
(src2 Imm32))
))
(type IntSelectOP (enum
(Smax)
(Umax)
(Smin)
(Umin)
))
(type AtomicOP (enum
(LrW)
(ScW)
(AmoswapW)
(AmoaddW)
(AmoxorW)
(AmoandW)
(AmoorW)
(AmominW)
(AmomaxW)
(AmominuW)
(AmomaxuW)
(LrD)
(ScD)
(AmoswapD)
(AmoaddD)
(AmoxorD)
(AmoandD)
(AmoorD)
(AmominD)
(AmomaxD)
(AmominuD)
(AmomaxuD)
))
(type LoadOP (enum
(I8)
(I16)
(I32)
(U8)
(U16)
(U32)
(U64)
(Flw)
(Fld)
))
(type StoreOP (enum
(I8)
(I16)
(I32)
(I64)
(Fsw)
(Fsd)
))
(type ZkasmBase (enum
(Heap)
(Global)
(Table)
))
(type AluOPRRR (enum
;; base set
(Add)
(Sub)
(Sll)
(Slt)
(SltU)
(Sgt)
(Sgtu)
(Xor)
(Srl)
(Sra)
(Or)
(And)
;; RV64I Base Instruction Set (in addition to RV32I)
(Addw)
(Subw)
(Sllw)
(Srlw)
(Sraw)
;;RV32M Standard Extension
(Mulh)
(Mulhsu)
(Mulhu)
(DivU)
(Rem)
(RemU)
;; RV64M Standard Extension (in addition to RV32M)
(Divuw)
(Remw)
(Remuw)
;; Zba: Address Generation Instructions
(Adduw)
(Sh1add)
(Sh1adduw)
(Sh2add)
(Sh2adduw)
(Sh3add)
(Sh3adduw)
;; Zbb: Bit Manipulation Instructions
(Andn)
(Orn)
(Xnor)
(Max)
(Maxu)
(Min)
(Minu)
(Rol)
(Rolw)
(Ror)
(Rorw)
;; Zbs: Single-bit instructions
(Bclr)
(Bext)
(Binv)
(Bset)
;; Zbc: Carry-less multiplication
(Clmul)
(Clmulh)
(Clmulr)
;; Zbkb: Bit-manipulation for Cryptography
(Pack)
(Packw)
(Packh)
))
(type AluOPRRI (enum
;; Base ISA
(Addi)
(Slti)
(SltiU)
(Xori)
(Ori)
(Andi)
(Slli)
(Srli)
(Srai)
(Addiw)
(Slliw)
(SrliW)
(Sraiw)
;; Zba: Address Generation Instructions
(SlliUw)
;; Zbb: Bit Manipulation Instructions
(Clz)
(Clzw)
(Ctz)
(Ctzw)
(Cpop)
(Cpopw)
(Sextb)
(Sexth)
(Zexth)
(Rori)
(Roriw)
(Rev8)
(Brev8)
(Orcb)
;; Zbs: Single-bit instructions
(Bclri)
(Bexti)
(Binvi)
(Bseti)
))
(type FFlagsException (enum
;; Invalid Operation
(NV)
;; Divide by Zero
(DZ)
;; Overflow
(OF)
;; Underflow
(UF)
;; Inexact
(NX)
))
;;;; input output read write
;;;; SI SO SR SW
;;;; PI PO PR PW
;;;; lowest four bit are used.
(type FenceReq (primitive u8))
(type VecBranchTarget (primitive VecBranchTarget))
(type BoxCallInfo (primitive BoxCallInfo))
(type BoxCallIndInfo (primitive BoxCallIndInfo))
(type BoxReturnCallInfo (primitive BoxReturnCallInfo))
(type IntegerCompare (primitive IntegerCompare))
(type AMode (primitive AMode))
(type OptionReg (primitive OptionReg))
(type OptionImm12 (primitive OptionImm12))
(type OptionUimm5 (primitive OptionUimm5))
(type Imm12 (primitive Imm12))
(type Imm32 (primitive Imm32))
(type UImm5 (primitive UImm5))
(type Imm5 (primitive Imm5))
(type Imm20 (primitive Imm20))
(type Imm3 (primitive Imm3))
(type BranchTarget (primitive BranchTarget))
(type VecU8 (primitive VecU8))
(type AMO (primitive AMO))
(type VecMachLabel extern (enum))
;;;; Newtypes for Different Register Classes ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(type XReg (primitive XReg))
(type WritableXReg (primitive WritableXReg))
(type FReg (primitive FReg))
(type WritableFReg (primitive WritableFReg))
(type VReg (primitive VReg))
(type WritableVReg (primitive WritableVReg))
;; Construct a new `XReg` from a `Reg`.
;;
;; Asserts that the register has a Integer RegClass.
(decl xreg_new (Reg) XReg)
(extern constructor xreg_new xreg_new)
(convert Reg XReg xreg_new)
;; Construct a new `WritableXReg` from a `WritableReg`.
;;
;; Asserts that the register has a Integer RegClass.
(decl writable_xreg_new (WritableReg) WritableXReg)
(extern constructor writable_xreg_new writable_xreg_new)
(convert WritableReg WritableXReg writable_xreg_new)
;; Put a value into a XReg.
;;
;; Asserts that the value goes into a XReg.
(decl put_in_xreg (Value) XReg)
(rule (put_in_xreg val) (xreg_new (put_in_reg val)))
(convert Value XReg put_in_xreg)
;; Construct an `InstOutput` out of a single XReg register.
(decl output_xreg (XReg) InstOutput)
(rule (output_xreg x) (output_reg x))
(convert XReg InstOutput output_xreg)
;; Convert a `WritableXReg` to an `XReg`.
(decl pure writable_xreg_to_xreg (WritableXReg) XReg)
(extern constructor writable_xreg_to_xreg writable_xreg_to_xreg)
(convert WritableXReg XReg writable_xreg_to_xreg)
;; Convert a `WritableXReg` to an `WritableReg`.
(decl pure writable_xreg_to_writable_reg (WritableXReg) WritableReg)
(extern constructor writable_xreg_to_writable_reg writable_xreg_to_writable_reg)
(convert WritableXReg WritableReg writable_xreg_to_writable_reg)
;; Convert a `WritableXReg` to an `Reg`.
(decl pure writable_xreg_to_reg (WritableXReg) Reg)
(rule (writable_xreg_to_reg x) (writable_xreg_to_writable_reg x))
(convert WritableXReg Reg writable_xreg_to_reg)
;; Convert an `XReg` to a `Reg`.
(decl pure xreg_to_reg (XReg) Reg)
(extern constructor xreg_to_reg xreg_to_reg)
(convert XReg Reg xreg_to_reg)
;; Convert a `XReg` to a `ValueRegs`.
(decl xreg_to_value_regs (XReg) ValueRegs)
(rule (xreg_to_value_regs x) (value_reg x))
(convert XReg ValueRegs xreg_to_reg)
;; Convert a `WritableXReg` to a `ValueRegs`.
(decl writable_xreg_to_value_regs (WritableXReg) ValueRegs)
(rule (writable_xreg_to_value_regs x) (value_reg x))
(convert WritableXReg ValueRegs writable_xreg_to_value_regs)
;; Allocates a new `WritableXReg`.
(decl temp_writable_xreg () WritableXReg)
(rule (temp_writable_xreg) (temp_writable_reg $I64))
;; Converters
(convert u8 i32 u8_as_i32)
(decl u8_as_i32 (u8) i32)
(extern constructor u8_as_i32 u8_as_i32)
;;;; Instruction Helpers ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(decl zk_add (Imm32 Imm32) XReg)
(rule (zk_add imm1 imm2)
(let ((dst WritableXReg (temp_writable_xreg))
(_ Unit (emit (MInst.AddImm32 dst imm1 imm2))))
dst))
;; Helper for emitting the `add` instruction.
;; rd ← rs1 + rs2
(decl rv_add (XReg XReg) XReg)
(rule (rv_add rs1 rs2)
(alu_rrr (AluOPRRR.Add) rs1 rs2))
;; Helper for emitting the `sub` instruction.
;; rd ← rs1 - rs2
(decl rv_sub (XReg XReg) XReg)
(rule (rv_sub rs1 rs2)
(alu_rrr (AluOPRRR.Sub) rs1 rs2))
;; Helper for emitting the `sll` ("Shift Left Logical") instruction.
;; rd ← rs1 << rs2
(decl rv_sll (XReg XReg) XReg)
(rule (rv_sll rs1 rs2)
(alu_rrr (AluOPRRR.Sll) rs1 rs2))
;; Helper for emitting the `srl` ("Shift Right Logical") instruction.
;; rd ← rs1 >> rs2
(decl rv_srl (XReg XReg) XReg)
(rule (rv_srl rs1 rs2)
(alu_rrr (AluOPRRR.Srl) rs1 rs2))
;; Helper for emitting the `sra` ("Shift Right Arithmetic") instruction.
;; rd ← rs1 >> rs2
(decl rv_sra (XReg XReg) XReg)
(rule (rv_sra rs1 rs2)
(alu_rrr (AluOPRRR.Sra) rs1 rs2))
;; Helper for emitting the `or` instruction.
;; rd ← rs1 ∨ rs2
(decl rv_or (XReg XReg) XReg)
(rule (rv_or rs1 rs2)
(alu_rrr (AluOPRRR.Or) rs1 rs2))
;; Helper for emitting the `xor` instruction.
;; rd ← rs1 ⊕ rs2
(decl rv_xor (XReg XReg) XReg)
(rule (rv_xor rs1 rs2)
(alu_rrr (AluOPRRR.Xor) rs1 rs2))
;; Helper for emitting the `and` instruction.
;; rd ← rs1 ∧ rs2
(decl rv_and (XReg XReg) XReg)
(rule (rv_and rs1 rs2)
(alu_rrr (AluOPRRR.And) rs1 rs2))
;; Helper for emitting the `sltu` ("Set Less Than Unsigned") instruction.
;; rd ← rs1 < rs2
(decl rv_sltu (XReg XReg) XReg)
(rule (rv_sltu rs1 rs2)
(alu_rrr (AluOPRRR.SltU) rs1 rs2))
;; Helper for emitting the `snez` instruction.
;; This instruction is a mnemonic for `sltu rd, zero, rs`.
(decl rv_snez (XReg) XReg)
(rule (rv_snez rs1)
(rv_sltu (zero_reg) rs1))
;; RV64I Base Integer Instruction Set
;; Unlike RV32I instructions these are only present in the 64bit ISA
;; Helper for emitting the `addw` ("Add Word") instruction.
;; rd ← sext32(rs1) + sext32(rs2)
(decl rv_addw (XReg XReg) XReg)
(rule (rv_addw rs1 rs2)
(alu_rrr (AluOPRRR.Addw) rs1 rs2))
;; Helper for emitting the `subw` ("Subtract Word") instruction.
;; rd ← sext32(rs1) - sext32(rs2)
(decl rv_subw (XReg XReg) XReg)
(rule (rv_subw rs1 rs2)
(alu_rrr (AluOPRRR.Subw) rs1 rs2))
;; Helper for emitting the `sllw` ("Shift Left Logical Word") instruction.
;; rd ← sext32(uext32(rs1) << rs2)
(decl rv_sllw (XReg XReg) XReg)
(rule (rv_sllw rs1 rs2)
(alu_rrr (AluOPRRR.Sllw) rs1 rs2))
;; Helper for emitting the `srlw` ("Shift Right Logical Word") instruction.
;; rd ← sext32(uext32(rs1) >> rs2)
(decl rv_srlw (XReg XReg) XReg)
(rule (rv_srlw rs1 rs2)
(alu_rrr (AluOPRRR.Srlw) rs1 rs2))
;; Helper for emitting the `sraw` ("Shift Right Arithmetic Word") instruction.
;; rd ← sext32(rs1 >> rs2)
(decl rv_sraw (XReg XReg) XReg)
(rule (rv_sraw rs1 rs2)
(alu_rrr (AluOPRRR.Sraw) rs1 rs2))
;; RV32M Extension
;; TODO: Enable these instructions only when we have the M extension
(decl zk_shr_u (XReg XReg) XReg)
(rule (zk_shr_u rs1 rs2)
(let ((dst WritableXReg (temp_writable_xreg))
(_ Unit (emit (MInst.Shru64 dst rs1 rs2))))
dst))
(decl zk_shl (XReg XReg) XReg)
(rule (zk_shl rs1 rs2)
(let ((dst WritableXReg (temp_writable_xreg))
(_ Unit (emit (MInst.Shl64 dst rs1 rs2))))
dst))
;; Helper for emitting the `mul` instruction.
;; rd ← rs1 × rs2
(decl zk_mul (XReg XReg) XReg)
(rule (zk_mul rs1 rs2)
(let ((dst WritableXReg (temp_writable_xreg))
(_ Unit (emit (MInst.MulArith dst rs1 rs2))))
dst))
(decl zk_divu (XReg XReg) XReg)
(rule (zk_divu rs1 rs2)
(let ((dst WritableXReg (temp_writable_xreg))
(_ Unit (emit (MInst.UDivArith dst rs1 rs2))))
dst))
;; Helper for emitting the `mulh` ("Multiply High Signed Signed") instruction.
;; rd ← (sext(rs1) × sext(rs2)) » xlen
(decl rv_mulh (XReg XReg) XReg)
(rule (rv_mulh rs1 rs2)
(alu_rrr (AluOPRRR.Mulh) rs1 rs2))
;; Helper for emitting the `mulhu` ("Multiply High Unsigned Unsigned") instruction.
;; rd ← (uext(rs1) × uext(rs2)) » xlen
(decl rv_mulhu (XReg XReg) XReg)
(rule (rv_mulhu rs1 rs2)
(alu_rrr (AluOPRRR.Mulhu) rs1 rs2))
;; Helper for emitting the `div` instruction.
;; rd ← rs1 ÷ rs2
(decl zk_div (XReg XReg) XReg)
(rule (zk_div rs1 rs2)
(let ((dst WritableXReg (temp_writable_xreg))
(_ Unit (emit (MInst.DivArith dst rs1 rs2))))
dst))
;; Helper for emitting the `rem` instruction.
;; rd ← rs1 % rs2
(decl zk_rem (XReg XReg) XReg)
(rule (zk_rem rs1 rs2)
(let ((dst WritableXReg (temp_writable_xreg))
(_ Unit (emit (MInst.RemArith dst rs1 rs2))))
dst))
(decl zk_remu (XReg XReg) XReg)
(rule (zk_remu rs1 rs2)
(let ((dst WritableXReg (temp_writable_xreg))
(_ Unit (emit (MInst.URemArith dst rs1 rs2))))
dst))
;; Helper for emitting the `divu` ("Divide Unsigned") instruction.
;; rd ← rs1 ÷ rs2
(decl rv_divu (XReg XReg) XReg)
(rule (rv_divu rs1 rs2)
(alu_rrr (AluOPRRR.DivU) rs1 rs2))
;; Helper for emitting the `rem` instruction.
;; rd ← rs1 mod rs2
(decl rv_rem (XReg XReg) XReg)
(rule (rv_rem rs1 rs2)
(alu_rrr (AluOPRRR.Rem) rs1 rs2))
;; Helper for emitting the `remu` ("Remainder Unsigned") instruction.
;; rd ← rs1 mod rs2
(decl rv_remu (XReg XReg) XReg)
(rule (rv_remu rs1 rs2)
(alu_rrr (AluOPRRR.RemU) rs1 rs2))
;; RV64M Extension
;; TODO: Enable these instructions only when we have the M extension
;; Helper for emitting the `divuw` ("Divide Unsigned Word") instruction.
;; rd ← uext32(rs1) ÷ uext32(rs2)
(decl rv_divuw (XReg XReg) XReg)
(rule (rv_divuw rs1 rs2)
(alu_rrr (AluOPRRR.Divuw) rs1 rs2))
;; Helper for emitting the `remw` ("Remainder Word") instruction.
;; rd ← sext32(rs1) mod sext32(rs2)
(decl rv_remw (XReg XReg) XReg)
(rule (rv_remw rs1 rs2)
(alu_rrr (AluOPRRR.Remw) rs1 rs2))
;; Helper for emitting the `remuw` ("Remainder Unsigned Word") instruction.
;; rd ← uext32(rs1) mod uext32(rs2)
(decl rv_remuw (XReg XReg) XReg)
(rule (rv_remuw rs1 rs2)
(alu_rrr (AluOPRRR.Remuw) rs1 rs2))
;; `Zba` Extension Instructions
;; Helper for emitting the `adduw` ("Add Unsigned Word") instruction.
;; rd ← uext32(rs1) + uext32(rs2)
(decl rv_adduw (XReg XReg) XReg)
(rule (rv_adduw rs1 rs2)
(alu_rrr (AluOPRRR.Adduw) rs1 rs2))
;; Helper for emitting the `zext.w` ("Zero Extend Word") instruction.
;; This instruction is a mnemonic for `adduw rd, rs1, zero`.
;; rd ← uext32(rs1)
(decl rv_zextw (XReg) XReg)
(rule (rv_zextw rs1)
(rv_adduw rs1 (zero_reg)))
;; `Zbb` Extension Instructions
;; Helper for emitting the `andn` ("And Negated") instruction.
;; rd ← rs1 ∧ ~(rs2)
(decl rv_andn (XReg XReg) XReg)
(rule (rv_andn rs1 rs2)
(alu_rrr (AluOPRRR.Andn) rs1 rs2))
;; Helper for emitting the `orn` ("Or Negated") instruction.
;; rd ← rs1 ∨ ~(rs2)
(decl rv_orn (XReg XReg) XReg)
(rule (rv_orn rs1 rs2)
(alu_rrr (AluOPRRR.Orn) rs1 rs2))
;; Helper for emitting the `max` instruction.
(decl rv_max (XReg XReg) XReg)
(rule (rv_max rs1 rs2)
(alu_rrr (AluOPRRR.Max) rs1 rs2))
;; Helper for emitting the `rol` ("Rotate Left") instruction.
(decl rv_rol (XReg XReg) XReg)
(rule (rv_rol rs1 rs2)
(alu_rrr (AluOPRRR.Rol) rs1 rs2))
;; Helper for emitting the `rolw` ("Rotate Left Word") instruction.
(decl rv_rolw (XReg XReg) XReg)
(rule (rv_rolw rs1 rs2)
(alu_rrr (AluOPRRR.Rolw) rs1 rs2))
;; Helper for emitting the `ror` ("Rotate Right") instruction.
(decl rv_ror (XReg XReg) XReg)
(rule (rv_ror rs1 rs2)
(alu_rrr (AluOPRRR.Ror) rs1 rs2))
;; Helper for emitting the `rorw` ("Rotate Right Word") instruction.
(decl rv_rorw (XReg XReg) XReg)
(rule (rv_rorw rs1 rs2)
(alu_rrr (AluOPRRR.Rorw) rs1 rs2))
;; `Zbkb` Extension Instructions
;; Helper for emitting the `pack` ("Pack low halves of registers") instruction.
(decl rv_pack (XReg XReg) XReg)
(rule (rv_pack rs1 rs2)
(alu_rrr (AluOPRRR.Pack) rs1 rs2))
;; Helper for emitting the `packw` ("Pack low 16-bits of registers") instruction.
(decl rv_packw (XReg XReg) XReg)
(rule (rv_packw rs1 rs2)
(alu_rrr (AluOPRRR.Packw) rs1 rs2))
;; Generate a mask for the bit-width of the given type
(decl pure shift_mask (Type) u64)
(rule (shift_mask ty) (u64_sub (ty_bits (lane_type ty)) 1))
;; for load immediate
(decl imm (Type u64) Reg)
(extern constructor imm imm)
;; Imm12 Rules
; (decl load_imm12 (i32) Reg)
; (rule
; (load_imm12 x)
; (rv_addi (zero_reg) (imm12_const x)))
;; Imm12 Extractors
;; Helper to go directly from a `Value`, when it's an `iconst`, to an `Imm12`.
(decl imm32_from_value (Imm32) Value)
(extractor
(imm32_from_value n)
(def_inst (iconst (u64_from_imm64 (imm32_from_u64 n)))))
(decl imm32_from_u64 (Imm32) u64)
(extern extractor imm32_from_u64 imm32_from_u64)
(decl pure partial u64_to_imm32 (u64) Imm32)
(rule (u64_to_imm32 (imm32_from_u64 n)) n)
;; Imm5 Extractors
(decl imm5_from_u64 (Imm5) u64)
(extern extractor imm5_from_u64 imm5_from_u64)
;; Construct a Imm5 from an i8
(decl pure partial imm5_from_i8 (i8) Imm5)
(extern constructor imm5_from_i8 imm5_from_i8)
;; Extractor that matches a `Value` equivalent to a replicated Imm5 on all lanes.
;; TODO(#6527): Try matching vconst here as well
(decl replicated_imm5 (Imm5) Value)
(extractor (replicated_imm5 n)
(def_inst (splat (iconst (u64_from_imm64 (imm5_from_u64 n))))))
;; UImm5 Helpers
;; Extractor that matches a `Value` equivalent to a replicated UImm5 on all lanes.
;; TODO(#6527): Try matching vconst here as well
(decl replicated_uimm5 (UImm5) Value)
(extractor (replicated_uimm5 n)
(def_inst (splat (uimm5_from_value n))))
;; Helper to go directly from a `Value`, when it's an `iconst`, to an `UImm5`.
(decl uimm5_from_value (UImm5) Value)
(extractor (uimm5_from_value n)
(iconst (u64_from_imm64 (uimm5_from_u64 n))))
;; Extract a `UImm5` from an `u8`.
(decl pure partial uimm5_from_u8 (UImm5) u8)
(extern extractor uimm5_from_u8 uimm5_from_u8)
;; Extract a `UImm5` from an `u64`.
(decl pure partial uimm5_from_u64 (UImm5) u64)
(extern extractor uimm5_from_u64 uimm5_from_u64)
;; Convert a `u64` into an `UImm5`
(decl pure partial u64_to_uimm5 (u64) UImm5)
(rule (u64_to_uimm5 (uimm5_from_u64 n)) n)
(decl uimm5_bitcast_to_imm5 (UImm5) Imm5)
(extern constructor uimm5_bitcast_to_imm5 uimm5_bitcast_to_imm5)
;; Float Helpers
;; Returns the bitpattern of the Canonical NaN for the given type.
(decl pure canonical_nan_u64 (Type) u64)
(rule (canonical_nan_u64 $F32) 0x7fc00000)
(rule (canonical_nan_u64 $F64) 0x7ff8000000000000)
;; Helper for emitting `MInst.AluRRR` instructions.
(decl alu_rrr (AluOPRRR Reg Reg) Reg)
(rule (alu_rrr op src1 src2)
(let ((dst WritableXReg (temp_writable_xreg))
(_ Unit (emit (MInst.AluRRR op dst src1 src2))))
dst))
(decl select_addi (Type) AluOPRRI)
(rule 1 (select_addi (fits_in_32 ty)) (AluOPRRI.Addiw))
(rule (select_addi (fits_in_64 ty)) (AluOPRRI.Addi))
(decl gen_and (Type ValueRegs ValueRegs) ValueRegs)
(rule 0 (gen_and (fits_in_64 _) x y)
(rv_and (value_regs_get x 0) (value_regs_get y 0)))
(decl gen_andi (XReg u64) XReg)
(rule 0 (gen_andi x y)
(rv_and x (imm $I64 y)))
(decl gen_or (Type ValueRegs ValueRegs) ValueRegs)
(rule 0 (gen_or (fits_in_64 _) x y)
(rv_or (value_regs_get x 0) (value_regs_get y 0)))
(decl gen_bswap (Type XReg) XReg)
;; This is only here to make the rule below work. bswap.i8 isn't valid
(rule 0 (gen_bswap $I8 x) x)
; (rule 1 (gen_bswap (ty_int_ref_16_to_64 ty) x)
; (if-let half_ty (ty_half_width ty))
; (if-let half_size (u64_to_imm12 (ty_bits half_ty)))
; (let (;; This swaps the top bytes and zeroes the bottom bytes, so that
; ;; we can or it with the bottom bytes later.
; (swap_top XReg (gen_bswap half_ty x))
; (top XReg (rv_slli swap_top half_size))
;
; ;; Get the top half, swap it, and zero extend it so we can `or` it
; ;; with the bottom half.
; (shifted XReg (rv_srli x half_size))
; (swap_bot XReg (gen_bswap half_ty shifted))
; (bot XReg (zext swap_bot half_ty $I64)))
; (rv_or top bot)))
(decl lower_ctz (Type Reg) Reg)
(rule (lower_ctz ty x)
(gen_cltz $false x ty))
(decl lower_clz (Type XReg) XReg)
(rule (lower_clz ty rs)
(gen_cltz $true rs ty))
(decl gen_cltz (bool XReg Type) XReg)
(rule (gen_cltz leading rs ty)
(let ((tmp WritableXReg (temp_writable_xreg))
(step WritableXReg (temp_writable_xreg))
(sum WritableXReg (temp_writable_xreg))
(_ Unit (emit (MInst.Cltz leading sum step tmp rs ty))))
sum))
;; Extends an integer if it is smaller than 64 bits.
(decl ext_int_if_need (bool ValueRegs Type) ValueRegs)
;;; For values smaller than 64 bits, we need to extend them to 64 bits
(rule 0 (ext_int_if_need $true val (fits_in_32 (ty_int ty)))
(extend val (ExtendOp.Signed) ty $I64))
(rule 0 (ext_int_if_need $false val (fits_in_32 (ty_int ty)))
(extend val (ExtendOp.Zero) ty $I64))