Coverage Report

Created: 2024-09-08 06:22

/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.80M
#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
18
19
void MCInst_Init(MCInst *inst)
20
8.30M
{
21
  // unnecessary to initialize in loop . its expensive and inst->size should be honored
22
8.30M
  inst->Operands[0].Kind = kInvalid;
23
8.30M
  inst->Operands[0].ImmVal = 0;
24
25
8.30M
  inst->Opcode = 0;
26
8.30M
  inst->OpcodePub = 0;
27
8.30M
  inst->size = 0;
28
8.30M
  inst->has_imm = false;
29
8.30M
  inst->op1_size = 0;
30
8.30M
  inst->ac_idx = 0;
31
8.30M
  inst->popcode_adjust = 0;
32
8.30M
  inst->assembly[0] = '\0';
33
8.30M
  inst->wasm_data.type = WASM_OP_INVALID;
34
8.30M
  inst->xAcquireRelease = 0;
35
406M
  for (int i = 0; i < MAX_MC_OPS; ++i)
36
398M
    inst->tied_op_idx[i] = -1;
37
8.30M
  inst->isAliasInstr = false;
38
8.30M
  inst->fillDetailOps = false;
39
8.30M
  memset(&inst->hppa_ext, 0, sizeof(inst->hppa_ext));
40
8.30M
}
41
42
void MCInst_clear(MCInst *inst)
43
8.68M
{
44
8.68M
  inst->size = 0;
45
8.68M
}
46
47
// does not free @Op
48
void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
49
4.81M
{
50
4.81M
  assert(index < MAX_MC_OPS);
51
4.81M
  int i;
52
53
8.28M
  for(i = inst->size; i > index; i--)
54
    //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
55
3.46M
    inst->Operands[i] = inst->Operands[i-1];
56
57
4.81M
  inst->Operands[index] = *Op;
58
4.81M
  inst->size++;
59
4.81M
}
60
61
void MCInst_setOpcode(MCInst *inst, unsigned Op)
62
8.28M
{
63
8.28M
  inst->Opcode = Op;
64
8.28M
}
65
66
void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
67
176k
{
68
176k
  inst->OpcodePub = Op;
69
176k
}
70
71
unsigned MCInst_getOpcode(const MCInst *inst)
72
65.0M
{
73
65.0M
  return inst->Opcode;
74
65.0M
}
75
76
unsigned MCInst_getOpcodePub(const MCInst *inst)
77
8.56M
{
78
8.56M
  return inst->OpcodePub;
79
8.56M
}
80
81
MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
82
38.7M
{
83
38.7M
  assert(i < MAX_MC_OPS);
84
38.7M
  return &inst->Operands[i];
85
38.7M
}
86
87
unsigned MCInst_getNumOperands(const MCInst *inst)
88
17.0M
{
89
17.0M
  return inst->size;
90
17.0M
}
91
92
// This addOperand2 function doesn't free Op
93
void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
94
2.89k
{
95
2.89k
  assert(inst->size < MAX_MC_OPS);
96
2.89k
  inst->Operands[inst->size] = *Op;
97
98
2.89k
  inst->size++;
99
2.89k
}
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.6M
{
108
12.6M
  return op->Kind == kRegister || op->MachineOperandType == kRegister;
109
12.6M
}
110
111
bool MCOperand_isImm(const MCOperand *op)
112
3.80M
{
113
3.80M
  return op->Kind == kImmediate || op->MachineOperandType == kImmediate;
114
3.80M
}
115
116
bool MCOperand_isFPImm(const MCOperand *op)
117
413
{
118
413
  return op->Kind == kFPImmediate;
119
413
}
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
27.5M
{
139
27.5M
  return op->RegVal;
140
27.5M
}
141
142
/// setReg - Set the register number.
143
void MCOperand_setReg(MCOperand *op, unsigned Reg)
144
25.5k
{
145
25.5k
  op->RegVal = Reg;
146
25.5k
}
147
148
int64_t MCOperand_getImm(const MCOperand *op)
149
13.6M
{
150
13.6M
  return op->ImmVal;
151
13.6M
}
152
153
void MCOperand_setImm(MCOperand *op, int64_t Val)
154
37.4k
{
155
37.4k
  op->ImmVal = Val;
156
37.4k
}
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.75M
{
170
2.75M
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
171
172
2.75M
  op->MachineOperandType = kRegister;
173
2.75M
  op->Kind = kRegister;
174
2.75M
  op->RegVal = Reg;
175
176
2.75M
  return op;
177
2.75M
}
178
179
void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
180
15.2M
{
181
15.2M
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
182
15.2M
  mcInst->size++;
183
184
15.2M
  op->MachineOperandType = kRegister;
185
15.2M
  op->Kind = kRegister;
186
15.2M
  op->RegVal = Reg;
187
15.2M
}
188
189
MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
190
2.04M
{
191
2.04M
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
192
193
2.04M
  op->MachineOperandType = kImmediate;
194
2.04M
  op->Kind = kImmediate;
195
2.04M
  op->ImmVal = Val;
196
197
2.04M
  return op;
198
2.04M
}
199
200
void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
201
7.16M
{
202
7.16M
  assert(mcInst->size < MAX_MC_OPS);
203
7.16M
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
204
7.16M
  mcInst->size++;
205
206
7.16M
  op->MachineOperandType = kImmediate;
207
7.16M
  op->Kind = kImmediate;
208
7.16M
  op->ImmVal = Val;
209
7.16M
}
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.27M
{
268
6.27M
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
269
294M
  for (int i = 0; i < MAX_MC_OPS; ++i) {
270
288M
    if (MI->tied_op_idx[i] == OpNum)
271
287k
      return true;
272
288M
  }
273
5.98M
  return false;
274
6.27M
}
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.21M
{
280
6.21M
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
281
6.21M
  return MI->tied_op_idx[OpNum] != -1;
282
6.21M
}
283
284
/// Returns the value of the @MCInst operand at index @OpNum.
285
uint64_t MCInst_getOpVal(MCInst *MI, unsigned OpNum)
286
6.19M
{
287
6.19M
  assert(OpNum < MAX_MC_OPS);
288
6.19M
  MCOperand *op = MCInst_getOperand(MI, OpNum);
289
6.19M
  if (MCOperand_isReg(op))
290
4.48M
    return MCOperand_getReg(op);
291
1.70M
  else if (MCOperand_isImm(op))
292
1.70M
    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.19M
}
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
}