Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 1 | //===-- llvm/MC/MCInst.h - MCInst class -------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the declaration of the MCInst and MCOperand classes, which |
| 11 | // is the basic representation used to represent low-level machine code |
| 12 | // instructions. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | /* Capstone Disassembler Engine */ |
| 17 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013> */ |
| 18 | |
Nguyen Anh Quynh | ae3649f | 2014-01-02 13:15:07 +0800 | [diff] [blame] | 19 | #ifndef CS_MCINST_H |
| 20 | #define CS_MCINST_H |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 21 | |
| 22 | #include <stdint.h> |
| 23 | #include <stdbool.h> |
| 24 | |
| 25 | #include "include/capstone.h" |
| 26 | |
| 27 | typedef struct MCInst MCInst; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 28 | typedef struct cs_struct cs_struct; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 29 | typedef struct MCOperand MCOperand; |
| 30 | |
| 31 | /// MCOperand - Instances of this class represent operands of the MCInst class. |
| 32 | /// This is a simple discriminated union. |
| 33 | struct MCOperand { |
| 34 | enum { |
| 35 | kInvalid = 0, ///< Uninitialized. |
| 36 | kRegister, ///< Register operand. |
| 37 | kImmediate, ///< Immediate operand. |
| 38 | kFPImmediate, ///< Floating-point immediate operand. |
| 39 | } MachineOperandType; |
| 40 | unsigned char Kind; |
| 41 | |
| 42 | union { |
| 43 | unsigned RegVal; |
| 44 | int64_t ImmVal; |
| 45 | double FPImmVal; |
| 46 | }; |
| 47 | }; |
| 48 | |
| 49 | bool MCOperand_isValid(const MCOperand *op); |
| 50 | |
| 51 | bool MCOperand_isReg(const MCOperand *op); |
| 52 | |
| 53 | bool MCOperand_isImm(const MCOperand *op); |
| 54 | |
| 55 | bool MCOperand_isFPImm(const MCOperand *op); |
| 56 | |
| 57 | bool MCOperand_isInst(const MCOperand *op); |
| 58 | |
| 59 | void MCInst_clear(MCInst *m); |
| 60 | |
| 61 | /// getReg - Returns the register number. |
| 62 | unsigned MCOperand_getReg(const MCOperand *op); |
| 63 | |
| 64 | /// setReg - Set the register number. |
| 65 | void MCOperand_setReg(MCOperand *op, unsigned Reg); |
| 66 | |
| 67 | int64_t MCOperand_getImm(MCOperand *op); |
| 68 | |
| 69 | void MCOperand_setImm(MCOperand *op, int64_t Val); |
| 70 | |
| 71 | double MCOperand_getFPImm(const MCOperand *op); |
| 72 | |
| 73 | void MCOperand_setFPImm(MCOperand *op, double Val); |
| 74 | |
| 75 | const MCInst *MCOperand_getInst(const MCOperand *op); |
| 76 | |
| 77 | void MCOperand_setInst(MCOperand *op, const MCInst *Val); |
| 78 | |
| 79 | MCOperand *MCOperand_CreateReg(unsigned Reg); |
| 80 | |
| 81 | MCOperand *MCOperand_CreateImm(int64_t Val); |
| 82 | |
| 83 | MCOperand *MCOperand_CreateFPImm(double Val); |
| 84 | |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 85 | // NOTE: this structure is a flatten version of cs_insn struct |
| 86 | // Detail information of disassembled instruction |
| 87 | typedef struct cs_insn_flat { |
| 88 | // Instruction ID |
| 89 | // Find the instruction id from header file of corresponding architecture, |
| 90 | // such as arm.h for ARM, x86.h for X86, etc... |
| 91 | // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF |
| 92 | unsigned int id; |
| 93 | |
| 94 | // Address (EIP) of this instruction |
| 95 | // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF |
| 96 | uint64_t address; |
| 97 | |
| 98 | // Size of this instruction |
| 99 | // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF |
| 100 | uint16_t size; |
| 101 | // Machine bytes of this instruction, with number of bytes indicated by @size above |
| 102 | // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF |
| 103 | uint8_t bytes[16]; |
| 104 | |
| 105 | // Ascii text of instruction mnemonic |
| 106 | // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF |
| 107 | char mnemonic[32]; |
| 108 | |
| 109 | // Ascii text of instruction operands |
| 110 | // This information is available even when CS_OPT_DETAIL = CS_OPT_OFF |
Nguyen Anh Quynh | 0636f68 | 2014-01-15 17:51:08 +0800 | [diff] [blame] | 111 | char op_str[160]; |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 112 | |
| 113 | // NOTE: All information below is not available when CS_OPT_DETAIL = CS_OPT_OFF |
| 114 | |
| 115 | uint8_t regs_read[12]; // list of implicit registers read by this insn |
| 116 | uint8_t regs_read_count; // number of implicit registers read by this insn |
| 117 | |
| 118 | uint8_t regs_write[20]; // list of implicit registers modified by this insn |
| 119 | uint8_t regs_write_count; // number of implicit registers modified by this insn |
| 120 | |
| 121 | uint8_t groups[8]; // list of group this instruction belong to |
| 122 | uint8_t groups_count; // number of groups this insn belongs to |
| 123 | |
| 124 | // Architecture-specific instruction info |
| 125 | union { |
| 126 | cs_x86 x86; // X86 architecture, including 16-bit, 32-bit & 64-bit mode |
| 127 | cs_arm64 arm64; // ARM64 architecture (aka AArch64) |
| 128 | cs_arm arm; // ARM architecture (including Thumb/Thumb2) |
| 129 | cs_mips mips; // MIPS architecture |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 130 | cs_ppc ppc; // PowerPC architecture |
Nguyen Anh Quynh | 48a14ca | 2014-03-23 08:35:45 +0800 | [diff] [blame] | 131 | cs_sparc sparc; // Sparc architecture |
| 132 | cs_sysz sysz; // SystemZ architecture |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 133 | }; |
| 134 | } cs_insn_flat; |
| 135 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 136 | /// MCInst - Instances of this class represent a single low-level machine |
| 137 | /// instruction. |
| 138 | struct MCInst { |
| 139 | unsigned Opcode; |
| 140 | MCOperand Operands[32]; |
| 141 | unsigned size; // number of operands |
Nguyen Anh Quynh | 4fe224b | 2013-12-24 16:49:36 +0800 | [diff] [blame] | 142 | cs_insn_flat flat_insn; // insn to be exposed to public |
Nguyen Anh Quynh | 6b7abe3 | 2013-11-30 00:54:24 +0800 | [diff] [blame] | 143 | unsigned OpcodePub; |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 144 | int insn_size; // instruction size |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 145 | uint64_t address; // address of this insn |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 146 | cs_struct *csh; // save the main csh |
Nguyen Anh Quynh | e51cf36 | 2014-03-27 12:36:46 +0800 | [diff] [blame] | 147 | uint8_t x86_imm_size; // save immediate size to print immediate properly |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | void MCInst_Init(MCInst *inst); |
| 151 | |
| 152 | void MCInst_clear(MCInst *inst); |
| 153 | |
| 154 | void MCInst_insert(MCInst *inst, int index, MCOperand *Op); |
| 155 | |
| 156 | void MCInst_setOpcode(MCInst *inst, unsigned Op); |
| 157 | |
| 158 | unsigned MCInst_getOpcode(const MCInst*); |
| 159 | |
Nguyen Anh Quynh | 6b7abe3 | 2013-11-30 00:54:24 +0800 | [diff] [blame] | 160 | void MCInst_setOpcodePub(MCInst *inst, unsigned Op); |
| 161 | |
| 162 | unsigned MCInst_getOpcodePub(const MCInst*); |
| 163 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 164 | MCOperand *MCInst_getOperand(MCInst *inst, unsigned i); |
| 165 | |
| 166 | unsigned MCInst_getNumOperands(const MCInst *inst); |
| 167 | |
| 168 | int MCInst_addOperand(MCInst *inst, MCOperand *Op); |
| 169 | |
| 170 | // This addOperand2 function doesnt free Op |
| 171 | int MCInst_addOperand2(MCInst *inst, MCOperand *Op); |
| 172 | |
| 173 | #endif |