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