blob: 46bb4e6e08e50ae69139719088ed5340fe0c320a [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
Nguyen Anh Quynh39da4262013-12-04 09:49:33 +080021// NOTE: this will free @Op argument
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080022void MCInst_insert(MCInst *inst, int index, MCOperand *Op)
23{
24 int i;
25
26 for(i = inst->size; i > index; i--)
27 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
28 inst->Operands[i] = inst->Operands[i-1];
29
30 inst->Operands[index] = *Op;
31 inst->size++;
Nguyen Anh Quynh39da4262013-12-04 09:49:33 +080032
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080033 cs_mem_free(Op);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080034}
35
36void MCInst_setOpcode(MCInst *inst, unsigned Op)
37{
38 inst->Opcode = Op;
39}
40
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080041void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
42{
43 inst->OpcodePub = Op;
44}
45
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080046unsigned MCInst_getOpcode(const MCInst *inst)
47{
48 return inst->Opcode;
49}
50
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080051unsigned MCInst_getOpcodePub(const MCInst *inst)
52{
53 return inst->OpcodePub;
54}
55
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080056MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
57{
58 return &inst->Operands[i];
59}
60
61unsigned MCInst_getNumOperands(const MCInst *inst)
62{
63 return inst->size;
64}
65
Nguyen Anh Quynh39da4262013-12-04 09:49:33 +080066// NOTE: this will free @Op argument
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080067int MCInst_addOperand(MCInst *inst, MCOperand *Op)
68{
69 if (inst->size == ARR_SIZE(inst->Operands))
70 // full
71 return -1;
72
73 inst->Operands[inst->size] = *Op;
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +080074 cs_mem_free(Op);
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080075
76 inst->size++;
77
78 return 0;
79}
80
81// This addOperand2 function doesnt free Op
82int MCInst_addOperand2(MCInst *inst, MCOperand *Op)
83{
84 if (inst->size == ARR_SIZE(inst->Operands))
85 // full
86 return -1;
87
88 inst->Operands[inst->size] = *Op;
89
90 inst->size++;
91
92 return 0;
93}
94
95void MCOperand_Init(MCOperand *op)
96{
97 op->Kind = kInvalid;
98 op->FPImmVal = 0.0;
99}
100
101bool MCOperand_isValid(const MCOperand *op)
102{
103 return op->Kind != kInvalid;
104}
105
106bool MCOperand_isReg(const MCOperand *op)
107{
108 return op->Kind == kRegister;
109}
110
111bool MCOperand_isImm(const MCOperand *op)
112{
113 return op->Kind == kImmediate;
114}
115
116bool MCOperand_isFPImm(const MCOperand *op)
117{
118 return op->Kind == kFPImmediate;
119}
120
121/// getReg - Returns the register number.
122unsigned MCOperand_getReg(const MCOperand *op)
123{
124 return op->RegVal;
125}
126
127/// setReg - Set the register number.
128void MCOperand_setReg(MCOperand *op, unsigned Reg)
129{
130 op->RegVal = Reg;
131}
132
133int64_t MCOperand_getImm(MCOperand *op)
134{
135 return op->ImmVal;
136}
137
138void MCOperand_setImm(MCOperand *op, int64_t Val)
139{
140 op->ImmVal = Val;
141}
142
143double MCOperand_getFPImm(const MCOperand *op)
144{
145 return op->FPImmVal;
146}
147
148void MCOperand_setFPImm(MCOperand *op, double Val)
149{
150 op->FPImmVal = Val;
151}
152
153MCOperand *MCOperand_CreateReg(unsigned Reg)
154{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800155 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800156
157 op->Kind = kRegister;
158 op->RegVal = Reg;
159
160 return op;
161}
162
163MCOperand *MCOperand_CreateImm(int64_t Val)
164{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800165 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800166
167 op->Kind = kImmediate;
168 op->ImmVal = Val;
169
170 return op;
171}
172
173MCOperand *MCOperand_CreateFPImm(double Val)
174{
Nguyen Anh Quynha8eb7a52014-01-11 12:55:31 +0800175 MCOperand *op = cs_mem_malloc(sizeof(*op));
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800176
177 op->Kind = kFPImmediate;
178 op->FPImmVal = Val;
179
180 return op;
181}