Coverage Report

Created: 2025-11-09 07:00

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
292k
#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
18
19
void MCInst_Init(MCInst *inst, cs_arch arch)
20
493k
{
21
493k
  memset(inst, 0, sizeof(MCInst));
22
  // unnecessary to initialize in loop . its expensive and inst->size should be honored
23
493k
  inst->Operands[0].Kind = kInvalid;
24
493k
  inst->Operands[0].ImmVal = 0;
25
26
493k
  inst->Opcode = 0;
27
493k
  inst->OpcodePub = 0;
28
493k
  inst->size = 0;
29
493k
  inst->has_imm = false;
30
493k
  inst->op1_size = 0;
31
493k
  inst->ac_idx = 0;
32
493k
  inst->popcode_adjust = 0;
33
493k
  inst->assembly[0] = '\0';
34
493k
  inst->wasm_data.type = WASM_OP_INVALID;
35
493k
  inst->xAcquireRelease = 0;
36
24.1M
  for (int i = 0; i < MAX_MC_OPS; ++i)
37
23.6M
    inst->tied_op_idx[i] = -1;
38
493k
  inst->isAliasInstr = false;
39
493k
  inst->fillDetailOps = false;
40
493k
  memset(&inst->hppa_ext, 0, sizeof(inst->hppa_ext));
41
42
  // Set default assembly dialect.
43
493k
  switch (arch) {
44
462k
  default:
45
462k
    break;
46
462k
  case CS_ARCH_SYSTEMZ:
47
30.0k
    inst->MAI.assemblerDialect = SYSTEMZASMDIALECT_AD_HLASM;
48
30.0k
    break;
49
493k
  }
50
493k
}
51
52
void MCInst_clear(MCInst *inst)
53
463k
{
54
463k
  inst->size = 0;
55
463k
}
56
57
// does not free @Op
58
void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
59
294k
{
60
294k
  CS_ASSERT_RET(index < MAX_MC_OPS);
61
294k
  int i;
62
63
513k
  for (i = inst->size; i > index; i--)
64
    //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
65
218k
    inst->Operands[i] = inst->Operands[i - 1];
66
67
294k
  inst->Operands[index] = *Op;
68
294k
  inst->size++;
69
294k
}
70
71
void MCInst_setOpcode(MCInst *inst, unsigned Op)
72
495k
{
73
495k
  inst->Opcode = Op;
74
495k
}
75
76
void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
77
2.94k
{
78
2.94k
  inst->OpcodePub = Op;
79
2.94k
}
80
81
unsigned MCInst_getOpcode(const MCInst *inst)
82
4.81M
{
83
4.81M
  return inst->Opcode;
84
4.81M
}
85
86
unsigned MCInst_getOpcodePub(const MCInst *inst)
87
508k
{
88
508k
  return inst->OpcodePub;
89
508k
}
90
91
MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
92
3.06M
{
93
3.06M
  assert(i < MAX_MC_OPS);
94
3.06M
  return &inst->Operands[i];
95
3.06M
}
96
97
unsigned MCInst_getNumOperands(const MCInst *inst)
98
1.34M
{
99
1.34M
  return inst->size;
100
1.34M
}
101
102
// This addOperand2 function doesn't free Op
103
void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
104
756
{
105
756
  CS_ASSERT_RET(inst->size < MAX_MC_OPS);
106
756
  inst->Operands[inst->size] = *Op;
107
108
756
  inst->size++;
109
756
}
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
1.80M
{
118
1.80M
  return op->Kind == kRegister || op->MachineOperandType == kRegister;
119
1.80M
}
120
121
bool MCOperand_isImm(const MCOperand *op)
122
539k
{
123
539k
  return op->Kind == kImmediate || op->MachineOperandType == kImmediate;
124
539k
}
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
18
{
133
18
  return op->Kind == kDFPImmediate;
134
18
}
135
136
bool MCOperand_isExpr(const MCOperand *op)
137
27.2k
{
138
27.2k
  return op->Kind == kExpr;
139
27.2k
}
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
1.97M
{
149
1.97M
  return op->RegVal;
150
1.97M
}
151
152
/// setReg - Set the register number.
153
void MCOperand_setReg(MCOperand *op, unsigned Reg)
154
2.95k
{
155
2.95k
  op->RegVal = Reg;
156
2.95k
}
157
158
int64_t MCOperand_getImm(const MCOperand *op)
159
1.09M
{
160
1.09M
  return op->ImmVal;
161
1.09M
}
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
2.97k
{
170
2.97k
  op->ImmVal = Val;
171
2.97k
}
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
171k
{
185
171k
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
186
187
171k
  op->MachineOperandType = kRegister;
188
171k
  op->Kind = kRegister;
189
171k
  op->RegVal = Reg;
190
191
171k
  return op;
192
171k
}
193
194
void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
195
970k
{
196
970k
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
197
970k
  mcInst->size++;
198
199
970k
  op->MachineOperandType = kRegister;
200
970k
  op->Kind = kRegister;
201
970k
  op->RegVal = Reg;
202
970k
}
203
204
MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
205
121k
{
206
121k
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
207
208
121k
  op->MachineOperandType = kImmediate;
209
121k
  op->Kind = kImmediate;
210
121k
  op->ImmVal = Val;
211
212
121k
  return op;
213
121k
}
214
215
void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
216
443k
{
217
443k
  assert(mcInst->size < MAX_MC_OPS);
218
443k
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
219
443k
  mcInst->size++;
220
221
443k
  op->MachineOperandType = kImmediate;
222
443k
  op->Kind = kImmediate;
223
443k
  op->ImmVal = Val;
224
443k
}
225
226
/// Check if any operand of the MCInstrDesc is predicable
227
bool MCInst_isPredicable(const MCInstrDesc *MIDesc)
228
146k
{
229
146k
  const MCOperandInfo *OpInfo = MIDesc->OpInfo;
230
146k
  unsigned NumOps = MIDesc->NumOperands;
231
667k
  for (unsigned i = 0; i < NumOps; ++i) {
232
654k
    if (MCOperandInfo_isPredicate(&OpInfo[i])) {
233
133k
      return true;
234
133k
    }
235
654k
  }
236
12.7k
  return false;
237
146k
}
238
239
/// Checks if tied operands exist in the instruction and sets
240
/// - The writeback flag in detail
241
/// - Saves the indices of the tied destination operands.
242
void MCInst_handleWriteback(MCInst *MI, const MCInstrDesc *InstDescTable,
243
          unsigned tbl_size)
