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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
use core::{
    fmt,
    fmt::{Display, Formatter, LowerHex},
};

use crate::generated::{
    parse_basic, parse_defs, parse_simplified, parse_uses, Arguments, Opcode, EMPTY_ARGS,
};

/// A PowerPC 750CL instruction.
#[derive(Default, Copy, Clone, Debug, Eq, PartialEq)]
pub struct Ins {
    pub code: u32,
    pub op: Opcode,
}

impl Ins {
    /// Create a new instruction from its raw code.
    pub fn new(code: u32) -> Self {
        Self { code, op: Opcode::_detect(code) }
    }

    /// Parse the instruction into a simplified mnemonic, if any match.
    #[inline]
    pub fn parse_simplified(self, out: &mut ParsedIns) {
        parse_simplified(out, self)
    }

    /// Returns the simplified form of the instruction, if any match.
    #[inline]
    pub fn simplified(self) -> ParsedIns {
        let mut out = ParsedIns::new();
        parse_simplified(&mut out, self);
        out
    }

    /// Parse the instruction into its basic form.
    #[inline]
    pub fn parse_basic(self, out: &mut ParsedIns) {
        parse_basic(out, self)
    }

    /// Returns the basic form of the instruction.
    #[inline]
    pub fn basic(self) -> ParsedIns {
        let mut out = ParsedIns::new();
        parse_basic(&mut out, self);
        out
    }

    /// Emits all registers defined by the instruction into the given argument list.
    #[inline]
    pub fn parse_defs(self, out: &mut Arguments) {
        parse_defs(out, self)
    }

    /// Returns all registers defined by the instruction.
    #[inline]
    pub fn defs(self) -> Arguments {
        let mut out = Arguments::default();
        parse_defs(&mut out, self);
        out
    }

    /// Emits all registers used by the instruction into the given argument list.
    #[inline]
    pub fn parse_uses(self, out: &mut Arguments) {
        parse_uses(out, self)
    }

    /// Returns all registers used by the instruction.
    #[inline]
    pub fn uses(self) -> Arguments {
        let mut out = Arguments::default();
        parse_uses(&mut out, self);
        out
    }

    /// Returns the relative branch offset of the instruction, if any.
    pub fn branch_offset(&self) -> Option<i32> {
        match self.op {
            Opcode::B => Some(self.field_li()),
            Opcode::Bc => Some(self.field_bd() as i32),
            _ => None,
        }
    }

    /// Returns the absolute branch destination of the instruction, if any.
    pub fn branch_dest(&self, addr: u32) -> Option<u32> {
        self.branch_offset().and_then(|offset| {
            if self.field_aa() {
                Some(offset as u32)
            } else {
                addr.checked_add_signed(offset)
            }
        })
    }

    /// Whether the instruction is any kind of branch.
    pub fn is_branch(&self) -> bool {
        matches!(self.op, Opcode::B | Opcode::Bc | Opcode::Bcctr | Opcode::Bclr)
    }

    /// Whether the instruction is a direct branch.
    pub fn is_direct_branch(&self) -> bool {
        matches!(self.op, Opcode::B | Opcode::Bc)
    }

    /// Whether the instruction is an unconditional branch.
    pub fn is_unconditional_branch(&self) -> bool {
        match self.op {
            Opcode::B => true,
            Opcode::Bc | Opcode::Bcctr | Opcode::Bclr => {
                self.field_bo() == 20 && self.field_bi() == 0
            }
            _ => false,
        }
    }

    /// Whether the instruction is a conditional branch.
    pub fn is_conditional_branch(&self) -> bool {
        self.is_branch() && !self.is_unconditional_branch()
    }

    /// Whether the instruction is a branch with link. (blr)
    #[inline]
    pub fn is_blr(&self) -> bool {
        self.code == 0x4e800020
    }
}

macro_rules! field_arg_no_display {
    ($name:ident, $typ:ident) => {
        #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
        pub struct $name(pub $typ);
        impl From<$name> for Argument {
            fn from(x: $name) -> Argument {
                Argument::$name(x)
            }
        }
        impl From<$typ> for $name {
            fn from(x: $typ) -> $name {
                $name(x)
            }
        }
    };
}

