blob: cc062d0044620deeb9ae5d537a22c53707add95e [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{
obs1dium33f39e12017-10-13 03:04:16 +020019 inst->Opcode = 0;
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +080020 inst->OpcodePub = 0;
21 inst->size = 0;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +080022 inst->has_imm = false;
23 inst->op1_size = 0;
Nguyen Anh Quynhe19490e2015-01-21 12:15:14 +080024 inst->writeback = false;
Nguyen Anh Quynh29f777b2015-04-07 11:59:26 +080025 inst->ac_idx = 0;
Nguyen Anh Quynhdabc9f22016-07-15 20:37:19 +080026 inst->popcode_adjust = 0;
Nguyen Anh Quynh64328e32017-05-07 11:17:23 +080027 inst->assembly[0] = '\0';
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080028}
29
30void MCInst_clear(MCInst *inst)
31{
32 inst->size = 0;
33}
34
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +070035// do not free @Op
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070036void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
37{
38 int i;
39
40 for(i = inst->size; i > index; i--)
41 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
42 inst->Operands[i] = inst->Operands[i-1];
43
44 inst->Operands[index] = *Op;
45 inst->size++;
46}
47
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080048void MCInst_setOpcode(MCInst *inst, unsigned Op)
49{
50 inst->Opcode = Op;
51}
52
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080053void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
54{
55 inst->OpcodePub = Op;
56}
57
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080058unsigned MCInst_getOpcode(const MCInst *inst)
59{
60 return inst->Opcode;
61}
62
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080063unsigned MCInst_getOpcodePub(const MCInst *inst)
64{
65 return inst->OpcodePub;
66}
67
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080068MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
69{
70 return &inst->Operands[i];
71}
72
73unsigned MCInst_getNumOperands(const MCInst *inst)
74{
75 return inst->size;
76}
77
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080078// This addOperand2 function doesnt free Op
Nguyen Anh Quynh264ca372014-06-16 14:52:09 +080079void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080080{
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080081 inst->Operands[inst->size] = *Op;
82
83 inst->size++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080084}
85
86void MCOperand_Init(MCOperand *op)
87{
88 op->Kind = kInvalid;
89 op->FPImmVal = 0.0;
90}
91
92bool MCOperand_isValid(const MCOperand *op)
93{
94 return op->Kind != kInvalid;
95}
96
97bool MCOperand_isReg(const MCOperand *op)
98{
99 return op->Kind == kRegister;
100}
101
102bool MCOperand_isImm(const MCOperand *op)
103{
104 return op->Kind == kImmediate;
105}
106
107bool MCOperand_isFPImm(const MCOperand *op)
108{
109 return op->Kind == kFPImmediate;
110}
111
112/// getReg - Returns the register number.
113unsigned MCOperand_getReg(const MCOperand *op)
114{
115 return op->RegVal;
116}
117
118/// setReg - Set the register number.
119void MCOperand_setReg(MCOperand *op, unsigned Reg)
120{
121 op->RegVal = Reg;
122}
123
124int64_t MCOperand_getImm(MCOperand *op)
125{
126 return op->ImmVal;
127}
128
129void MCOperand_setImm(MCOperand *op, int64_t Val)
130{
131 op->ImmVal = Val;
132}
133
134double MCOperand_getFPImm(const MCOperand *op)
135{
136 return op->FPImmVal;
137}
138
139void MCOperand_setFPImm(MCOperand *op, double Val)
140{
141 op->FPImmVal = Val;
142}
143
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700144MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700145{
146 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
147
148 op->Kind = kRegister;
149 op->RegVal = Reg;
150
151 return op;
152}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800153
154void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
155{
156 MCOperand *op = &(mcInst->Operands[mcInst->size]);
157 mcInst->size++;
158
159 op->Kind = kRegister;
160 op->RegVal = Reg;
161}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700162
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700163MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700164{
165 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
166
167 op->Kind = kImmediate;
168 op->ImmVal = Val;
169
170 return op;
171}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800172
173void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
174{
175 MCOperand *op = &(mcInst->Operands[mcInst->size]);
176 mcInst->size++;
177
178 op->Kind = kImmediate;
179 op->ImmVal = Val;
180}