Coverage Report

Created: 2026-12-01 07:03

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