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