Coverage Report

Created: 2024-09-08 06:22

/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
4.80M
#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
18
19
void MCInst_Init(MCInst *inst)
20
8.30M
{
21
  // unnecessary to initialize in loop . its expensive and inst->size shuold be honored
22
8.30M
  inst->Operands[0].Kind = kInvalid;
23
8.30M
  inst->Operands[0].ImmVal = 0;
24
25
8.30M
  inst->Opcode = 0;
26
8.30M
  inst->OpcodePub = 0;
27
8.30M
  inst->size = 0;
28
8.30M
  inst->has_imm = false;
29
8.30M
  inst->op1_size = 0;
30
8.30M
  inst->writeback = false;
31
8.30M
  inst->ac_idx = 0;
32
8.30M
  inst->popcode_adjust = 0;
33
8.30M
  inst->assembly[0] = '\0';
34
8.30M
  inst->wasm_data.type = WASM_OP_INVALID;
35
8.30M
  inst->xAcquireRelease = 0;
36
406M
  for (int i = 0; i < MAX_MC_OPS; ++i)
37
398M
    inst->tied_op_idx[i] = -1;
38
8.30M
}
39
40
void MCInst_clear(MCInst *inst)
41
8.68M
{
42
8.68M
  inst->size = 0;
43
8.68M
}
44
45
// does not free @Op
46
void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
47
4.81M
{
48
4.81M
  assert(index < MAX_MC_OPS);
49
4.81M
  int i;
50
51
8.28M
  for(i = inst->size; i > index; i--)
52
    //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
53
3.46M
    inst->Operands[i] = inst->Operands[i-1];
54
55
4.81M
  inst->Operands[index] = *Op;
56
4.81M
  inst->size++;
57
4.81M
}
58
59
void MCInst_setOpcode(MCInst *inst, unsigned Op)
60
8.28M
{
61
8.28M
  inst->Opcode = Op;
62
8.28M
}
63
64
void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
65
176k
{
66
176k
  inst->OpcodePub = Op;
67
176k
}
68
69
unsigned MCInst_getOpcode(const MCInst *inst)
70
65.0M
{
71
65.0M
  return inst->Opcode;
72
65.0M
}
73
74
unsigned MCInst_getOpcodePub(const MCInst *inst)
75
8.56M
{
76
8.56M
  return inst->OpcodePub;
77
8.56M
}
78
79
MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
80
38.7M
{
81
38.7M
  assert(i < MAX_MC_OPS);
82
38.7M
  return &inst->Operands[i];
83
38.7M
}
84
85
unsigned MCInst_getNumOperands(const MCInst *inst)
86
17.0M
{
87
17.0M
  return inst->size;
88
17.0M
}
89
90
// This addOperand2 function doesnt free Op
91
void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
92
2.89k
{
93
2.89k
  assert(inst->size < MAX_MC_OPS);
94
2.89k
  inst->Operands[inst->size] = *Op;
95
96
2.89k
  inst->size++;
97
2.89k
}
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
6.36M
{
106
6.36M
  return op->Kind == kRegister;
107
6.36M
}
108
109
bool MCOperand_isImm(const MCOperand *op)
110
2.44M
{
111
2.44M
  return op->Kind == kImmediate;
112
2.44M
}
113
114
bool MCOperand_isFPImm(const MCOperand *op)
115
413
{
116
413
  return op->Kind == kFPImmediate;
117
413
}
118
119
bool MCOperand_isDFPImm(const MCOperand *op)
120
1.91k
{
121
1.91k
  return op->Kind == kDFPImmediate;
122
1.91k
}
123
124
bool MCOperand_isExpr(const MCOperand *op)
125
136k
{
126
136k
  return op->Kind == kExpr;
127
136k
}
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
27.5M
{
137
27.5M
  return op->RegVal;
138
27.5M
}
139
140
/// setReg - Set the register number.
141
void MCOperand_setReg(MCOperand *op, unsigned Reg)
142
25.5k
{
143
25.5k
  op->RegVal = Reg;
144
25.5k
}
145
146
int64_t MCOperand_getImm(MCOperand *op)
147
13.6M
{
148
13.6M
  return op->ImmVal;
149
13.6M
}
150
151
void MCOperand_setImm(MCOperand *op, int64_t Val)
152
37.4k
{
153
37.4k
  op->ImmVal = Val;
154
37.4k
}
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
2.75M
{
168
2.75M
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
169
170
2.75M
  op->MachineOperandType = kRegister;
171
2.75M
  op->Kind = kRegister;
172
2.75M
  op->RegVal = Reg;
173
174
2.75M
  return op;
175
2.75M
}
176
177
void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
178
15.2M
{
179
15.2M
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
180
15.2M
  mcInst->size++;
181
182
15.2M
  op->MachineOperandType = kRegister;
183
15.2M
  op->Kind = kRegister;
184
15.2M
  op->RegVal = Reg;
185
15.2M
}
186
187
MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
188
2.04M
{
189
2.04M
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
190
191
2.04M
  op->MachineOperandType = kImmediate;
192
2.04M
  op->Kind = kImmediate;
193
2.04M
  op->ImmVal = Val;
194
195
2.04M
  return op;
196
2.04M
}
197
198
void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
199
7.16M
{
200
7.16M
  assert(mcInst->size < MAX_MC_OPS);
201
7.16M
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
202
7.16M
  mcInst->size++;
203
204
7.16M
  op->MachineOperandType = kImmediate;
205
7.16M
  op->Kind = kImmediate;
206
7.16M
  op->ImmVal = Val;
207
7.16M
}
208
209
/// Check if any operand of the MCInstrDesc is predicable
210
bool MCInst_isPredicable(const MCInstrDesc *MIDesc)
211
1.28M
{
212
1.28M
  const MCOperandInfo *OpInfo = MIDesc->OpInfo;
213
1.28M
  unsigned NumOps = MIDesc->NumOperands;
214
5.62M
  for (unsigned i = 0; i < NumOps; ++i) {
215
5.54M
    if (MCOperandInfo_isPredicate(&OpInfo[i])) {
216
1.20M
      return true;
217
1.20M
    }
218
5.54M
  }
219
79.7k
  return false;
220
1.28M
}
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
6.27M
{
256
6.27M
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
257
294M
  for (int i = 0; i < MAX_MC_OPS; ++i) {
258
288M
    if (MI->tied_op_idx[i] == OpNum)
259
287k
      return true;
260
288M
  }
261
5.98M
  return false;
262
6.27M
}
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.21M
{
268
6.21M
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
269
6.21M
  return MI->tied_op_idx[OpNum] != -1;
270
6.21M
}