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