capstone/arch/
m680x.rs

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
//! Contains m680x-specific types

use core::convert::From;
use core::{fmt, slice};

use capstone_sys::{
    cs_m680x, cs_m680x_op, m680x_op_ext, m680x_op_idx, m680x_op_rel, m680x_op_type,
};

// XXX todo(tmfink): create rusty versions
pub use capstone_sys::m680x_insn as M680xInsn;
pub use capstone_sys::m680x_reg as M680xReg;

pub use crate::arch::arch_builder::m680x::*;
use crate::arch::DetailsArchInsn;
use crate::instruction::{RegId, RegIdInt};


/// Contains M680X-specific details for an instruction
pub struct M680xInsnDetail<'a>(pub(crate) &'a cs_m680x);

impl_PartialEq_repr_fields!(M680xInsnDetail<'a> [ 'a ];
    operands, flags
);

// M680X instruction flags
const M680X_FIRST_OP_IN_MNEM: u8 = 1;
const M680X_SECOND_OP_IN_MNEM: u8 = 2;

define_impl_bitmask!(
    impl M680xInsnDetail<'a>;
    flags: u8 = { |self_: &M680xInsnDetail| self_.0.flags }
    test_mod = test_M680xInsnDetail;

    /// The first (register) operand is part of the instruction mnemonic
    => is_first_op_in_mnem = M680X_FIRST_OP_IN_MNEM;

    /// The second (register) operand is part of the instruction mnemonic
    => is_second_op_in_mnem = M680X_SECOND_OP_IN_MNEM;
);

/// Instruction's operand referring to indexed addressing
#[derive(Clone, Debug)]
pub struct M680xOpIdx(pub(crate) m680x_op_idx);

impl_PartialEq_repr_fields!(M680xOpIdx [ ];
    base_reg, offset_reg, offset, offset_addr, offset_bits, inc_dec, flags
);

impl Eq for M680xOpIdx {}

