1# Capstone Python bindings, by Nguyen Anh Quynnh <aquynh@gmail.com>
2
3import ctypes
4from . import copy_ctypes_list
5from .arm_const import *
6
7# define the API
8class ArmOpMem(ctypes.Structure):
9    _fields_ = (
10        ('base', ctypes.c_uint),
11        ('index', ctypes.c_uint),
12        ('scale', ctypes.c_int),
13        ('disp', ctypes.c_int),
14        ('lshift', ctypes.c_int),
15    )
16
17class ArmOpShift(ctypes.Structure):
18    _fields_ = (
19        ('type', ctypes.c_uint),
20        ('value', ctypes.c_uint),
21    )
22
23class ArmOpValue(ctypes.Union):
24    _fields_ = (
25        ('reg', ctypes.c_uint),
26        ('imm', ctypes.c_int32),
27        ('fp', ctypes.c_double),
28        ('mem', ArmOpMem),
29        ('setend', ctypes.c_int),
30    )
31
32class ArmOp(ctypes.Structure):
33    _fields_ = (
34        ('vector_index', ctypes.c_int),
35        ('shift', ArmOpShift),
36        ('type', ctypes.c_uint),
37        ('value', ArmOpValue),
38        ('subtracted', ctypes.c_bool),
39        ('access', ctypes.c_uint8),
40        ('neon_lane', ctypes.c_int8),
41    )
42
43    @property
44    def imm(self):
45        return self.value.imm
46
47    @property
48    def reg(self):
49        return self.value.reg
50
51    @property
52    def fp(self):
53        return self.value.fp
54
55    @property
56    def mem(self):
57        return self.value.mem
58
59    @property
60    def setend(self):
61        return self.value.setend
62
63
64class CsArm(ctypes.Structure):
65    _fields_ = (
66        ('usermode', ctypes.c_bool),
67        ('vector_size', ctypes.c_int),
68        ('vector_data', ctypes.c_int),
69        ('cps_mode', ctypes.c_int),
70        ('cps_flag', ctypes.c_int),
71        ('cc', ctypes.c_uint),
72        ('update_flags', ctypes.c_bool),
73        ('writeback', ctypes.c_bool),
74        ('mem_barrier', ctypes.c_int),
75        ('op_count', ctypes.c_uint8),
76        ('operands', ArmOp * 36),
77    )
78
79def get_arch_info(a):
80    return (a.usermode, a.vector_size, a.vector_data, a.cps_mode, a.cps_flag, a.cc, a.update_flags, \
81        a.writeback, a.mem_barrier, copy_ctypes_list(a.operands[:a.op_count]))
82
83