blob: ab9ded9325e0edda4d0f2e6569350e5a4fdd581d [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 Quynhff7bba32014-11-03 16:32:06 +080017 inst->has_imm = false;
18 inst->op1_size = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080019}
20
21void MCInst_clear(MCInst *inst)
22{
23 inst->size = 0;
24}
25
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +070026// do not free @Op
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070027void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
28{
29 int i;
30
31 for(i = inst->size; i > index; i--)
32 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
33 inst->Operands[i] = inst->Operands[i-1];
34
35 inst->Operands[index] = *Op;
36 inst->size++;
37}
38
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080039void MCInst_setOpcode(MCInst *inst, unsigned Op)
40{
41 inst->Opcode = Op;
42}
43
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080044void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
45{
46 inst->OpcodePub = Op;
47}
48
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080049unsigned MCInst_getOpcode(const MCInst *inst)
50{
51 return inst->Opcode;
52}
53
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080054unsigned MCInst_getOpcodePub(const MCInst *inst)
55{
56 return inst->OpcodePub;
57}
58
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080059MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
60{
61 return &inst->Operands[i];
62}
63
64unsigned MCInst_getNumOperands(const MCInst *inst)
65{
66 return inst->size;
67}
68
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080069// This addOperand2 function doesnt free Op
Nguyen Anh Quynh264ca372014-06-16 14:52:09 +080070void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080071{
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080072 inst->Operands[inst->size] = *Op;
73
74 inst->size++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080075}
76
77void MCOperand_Init(MCOperand *op)
78{
79 op->Kind = kInvalid;
80 op->FPImmVal = 0.0;
81}
82
83bool MCOperand_isValid(const MCOperand *op)
84{
85 return op->Kind != kInvalid;
86}
87
88bool MCOperand_isReg(const MCOperand *op)
89{
90 return op->Kind == kRegister;
91}
92
93bool MCOperand_isImm(const MCOperand *op)
94{
95 return op->Kind == kImmediate;
96}
97
98bool MCOperand_isFPImm(const MCOperand *op)
99{
100 return op->Kind == kFPImmediate;
101}
102
103/// getReg - Returns the register number.
104unsigned MCOperand_getReg(const MCOperand *op)
105{
106 return op->RegVal;
107}
108
109/// setReg - Set the register number.
110void MCOperand_setReg(MCOperand *op, unsigned Reg)
111{
112 op->RegVal = Reg;
113}
114
115int64_t MCOperand_getImm(MCOperand *op)
116{
117 return op->ImmVal;
118}
119
120void MCOperand_setImm(MCOperand *op, int64_t Val)
121{
122 op->ImmVal = Val;
123}
124
125double MCOperand_getFPImm(const MCOperand *op)
126{
127 return op->FPImmVal;
128}
129
130void MCOperand_setFPImm(MCOperand *op, double Val)
131{
132 op->FPImmVal = Val;
133}
134
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700135MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700136{
137 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
138
139 op->Kind = kRegister;
140 op->RegVal = Reg;
141
142 return op;
143}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800144
145void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
146{
147 MCOperand *op = &(mcInst->Operands[mcInst->size]);
148 mcInst->size++;
149
150 op->Kind = kRegister;
151 op->RegVal = Reg;
152}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700153
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700154MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700155{
156 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
157
158 op->Kind = kImmediate;
159 op->ImmVal = Val;
160
161 return op;
162}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800163
164void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
165{
166 MCOperand *op = &(mcInst->Operands[mcInst->size]);
167 mcInst->size++;
168
169 op->Kind = kImmediate;
170 op->ImmVal = Val;
171}