macro_rules! define_m680x_register_option_getter {
    (
        $( #[$enum_attr:meta] )*
        => $field:ident
    ) => {
        $( #[$enum_attr] )*
        pub fn $field(&self) -> Option<RegId> {
            if (self.0).$field == M680xReg::M680X_REG_INVALID {
                None
            } else {
                Some(RegId((self.0).$field as RegIdInt))
            }
        }
    }
}

impl M680xOpIdx {
    fn new(op_idx: &m680x_op_idx) -> Self {
        M680xOpIdx(*op_idx)
    }

    define_m680x_register_option_getter!(
        /// Base register
        => base_reg
    );

    define_m680x_register_option_getter!(
        /// Offset register
        => offset_reg
    );

    /// 5-,8- or 16-bit offset
    pub fn offset(&self) -> i16 {
        self.0.offset
    }

    /// Offset address
    ///
    /// if base_reg == M680X_REG_PC, then calculated as offset + PC
    pub fn offset_addr(&self) -> u16 {
        self.0.offset_addr
    }

    /// Offset bits
    pub fn offset_bits(&self) -> u8 {
        self.0.offset_bits
    }

    /// Increment or decrement value
    ///
    /// - `0`: no inc-/decrement
    /// - `1 .. 8`: increment by `1 .. 8`
    /// - `-1 .. -8`: decrement by `1 .. 8`
    ///
    /// if flag `M680X_IDX_POST_INC_DEC` set it is post
    /// inc-/decrement, otherwise pre inc-/decrement.
    pub fn inc_dec(&self) -> i8 {
        self.0.inc_dec
    }
}

// Comes from M680X_IDX_* #defines
const M680X_IDX_INDIRECT: u8 = 1;
const M680X_IDX_NO_COMMA: u8 = 2;
const M680X_IDX_POST_INC_DEC: u8 = 4;

define_impl_bitmask!(
    impl M680xOpIdx<>;
    flags: u8 = { |self_: &M680xOpIdx| self_.0.flags }
    test_mod = test_M680xOpIdx;

    /// Is index indirect?
    => is_indirect = M680X_IDX_INDIRECT;

    /// Is there no comma?
    => is_no_comma = M680X_IDX_NO_COMMA;

    /// Is index indirect?
    => is_post_inc_dec = M680X_IDX_POST_INC_DEC;
);

/// M680X operand
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum M680xOperandType {
    /// Register
    Reg(RegId),

    /// Immediate
    Imm(i32),

    /// Indexed addressing operand
    Indexed(M680xOpIdx),

    /// Extended addressing operand
    Extended {
        /// Absolute address
        address: u16,

        /// Whether extended indirect addressing
        indirect: bool,
    },

    /// Direct addressing operand
    Direct {
        /// Direct address (lower 8-bit)
        direct_addr: u8,
    },

    /// Relative addressing operand
    Relative {
        /// Absolute address
        address: u16,

        /// Offset/displacement value
        offset: i16,
    },

    /// Constant operand (displayed as number only)
    ///
    /// Used e.g. for a bit index or page number.
    Constant(u8),

    /// Invalid
    Invalid,
}

impl Default for M680xOperandType {
    fn default() -> Self {
        M680xOperandType::Invalid
    }
}

impl<'a> From<&'a cs_m680x_op> for M680xOperand {
    fn from(op: &cs_m680x_op) -> M680xOperand {
        let op_type = match op.type_ {
            m680x_op_type::M680X_OP_REGISTER => {
                M680xOperandType::Reg(RegId(unsafe { op.__bindgen_anon_1.reg } as RegIdInt))
            }
            m680x_op_type::M680X_OP_IMMEDIATE => {
                M680xOperandType::Imm(unsafe { op.__bindgen_anon_1.imm })
            }
            m680x_op_type::M680X_OP_INDEXED => {
                M680xOperandType::Indexed(M680xOpIdx::new(unsafe { &op.__bindgen_anon_1.idx }))
            }
            m680x_op_type::M680X_OP_EXTENDED => {
                let op_ext: m680x_op_ext = unsafe { op.__bindgen_anon_1.ext };
                M680xOperandType::Extended {
                    address: op_ext.address,
                    indirect: op_ext.indirect,
                }
            }
            m680x_op_type::M680X_OP_DIRECT => M680xOperandType::Direct {
                direct_addr: unsafe { op.__bindgen_anon_1.direct_addr },
            },
            m680x_op_type::M680X_OP_RELATIVE => {
                let op_rel: m680x_op_rel = unsafe { op.__bindgen_anon_1.rel };
                M680xOperandType::Relative {
                    address: op_rel.address,
                    offset: op_rel.offset,
                }
            }
            m680x_op_type::M680X_OP_CONSTANT => {
                M680xOperandType::Constant(unsafe { op.__bindgen_anon_1.const_val })
            }
            m680x_op_type::M680X_OP_INVALID => M680xOperandType::Invalid,
        };

        M680xOperand {
            op_type,
            size: op.size,
        }
    }
}

/// M680X operand
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct M680xOperand {
    /// Operand type
    pub op_type: M680xOperandType,

    /// Size of this operand in bytes
    pub size: u8,
}

def_arch_details_struct!(
    InsnDetail = M680xInsnDetail;
    Operand = M680xOperand;
    OperandIterator = M680xOperandIterator;
    OperandIteratorLife = M680xOperandIterator<'a>;
    [ pub struct M680xOperandIterator<'a>(slice::Iter<'a, cs_m680x_op>); ]
    cs_arch_op = cs_m680x_op;
    cs_arch = cs_m680x;
);

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

    #[test]
    fn m680x_op_type() {
        let op_base = cs_m680x_op {
            type_: m680x_op_type::M680X_OP_INVALID,
            __bindgen_anon_1: cs_m680x_op__bindgen_ty_1 { reg: 0 },
            size: 1,
            access: 0,
        };

        assert_eq!(
            M680xOperand::from(&op_base).op_type,
            M680xOperandType::Invalid
        );
        assert_eq!(
            M680xOperand::from(&cs_m680x_op {
                type_: m680x_op_type::M680X_OP_REGISTER,
                __bindgen_anon_1: cs_m680x_op__bindgen_ty_1 {
                    reg: M680xReg::M680X_REG_E
                },
                ..op_base
            })
            .op_type,
            M680xOperandType::Reg(RegId(M680xReg::M680X_REG_E as RegIdInt))
        );
        assert_eq!(
            M680xOperand::from(&cs_m680x_op {
                type_: m680x_op_type::M680X_OP_CONSTANT,
                __bindgen_anon_1: cs_m680x_op__bindgen_ty_1 { const_val: 42 },
                ..op_base
            })
            .op_type,
            M680xOperandType::Constant(42)
        );
        assert_eq!(
            M680xOperand::from(&cs_m680x_op {
                type_: m680x_op_type::M680X_OP_IMMEDIATE,
                __bindgen_anon_1: cs_m680x_op__bindgen_ty_1 { imm: 1037 },
                ..op_base
            })
            .op_type,
            M680xOperandType::Imm(1037)
        );
        assert_eq!(
            M680xOperand::from(&cs_m680x_op {
                type_: m680x_op_type::M680X_OP_DIRECT,
                __bindgen_anon_1: cs_m680x_op__bindgen_ty_1 { direct_addr: 67 },
                ..op_base
            })
            .op_type,
            M680xOperandType::Direct { direct_addr: 67 }
        );
        assert_eq!(
            M680xOperand::from(&cs_m680x_op {
                type_: m680x_op_type::M680X_OP_EXTENDED,
                __bindgen_anon_1: cs_m680x_op__bindgen_ty_1 {
                    ext: m680x_op_ext {
                        address: 45876,
                        indirect: true,
                    }
                },
                ..op_base
            })
            .op_type,
            M680xOperandType::Extended {
                address: 45876,
                indirect: true
            }
        );

        let base_reg = m680x_reg::M680X_REG_A;
        let offset_reg = m680x_reg::M680X_REG_B;
        let offset = 5;
        let offset_addr = 0x1337;
        let offset_bits = 4;
        let inc_dec = -3;
        let cs_op_idx = m680x_op_idx {
            base_reg,
            offset_reg,
            offset,
            offset_addr,
            offset_bits,
            inc_dec,
            flags: 7,
        };
        assert_eq!(
            M680xOperand::from(&cs_m680x_op {
                type_: m680x_op_type::M680X_OP_INDEXED,
                __bindgen_anon_1: cs_m680x_op__bindgen_ty_1 { idx: cs_op_idx },
                ..op_base
            })
            .op_type,
            M680xOperandType::Indexed(M680xOpIdx(cs_op_idx))
        );
    }

    #[test]
    fn op_idx() {
        let base_reg = m680x_reg::M680X_REG_A;
        let offset_reg = m680x_reg::M680X_REG_B;
        let offset = 5;
        let offset_addr = 0x1337;
        let offset_bits = 4;
        let inc_dec = -3;

        let mut idx = M680xOpIdx(m680x_op_idx {
            base_reg,
            offset_reg,
            offset,
            offset_addr,
            offset_bits,
            inc_dec,
            flags: 7,
        });

        assert_eq!(idx.base_reg(), Some(RegId(base_reg as RegIdInt)));
        assert_eq!(idx.offset_reg(), Some(RegId(offset_reg as RegIdInt)));
        assert_eq!(idx.offset(), offset);
        assert_eq!(idx.offset_addr(), offset_addr);
        assert_eq!(idx.offset_bits(), offset_bits);
        assert_eq!(idx.inc_dec(), inc_dec);
        assert!(idx.is_indirect());
        assert!(idx.is_no_comma());
        assert!(idx.is_post_inc_dec());

        idx.0.flags = 5;
        assert!(idx.is_indirect());
        assert!(!idx.is_no_comma());
        assert!(idx.is_post_inc_dec());
    }
}