macro_rules! field_arg {
    ($name:ident, $typ:ident) => {
        field_arg_no_display!($name, $typ);
        impl Display for $name {
            fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
                write!(f, "{}", self.0)
            }
        }
    };
    ($name:ident, $typ:ident, $format:literal) => {
        field_arg_no_display!($name, $typ);
        impl Display for $name {
            fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
                write!(f, $format, self.0)
            }
        }
    };
    ($name:ident, $typ:ident, $format:literal, $format_arg:expr) => {
        field_arg_no_display!($name, $typ);
        impl Display for $name {
            fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
                write!(f, $format, $format_arg(self.0))
            }
        }
    };
}

// General-purpose register.
field_arg!(GPR, u8, "r{}");
// Floating-point register (direct or paired-singles mode).
field_arg!(FPR, u8, "f{}");
// Segment register.
field_arg!(SR, u8);
// Special-purpose register.
field_arg_no_display!(SPR, u16);
impl Display for SPR {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.write_str(match self.0 {
            1 => "XER",
            8 => "LR",
            9 => "CTR",
            18 => "DSISR",
            19 => "DAR",
            22 => "DEC",
            25 => "SDR1",
            26 => "SRR0",
            27 => "SRR1",
            272 => "SPRG0",
            273 => "SPRG1",
            274 => "SPRG2",
            275 => "SPRG3",
            282 => "EAR",
            287 => "PVR",
            528 => "IBAT0U",
            529 => "IBAT0L",
            530 => "IBAT1U",
            531 => "IBAT1L",
            532 => "IBAT2U",
            533 => "IBAT2L",
            534 => "IBAT3U",
            535 => "IBAT3L",
            536 => "DBAT0U",
            537 => "DBAT0L",
            538 => "DBAT1U",
            539 => "DBAT1L",
            540 => "DBAT2U",
            541 => "DBAT2L",
            542 => "DBAT3U",
            543 => "DBAT3L",
            912 => "GQR0",
            913 => "GQR1",
            914 => "GQR2",
            915 => "GQR3",
            916 => "GQR4",
            917 => "GQR5",
            918 => "GQR6",
            919 => "GQR7",
            920 => "HID2",
            921 => "WPAR",
            922 => "DMA_U",
            923 => "DMA_L",
            936 => "UMMCR0",
            937 => "UPMC1",
            938 => "UPMC2",
            939 => "USIA",
            940 => "UMMCR1",
            941 => "UPMC3",
            942 => "UPMC4",
            943 => "USDA",
            952 => "MMCR0",
            953 => "PMC1",
            954 => "PMC2",
            955 => "SIA",
            956 => "MMCR1",
            957 => "PMC3",
            958 => "PMC4",
            959 => "SDA",
            1008 => "HID0",
            1009 => "HID1",
            1010 => "IABR",
            1013 => "DABR",
            1017 => "L2CR",
            1019 => "ICTC",
            1020 => "THRM1",
            1021 => "THRM2",
            1022 => "THRM3",
            _ => return write!(f, "{}", self.0),
        })
    }
}
// Condition register field.
field_arg!(CRField, u8, "cr{}");
// Condition register bit (index + condition case).
field_arg_no_display!(CRBit, u8);
impl Display for CRBit {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        let cr = self.0 >> 2;
        let cc = self.0 & 3;
        if cr != 0 {
            write!(f, "{}", CRField(cr))?;
        }
        const CR_NAMES: [&str; 4] = ["lt", "gt", "eq", "un"];
        f.write_str(CR_NAMES[cc as usize])
    }
}
// Paired-single graphics quantization register
field_arg!(GQR, u8, "qr{}");
// Unsigned immediate.
field_arg!(Uimm, u16, "{:#x}");
// Signed immediate.
field_arg!(Simm, i16, "{:#x}", SignedHexLiteral);
// Offset for indirect memory reference.
field_arg!(Offset, i16, "{:#x}", SignedHexLiteral);
// Branch destination.
field_arg!(BranchDest, i32, "{:#x}", SignedHexLiteral);
impl From<i16> for BranchDest {
    fn from(x: i16) -> BranchDest {
        BranchDest(x as i32)
    }
}
// Unsigned opaque argument.
field_arg!(OpaqueU, u16);
impl From<u8> for OpaqueU {
    fn from(x: u8) -> OpaqueU {
        OpaqueU(x as u16)
    }
}

