blob: bdbb82d6c68b4295fe0e149d2c2a6e5a61faf4fc [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
reverserbcf09f42015-04-09 18:28:19 +01004#if defined(CAPSTONE_HAS_OSXKERNEL)
5#include <libkern/libkern.h>
6#else
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08007#include <stdio.h>
8#include <stdlib.h>
reverserbcf09f42015-04-09 18:28:19 +01009#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080010#include <string.h>
11
12#include "MCInst.h"
13#include "utils.h"
14
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070015#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
16
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +080017void MCInst_Init(MCInst *inst)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080018{
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +080019 inst->OpcodePub = 0;
20 inst->size = 0;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +080021 inst->has_imm = false;
22 inst->op1_size = 0;
Nguyen Anh Quynhe19490e2015-01-21 12:15:14 +080023 inst->writeback = false;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080024}
25
26void MCInst_clear(MCInst *inst)
27{
28 inst->size = 0;
29}
30
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +070031// do not free @Op
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070032void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
33{
34 int i;
35
36 for(i = inst->size; i > index; i--)
37 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
38 inst->Operands[i] = inst->Operands[i-1];
39
40 inst->Operands[index] = *Op;
41 inst->size++;
42}
43
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080044void MCInst_setOpcode(MCInst *inst, unsigned Op)
45{
46 inst->Opcode = Op;
47}
48
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080049void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
50{
51 inst->OpcodePub = Op;
52}
53
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080054unsigned MCInst_getOpcode(const MCInst *inst)
55{
56 return inst->Opcode;
57}
58
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080059unsigned MCInst_getOpcodePub(const MCInst *inst)
60{
61 return inst->OpcodePub;
62}
63
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080064MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
65{
66 return &inst->Operands[i];
67}
68
69unsigned MCInst_getNumOperands(const MCInst *inst)
70{
71 return inst->size;
72}
73
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080074// This addOperand2 function doesnt free Op
Nguyen Anh Quynh264ca372014-06-16 14:52:09 +080075void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080076{
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080077 inst->Operands[inst->size] = *Op;
78
79 inst->size++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080080}
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
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700140MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700141{
142 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
143
144 op->Kind = kRegister;
145 op->RegVal = Reg;
146
147 return op;
148}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800149
150void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
151{
152 MCOperand *op = &(mcInst->Operands[mcInst->size]);
153 mcInst->size++;
154
155 op->Kind = kRegister;
156 op->RegVal = Reg;
157}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700158
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700159MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700160{
161 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
162
163 op->Kind = kImmediate;
164 op->ImmVal = Val;
165
166 return op;
167}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800168
169void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
170{
171 MCOperand *op = &(mcInst->Operands[mcInst->size]);
172 mcInst->size++;
173
174 op->Kind = kImmediate;
175 op->ImmVal = Val;
176}