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