#[derive(Debug, Default, Copy, Clone, Eq, PartialEq)]
pub enum Argument {
    #[default]
    None,
    GPR(GPR),
    FPR(FPR),
    SR(SR),
    SPR(SPR),
    CRField(CRField),
    CRBit(CRBit),
    GQR(GQR),
    Uimm(Uimm),
    Simm(Simm),
    Offset(Offset),
    BranchDest(BranchDest),
    OpaqueU(OpaqueU),
}

impl Display for Argument {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            Argument::None => Ok(()),
            Argument::GPR(x) => x.fmt(f),
            Argument::FPR(x) => x.fmt(f),
            Argument::SR(x) => x.fmt(f),
            Argument::SPR(x) => x.fmt(f),
            Argument::CRField(x) => x.fmt(f),
            Argument::CRBit(x) => x.fmt(f),
            Argument::GQR(x) => x.fmt(f),
            Argument::Uimm(x) => x.fmt(f),
            Argument::Simm(x) => x.fmt(f),
            Argument::Offset(x) => x.fmt(f),
            Argument::BranchDest(x) => x.fmt(f),
            Argument::OpaqueU(x) => x.fmt(f),
        }
    }
}

/// A parsed PowerPC 750CL instruction.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ParsedIns {
    pub mnemonic: &'static str,
    pub args: Arguments,
}

impl Default for ParsedIns {
    fn default() -> Self {
        Self::new()
    }
}

impl ParsedIns {
    /// An empty parsed instruction.
    pub const fn new() -> Self {
        Self { mnemonic: "<illegal>", args: EMPTY_ARGS }
    }

    /// Returns an iterator over the arguments of the instruction,
    /// stopping at the first [Argument::None].
    #[inline]
    pub fn args_iter(&self) -> impl Iterator<Item = &Argument> {
        self.args.iter().take_while(|x| !matches!(x, Argument::None))
    }
}

impl Display for ParsedIns {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.mnemonic)?;
        let mut writing_offset = false;
        for (i, argument) in self.args_iter().enumerate() {
            if i == 0 {
                write!(f, " ")?;
            } else if !writing_offset {
                write!(f, ", ")?;
            }
            write!(f, "{}", argument)?;
            if let Argument::Offset(_) = argument {
                write!(f, "(")?;
                writing_offset = true;
            } else if writing_offset {
                write!(f, ")")?;
                writing_offset = false;
            }
        }
        Ok(())
    }
}

pub struct SignedHexLiteral<T>(pub T);

impl LowerHex for SignedHexLiteral<i16> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if self.0 < 0 {
            write!(f, "-")?;
            LowerHex::fmt(&-(self.0 as i32), f)
        } else {
            LowerHex::fmt(&self.0, f)
        }
    }
}

impl LowerHex for SignedHexLiteral<i32> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if self.0 < 0 {
            write!(f, "-")?;
            LowerHex::fmt(&-(self.0 as i64), f)
        } else {
            LowerHex::fmt(&self.0, f)
        }
    }
}

pub struct InsIter<'a> {
    address: u32,
    data: &'a [u8],
}

impl<'a> InsIter<'a> {
    pub fn new(data: &'a [u8], address: u32) -> Self {
        Self { address, data }
    }

    pub fn address(&self) -> u32 {
        self.address
    }

    pub fn data(&self) -> &'a [u8] {
        self.data
    }
}

impl<'a> Iterator for InsIter<'a> {
    type Item = (u32, Ins);

    fn next(&mut self) -> Option<Self::Item> {
        if self.data.len() < 4 {
            return None;
        }

        // SAFETY: The slice is guaranteed to be at least 4 bytes long.
        let chunk = unsafe { *(self.data.as_ptr() as *const [u8; 4]) };
        let ins = Ins::new(u32::from_be_bytes(chunk));
        let addr = self.address;
        self.address += 4;
        self.data = &self.data[4..];
        Some((addr, ins))
    }
}