Nguyen Anh Quynh | 30e4d7f | 2014-05-08 22:54:58 +0800 | [diff] [blame] | 1 | /* Capstone Disassembly Engine */ |
| 2 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2014 */ |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | |
| 8 | #include "MCInst.h" |
| 9 | #include "utils.h" |
| 10 | |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame^] | 11 | #define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1) |
| 12 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 13 | void MCInst_Init(MCInst *inst) |
| 14 | { |
| 15 | memset(inst, 0, sizeof(*inst)); |
| 16 | } |
| 17 | |
| 18 | void MCInst_clear(MCInst *inst) |
| 19 | { |
| 20 | inst->size = 0; |
| 21 | } |
| 22 | |
Nguyen Anh Quynh | 39da426 | 2013-12-04 09:49:33 +0800 | [diff] [blame] | 23 | // NOTE: this will free @Op argument |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 24 | void MCInst_insert(MCInst *inst, int index, MCOperand *Op) |
| 25 | { |
| 26 | int i; |
| 27 | |
| 28 | for(i = inst->size; i > index; i--) |
| 29 | //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand)); |
| 30 | inst->Operands[i] = inst->Operands[i-1]; |
| 31 | |
| 32 | inst->Operands[index] = *Op; |
| 33 | inst->size++; |
Nguyen Anh Quynh | 39da426 | 2013-12-04 09:49:33 +0800 | [diff] [blame] | 34 | |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 35 | cs_mem_free(Op); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 36 | } |
| 37 | |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame^] | 38 | void MCInst_insert0(MCInst *inst, int index, MCOperand *Op) |
| 39 | { |
| 40 | int i; |
| 41 | |
| 42 | for(i = inst->size; i > index; i--) |
| 43 | //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand)); |
| 44 | inst->Operands[i] = inst->Operands[i-1]; |
| 45 | |
| 46 | inst->Operands[index] = *Op; |
| 47 | inst->size++; |
| 48 | } |
| 49 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 50 | void MCInst_setOpcode(MCInst *inst, unsigned Op) |
| 51 | { |
| 52 | inst->Opcode = Op; |
| 53 | } |
| 54 | |
Nguyen Anh Quynh | 6b7abe3 | 2013-11-30 00:54:24 +0800 | [diff] [blame] | 55 | void MCInst_setOpcodePub(MCInst *inst, unsigned Op) |
| 56 | { |
| 57 | inst->OpcodePub = Op; |
| 58 | } |
| 59 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 60 | unsigned MCInst_getOpcode(const MCInst *inst) |
| 61 | { |
| 62 | return inst->Opcode; |
| 63 | } |
| 64 | |
Nguyen Anh Quynh | 6b7abe3 | 2013-11-30 00:54:24 +0800 | [diff] [blame] | 65 | unsigned MCInst_getOpcodePub(const MCInst *inst) |
| 66 | { |
| 67 | return inst->OpcodePub; |
| 68 | } |
| 69 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 70 | MCOperand *MCInst_getOperand(MCInst *inst, unsigned i) |
| 71 | { |
| 72 | return &inst->Operands[i]; |
| 73 | } |
| 74 | |
| 75 | unsigned MCInst_getNumOperands(const MCInst *inst) |
| 76 | { |
| 77 | return inst->size; |
| 78 | } |
| 79 | |
Nguyen Anh Quynh | 39da426 | 2013-12-04 09:49:33 +0800 | [diff] [blame] | 80 | // NOTE: this will free @Op argument |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 81 | int MCInst_addOperand(MCInst *inst, MCOperand *Op) |
| 82 | { |
| 83 | if (inst->size == ARR_SIZE(inst->Operands)) |
| 84 | // full |
| 85 | return -1; |
| 86 | |
| 87 | inst->Operands[inst->size] = *Op; |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 88 | cs_mem_free(Op); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 89 | |
| 90 | inst->size++; |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame^] | 95 | int MCInst_addOperand0(MCInst *inst, MCOperand *Op) |
| 96 | { |
| 97 | if (inst->size == ARR_SIZE(inst->Operands)) |
| 98 | // full |
| 99 | return -1; |
| 100 | |
| 101 | inst->Operands[inst->size] = *Op; |
| 102 | |
| 103 | inst->size++; |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 108 | // This addOperand2 function doesnt free Op |
| 109 | int MCInst_addOperand2(MCInst *inst, MCOperand *Op) |
| 110 | { |
| 111 | if (inst->size == ARR_SIZE(inst->Operands)) |
| 112 | // full |
| 113 | return -1; |
| 114 | |
| 115 | inst->Operands[inst->size] = *Op; |
| 116 | |
| 117 | inst->size++; |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | void MCOperand_Init(MCOperand *op) |
| 123 | { |
| 124 | op->Kind = kInvalid; |
| 125 | op->FPImmVal = 0.0; |
| 126 | } |
| 127 | |
| 128 | bool MCOperand_isValid(const MCOperand *op) |
| 129 | { |
| 130 | return op->Kind != kInvalid; |
| 131 | } |
| 132 | |
| 133 | bool MCOperand_isReg(const MCOperand *op) |
| 134 | { |
| 135 | return op->Kind == kRegister; |
| 136 | } |
| 137 | |
| 138 | bool MCOperand_isImm(const MCOperand *op) |
| 139 | { |
| 140 | return op->Kind == kImmediate; |
| 141 | } |
| 142 | |
| 143 | bool MCOperand_isFPImm(const MCOperand *op) |
| 144 | { |
| 145 | return op->Kind == kFPImmediate; |
| 146 | } |
| 147 | |
| 148 | /// getReg - Returns the register number. |
| 149 | unsigned MCOperand_getReg(const MCOperand *op) |
| 150 | { |
| 151 | return op->RegVal; |
| 152 | } |
| 153 | |
| 154 | /// setReg - Set the register number. |
| 155 | void MCOperand_setReg(MCOperand *op, unsigned Reg) |
| 156 | { |
| 157 | op->RegVal = Reg; |
| 158 | } |
| 159 | |
| 160 | int64_t MCOperand_getImm(MCOperand *op) |
| 161 | { |
| 162 | return op->ImmVal; |
| 163 | } |
| 164 | |
| 165 | void MCOperand_setImm(MCOperand *op, int64_t Val) |
| 166 | { |
| 167 | op->ImmVal = Val; |
| 168 | } |
| 169 | |
| 170 | double MCOperand_getFPImm(const MCOperand *op) |
| 171 | { |
| 172 | return op->FPImmVal; |
| 173 | } |
| 174 | |
| 175 | void MCOperand_setFPImm(MCOperand *op, double Val) |
| 176 | { |
| 177 | op->FPImmVal = Val; |
| 178 | } |
| 179 | |
| 180 | MCOperand *MCOperand_CreateReg(unsigned Reg) |
| 181 | { |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 182 | MCOperand *op = cs_mem_malloc(sizeof(*op)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 183 | |
| 184 | op->Kind = kRegister; |
| 185 | op->RegVal = Reg; |
| 186 | |
| 187 | return op; |
| 188 | } |
| 189 | |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame^] | 190 | MCOperand *MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg) |
| 191 | { |
| 192 | MCOperand *op = &(mcInst->Operands[MCINST_CACHE]); |
| 193 | |
| 194 | op->Kind = kRegister; |
| 195 | op->RegVal = Reg; |
| 196 | |
| 197 | return op; |
| 198 | } |
| 199 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 200 | MCOperand *MCOperand_CreateImm(int64_t Val) |
| 201 | { |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 202 | MCOperand *op = cs_mem_malloc(sizeof(*op)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 203 | |
| 204 | op->Kind = kImmediate; |
| 205 | op->ImmVal = Val; |
| 206 | |
| 207 | return op; |
| 208 | } |
| 209 | |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame^] | 210 | MCOperand *MCOperand_CreateImm0(MCInst *mcInst, int64_t Val) |
| 211 | { |
| 212 | MCOperand *op = &(mcInst->Operands[MCINST_CACHE]); |
| 213 | |
| 214 | op->Kind = kImmediate; |
| 215 | op->ImmVal = Val; |
| 216 | |
| 217 | return op; |
| 218 | } |
| 219 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 220 | MCOperand *MCOperand_CreateFPImm(double Val) |
| 221 | { |
Nguyen Anh Quynh | a8eb7a5 | 2014-01-11 12:55:31 +0800 | [diff] [blame] | 222 | MCOperand *op = cs_mem_malloc(sizeof(*op)); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 223 | |
| 224 | op->Kind = kFPImmediate; |
| 225 | op->FPImmVal = Val; |
| 226 | |
| 227 | return op; |
| 228 | } |