244
225k
{
245
225k
  const MCInstrDesc *InstDesc = NULL;
246
225k
  const MCOperandInfo *OpInfo = NULL;
247
225k
  unsigned short NumOps = 0;
248
225k
  InstDesc =
249
225k
    MCInstrDesc_get(MCInst_getOpcode(MI), InstDescTable, tbl_size);
250
225k
  OpInfo = InstDesc->OpInfo;
251
225k
  NumOps = InstDesc->NumOperands;
252
253
1.29M
  for (unsigned i = 0; i < NumOps; ++i) {
254
1.06M
    if (MCOperandInfo_isTiedToOp(&OpInfo[i])) {
255
58.9k
      int idx = MCOperandInfo_getOperandConstraint(
256
58.9k
        InstDesc, i, MCOI_TIED_TO);
257
258
58.9k
      if (idx == -1)
259
0
        continue;
260
261
58.9k
      if (i >= MAX_MC_OPS) {
262
0
        assert(0 &&
263
0
               "Maximum number of MC operands reached.");
264
0
      }
265
58.9k
      MI->tied_op_idx[i] = idx;
266
267
58.9k
      if (MI->flat_insn->detail)
268
58.9k
        MI->flat_insn->detail->writeback = true;
269
58.9k
    }
270
1.06M
  }
271
225k
}
272
273
/// Check if operand with OpNum is tied by another operand
274
/// (operand is tying destination).
275
bool MCInst_opIsTied(const MCInst *MI, unsigned OpNum)
276
954k
{
277
954k
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
278
44.9M
  for (int i = 0; i < MAX_MC_OPS; ++i) {
279
44.0M
    if (MI->tied_op_idx[i] == OpNum)
280
39.7k
      return true;
281
44.0M
  }
282
914k
  return false;
283
954k
}
284
285
/// Check if operand with OpNum is tying another operand
286
/// (operand is tying src).
287
bool MCInst_opIsTying(const MCInst *MI, unsigned OpNum)
288
962k
{
289
962k
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
290
962k
  return MI->tied_op_idx[OpNum] != -1;
291
962k
}
292
293
/// Returns the value of the @MCInst operand at index @OpNum.
294
uint64_t MCInst_getOpVal(MCInst *MI, unsigned OpNum)
295
912k
{
296
912k
  assert(OpNum < MAX_MC_OPS);
297
912k
  MCOperand *op = MCInst_getOperand(MI, OpNum);
298
912k
  if (MCOperand_isReg(op))
299
649k
    return MCOperand_getReg(op);
300
263k
  else if (MCOperand_isImm(op))
301
263k
    return MCOperand_getImm(op);
302
0
  else
303
263k
    assert(0 && "Operand type not handled in this getter.");
304
0
  return MCOperand_getImm(op);
305
912k
}
306
307
void MCInst_setIsAlias(MCInst *MI, bool Flag)
308
241k
{
309
241k
  assert(MI);
310
241k
  MI->isAliasInstr = Flag;
311
241k
  MI->flat_insn->is_alias = Flag;
312
241k
}
313
314
/// @brief Copies the relevant members of a temporary MCInst to
315
/// the main MCInst. This is used if TryDecode was run on a temporary MCInst.
316
/// @param MI The main MCInst
317
/// @param TmpMI The temporary MCInst.
318
void MCInst_updateWithTmpMI(MCInst *MI, MCInst *TmpMI)
319
0
{
320
0
  MI->size = TmpMI->size;
321
0
  MI->Opcode = TmpMI->Opcode;
322
0
  assert(MI->size < MAX_MC_OPS);
323
0
  memcpy(MI->Operands, TmpMI->Operands,
324
0
         sizeof(MI->Operands[0]) * MI->size);
325
0
}
326
327
/// @brief Sets the softfail/illegal flag in the cs_insn.
328
/// Setting it indicates the instruction can be decoded, but is invalid
329
/// due to not allowed operands or an illegal context.
330
///
331
/// @param MI The MCInst holding the cs_insn currently decoded.
332
void MCInst_setSoftFail(MCInst *MI)
333
18.1k
{
334
18.1k
  assert(MI && MI->flat_insn);
335
18.1k
  MI->flat_insn->illegal = true;
336
18.1k
}