Coverage Report

Created: 2025-08-26 06:30

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