Line | Count | Source |
1 | | /* Capstone Disassembly Engine */ |
2 | | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */ |
3 | | |
4 | | #if defined(CAPSTONE_HAS_OSXKERNEL) |
5 | | #include <Availability.h> |
6 | | #include <libkern/libkern.h> |
7 | | #else |
8 | | #include <stdio.h> |
9 | | #include <stdlib.h> |
10 | | #endif |
11 | | #include <string.h> |
12 | | #include <assert.h> |
13 | | |
14 | | #include "MCInst.h" |
15 | | #include "utils.h" |
16 | | |
17 | 992k | #define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1) |
18 | | |
19 | | void MCInst_Init(MCInst *inst) |
20 | 2.57M | { |
21 | 2.57M | memset(inst, 0, sizeof(MCInst)); |
22 | 126M | for (int i = 0; i < MAX_MC_OPS; ++i) { |
23 | 123M | inst->tied_op_idx[i] = -1; |
24 | 123M | } |
25 | 2.57M | } |
26 | | |
27 | | void MCInst_clear(MCInst *inst) |
28 | 3.15M | { |
29 | 3.15M | inst->size = 0; |
30 | 3.15M | } |
31 | | |
32 | | // does not free @Op |
33 | | void MCInst_insert0(MCInst *inst, int index, MCOperand *Op) |
34 | 994k | { |
35 | 994k | assert(index < MAX_MC_OPS); |
36 | 994k | int i; |
37 | | |
38 | 1.61M | for(i = inst->size; i > index; i--) |
39 | | //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand)); |
40 | 625k | inst->Operands[i] = inst->Operands[i-1]; |
41 | | |
42 | 994k | inst->Operands[index] = *Op; |
43 | 994k | inst->size++; |
44 | 994k | } |
45 | | |
46 | | void MCInst_setOpcode(MCInst *inst, unsigned Op) |
47 | 2.58M | { |
48 | 2.58M | inst->Opcode = Op; |
49 | 2.58M | } |
50 | | |
51 | | void MCInst_setOpcodePub(MCInst *inst, unsigned Op) |
52 | 128k | { |
53 | 128k | inst->OpcodePub = Op; |
54 | 128k | } |
55 | | |
56 | | unsigned MCInst_getOpcode(const MCInst *inst) |
57 | 14.3M | { |
58 | 14.3M | return inst->Opcode; |
59 | 14.3M | } |
60 | | |
61 | | unsigned MCInst_getOpcodePub(const MCInst *inst) |
62 | 2.72M | { |
63 | 2.72M | return inst->OpcodePub; |
64 | 2.72M | } |
65 | | |
66 | | MCOperand *MCInst_getOperand(MCInst *inst, unsigned i) |
67 | 11.5M | { |
68 | 11.5M | assert(i < MAX_MC_OPS); |
69 | 11.5M | return &inst->Operands[i]; |
70 | 11.5M | } |
71 | | |
72 | | unsigned MCInst_getNumOperands(const MCInst *inst) |
73 | 3.17M | { |
74 | 3.17M | return inst->size; |
75 | 3.17M | } |
76 | | |
77 | | // This addOperand2 function doesnt free Op |
78 | | void MCInst_addOperand2(MCInst *inst, MCOperand *Op) |
79 | 1.82k | { |
80 | 1.82k | assert(inst->size < MAX_MC_OPS); |
81 | 1.82k | inst->Operands[inst->size] = *Op; |
82 | | |
83 | 1.82k | inst->size++; |
84 | 1.82k | } |
85 | | |
86 | | bool MCOperand_isValid(const MCOperand *op) |
87 | 0 | { |
88 | 0 | return op->Kind != kInvalid; |
89 | 0 | } |
90 | | |
91 | | bool MCOperand_isReg(const MCOperand *op) |
92 | 4.40M | { |
93 | 4.40M | return op->Kind == kRegister; |
94 | 4.40M | } |
95 | | |
96 | | bool MCOperand_isImm(const MCOperand *op) |
97 | 1.73M | { |
98 | 1.73M | return op->Kind == kImmediate; |
99 | 1.73M | } |
100 | | |
101 | | bool MCOperand_isFPImm(const MCOperand *op) |
102 | 1.44k | { |
103 | 1.44k | return op->Kind == kFPImmediate; |
104 | 1.44k | } |
105 | | |
106 | | bool MCOperand_isDFPImm(const MCOperand *op) |
107 | 0 | { |
108 | 0 | return op->Kind == kDFPImmediate; |
109 | 0 | } |
110 | | |
111 | | bool MCOperand_isExpr(const MCOperand *op) |
112 | 0 | { |
113 | 0 | return op->Kind == kExpr; |
114 | 0 | } |
115 | | |
116 | | bool MCOperand_isInst(const MCOperand *op) |
117 | 0 | { |
118 | 0 | return op->Kind == kInst; |
119 | 0 | } |
120 | | |
121 | | /// getReg - Returns the register number. |
122 | | unsigned MCOperand_getReg(const MCOperand *op) |
123 | 7.61M | { |
124 | 7.61M | return op->RegVal; |
125 | 7.61M | } |
126 | | |
127 | | /// setReg - Set the register number. |
128 | | void MCOperand_setReg(MCOperand *op, unsigned Reg) |
129 | 6.60k | { |
130 | 6.60k | op->RegVal = Reg; |
131 | 6.60k | } |
132 | | |
133 | | int64_t MCOperand_getImm(MCOperand *op) |
134 | 3.55M | { |
135 | 3.55M | return op->ImmVal; |
136 | 3.55M | } |
137 | | |
138 | | void MCOperand_setImm(MCOperand *op, int64_t Val) |
139 | 13.1k | { |
140 | 13.1k | op->ImmVal = Val; |
141 | 13.1k | } |
142 | | |
143 | | double MCOperand_getFPImm(const MCOperand *op) |
144 | 0 | { |
145 | 0 | return op->FPImmVal; |
146 | 0 | } |
147 | | |
148 | | void MCOperand_setFPImm(MCOperand *op, double Val) |
149 | 0 | { |
150 | 0 | op->FPImmVal = Val; |
151 | 0 | } |
152 | | |
153 | | MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg) |
154 | 556k | { |
155 | 556k | MCOperand *op = &(mcInst->Operands[MCINST_CACHE]); |
156 | | |
157 | 556k | op->MachineOperandType = kRegister; |
158 | 556k | op->Kind = kRegister; |
159 | 556k | op->RegVal = Reg; |
160 | | |
161 | 556k | return op; |
162 | 556k | } |
163 | | |
164 | | void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg) |
165 | 4.84M | { |
166 | 4.84M | MCOperand *op = &(mcInst->Operands[mcInst->size]); |
167 | 4.84M | mcInst->size++; |
168 | | |
169 | 4.84M | op->MachineOperandType = kRegister; |
170 | 4.84M | op->Kind = kRegister; |
171 | 4.84M | op->RegVal = Reg; |
172 | 4.84M | } |
173 | | |
174 | | MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val) |
175 | 436k | { |
176 | 436k | MCOperand *op = &(mcInst->Operands[MCINST_CACHE]); |
177 | | |
178 | 436k | op->MachineOperandType = kImmediate; |
179 | 436k | op->Kind = kImmediate; |
180 | 436k | op->ImmVal = Val; |
181 | | |
182 | 436k | return op; |
183 | 436k | } |
184 | | |
185 | | void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val) |
186 | 2.24M | { |
187 | 2.24M | assert(mcInst->size < MAX_MC_OPS); |
188 | 2.24M | MCOperand *op = &(mcInst->Operands[mcInst->size]); |
189 | 2.24M | mcInst->size++; |
190 | | |
191 | 2.24M | op->MachineOperandType = kImmediate; |
192 | 2.24M | op->Kind = kImmediate; |
193 | 2.24M | op->ImmVal = Val; |
194 | 2.24M | } |
195 | | |
196 | | /// Check if any operand of the MCInstrDesc is predicable |
197 | | bool MCInst_isPredicable(const MCInstrDesc *MIDesc) |
198 | 0 | { |
199 | 0 | const MCOperandInfo *OpInfo = MIDesc->OpInfo; |
200 | 0 | unsigned NumOps = MIDesc->NumOperands; |
201 | 0 | for (unsigned i = 0; i < NumOps; ++i) { |
202 | 0 | if (MCOperandInfo_isPredicate(&OpInfo[i])) { |
203 | 0 | return true; |
204 | 0 | } |
205 | 0 | } |
206 | 0 | return false; |
207 | 0 | } |
208 | | |
209 | | /// Checks if tied operands exist in the instruction and sets |
210 | | /// - The writeback flag in detail |
211 | | /// - Saves the indices of the tied destination operands. |
212 | | void MCInst_handleWriteback(MCInst *MI, const MCInstrDesc *InstDesc) |
213 | 0 | { |
214 | 0 | const MCOperandInfo *OpInfo = InstDesc[MCInst_getOpcode(MI)].OpInfo; |
215 | 0 | unsigned short NumOps = InstDesc[MCInst_getOpcode(MI)].NumOperands; |
216 | |
|
217 | 0 | unsigned i; |
218 | 0 | for (i = 0; i < NumOps; ++i) { |
219 | 0 | if (MCOperandInfo_isTiedToOp(&OpInfo[i])) { |
220 | 0 | int idx = MCOperandInfo_getOperandConstraint( |
221 | 0 | &InstDesc[MCInst_getOpcode(MI)], i, |
222 | 0 | MCOI_TIED_TO); |
223 | |
|
224 | 0 | if (idx == -1) |
225 | 0 | continue; |
226 | | |
227 | 0 | if (i >= MAX_MC_OPS) { |
228 | 0 | assert(0 && |
229 | 0 | "Maximum number of MC operands reached."); |
230 | 0 | } |
231 | 0 | MI->tied_op_idx[i] = idx; |
232 | |
|
233 | 0 | if (MI->flat_insn->detail) |
234 | 0 | MI->flat_insn->detail->writeback = true; |
235 | 0 | } |
236 | 0 | } |
237 | 0 | } |
238 | | |
239 | | /// Check if operand with OpNum is tied by another operand |
240 | | /// (operand is tying destination). |
241 | | bool MCInst_opIsTied(const MCInst *MI, unsigned OpNum) |
242 | 0 | { |
243 | 0 | assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded."); |
244 | 0 | for (int i = 0; i < MAX_MC_OPS; ++i) { |
245 | 0 | if (MI->tied_op_idx[i] == OpNum) |
246 | 0 | return true; |
247 | 0 | } |
248 | 0 | return false; |
249 | 0 | } |
250 | | |
251 | | /// Check if operand with OpNum is tying another operand |
252 | | /// (operand is tying src). |
253 | | bool MCInst_opIsTying(const MCInst *MI, unsigned OpNum) |
254 | 0 | { |
255 | 0 | assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded."); |
256 | 0 | return MI->tied_op_idx[OpNum] != -1; |
257 | 0 | } |