blob: ddaee450b4269a3251e5172c2e8f6a64ebe38f9b [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 Quynh0f648ea2014-06-10 01:01:23 +070030// do not free @Op
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070031void MCInst_insert0(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++;
41}
42
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080043void MCInst_setOpcode(MCInst *inst, unsigned Op)
44{
45 inst->Opcode = Op;
46}
47
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080048void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
49{
50 inst->OpcodePub = Op;
51}
52
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080053unsigned MCInst_getOpcode(const MCInst *inst)
54{
55 return inst->Opcode;
56}
57
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080058unsigned MCInst_getOpcodePub(const MCInst *inst)
59{
60 return inst->OpcodePub;
61}
62
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080063MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
64{
65 return &inst->Operands[i];
66}
67
68unsigned MCInst_getNumOperands(const MCInst *inst)
69{
70 return inst->size;
71}
72
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080073// This addOperand2 function doesnt free Op
Nguyen Anh Quynh264ca372014-06-16 14:52:09 +080074void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080075{
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080076 inst->Operands[inst->size] = *Op;
77
78 inst->size++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080079}
80
81void MCOperand_Init(MCOperand *op)
82{
83 op->Kind = kInvalid;
84 op->FPImmVal = 0.0;
85}
86
87bool MCOperand_isValid(const MCOperand *op)
88{
89 return op->Kind != kInvalid;
90}
91
92bool MCOperand_isReg(const MCOperand *op)
93{
94 return op->Kind == kRegister;
95}
96
97bool MCOperand_isImm(const MCOperand *op)
98{
99 return op->Kind == kImmediate;
100}
101
102bool MCOperand_isFPImm(const MCOperand *op)
103{
104 return op->Kind == kFPImmediate;
105}
106
107/// getReg - Returns the register number.
108unsigned MCOperand_getReg(const MCOperand *op)
109{
110 return op->RegVal;
111}
112
113/// setReg - Set the register number.
114void MCOperand_setReg(MCOperand *op, unsigned Reg)
115{
116 op->RegVal = Reg;
117}
118
119int64_t MCOperand_getImm(MCOperand *op)
120{
121 return op->ImmVal;
122}
123
124void MCOperand_setImm(MCOperand *op, int64_t Val)
125{
126 op->ImmVal = Val;
127}
128
129double MCOperand_getFPImm(const MCOperand *op)
130{
131 return op->FPImmVal;
132}
133
134void MCOperand_setFPImm(MCOperand *op, double Val)
135{
136 op->FPImmVal = Val;
137}
138
139MCOperand *MCOperand_CreateReg(unsigned Reg)
140{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800141 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800142
143 op->Kind = kRegister;
144 op->RegVal = Reg;
145
146 return op;
147}
148
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700149MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700150{
151 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
152
153 op->Kind = kRegister;
154 op->RegVal = Reg;
155
156 return op;
157}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800158
159void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
160{
161 MCOperand *op = &(mcInst->Operands[mcInst->size]);
162 mcInst->size++;
163
164 op->Kind = kRegister;
165 op->RegVal = Reg;
166}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700167
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800168MCOperand *MCOperand_CreateImm(int64_t Val)
169{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800170 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800171
172 op->Kind = kImmediate;
173 op->ImmVal = Val;
174
175 return op;
176}
177
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700178MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700179{
180 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
181
182 op->Kind = kImmediate;
183 op->ImmVal = Val;
184
185 return op;
186}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800187
188void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
189{
190 MCOperand *op = &(mcInst->Operands[mcInst->size]);
191 mcInst->size++;
192
193 op->Kind = kImmediate;
194 op->ImmVal = Val;
195}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700196
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800197MCOperand *MCOperand_CreateFPImm(double Val)
198{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800199 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800200
201 op->Kind = kFPImmediate;
202 op->FPImmVal = Val;
203
204 return op;
205}