LLVM 24.0.0git
MCInst.h
Go to the documentation of this file.
1//===- llvm/MC/MCInst.h - MCInst class --------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the declaration of the MCInst and MCOperand classes, which
10// is the basic representation used to represent low-level machine code
11// instructions.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_MC_MCINST_H
16#define LLVM_MC_MCINST_H
17
18#include "llvm/ADT/ArrayRef.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/ADT/bit.h"
22#include "llvm/MC/MCRegister.h"
24#include "llvm/Support/SMLoc.h"
25#include <cassert>
26#include <cstddef>
27#include <cstdint>
28
29namespace llvm {
30
31class MCContext;
32class MCExpr;
33class MCInst;
34class MCInstPrinter;
35class raw_ostream;
36
37/// Instances of this class represent operands of the MCInst class.
38/// This is a simple discriminated union.
39class MCOperand {
40 enum MachineOperandType : unsigned char {
41 kInvalid, ///< Uninitialized.
42 kRegister, ///< Register operand.
43 kImmediate, ///< Immediate operand.
44 kSFPImmediate, ///< Single-floating-point immediate operand.
45 kDFPImmediate, ///< Double-Floating-point immediate operand.
46 kExpr, ///< Relocatable immediate operand.
47 kInst ///< Sub-instruction operand.
48 };
49 MachineOperandType Kind = kInvalid;
50
51 union {
52 unsigned RegVal;
53 int64_t ImmVal;
58 };
59
60public:
62
63 bool isValid() const { return Kind != kInvalid; }
64 bool isReg() const { return Kind == kRegister; }
65 bool isImm() const { return Kind == kImmediate; }
66 bool isSFPImm() const { return Kind == kSFPImmediate; }
67 bool isDFPImm() const { return Kind == kDFPImmediate; }
68 bool isExpr() const { return Kind == kExpr; }
69 bool isInst() const { return Kind == kInst; }
70
71 /// Returns the register number.
73 assert(isReg() && "This is not a register operand!");
74 return RegVal;
75 }
76
77 /// Set the register number.
79 assert(isReg() && "This is not a register operand!");
80 RegVal = Reg.id();
81 }
82
83 int64_t getImm() const {
84 assert(isImm() && "This is not an immediate");
85 return ImmVal;
86 }
87
88 void setImm(int64_t Val) {
89 assert(isImm() && "This is not an immediate");
90 ImmVal = Val;
91 }
92
94 assert(isSFPImm() && "This is not an SFP immediate");
95 return SFPImmVal;
96 }
97
98 void setSFPImm(uint32_t Val) {
99 assert(isSFPImm() && "This is not an SFP immediate");
100 SFPImmVal = Val;
101 }
102
104 assert(isDFPImm() && "This is not an FP immediate");
105 return FPImmVal;
106 }
107
108 void setDFPImm(uint64_t Val) {
109 assert(isDFPImm() && "This is not an FP immediate");
110 FPImmVal = Val;
111 }
112 void setFPImm(double Val) {
113 assert(isDFPImm() && "This is not an FP immediate");
115 }
116
117 const MCExpr *getExpr() const {
118 assert(isExpr() && "This is not an expression");
119 return ExprVal;
120 }
121
122 void setExpr(const MCExpr *Val) {
123 assert(isExpr() && "This is not an expression");
124 ExprVal = Val;
125 }
126
127 const MCInst *getInst() const {
128 assert(isInst() && "This is not a sub-instruction");
129 return InstVal;
130 }
131
132 void setInst(const MCInst *Val) {
133 assert(isInst() && "This is not a sub-instruction");
134 InstVal = Val;
135 }
136
139 Op.Kind = kRegister;
140 Op.RegVal = Reg.id();
141 return Op;
142 }
143
144 static MCOperand createImm(int64_t Val) {
146 Op.Kind = kImmediate;
147 Op.ImmVal = Val;
148 return Op;
149 }
150
153 Op.Kind = kSFPImmediate;
154 Op.SFPImmVal = Val;
155 return Op;
156 }
157
160 Op.Kind = kDFPImmediate;
161 Op.FPImmVal = Val;
162 return Op;
163 }
164
165 static MCOperand createExpr(const MCExpr *Val) {
167 Op.Kind = kExpr;
168 Op.ExprVal = Val;
169 return Op;
170 }
171
172 static MCOperand createInst(const MCInst *Val) {
174 Op.Kind = kInst;
175 Op.InstVal = Val;
176 return Op;
177 }
178
179 LLVM_ABI void print(raw_ostream &OS, const MCContext *Ctx = nullptr) const;
180 LLVM_ABI void dump() const;
181 LLVM_ABI bool isBareSymbolRef() const;
182 LLVM_ABI bool evaluateAsConstantImm(int64_t &Imm) const;
183};
184
185/// Instances of this class represent a single low-level machine
186/// instruction.
187class MCInst {
188 unsigned Opcode = 0;
189 // These flags could be used to pass some info from one target subcomponent
190 // to another, for example, from disassembler to asm printer. The values of
191 // the flags have any sense on target level only (e.g. prefixes on x86).
192 unsigned Flags = 0;
193
194 SMLoc Loc;
196
197public:
198 MCInst() = default;
199
200 void setOpcode(unsigned Op) { Opcode = Op; }
201 unsigned getOpcode() const { return Opcode; }
202
203 void setFlags(unsigned F) { Flags = F; }
204 unsigned getFlags() const { return Flags; }
205
206 void setLoc(SMLoc loc) { Loc = loc; }
207 SMLoc getLoc() const { return Loc; }
208
209 const MCOperand &getOperand(unsigned i) const { return Operands[i]; }
210 MCOperand &getOperand(unsigned i) { return Operands[i]; }
211 unsigned getNumOperands() const { return Operands.size(); }
212
213 ArrayRef<MCOperand> getOperands() const { return Operands; }
214 void addOperand(const MCOperand Op) { Operands.push_back(Op); }
216 Operands.assign(Ops.begin(), Ops.end());
217 }
218
221
222 void clear() { Operands.clear(); }
223 void erase(iterator I) { Operands.erase(I); }
224 void erase(iterator First, iterator Last) { Operands.erase(First, Last); }
225 size_t size() const { return Operands.size(); }
226 iterator begin() { return Operands.begin(); }
227 const_iterator begin() const { return Operands.begin(); }
228 iterator end() { return Operands.end(); }
229 const_iterator end() const { return Operands.end(); }
230
232 return Operands.insert(I, Op);
233 }
234
235 LLVM_ABI void print(raw_ostream &OS, const MCContext *Ctx = nullptr) const;
236 LLVM_ABI void dump() const;
237
238 /// Dump the MCInst as prettily as possible using the additional MC
239 /// structures, if given. Operators are separated by the \p Separator
240 /// string.
242 const MCInstPrinter *Printer = nullptr,
243 StringRef Separator = " ",
244 const MCContext *Ctx = nullptr) const;
246 StringRef Separator = " ",
247 const MCContext *Ctx = nullptr) const;
248};
249
251 MO.print(OS);
252 return OS;
253}
254
256 MI.print(OS);
257 return OS;
258}
259
260} // end namespace llvm
261
262#endif // LLVM_MC_MCINST_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
amdgpu next use AMDGPU Next Use Analysis Printer
#define LLVM_ABI
Definition Compiler.h:211
IRTranslator LLVM IR MI
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
This file defines the SmallVector class.
This file implements the C++20 <bit> header.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Context object for machine code objects.
Definition MCContext.h:84
Base class for the full range of assembler expressions which are needed for parsing.
Definition MCExpr.h:31
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Instances of this class represent a single low-level machine instruction.
Definition MCInst.h:187
LLVM_ABI void print(raw_ostream &OS, const MCContext *Ctx=nullptr) const
Definition MCInst.cpp:81
const_iterator end() const
Definition MCInst.h:229
void erase(iterator I)
Definition MCInst.h:223
MCOperand & getOperand(unsigned i)
Definition MCInst.h:210
unsigned getNumOperands() const
Definition MCInst.h:211
SMLoc getLoc() const
Definition MCInst.h:207
SmallVectorImpl< MCOperand >::iterator iterator
Definition MCInst.h:219
unsigned getFlags() const
Definition MCInst.h:204
void setLoc(SMLoc loc)
Definition MCInst.h:206
unsigned getOpcode() const
Definition MCInst.h:201
LLVM_ABI void dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer=nullptr, StringRef Separator=" ", const MCContext *Ctx=nullptr) const
Dump the MCInst as prettily as possible using the additional MC structures, if given.
Definition MCInst.cpp:90
void erase(iterator First, iterator Last)
Definition MCInst.h:224
MCInst()=default
ArrayRef< MCOperand > getOperands() const
Definition MCInst.h:213
const_iterator begin() const
Definition MCInst.h:227
iterator insert(iterator I, const MCOperand &Op)
Definition MCInst.h:231
void setFlags(unsigned F)
Definition MCInst.h:203
void setOperands(ArrayRef< MCOperand > Ops)
Definition MCInst.h:215
LLVM_ABI void dump() const
Definition MCInst.cpp:112
void addOperand(const MCOperand Op)
Definition MCInst.h:214
iterator begin()
Definition MCInst.h:226
iterator end()
Definition MCInst.h:228
SmallVectorImpl< MCOperand >::const_iterator const_iterator
Definition MCInst.h:220
size_t size() const
Definition MCInst.h:225
void setOpcode(unsigned Op)
Definition MCInst.h:200
void clear()
Definition MCInst.h:222
const MCOperand & getOperand(unsigned i) const
Definition MCInst.h:209
Instances of this class represent operands of the MCInst class.
Definition MCInst.h:39
bool isSFPImm() const
Definition MCInst.h:66
unsigned RegVal
Definition MCInst.h:52
const MCExpr * ExprVal
Definition MCInst.h:56
void setImm(int64_t Val)
Definition MCInst.h:88
static MCOperand createExpr(const MCExpr *Val)
Definition MCInst.h:165
LLVM_ABI bool isBareSymbolRef() const
Definition MCInst.cpp:65
static MCOperand createSFPImm(uint32_t Val)
Definition MCInst.h:151
LLVM_ABI void print(raw_ostream &OS, const MCContext *Ctx=nullptr) const
Definition MCInst.cpp:23
void setExpr(const MCExpr *Val)
Definition MCInst.h:122
int64_t getImm() const
Definition MCInst.h:83
static MCOperand createReg(MCRegister Reg)
Definition MCInst.h:137
static MCOperand createImm(int64_t Val)
Definition MCInst.h:144
bool isImm() const
Definition MCInst.h:65
void setReg(MCRegister Reg)
Set the register number.
Definition MCInst.h:78
uint32_t SFPImmVal
Definition MCInst.h:54
int64_t ImmVal
Definition MCInst.h:53
bool isInst() const
Definition MCInst.h:69
bool isReg() const
Definition MCInst.h:64
void setInst(const MCInst *Val)
Definition MCInst.h:132
MCRegister getReg() const
Returns the register number.
Definition MCInst.h:72
void setSFPImm(uint32_t Val)
Definition MCInst.h:98
uint64_t FPImmVal
Definition MCInst.h:55
LLVM_ABI bool evaluateAsConstantImm(int64_t &Imm) const
Definition MCInst.cpp:57
bool isDFPImm() const
Definition MCInst.h:67
LLVM_ABI void dump() const
Definition MCInst.cpp:75
const MCInst * getInst() const
Definition MCInst.h:127
bool isValid() const
Definition MCInst.h:63
void setDFPImm(uint64_t Val)
Definition MCInst.h:108
const MCExpr * getExpr() const
Definition MCInst.h:117
void setFPImm(double Val)
Definition MCInst.h:112
uint32_t getSFPImm() const
Definition MCInst.h:93
static MCOperand createDFPImm(uint64_t Val)
Definition MCInst.h:158
uint64_t getDFPImm() const
Definition MCInst.h:103
bool isExpr() const
Definition MCInst.h:68
static MCOperand createInst(const MCInst *Val)
Definition MCInst.h:172
const MCInst * InstVal
Definition MCInst.h:57
Wrapper class representing physical registers and target specific virtual registers.
Definition MCRegister.h:42
Represents a location in source code.
Definition SMLoc.h:22
typename SuperClass::const_iterator const_iterator
typename SuperClass::iterator iterator
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:74
To bit_cast(const From &from) noexcept
Definition bit.h:90
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)