Coverage Report

Created: 2026-07-16 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/capstonenext/MCInst.c
Line
Count
Source
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
2.34M
#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
18
19
void MCInst_Init(MCInst *inst, cs_arch arch)
20
4.27M
{
21
4.27M
  memset(inst, 0, sizeof(MCInst));
22
  // unnecessary to initialize in loop . its expensive and inst->size should be honored
23
4.27M
  inst->Operands[0].Kind = kInvalid;
24
4.27M
  inst->Operands[0].ImmVal = 0;
25
26
4.27M
  inst->Opcode = 0;
27
4.27M
  inst->OpcodePub = 0;
28
4.27M
  inst->size = 0;
29
4.27M
  inst->has_imm = false;
30
4.27M
  inst->op1_size = 0;
31
4.27M
  inst->ac_idx = 0;
32
4.27M
  inst->popcode_adjust = 0;
33
4.27M
  inst->assembly[0] = '\0';
34
4.27M
  inst->wasm_data.type = WASM_OP_INVALID;
35
4.27M
  inst->xAcquireRelease = 0;
36
209M
  for (int i = 0; i < MAX_MC_OPS; ++i)
37
205M
    inst->tied_op_idx[i] = -1;
38
4.27M
  inst->isAliasInstr = false;
39
4.27M
  inst->fillDetailOps = false;
40
4.27M
  memset(&inst->hppa_ext, 0, sizeof(inst->hppa_ext));
41
42
  // Set default assembly dialect.
43
4.27M
  switch (arch) {
44
4.12M
  default:
45
4.12M
    break;
46
4.12M
  case CS_ARCH_SYSTEMZ:
47
154k
    inst->MAI.assemblerDialect = SYSTEMZASMDIALECT_AD_HLASM;
48
154k
    break;
49
4.27M
  }
50
4.27M
}
51
52
void MCInst_clear(MCInst *inst)
53
3.83M
{
54
3.83M
  inst->size = 0;
55
3.83M
}
56
57
// does not free @Op
58
void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
59
2.35M
{
60
2.35M
  CS_ASSERT_RET(index < MAX_MC_OPS && inst->size < MAX_MC_OPS);
61
2.35M
  int i;
62
63
4.15M
  for (i = inst->size; i > index; i--)
64
    //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
65
1.80M
    inst->Operands[i] = inst->Operands[i - 1];
66
67
2.35M
  inst->Operands[index] = *Op;
68
2.35M
  inst->size++;
69
2.35M
}
70
71
void MCInst_setOpcode(MCInst *inst, unsigned Op)
72
4.28M
{
73
4.28M
  inst->Opcode = Op;
74
4.28M
}
75
76
void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
77
33.5k
{
78
33.5k
  inst->OpcodePub = Op;
79
33.5k
}
80
81
unsigned MCInst_getOpcode(const MCInst *inst)
82
37.7M
{
83
37.7M
  return inst->Opcode;
84
37.7M
}
85
86
unsigned MCInst_getOpcodePub(const MCInst *inst)
87
4.30M
{
88
4.30M
  return inst->OpcodePub;
89
4.30M
}
90
91
MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
92
23.3M
{
93
23.3M
  assert(i < MAX_MC_OPS);
94
23.3M
  return &inst->Operands[i];
95
23.3M
}
96
97
unsigned MCInst_getNumOperands(const MCInst *inst)
98
10.4M
{
99
10.4M
  return inst->size;
100
10.4M
}
101
102
// This addOperand2 function doesn't free Op
103
void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
104
136k
{
105
136k
  CS_ASSERT_RET(inst->size < MAX_MC_OPS);
106
136k
  inst->Operands[inst->size] = *Op;
107
108
136k
  inst->size++;
109
136k
}
110
111
bool MCOperand_isValid(const MCOperand *op)
112
0
{
113
0
  return op->Kind != kInvalid;
114
0
}
115
116
bool MCOperand_isReg(const MCOperand *op)
117
13.4M
{
118
13.4M
  return op->Kind == kRegister || op->MachineOperandType == kRegister;
119
13.4M
}
120
121
bool MCOperand_isImm(const MCOperand *op)
122
4.08M
{
123
4.08M
  return op->Kind == kImmediate || op->MachineOperandType == kImmediate;
124
4.08M
}
125
126
bool MCOperand_isFPImm(const MCOperand *op)
127
0
{
128
0
  return op->Kind == kFPImmediate;
129
0
}
130
131
bool MCOperand_isDFPImm(const MCOperand *op)
132
1.68k
{
133
1.68k
  return op->Kind == kDFPImmediate;
134
1.68k
}
135
136
bool MCOperand_isExpr(const MCOperand *op)
137
198k
{
138
198k
  return op->Kind == kExpr;
139
198k
}
140
141
bool MCOperand_isInst(const MCOperand *op)
142
0
{
143
0
  return op->Kind == kInst;
144
0
}
145
146
/// getReg - Returns the register number.
147
unsigned MCOperand_getReg(const MCOperand *op)
148
15.6M
{
149
15.6M
  return op->RegVal;
150
15.6M
}
151
152
/// setReg - Set the register number.
153
void MCOperand_setReg(MCOperand *op, unsigned Reg)
154
18.1k
{
155
18.1k
  op->RegVal = Reg;
156
18.1k
}
157
158
int64_t MCOperand_getImm(const MCOperand *op)
159
8.38M
{
160
8.38M
  return op->ImmVal;
161
8.38M
}
162
163
int64_t MCOperand_getExpr(const MCOperand *op)
164
0
{
165
0
  return op->ImmVal;
166
0
}
167
168
void MCOperand_setImm(MCOperand *op, int64_t Val)
169
18.2k
{
170
18.2k
  op->ImmVal = Val;
171
18.2k
}
172
173
double MCOperand_getFPImm(const MCOperand *op)
174
0
{
175
0
  return op->FPImmVal;
176
0
}
177
178
void MCOperand_setFPImm(MCOperand *op, double Val)
179
0
{
180
0
  op->FPImmVal = Val;
181
0
}
182
183
MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
184
1.35M
{
185
1.35M
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
186
187
1.35M
  op->MachineOperandType = kRegister;
188
1.35M
  op->Kind = kRegister;
189
1.35M
  op->RegVal = Reg;
190
191
1.35M
  return op;
192
1.35M
}
193
194
void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
195
7.90M
{
196
7.90M
  CS_ASSERT_RET(mcInst->size < MAX_MC_OPS);
197
7.90M
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
198
7.90M
  mcInst->size++;
199
200
7.90M
  op->MachineOperandType = kRegister;
201
7.90M
  op->Kind = kRegister;
202
7.90M
  op->RegVal = Reg;
203
7.90M
}
204
205
MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
206
988k
{
207
988k
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
208
209
988k
  op->MachineOperandType = kImmediate;
210
988k
  op->Kind = kImmediate;
211
988k
  op->ImmVal = Val;
212
213
988k
  return op;
214
988k
}
215
216
void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
217
3.67M
{
218
3.67M
  CS_ASSERT_RET(mcInst->size < MAX_MC_OPS);
219
3.67M
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
220
3.67M
  mcInst->size++;
221
222
3.67M
  op->MachineOperandType = kImmediate;
223
3.67M
  op->Kind = kImmediate;
224
3.67M
  op->ImmVal = Val;
225
3.67M
}
226
227
/// Check if any operand of the MCInstrDesc is predicable
228
bool MCInst_isPredicable(const MCInstrDesc *MIDesc)
229
1.18M
{
230
1.18M
  const MCOperandInfo *OpInfo = MIDesc->OpInfo;
231
1.18M
  unsigned NumOps = MIDesc->NumOperands;
232
5.18M
  for (unsigned i = 0; i < NumOps; ++i) {
233
5.10M
    if (MCOperandInfo_isPredicate(&OpInfo[i])) {
234
1.10M
      return true;
235
1.10M
    }
236
5.10M
  }
237
73.9k
  return false;
238
1.18M
}
239
240
/// Checks if tied operands exist in the instruction and sets
241
/// - The writeback flag in detail
242
/// - Saves the indices of the tied destination operands.
243
void MCInst_handleWriteback(MCInst *MI, const MCInstrDesc *InstDescTable,
244
          unsigned tbl_size)
