blob: a87c30d2640b50a5ad3808d05fc1ea1de70eafb5 [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
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080093bool MCOperand_isValid(const MCOperand *op)
94{
95 return op->Kind != kInvalid;
96}
97
98bool MCOperand_isReg(const MCOperand *op)
99{
100 return op->Kind == kRegister;
101}
102
103bool MCOperand_isImm(const MCOperand *op)
104{
105 return op->Kind == kImmediate;
106}
107
108bool MCOperand_isFPImm(const MCOperand *op)
109{
110 return op->Kind == kFPImmediate;
111}
112
113/// getReg - Returns the register number.
114unsigned MCOperand_getReg(const MCOperand *op)
115{
116 return op->RegVal;
117}
118
119/// setReg - Set the register number.
120void MCOperand_setReg(MCOperand *op, unsigned Reg)
121{
122 op->RegVal = Reg;
123}
124
125int64_t MCOperand_getImm(MCOperand *op)
126{
127 return op->ImmVal;
128}
129
130void MCOperand_setImm(MCOperand *op, int64_t Val)
131{
132 op->ImmVal = Val;
133}
134
135double MCOperand_getFPImm(const MCOperand *op)
136{
137 return op->FPImmVal;
138}
139
140void MCOperand_setFPImm(MCOperand *op, double Val)
141{
142 op->FPImmVal = Val;
143}
144
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700145MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700146{
147 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
148
149 op->Kind = kRegister;
150 op->RegVal = Reg;
151
152 return op;
153}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800154
155void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
156{
157 MCOperand *op = &(mcInst->Operands[mcInst->size]);
158 mcInst->size++;
159
160 op->Kind = kRegister;
161 op->RegVal = Reg;
162}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700163
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700164MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700165{
166 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
167
168 op->Kind = kImmediate;
169 op->ImmVal = Val;
170
171 return op;
172}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800173
174void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
175{
176 MCOperand *op = &(mcInst->Operands[mcInst->size]);
177 mcInst->size++;
178
179 op->Kind = kImmediate;
180 op->ImmVal = Val;
181}