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