blob: cdd7a0da5596ea526e9b2b9988d4bd5d046fb2c2 [file] [log] [blame]
Nguyen Anh Quynh30e4d7f2014-05-08 22:54:58 +08001/* Capstone Disassembly Engine */
2/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08003
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
8#include "MCInst.h"
9#include "utils.h"
10
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070011#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
12
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +080013void MCInst_Init(MCInst *inst)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014{
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +080015 inst->OpcodePub = 0;
16 inst->size = 0;
Nguyen Anh Quynhf1ec5262014-06-25 22:03:18 +080017 inst->has_imm = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080018}
19
20void MCInst_clear(MCInst *inst)
21{
22 inst->size = 0;
23}
24
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +070025// do not free @Op
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070026void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
27{
28 int i;
29
30 for(i = inst->size; i > index; i--)
31 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
32 inst->Operands[i] = inst->Operands[i-1];
33
34 inst->Operands[index] = *Op;
35 inst->size++;
36}
37
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080038void MCInst_setOpcode(MCInst *inst, unsigned Op)
39{
40 inst->Opcode = Op;
41}
42
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080043void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
44{
45 inst->OpcodePub = Op;
46}
47
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080048unsigned MCInst_getOpcode(const MCInst *inst)
49{
50 return inst->Opcode;
51}
52
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080053unsigned MCInst_getOpcodePub(const MCInst *inst)
54{
55 return inst->OpcodePub;
56}
57
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080058MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
59{
60 return &inst->Operands[i];
61}
62
63unsigned MCInst_getNumOperands(const MCInst *inst)
64{
65 return inst->size;
66}
67
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080068// This addOperand2 function doesnt free Op
Nguyen Anh Quynh264ca372014-06-16 14:52:09 +080069void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080070{
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080071 inst->Operands[inst->size] = *Op;
72
73 inst->size++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080074}
75
76void MCOperand_Init(MCOperand *op)
77{
78 op->Kind = kInvalid;
79 op->FPImmVal = 0.0;
80}
81
82bool MCOperand_isValid(const MCOperand *op)
83{
84 return op->Kind != kInvalid;
85}
86
87bool MCOperand_isReg(const MCOperand *op)
88{
89 return op->Kind == kRegister;
90}
91
92bool MCOperand_isImm(const MCOperand *op)
93{
94 return op->Kind == kImmediate;
95}
96
97bool MCOperand_isFPImm(const MCOperand *op)
98{
99 return op->Kind == kFPImmediate;
100}
101
102/// getReg - Returns the register number.
103unsigned MCOperand_getReg(const MCOperand *op)
104{
105 return op->RegVal;
106}
107
108/// setReg - Set the register number.
109void MCOperand_setReg(MCOperand *op, unsigned Reg)
110{
111 op->RegVal = Reg;
112}
113
114int64_t MCOperand_getImm(MCOperand *op)
115{
116 return op->ImmVal;
117}
118
119void MCOperand_setImm(MCOperand *op, int64_t Val)
120{
121 op->ImmVal = Val;
122}
123
124double MCOperand_getFPImm(const MCOperand *op)
125{
126 return op->FPImmVal;
127}
128
129void MCOperand_setFPImm(MCOperand *op, double Val)
130{
131 op->FPImmVal = Val;
132}
133
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700134MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700135{
136 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
137
138 op->Kind = kRegister;
139 op->RegVal = Reg;
140
141 return op;
142}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800143
144void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
145{
146 MCOperand *op = &(mcInst->Operands[mcInst->size]);
147 mcInst->size++;
148
149 op->Kind = kRegister;
150 op->RegVal = Reg;
151}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700152
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700153MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700154{
155 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
156
157 op->Kind = kImmediate;
158 op->ImmVal = Val;
159
160 return op;
161}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800162
163void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
164{
165 MCOperand *op = &(mcInst->Operands[mcInst->size]);
166 mcInst->size++;
167
168 op->Kind = kImmediate;
169 op->ImmVal = Val;
170}