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