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

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

use libc::c_int;
use capstone_sys::{
    cs_tms320c64x, cs_tms320c64x_op, tms320c64x_funit, tms320c64x_mem_dir, tms320c64x_mem_disp,
    tms320c64x_mem_mod, tms320c64x_op_mem, tms320c64x_op_type,
};

// XXX todo(tmfink): create rusty versions
pub use capstone_sys::tms320c64x_insn as Tms320c64xInsn;
pub use capstone_sys::tms320c64x_insn_group as Tms320c64xInsnGroup;
pub use capstone_sys::tms320c64x_reg as Tms320c64xReg;

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


/// Contains TMS320C64X-specific details for an instruction
pub struct Tms320c64xInsnDetail<'a>(pub(crate) &'a cs_tms320c64x);

define_cs_enum_wrapper_reverse!(
    [
        /// TMS320C64X Functional Unit
        => Tms320c64xFuntionalUnit = tms320c64x_funit,
    ]
    /// Invalid or unspecified
    => Invalid = TMS320C64X_FUNIT_INVALID;
    /// D
    => D = TMS320C64X_FUNIT_D;
    /// L
    => L = TMS320C64X_FUNIT_L;
    /// M
    => M = TMS320C64X_FUNIT_M;
    /// S
    => S = TMS320C64X_FUNIT_S;
    /// NO
    => No = TMS320C64X_FUNIT_NO;
);

impl<'a> Tms320c64xInsnDetail<'a> {
    /// Whether condition is zero
    pub fn is_condition_zero(&self) -> bool {
        self.0.condition.zero != 0
    }

    /// Condition register
    pub fn condition_reg(&self) -> RegId {
        RegId(self.0.condition.reg as RegIdInt)
    }

    /// Functional unit
    pub fn functional_unit(&self) -> Tms320c64xFuntionalUnit {
        Tms320c64xFuntionalUnit::from_u32(self.0.funit.unit)
            .unwrap_or(Tms320c64xFuntionalUnit::Invalid)
    }

    /// Functional unit side
    pub fn functional_unit_side(&self) -> u8 {
        self.0.funit.side as u8
    }

    /// Functional unit cross path
    pub fn functional_unit_cross_path(&self) -> i8 {
        // todo(tmfink): capstone bug where cs_tms320c64x.funit.crosspath is stored as unsigned
        // instead of signed
        self.0.funit.crosspath as i8
    }

    /// Instruction parallel
    pub fn parallel(&self) -> i8 {
        self.0.parallel as c_int as i8
    }
}

impl_PartialEq_repr_fields!(Tms320c64xInsnDetail<'a> [ 'a ];
    is_condition_zero, condition_reg, functional_unit, functional_unit_side,
    functional_unit_cross_path, parallel
);

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

    /// Immediate
    Imm(i32),

    /// Memory
    Mem(Tms320c64xOpMem),

    /// Pair of registers
    RegPair(RegId, RegId),

    /// Invalid
    Invalid,
}

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

define_cs_enum_wrapper_reverse!(
    [
        /// TMS320C64X Memory Operand modification
        => Tms320c64xMemDisplayType = tms320c64x_mem_disp,
    ]
    /// Invalid or unspecified
    => Invalid = TMS320C64X_MEM_DISP_INVALID;
    /// Constant
    => Constant = TMS320C64X_MEM_DISP_CONSTANT;
    /// Regiter
    => Register = TMS320C64X_MEM_DISP_REGISTER;
);

/// TMS320C64X Operand Memory Display
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum Tms320c64xMemDisplay {
    /// Invalid or unspecified
    Invalid,

    /// Constant
    Constant(u32),

    /// Register
    Register(RegId),
}

define_cs_enum_wrapper_reverse!(
    [
        /// TMS320C64X Memory Operand direction
        => Tms320c64xMemDirection = tms320c64x_mem_dir,
    ]
    /// Invalid or unspecified
    => Invalid = TMS320C64X_MEM_DIR_INVALID;
    /// Forward
    => Forward = TMS320C64X_MEM_DIR_FW;
    /// Backward
    => Backward = TMS320C64X_MEM_DIR_BW;
);

define_cs_enum_wrapper_reverse!(
    [
        /// TMS320C64X Memory Operand modification
        => Tms320c64xMemModify = tms320c64x_mem_mod,
    ]
    /// Invalid or unspecified
    => Invalid = TMS320C64X_MEM_MOD_INVALID;
    /// No
    => No = TMS320C64X_MEM_MOD_NO;
    /// Pre
    => Pre = TMS320C64X_MEM_MOD_PRE;
    /// Post
    => Post = TMS320C64X_MEM_MOD_POST;
);

