blob: 3a70af70c90ab5952c38a64aee3c6f8bd5cddf99 [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{
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080083 inst->Operands[inst->size] = *Op;
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080084 cs_mem_free(Op);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080085
86 inst->size++;
87
88 return 0;
89}
90
91// This addOperand2 function doesnt free Op
92int MCInst_addOperand2(MCInst *inst, MCOperand *Op)
93{
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080094 inst->Operands[inst->size] = *Op;
95
96 inst->size++;
97
98 return 0;
99}
100
101void MCOperand_Init(MCOperand *op)
102{
103 op->Kind = kInvalid;
104 op->FPImmVal = 0.0;
105}
106
107bool MCOperand_isValid(const MCOperand *op)
108{
109 return op->Kind != kInvalid;
110}
111
112bool MCOperand_isReg(const MCOperand *op)
113{
114 return op->Kind == kRegister;
115}
116
117bool MCOperand_isImm(const MCOperand *op)
118{
119 return op->Kind == kImmediate;
120}
121
122bool MCOperand_isFPImm(const MCOperand *op)
123{
124 return op->Kind == kFPImmediate;
125}
126
127/// getReg - Returns the register number.
128unsigned MCOperand_getReg(const MCOperand *op)
129{
130 return op->RegVal;
131}
132
133/// setReg - Set the register number.
134void MCOperand_setReg(MCOperand *op, unsigned Reg)
135{
136 op->RegVal = Reg;
137}
138
139int64_t MCOperand_getImm(MCOperand *op)
140{
141 return op->ImmVal;
142}
143
144void MCOperand_setImm(MCOperand *op, int64_t Val)
145{
146 op->ImmVal = Val;
147}
148
149double MCOperand_getFPImm(const MCOperand *op)
150{
151 return op->FPImmVal;
152}
153
154void MCOperand_setFPImm(MCOperand *op, double Val)
155{
156 op->FPImmVal = Val;
157}
158
159MCOperand *MCOperand_CreateReg(unsigned Reg)
160{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800161 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800162
163 op->Kind = kRegister;
164 op->RegVal = Reg;
165
166 return op;
167}
168
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800169/*
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700170MCOperand *MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
171{
172 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
173
174 op->Kind = kRegister;
175 op->RegVal = Reg;
176
177 return op;
178}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800179*/
180
181void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
182{
183 MCOperand *op = &(mcInst->Operands[mcInst->size]);
184 mcInst->size++;
185
186 op->Kind = kRegister;
187 op->RegVal = Reg;
188}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700189
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800190MCOperand *MCOperand_CreateImm(int64_t Val)
191{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800192 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800193
194 op->Kind = kImmediate;
195 op->ImmVal = Val;
196
197 return op;
198}
199
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800200/*
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700201MCOperand *MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
202{
203 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
204
205 op->Kind = kImmediate;
206 op->ImmVal = Val;
207
208 return op;
209}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800210*/
211
212void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
213{
214 MCOperand *op = &(mcInst->Operands[mcInst->size]);
215 mcInst->size++;
216
217 op->Kind = kImmediate;
218 op->ImmVal = Val;
219}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700220
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800221MCOperand *MCOperand_CreateFPImm(double Val)
222{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800223 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800224
225 op->Kind = kFPImmediate;
226 op->FPImmVal = Val;
227
228 return op;
229}