Coverage Report

Created: 2023-09-25 06:24

/src/capstonenext/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
2.09M
#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
18
19
void MCInst_Init(MCInst *inst)
20
3.40M
{
21
  // unnecessary to initialize in loop . its expensive and inst->size shuold be honored
22
3.40M
  inst->Operands[0].Kind = kInvalid;
23
3.40M
  inst->Operands[0].ImmVal = 0;
24
25
3.40M
  inst->Opcode = 0;
26
3.40M
  inst->OpcodePub = 0;
27
3.40M
  inst->size = 0;
28
3.40M
  inst->has_imm = false;
29
3.40M
  inst->op1_size = 0;
30
3.40M
  inst->ac_idx = 0;
31
3.40M
  inst->popcode_adjust = 0;
32
3.40M
  inst->assembly[0] = '\0';
33
3.40M
  inst->wasm_data.type = WASM_OP_INVALID;
34
3.40M
  inst->xAcquireRelease = 0;
35
166M
  for (int i = 0; i < MAX_MC_OPS; ++i)
36
163M
    inst->tied_op_idx[i] = -1;
37
3.40M
  inst->isAliasInstr = false;
38
3.40M
  inst->fillDetailOps = false;
39
3.40M
}
40
41
void MCInst_clear(MCInst *inst)
42
2.90M
{
43
2.90M
  inst->size = 0;
44
2.90M
}
45
46
// does not free @Op
47
void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
48
2.10M
{
49
2.10M
  assert(index < MAX_MC_OPS);
50
2.10M
  int i;
51
52
3.47M
  for(i = inst->size; i > index; i--)
53
    //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
54
1.37M
    inst->Operands[i] = inst->Operands[i-1];
55
56
2.10M
  inst->Operands[index] = *Op;
57
2.10M
  inst->size++;
58
2.10M
}
59
60
void MCInst_setOpcode(MCInst *inst, unsigned Op)
61
3.43M
{
62
3.43M
  inst->Opcode = Op;
63
3.43M
}
64
65
void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
66
128k
{
67
128k
  inst->OpcodePub = Op;
68
128k
}
69
70
unsigned MCInst_getOpcode(const MCInst *inst)
71
32.0M
{
72
32.0M
  return inst->Opcode;
73
32.0M
}
74
75
unsigned MCInst_getOpcodePub(const MCInst *inst)
76
3.53M
{
77
3.53M
  return inst->OpcodePub;
78
3.53M
}
79
80
MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
81
20.5M
{
82
20.5M
  assert(i < MAX_MC_OPS);
83
20.5M
  return &inst->Operands[i];
84
20.5M
}
85
86
unsigned MCInst_getNumOperands(const MCInst *inst)
87
10.1M
{
88
10.1M
  return inst->size;
89
10.1M
}
90
91
// This addOperand2 function doesnt free Op
92
void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
93
2.28k
{
94
2.28k
  assert(inst->size < MAX_MC_OPS);
95
2.28k
  inst->Operands[inst->size] = *Op;
96
97
2.28k
  inst->size++;
98
2.28k
}
99
100
bool MCOperand_isValid(const MCOperand *op)
101
0
{
102
0
  return op->Kind != kInvalid;
103
0
}
104
105
bool MCOperand_isReg(const MCOperand *op)
106
10.2M
{
107
10.2M
  return op->Kind == kRegister;
108
10.2M
}
109
110
bool MCOperand_isImm(const MCOperand *op)
111
3.27M
{
112
3.27M
  return op->Kind == kImmediate;
113
3.27M
}
114
115
bool MCOperand_isFPImm(const MCOperand *op)
116
3.36k
{
117
3.36k
  return op->Kind == kFPImmediate;
118
3.36k
}
119
120
bool MCOperand_isDFPImm(const MCOperand *op)
121
0
{
122
0
  return op->Kind == kDFPImmediate;
123
0
}
124
125
bool MCOperand_isExpr(const MCOperand *op)
126
116k
{
127
116k
  return op->Kind == kExpr;
128
116k
}
129
130
bool MCOperand_isInst(const MCOperand *op)
131
0
{
132
0
  return op->Kind == kInst;
133
0
}
134
135
/// getReg - Returns the register number.
136
unsigned MCOperand_getReg(const MCOperand *op)
137
12.5M
{
138
12.5M
  return op->RegVal;
139
12.5M
}
140
141
/// setReg - Set the register number.
142
void MCOperand_setReg(MCOperand *op, unsigned Reg)
143
14.1k
{
144
14.1k
  op->RegVal = Reg;
145
14.1k
}
146
147
int64_t MCOperand_getImm(MCOperand *op)
148
7.35M
{
149
7.35M
  return op->ImmVal;
150
7.35M
}
151
152
void MCOperand_setImm(MCOperand *op, int64_t Val)
153
14.6k
{
154
14.6k
  op->ImmVal = Val;
155
14.6k
}
156
157
double MCOperand_getFPImm(const MCOperand *op)
158
0
{
159
0
  return op->FPImmVal;
160
0
}
161
162
void MCOperand_setFPImm(MCOperand *op, double Val)
163
0
{
164
0
  op->FPImmVal = Val;
165
0
}
166
167
MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
168
1.19M
{
169
1.19M
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
170
171
1.19M
  op->MachineOperandType = kRegister;
172
1.19M
  op->Kind = kRegister;
173
1.19M
  op->RegVal = Reg;
174
175
1.19M
  return op;
176
1.19M
}
177
178
void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
179
6.68M
{
180
6.68M
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
181
6.68M
  mcInst->size++;
182
183
6.68M
  op->MachineOperandType = kRegister;
184
6.68M
  op->Kind = kRegister;
185
6.68M
  op->RegVal = Reg;
186
6.68M
}
187
188
MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
189
894k
{
190
894k
  MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
191
192
894k
  op->MachineOperandType = kImmediate;
193
894k
  op->Kind = kImmediate;
194
894k
  op->ImmVal = Val;
195
196
894k
  return op;
197
894k
}
198
199
void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
200
3.17M
{
201
3.17M
  assert(mcInst->size < MAX_MC_OPS);
202
3.17M
  MCOperand *op = &(mcInst->Operands[mcInst->size]);
203
3.17M
  mcInst->size++;
204
205
3.17M
  op->MachineOperandType = kImmediate;
206
3.17M
  op->Kind = kImmediate;
207
3.17M
  op->ImmVal = Val;
208
3.17M
}
209
210
/// Check if any operand of the MCInstrDesc is predicable
211
bool MCInst_isPredicable(const MCInstrDesc *MIDesc)
212
1.06M
{
213
1.06M
  const MCOperandInfo *OpInfo = MIDesc->OpInfo;
214
1.06M
  unsigned NumOps = MIDesc->NumOperands;
215
4.78M
  for (unsigned i = 0; i < NumOps; ++i) {
216
4.71M
    if (MCOperandInfo_isPredicate(&OpInfo[i])) {
217
995k
      return true;
218
995k
    }
219
4.71M
  }
220
74.1k
  return false;
221
1.06M
}
222
223
/// Checks if tied operands exist in the instruction and sets
224
/// - The writeback flag in detail
225
/// - Saves the indices of the tied destination operands.
226
void MCInst_handleWriteback(MCInst *MI, const MCInstrDesc *InstDesc)
227
1.22M
{
228
1.22M
  const MCOperandInfo *OpInfo = InstDesc[MCInst_getOpcode(MI)].OpInfo;
229
1.22M
  unsigned short NumOps = InstDesc[MCInst_getOpcode(MI)].NumOperands;
230
231
1.22M
  unsigned i;
232
7.41M
  for (i = 0; i < NumOps; ++i) {
233
6.19M
    if (MCOperandInfo_isTiedToOp(&OpInfo[i])) {
234
258k
      int idx = MCOperandInfo_getOperandConstraint(
235
258k
        &InstDesc[MCInst_getOpcode(MI)], i,
236
258k
        MCOI_TIED_TO);
237
238
258k
      if (idx == -1)
239
0
        continue;
240
241
258k
      if (i >= MAX_MC_OPS) {
242
0
        assert(0 &&
243
0
               "Maximum number of MC operands reached.");
244
0
      }
245
258k
      MI->tied_op_idx[i] = idx;
246
247
258k
      if (MI->flat_insn->detail)
248
258k
        MI->flat_insn->detail->writeback = true;
249
258k
    }
250
6.19M
  }
251
1.22M
}
252
253
/// Check if operand with OpNum is tied by another operand
254
/// (operand is tying destination).
255
bool MCInst_opIsTied(const MCInst *MI, unsigned OpNum)
256
4.04M
{
257
4.04M
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
258
190M
  for (int i = 0; i < MAX_MC_OPS; ++i) {
259
186M
    if (MI->tied_op_idx[i] == OpNum)
260
168k
      return true;
261
186M
  }
262
3.87M
  return false;
263
4.04M
}
264
265
/// Check if operand with OpNum is tying another operand
266
/// (operand is tying src).
267
bool MCInst_opIsTying(const MCInst *MI, unsigned OpNum)
268
3.98M
{
269
3.98M
  assert(OpNum < MAX_MC_OPS && "Maximum number of MC operands exceeded.");
270
3.98M
  return MI->tied_op_idx[OpNum] != -1;
271
3.98M
}
272
273
/// Returns the value of the @MCInst operand at index @OpNum.
274
uint64_t MCInst_getOpVal(MCInst *MI, unsigned OpNum)
275
3.97M
{
276
3.97M
  assert(OpNum < MAX_MC_OPS);
277
3.97M
  MCOperand *op = MCInst_getOperand(MI, OpNum);
278
3.97M
  if (MCOperand_isReg(op))
279
2.77M
    return MCOperand_getReg(op);
280
1.20M
  else if (MCOperand_isImm(op))
281
1.20M
    return MCOperand_getImm(op);
282
0
  else
283
0
    assert(0 && "Operand type not handled in this getter.");
284
0
  return false;
285
3.97M
}
286
287
62.3k
void MCInst_setIsAlias(MCInst *MI, bool Flag) {
288
62.3k
  assert(MI);
289
62.3k
  MI->isAliasInstr = Flag;
290
62.3k
  MI->flat_insn->is_alias = Flag;
291
62.3k
}