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