1 // Capstone Java binding
2 // By Nguyen Anh Quynh & Dang Hoang Vu,  2013
3 
4 package capstone;
5 
6 import com.sun.jna.Structure;
7 import com.sun.jna.Union;
8 
9 import java.util.List;
10 import java.util.Arrays;
11 
12 import static capstone.Arm_const.*;
13 
14 public class Arm {
15 
16   public static class MemType extends Structure {
17     public int base;
18     public int index;
19     public int scale;
20     public int disp;
21 
22     @Override
getFieldOrder()23     public List getFieldOrder() {
24       return Arrays.asList("base", "index", "scale", "disp");
25     }
26   }
27 
28   public static class OpValue extends Union {
29     public int reg;
30     public int imm;
31     public double fp;
32     public MemType mem;
33     public int setend;
34 
35     @Override
getFieldOrder()36     public List getFieldOrder() {
37       return Arrays.asList("reg", "imm", "fp", "mem", "setend");
38     }
39   }
40 
41   public static class OpShift extends Structure {
42     public int type;
43     public int value;
44 
45     @Override
getFieldOrder()46     public List getFieldOrder() {
47       return Arrays.asList("type","value");
48     }
49   }
50 
51   public static class Operand extends Structure {
52     public int vector_index;
53     public OpShift shift;
54     public int type;
55     public OpValue value;
56     public boolean subtracted;
57 
read()58     public void read() {
59       readField("vector_index");
60       readField("type");
61       if (type == ARM_OP_MEM)
62         value.setType(MemType.class);
63       if (type == ARM_OP_FP)
64         value.setType(Double.TYPE);
65       if (type == ARM_OP_PIMM || type == ARM_OP_IMM || type == ARM_OP_CIMM)
66         value.setType(Integer.TYPE);
67       if (type == ARM_OP_REG)
68         value.setType(Integer.TYPE);
69       if (type == ARM_OP_INVALID)
70         return;
71       readField("value");
72       readField("shift");
73       readField("subtracted");
74     }
75 
76     @Override
getFieldOrder()77     public List getFieldOrder() {
78       return Arrays.asList("vector_index", "shift", "type", "value", "subtracted");
79     }
80   }
81 
82   public static class UnionOpInfo extends Capstone.UnionOpInfo {
83     public boolean usermode;
84     public int vector_size;
85     public int vector_data;
86     public int cps_mode;
87     public int cps_flag;
88     public int cc;
89     public byte update_flags;
90     public byte writeback;
91     public byte mem_barrier;
92     public byte op_count;
93 
94     public Operand [] op;
95 
UnionOpInfo()96     public UnionOpInfo() {
97       op = new Operand[36];
98     }
99 
read()100     public void read() {
101       readField("usermode");
102       readField("vector_size");
103       readField("vector_data");
104       readField("cps_mode");
105       readField("cps_flag");
106       readField("cc");
107       readField("update_flags");
108       readField("writeback");
109       readField("mem_barrier");
110       readField("op_count");
111       op = new Operand[op_count];
112       if (op_count != 0)
113         readField("op");
114     }
115 
116     @Override
getFieldOrder()117     public List getFieldOrder() {
118       return Arrays.asList("usermode", "vector_size", "vector_data",
119           "cps_mode", "cps_flag", "cc", "update_flags", "writeback", "mem_barrier", "op_count", "op");
120     }
121   }
122 
123   public static class OpInfo extends Capstone.OpInfo {
124     public boolean usermode;
125     public int vectorSize;
126     public int vectorData;
127     public int cpsMode;
128     public int cpsFlag;
129     public int cc;
130     public boolean updateFlags;
131     public boolean writeback;
132     public int memBarrier;
133     public Operand [] op = null;
134 
OpInfo(UnionOpInfo op_info)135     public OpInfo(UnionOpInfo op_info) {
136       usermode = op_info.usermode;
137       vectorSize = op_info.vector_size;
138       vectorData = op_info.vector_data;
139       cpsMode = op_info.cps_mode;
140       cpsFlag = op_info.cps_flag;
141       cc = op_info.cc;
142       updateFlags = (op_info.update_flags > 0);
143       writeback = (op_info.writeback > 0);
144       memBarrier = op_info.mem_barrier;
145       op = op_info.op;
146     }
147   }
148 }
149