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
17void 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
26void MCInst_clear(MCInst *inst)
27{
28 inst->size = 0;
29}
30
31// do not free @Op
32void 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
44void MCInst_setOpcode(MCInst *inst, unsigned Op)
45{
46 inst->Opcode = Op;
47}
48
49void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
50{
51 inst->OpcodePub = Op;
52}
53
54unsigned MCInst_getOpcode(const MCInst *inst)
55{
56 return inst->Opcode;
57}
58
59unsigned MCInst_getOpcodePub(const MCInst *inst)
60{
61 return inst->OpcodePub;
62}
63
64MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
65{
66 return &inst->Operands[i];
67}
68
69unsigned MCInst_getNumOperands(const MCInst *inst)
70{
71 return inst->size;
72}
73
74// This addOperand2 function doesnt free Op
75void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
76{
77 inst->Operands[inst->size] = *Op;
78
79 inst->size++;
80}
81
82void MCOperand_Init(MCOperand *op)
83{
84 op->Kind = kInvalid;
85 op->FPImmVal = 0.0;
86}
87
88bool MCOperand_isValid(const MCOperand *op)
89{
90 return op->Kind != kInvalid;
91}
92
93bool MCOperand_isReg(const MCOperand *op)
94{
95 return op->Kind == kRegister;
96}
97
98bool MCOperand_isImm(const MCOperand *op)
99{
100 return op->Kind == kImmediate;
101}
102
103bool MCOperand_isFPImm(const MCOperand *op)
104{
105 return op->Kind == kFPImmediate;
106}
107
108/// getReg - Returns the register number.
109unsigned MCOperand_getReg(const MCOperand *op)
110{
111 return op->RegVal;
112}
113
114/// setReg - Set the register number.
115void MCOperand_setReg(MCOperand *op, unsigned Reg)
116{
117 op->RegVal = Reg;
118}
119
120int64_t MCOperand_getImm(MCOperand *op)
121{
122 return op->ImmVal;
123}
124
125void MCOperand_setImm(MCOperand *op, int64_t Val)
126{
127 op->ImmVal = Val;
128}
129
130double MCOperand_getFPImm(const MCOperand *op)
131{
132 return op->FPImmVal;
133}
134
135void MCOperand_setFPImm(MCOperand *op, double Val)
136{
137 op->FPImmVal = Val;
138}
139
140MCOperand *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
150void 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
159MCOperand *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
169void 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