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