blob: c7d8b1763b28872076b21dc86e57cb5a2c84d648 [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;
70 // FIXME
71 free(Op);
72
73 inst->size++;
74
75 return 0;
76}
77
78// This addOperand2 function doesnt free Op
79int MCInst_addOperand2(MCInst *inst, MCOperand *Op)
80{
81 if (inst->size == ARR_SIZE(inst->Operands))
82 // full
83 return -1;
84
85 inst->Operands[inst->size] = *Op;
86
87 inst->size++;
88
89 return 0;
90}
91
92void MCOperand_Init(MCOperand *op)
93{
94 op->Kind = kInvalid;
95 op->FPImmVal = 0.0;
96}
97
98bool MCOperand_isValid(const MCOperand *op)
99{
100 return op->Kind != kInvalid;
101}
102
103bool MCOperand_isReg(const MCOperand *op)
104{
105 return op->Kind == kRegister;
106}
107
108bool MCOperand_isImm(const MCOperand *op)
109{
110 return op->Kind == kImmediate;
111}
112
113bool MCOperand_isFPImm(const MCOperand *op)
114{
115 return op->Kind == kFPImmediate;
116}
117
118/// getReg - Returns the register number.
119unsigned MCOperand_getReg(const MCOperand *op)
120{
121 return op->RegVal;
122}
123
124/// setReg - Set the register number.
125void MCOperand_setReg(MCOperand *op, unsigned Reg)
126{
127 op->RegVal = Reg;
128}
129
130int64_t MCOperand_getImm(MCOperand *op)
131{
132 return op->ImmVal;
133}
134
135void MCOperand_setImm(MCOperand *op, int64_t Val)
136{
137 op->ImmVal = Val;
138}
139
140double MCOperand_getFPImm(const MCOperand *op)
141{
142 return op->FPImmVal;
143}
144
145void MCOperand_setFPImm(MCOperand *op, double Val)
146{
147 op->FPImmVal = Val;
148}
149
150MCOperand *MCOperand_CreateReg(unsigned Reg)
151{
152 MCOperand *op = malloc(sizeof(*op));
153
154 op->Kind = kRegister;
155 op->RegVal = Reg;
156
157 return op;
158}
159
160MCOperand *MCOperand_CreateImm(int64_t Val)
161{
162 MCOperand *op = malloc(sizeof(*op));
163
164 op->Kind = kImmediate;
165 op->ImmVal = Val;
166
167 return op;
168}
169
170MCOperand *MCOperand_CreateFPImm(double Val)
171{
172 MCOperand *op = malloc(sizeof(*op));
173
174 op->Kind = kFPImmediate;
175 op->FPImmVal = Val;
176
177 return op;
178}