summaryrefslogtreecommitdiff
path: root/yjit/src/asm/arm64/inst/reg_pair.rs
blob: 87690e3b4ab0e1bcb9014740960b76cdea198ecf (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
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
use super::super::arg::truncate_imm;

/// The operation to perform for this instruction.
enum Opc {
    /// When the registers are 32-bits wide.
    Opc32 = 0b00,

    /// When the registers are 64-bits wide.
    Opc64 = 0b10
}

/// The kind of indexing to perform for this instruction.
enum Index {
    StorePostIndex = 0b010,
    LoadPostIndex = 0b011,
    StoreSignedOffset = 0b100,
    LoadSignedOffset = 0b101,
    StorePreIndex = 0b110,
    LoadPreIndex = 0b111
}

/// A convenience function so that we can convert the number of bits of a
/// register operand directly into an Opc variant.
impl From<u8> for Opc {
    fn from(num_bits: u8) -> Self {
        match num_bits {
            64 => Opc::Opc64,
            32 => Opc::Opc32,
            _ => panic!("Invalid number of bits: {}", num_bits)
        }
    }
}

/// The struct that represents an A64 register pair instruction that can be
/// encoded.
///
/// STP/LDP
/// +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
/// | 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 |
/// |     0  1  0    1  0  0                                                                                        |
/// | opc                    index..... imm7.................... rt2............. rn.............. rt1............. |
/// +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
///
pub struct RegisterPair {
    /// The number of the first register to be transferred.
    rt1: u8,

    /// The number of the base register.
    rn: u8,

    /// The number of the second register to be transferred.
    rt2: u8,

    /// The signed immediate byte offset, a multiple of 8.
    imm7: i16,

    /// The kind of indexing to use for this instruction.
    index: Index,

    /// The operation to be performed (in terms of size).
    opc: Opc
}

impl RegisterPair {
    /// Create a register pair instruction with a given indexing mode.
    fn new(rt1: u8, rt2: u8, rn: u8, disp: i16, index: Index, num_bits: u8) -> Self {
        Self { rt1, rn, rt2, imm7: disp / 8, index, opc: num_bits.into() }
    }

    /// LDP (signed offset)
    /// LDP <Xt1>, <Xt2>, [<Xn|SP>{, #<imm>}]
    /// https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/LDP--Load-Pair-of-Registers-?lang=en
    pub fn ldp(rt1: u8, rt2: u8, rn: u8, disp: i16, num_bits: u8) -> Self {
        Self::new(rt1, rt2, rn, disp, Index::LoadSignedOffset, num_bits)
    }

    /// LDP (pre-index)
    /// LDP <Xt1>, <Xt2>, [<Xn|SP>, #<imm>]!
    /// https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/LDP--Load-Pair-of-Registers-?lang=en
    pub fn ldp_pre(rt1: u8, rt2: u8, rn: u8, disp: i16, num_bits: u8) -> Self {
        Self::new(rt1, rt2, rn, disp, Index::LoadPreIndex, num_bits)
    }

    /// LDP (post-index)
    /// LDP <Xt1>, <Xt2>, [<Xn|SP>], #<imm>
    /// https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/LDP--Load-Pair-of-Registers-?lang=en
    pub fn ldp_post(rt1: u8, rt2: u8, rn: u8, disp: i16, num_bits: u8) -> Self {
        Self::new(rt1, rt2, rn, disp, Index::LoadPostIndex, num_bits)
    }

    /// STP (signed offset)
    /// STP <Xt1>, <Xt2>, [<Xn|SP>{, #<imm>}]
    /// https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/STP--Store-Pair-of-Registers-?lang=en
    pub fn stp(rt1: u8, rt2: u8, rn: u8, disp: i16, num_bits: u8) -> Self {
        Self::new(rt1, rt2, rn, disp, Index::StoreSignedOffset, num_bits)
    }

    /// STP (pre-index)
    /// STP <Xt1>, <Xt2>, [<Xn|SP>, #<imm>]!
    /// https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/STP--Store-Pair-of-Registers-?lang=en
    pub fn stp_pre(rt1: u8, rt2: u8, rn: u8, disp: i16, num_bits: u8) -> Self {
        Self::new(rt1, rt2, rn, disp, Index::StorePreIndex, num_bits)
    }

    /// STP (post-index)
    /// STP <Xt1>, <Xt2>, [<Xn|SP>], #<imm>
    /// https://developer.arm.com/documentation/ddi0596/2021-12/Base-Instructions/STP--Store-Pair-of-Registers-?lang=en
    pub fn stp_post(rt1: u8, rt2: u8, rn: u8, disp: i16, num_bits: u8) -> Self {
        Self::new(rt1, rt2, rn, disp, Index::StorePostIndex, num_bits)
    }
}

/// https://developer.arm.com/documentation/ddi0602/2022-03/Index-by-Encoding/Loads-and-Stores?lang=en
const FAMILY: u32 = 0b0100;

impl From<RegisterPair> for u32 {
    /// Convert an instruction into a 32-bit value.
    fn from(inst: RegisterPair) -> Self {
        0
        | ((inst.opc as u32) << 30)
        | (1 << 29)
        | (FAMILY << 25)
        | ((inst.index as u32) << 22)
        | (truncate_imm::<_, 7>(inst.imm7) << 15)
        | ((inst.rt2 as u32) << 10)
        | ((inst.rn as u32) << 5)
        | (inst.rt1 as u32)
    }
}

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

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

    #[test]
    fn test_ldp() {
        let inst = RegisterPair::ldp(0, 1, 2, 0, 64);
        let result: u32 = inst.into();
        assert_eq!(0xa9400440, result);
    }

    #[test]
    fn test_ldp_maximum_displacement() {
        let inst = RegisterPair::ldp(0, 1, 2, 504, 64);
        let result: u32 = inst.into();
        assert_eq!(0xa95f8440, result);
    }

    #[test]
    fn test_ldp_minimum_displacement() {
        let inst = RegisterPair::ldp(0, 1, 2, -512, 64);
        let result: u32 = inst.into();
        assert_eq!(0xa9600440, result);
    }

    #[test]
    fn test_ldp_pre() {
        let inst = RegisterPair::ldp_pre(0, 1, 2, 256, 64);
        let result: u32 = inst.into();
        assert_eq!(0xa9d00440, result);
    }

    #[test]
    fn test_ldp_post() {
        let inst = RegisterPair::ldp_post(0, 1, 2, 256, 64);
        let result: u32 = inst.into();
        assert_eq!(0xa8d00440, result);
    }

    #[test]
    fn test_stp() {
        let inst = RegisterPair::stp(0, 1, 2, 0, 64);
        let result: u32 = inst.into();
        assert_eq!(0xa9000440, result);
    }

    #[test]
    fn test_stp_maximum_displacement() {
        let inst = RegisterPair::stp(0, 1, 2, 504, 64);
        let result: u32 = inst.into();
        assert_eq!(0xa91f8440, result);
    }

    #[test]
    fn test_stp_minimum_displacement() {
        let inst = RegisterPair::stp(0, 1, 2, -512, 64);
        let result: u32 = inst.into();
        assert_eq!(0xa9200440, result);
    }

    #[test]
    fn test_stp_pre() {
        let inst = RegisterPair::stp_pre(0, 1, 2, 256, 64);
        let result: u32 = inst.into();
        assert_eq!(0xa9900440, result);
    }

    #[test]
    fn test_stp_post() {
        let inst = RegisterPair::stp_post(0, 1, 2, 256, 64);
        let result: u32 = inst.into();
        assert_eq!(0xa8900440, result);
    }
}