blob: f4baf4e0fd6995cc51a525ac7ad4a24d7922bb95 [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
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7
8#include "MCInst.h"
9#include "utils.h"
10
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070011#define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1)
12
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +080013void MCInst_Init(MCInst *inst)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080014{
Nguyen Anh Quynh495295e2014-06-16 15:54:32 +080015 inst->OpcodePub = 0;
16 inst->size = 0;
Nguyen Anh Quynhff7bba32014-11-03 16:32:06 +080017 inst->has_imm = false;
18 inst->op1_size = 0;
Nguyen Anh Quynhe19490e2015-01-21 12:15:14 +080019 inst->writeback = false;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080020}
21
22void MCInst_clear(MCInst *inst)
23{
24 inst->size = 0;
25}
26
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +070027// do not free @Op
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +070028void MCInst_insert0(MCInst *inst, int index, MCOperand *Op)
29{
30 int i;
31
32 for(i = inst->size; i > index; i--)
33 //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand));
34 inst->Operands[i] = inst->Operands[i-1];
35
36 inst->Operands[index] = *Op;
37 inst->size++;
38}
39
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080040void MCInst_setOpcode(MCInst *inst, unsigned Op)
41{
42 inst->Opcode = Op;
43}
44
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080045void MCInst_setOpcodePub(MCInst *inst, unsigned Op)
46{
47 inst->OpcodePub = Op;
48}
49
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080050unsigned MCInst_getOpcode(const MCInst *inst)
51{
52 return inst->Opcode;
53}
54
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +080055unsigned MCInst_getOpcodePub(const MCInst *inst)
56{
57 return inst->OpcodePub;
58}
59
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080060MCOperand *MCInst_getOperand(MCInst *inst, unsigned i)
61{
62 return &inst->Operands[i];
63}
64
65unsigned MCInst_getNumOperands(const MCInst *inst)
66{
67 return inst->size;
68}
69
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080070// This addOperand2 function doesnt free Op
Nguyen Anh Quynh264ca372014-06-16 14:52:09 +080071void MCInst_addOperand2(MCInst *inst, MCOperand *Op)
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080072{
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080073 inst->Operands[inst->size] = *Op;
74
75 inst->size++;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080076}
77
78void MCOperand_Init(MCOperand *op)
79{
80 op->Kind = kInvalid;
81 op->FPImmVal = 0.0;
82}
83
84bool MCOperand_isValid(const MCOperand *op)
85{
86 return op->Kind != kInvalid;
87}
88
89bool MCOperand_isReg(const MCOperand *op)
90{
91 return op->Kind == kRegister;
92}
93
94bool MCOperand_isImm(const MCOperand *op)
95{
96 return op->Kind == kImmediate;
97}
98
99bool MCOperand_isFPImm(const MCOperand *op)
100{
101 return op->Kind == kFPImmediate;
102}
103
104/// getReg - Returns the register number.
105unsigned MCOperand_getReg(const MCOperand *op)
106{
107 return op->RegVal;
108}
109
110/// setReg - Set the register number.
111void MCOperand_setReg(MCOperand *op, unsigned Reg)
112{
113 op->RegVal = Reg;
114}
115
116int64_t MCOperand_getImm(MCOperand *op)
117{
118 return op->ImmVal;
119}
120
121void MCOperand_setImm(MCOperand *op, int64_t Val)
122{
123 op->ImmVal = Val;
124}
125
126double MCOperand_getFPImm(const MCOperand *op)
127{
128 return op->FPImmVal;
129}
130
131void MCOperand_setFPImm(MCOperand *op, double Val)
132{
133 op->FPImmVal = Val;
134}
135
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700136MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700137{
138 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
139
140 op->Kind = kRegister;
141 op->RegVal = Reg;
142
143 return op;
144}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800145
146void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg)
147{
148 MCOperand *op = &(mcInst->Operands[mcInst->size]);
149 mcInst->size++;
150
151 op->Kind = kRegister;
152 op->RegVal = Reg;
153}
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700154
Nguyen Anh Quynh0f648ea2014-06-10 01:01:23 +0700155MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val)
Nguyen Anh Quynh937e4832014-06-04 22:51:51 +0700156{
157 MCOperand *op = &(mcInst->Operands[MCINST_CACHE]);
158
159 op->Kind = kImmediate;
160 op->ImmVal = Val;
161
162 return op;
163}
Nguyen Anh Quynhcf081382014-06-06 00:56:46 +0800164
165void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val)
166{
167 MCOperand *op = &(mcInst->Operands[mcInst->size]);
168 mcInst->size++;
169
170 op->Kind = kImmediate;
171 op->ImmVal = Val;
172}