Coverage Report

Created: 2025-11-24 06:12

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.00M
#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
18
19
void MCInst_Init(MCInst *inst)
20
400k
{
21
  // unnecessary to initialize in loop . its expensive and inst->size shuold be honored
22
400k
  inst->Operands[0].Kind = kInvalid;
23
400k
  inst->Operands[0].ImmVal = 0;
24
25
400k
  inst->Opcode = 0;
26
400k
  inst->OpcodePub = 0;
27
400k
  inst->size = 0;
28
400k
  inst->has_imm = false;
29
400k
  inst->op1_size = 0;
30
400k
  inst->writeback = false;
31
400k
  inst->ac_idx = 0;
32
400k
  inst->popcode_adjust = 0;
33
400k
  inst->assembly[0] = '\0';
34
400k
  inst->wasm_data.type = WASM_OP_INVALID;
35
400k
  inst->xAcquireRelease = 0;
36
19.6M
  for (int i = 0; i < MAX_MC_OPS; ++i)
37
19.2M
    inst->tied_op_idx[i] = -1;
38
400k
}
39
40
void MCInst_clear(MCInst *inst)
41
3.81M
{
42
3.81M
  inst->size = 0;
43
3.81M
}
44
45
// does not free @Op
46
void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
47
149k
{
48
149k
  assert(index < MAX_MC_OPS);
49
149k
  int i;
50
51
230k
  for(i = inst->size; i > index; i--)
52
    //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
53
80.9k
    inst->Operands[i] = inst->Operands[i-1];
54
55
149k
  inst->Operands[index] = *Op;
56
149k
  inst->size++;
57
149k
}
58
59
void MCInst_setOpcode(MCInst *inst, unsigned Op)
60
3.93M
{
61
3.93M
  inst->Opcode = Op;
62
3.93M
}
63
64
void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
65
58.2k
{
66
58.2k
  inst->OpcodePub = Op;
67
58.2k
}
68
69
unsigned MCInst_getOpcode(const MCInst *inst)
70
36.3M
{
71
36.3M
  return inst->Opcode;
72
36.3M
}
73
74
unsigned MCInst_getOpcodePub(const MCInst *inst)
75
4.00M
{
76
4.00M
  return inst->OpcodePub;
77
4.00M
}
78
79
MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
80
24.1M
{
81
24.1M
  assert(i < MAX_MC_OPS);
82
24.1M
  return &inst->Operands[i];
83
24.1M
}
84
85
unsigned MCInst_getNumOperands(const MCInst *inst)
86
9.48M
{
87
9.48M
  return inst->size;
88
9.48M
}
89
90
// This addOperand2 function doesnt free Op
91
void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
92
731
{
93
731
  assert(inst->size < MAX_MC_OPS);
94
731
  inst->Operands[inst->size] = *Op;
95
96
731
  inst->size++;
97
731
}
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
780k
{
106
780k
  return op->Kind == kRegister;
107
780k
}
108
109
bool MCOperand_isImm(const MCOperand *op)
110
400k
{
111
400k
  return op->Kind == kImmediate;
112
400k
}
113
114
bool MCOperand_isFPImm(const MCOperand *op)
115
152
{
116
152
  return op->Kind == kFPImmediate;
117
152
}
118
119
bool MCOperand_isDFPImm(const MCOperand *op)
120
1.70k
{
121
1.70k
  return op->Kind == kDFPImmediate;
122
1.70k
}
123
124
bool MCOperand_isExpr(const MCOperand *op)
125
187k
{
126
187k
  return op->Kind == kExpr;
127
187k
}
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
15.7M
{
137
15.7M
  return op->RegVal;
138
15.7M
}
139
140
/// setReg - Set the register number.
141
void MCOperand_setReg(MCOperand *op, unsigned Reg)
142
19.7k
{
143
19.7k
  op->RegVal = Reg;
144
19.7k
}
145
146
int64_t MCOperand_getImm(MCOperand *op)
147
8.33M
{
148
8.33M
  return op->ImmVal;
149
8.33M
}
150
151
void MCOperand_setImm(MCOperand *op, int64_t Val)
152
22.6k
{
153
22.6k
  op->ImmVal = Val;
154
22.6k
}
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.15M
{
168
1.15M
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
169
170
1.15M
  op->MachineOperandType = kRegister;
171
1.15M
  op->Kind = kRegister;
172
1.15M
  op->RegVal = Reg;
173
174
1.15M
  return op;
175
1.15M
}
176
177
void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
178
7.82M
{
179
7.82M
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
180
7.82M
  mcInst->size++;
181
182
7.82M
  op->MachineOperandType = kRegister;
183
7.82M
  op->Kind = kRegister;
184
7.82M
  op->RegVal = Reg;
185
7.82M
}
186
187
MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
188
844k
{
189
844k
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
190
191
844k
  op->MachineOperandType = kImmediate;
192
844k
  op->Kind = kImmediate;
193
844k
  op->ImmVal = Val;
194
195
844k
  return op;
196
844k
}
197
198
void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
199
3.56M
{
200
3.56M
  assert(mcInst->size < MAX_MC_OPS);
201
3.56M
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
202
3.56M
  mcInst->size++;
203
204
3.56M
  op->MachineOperandType = kImmediate;
205
3.56M
  op->Kind = kImmediate;
206
3.56M
  op->ImmVal = Val;
207
3.56M
}
208
209
/// Check if any operand of the MCInstrDesc is predicable
210
bool MCInst_isPredicable(const MCInstrDesc *MIDesc)
211
949k
{
212
949k
  const MCOperandInfo *OpInfo = MIDesc->OpInfo;
213
949k
  unsigned NumOps = MIDesc->NumOperands;
214
4.27M
  for (unsigned i = 0; i < NumOps; ++i) {
215
4.20M
    if (MCOperandInfo_isPredicate(&OpInfo[i])) {
216
873k
      return true;
217
873k
    }
218
4.20M
  }
219
75.2k
  return false;
220
949k
}
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.52M
{
227
1.52M
  const MCOperandInfo *OpInfo = InstDesc[MCInst_getOpcode(MI)].OpInfo;
228
1.52M
  unsigned short NumOps = InstDesc[MCInst_getOpcode(MI)].NumOperands;
229
230
1.52M
  unsigned i;
231
8.67M
  for (i = 0; i < NumOps; ++i) {
232
7.14M
    if (MCOperandInfo_isTiedToOp(&OpInfo[i])) {
233
401k
      int idx = MCOperandInfo_getOperandConstraint(
234
401k
        &InstDesc[MCInst_getOpcode(MI)], i,
235
401k
        MCOI_TIED_TO);
236
237
401k
      if (idx == -1)
238
0
        continue;
239
240
401k
      if (i >= MAX_MC_OPS) {
241
0
        assert(0 &&
242
0
               "Maximum number of MC operands reached.");
243
0
      }
244
401k
      MI->tied_op_idx[i] = idx;
245
246
401k
      if (MI->flat_insn->detail)
247
401k
        MI->flat_insn->detail->writeback = true;
248
401k
    }
249
7.14M
  }
250
1.52M
}
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
6.52M
{
256
6.52M
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
257
307M
  for (int i = 0; i < MAX_MC_OPS; ++i) {
258
301M
    if (MI->tied_op_idx[i] == OpNum)
259
267k
      return true;
260
301M
  }
261
6.26M
  return false;
262
6.52M
}
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
6.55M
{
268
6.55M
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
269
6.55M
  return MI->tied_op_idx[OpNum] != -1;
270
6.55M
}