/src/capstonenext/MCInst.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Capstone Disassembly Engine */ |
2 | | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2019 */ |
3 | | #if defined(CAPSTONE_HAS_OSXKERNEL) |
4 | | #include <Availability.h> |
5 | | #include <libkern/libkern.h> |
6 | | #else |
7 | | #include <stdio.h> |
8 | | #include <stdlib.h> |
9 | | #endif |
10 | | #include <string.h> |
11 | | #include <assert.h> |
12 | | |
13 | | #include "MCInstrDesc.h" |
14 | | #include "MCInst.h" |
15 | | #include "utils.h" |
16 | | |
17 | 4.09M | #define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1) |
18 | | |
19 | | void MCInst_Init(MCInst *inst) |
20 | 7.05M | { |
21 | | // unnecessary to initialize in loop . its expensive and inst->size should be honored |
22 | 7.05M | inst->Operands[0].Kind = kInvalid; |
23 | 7.05M | inst->Operands[0].ImmVal = 0; |
24 | | |
25 | 7.05M | inst->Opcode = 0; |
26 | 7.05M | inst->OpcodePub = 0; |
27 | 7.05M | inst->size = 0; |
28 | 7.05M | inst->has_imm = false; |
29 | 7.05M | inst->op1_size = 0; |
30 | 7.05M | inst->ac_idx = 0; |
31 | 7.05M | inst->popcode_adjust = 0; |
32 | 7.05M | inst->assembly[0] = '\0'; |
33 | 7.05M | inst->wasm_data.type = WASM_OP_INVALID; |
34 | 7.05M | inst->xAcquireRelease = 0; |
35 | 345M | for (int i = 0; i < MAX_MC_OPS; ++i) |
36 | 338M | inst->tied_op_idx[i] = -1; |
37 | 7.05M | inst->isAliasInstr = false; |
38 | 7.05M | inst->fillDetailOps = false; |
39 | 7.05M | memset(&inst->hppa_ext, 0, sizeof(inst->hppa_ext)); |
40 | 7.05M | } |
41 | | |
42 | | void MCInst_clear(MCInst *inst) |
43 | 7.01M | { |
44 | 7.01M | inst->size = 0; |
45 | 7.01M | } |
46 | | |
47 | | // does not free @Op |
48 | | void MCInst_insert0(MCInst *inst, int index, MCOperand *Op) |
49 | 4.11M | { |
50 | 4.11M | assert(index < MAX_MC_OPS); |
51 | 4.11M | int i; |
52 | | |
53 | 7.11M | for(i = inst->size; i > index; i--) |
54 | | //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand)); |
55 | 3.00M | inst->Operands[i] = inst->Operands[i-1]; |
56 | | |
57 | 4.11M | inst->Operands[index] = *Op; |
58 | 4.11M | inst->size++; |
59 | 4.11M | } |
60 | | |
61 | | void MCInst_setOpcode(MCInst *inst, unsigned Op) |
62 | 7.02M | { |
63 | 7.02M | inst->Opcode = Op; |
64 | 7.02M | } |
65 | | |
66 | | void MCInst_setOpcodePub(MCInst *inst, unsigned Op) |
67 | 155k | { |
68 | 155k | inst->OpcodePub = Op; |
69 | 155k | } |
70 | | |
71 | | unsigned MCInst_getOpcode(const MCInst *inst) |
72 | 57.4M | { |
73 | 57.4M | return inst->Opcode; |
74 | 57.4M | } |
75 | | |
76 | | unsigned MCInst_getOpcodePub(const MCInst *inst) |
77 | 7.29M | { |
78 | 7.29M | return inst->OpcodePub; |
79 | 7.29M | } |
80 | | |
81 | | MCOperand *MCInst_getOperand(MCInst *inst, unsigned i) |
82 | 33.9M | { |
83 | 33.9M | assert(i < MAX_MC_OPS); |
84 | 33.9M | return &inst->Operands[i]; |
85 | 33.9M | } |
86 | | |
87 | | unsigned MCInst_getNumOperands(const MCInst *inst) |
88 | 15.3M | { |
89 | 15.3M | return inst->size; |
90 | 15.3M | } |
91 | | |
92 | | // This addOperand2 function doesn't free Op |
93 | | void MCInst_addOperand2(MCInst *inst, MCOperand *Op) |
94 | 1.43k | { |
95 | 1.43k | assert(inst->size < MAX_MC_OPS); |
96 | 1.43k | inst->Operands[inst->size] = *Op; |
97 | | |
98 | 1.43k | inst->size++; |
99 | 1.43k | } |
100 | | |
101 | | bool MCOperand_isValid(const MCOperand *op) |
102 | 0 | { |
103 | 0 | return op->Kind != kInvalid; |
104 | 0 | } |
105 | | |
106 | | bool MCOperand_isReg(const MCOperand *op) |
107 | 12.4M | { |
108 | 12.4M | return op->Kind == kRegister || op->MachineOperandType == kRegister; |
109 | 12.4M | } |
110 | | |
111 | | bool MCOperand_isImm(const MCOperand *op) |
112 | 3.77M | { |
113 | 3.77M | return op->Kind == kImmediate || op->MachineOperandType == kImmediate; |
114 | 3.77M | } |
115 | | |
116 | | bool MCOperand_isFPImm(const MCOperand *op) |
117 | 269 | { |
118 | 269 | return op->Kind == kFPImmediate; |
119 | 269 | } |
120 | | |
121 | | bool MCOperand_isDFPImm(const MCOperand *op) |
122 | 1.91k | { |
123 | 1.91k | return op->Kind == kDFPImmediate; |
124 | 1.91k | } |
125 | | |
126 | | bool MCOperand_isExpr(const MCOperand *op) |
127 | 136k | { |
128 | 136k | return op->Kind == kExpr; |
129 | 136k | } |
130 | | |
131 | | bool MCOperand_isInst(const MCOperand *op) |
132 | 0 | { |
133 | 0 | return op->Kind == kInst; |
134 | 0 | } |
135 | | |
136 | | /// getReg - Returns the register number. |
137 | | unsigned MCOperand_getReg(const MCOperand *op) |
138 | 23.8M | { |
139 | 23.8M | return op->RegVal; |
140 | 23.8M | } |
141 | | |
142 | | /// setReg - Set the register number. |
143 | | void MCOperand_setReg(MCOperand *op, unsigned Reg) |
144 | 22.9k | { |
145 | 22.9k | op->RegVal = Reg; |
146 | 22.9k | } |
147 | | |
148 | | int64_t MCOperand_getImm(const MCOperand *op) |
149 | 12.0M | { |
150 | 12.0M | return op->ImmVal; |
151 | 12.0M | } |
152 | | |
153 | | void MCOperand_setImm(MCOperand *op, int64_t Val) |
154 | 29.1k | { |
155 | 29.1k | op->ImmVal = Val; |
156 | 29.1k | } |
157 | | |
158 | | double MCOperand_getFPImm(const MCOperand *op) |
159 | 0 | { |
160 | 0 | return op->FPImmVal; |
161 | 0 | } |
162 | | |
163 | | void MCOperand_setFPImm(MCOperand *op, double Val) |
164 | 0 | { |
165 | 0 | op->FPImmVal = Val; |
166 | 0 | } |
167 | | |
168 | | MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg) |
169 | 2.36M | { |
170 | 2.36M | MCOperand *op = &(mcInst->Operands[MCINST_CACHE]); |
171 | | |
172 | 2.36M | op->MachineOperandType = kRegister; |
173 | 2.36M | op->Kind = kRegister; |
174 | 2.36M | op->RegVal = Reg; |
175 | | |
176 | 2.36M | return op; |
177 | 2.36M | } |
178 | | |
179 | | void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg) |
180 | 12.9M | { |
181 | 12.9M | MCOperand *op = &(mcInst->Operands[mcInst->size]); |
182 | 12.9M | mcInst->size++; |
183 | | |
184 | 12.9M | op->MachineOperandType = kRegister; |
185 | 12.9M | op->Kind = kRegister; |
186 | 12.9M | op->RegVal = Reg; |
187 | 12.9M | } |
188 | | |
189 | | MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val) |
190 | 1.73M | { |
191 | 1.73M | MCOperand *op = &(mcInst->Operands[MCINST_CACHE]); |
192 | | |
193 | 1.73M | op->MachineOperandType = kImmediate; |
194 | 1.73M | op->Kind = kImmediate; |
195 | 1.73M | op->ImmVal = Val; |
196 | | |
197 | 1.73M | return op; |
198 | 1.73M | } |
199 | | |
200 | | void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val) |
201 | 6.07M | { |
202 | 6.07M | assert(mcInst->size < MAX_MC_OPS); |
203 | 6.07M | MCOperand *op = &(mcInst->Operands[mcInst->size]); |
204 | 6.07M | mcInst->size++; |
205 | | |
206 | 6.07M | op->MachineOperandType = kImmediate; |
207 | 6.07M | op->Kind = kImmediate; |
208 | 6.07M | op->ImmVal = Val; |
209 | 6.07M | } |
210 | | |
211 | | /// Check if any operand of the MCInstrDesc is predicable |
212 | | bool MCInst_isPredicable(const MCInstrDesc *MIDesc) |
213 | 1.28M | { |
214 | 1.28M | const MCOperandInfo *OpInfo = MIDesc->OpInfo; |
215 | 1.28M | unsigned NumOps = MIDesc->NumOperands; |
216 | 5.62M | for (unsigned i = 0; i < NumOps; ++i) { |
217 | 5.54M | if (MCOperandInfo_isPredicate(&OpInfo[i])) { |
218 | 1.20M | return true; |
219 | 1.20M | } |
220 | 5.54M | } |
221 | 79.7k | return false; |
222 | 1.28M | } |
223 | | |
224 | | /// Checks if tied operands exist in the instruction and sets |
225 | | /// - The writeback flag in detail |
226 | | /// - Saves the indices of the tied destination operands. |
227 | | void MCInst_handleWriteback(MCInst *MI, const MCInstrDesc *InstDescTable, unsigned tbl_size) |
228 | 1.82M | { |
229 | 1.82M | const MCInstrDesc *InstDesc = NULL; |
230 | 1.82M | const MCOperandInfo *OpInfo = NULL; |
231 | 1.82M | unsigned short NumOps = 0; |
232 | 1.82M | if (MI->csh->arch == CS_ARCH_ARM) { |
233 | | // Uses old (pre LLVM 18) indexing method. |
234 | 1.48M | InstDesc = &InstDescTable[MCInst_getOpcode(MI)]; |
235 | 1.48M | OpInfo = InstDescTable[MCInst_getOpcode(MI)].OpInfo; |
236 | 1.48M | NumOps = InstDescTable[MCInst_getOpcode(MI)].NumOperands; |
237 | 1.48M | } else { |
238 | 345k | InstDesc = MCInstrDesc_get(MCInst_getOpcode(MI), InstDescTable, tbl_size); |
239 | 345k | OpInfo = MCInstrDesc_get(MCInst_getOpcode(MI), InstDescTable, tbl_size)->OpInfo; |
240 | 345k | NumOps = MCInstrDesc_get(MCInst_getOpcode(MI), InstDescTable, tbl_size)->NumOperands; |
241 | 345k | } |
242 | | |
243 | 10.4M | for (unsigned i = 0; i < NumOps; ++i) { |
244 | 8.57M | if (MCOperandInfo_isTiedToOp(&OpInfo[i])) { |
245 | 426k | int idx = MCOperandInfo_getOperandConstraint( |
246 | 426k | InstDesc, i, |
247 | 426k | MCOI_TIED_TO); |
248 | | |
249 | 426k | if (idx == -1) |
250 | 0 | continue; |
251 | | |
252 | 426k | if (i >= MAX_MC_OPS) { |
253 | 0 | assert(0 && |
254 | 0 | "Maximum number of MC operands reached."); |
255 | 0 | } |
256 | 426k | MI->tied_op_idx[i] = idx; |
257 | | |
258 | 426k | if (MI->flat_insn->detail) |
259 | 426k | MI->flat_insn->detail->writeback = true; |
260 | 426k | } |
261 | 8.57M | } |
262 | 1.82M | } |
263 | | |
264 | | /// Check if operand with OpNum is tied by another operand |
265 | | /// (operand is tying destination). |
266 | | bool MCInst_opIsTied(const MCInst *MI, unsigned OpNum) |
267 | 6.08M | { |
268 | 6.08M | assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded."); |
269 | 284M | for (int i = 0; i < MAX_MC_OPS; ++i) { |
270 | 279M | if (MI->tied_op_idx[i] == OpNum) |
271 | 287k | return true; |
272 | 279M | } |
273 | 5.79M | return false; |
274 | 6.08M | } |
275 | | |
276 | | /// Check if operand with OpNum is tying another operand |
277 | | /// (operand is tying src). |
278 | | bool MCInst_opIsTying(const MCInst *MI, unsigned OpNum) |
279 | 6.02M | { |
280 | 6.02M | assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded."); |
281 | 6.02M | return MI->tied_op_idx[OpNum] != -1; |
282 | 6.02M | } |
283 | | |
284 | | /// Returns the value of the @MCInst operand at index @OpNum. |
285 | | uint64_t MCInst_getOpVal(MCInst *MI, unsigned OpNum) |
286 | 6.01M | { |
287 | 6.01M | assert(OpNum < MAX_MC_OPS); |
288 | 6.01M | MCOperand *op = MCInst_getOperand(MI, OpNum); |
289 | 6.01M | if (MCOperand_isReg(op)) |
290 | 4.36M | return MCOperand_getReg(op); |
291 | 1.65M | else if (MCOperand_isImm(op)) |
292 | 1.65M | return MCOperand_getImm(op); |
293 | 0 | else |
294 | 0 | assert(0 && "Operand type not handled in this getter."); |
295 | 0 | return MCOperand_getImm(op); |
296 | 6.01M | } |
297 | | |
298 | 1.88M | void MCInst_setIsAlias(MCInst *MI, bool Flag) { |
299 | 1.88M | assert(MI); |
300 | 1.88M | MI->isAliasInstr = Flag; |
301 | 1.88M | MI->flat_insn->is_alias = Flag; |
302 | 1.88M | } |
303 | | |
304 | | /// @brief Copies the relevant members of a temporary MCInst to |
305 | | /// the main MCInst. This is used if TryDecode was run on a temporary MCInst. |
306 | | /// @param MI The main MCInst |
307 | | /// @param TmpMI The temporary MCInst. |
308 | 0 | void MCInst_updateWithTmpMI(MCInst *MI, MCInst *TmpMI) { |
309 | 0 | MI->size = TmpMI->size; |
310 | 0 | MI->Opcode = TmpMI->Opcode; |
311 | 0 | assert(MI->size < MAX_MC_OPS); |
312 | 0 | memcpy(MI->Operands, TmpMI->Operands, sizeof(MI->Operands[0]) * MI->size); |
313 | 0 | } |