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
use crate::operand::{Operand, OperandWidth};

use std::fmt;

/// All single operand instructions implement this trait to provide a common
/// interface and polymorphism
pub trait SingleOperand {
    /// Return the mnemonic for the instruction. This is operand width aware
    fn mnemonic(&self) -> &str;
    /// Returns the source operand
    fn source(&self) -> &Operand;
    /// Returns the size of the instruction (in bytes)
    fn size(&self) -> usize;
    /// Returns the operand width if one is specified
    fn operand_width(&self) -> &Option<OperandWidth>;
}

macro_rules! single_operand {
    ($t:ident, $n:expr) => {
        #[derive(Debug, Clone, Copy, PartialEq)]
        pub struct $t {
            source: Operand,
            operand_width: Option<OperandWidth>,
        }

        impl $t {
            pub fn new(source: Operand, operand_width: Option<OperandWidth>) -> $t {
                $t {
                    source,
                    operand_width,
                }
            }
        }

        impl SingleOperand for $t {
            fn mnemonic(&self) -> &str {
                match self.operand_width {
                    Some(OperandWidth::Word) | None => $n,
                    Some(OperandWidth::Byte) => concat!($n, ".b"),
                }
            }

            fn source(&self) -> &Operand {
                &self.source
            }

            fn size(&self) -> usize {
                2 + self.source.size()
            }

            fn operand_width(&self) -> &Option<OperandWidth> {
                &self.operand_width
            }
        }

        impl fmt::Display for $t {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, "{} {}", self.mnemonic(), self.source)
            }
        }
    };
}

single_operand!(Rrc, "rrc");
single_operand!(Swpb, "swpb");
single_operand!(Rra, "rra");
single_operand!(Sxt, "sxt");
single_operand!(Push, "push");
single_operand!(Call, "call");

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Reti {}

impl Reti {
    pub fn new() -> Reti {
        Reti {}
    }

    pub fn size(&self) -> usize {
        2
    }
}

impl fmt::Display for Reti {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "reti")
    }
}