/// TMS320C64X memory operand
#[derive(Debug, Copy, Clone)]
pub struct Tms320c64xOpMem(pub(crate) tms320c64x_op_mem);

/// todo(tmfink): add all getters
impl Tms320c64xOpMem {
    /// Base register
    pub fn base(&self) -> RegId {
        RegId(self.0.base as RegIdInt)
    }

    /// Disp value (type depends on display_type)
    fn disp(&self) -> u32 {
        self.0.disp as u32
    }

    /// Unit of base and offset register
    pub fn unit(&self) -> u32 {
        self.0.unit as u32
    }

    /// Offset scaled
    pub fn scaled(&self) -> u32 {
        self.0.scaled as u32
    }

    /// Displacement type
    fn display_type(&self) -> Tms320c64xMemDisplayType {
        Tms320c64xMemDisplayType::from_u32(self.0.disptype as u32)
            .unwrap_or(Tms320c64xMemDisplayType::Invalid)
    }

    /// Display
    pub fn display(&self) -> Tms320c64xMemDisplay {
        match self.display_type() {
            Tms320c64xMemDisplayType::Invalid => Tms320c64xMemDisplay::Invalid,
            Tms320c64xMemDisplayType::Constant => Tms320c64xMemDisplay::Constant(self.disp()),
            Tms320c64xMemDisplayType::Register => {
                Tms320c64xMemDisplay::Register(RegId(self.disp() as RegIdInt))
            }
        }
    }

    /// Direction
    pub fn direction(&self) -> Tms320c64xMemDirection {
        Tms320c64xMemDirection::from_u32(self.0.direction as u32)
            .unwrap_or(Tms320c64xMemDirection::Invalid)
    }

    /// Modification
    pub fn modify(&self) -> Tms320c64xMemModify {
        Tms320c64xMemModify::from_u32(self.0.modify as u32).unwrap_or(Tms320c64xMemModify::Invalid)
    }
}

impl_PartialEq_repr_fields!(Tms320c64xOpMem;
    base, disp, unit, scaled, display_type, direction, modify
);

impl cmp::Eq for Tms320c64xOpMem {}

impl<'a> From<&'a cs_tms320c64x_op> for Tms320c64xOperand {
    fn from(insn: &cs_tms320c64x_op) -> Tms320c64xOperand {
        match insn.type_ {
            tms320c64x_op_type::TMS320C64X_OP_REG => {
                Tms320c64xOperand::Reg(RegId(unsafe { insn.__bindgen_anon_1.reg } as RegIdInt))
            }
            tms320c64x_op_type::TMS320C64X_OP_IMM => {
                Tms320c64xOperand::Imm(unsafe { insn.__bindgen_anon_1.imm } as i32)
            }
            tms320c64x_op_type::TMS320C64X_OP_MEM => {
                Tms320c64xOperand::Mem(Tms320c64xOpMem(unsafe { insn.__bindgen_anon_1.mem }))
            }
            tms320c64x_op_type::TMS320C64X_OP_REGPAIR => {
                let reg = unsafe { insn.__bindgen_anon_1.reg };
                // todo(tmfink): bug in capstone?
                Tms320c64xOperand::RegPair(RegId((reg as RegIdInt) + 1), RegId(reg as RegIdInt))
            }
            tms320c64x_op_type::TMS320C64X_OP_INVALID => Tms320c64xOperand::Invalid,
        }
    }
}

def_arch_details_struct!(
    InsnDetail = Tms320c64xInsnDetail;
    Operand = Tms320c64xOperand;
    OperandIterator = Tms320c64xOperandIterator;
    OperandIteratorLife = Tms320c64xOperandIterator<'a>;
    [ pub struct Tms320c64xOperandIterator<'a>(slice::Iter<'a, cs_tms320c64x_op>); ]
    cs_arch_op = cs_tms320c64x_op;
    cs_arch = cs_tms320c64x;
);

#[cfg(test)]
mod test {
    use super::*;
    use capstone_sys::*;
    use libc::{c_int, c_uint};

    const OP_MEM_ZERO: tms320c64x_op_mem = tms320c64x_op_mem {
        base: 0,
        disp: 0,
        unit: 0,
        scaled: 0,
        disptype: 0,
        direction: 0,
        modify: 0,
    };

