blob: e63e55c26365edeb9142bdcd03306c15c5e6a70c [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001/* Capstone Disassembler Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
8#include "MCInst.h"
9#include "utils.h"
10
11void MCInst_Init(MCInst *inst)
12{
13 memset(inst, 0, sizeof(*inst));
14}
15
16void MCInst_clear(MCInst *inst)
17{
18 inst->size = 0;
19}
20
21void MCInst_insert(MCInst *inst, int index, MCOperand *Op)
22{
23 int i;
24
25 for(i = inst->size; i > index; i--)
26 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
27 inst->Operands[i] = inst->Operands[i-1];
28
29 inst->Operands[index] = *Op;
30 inst->size++;
31}
32
33void MCInst_setOpcode(MCInst *inst, unsigned Op)
34{
35 inst->Opcode = Op;
36}
37
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080038void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
39{
40 inst->OpcodePub = Op;
41}
42
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080043unsigned MCInst_getOpcode(const MCInst *inst)
44{
45 return inst->Opcode;
46}
47
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080048unsigned MCInst_getOpcodePub(const MCInst *inst)
49{
50 return inst->OpcodePub;
51}
52
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080053MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
54{
55 return &inst->Operands[i];
56}
57
58unsigned MCInst_getNumOperands(const MCInst *inst)
59{
60 return inst->size;
61}
62
63int MCInst_addOperand(MCInst *inst, MCOperand *Op)
64{
65 if (inst->size == ARR_SIZE(inst->Operands))
66 // full
67 return -1;
68
69 inst->Operands[inst->size] = *Op;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080070 free(Op);
71
72 inst->size++;
73
74 return 0;
75}
76
77// This addOperand2 function doesnt free Op
78int MCInst_addOperand2(MCInst *inst, MCOperand *Op)
79{
80 if (inst->size == ARR_SIZE(inst->Operands))
81 // full
82 return -1;
83
84 inst->Operands[inst->size] = *Op;
85
86 inst->size++;
87
88 return 0;
89}
90
91void MCOperand_Init(MCOperand *op)
92{
93 op->Kind = kInvalid;
94 op->FPImmVal = 0.0;
95}
96
97bool MCOperand_isValid(const MCOperand *op)
98{
99 return op->Kind != kInvalid;
100}
101
102bool MCOperand_isReg(const MCOperand *op)
103{
104 return op->Kind == kRegister;
105}
106
107bool MCOperand_isImm(const MCOperand *op)
108{
109 return op->Kind == kImmediate;
110}
111
112bool MCOperand_isFPImm(const MCOperand *op)
113{
114 return op->Kind == kFPImmediate;
115}
116
117/// getReg - Returns the register number.
118unsigned MCOperand_getReg(const MCOperand *op)
119{
120 return op->RegVal;
121}
122
123/// setReg - Set the register number.
124void MCOperand_setReg(MCOperand *op, unsigned Reg)
125{
126 op->RegVal = Reg;
127}
128
129int64_t MCOperand_getImm(MCOperand *op)
130{
131 return op->ImmVal;
132}
133
134void MCOperand_setImm(MCOperand *op, int64_t Val)
135{
136 op->ImmVal = Val;
137}
138
139double MCOperand_getFPImm(const MCOperand *op)
140{
141 return op->FPImmVal;
142}
143
144void MCOperand_setFPImm(MCOperand *op, double Val)
145{
146 op->FPImmVal = Val;
147}
148
149MCOperand *MCOperand_CreateReg(unsigned Reg)
150{
151 MCOperand *op = malloc(sizeof(*op));
152
153 op->Kind = kRegister;
154 op->RegVal = Reg;
155
156 return op;
157}
158
159MCOperand *MCOperand_CreateImm(int64_t Val)
160{
161 MCOperand *op = malloc(sizeof(*op));
162
163 op->Kind = kImmediate;
164 op->ImmVal = Val;
165
166 return op;
167}
168
169MCOperand *MCOperand_CreateFPImm(double Val)
170{
171 MCOperand *op = malloc(sizeof(*op));
172
173 op->Kind = kFPImmediate;
174 op->FPImmVal = Val;
175
176 return op;
177}