1use super::*;
2
3#[derive(Clone, PartialEq, Eq, Debug)]
4pub struct Alu3 {
5 pub dst: Op,
6 pub lhs: Op,
7 pub rhs: Op,
8}
9
10impl Alu3 {
11 pub fn from_operands(mn: Mnemonic, ops: &[Op]) -> Result<Self, ExpandError> {
19 match ops {
20 [dst, rhs] => Ok(Alu3 {
21 dst: *dst,
22 lhs: *dst,
23 rhs: *rhs,
24 }),
25 [dst, lhs, rhs] => Ok(Alu3 {
26 dst: *dst,
27 lhs: *lhs,
28 rhs: *rhs,
29 }),
30 _ => Err(bad(mn, "takes 2 or 3 operands")),
31 }
32 }
33}
34
35#[derive(Clone, Copy, PartialEq, Eq, Debug)]
41pub struct BranchArgs {
42 ops: [Op; 3],
43 len: u8,
44}
45
46impl BranchArgs {
47 pub fn two(rhs: Op, dst: Op) -> Self {
49 Self {
50 ops: [rhs, dst, Op::Imm(0)],
51 len: 2,
52 }
53 }
54
55 pub fn three(lhs: Op, rhs: Op, dst: Op) -> Self {
57 Self {
58 ops: [lhs, rhs, dst],
59 len: 3,
60 }
61 }
62
63 pub fn as_slice(&self) -> &[Op] {
65 &self.ops[..self.len as usize]
66 }
67
68 pub fn as_mut_slice(&mut self) -> &mut [Op] {
70 &mut self.ops[..self.len as usize]
71 }
72}
73
74#[derive(Clone, PartialEq, Eq, Debug)]
75pub enum Inst {
76 Mov {
77 w: Width,
78 dst: Op,
79 src: Op,
80 patch: bool,
81 },
82 Bind(Lbl),
83 MovZero32 {
84 dst: Op,
85 src: Op,
86 },
87 Bswap {
88 w: Width,
89 dst: Op,
90 src: Op,
91 },
92 Load {
93 w: Width,
94 sx: Sx,
95 dst: Gpr,
96 addr: Mem,
97 acquire: bool,
98 },
99 Store {
100 w: Width,
101 src: Op,
102 addr: Mem,
103 },
104 LoadF {
105 k: FKind,
106 dst: Fpr,
107 addr: Mem,
108 },
109 StoreF {
110 k: FKind,
111 src: Fpr,
112 addr: Mem,
113 },
114 FMov {
115 k: FKind,
116 dst: Fpr,
117 src: Fpr,
118 },
119 LoadV {
120 dst: Fpr,
121 addr: Mem,
122 },
123 StoreV {
124 src: Fpr,
125 addr: Mem,
126 },
127 VMov {
128 dst: Fpr,
129 src: Fpr,
130 },
131 Lea {
132 w: Width,
133 dst: Gpr,
134 src: LeaSrc,
135 },
136 Push {
137 op: Op,
138 },
139 Pop {
140 op: Op,
141 },
142 PushV {
143 src: Fpr,
144 },
145 PopV {
146 dst: Fpr,
147 },
148 Peek {
149 disp: i32,
150 dst: Gpr,
151 },
152 Poke {
153 disp: i32,
154 src: Gpr,
155 },
156
157 Add {
158 w: Width,
159 op: Alu3,
160 },
161 Sub {
162 w: Width,
163 op: Alu3,
164 },
165 Mul {
166 w: Width,
167 op: Alu3,
168 },
169 And {
170 w: Width,
171 op: Alu3,
172 },
173 Or {
174 w: Width,
175 op: Alu3,
176 },
177 Xor {
178 w: Width,
179 op: Alu3,
180 },
181 Not {
182 w: Width,
183 dst: Op,
184 },
185 Neg {
186 w: Width,
187 dst: Op,
188 },
189 Shift {
190 w: Width,
191 op: ShiftOp,
192 dst: Gpr,
193 cnt: ShiftCnt,
194 },
195 MulImm {
196 w: Width,
197 dst: Gpr,
198 lhs: Gpr,
199 imm: i64,
200 },
201 AluS {
202 op: AluOp,
203 w: Width,
204 alu: Alu3,
205 },
206 Adc {
207 dst: Op,
208 src: Op,
209 },
210 Sbb {
211 dst: Op,
212 src: Op,
213 },
214 MulWide {
215 signed: bool,
216 dst: Gpr,
217 lhs: Gpr,
218 rhs: Gpr,
219 },
220 MulHigh {
221 signed: bool,
222 rhs: Op,
223 },
224 DivMod1 {
225 signed: bool,
226 w: Width,
227 divisor: Op,
228 },
229 Div3 {
230 signed: bool,
231 w: Width,
232 dst: Gpr,
233 dvd: Op,
234 dvs: Op,
235 },
236 Rem3 {
237 signed: bool,
238 w: Width,
239 dst: Gpr,
240 dvd: Op,
241 dvs: Op,
242 },
243 Extend {
244 from: Width,
245 to: Width,
246 sx: Sx,
247 dst: Gpr,
248 src: Op,
249 },
250 Ctz {
251 w: Width,
252 dst: Gpr,
253 src: Op,
254 },
255 Clz {
256 w: Width,
257 dst: Gpr,
258 src: Op,
259 },
260 Popcnt {
261 w: Width,
262 dst: Gpr,
263 src: Op,
264 },
265 Cqo {
266 quad: bool,
267 },
268 AddShifted {
269 dst: Gpr,
270 lhs: Gpr,
271 rhs: Gpr,
272 amount: u8,
273 },
274 Cmov {
275 w: Width,
276 zero: bool,
277 dst: Gpr,
278 src: Op,
279 test: Gpr,
280 },
281 VecExtract {
282 w: Width,
283 dst: Gpr,
284 src: Fpr,
285 lane: u8,
286 },
287
288 Branch {
289 cc: IntCc,
290 w: Width,
291 lhs: Op,
292 rhs: Op,
293 target: Lbl,
294 patch: bool,
295 },
296
297 TestBranch {
298 w: Width,
299 value: Op,
300 mask: Option<Op>,
301 test: TestKind,
302 target: Lbl,
303 patch: bool,
304 },
305
306 OpBranch {
307 op: FlagAlu,
308 w: Width,
309 flag: PredFlag,
310 args: BranchArgs,
311 target: Lbl,
312 patch: bool,
313 },
314
315 FlagBranch {
316 flag: PredFlag,
317 target: Lbl,
318 patch: bool,
319 },
320 Jump {
321 target: Target,
322 patch: bool,
323 },
324 Call {
325 target: Target,
326 patch: bool,
327 },
328 Ret,
329 Break,
331 Nop,
332
333 IntSet {
335 cc: IntCc,
336 w: Width,
337 dst: Op,
338 lhs: Op,
339 rhs: Op,
340 },
341 TestSet {
343 w: Width,
344 value: Op,
345 mask: Option<Op>,
346 test: TestKind,
347 dst: Op,
348 },
349
350 FArith {
352 op: FAlu,
353 k: FKind,
354 arith: FArith3,
355 },
356 Fma {
358 op: FmaOp,
359 k: FKind,
360 a: Op,
361 b: Op,
362 c: Op,
363 dst: Fpr,
364 },
365 FSqrt {
366 k: FKind,
367 dst: Fpr,
368 src: Op,
369 },
370 FRound {
371 k: FKind,
372 mode: RoundMode,
373 dst: Fpr,
374 src: Op,
375 },
376 FAbs {
377 k: FKind,
378 dst: Fpr,
379 src: Op,
380 },
381 FNeg {
382 k: FKind,
383 dst: Fpr,
384 src: Op,
385 },
386 FBit {
388 or: bool,
389 k: FKind,
390 dst: Fpr,
391 src: Op,
392 },
393 CvtFI {
397 k: FKind,
398 w: Width,
399 dst: Gpr,
400 src: Op,
401 },
402 CvtIF {
404 signed: bool,
405 from: Width,
406 k: FKind,
407 dst: Fpr,
408 src: Op,
409 },
410 CvtFF {
412 from: FKind,
413 to: FKind,
414 dst: Fpr,
415 src: Op,
416 },
417 Bitcast {
419 dir: BitcastDir,
420 dst: Op,
421 src: Op,
422 },
423 BranchF {
425 cc: FCc,
426 k: FKind,
427 lhs: Op,
428 rhs: Op,
429 target: Lbl,
430 patch: bool,
431 },
432 FSet {
434 cc: FCc,
435 k: FKind,
436 dst: Op,
437 lhs: Op,
438 rhs: Op,
439 },
440
441 Fence {
443 full: bool,
444 },
445 Pause,
447 TlsLoad {
452 w: Width,
453 dst: Gpr,
454 idx: i64,
455 },
456 TlsStore {
461 w: Width,
462 src: Gpr,
463 idx: i64,
464 },
465 TlsLea {
467 dst: Gpr,
468 idx: i64,
469 },
470 AtomicRmw {
472 op: AtomicOp,
473 w: Width,
474 a: Op,
475 b: Op,
476 },
477 AtomicCas {
481 w: Width,
482 mem: Mem,
483 src: Gpr,
484 branch: Option<Lbl>,
485 },
486 LoadLinked {
488 w: Width,
489 mem: Mem,
490 dst: Gpr,
491 },
492 StoreCond {
494 w: Width,
495 status: Gpr,
496 src: Gpr,
497 mem: Mem,
498 },
499 PairLoad {
501 w: PairW,
502 dst1: Op,
503 dst2: Op,
504 mem: Mem,
505 },
506 PairStore {
508 w: PairW,
509 src1: Op,
510 src2: Op,
511 mem: Mem,
512 },
513}
514
515#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
517pub enum Sx {
518 Zero,
519 Sign,
520}
521
522#[derive(Clone, PartialEq, Eq, Debug)]
524pub enum ShiftCnt {
525 Imm(i64),
526 Reg(Gpr),
527}
528
529#[derive(Clone, PartialEq, Eq, Debug)]
531pub enum LeaSrc {
532 Mem(Mem),
533 Label(Lbl),
534 Extern(Extern),
535}
536
537#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
539pub enum FlagAlu {
540 Add,
541 Sub,
542 Mul,
543 Or,
544}
545
546#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
548pub enum AluOp {
549 Add,
550 Sub,
551 Or,
552}
553
554#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
556pub enum FAlu {
557 Add,
558 Sub,
559 Mul,
560 Div,
561 Min,
562 Max,
563}
564
565impl FAlu {
566 pub const fn is_commutative(self) -> bool {
568 matches!(self, FAlu::Add | FAlu::Mul | FAlu::Min | FAlu::Max)
569 }
570}
571
572#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
574pub enum FmaOp {
575 Add,
577 Sub,
579 NegAdd,
581 NegSub,
583}
584
585#[derive(Clone, PartialEq, Eq, Debug)]
586pub struct FArith3 {
587 pub dst: Fpr,
588 pub lhs: Op,
589 pub rhs: Option<Op>,
590}
591
592impl FArith3 {
593 pub fn from_operands(mn: Mnemonic, ops: &[Op]) -> Result<Self, ExpandError> {
601 match ops {
602 [dst, lhs] => Ok(FArith3 {
603 dst: fpr_op(mn, dst)?,
604 lhs: *lhs,
605 rhs: None,
606 }),
607 _ => Err(bad(mn, "takes 2 operands")),
608 }
609 }
610}
611
612#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
614pub enum BitcastDir {
615 Q2D,
616 D2Q,
617 I2F,
618 F2I,
619}
620
621#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
623pub enum AtomicOp {
624 Add,
625 Sub,
626 Xchg,
627 And,
628 Or,
629 Xor,
630}
631
632#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
634pub enum PairW {
635 B32,
636 B64,
637 F64,
638 V128,
639}
640
641pub fn bad(mn: Mnemonic, msg: impl Into<String>) -> ExpandError {
645 ExpandError::BadOperand {
646 mnemonic: mn.as_str().to_string(),
647 msg: msg.into(),
648 }
649}
650
651pub fn expect_len(mn: Mnemonic, ops: &[Op], n: usize) -> Result<(), ExpandError> {
657 if ops.len() == n {
658 Ok(())
659 } else {
660 Err(bad(
661 mn,
662 format!("expected {} operands, got {}", n, ops.len()),
663 ))
664 }
665}
666
667pub fn imm_op(mn: Mnemonic, op: &Op) -> Result<i64, ExpandError> {
673 match op {
674 Op::Imm(v) => Ok(*v),
675 _ => Err(bad(mn, "expected immediate")),
676 }
677}
678
679pub fn gpr_op(mn: Mnemonic, op: &Op) -> Result<Gpr, ExpandError> {
685 match op {
686 Op::Gpr(g) => Ok(*g),
687 _ => Err(bad(mn, "expected GPR")),
688 }
689}
690
691pub fn fpr_op(mn: Mnemonic, op: &Op) -> Result<Fpr, ExpandError> {
697 match op {
698 Op::Fpr(x) => Ok(*x),
699 _ => Err(bad(mn, "expected FPR")),
700 }
701}
702
703pub fn mem_op(mn: Mnemonic, op: &Op) -> Result<Mem, ExpandError> {
709 match op {
710 Op::Mem(a) => Ok(*a),
711 _ => Err(bad(mn, "expected memory")),
712 }
713}
714
715pub fn label_op(mn: Mnemonic, op: &Op) -> Result<Lbl, ExpandError> {
721 match op {
722 Op::Label(l) => Ok(*l),
723 _ => Err(bad(mn, "expected label")),
724 }
725}
726
727pub fn op_eq(a: &Op, b: &Op) -> bool {
732 match (a, b) {
733 (Op::Imm(x), Op::Imm(y)) => x == y,
734 (Op::Gpr(x), Op::Gpr(y)) => x == y,
735 (Op::Fpr(x), Op::Fpr(y)) => x == y,
736 _ => false,
737 }
738}
739
740impl Inst {
741 pub fn visit_labels(&self, f: &mut dyn FnMut(Lbl)) {
747 fn visit_op(f: &mut dyn FnMut(Lbl), o: &Op) {
748 if let Op::Label(l) = o {
749 f(*l);
750 }
751 }
752 fn visit_target(f: &mut dyn FnMut(Lbl), t: &Target) {
753 if let Target::Label(l) = t {
754 f(*l);
755 }
756 }
757 fn visit_lea(f: &mut dyn FnMut(Lbl), s: &LeaSrc) {
758 if let LeaSrc::Label(l) = s {
759 f(*l);
760 }
761 }
762 fn visit_ops(f: &mut dyn FnMut(Lbl), ops: &[Op]) {
763 for o in ops {
764 visit_op(f, o);
765 }
766 }
767 match self {
768 Inst::Mov { dst, src, .. }
769 | Inst::MovZero32 { dst, src }
770 | Inst::Bswap { dst, src, .. }
771 | Inst::Adc { dst, src }
772 | Inst::Sbb { dst, src }
773 | Inst::Bitcast { dst, src, .. } => {
774 visit_op(f, dst);
775 visit_op(f, src);
776 }
777 Inst::IntSet { dst, lhs, rhs, .. } | Inst::FSet { dst, lhs, rhs, .. } => {
778 visit_op(f, dst);
779 visit_op(f, lhs);
780 visit_op(f, rhs);
781 }
782 Inst::Bind(l) => f(*l),
783 Inst::Lea { src, .. } => visit_lea(f, src),
784 Inst::Store { src, .. } => visit_op(f, src),
785 Inst::Push { op } | Inst::Pop { op } => visit_op(f, op),
786 Inst::Add { op: a, .. }
787 | Inst::Sub { op: a, .. }
788 | Inst::Mul { op: a, .. }
789 | Inst::And { op: a, .. }
790 | Inst::Or { op: a, .. }
791 | Inst::Xor { op: a, .. } => {
792 visit_op(f, &a.dst);
793 visit_op(f, &a.lhs);
794 visit_op(f, &a.rhs);
795 }
796 Inst::AluS { alu, .. } => {
797 visit_op(f, &alu.dst);
798 visit_op(f, &alu.lhs);
799 visit_op(f, &alu.rhs);
800 }
801 Inst::Not { dst, .. } | Inst::Neg { dst, .. } => visit_op(f, dst),
802 Inst::MulHigh { rhs, .. } => visit_op(f, rhs),
803 Inst::DivMod1 { divisor, .. } => visit_op(f, divisor),
804 Inst::Div3 { dvd, dvs, .. } | Inst::Rem3 { dvd, dvs, .. } => {
805 visit_op(f, dvd);
806 visit_op(f, dvs);
807 }
808 Inst::Branch {
809 lhs, rhs, target, ..
810 } => {
811 visit_op(f, lhs);
812 visit_op(f, rhs);
813 f(*target);
814 }
815 Inst::TestBranch {
816 value,
817 mask,
818 target,
819 ..
820 } => {
821 visit_op(f, value);
822 if let Some(m) = mask {
823 visit_op(f, m);
824 }
825 f(*target);
826 }
827 Inst::OpBranch { args, target, .. } => {
828 visit_ops(f, args.as_slice());
829 f(*target);
830 }
831 Inst::FlagBranch { target, .. } => f(*target),
832 Inst::Jump { target, .. } | Inst::Call { target, .. } => visit_target(f, target),
833 Inst::TestSet {
834 value, mask, dst, ..
835 } => {
836 visit_op(f, value);
837 if let Some(m) = mask {
838 visit_op(f, m);
839 }
840 visit_op(f, dst);
841 }
842 Inst::FArith { arith, .. } => {
843 visit_op(f, &arith.lhs);
844 if let Some(r) = &arith.rhs {
845 visit_op(f, r);
846 }
847 }
848 Inst::Fma { a, b, c, .. } => {
849 visit_op(f, a);
850 visit_op(f, b);
851 visit_op(f, c);
852 }
853 Inst::FSqrt { src, .. }
854 | Inst::FRound { src, .. }
855 | Inst::FAbs { src, .. }
856 | Inst::FNeg { src, .. }
857 | Inst::FBit { src, .. }
858 | Inst::CvtFI { src, .. }
859 | Inst::CvtIF { src, .. }
860 | Inst::CvtFF { src, .. } => visit_op(f, src),
861 Inst::BranchF {
862 lhs, rhs, target, ..
863 } => {
864 visit_op(f, lhs);
865 visit_op(f, rhs);
866 f(*target);
867 }
868 Inst::AtomicRmw { a, b, .. } => {
869 visit_op(f, a);
870 visit_op(f, b);
871 }
872 Inst::Load { .. }
873 | Inst::LoadF { .. }
874 | Inst::StoreF { .. }
875 | Inst::FMov { .. }
876 | Inst::LoadV { .. }
877 | Inst::StoreV { .. }
878 | Inst::VMov { .. }
879 | Inst::PushV { .. }
880 | Inst::PopV { .. }
881 | Inst::Peek { .. }
882 | Inst::Poke { .. }
883 | Inst::Shift { .. }
884 | Inst::MulImm { .. }
885 | Inst::Extend { .. }
886 | Inst::Ctz { .. }
887 | Inst::Clz { .. }
888 | Inst::Popcnt { .. }
889 | Inst::Cqo { .. }
890 | Inst::MulWide { .. }
891 | Inst::AddShifted { .. }
892 | Inst::Cmov { .. }
893 | Inst::VecExtract { .. }
894 | Inst::Fence { .. }
895 | Inst::Pause
896 | Inst::TlsLoad { .. }
897 | Inst::TlsStore { .. }
898 | Inst::TlsLea { .. }
899 | Inst::AtomicCas { .. }
900 | Inst::LoadLinked { .. }
901 | Inst::StoreCond { .. }
902 | Inst::PairLoad { .. }
903 | Inst::PairStore { .. }
904 | Inst::Ret
905 | Inst::Break
906 | Inst::Nop => {}
907 }
908 }
909}