blob: 17cbcf27caf9403bb9bd3718a7cf7cfc7d863641 [file] [log] [blame]
Nguyen Anh Quynh30e4d7f2014-05-08 22:54:58 +08001/* Capstone Disassembly Engine */
Nguyen Anh Quynhbfcaba52015-03-04 17:45:23 +08002/* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08003
reverser160e1982015-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>
reverser160e1982015-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 Quynh29f777b2015-04-07 11:59:26 +080024 inst->ac_idx = 0;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080025}
26
27void MCInst_clear(MCInst *inst)
28{
29 inst->size = 0;
30}
31
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +070032// do not free @Op
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070033void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
34{
35 int i;
36
37 for(i = inst->size; i > index; i--)
38 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
39 inst->Operands[i] = inst->Operands[i-1];
40
41 inst->Operands[index] = *Op;
42 inst->size++;
43}
44
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080045void MCInst_setOpcode(MCInst *inst, unsigned Op)
46{
47 inst->Opcode = Op;
48}
49
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080050void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
51{
52 inst->OpcodePub = Op;
53}
54
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080055unsigned MCInst_getOpcode(const MCInst *inst)
56{
57 return inst->Opcode;
58}
59
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080060unsigned MCInst_getOpcodePub(const MCInst *inst)
61{
62 return inst->OpcodePub;
63}
64
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080065MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
66{
67 return &inst->Operands[i];
68}
69
70unsigned MCInst_getNumOperands(const MCInst *inst)
71{
72 return inst->size;
73}
74
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080075// This addOperand2 function doesnt free Op
Nguyen Anh Quynh264ca372014-06-16 14:52:09 +080076void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080077{
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080078 inst->Operands[inst->size] = *Op;
79
80 inst->size++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080081}
82
83void MCOperand_Init(MCOperand *op)
84{
85 op->Kind = kInvalid;
86 op->FPImmVal = 0.0;
87}
88
89bool MCOperand_isValid(const MCOperand *op)
90{
91 return op->Kind != kInvalid;
92}
93
94bool MCOperand_isReg(const MCOperand *op)
95{
96 return op->Kind == kRegister;
97}
98
99bool MCOperand_isImm(const MCOperand *op)
100{
101 return op->Kind == kImmediate;
102}
103
104bool MCOperand_isFPImm(const MCOperand *op)
105{
106 return op->Kind == kFPImmediate;
107}
108
109/// getReg - Returns the register number.
110unsigned MCOperand_getReg(const MCOperand *op)
111{
112 return op->RegVal;
113}
114
115/// setReg - Set the register number.
116void MCOperand_setReg(MCOperand *op, unsigned Reg)
117{
118 op->RegVal = Reg;
119}
120
121int64_t MCOperand_getImm(MCOperand *op)
122{
123 return op->ImmVal;
124}
125
126void MCOperand_setImm(MCOperand *op, int64_t Val)
127{
128 op->ImmVal = Val;
129}
130
131double MCOperand_getFPImm(const MCOperand *op)
132{
133 return op->FPImmVal;
134}
135
136void MCOperand_setFPImm(MCOperand *op, double Val)
137{
138 op->FPImmVal = Val;
139}
140
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700141MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700142{
143 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
144
145 op->Kind = kRegister;
146 op->RegVal = Reg;
147
148 return op;
149}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800150
151void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
152{
153 MCOperand *op = &(mcInst->Operands[mcInst->size]);
154 mcInst->size++;
155
156 op->Kind = kRegister;
157 op->RegVal = Reg;
158}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700159
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700160MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700161{
162 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
163
164 op->Kind = kImmediate;
165 op->ImmVal = Val;
166
167 return op;
168}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800169
170void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
171{
172 MCOperand *op = &(mcInst->Operands[mcInst->size]);
173 mcInst->size++;
174
175 op->Kind = kImmediate;
176 op->ImmVal = Val;
177}