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

/// Whether this is a load or a store.
enum Op {
    Load = 1,
    Store = 0
}

/// The type of indexing to perform for this instruction.
enum Index {
    /// No indexing.
    None = 0b00,

    /// Mutate the register after the read.
    PostIndex = 0b01,

    /// Mutate the register before the read.
    PreIndex = 0b11
}

/// The struct that represents an A64 halfword instruction that can be encoded.
///
/// LDRH/STRH
/// +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
/// | 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  1  1    1  0  0  1    0                                                                                |
/// |                                op imm12.................................... rn.............. rt.............. |
/// +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
///
/// LDRH (pre-index/post-index)
/// +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
/// | 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  1  1    1  0  0  0    0     0                                                                          |
/// |                                op    imm9..........................   index rn.............. rt.............. |
/// +-------------+-------------+-------------+-------------+-------------+-------------+-------------+-------------+
///
pub struct HalfwordImm {
    /// The number of the 32-bit register to be loaded.
    rt: u8,

    /// The number of the 64-bit base register to calculate the memory address.
    rn: u8,

    /// The type of indexing to perform for this instruction.
    index: Index,

    /// The immediate offset from the base register.
    imm: i16,

    /// The operation to perform.
    op: Op
}

impl HalfwordImm {
    /// LDRH
    /// https://developer.arm.com/documentation/ddi0602/2022-06/Base-Instructions/LDRH--immediate---Load-Register-Halfword--immediate--
    pub fn ldrh(rt: u8, rn: u8, imm12: i16) -> Self {
        Self { rt, rn, index: Index::None, imm: imm12, op: Op::Load }
    }

    /// LDRH (pre-index)
    /// https://developer.arm.com/documentation/ddi0602/2022-06/Base-Instructions/LDRH--immediate---Load-Register-Halfword--immediate--
    pub fn ldrh_pre(rt: u8, rn: u8, imm9: i16) -> Self {
        Self { rt, rn, index: Index::PreIndex, imm: imm9, op: Op::Load }
    }

    /// LDRH (post-index)
    /// https://developer.arm.com/documentation/ddi0602/2022-06/Base-Instructions/LDRH--immediate---Load-Register-Halfword--immediate--
    pub fn ldrh_post(rt: u8, rn: u8, imm9: i16) -> Self {
        Self { rt, rn, index: Index::PostIndex, imm: imm9, op: Op::Load }
    }

    /// STRH
    /// https://developer.arm.com/documentation/ddi0602/2022-06/Base-Instructions/STRH--immediate---Store-Register-Halfword--immediate--
    pub fn strh(rt: u8, rn: u8, imm12: i16) -> Self {
        Self { rt, rn, index: Index::None, imm: imm12, op: Op::Store }
    }

    /// STRH (pre-index)
    /// https://developer.arm.com/documentation/ddi0602/2022-06/Base-Instructions/STRH--immediate---Store-Register-Halfword--immediate--
    pub fn strh_pre(rt: u8, rn: u8, imm9: i16) -> Self {
        Self { rt, rn, index: Index::PreIndex, imm: imm9, op: Op::Store }
    }

    /// STRH (post-index)
    /// https://developer.arm.com/documentation/ddi0602/2022-06/Base-Instructions/STRH--immediate---Store-Register-Halfword--immediate--
    pub fn strh_post(rt: u8, rn: u8, imm9: i16) -> Self {
        Self { rt, rn, index: Index::PostIndex, imm: imm9, op: Op::Store }
    }
}

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

impl From<HalfwordImm> for u32 {
    /// Convert an instruction into a 32-bit value.
    fn from(inst: HalfwordImm) -> Self {
        let (opc, imm) = match inst.index {
            Index::None => {
                assert_eq!(inst.imm & 1, 0, "immediate offset must be even");
                let imm12 = truncate_imm::<_, 12>(inst.imm / 2);
                (0b100, imm12)
            },
            Index::PreIndex | Index::PostIndex => {
                let imm9 = truncate_imm::<_, 9>(inst.imm);
                (0b000, (imm9 << 2) | (inst.index as u32))
            }
        };

        0
        | (FAMILY << 25)
        | ((opc | (inst.op as u32)) << 22)
        | (imm << 10)
        | ((inst.rn as u32) << 5)
        | (inst.rt as u32)
    }
}

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

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

    #[test]
    fn test_ldrh() {
        let inst = HalfwordImm::ldrh(0, 1, 8);
        let result: u32 = inst.into();
        assert_eq!(0x79401020, result);
    }

    #[test]
    fn test_ldrh_pre() {
        let inst = HalfwordImm::ldrh_pre(0, 1, 16);
        let result: u32 = inst.into();
        assert_eq!(0x78410c20, result);
    }

    #[test]
    fn test_ldrh_post() {
        let inst = HalfwordImm::ldrh_post(0, 1, 24);
        let result: u32 = inst.into();
        assert_eq!(0x78418420, result);
    }

    #[test]
    fn test_ldrh_post_negative() {
        let inst = HalfwordImm::ldrh_post(0, 1, -24);
        let result: u32 = inst.into();
        assert_eq!(0x785e8420, result);
    }

    #[test]
    fn test_strh() {
        let inst = HalfwordImm::strh(0, 1, 0);
        let result: u32 = inst.into();
        assert_eq!(0x79000020, result);
    }

    #[test]
    fn test_strh_pre() {
        let inst = HalfwordImm::strh_pre(0, 1, 0);
        let result: u32 = inst.into();
        assert_eq!(0x78000c20, result);
    }

    #[test]
    fn test_strh_post() {
        let inst = HalfwordImm::strh_post(0, 1, 0);
        let result: u32 = inst.into();
        assert_eq!(0x78000420, result);
    }
}