| 1 | /* Capstone Disassembly Engine */cxx . . $ `<file>/MCInst.c`/ |
| 2 | /* By Nguyen Anh Quynh <[email protected]>, 2013-2019 */ |
| 3 | |
| 4 | #if defined(CAPSTONE_HAS_OSXKERNEL) |
| 5 | #include <Availability.h> |
| 6 | #include <libkern/libkern.h> |
| 7 | #else |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #endif |
| 11 | #include <string.h> |
| 12 | #include <assert.h> |
| 13 | |
| 14 | #include "MCInst.h"cxx . . $ `<file>/MCInst.h`/ |
| 15 | #include "utils.h"cxx . . $ `<file>/utils.h`/ |
| 16 | |
| 17 | #define MCINST_CACHEcxx . . $ `MCInst.c:17:9`! (ARR_SIZEcxx . . $ `utils.h:24:9`!(mcInst->Operands) - 1) |
| 18 | |
| 19 | void MCInst_Initcxx . . $ MCInst_Init(94c8a3269074abe9).(MCInstcxx . . $ MCInst# *inst) |
| 20 | { |
| 21 | // unnecessary to initialize in loop . its expensive and inst->size shuold be honored |
| 22 | inst->Operandscxx . . $ MCInst#Operands.[0].Kindcxx . . $ MCOperand#Kind. = kInvalidcxx . . $ MCOperand#kInvalid.; |
| 23 | inst->Operandscxx . . $ MCInst#Operands.[0].ImmValcxx . . $ MCOperand#$anonymous_type_1#ImmVal. = 0; |
| 24 | |
| 25 | inst->Opcodecxx . . $ MCInst#Opcode. = 0; |
| 26 | inst->OpcodePubcxx . . $ MCInst#OpcodePub. = 0; |
| 27 | inst->sizecxx . . $ MCInst#size. = 0; |
| 28 | inst->has_immcxx . . $ MCInst#has_imm. = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 29 | inst->op1_sizecxx . . $ MCInst#op1_size. = 0; |
| 30 | inst->writebackcxx . . $ MCInst#writeback. = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 31 | inst->ac_idxcxx . . $ MCInst#ac_idx. = 0; |
| 32 | inst->popcode_adjustcxx . . $ MCInst#popcode_adjust. = 0; |
| 33 | inst->assemblycxx . . $ MCInst#assembly.[0] = '\0'; |
| 34 | inst->wasm_datacxx . . $ MCInst#wasm_data..typecxx . . $ cs_wasm_op#type. = WASM_OP_INVALIDcxx . . $ WASM_OP_INVALID.; |
| 35 | inst->xAcquireReleasecxx . . $ MCInst#xAcquireRelease. = 0; |
| 36 | for (int i = 0; i < MAX_MC_OPScxx . . $ `MCInst.h:96:9`!; ++i) |
| 37 | inst->tied_op_idxcxx . . $ MCInst#tied_op_idx.[i] = -1; |
| 38 | } |
| 39 | |
| 40 | void MCInst_clearcxx . . $ MCInst_clear(94c8a3269074abe9).(MCInstcxx . . $ MCInst# *inst) |
| 41 | { |
| 42 | inst->sizecxx . . $ MCInst#size. = 0; |
| 43 | } |
| 44 | |
| 45 | // does not free @Op |
| 46 | void MCInst_insert0cxx . . $ MCInst_insert0(add3700b84a5c3fa).(MCInstcxx . . $ MCInst# *inst, int index, MCOperandcxx . . $ MCOperand# *Op) |
| 47 | { |
| 48 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(index < MAX_MC_OPScxx . . $ `MCInst.h:96:9`!); |
| 49 | int i; |
| 50 | |
| 51 | for(i = inst->sizecxx . . $ MCInst#size.; i > index; i--) |
| 52 | //memcpy(&(inst->Operands[i]), &(inst->Operands[i-1]), sizeof(MCOperand)); |
| 53 | inst->Operandscxx . . $ MCInst#Operands.[i] = inst->Operandscxx . . $ MCInst#Operands.[i-1]; |
| 54 | |
| 55 | inst->Operandscxx . . $ MCInst#Operands.[index] = *Op; |
| 56 | inst->sizecxx . . $ MCInst#size.++; |
| 57 | } |
| 58 | |
| 59 | void MCInst_setOpcodecxx . . $ MCInst_setOpcode(1d3fdd902ae43da2).(MCInstcxx . . $ MCInst# *inst, unsigned Op) |
| 60 | { |
| 61 | inst->Opcodecxx . . $ MCInst#Opcode. = Op; |
| 62 | } |
| 63 | |
| 64 | void MCInst_setOpcodePubcxx . . $ MCInst_setOpcodePub(1d3fdd902ae43da2).(MCInstcxx . . $ MCInst# *inst, unsigned Op) |
| 65 | { |
| 66 | inst->OpcodePubcxx . . $ MCInst#OpcodePub. = Op; |
| 67 | } |
| 68 | |
| 69 | unsigned MCInst_getOpcodecxx . . $ MCInst_getOpcode(66549afbf0596d6c).(const MCInstcxx . . $ MCInst# *inst) |
| 70 | { |
| 71 | return inst->Opcodecxx . . $ MCInst#Opcode.; |
| 72 | } |
| 73 | |
| 74 | unsigned MCInst_getOpcodePubcxx . . $ MCInst_getOpcodePub(66549afbf0596d6c).(const MCInstcxx . . $ MCInst# *inst) |
| 75 | { |
| 76 | return inst->OpcodePubcxx . . $ MCInst#OpcodePub.; |
| 77 | } |
| 78 | |
| 79 | MCOperandcxx . . $ MCOperand# *MCInst_getOperandcxx . . $ MCInst_getOperand(4687f87e75986a5b).(MCInstcxx . . $ MCInst# *inst, unsigned i) |
| 80 | { |
| 81 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(i < MAX_MC_OPScxx . . $ `MCInst.h:96:9`!); |
| 82 | return &inst->Operandscxx . . $ MCInst#Operands.[i]; |
| 83 | } |
| 84 | |
| 85 | unsigned MCInst_getNumOperandscxx . . $ MCInst_getNumOperands(66549afbf0596d6c).(const MCInstcxx . . $ MCInst# *inst) |
| 86 | { |
| 87 | return inst->sizecxx . . $ MCInst#size.; |
| 88 | } |
| 89 | |
| 90 | // This addOperand2 function doesnt free Op |
| 91 | void MCInst_addOperand2cxx . . $ MCInst_addOperand2(c51f4ddcb6809868).(MCInstcxx . . $ MCInst# *inst, MCOperandcxx . . $ MCOperand# *Op) |
| 92 | { |
| 93 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(inst->size < MAX_MC_OPScxx . . $ `MCInst.h:96:9`!); |
| 94 | inst->Operandscxx . . $ MCInst#Operands.[inst->sizecxx . . $ MCInst#size.] = *Op; |
| 95 | |
| 96 | inst->sizecxx . . $ MCInst#size.++; |
| 97 | } |
| 98 | |
| 99 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! MCOperand_isValidcxx . . $ MCOperand_isValid(8be14e8515538be0).(const MCOperandcxx . . $ MCOperand# *op) |
| 100 | { |
| 101 | return op->Kindcxx . . $ MCOperand#Kind. != kInvalidcxx . . $ MCOperand#kInvalid.; |
| 102 | } |
| 103 | |
| 104 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! MCOperand_isRegcxx . . $ MCOperand_isReg(8be14e8515538be0).(const MCOperandcxx . . $ MCOperand# *op) |
| 105 | { |
| 106 | return op->Kindcxx . . $ MCOperand#Kind. == kRegistercxx . . $ MCOperand#kRegister.; |
| 107 | } |
| 108 | |
| 109 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! MCOperand_isImmcxx . . $ MCOperand_isImm(8be14e8515538be0).(const MCOperandcxx . . $ MCOperand# *op) |
| 110 | { |
| 111 | return op->Kindcxx . . $ MCOperand#Kind. == kImmediatecxx . . $ MCOperand#kImmediate.; |
| 112 | } |
| 113 | |
| 114 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! MCOperand_isFPImmcxx . . $ MCOperand_isFPImm(8be14e8515538be0).(const MCOperandcxx . . $ MCOperand# *op) |
| 115 | { |
| 116 | return op->Kindcxx . . $ MCOperand#Kind. == kFPImmediatecxx . . $ MCOperand#kFPImmediate.; |
| 117 | } |
| 118 | |
| 119 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! MCOperand_isDFPImmcxx . . $ MCOperand_isDFPImm(8be14e8515538be0).(const MCOperandcxx . . $ MCOperand# *op) |
| 120 | { |
| 121 | return op->Kindcxx . . $ MCOperand#Kind. == kDFPImmediatecxx . . $ MCOperand#kDFPImmediate.; |
| 122 | } |
| 123 | |
| 124 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! MCOperand_isExprcxx . . $ MCOperand_isExpr(8be14e8515538be0).(const MCOperandcxx . . $ MCOperand# *op) |
| 125 | { |
| 126 | return op->Kindcxx . . $ MCOperand#Kind. == kExprcxx . . $ MCOperand#kExpr.; |
| 127 | } |
| 128 | |
| 129 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! MCOperand_isInstcxx . . $ MCOperand_isInst(8be14e8515538be0).(const MCOperandcxx . . $ MCOperand# *op) |
| 130 | { |
| 131 | return op->Kindcxx . . $ MCOperand#Kind. == kInstcxx . . $ MCOperand#kInst.; |
| 132 | } |
| 133 | |
| 134 | /// getReg - Returns the register number. |
| 135 | unsigned MCOperand_getRegcxx . . $ MCOperand_getReg(2f0d38ead81b409a).(const MCOperandcxx . . $ MCOperand# *op) |
| 136 | { |
| 137 | return op->RegValcxx . . $ MCOperand#$anonymous_type_1#RegVal.; |
| 138 | } |
| 139 | |
| 140 | /// setReg - Set the register number. |
| 141 | void MCOperand_setRegcxx . . $ MCOperand_setReg(a7de7e2b93b98a5e).(MCOperandcxx . . $ MCOperand# *op, unsigned Reg) |
| 142 | { |
| 143 | op->RegValcxx . . $ MCOperand#$anonymous_type_1#RegVal. = Reg; |
| 144 | } |
| 145 | |
| 146 | int64_tcxx . . $ int64_t# MCOperand_getImmcxx . . $ MCOperand_getImm(e2e9552a97d88948).(MCOperandcxx . . $ MCOperand# *op) |
| 147 | { |
| 148 | return op->ImmValcxx . . $ MCOperand#$anonymous_type_1#ImmVal.; |
| 149 | } |
| 150 | |
| 151 | void MCOperand_setImmcxx . . $ MCOperand_setImm(6062e6a57a403332).(MCOperandcxx . . $ MCOperand# *op, int64_tcxx . . $ int64_t# Val) |
| 152 | { |
| 153 | op->ImmValcxx . . $ MCOperand#$anonymous_type_1#ImmVal. = Val; |
| 154 | } |
| 155 | |
| 156 | double MCOperand_getFPImmcxx . . $ MCOperand_getFPImm(6f1dad62f05c77b7).(const MCOperandcxx . . $ MCOperand# *op) |
| 157 | { |
| 158 | return op->FPImmValcxx . . $ MCOperand#$anonymous_type_1#FPImmVal.; |
| 159 | } |
| 160 | |
| 161 | void MCOperand_setFPImmcxx . . $ MCOperand_setFPImm(ef60f7f840c96669).(MCOperandcxx . . $ MCOperand# *op, double Val) |
| 162 | { |
| 163 | op->FPImmValcxx . . $ MCOperand#$anonymous_type_1#FPImmVal. = Val; |
| 164 | } |
| 165 | |
| 166 | MCOperandcxx . . $ MCOperand# *MCOperand_CreateReg1cxx . . $ MCOperand_CreateReg1(4687f87e75986a5b).(MCInstcxx . . $ MCInst# *mcInst, unsigned Reg) |
| 167 | { |
| 168 | MCOperandcxx . . $ MCOperand# *op = &(mcInst->Operandscxx . . $ MCInst#Operands.[MCINST_CACHEcxx . . $ `MCInst.c:17:9`!]); |
| 169 | |
| 170 | op->MachineOperandTypecxx . . $ MCOperand#MachineOperandType. = kRegistercxx . . $ MCOperand#kRegister.; |
| 171 | op->Kindcxx . . $ MCOperand#Kind. = kRegistercxx . . $ MCOperand#kRegister.; |
| 172 | op->RegValcxx . . $ MCOperand#$anonymous_type_1#RegVal. = Reg; |
| 173 | |
| 174 | return op; |
| 175 | } |
| 176 | |
| 177 | void MCOperand_CreateReg0cxx . . $ MCOperand_CreateReg0(1d3fdd902ae43da2).(MCInstcxx . . $ MCInst# *mcInst, unsigned Reg) |
| 178 | { |
| 179 | MCOperandcxx . . $ MCOperand# *op = &(mcInst->Operandscxx . . $ MCInst#Operands.[mcInst->sizecxx . . $ MCInst#size.]); |
| 180 | mcInst->sizecxx . . $ MCInst#size.++; |
| 181 | |
| 182 | op->MachineOperandTypecxx . . $ MCOperand#MachineOperandType. = kRegistercxx . . $ MCOperand#kRegister.; |
| 183 | op->Kindcxx . . $ MCOperand#Kind. = kRegistercxx . . $ MCOperand#kRegister.; |
| 184 | op->RegValcxx . . $ MCOperand#$anonymous_type_1#RegVal. = Reg; |
| 185 | } |
| 186 | |
| 187 | MCOperandcxx . . $ MCOperand# *MCOperand_CreateImm1cxx . . $ MCOperand_CreateImm1(d9aeb76e8d27515e).(MCInstcxx . . $ MCInst# *mcInst, int64_tcxx . . $ int64_t# Val) |
| 188 | { |
| 189 | MCOperandcxx . . $ MCOperand# *op = &(mcInst->Operandscxx . . $ MCInst#Operands.[MCINST_CACHEcxx . . $ `MCInst.c:17:9`!]); |
| 190 | |
| 191 | op->MachineOperandTypecxx . . $ MCOperand#MachineOperandType. = kImmediatecxx . . $ MCOperand#kImmediate.; |
| 192 | op->Kindcxx . . $ MCOperand#Kind. = kImmediatecxx . . $ MCOperand#kImmediate.; |
| 193 | op->ImmValcxx . . $ MCOperand#$anonymous_type_1#ImmVal. = Val; |
| 194 | |
| 195 | return op; |
| 196 | } |
| 197 | |
| 198 | void MCOperand_CreateImm0cxx . . $ MCOperand_CreateImm0(c25c894236a70359).(MCInstcxx . . $ MCInst# *mcInst, int64_tcxx . . $ int64_t# Val) |
| 199 | { |
| 200 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(mcInst->size < MAX_MC_OPScxx . . $ `MCInst.h:96:9`!); |
| 201 | MCOperandcxx . . $ MCOperand# *op = &(mcInst->Operandscxx . . $ MCInst#Operands.[mcInst->sizecxx . . $ MCInst#size.]); |
| 202 | mcInst->sizecxx . . $ MCInst#size.++; |
| 203 | |
| 204 | op->MachineOperandTypecxx . . $ MCOperand#MachineOperandType. = kImmediatecxx . . $ MCOperand#kImmediate.; |
| 205 | op->Kindcxx . . $ MCOperand#Kind. = kImmediatecxx . . $ MCOperand#kImmediate.; |
| 206 | op->ImmValcxx . . $ MCOperand#$anonymous_type_1#ImmVal. = Val; |
| 207 | } |
| 208 | |
| 209 | /// Check if any operand of the MCInstrDesc is predicable |
| 210 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! MCInst_isPredicablecxx . . $ MCInst_isPredicable(64ccc6f291f11a49).(const MCInstrDesccxx . . $ MCInstrDesc# *MIDesc) |
| 211 | { |
| 212 | const MCOperandInfocxx . . $ MCOperandInfo# *OpInfo = MIDesc->OpInfocxx . . $ MCInstrDesc#OpInfo.; |
| 213 | unsigned NumOps = MIDesc->NumOperandscxx . . $ MCInstrDesc#NumOperands.; |
| 214 | for (unsigned i = 0; i < NumOps; ++i) { |
| 215 | if (MCOperandInfo_isPredicatecxx . . $ MCOperandInfo_isPredicate(fd07c0332416d4df).(&OpInfo[i])) { |
| 216 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 217 | } |
| 218 | } |
| 219 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 220 | } |
| 221 | |
| 222 | /// Checks if tied operands exist in the instruction and sets |
| 223 | /// - The writeback flag in detail |
| 224 | /// - Saves the indices of the tied destination operands. |
| 225 | void MCInst_handleWritebackcxx . . $ MCInst_handleWriteback(bc2af1aac6b3a292).(MCInstcxx . . $ MCInst# *MI, const MCInstrDesccxx . . $ MCInstrDesc# *InstDesc) |
| 226 | { |
| 227 | const MCOperandInfocxx . . $ MCOperandInfo# *OpInfo = InstDesc[MCInst_getOpcodecxx . . $ MCInst_getOpcode(66549afbf0596d6c).(MI)].OpInfocxx . . $ MCInstrDesc#OpInfo.; |
| 228 | unsigned short NumOps = InstDesc[MCInst_getOpcodecxx . . $ MCInst_getOpcode(66549afbf0596d6c).(MI)].NumOperandscxx . . $ MCInstrDesc#NumOperands.; |
| 229 | |
| 230 | unsigned i; |
| 231 | for (i = 0; i < NumOps; ++i) { |
| 232 | if (MCOperandInfo_isTiedToOpcxx . . $ MCOperandInfo_isTiedToOp(fd07c0332416d4df).(&OpInfo[i])) { |
| 233 | int idx = MCOperandInfo_getOperandConstraintcxx . . $ MCOperandInfo_getOperandConstraint(cc1c42ad4c38c5e3).( |
| 234 | &InstDesc[MCInst_getOpcodecxx . . $ MCInst_getOpcode(66549afbf0596d6c).(MI)], i, |
| 235 | MCOI_TIED_TOcxx . . $ MCOI_TIED_TO.); |
| 236 | |
| 237 | if (idx == -1) |
| 238 | continue; |
| 239 | |
| 240 | if (i >= MAX_MC_OPScxx . . $ `MCInst.h:96:9`!) { |
| 241 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(0 && |
| 242 | "Maximum number of MC operands reached."); |
| 243 | } |
| 244 | MI->tied_op_idxcxx . . $ MCInst#tied_op_idx.[i] = idx; |
| 245 | |
| 246 | if (MI->flat_insncxx . . $ MCInst#flat_insn.->detailcxx . . $ cs_insn#detail.) |
| 247 | MI->flat_insncxx . . $ MCInst#flat_insn.->detailcxx . . $ cs_insn#detail.->writebackcxx . . $ cs_detail#writeback. = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /// Check if operand with OpNum is tied by another operand |
| 253 | /// (operand is tying destination). |
| 254 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! MCInst_opIsTiedcxx . . $ MCInst_opIsTied(3f89b876b015f5e2).(const MCInstcxx . . $ MCInst# *MI, unsigned OpNum) |
| 255 | { |
| 256 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(OpNum < MAX_MC_OPScxx . . $ `MCInst.h:96:9`! && "Maximum number of MC operands exceeded."); |
| 257 | for (int i = 0; i < MAX_MC_OPScxx . . $ `MCInst.h:96:9`!; ++i) { |
| 258 | if (MI->tied_op_idxcxx . . $ MCInst#tied_op_idx.[i] == OpNum) |
| 259 | return truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 260 | } |
| 261 | return falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 262 | } |
| 263 | |
| 264 | /// Check if operand with OpNum is tying another operand |
| 265 | /// (operand is tying src). |
| 266 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! MCInst_opIsTyingcxx . . $ MCInst_opIsTying(3f89b876b015f5e2).(const MCInstcxx . . $ MCInst# *MI, unsigned OpNum) |
| 267 | { |
| 268 | assertcxx . . $ `<external>/4398592474888995393/assert.h:169:12`!(OpNum < MAX_MC_OPScxx . . $ `MCInst.h:96:9`! && "Maximum number of MC operands exceeded."); |
| 269 | return MI->tied_op_idxcxx . . $ MCInst#tied_op_idx.[OpNum] != -1; |
| 270 | } |