summaryrefslogtreecommitdiff
path: root/yjit/src/asm/arm64/inst/mov.rs
blob: 0d68ffd206ac8c12b8211957e15cec596bce4fe0 (plain)
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
use super::sf::Sf;

/// Which operation is being performed.
enum Op {
    /// A movz operation which zeroes out the other bits.
    MOVZ = 0b10,

    /// A movk operation which keeps the other bits in place.
    MOVK = 0b11
}

/// How much to shift the immediate by.
enum Hw {
    LSL0 = 0b00,
    LSL16 = 0b01,
    LSL32 = 0b10,
    LSL48 = 0b11
}

impl From<u8> for Hw {
    fn from(shift: u8) -> Self {
        match shift {
            0 => Hw::LSL0,
            16 => Hw::LSL16,
            32 => Hw::LSL32,
            48 => Hw::LSL48,
            _ => panic!("Invalid value for shift: {}", shift)
        }
    }
}

/// The struct that represents a MOVK or MOVZ instruction.
///
/// +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
/// | 31 30 29 28 | 27 26 25 24 | 23 22 21 20 | 19 18 17 16 | 15 14 13 12 | 11 10 09 08 | 07 06 05 04 | 03 02 01 00 |
/// |           1    0  0  1  0    1                                                                                |
/// | sf op...                       hw... imm16.................................................. rd.............. |
/// +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
///
pub struct Mov {
    /// The register number of the destination register.
    rd: u8,

    /// The value to move into the register.
    imm16: u16,

    /// The shift of the value to move.
    hw: Hw,

    /// Which operation is being performed.
    op: Op,

    /// Whether or not this instruction is operating on 64-bit operands.
    sf: Sf
}

impl Mov {
    /// MOVK
    /// https://developer.arm.com/documentation/ddi0602/2022-03/Base-Instructions/MOVK--Move-wide-with-keep-?lang=en
    pub fn movk(rd: u8, imm16: u16, hw: u8, num_bits: u8) -> Self {
        Self { rd, imm16, hw: hw.into(), op: Op::MOVK, sf: num_bits.into() }
    }

    /// MOVZ
    /// https://developer.arm.com/documentation/ddi0602/2022-03/Base-Instructions/MOVZ--Move-wide-with-zero-?lang=en
    pub fn movz(rd: u8, imm16: u16, hw: u8, num_bits: u8) -> Self {
        Self { rd, imm16, hw: hw.into(), op: Op::MOVZ, sf: num_bits.into() }
    }
}

/// https://developer.arm.com/documentation/ddi0602/2022-03/Index-by-Encoding/Data-Processing----Immediate?lang=en
const FAMILY: u32 = 0b1000;

impl From<Mov> for u32 {
    /// Convert an instruction into a 32-bit value.
    fn from(inst: Mov) -> Self {
        0
        | ((inst.sf as u32) << 31)
        | ((inst.op as u32) << 29)
        | (FAMILY << 25)
        | (0b101 << 23)
        | ((inst.hw as u32) << 21)
        | ((inst.imm16 as u32) << 5)
        | inst.rd as u32
    }
}

impl From<Mov> for [u8; 4] {
    /// Convert an instruction into a 4 byte array.
    fn from(inst: Mov) -> [u8; 4] {
        let result: u32 = inst.into();
        result.to_le_bytes()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_movk_unshifted() {
        let inst = Mov::movk(0, 123, 0, 64);
        let result: u32 = inst.into();
        assert_eq!(0xf2800f60, result);
    }

    #[test]
    fn test_movk_shifted_16() {
        let inst = Mov::movk(0, 123, 16, 64);
        let result: u32 = inst.into();
        assert_eq!(0xf2A00f60, result);
    }

    #[test]
    fn test_movk_shifted_32() {
        let inst = Mov::movk(0, 123, 32, 64);
        let result: u32 = inst.into();
        assert_eq!(0xf2C00f60, result);
    }

    #[test]
    fn test_movk_shifted_48() {
        let inst = Mov::movk(0, 123, 48, 64);
        let result: u32 = inst.into();
        assert_eq!(0xf2e00f60, result);
    }

    #[test]
    fn test_movz_unshifted() {
        let inst = Mov::movz(0, 123, 0, 64);
        let result: u32 = inst.into();
        assert_eq!(0xd2800f60, result);
    }

    #[test]
    fn test_movz_shifted_16() {
        let inst = Mov::movz(0, 123, 16, 64);
        let result: u32 = inst.into();
        assert_eq!(0xd2a00f60, result);
    }

    #[test]
    fn test_movz_shifted_32() {
        let inst = Mov::movz(0, 123, 32, 64);
        let result: u32 = inst.into();
        assert_eq!(0xd2c00f60, result);
    }

    #[test]
    fn test_movz_shifted_48() {
        let inst = Mov::movz(0, 123, 48, 64);
        let result: u32 = inst.into();
        assert_eq!(0xd2e00f60, result);
    }
}