    #[test]
    fn tms320c64x_insn_detail() {
        let op = cs_tms320c64x_op {
            type_: tms320c64x_op_type::TMS320C64X_OP_IMM,
            __bindgen_anon_1: cs_tms320c64x_op__bindgen_ty_1 { imm: 0 },
        };
        let cs_insn = cs_tms320c64x {
            op_count: 0,
            operands: [op; 8],
            condition: cs_tms320c64x__bindgen_ty_1 {
                reg: tms320c64x_reg::TMS320C64X_REG_GPLYA as c_uint,
                zero: 1,
            },
            funit: cs_tms320c64x__bindgen_ty_2 {
                unit: tms320c64x_funit::TMS320C64X_FUNIT_L as c_uint,
                side: 18,
                crosspath: -1 as c_int as c_uint,
            },
            parallel: 1,
        };
        let d = Tms320c64xInsnDetail(&cs_insn);

        assert!(d.is_condition_zero());
        assert_eq!(
            d.condition_reg(),
            RegId(Tms320c64xReg::TMS320C64X_REG_GPLYA as RegIdInt)
        );
        assert_eq!(d.functional_unit(), Tms320c64xFuntionalUnit::L);
        assert_eq!(d.functional_unit_side(), 18);
        assert_eq!(d.functional_unit_cross_path(), -1);
        assert_eq!(d.parallel(), 1);
    }

    #[test]
    fn tms320c64x_op_from() {
        let op = cs_tms320c64x_op {
            type_: tms320c64x_op_type::TMS320C64X_OP_INVALID,
            __bindgen_anon_1: cs_tms320c64x_op__bindgen_ty_1 { reg: 0 },
        };
        assert_eq!(
            Tms320c64xOperand::from(&op),
            Tms320c64xOperand::Invalid
        );
    }

    #[test]
    fn op_mem() {
        // display type
        assert_eq!(
            Tms320c64xOpMem(OP_MEM_ZERO).display(),
            Tms320c64xMemDisplay::Invalid
        );
        assert_eq!(
            Tms320c64xOpMem(tms320c64x_op_mem {
                disptype: 999,
                ..OP_MEM_ZERO
            })
            .display(),
            Tms320c64xMemDisplay::Invalid
        );
        assert_eq!(
            Tms320c64xOpMem(tms320c64x_op_mem {
                disptype: tms320c64x_mem_disp::TMS320C64X_MEM_DISP_CONSTANT as c_uint,
                disp: 3133789374,
                ..OP_MEM_ZERO
            })
            .display(),
            Tms320c64xMemDisplay::Constant(3133789374)
        );
        assert_eq!(
            Tms320c64xOpMem(tms320c64x_op_mem {
                disptype: tms320c64x_mem_disp::TMS320C64X_MEM_DISP_REGISTER as c_uint,
                disp: tms320c64x_reg::TMS320C64X_REG_A13 as c_uint,
                ..OP_MEM_ZERO
            })
            .display(),
            Tms320c64xMemDisplay::Register(RegId(Tms320c64xReg::TMS320C64X_REG_A13 as RegIdInt))
        );

        // Simple getters
        assert_eq!(
            Tms320c64xOpMem(tms320c64x_op_mem {
                base: tms320c64x_reg::TMS320C64X_REG_A13 as c_uint,
                ..OP_MEM_ZERO
            })
            .base(),
            RegId(Tms320c64xReg::TMS320C64X_REG_A13 as RegIdInt)
        );
        assert_eq!(
            Tms320c64xOpMem(tms320c64x_op_mem {
                unit: 29393 as c_uint,
                ..OP_MEM_ZERO
            })
            .unit(),
            29393
        );
        assert_eq!(
            Tms320c64xOpMem(tms320c64x_op_mem {
                scaled: 29393 as c_uint,
                ..OP_MEM_ZERO
            })
            .scaled(),
            29393
        );
        assert_eq!(
            Tms320c64xOpMem(tms320c64x_op_mem {
                direction: tms320c64x_mem_dir::TMS320C64X_MEM_DIR_FW as c_uint,
                ..OP_MEM_ZERO
            })
            .direction(),
            Tms320c64xMemDirection::Forward,
        );
        assert_eq!(
            Tms320c64xOpMem(tms320c64x_op_mem {
                modify: tms320c64x_mem_mod::TMS320C64X_MEM_MOD_PRE as c_uint,
                ..OP_MEM_ZERO
            })
            .modify(),
            Tms320c64xMemModify::Pre,
        );
    }
}