Coverage Report

Created: 2026-03-11 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonev5/MCInst.c
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
2.39M
#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
18
19
void MCInst_Init(MCInst *inst)
20
2.60M
{
21
  // unnecessary to initialize in loop . its expensive and inst->size shuold be honored
22
2.60M
  inst->Operands[0].Kind = kInvalid;
23
2.60M
  inst->Operands[0].ImmVal = 0;
24
25
2.60M
  inst->Opcode = 0;
26
2.60M
  inst->OpcodePub = 0;
27
2.60M
  inst->size = 0;
28
2.60M
  inst->has_imm = false;
29
2.60M
  inst->op1_size = 0;
30
2.60M
  inst->writeback = false;
31
2.60M
  inst->ac_idx = 0;
32
2.60M
  inst->popcode_adjust = 0;
33
2.60M
  inst->assembly[0] = '\0';
34
2.60M
  inst->wasm_data.type = WASM_OP_INVALID;
35
2.60M
  inst->xAcquireRelease = 0;
36
127M
  for (int i = 0; i < MAX_MC_OPS; ++i)
37
125M
    inst->tied_op_idx[i] = -1;
38
2.60M
}
39
40
void MCInst_clear(MCInst *inst)
41
5.55M
{
42
5.55M
  inst->size = 0;
43
5.55M
}
44
45
// does not free @Op
46
void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
47
986k
{
48
986k
  assert(index < MAX_MC_OPS);
49
986k
  int i;
50
51
1.61M
  for(i = inst->size; i > index; i--)
52
    //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
53
627k
    inst->Operands[i] = inst->Operands[i-1];
54
55
986k
  inst->Operands[index] = *Op;
56
986k
  inst->size++;
57
986k
}
58
59
void MCInst_setOpcode(MCInst *inst, unsigned Op)
60
5.22M
{
61
5.22M
  inst->Opcode = Op;
62
5.22M
}
63
64
void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
65
173k
{
66
173k
  inst->OpcodePub = Op;
67
173k
}
68
69
unsigned MCInst_getOpcode(const MCInst *inst)
70
37.6M
{
71
37.6M
  return inst->Opcode;
72
37.6M
}
73
74
unsigned MCInst_getOpcodePub(const MCInst *inst)
75
5.42M
{
76
5.42M
  return inst->OpcodePub;
77
5.42M
}
78
79
MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
80
26.4M
{
81
26.4M
  assert(i < MAX_MC_OPS);
82
26.4M
  return &inst->Operands[i];
83
26.4M
}
84
85
unsigned MCInst_getNumOperands(const MCInst *inst)
86
9.60M
{
87
9.60M
  return inst->size;
88
9.60M
}
89
90
// This addOperand2 function doesnt free Op
91
void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
92
3.12k
{
93
3.12k
  assert(inst->size < MAX_MC_OPS);
94
3.12k
  inst->Operands[inst->size] = *Op;
95
96
3.12k
  inst->size++;
97
3.12k
}
98
99
bool MCOperand_isValid(const MCOperand *op)
100
0
{
101
0
  return op->Kind != kInvalid;
102
0
}
103
104
bool MCOperand_isReg(const MCOperand *op)
105
4.48M
{
106
4.48M
  return op->Kind == kRegister;
107
4.48M
}
108
109
bool MCOperand_isImm(const MCOperand *op)
110
1.87M
{
111
1.87M
  return op->Kind == kImmediate;
112
1.87M
}
113
114
bool MCOperand_isFPImm(const MCOperand *op)
115
1.49k
{
116
1.49k
  return op->Kind == kFPImmediate;
117
1.49k
}
118
119
bool MCOperand_isDFPImm(const MCOperand *op)
120
918
{
121
918
  return op->Kind == kDFPImmediate;
122
918
}
123
124
bool MCOperand_isExpr(const MCOperand *op)
125
111k
{
126
111k
  return op->Kind == kExpr;
127
111k
}
128
129
bool MCOperand_isInst(const MCOperand *op)
130
0
{
131
0
  return op->Kind == kInst;
132
0
}
133
134
/// getReg - Returns the register number.
135
unsigned MCOperand_getReg(const MCOperand *op)
136
17.3M
{
137
17.3M
  return op->RegVal;
138
17.3M
}
139
140
/// setReg - Set the register number.
141
void MCOperand_setReg(MCOperand *op, unsigned Reg)
142
17.6k
{
143
17.6k
  op->RegVal = Reg;
144
17.6k
}
145
146
int64_t MCOperand_getImm(MCOperand *op)
147
8.95M
{
148
8.95M
  return op->ImmVal;
149
8.95M
}
150
151
void MCOperand_setImm(MCOperand *op, int64_t Val)
152
27.2k
{
153
27.2k
  op->ImmVal = Val;
154
27.2k
}
155
156
double MCOperand_getFPImm(const MCOperand *op)
157
0
{
158
0
  return op->FPImmVal;
159
0
}
160
161
void MCOperand_setFPImm(MCOperand *op, double Val)
162
0
{
163
0
  op->FPImmVal = Val;
164
0
}
165
166
MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
167
1.36M
{
168
1.36M
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
169
170
1.36M
  op->MachineOperandType = kRegister;
171
1.36M
  op->Kind = kRegister;
172
1.36M
  op->RegVal = Reg;
173
174
1.36M
  return op;
175
1.36M
}
176
177
void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
178
9.77M
{
179
9.77M
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
180
9.77M
  mcInst->size++;
181
182
9.77M
  op->MachineOperandType = kRegister;
183
9.77M
  op->Kind = kRegister;
184
9.77M
  op->RegVal = Reg;
185
9.77M
}
186
187
MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
188
1.02M
{
189
1.02M
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
190
191
1.02M
  op->MachineOperandType = kImmediate;
192
1.02M
  op->Kind = kImmediate;
193
1.02M
  op->ImmVal = Val;
194
195
1.02M
  return op;
196
1.02M
}
197
198
void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
199
4.57M
{
200
4.57M
  assert(mcInst->size < MAX_MC_OPS);
201
4.57M
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
202
4.57M
  mcInst->size++;
203
204
4.57M
  op->MachineOperandType = kImmediate;
205
4.57M
  op->Kind = kImmediate;
206
4.57M
  op->ImmVal = Val;
207
4.57M
}
208
209
/// Check if any operand of the MCInstrDesc is predicable
210
bool MCInst_isPredicable(const MCInstrDesc *MIDesc)
211
713k
{
212
713k
  const MCOperandInfo *OpInfo = MIDesc->OpInfo;
213
713k
  unsigned NumOps = MIDesc->NumOperands;
214
3.13M
  for (unsigned i = 0; i < NumOps; ++i) {
215
3.09M
    if (MCOperandInfo_isPredicate(&OpInfo[i])) {
216
671k
      return true;
217
671k
    }
218
3.09M
  }
219
42.0k
  return false;
220
713k
}
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
1.02M
{
227
1.02M
  const MCOperandInfo *OpInfo = InstDesc[MCInst_getOpcode(MI)].OpInfo;
228
1.02M
  unsigned short NumOps = InstDesc[MCInst_getOpcode(MI)].NumOperands;
229
230
1.02M
  unsigned i;
231
5.87M
  for (i = 0; i < NumOps; ++i) {
232
4.85M
    if (MCOperandInfo_isTiedToOp(&OpInfo[i])) {
233
238k
      int idx = MCOperandInfo_getOperandConstraint(
234
238k
        &InstDesc[MCInst_getOpcode(MI)], i,
235
238k
        MCOI_TIED_TO);
236
237
238k
      if (idx == -1)
238
0
        continue;
239
240
238k
      if (i >= MAX_MC_OPS) {
241
0
        assert(0 &&
242
0
               "Maximum number of MC operands reached.");
243
0
      }
244
238k
      MI->tied_op_idx[i] = idx;
245
246
238k
      if (MI->flat_insn->detail)
247
238k
        MI->flat_insn->detail->writeback = true;
248
238k
    }
249
4.85M
  }
250
1.02M
}
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
4.18M
{
256
4.18M
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
257
197M
  for (int i = 0; i < MAX_MC_OPS; ++i) {
258
193M
    if (MI->tied_op_idx[i] == OpNum)
259
157k
      return true;
260
193M
  }
261
4.02M
  return false;
262
4.18M
}
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
4.18M
{
268
4.18M
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
269
4.18M
  return MI->tied_op_idx[OpNum] != -1;
270
4.18M
}