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