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
13 #include "MCInst.h"
14 #include "utils.h"
15
16 #define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
17
MCInst_Init(MCInst * inst)18 void MCInst_Init(MCInst *inst)
19 {
20 unsigned int i;
21
22 for (i = 0; i < 48; i++) {
23 inst->Operands[i].Kind = kInvalid;
24 }
25
26 inst->Opcode = 0;
27 inst->OpcodePub = 0;
28 inst->size = 0;
29 inst->has_imm = false;
30 inst->op1_size = 0;
31 inst->writeback = false;
32 inst->ac_idx = 0;
33 inst->popcode_adjust = 0;
34 inst->assembly[0] = '\0';
35 inst->wasm_data.type = WASM_OP_INVALID;
36 inst->xAcquireRelease = 0;
37 }
38
MCInst_clear(MCInst * inst)39 void MCInst_clear(MCInst *inst)
40 {
41 inst->size = 0;
42 }
43
44 // do not free @Op
MCInst_insert0(MCInst * inst,int index,MCOperand * Op)45 void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
46 {
47 int i;
48
49 for(i = inst->size; i > index; i--)
50 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
51 inst->Operands[i] = inst->Operands[i-1];
52
53 inst->Operands[index] = *Op;
54 inst->size++;
55 }
56
MCInst_setOpcode(MCInst * inst,unsigned Op)57 void MCInst_setOpcode(MCInst *inst, unsigned Op)
58 {
59 inst->Opcode = Op;
60 }
61
MCInst_setOpcodePub(MCInst * inst,unsigned Op)62 void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
63 {
64 inst->OpcodePub = Op;
65 }
66
MCInst_getOpcode(const MCInst * inst)67 unsigned MCInst_getOpcode(const MCInst *inst)
68 {
69 return inst->Opcode;
70 }
71
MCInst_getOpcodePub(const MCInst * inst)72 unsigned MCInst_getOpcodePub(const MCInst *inst)
73 {
74 return inst->OpcodePub;
75 }
76
MCInst_getOperand(MCInst * inst,unsigned i)77 MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
78 {
79 return &inst->Operands[i];
80 }
81
MCInst_getNumOperands(const MCInst * inst)82 unsigned MCInst_getNumOperands(const MCInst *inst)
83 {
84 return inst->size;
85 }
86
87 // This addOperand2 function doesnt free Op
MCInst_addOperand2(MCInst * inst,MCOperand * Op)88 void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
89 {
90 inst->Operands[inst->size] = *Op;
91
92 inst->size++;
93 }
94
MCOperand_isValid(const MCOperand * op)95 bool MCOperand_isValid(const MCOperand *op)
96 {
97 return op->Kind != kInvalid;
98 }
99
MCOperand_isReg(const MCOperand * op)100 bool MCOperand_isReg(const MCOperand *op)
101 {
102 return op->Kind == kRegister;
103 }
104
MCOperand_isImm(const MCOperand * op)105 bool MCOperand_isImm(const MCOperand *op)
106 {
107 return op->Kind == kImmediate;
108 }
109
MCOperand_isFPImm(const MCOperand * op)110 bool MCOperand_isFPImm(const MCOperand *op)
111 {
112 return op->Kind == kFPImmediate;
113 }
114
115 /// getReg - Returns the register number.
MCOperand_getReg(const MCOperand * op)116 unsigned MCOperand_getReg(const MCOperand *op)
117 {
118 return op->RegVal;
119 }
120
121 /// setReg - Set the register number.
MCOperand_setReg(MCOperand * op,unsigned Reg)122 void MCOperand_setReg(MCOperand *op, unsigned Reg)
123 {
124 op->RegVal = Reg;
125 }
126
MCOperand_getImm(MCOperand * op)127 int64_t MCOperand_getImm(MCOperand *op)
128 {
129 return op->ImmVal;
130 }
131
MCOperand_setImm(MCOperand * op,int64_t Val)132 void MCOperand_setImm(MCOperand *op, int64_t Val)
133 {
134 op->ImmVal = Val;
135 }
136
MCOperand_getFPImm(const MCOperand * op)137 double MCOperand_getFPImm(const MCOperand *op)
138 {
139 return op->FPImmVal;
140 }
141
MCOperand_setFPImm(MCOperand * op,double Val)142 void MCOperand_setFPImm(MCOperand *op, double Val)
143 {
144 op->FPImmVal = Val;
145 }
146
MCOperand_CreateReg1(MCInst * mcInst,unsigned Reg)147 MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
148 {
149 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
150
151 op->Kind = kRegister;
152 op->RegVal = Reg;
153
154 return op;
155 }
156
MCOperand_CreateReg0(MCInst * mcInst,unsigned Reg)157 void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
158 {
159 MCOperand *op = &(mcInst->Operands[mcInst->size]);
160 mcInst->size++;
161
162 op->Kind = kRegister;
163 op->RegVal = Reg;
164 }
165
MCOperand_CreateImm1(MCInst * mcInst,int64_t Val)166 MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
167 {
168 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
169
170 op->Kind = kImmediate;
171 op->ImmVal = Val;
172
173 return op;
174 }
175
MCOperand_CreateImm0(MCInst * mcInst,int64_t Val)176 void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
177 {
178 MCOperand *op = &(mcInst->Operands[mcInst->size]);
179 mcInst->size++;
180
181 op->Kind = kImmediate;
182 op->ImmVal = Val;
183 }
184