blob: c459171d9822ccc5932413a9105ac5856ab4ba86 [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 Quynh26ee41a2013-11-27 12:11:31 +080013void MCInst_Init(MCInst *inst)
14{
15 memset(inst, 0, sizeof(*inst));
16}
17
18void MCInst_clear(MCInst *inst)
19{
20 inst->size = 0;
21}
22
Nguyen Anh Quynh39da4262013-12-04 09:49:33 +080023// NOTE: this will free @Op argument
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080024void MCInst_insert(MCInst *inst, int index, MCOperand *Op)
25{
26 int i;
27
28 for(i = inst->size; i > index; i--)
29 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
30 inst->Operands[i] = inst->Operands[i-1];
31
32 inst->Operands[index] = *Op;
33 inst->size++;
Nguyen Anh Quynh39da4262013-12-04 09:49:33 +080034
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080035 cs_mem_free(Op);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080036}
37
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070038void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
39{
40 int i;
41
42 for(i = inst->size; i > index; i--)
43 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
44 inst->Operands[i] = inst->Operands[i-1];
45
46 inst->Operands[index] = *Op;
47 inst->size++;
48}
49
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080050void MCInst_setOpcode(MCInst *inst, unsigned Op)
51{
52 inst->Opcode = Op;
53}
54
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080055void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
56{
57 inst->OpcodePub = Op;
58}
59
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080060unsigned MCInst_getOpcode(const MCInst *inst)
61{
62 return inst->Opcode;
63}
64
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080065unsigned MCInst_getOpcodePub(const MCInst *inst)
66{
67 return inst->OpcodePub;
68}
69
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080070MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
71{
72 return &inst->Operands[i];
73}
74
75unsigned MCInst_getNumOperands(const MCInst *inst)
76{
77 return inst->size;
78}
79
Nguyen Anh Quynh39da4262013-12-04 09:49:33 +080080// NOTE: this will free @Op argument
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080081int MCInst_addOperand(MCInst *inst, MCOperand *Op)
82{
83 if (inst->size == ARR_SIZE(inst->Operands))
84 // full
85 return -1;
86
87 inst->Operands[inst->size] = *Op;
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080088 cs_mem_free(Op);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080089
90 inst->size++;
91
92 return 0;
93}
94
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070095int MCInst_addOperand0(MCInst *inst, MCOperand *Op)
96{
97 if (inst->size == ARR_SIZE(inst->Operands))
98 // full
99 return -1;
100
101 inst->Operands[inst->size] = *Op;
102
103 inst->size++;
104
105 return 0;
106}
107
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800108// This addOperand2 function doesnt free Op
109int MCInst_addOperand2(MCInst *inst, MCOperand *Op)
110{
111 if (inst->size == ARR_SIZE(inst->Operands))
112 // full
113 return -1;
114
115 inst->Operands[inst->size] = *Op;
116
117 inst->size++;
118
119 return 0;
120}
121
122void MCOperand_Init(MCOperand *op)
123{
124 op->Kind = kInvalid;
125 op->FPImmVal = 0.0;
126}
127
128bool MCOperand_isValid(const MCOperand *op)
129{
130 return op->Kind != kInvalid;
131}
132
133bool MCOperand_isReg(const MCOperand *op)
134{
135 return op->Kind == kRegister;
136}
137
138bool MCOperand_isImm(const MCOperand *op)
139{
140 return op->Kind == kImmediate;
141}
142
143bool MCOperand_isFPImm(const MCOperand *op)
144{
145 return op->Kind == kFPImmediate;
146}
147
148/// getReg - Returns the register number.
149unsigned MCOperand_getReg(const MCOperand *op)
150{
151 return op->RegVal;
152}
153
154/// setReg - Set the register number.
155void MCOperand_setReg(MCOperand *op, unsigned Reg)
156{
157 op->RegVal = Reg;
158}
159
160int64_t MCOperand_getImm(MCOperand *op)
161{
162 return op->ImmVal;
163}
164
165void MCOperand_setImm(MCOperand *op, int64_t Val)
166{
167 op->ImmVal = Val;
168}
169
170double MCOperand_getFPImm(const MCOperand *op)
171{
172 return op->FPImmVal;
173}
174
175void MCOperand_setFPImm(MCOperand *op, double Val)
176{
177 op->FPImmVal = Val;
178}
179
180MCOperand *MCOperand_CreateReg(unsigned Reg)
181{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800182 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800183
184 op->Kind = kRegister;
185 op->RegVal = Reg;
186
187 return op;
188}
189
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700190MCOperand *MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
191{
192 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
193
194 op->Kind = kRegister;
195 op->RegVal = Reg;
196
197 return op;
198}
199
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800200MCOperand *MCOperand_CreateImm(int64_t Val)
201{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800202 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800203
204 op->Kind = kImmediate;
205 op->ImmVal = Val;
206
207 return op;
208}
209
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700210MCOperand *MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
211{
212 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
213
214 op->Kind = kImmediate;
215 op->ImmVal = Val;
216
217 return op;
218}
219
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800220MCOperand *MCOperand_CreateFPImm(double Val)
221{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800222 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800223
224 op->Kind = kFPImmediate;
225 op->FPImmVal = Val;
226
227 return op;
228}