cranelift_assembler_x64/
xmm.rs

1//! Xmm register operands; see [`Xmm`].
2
3use crate::{AsReg, CodeSink, rex::encode_modrm};
4
5/// An x64 SSE register (e.g., `%xmm0`).
6#[derive(Clone, Copy, Debug)]
7pub struct Xmm<R: AsReg = u8>(pub(crate) R);
8
9impl<R: AsReg> Xmm<R> {
10    /// Create a new [`Xmm`] register.
11    pub fn new(reg: R) -> Self {
12        Self(reg)
13    }
14
15    /// Return the register's hardware encoding; the underlying type `R` _must_
16    /// be a real register at this point.
17    ///
18    /// # Panics
19    ///
20    /// Panics if the register is not a valid Xmm register.
21    pub fn enc(&self) -> u8 {
22        let enc = self.0.enc();
23        assert!(enc < 16, "invalid register: {enc}");
24        enc
25    }
26
27    /// Return the register name.
28    pub fn to_string(&self) -> String {
29        self.0.to_string(None)
30    }
31
32    /// Emit this register as the `r/m` field of a ModR/M byte.
33    pub(crate) fn encode_modrm(&self, sink: &mut impl CodeSink, enc_reg: u8) {
34        sink.put1(encode_modrm(0b11, enc_reg & 0b111, self.enc() & 0b111));
35    }
36
37    /// Return the registers for encoding the `b` and `x` bits (e.g., in a VEX
38    /// prefix).
39    ///
40    /// This is primarily used for `*Mem` variants (see
41    /// `XmmMem::encode_bx_regs`), but when used on a single `Xmm` register,
42    /// only the `b` bit is set by the topmost bit (the fourth bit) of this
43    /// register. We expect this register to be in the `rm` slot.
44    pub(crate) fn encode_bx_regs(&self) -> (Option<u8>, Option<u8>) {
45        (Some(self.enc()), None)
46    }
47}
48
49impl<R: AsReg> AsRef<R> for Xmm<R> {
50    fn as_ref(&self) -> &R {
51        &self.0
52    }
53}
54
55impl<R: AsReg> AsMut<R> for Xmm<R> {
56    fn as_mut(&mut self) -> &mut R {
57        &mut self.0
58    }
59}
60
61impl<R: AsReg> From<R> for Xmm<R> {
62    fn from(reg: R) -> Xmm<R> {
63        Xmm(reg)
64    }
65}
66
67/// Encode xmm registers.
68pub mod enc {
69    pub const XMM0: u8 = 0;
70    pub const XMM1: u8 = 1;
71    pub const XMM2: u8 = 2;
72    pub const XMM3: u8 = 3;
73    pub const XMM4: u8 = 4;
74    pub const XMM5: u8 = 5;
75    pub const XMM6: u8 = 6;
76    pub const XMM7: u8 = 7;
77    pub const XMM8: u8 = 8;
78    pub const XMM9: u8 = 9;
79    pub const XMM10: u8 = 10;
80    pub const XMM11: u8 = 11;
81    pub const XMM12: u8 = 12;
82    pub const XMM13: u8 = 13;
83    pub const XMM14: u8 = 14;
84    pub const XMM15: u8 = 15;
85
86    /// Return the name of a XMM encoding (`enc`).
87    ///
88    /// # Panics
89    ///
90    /// This function will panic if the encoding is not a valid x64 register.
91    pub fn to_string(enc: u8) -> &'static str {
92        match enc {
93            XMM0 => "%xmm0",
94            XMM1 => "%xmm1",
95            XMM2 => "%xmm2",
96            XMM3 => "%xmm3",
97            XMM4 => "%xmm4",
98            XMM5 => "%xmm5",
99            XMM6 => "%xmm6",
100            XMM7 => "%xmm7",
101            XMM8 => "%xmm8",
102            XMM9 => "%xmm9",
103            XMM10 => "%xmm10",
104            XMM11 => "%xmm11",
105            XMM12 => "%xmm12",
106            XMM13 => "%xmm13",
107            XMM14 => "%xmm14",
108            XMM15 => "%xmm15",
109            _ => panic!("%invalid{enc}"),
110        }
111    }
112}