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 | |
reverser | bcf09f4 | 2015-04-09 18:28:19 +0100 | [diff] [blame] | 4 | #if defined(CAPSTONE_HAS_OSXKERNEL) |
| 5 | #include <libkern/libkern.h> |
| 6 | #else |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
reverser | bcf09f4 | 2015-04-09 18:28:19 +0100 | [diff] [blame] | 9 | #endif |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 10 | #include <string.h> |
| 11 | |
| 12 | #include "MCInst.h" |
| 13 | #include "utils.h" |
| 14 | |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame] | 15 | #define MCINST_CACHE (ARR_SIZE(mcInst->Operands) - 1) |
| 16 | |
Nguyen Anh Quynh | 495295e | 2014-06-16 15:54:32 +0800 | [diff] [blame] | 17 | void MCInst_Init(MCInst *inst) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 18 | { |
Nguyen Anh Quynh | 495295e | 2014-06-16 15:54:32 +0800 | [diff] [blame] | 19 | inst->OpcodePub = 0; |
| 20 | inst->size = 0; |
Nguyen Anh Quynh | ff7bba3 | 2014-11-03 16:32:06 +0800 | [diff] [blame] | 21 | inst->has_imm = false; |
| 22 | inst->op1_size = 0; |
Nguyen Anh Quynh | e19490e | 2015-01-21 12:15:14 +0800 | [diff] [blame] | 23 | inst->writeback = false; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | void MCInst_clear(MCInst *inst) |
| 27 | { |
| 28 | inst->size = 0; |
| 29 | } |
| 30 | |
Nguyen Anh Quynh | 0f648ea | 2014-06-10 01:01:23 +0700 | [diff] [blame] | 31 | // do not free @Op |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame] | 32 | void MCInst_insert0(MCInst *inst, int index, MCOperand *Op) |
| 33 | { |
| 34 | int i; |
| 35 | |
| 36 | for(i = inst->size; i > index; i--) |
| 37 | //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand)); |
| 38 | inst->Operands[i] = inst->Operands[i-1]; |
| 39 | |
| 40 | inst->Operands[index] = *Op; |
| 41 | inst->size++; |
| 42 | } |
| 43 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 44 | void MCInst_setOpcode(MCInst *inst, unsigned Op) |
| 45 | { |
| 46 | inst->Opcode = Op; |
| 47 | } |
| 48 | |
Nguyen Anh Quynh | 6b7abe3 | 2013-11-30 00:54:24 +0800 | [diff] [blame] | 49 | void MCInst_setOpcodePub(MCInst *inst, unsigned Op) |
| 50 | { |
| 51 | inst->OpcodePub = Op; |
| 52 | } |
| 53 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 54 | unsigned MCInst_getOpcode(const MCInst *inst) |
| 55 | { |
| 56 | return inst->Opcode; |
| 57 | } |
| 58 | |
Nguyen Anh Quynh | 6b7abe3 | 2013-11-30 00:54:24 +0800 | [diff] [blame] | 59 | unsigned MCInst_getOpcodePub(const MCInst *inst) |
| 60 | { |
| 61 | return inst->OpcodePub; |
| 62 | } |
| 63 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 64 | MCOperand *MCInst_getOperand(MCInst *inst, unsigned i) |
| 65 | { |
| 66 | return &inst->Operands[i]; |
| 67 | } |
| 68 | |
| 69 | unsigned MCInst_getNumOperands(const MCInst *inst) |
| 70 | { |
| 71 | return inst->size; |
| 72 | } |
| 73 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 74 | // This addOperand2 function doesnt free Op |
Nguyen Anh Quynh | 264ca37 | 2014-06-16 14:52:09 +0800 | [diff] [blame] | 75 | void MCInst_addOperand2(MCInst *inst, MCOperand *Op) |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 76 | { |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 77 | inst->Operands[inst->size] = *Op; |
| 78 | |
| 79 | inst->size++; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void MCOperand_Init(MCOperand *op) |
| 83 | { |
| 84 | op->Kind = kInvalid; |
| 85 | op->FPImmVal = 0.0; |
| 86 | } |
| 87 | |
| 88 | bool MCOperand_isValid(const MCOperand *op) |
| 89 | { |
| 90 | return op->Kind != kInvalid; |
| 91 | } |
| 92 | |
| 93 | bool MCOperand_isReg(const MCOperand *op) |
| 94 | { |
| 95 | return op->Kind == kRegister; |
| 96 | } |
| 97 | |
| 98 | bool MCOperand_isImm(const MCOperand *op) |
| 99 | { |
| 100 | return op->Kind == kImmediate; |
| 101 | } |
| 102 | |
| 103 | bool MCOperand_isFPImm(const MCOperand *op) |
| 104 | { |
| 105 | return op->Kind == kFPImmediate; |
| 106 | } |
| 107 | |
| 108 | /// getReg - Returns the register number. |
| 109 | unsigned MCOperand_getReg(const MCOperand *op) |
| 110 | { |
| 111 | return op->RegVal; |
| 112 | } |
| 113 | |
| 114 | /// setReg - Set the register number. |
| 115 | void MCOperand_setReg(MCOperand *op, unsigned Reg) |
| 116 | { |
| 117 | op->RegVal = Reg; |
| 118 | } |
| 119 | |
| 120 | int64_t MCOperand_getImm(MCOperand *op) |
| 121 | { |
| 122 | return op->ImmVal; |
| 123 | } |
| 124 | |
| 125 | void MCOperand_setImm(MCOperand *op, int64_t Val) |
| 126 | { |
| 127 | op->ImmVal = Val; |
| 128 | } |
| 129 | |
| 130 | double MCOperand_getFPImm(const MCOperand *op) |
| 131 | { |
| 132 | return op->FPImmVal; |
| 133 | } |
| 134 | |
| 135 | void MCOperand_setFPImm(MCOperand *op, double Val) |
| 136 | { |
| 137 | op->FPImmVal = Val; |
| 138 | } |
| 139 | |
Nguyen Anh Quynh | 0f648ea | 2014-06-10 01:01:23 +0700 | [diff] [blame] | 140 | MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg) |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame] | 141 | { |
| 142 | MCOperand *op = &(mcInst->Operands[MCINST_CACHE]); |
| 143 | |
| 144 | op->Kind = kRegister; |
| 145 | op->RegVal = Reg; |
| 146 | |
| 147 | return op; |
| 148 | } |
Nguyen Anh Quynh | cf08138 | 2014-06-06 00:56:46 +0800 | [diff] [blame] | 149 | |
| 150 | void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg) |
| 151 | { |
| 152 | MCOperand *op = &(mcInst->Operands[mcInst->size]); |
| 153 | mcInst->size++; |
| 154 | |
| 155 | op->Kind = kRegister; |
| 156 | op->RegVal = Reg; |
| 157 | } |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame] | 158 | |
Nguyen Anh Quynh | 0f648ea | 2014-06-10 01:01:23 +0700 | [diff] [blame] | 159 | MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val) |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame] | 160 | { |
| 161 | MCOperand *op = &(mcInst->Operands[MCINST_CACHE]); |
| 162 | |
| 163 | op->Kind = kImmediate; |
| 164 | op->ImmVal = Val; |
| 165 | |
| 166 | return op; |
| 167 | } |
Nguyen Anh Quynh | cf08138 | 2014-06-06 00:56:46 +0800 | [diff] [blame] | 168 | |
| 169 | void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val) |
| 170 | { |
| 171 | MCOperand *op = &(mcInst->Operands[mcInst->size]); |
| 172 | mcInst->size++; |
| 173 | |
| 174 | op->Kind = kImmediate; |
| 175 | op->ImmVal = Val; |
| 176 | } |