blob: 1694e5de1ff13583e8ac8ce92298c007e83d9801 [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)
vit969636d45852018-06-15 00:12:26 +03005#include <Availability.h>
reverser160e1982015-04-09 18:28:19 +01006#include <libkern/libkern.h>
7#else
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08008#include <stdio.h>
9#include <stdlib.h>
reverser160e1982015-04-09 18:28:19 +010010#endif
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080011#include <string.h>
12
13#include "MCInst.h"
14#include "utils.h"
15
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070016#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
17
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +080018void MCInst_Init(MCInst *inst)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080019{
Nguyen Anh Quynh82d2efd2018-06-02 22:22:26 +080020 unsigned int i;
Catena cyberc3e59fe2018-06-02 16:21:09 +020021
Nguyen Anh Quynh82d2efd2018-06-02 22:22:26 +080022 for (i = 0; i < 48; i++) {
Catena cyberc3e59fe2018-06-02 16:21:09 +020023 inst->Operands[i].Kind = kInvalid;
24 }
Nguyen Anh Quynh82d2efd2018-06-02 22:22:26 +080025
obs1dium33f39e12017-10-13 03:04:16 +020026 inst->Opcode = 0;
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +080027 inst->OpcodePub = 0;
28 inst->size = 0;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +080029 inst->has_imm = false;
30 inst->op1_size = 0;
Nguyen Anh Quynhe19490e2015-01-21 12:15:14 +080031 inst->writeback = false;
Nguyen Anh Quynh29f777b2015-04-07 11:59:26 +080032 inst->ac_idx = 0;
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +080033 inst->popcode_adjust = 0;
Nguyen Anh Quynh64328e32017-05-07 11:17:23 +080034 inst->assembly[0] = '\0';
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080035}
36
37void MCInst_clear(MCInst *inst)
38{
39 inst->size = 0;
40}
41
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +070042// do not free @Op
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070043void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
44{
45 int i;
46
47 for(i = inst->size; i > index; i--)
48 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
49 inst->Operands[i] = inst->Operands[i-1];
50
51 inst->Operands[index] = *Op;
52 inst->size++;
53}
54
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080055void MCInst_setOpcode(MCInst *inst, unsigned Op)
56{
57 inst->Opcode = Op;
58}
59
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080060void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
61{
62 inst->OpcodePub = Op;
63}
64
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080065unsigned MCInst_getOpcode(const MCInst *inst)
66{
67 return inst->Opcode;
68}
69
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080070unsigned MCInst_getOpcodePub(const MCInst *inst)
71{
72 return inst->OpcodePub;
73}
74
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080075MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
76{
77 return &inst->Operands[i];
78}
79
80unsigned MCInst_getNumOperands(const MCInst *inst)
81{
82 return inst->size;
83}
84
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080085// This addOperand2 function doesnt free Op
Nguyen Anh Quynh264ca372014-06-16 14:52:09 +080086void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080087{
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080088 inst->Operands[inst->size] = *Op;
89
90 inst->size++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080091}
92
93void MCOperand_Init(MCOperand *op)
94{
95 op->Kind = kInvalid;
96 op->FPImmVal = 0.0;
97}
98
99bool MCOperand_isValid(const MCOperand *op)
100{
101 return op->Kind != kInvalid;
102}
103
104bool MCOperand_isReg(const MCOperand *op)
105{
106 return op->Kind == kRegister;
107}
108
109bool MCOperand_isImm(const MCOperand *op)
110{
111 return op->Kind == kImmediate;
112}
113
114bool MCOperand_isFPImm(const MCOperand *op)
115{
116 return op->Kind == kFPImmediate;
117}
118
119/// getReg - Returns the register number.
120unsigned MCOperand_getReg(const MCOperand *op)
121{
122 return op->RegVal;
123}
124
125/// setReg - Set the register number.
126void MCOperand_setReg(MCOperand *op, unsigned Reg)
127{
128 op->RegVal = Reg;
129}
130
131int64_t MCOperand_getImm(MCOperand *op)
132{
133 return op->ImmVal;
134}
135
136void MCOperand_setImm(MCOperand *op, int64_t Val)
137{
138 op->ImmVal = Val;
139}
140
141double MCOperand_getFPImm(const MCOperand *op)
142{
143 return op->FPImmVal;
144}
145
146void MCOperand_setFPImm(MCOperand *op, double Val)
147{
148 op->FPImmVal = Val;
149}
150
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700151MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700152{
153 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
154
155 op->Kind = kRegister;
156 op->RegVal = Reg;
157
158 return op;
159}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800160
161void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
162{
163 MCOperand *op = &(mcInst->Operands[mcInst->size]);
164 mcInst->size++;
165
166 op->Kind = kRegister;
167 op->RegVal = Reg;
168}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700169
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700170MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700171{
172 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
173
174 op->Kind = kImmediate;
175 op->ImmVal = Val;
176
177 return op;
178}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800179
180void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
181{
182 MCOperand *op = &(mcInst->Operands[mcInst->size]);
183 mcInst->size++;
184
185 op->Kind = kImmediate;
186 op->ImmVal = Val;
187}