blob: 1144c6cc6983cb8bf45a9a87ff9dca60ebdb6707 [file] [log] [blame]
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +08001//===-- 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 Quynhae3649f2014-01-02 13:15:07 +080019#ifndef CS_MCINST_H
20#define CS_MCINST_H
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080021
22#include <stdint.h>
23#include <stdbool.h>
24
25#include "include/capstone.h"
26
27typedef struct MCInst MCInst;
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +080028typedef struct cs_struct cs_struct;
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +080029typedef struct MCOperand MCOperand;
30
31/// MCOperand - Instances of this class represent operands of the MCInst class.
32/// This is a simple discriminated union.
33struct 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
49bool MCOperand_isValid(const MCOperand *op);
50
51bool MCOperand_isReg(const MCOperand *op);
52
53bool MCOperand_isImm(const MCOperand *op);
54
55bool MCOperand_isFPImm(const MCOperand *op);
56
57bool MCOperand_isInst(const MCOperand *op);
58
59void MCInst_clear(MCInst *m);
60
61/// getReg - Returns the register number.
62unsigned MCOperand_getReg(const MCOperand *op);
63
64/// setReg - Set the register number.
65void MCOperand_setReg(MCOperand *op, unsigned Reg);
66
67int64_t MCOperand_getImm(MCOperand *op);
68
69void MCOperand_setImm(MCOperand *op, int64_t Val);
70
71double MCOperand_getFPImm(const MCOperand *op);
72
73void MCOperand_setFPImm(MCOperand *op, double Val);
74
75const MCInst *MCOperand_getInst(const MCOperand *op);
76
77void MCOperand_setInst(MCOperand *op, const MCInst *Val);
78
79MCOperand *MCOperand_CreateReg(unsigned Reg);
80
81MCOperand *MCOperand_CreateImm(int64_t Val);
82
83MCOperand *MCOperand_CreateFPImm(double Val);
84
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +080085// NOTE: this structure is a flatten version of cs_insn struct
86// Detail information of disassembled instruction
87typedef 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 Quynh0636f682014-01-15 17:51:08 +0800111 char op_str[160];
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800112
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 Quynh42c6b1a2013-12-30 00:15:25 +0800130 cs_ppc ppc; // PowerPC architecture
Nguyen Anh Quynh48a14ca2014-03-23 08:35:45 +0800131 cs_sparc sparc; // Sparc architecture
132 cs_sysz sysz; // SystemZ architecture
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800133 };
134} cs_insn_flat;
135
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800136/// MCInst - Instances of this class represent a single low-level machine
137/// instruction.
138struct MCInst {
139 unsigned Opcode;
140 MCOperand Operands[32];
141 unsigned size; // number of operands
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800142 cs_insn_flat flat_insn; // insn to be exposed to public
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +0800143 unsigned OpcodePub;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800144 int insn_size; // instruction size
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800145 uint64_t address; // address of this insn
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800146 cs_struct *csh; // save the main csh
Nguyen Anh Quynhe51cf362014-03-27 12:36:46 +0800147 uint8_t x86_imm_size; // save immediate size to print immediate properly
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800148};
149
150void MCInst_Init(MCInst *inst);
151
152void MCInst_clear(MCInst *inst);
153
154void MCInst_insert(MCInst *inst, int index, MCOperand *Op);
155
156void MCInst_setOpcode(MCInst *inst, unsigned Op);
157
158unsigned MCInst_getOpcode(const MCInst*);
159
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +0800160void MCInst_setOpcodePub(MCInst *inst, unsigned Op);
161
162unsigned MCInst_getOpcodePub(const MCInst*);
163
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800164MCOperand *MCInst_getOperand(MCInst *inst, unsigned i);
165
166unsigned MCInst_getNumOperands(const MCInst *inst);
167
168int MCInst_addOperand(MCInst *inst, MCOperand *Op);
169
170// This addOperand2 function doesnt free Op
171int MCInst_addOperand2(MCInst *inst, MCOperand *Op);
172
173#endif