Coverage Report

Created: 2023-09-25 06:24

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