cranelift_assembler_x64/
xmm.rs

1//! Xmm register operands; see [`Xmm`].
2
3use crate::AsReg;
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) -> &str {
29        enc::to_string(self.enc())
30    }
31}
32
33impl<R: AsReg> AsRef<R> for Xmm<R> {
34    fn as_ref(&self) -> &R {
35        &self.0
36    }
37}
38
39impl<R: AsReg> AsMut<R> for Xmm<R> {
40    fn as_mut(&mut self) -> &mut R {
41        &mut self.0
42    }
43}
44
45/// Encode xmm registers.
46pub mod enc {
47    pub const XMM0: u8 = 0;
48    pub const XMM1: u8 = 1;
49    pub const XMM2: u8 = 2;
50    pub const XMM3: u8 = 3;
51    pub const XMM4: u8 = 4;
52    pub const XMM5: u8 = 5;
53    pub const XMM6: u8 = 6;
54    pub const XMM7: u8 = 7;
55    pub const XMM8: u8 = 8;
56    pub const XMM9: u8 = 9;
57    pub const XMM10: u8 = 10;
58    pub const XMM11: u8 = 11;
59    pub const XMM12: u8 = 12;
60    pub const XMM13: u8 = 13;
61    pub const XMM14: u8 = 14;
62    pub const XMM15: u8 = 15;
63
64    /// Return the name of a XMM encoding (`enc`).
65    ///
66    /// # Panics
67    ///
68    /// This function will panic if the encoding is not a valid x64 register.
69    pub fn to_string(enc: u8) -> &'static str {
70        match enc {
71            XMM0 => "%xmm0",
72            XMM1 => "%xmm1",
73            XMM2 => "%xmm2",
74            XMM3 => "%xmm3",
75            XMM4 => "%xmm4",
76            XMM5 => "%xmm5",
77            XMM6 => "%xmm6",
78            XMM7 => "%xmm7",
79            XMM8 => "%xmm8",
80            XMM9 => "%xmm9",
81            XMM10 => "%xmm10",
82            XMM11 => "%xmm11",
83            XMM12 => "%xmm12",
84            XMM13 => "%xmm13",
85            XMM14 => "%xmm14",
86            XMM15 => "%xmm15",
87            _ => panic!("%invalid{enc}"),
88        }
89    }
90}