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 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 93 | bool MCOperand_isValid(const MCOperand *op) |
| 94 | { |
| 95 | return op->Kind != kInvalid; |
| 96 | } |
| 97 | |
| 98 | bool MCOperand_isReg(const MCOperand *op) |
| 99 | { |
| 100 | return op->Kind == kRegister; |
| 101 | } |
| 102 | |
| 103 | bool MCOperand_isImm(const MCOperand *op) |
| 104 | { |
| 105 | return op->Kind == kImmediate; |
| 106 | } |
| 107 | |
| 108 | bool MCOperand_isFPImm(const MCOperand *op) |
| 109 | { |
| 110 | return op->Kind == kFPImmediate; |
| 111 | } |
| 112 | |
| 113 | /// getReg - Returns the register number. |
| 114 | unsigned MCOperand_getReg(const MCOperand *op) |
| 115 | { |
| 116 | return op->RegVal; |
| 117 | } |
| 118 | |
| 119 | /// setReg - Set the register number. |
| 120 | void MCOperand_setReg(MCOperand *op, unsigned Reg) |
| 121 | { |
| 122 | op->RegVal = Reg; |
| 123 | } |
| 124 | |
| 125 | int64_t MCOperand_getImm(MCOperand *op) |
| 126 | { |
| 127 | return op->ImmVal; |
| 128 | } |
| 129 | |
| 130 | void MCOperand_setImm(MCOperand *op, int64_t Val) |
| 131 | { |
| 132 | op->ImmVal = Val; |
| 133 | } |
| 134 | |
| 135 | double MCOperand_getFPImm(const MCOperand *op) |
| 136 | { |
| 137 | return op->FPImmVal; |
| 138 | } |
| 139 | |
| 140 | void MCOperand_setFPImm(MCOperand *op, double Val) |
| 141 | { |
| 142 | op->FPImmVal = Val; |
| 143 | } |
| 144 | |
Nguyen Anh Quynh | 0f648ea | 2014-06-10 01:01:23 +0700 | [diff] [blame] | 145 | MCOperand *MCOperand_CreateReg1(MCInst *mcInst, unsigned Reg) |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame] | 146 | { |
| 147 | MCOperand *op = &(mcInst->Operands[MCINST_CACHE]); |
| 148 | |
| 149 | op->Kind = kRegister; |
| 150 | op->RegVal = Reg; |
| 151 | |
| 152 | return op; |
| 153 | } |
Nguyen Anh Quynh | cf08138 | 2014-06-06 00:56:46 +0800 | [diff] [blame] | 154 | |
| 155 | void MCOperand_CreateReg0(MCInst *mcInst, unsigned Reg) |
| 156 | { |
| 157 | MCOperand *op = &(mcInst->Operands[mcInst->size]); |
| 158 | mcInst->size++; |
| 159 | |
| 160 | op->Kind = kRegister; |
| 161 | op->RegVal = Reg; |
| 162 | } |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame] | 163 | |
Nguyen Anh Quynh | 0f648ea | 2014-06-10 01:01:23 +0700 | [diff] [blame] | 164 | MCOperand *MCOperand_CreateImm1(MCInst *mcInst, int64_t Val) |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame] | 165 | { |
| 166 | MCOperand *op = &(mcInst->Operands[MCINST_CACHE]); |
| 167 | |
| 168 | op->Kind = kImmediate; |
| 169 | op->ImmVal = Val; |
| 170 | |
| 171 | return op; |
| 172 | } |
Nguyen Anh Quynh | cf08138 | 2014-06-06 00:56:46 +0800 | [diff] [blame] | 173 | |
| 174 | void MCOperand_CreateImm0(MCInst *mcInst, int64_t Val) |
| 175 | { |
| 176 | MCOperand *op = &(mcInst->Operands[mcInst->size]); |
| 177 | mcInst->size++; |
| 178 | |
| 179 | op->Kind = kImmediate; |
| 180 | op->ImmVal = Val; |
| 181 | } |