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 | |
Nguyen Anh Quynh | 6023ef7 | 2014-04-29 11:21:04 +0800 | [diff] [blame] | 16 | /* Capstone Disassembly Engine */ |
Nguyen Anh Quynh | bfcaba5 | 2015-03-04 17:45:23 +0800 | [diff] [blame] | 17 | /* By Nguyen Anh Quynh <aquynh@gmail.com>, 2013-2015 */ |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 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 | |
pancake | 9c10ace | 2015-02-24 04:55:55 +0100 | [diff] [blame] | 22 | #include "include/capstone/capstone.h" |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 23 | |
| 24 | typedef struct MCInst MCInst; |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 25 | typedef struct cs_struct cs_struct; |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 26 | typedef struct MCOperand MCOperand; |
| 27 | |
| 28 | /// MCOperand - Instances of this class represent operands of the MCInst class. |
| 29 | /// This is a simple discriminated union. |
| 30 | struct MCOperand { |
| 31 | enum { |
| 32 | kInvalid = 0, ///< Uninitialized. |
| 33 | kRegister, ///< Register operand. |
| 34 | kImmediate, ///< Immediate operand. |
| 35 | kFPImmediate, ///< Floating-point immediate operand. |
| 36 | } MachineOperandType; |
| 37 | unsigned char Kind; |
| 38 | |
| 39 | union { |
| 40 | unsigned RegVal; |
| 41 | int64_t ImmVal; |
| 42 | double FPImmVal; |
| 43 | }; |
| 44 | }; |
| 45 | |
| 46 | bool MCOperand_isValid(const MCOperand *op); |
| 47 | |
| 48 | bool MCOperand_isReg(const MCOperand *op); |
| 49 | |
| 50 | bool MCOperand_isImm(const MCOperand *op); |
| 51 | |
| 52 | bool MCOperand_isFPImm(const MCOperand *op); |
| 53 | |
| 54 | bool MCOperand_isInst(const MCOperand *op); |
| 55 | |
| 56 | void MCInst_clear(MCInst *m); |
| 57 | |
| 58 | /// getReg - Returns the register number. |
| 59 | unsigned MCOperand_getReg(const MCOperand *op); |
| 60 | |
| 61 | /// setReg - Set the register number. |
| 62 | void MCOperand_setReg(MCOperand *op, unsigned Reg); |
| 63 | |
| 64 | int64_t MCOperand_getImm(MCOperand *op); |
| 65 | |
| 66 | void MCOperand_setImm(MCOperand *op, int64_t Val); |
| 67 | |
| 68 | double MCOperand_getFPImm(const MCOperand *op); |
| 69 | |
| 70 | void MCOperand_setFPImm(MCOperand *op, double Val); |
| 71 | |
| 72 | const MCInst *MCOperand_getInst(const MCOperand *op); |
| 73 | |
| 74 | void MCOperand_setInst(MCOperand *op, const MCInst *Val); |
| 75 | |
Nguyen Anh Quynh | 0f648ea | 2014-06-10 01:01:23 +0700 | [diff] [blame] | 76 | // create Reg operand in the next slot |
Nguyen Anh Quynh | cf08138 | 2014-06-06 00:56:46 +0800 | [diff] [blame] | 77 | void MCOperand_CreateReg0(MCInst *inst, unsigned Reg); |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame] | 78 | |
Nguyen Anh Quynh | 0f648ea | 2014-06-10 01:01:23 +0700 | [diff] [blame] | 79 | // create Reg operand use the last-unused slot |
| 80 | MCOperand *MCOperand_CreateReg1(MCInst *inst, unsigned Reg); |
| 81 | |
| 82 | // create Imm operand in the next slot |
Nguyen Anh Quynh | cf08138 | 2014-06-06 00:56:46 +0800 | [diff] [blame] | 83 | void MCOperand_CreateImm0(MCInst *inst, int64_t Val); |
Nguyen Anh Quynh | 937e483 | 2014-06-04 22:51:51 +0700 | [diff] [blame] | 84 | |
Nguyen Anh Quynh | 0f648ea | 2014-06-10 01:01:23 +0700 | [diff] [blame] | 85 | // create Imm operand in the last-unused slot |
| 86 | MCOperand *MCOperand_CreateImm1(MCInst *inst, int64_t Val); |
| 87 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 88 | /// MCInst - Instances of this class represent a single low-level machine |
| 89 | /// instruction. |
| 90 | struct MCInst { |
Nguyen Anh Quynh | 495295e | 2014-06-16 15:54:32 +0800 | [diff] [blame] | 91 | unsigned OpcodePub; |
Nguyen Anh Quynh | ff7bba3 | 2014-11-03 16:32:06 +0800 | [diff] [blame] | 92 | uint8_t size; // number of operands |
| 93 | bool has_imm; // indicate this instruction has an X86_OP_IMM operand - used for ATT syntax |
| 94 | uint8_t op1_size; // size of 1st operand - for X86 Intel syntax |
Nguyen Anh Quynh | f1ec526 | 2014-06-25 22:03:18 +0800 | [diff] [blame] | 95 | unsigned Opcode; |
| 96 | MCOperand Operands[48]; |
Nguyen Anh Quynh | 5329a6f | 2014-06-08 23:35:52 +0700 | [diff] [blame] | 97 | cs_insn *flat_insn; // insn to be exposed to public |
Nguyen Anh Quynh | a209e67 | 2013-12-14 00:23:41 +0800 | [diff] [blame] | 98 | uint64_t address; // address of this insn |
Nguyen Anh Quynh | 42c6b1a | 2013-12-30 00:15:25 +0800 | [diff] [blame] | 99 | cs_struct *csh; // save the main csh |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 100 | uint8_t x86opsize; // opsize for [mem] operand |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 101 | |
Nguyen Anh Quynh | 1085073 | 2014-06-18 12:16:24 +0800 | [diff] [blame] | 102 | // (Optional) instruction prefix, which can be up to 4 bytes. |
Nguyen Anh Quynh | a5ffdc3 | 2014-05-07 08:25:24 +0800 | [diff] [blame] | 103 | // A prefix byte gets value 0 when irrelevant. |
| 104 | // This is copied from cs_x86 struct |
Nguyen Anh Quynh | 46b6693 | 2014-06-04 19:04:46 +0800 | [diff] [blame] | 105 | uint8_t x86_prefix[4]; |
Nguyen Anh Quynh | f1ec526 | 2014-06-25 22:03:18 +0800 | [diff] [blame] | 106 | uint8_t imm_size; // immediate size for X86_OP_IMM operand |
Nguyen Anh Quynh | e19490e | 2015-01-21 12:15:14 +0800 | [diff] [blame] | 107 | bool writeback; // writeback for ARM |
Nguyen Anh Quynh | 29f777b | 2015-04-07 11:59:26 +0800 | [diff] [blame] | 108 | // operand access index for list of registers sharing the same access right (for ARM) |
| 109 | uint8_t ac_idx; |
Nguyen Anh Quynh | dabc9f2 | 2016-07-15 20:37:19 +0800 | [diff] [blame] | 110 | uint8_t popcode_adjust; // Pseudo X86 instruction adjust |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 111 | }; |
| 112 | |
Nguyen Anh Quynh | 495295e | 2014-06-16 15:54:32 +0800 | [diff] [blame] | 113 | void MCInst_Init(MCInst *inst); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 114 | |
| 115 | void MCInst_clear(MCInst *inst); |
| 116 | |
Nguyen Anh Quynh | 0f648ea | 2014-06-10 01:01:23 +0700 | [diff] [blame] | 117 | // do not free operand after inserting |
| 118 | void MCInst_insert0(MCInst *inst, int index, MCOperand *Op); |
| 119 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 120 | void MCInst_setOpcode(MCInst *inst, unsigned Op); |
| 121 | |
| 122 | unsigned MCInst_getOpcode(const MCInst*); |
| 123 | |
Nguyen Anh Quynh | 6b7abe3 | 2013-11-30 00:54:24 +0800 | [diff] [blame] | 124 | void MCInst_setOpcodePub(MCInst *inst, unsigned Op); |
| 125 | |
| 126 | unsigned MCInst_getOpcodePub(const MCInst*); |
| 127 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 128 | MCOperand *MCInst_getOperand(MCInst *inst, unsigned i); |
| 129 | |
| 130 | unsigned MCInst_getNumOperands(const MCInst *inst); |
| 131 | |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 132 | // This addOperand2 function doesnt free Op |
Nguyen Anh Quynh | 264ca37 | 2014-06-16 14:52:09 +0800 | [diff] [blame] | 133 | void MCInst_addOperand2(MCInst *inst, MCOperand *Op); |
Nguyen Anh Quynh | 26ee41a | 2013-11-27 12:11:31 +0800 | [diff] [blame] | 134 | |
| 135 | #endif |