blob: 1ec8f8a9ff615584635a4cc5d0df1c8f23a13723 [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
38unsigned MCInst_getOpcode(const MCInst *inst)
39{
40 return inst->Opcode;
41}
42
43MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
44{
45 return &inst->Operands[i];
46}
47
48unsigned MCInst_getNumOperands(const MCInst *inst)
49{
50 return inst->size;
51}
52
53int MCInst_addOperand(MCInst *inst, MCOperand *Op)
54{
55 if (inst->size == ARR_SIZE(inst->Operands))
56 // full
57 return -1;
58
59 inst->Operands[inst->size] = *Op;
60 // FIXME
61 free(Op);
62
63 inst->size++;
64
65 return 0;
66}
67
68// This addOperand2 function doesnt free Op
69int MCInst_addOperand2(MCInst *inst, MCOperand *Op)
70{
71 if (inst->size == ARR_SIZE(inst->Operands))
72 // full
73 return -1;
74
75 inst->Operands[inst->size] = *Op;
76
77 inst->size++;
78
79 return 0;
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_CreateReg(unsigned Reg)
141{
142 MCOperand *op = malloc(sizeof(*op));
143
144 op->Kind = kRegister;
145 op->RegVal = Reg;
146
147 return op;
148}
149
150MCOperand *MCOperand_CreateImm(int64_t Val)
151{
152 MCOperand *op = malloc(sizeof(*op));
153
154 op->Kind = kImmediate;
155 op->ImmVal = Val;
156
157 return op;
158}
159
160MCOperand *MCOperand_CreateFPImm(double Val)
161{
162 MCOperand *op = malloc(sizeof(*op));
163
164 op->Kind = kFPImmediate;
165 op->FPImmVal = Val;
166
167 return op;
168}