245
1.75M
{
246
1.75M
  const MCInstrDesc *InstDesc = NULL;
247
1.75M
  const MCOperandInfo *OpInfo = NULL;
248
1.75M
  unsigned short NumOps = 0;
249
1.75M
  InstDesc =
250
1.75M
    MCInstrDesc_get(MCInst_getOpcode(MI), InstDescTable, tbl_size);
251
1.75M
  OpInfo = InstDesc->OpInfo;
252
1.75M
  NumOps = InstDesc->NumOperands;
253
254
9.89M
  for (unsigned i = 0; i < NumOps; ++i) {
255
8.13M
    if (MCOperandInfo_isTiedToOp(&OpInfo[i])) {
256
387k
      int idx = MCOperandInfo_getOperandConstraint(
257
387k
        InstDesc, i, MCOI_TIED_TO);
258
259
387k
      if (idx == -1)
260
0
        continue;
261
262
387k
      if (i >= MAX_MC_OPS) {
263
0
        assert(0 &&
264
0
               "Maximum number of MC operands reached.");
265
0
      }
266
387k
      MI->tied_op_idx[i] = idx;
267
268
387k
      if (MI->flat_insn->detail)
269
387k
        MI->flat_insn->detail->writeback = true;
270
387k
    }
271
8.13M
  }
272
1.75M
}
273
274
/// Check if operand with OpNum is tied by another operand
275
/// (operand is tying destination).
276
bool MCInst_opIsTied(const MCInst *MI, unsigned OpNum)
277
6.98M
{
278
6.98M
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
279
330M
  for (int i = 0; i < MAX_MC_OPS; ++i) {
280
323M
    if (MI->tied_op_idx[i] == OpNum)
281
259k
      return true;
282
323M
  }
283
6.73M
  return false;
284
6.98M
}
285
286
/// Check if operand with OpNum is tying another operand
287
/// (operand is tying src).
288
bool MCInst_opIsTying(const MCInst *MI, unsigned OpNum)
289
6.97M
{
290
6.97M
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
291
6.97M
  return MI->tied_op_idx[OpNum] != -1;
292
6.97M
}
293
294
/// Returns the value of the @MCInst operand at index @OpNum.
295
uint64_t MCInst_getOpVal(MCInst *MI, unsigned OpNum)
296
6.34M
{
297
6.34M
  assert(OpNum < MAX_MC_OPS);
298
6.34M
  MCOperand *op = MCInst_getOperand(MI, OpNum);
299
6.34M
  if (MCOperand_isReg(op))
300
4.47M
    return MCOperand_getReg(op);
301
1.87M
  else if (MCOperand_isImm(op))
302
1.87M
    return MCOperand_getImm(op);
303
0
  else
304
1.87M
    assert(0 && "Operand type not handled in this getter.");
305
0
  return MCOperand_getImm(op);
306
6.34M
}
307
308
void MCInst_setIsAlias(MCInst *MI, bool Flag)
309
1.89M
{
310
1.89M
  assert(MI);
311
1.89M
  MI->isAliasInstr = Flag;
312
1.89M
  MI->flat_insn->is_alias = Flag;
313
1.89M
}
314
315
/// @brief Copies the relevant members of a temporary MCInst to
316
/// the main MCInst. This is used if TryDecode was run on a temporary MCInst.
317
/// @param MI The main MCInst
318
/// @param TmpMI The temporary MCInst.
319
void MCInst_updateWithTmpMI(MCInst *MI, MCInst *TmpMI)
320
0
{
321
0
  MI->size = TmpMI->size;
322
0
  MI->Opcode = TmpMI->Opcode;
323
0
  assert(MI->size < MAX_MC_OPS);
324
0
  memcpy(MI->Operands, TmpMI->Operands,
325
0
         sizeof(MI->Operands[0]) * MI->size);
326
0
}
327
328
/// @brief Sets the softfail/illegal flag in the cs_insn.
329
/// Setting it indicates the instruction can be decoded, but is invalid
330
/// due to not allowed operands or an illegal context.
331
///
332
/// @param MI The MCInst holding the cs_insn currently decoded.
333
void MCInst_setSoftFail(MCInst *MI)
334
121k
{
335
121k
  assert(MI && MI->flat_insn);
336
121k
  MI->flat_insn->illegal = true;
337
121k
}