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