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