blob: 88a48bcd4c7f72cd6cff22de12134faf6379dafb [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 Quynh4fe224b2013-12-24 16:49:36 +0800131 };
132} cs_insn_flat;
133
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800134/// MCInst - Instances of this class represent a single low-level machine
135/// instruction.
136struct MCInst {
137 unsigned Opcode;
138 MCOperand Operands[32];
139 unsigned size; // number of operands
Nguyen Anh Quynh4fe224b2013-12-24 16:49:36 +0800140 cs_insn_flat flat_insn; // insn to be exposed to public
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +0800141 unsigned OpcodePub;
Nguyen Anh Quynha209e672013-12-14 00:23:41 +0800142 int insn_size; // instruction size
143 int x86_segment; // remove when segment mem ref hack is redundant.
144 uint64_t address; // address of this insn
Nguyen Anh Quynh42c6b1a2013-12-30 00:15:25 +0800145 cs_struct *csh; // save the main csh
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800146};
147
148void MCInst_Init(MCInst *inst);
149
150void MCInst_clear(MCInst *inst);
151
152void MCInst_insert(MCInst *inst, int index, MCOperand *Op);
153
154void MCInst_setOpcode(MCInst *inst, unsigned Op);
155
156unsigned MCInst_getOpcode(const MCInst*);
157
Nguyen Anh Quynh6b7abe32013-11-30 00:54:24 +0800158void MCInst_setOpcodePub(MCInst *inst, unsigned Op);
159
160unsigned MCInst_getOpcodePub(const MCInst*);
161
Nguyen Anh Quynh26ee41a2013-11-27 12:11:31 +0800162MCOperand *MCInst_getOperand(MCInst *inst, unsigned i);
163
164unsigned MCInst_getNumOperands(const MCInst *inst);
165
166int MCInst_addOperand(MCInst *inst, MCOperand *Op);
167
168// This addOperand2 function doesnt free Op
169int MCInst_addOperand2(MCInst *inst, MCOperand *Op);
170
171#endif