Skip to main content

neo_decompiler/instruction/
operand.rs

1use std::fmt;
2
3use crate::syscalls;
4use crate::util;
5
6/// Instruction operands supported by the disassembler.
7#[derive(Debug, Clone, PartialEq, Eq, Hash)]
8#[non_exhaustive]
9pub enum Operand {
10    /// Signed 8-bit integer.
11    I8(i8),
12    /// Signed 16-bit integer.
13    I16(i16),
14    /// Signed 32-bit integer.
15    I32(i32),
16    /// Signed 64-bit integer.
17    I64(i64),
18    /// Raw byte payload (e.g. PUSHDATA/PUSHBYTES).
19    Bytes(Vec<u8>),
20    /// Signed 8-bit jump offset.
21    Jump(i8),
22    /// Signed 32-bit jump offset.
23    Jump32(i32),
24    /// Syscall identifier (little-endian u32).
25    Syscall(u32),
26    /// Unsigned 8-bit integer.
27    U8(u8),
28    /// Unsigned 16-bit integer.
29    U16(u16),
30    /// Unsigned 32-bit integer.
31    U32(u32),
32    /// Boolean value.
33    Bool(bool),
34    /// Null literal.
35    Null,
36}
37
38impl fmt::Display for Operand {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Operand::I8(v) => write!(f, "{v}"),
42            Operand::I16(v) => write!(f, "{v}"),
43            Operand::I32(v) => write!(f, "{v}"),
44            Operand::I64(v) => write!(f, "{v}"),
45            Operand::Bytes(bytes) => {
46                write!(f, "0x")?;
47                util::write_upper_hex(f, bytes)
48            }
49            Operand::Jump(offset) => write!(f, "{offset}"),
50            Operand::Jump32(offset) => write!(f, "{offset}"),
51            Operand::Syscall(hash) => {
52                if let Some(info) = syscalls::lookup(*hash) {
53                    write!(f, "{} (0x{hash:08X})", info.name)
54                } else {
55                    write!(f, "0x{hash:08X}")
56                }
57            }
58            Operand::U8(value) => write!(f, "{value}"),
59            Operand::U16(value) => write!(f, "{value}"),
60            Operand::U32(value) => write!(f, "{value}"),
61            Operand::Bool(value) => write!(f, "{value}"),
62            Operand::Null => write!(f, "null"),
63        }
64    }
65}