blob: d6f513ad2db0528ec076dc85cb696d337609640b [file] [log] [blame]
Nguyen Anh Quynh30e4d7f2014-05-08 22:54:58 +08001/* Capstone Disassembly Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08003
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
8#include "MCInst.h"
9#include "utils.h"
10
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070011#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
12
Nguyen Anh Quynh30c06592014-06-07 13:30:59 +080013void MCInst_Init(cs_struct *handle, MCInst *inst)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014{
Nguyen Anh Quynh30c06592014-06-07 13:30:59 +080015 switch(handle->arch) {
16 default:
17 memset(inst, 0, sizeof(*inst));
18 break;
19 case CS_ARCH_X86:
20 inst->size = 0;
Nguyen Anh Quynh30c06592014-06-07 13:30:59 +080021 break;
22 }
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080023}
24
25void MCInst_clear(MCInst *inst)
26{
27 inst->size = 0;
28}
29
Nguyen Anh Quynh39da4262013-12-04 09:49:33 +080030// NOTE: this will free @Op argument
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080031void MCInst_insert(MCInst *inst, int index, MCOperand *Op)
32{
33 int i;
34
35 for(i = inst->size; i > index; i--)
36 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
37 inst->Operands[i] = inst->Operands[i-1];
38
39 inst->Operands[index] = *Op;
40 inst->size++;
Nguyen Anh Quynh39da4262013-12-04 09:49:33 +080041
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080042 cs_mem_free(Op);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080043}
44
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +070045// do not free @Op
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070046void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
47{
48 int i;
49
50 for(i = inst->size; i > index; i--)
51 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
52 inst->Operands[i] = inst->Operands[i-1];
53
54 inst->Operands[index] = *Op;
55 inst->size++;
56}
57
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080058void MCInst_setOpcode(MCInst *inst, unsigned Op)
59{
60 inst->Opcode = Op;
61}
62
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080063void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
64{
65 inst->OpcodePub = Op;
66}
67
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080068unsigned MCInst_getOpcode(const MCInst *inst)
69{
70 return inst->Opcode;
71}
72
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080073unsigned MCInst_getOpcodePub(const MCInst *inst)
74{
75 return inst->OpcodePub;
76}
77
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080078MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
79{
80 return &inst->Operands[i];
81}
82
83unsigned MCInst_getNumOperands(const MCInst *inst)
84{
85 return inst->size;
86}
87
Nguyen Anh Quynh39da4262013-12-04 09:49:33 +080088// NOTE: this will free @Op argument
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080089int MCInst_addOperand(MCInst *inst, MCOperand *Op)
90{
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080091 inst->Operands[inst->size] = *Op;
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080092 cs_mem_free(Op);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080093
94 inst->size++;
95
96 return 0;
97}
98
99// This addOperand2 function doesnt free Op
100int MCInst_addOperand2(MCInst *inst, MCOperand *Op)
101{
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800102 inst->Operands[inst->size] = *Op;
103
104 inst->size++;
105
106 return 0;
107}
108
109void MCOperand_Init(MCOperand *op)
110{
111 op->Kind = kInvalid;
112 op->FPImmVal = 0.0;
113}
114
115bool MCOperand_isValid(const MCOperand *op)
116{
117 return op->Kind != kInvalid;
118}
119
120bool MCOperand_isReg(const MCOperand *op)
121{
122 return op->Kind == kRegister;
123}
124
125bool MCOperand_isImm(const MCOperand *op)
126{
127 return op->Kind == kImmediate;
128}
129
130bool MCOperand_isFPImm(const MCOperand *op)
131{
132 return op->Kind == kFPImmediate;
133}
134
135/// getReg - Returns the register number.
136unsigned MCOperand_getReg(const MCOperand *op)
137{
138 return op->RegVal;
139}
140
141/// setReg - Set the register number.
142void MCOperand_setReg(MCOperand *op, unsigned Reg)
143{
144 op->RegVal = Reg;
145}
146
147int64_t MCOperand_getImm(MCOperand *op)
148{
149 return op->ImmVal;
150}
151
152void MCOperand_setImm(MCOperand *op, int64_t Val)
153{
154 op->ImmVal = Val;
155}
156
157double MCOperand_getFPImm(const MCOperand *op)
158{
159 return op->FPImmVal;
160}
161
162void MCOperand_setFPImm(MCOperand *op, double Val)
163{
164 op->FPImmVal = Val;
165}
166
167MCOperand *MCOperand_CreateReg(unsigned Reg)
168{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800169 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800170
171 op->Kind = kRegister;
172 op->RegVal = Reg;
173
174 return op;
175}
176
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700177MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700178{
179 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
180
181 op->Kind = kRegister;
182 op->RegVal = Reg;
183
184 return op;
185}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800186
187void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
188{
189 MCOperand *op = &(mcInst->Operands[mcInst->size]);
190 mcInst->size++;
191
192 op->Kind = kRegister;
193 op->RegVal = Reg;
194}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700195
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800196MCOperand *MCOperand_CreateImm(int64_t Val)
197{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800198 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800199
200 op->Kind = kImmediate;
201 op->ImmVal = Val;
202
203 return op;
204}
205
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700206MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700207{
208 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
209
210 op->Kind = kImmediate;
211 op->ImmVal = Val;
212
213 return op;
214}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800215
216void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
217{
218 MCOperand *op = &(mcInst->Operands[mcInst->size]);
219 mcInst->size++;
220
221 op->Kind = kImmediate;
222 op->ImmVal = Val;
223}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700224
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800225MCOperand *MCOperand_CreateFPImm(double Val)
226{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800227 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800228
229 op->Kind = kFPImmediate;
230 op->FPImmVal = Val;
231
232 return op;
233}