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