Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/capstone/MCInst.c
4387 views
1
/* Capstone Disassembly Engine */
2
/* By Nguyen Anh Quynh <[email protected]>, 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
#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
18
19
void MCInst_Init(MCInst *inst)
20
{
21
// unnecessary to initialize in loop . its expensive and inst->size shuold be honored
22
inst->Operands[0].Kind = kInvalid;
23
inst->Operands[0].ImmVal = 0;
24
25
inst->Opcode = 0;
26
inst->OpcodePub = 0;
27
inst->size = 0;
28
inst->has_imm = false;
29
inst->op1_size = 0;
30
inst->writeback = false;
31
inst->ac_idx = 0;
32
inst->popcode_adjust = 0;
33
inst->assembly[0] = '\0';
34
inst->wasm_data.type = WASM_OP_INVALID;
35
inst->xAcquireRelease = 0;
36
for (int i = 0; i < MAX_MC_OPS; ++i)
37
inst->tied_op_idx[i] = -1;
38
}
39
40
void MCInst_clear(MCInst *inst)
41
{
42
inst->size = 0;
43
}
44
45
// does not free @Op
46
void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
47
{
48
assert(index < MAX_MC_OPS);
49
int i;
50
51
for(i = inst->size; i > index; i--)
52
//memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
53
inst->Operands[i] = inst->Operands[i-1];
54
55
inst->Operands[index] = *Op;
56
inst->size++;
57
}
58
59
void MCInst_setOpcode(MCInst *inst, unsigned Op)
60
{
61
inst->Opcode = Op;
62
}
63
64
void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
65
{
66
inst->OpcodePub = Op;
67
}
68
69
unsigned MCInst_getOpcode(const MCInst *inst)
70
{
71
return inst->Opcode;
72
}
73
74
unsigned MCInst_getOpcodePub(const MCInst *inst)
75
{
76
return inst->OpcodePub;
77
}
78
79
MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
80
{
81
assert(i < MAX_MC_OPS);
82
return &inst->Operands[i];
83
}
84
85
unsigned MCInst_getNumOperands(const MCInst *inst)
86
{
87
return inst->size;
88
}
89
90
// This addOperand2 function doesnt free Op
91
void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
92
{
93
assert(inst->size < MAX_MC_OPS);
94
inst->Operands[inst->size] = *Op;
95
96
inst->size++;
97
}
98
99
bool MCOperand_isValid(const MCOperand *op)
100
{
101
return op->Kind != kInvalid;
102
}
103
104
bool MCOperand_isReg(const MCOperand *op)
105
{
106
return op->Kind == kRegister;
107
}
108
109
bool MCOperand_isImm(const MCOperand *op)
110
{
111
return op->Kind == kImmediate;
112
}
113
114
bool MCOperand_isFPImm(const MCOperand *op)
115
{
116
return op->Kind == kFPImmediate;
117
}
118
119
bool MCOperand_isDFPImm(const MCOperand *op)
120
{
121
return op->Kind == kDFPImmediate;
122
}
123
124
bool MCOperand_isExpr(const MCOperand *op)
125
{
126
return op->Kind == kExpr;
127
}
128
129
bool MCOperand_isInst(const MCOperand *op)
130
{
131
return op->Kind == kInst;
132
}
133
134
/// getReg - Returns the register number.
135
unsigned MCOperand_getReg(const MCOperand *op)
136
{
137
return op->RegVal;
138
}
139
140
/// setReg - Set the register number.
141
void MCOperand_setReg(MCOperand *op, unsigned Reg)
142
{
143
op->RegVal = Reg;
144
}
145
146
int64_t MCOperand_getImm(MCOperand *op)
147
{
148
return op->ImmVal;
149
}
150
151
void MCOperand_setImm(MCOperand *op, int64_t Val)
152
{
153
op->ImmVal = Val;
154
}
155
156
double MCOperand_getFPImm(const MCOperand *op)
157
{
158
return op->FPImmVal;
159
}
160
161
void MCOperand_setFPImm(MCOperand *op, double Val)
162
{
163
op->FPImmVal = Val;
164
}
165
166
MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
167
{
168
MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
169
170
op->MachineOperandType = kRegister;
171
op->Kind = kRegister;
172
op->RegVal = Reg;
173
174
return op;
175
}
176
177
void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
178
{
179
MCOperand *op = &(mcInst->Operands[mcInst->size]);
180
mcInst->size++;
181
182
op->MachineOperandType = kRegister;
183
op->Kind = kRegister;
184
op->RegVal = Reg;
185
}
186
187
MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
188
{
189
MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
190
191
op->MachineOperandType = kImmediate;
192
op->Kind = kImmediate;
193
op->ImmVal = Val;
194
195
return op;
196
}
197
198
void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
199
{
200
assert(mcInst->size < MAX_MC_OPS);
201
MCOperand *op = &(mcInst->Operands[mcInst->size]);
202
mcInst->size++;
203
204
op->MachineOperandType = kImmediate;
205
op->Kind = kImmediate;
206
op->ImmVal = Val;
207
}
208
209
/// Check if any operand of the MCInstrDesc is predicable
210
bool MCInst_isPredicable(const MCInstrDesc *MIDesc)
211
{
212
const MCOperandInfo *OpInfo = MIDesc->OpInfo;
213
unsigned NumOps = MIDesc->NumOperands;
214
for (unsigned i = 0; i < NumOps; ++i) {
215
if (MCOperandInfo_isPredicate(&OpInfo[i])) {
216
return true;
217
}
218
}
219
return false;
220
}
221
222
/// Checks if tied operands exist in the instruction and sets
223
/// - The writeback flag in detail
224
/// - Saves the indices of the tied destination operands.
225
void MCInst_handleWriteback(MCInst *MI, const MCInstrDesc *InstDesc)
226
{
227
const MCOperandInfo *OpInfo = InstDesc[MCInst_getOpcode(MI)].OpInfo;
228
unsigned short NumOps = InstDesc[MCInst_getOpcode(MI)].NumOperands;
229
230
unsigned i;
231
for (i = 0; i < NumOps; ++i) {
232
if (MCOperandInfo_isTiedToOp(&OpInfo[i])) {
233
int idx = MCOperandInfo_getOperandConstraint(
234
&InstDesc[MCInst_getOpcode(MI)], i,
235
MCOI_TIED_TO);
236
237
if (idx == -1)
238
continue;
239
240
if (i >= MAX_MC_OPS) {
241
assert(0 &&
242
"Maximum number of MC operands reached.");
243
}
244
MI->tied_op_idx[i] = idx;
245
246
if (MI->flat_insn->detail)
247
MI->flat_insn->detail->writeback = true;
248
}
249
}
250
}
251
252
/// Check if operand with OpNum is tied by another operand
253
/// (operand is tying destination).
254
bool MCInst_opIsTied(const MCInst *MI, unsigned OpNum)
255
{
256
assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
257
for (int i = 0; i < MAX_MC_OPS; ++i) {
258
if (MI->tied_op_idx[i] == OpNum)
259
return true;
260
}
261
return false;
262
}
263
264
/// Check if operand with OpNum is tying another operand
265
/// (operand is tying src).
266
bool MCInst_opIsTying(const MCInst *MI, unsigned OpNum)
267
{
268
assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
269
return MI->tied_op_idx[OpNum] != -1;
270
}
271
272