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