LLVM 22.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 MCRegisterInfo;
36class raw_ostream;
37
38/// Instances of this class represent operands of the MCInst class.
39/// This is a simple discriminated union.
40class MCOperand {
41 enum MachineOperandType : unsigned char {
42 kInvalid, ///< Uninitialized.
43 kRegister, ///< Register operand.
44 kImmediate, ///< Immediate operand.
45 kSFPImmediate, ///< Single-floating-point immediate operand.
46 kDFPImmediate, ///< Double-Floating-point immediate operand.
47 kExpr, ///< Relocatable immediate operand.
48 kInst ///< Sub-instruction operand.
49 };
50 MachineOperandType Kind = kInvalid;
51
52 union {
53 unsigned RegVal;
54 int64_t ImmVal;
59 };
60
61public:
63
64 bool isValid() const { return Kind != kInvalid; }
65 bool isReg() const { return Kind == kRegister; }
66 bool isImm() const { return Kind == kImmediate; }
67 bool isSFPImm() const { return Kind == kSFPImmediate; }
68 bool isDFPImm() const { return Kind == kDFPImmediate; }
69 bool isExpr() const { return Kind == kExpr; }
70 bool isInst() const { return Kind == kInst; }
71
72 /// Returns the register number.
74 assert(isReg() && "This is not a register operand!");
75 return RegVal;
76 }
77
78 /// Set the register number.
80 assert(isReg() && "This is not a register operand!");
81 RegVal = Reg.id();
82 }
83
84 int64_t getImm() const {
85 assert(isImm() && "This is not an immediate");
86 return ImmVal;
87 }
88
89 void setImm(int64_t Val) {
90 assert(isImm() && "This is not an immediate");
91 ImmVal = Val;
92 }
93
95 assert(isSFPImm() && "This is not an SFP immediate");
96 return SFPImmVal;
97 }
98
99 void setSFPImm(uint32_t Val) {
100 assert(isSFPImm() && "This is not an SFP immediate");
101 SFPImmVal = Val;
102 }
103
105 assert(isDFPImm() && "This is not an FP immediate");
106 return FPImmVal;
107 }
108
109 void setDFPImm(uint64_t Val) {
110 assert(isDFPImm() && "This is not an FP immediate");
111 FPImmVal = Val;
112 }
113 void setFPImm(double Val) {
114 assert(isDFPImm() && "This is not an FP immediate");
115 FPImmVal = bit_cast<uint64_t>(Val);
116 }
117
118 const MCExpr *getExpr() const {
119 assert(isExpr() && "This is not an expression");
120 return ExprVal;
121 }
122
123 void setExpr(const MCExpr *Val) {
124 assert(isExpr() && "This is not an expression");
125 ExprVal = Val;
126 }
127
128 const MCInst *getInst() const {
129 assert(isInst() && "This is not a sub-instruction");
130 return InstVal;
131 }
132
133 void setInst(const MCInst *Val) {
134 assert(isInst() && "This is not a sub-instruction");
135 InstVal = Val;
136 }
137
140 Op.Kind = kRegister;
141 Op.RegVal = Reg.id();
142 return Op;
143 }
144
145 static MCOperand createImm(int64_t Val) {
147 Op.Kind = kImmediate;
148 Op.ImmVal = Val;
149 return Op;
150 }
151
154 Op.Kind = kSFPImmediate;
155 Op.SFPImmVal = Val;
156 return Op;
157 }
158
161 Op.Kind = kDFPImmediate;
162 Op.FPImmVal = Val;
163 return Op;
164 }
165
166 static MCOperand createExpr(const MCExpr *Val) {
168 Op.Kind = kExpr;
169 Op.ExprVal = Val;
170 return Op;
171 }
172
173 static MCOperand createInst(const MCInst *Val) {
175 Op.Kind = kInst;
176 Op.InstVal = Val;
177 return Op;
178 }
179
180 LLVM_ABI void print(raw_ostream &OS, const MCContext *Ctx = nullptr) const;
181 LLVM_ABI void dump() const;
182 LLVM_ABI bool isBareSymbolRef() const;
183 LLVM_ABI bool evaluateAsConstantImm(int64_t &Imm) const;
184};
185
186/// Instances of this class represent a single low-level machine
187/// instruction.
188class MCInst {
189 unsigned Opcode = 0;
190 // These flags could be used to pass some info from one target subcomponent
191 // to another, for example, from disassembler to asm printer. The values of
192 // the flags have any sense on target level only (e.g. prefixes on x86).
193 unsigned Flags = 0;
194
195 SMLoc Loc;
197
198public:
199 MCInst() = default;
200
201 void setOpcode(unsigned Op) { Opcode = Op; }
202 unsigned getOpcode() const { return Opcode; }
203
204 void setFlags(unsigned F) { Flags = F; }
205 unsigned getFlags() const { return Flags; }
206
207 void setLoc(SMLoc loc) { Loc = loc; }
208 SMLoc getLoc() const { return Loc; }
209
210 const MCOperand &getOperand(unsigned i) const { return Operands[i]; }
211 MCOperand &getOperand(unsigned i) { return Operands[i]; }
212 unsigned getNumOperands() const { return Operands.size(); }
213
215 void addOperand(const MCOperand Op) { Operands.push_back(Op); }
217 Operands.assign(Ops.begin(), Ops.end());
218 }
219
222
223 void clear() { Operands.clear(); }
224 void erase(iterator I) { Operands.erase(I); }
226 size_t size() const { return Operands.size(); }
227 iterator begin() { return Operands.begin(); }
228 const_iterator begin() const { return Operands.begin(); }
229 iterator end() { return Operands.end(); }
230 const_iterator end() const { return Operands.end(); }
231
233 return Operands.insert(I, Op);
234 }
235
236 LLVM_ABI void print(raw_ostream &OS, const MCContext *Ctx = nullptr) const;
237 LLVM_ABI void dump() const;
238
239 /// Dump the MCInst as prettily as possible using the additional MC
240 /// structures, if given. Operators are separated by the \p Separator
241 /// string.
243 const MCInstPrinter *Printer = nullptr,
244 StringRef Separator = " ",
245 const MCContext *Ctx = nullptr) const;
247 StringRef Separator = " ",
248 const MCContext *Ctx = nullptr) const;
249};
250
252 MO.print(OS);
253 return OS;
254}
255
257 MI.print(OS);
258 return OS;
259}
260
261} // end namespace llvm
262
263#endif // LLVM_MC_MCINST_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
dxil pretty DXIL Metadata Pretty Printer
std::string Name
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
mir Rename Register Operands
Register Reg
raw_pwrite_stream & OS
This file defines the SmallVector class.
This file implements the C++20 <bit> header.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:136
iterator begin() const
Definition: ArrayRef.h:135
This class represents an Operation in the Expression.
Context object for machine code objects.
Definition: MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:46
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:188
LLVM_ABI void print(raw_ostream &OS, const MCContext *Ctx=nullptr) const
Definition: MCInst.cpp:81
const_iterator end() const
Definition: MCInst.h:230
void erase(iterator I)
Definition: MCInst.h:224
MCOperand & getOperand(unsigned i)
Definition: MCInst.h:211
unsigned getNumOperands() const
Definition: MCInst.h:212
SMLoc getLoc() const
Definition: MCInst.h:208
SmallVectorImpl< MCOperand >::iterator iterator
Definition: MCInst.h:220
unsigned getFlags() const
Definition: MCInst.h:205
void setLoc(SMLoc loc)
Definition: MCInst.h:207
unsigned getOpcode() const
Definition: MCInst.h:202
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:225
MCInst()=default
ArrayRef< MCOperand > getOperands() const
Definition: MCInst.h:214
const_iterator begin() const
Definition: MCInst.h:228
iterator insert(iterator I, const MCOperand &Op)
Definition: MCInst.h:232
void setFlags(unsigned F)
Definition: MCInst.h:204
void setOperands(ArrayRef< MCOperand > Ops)
Definition: MCInst.h:216
LLVM_ABI void dump() const
Definition: MCInst.cpp:112
void addOperand(const MCOperand Op)
Definition: MCInst.h:215
iterator begin()
Definition: MCInst.h:227
iterator end()
Definition: MCInst.h:229
SmallVectorImpl< MCOperand >::const_iterator const_iterator
Definition: MCInst.h:221
size_t size() const
Definition: MCInst.h:226
void setOpcode(unsigned Op)
Definition: MCInst.h:201
void clear()
Definition: MCInst.h:223
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:210
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:40
bool isSFPImm() const
Definition: MCInst.h:67
unsigned RegVal
Definition: MCInst.h:53
const MCExpr * ExprVal
Definition: MCInst.h:57
void setImm(int64_t Val)
Definition: MCInst.h:89
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:166
LLVM_ABI bool isBareSymbolRef() const
Definition: MCInst.cpp:65
static MCOperand createSFPImm(uint32_t Val)
Definition: MCInst.h:152
LLVM_ABI void print(raw_ostream &OS, const MCContext *Ctx=nullptr) const
Definition: MCInst.cpp:23
void setExpr(const MCExpr *Val)
Definition: MCInst.h:123
int64_t getImm() const
Definition: MCInst.h:84
static MCOperand createReg(MCRegister Reg)
Definition: MCInst.h:138
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:145
bool isImm() const
Definition: MCInst.h:66
void setReg(MCRegister Reg)
Set the register number.
Definition: MCInst.h:79
uint32_t SFPImmVal
Definition: MCInst.h:55
int64_t ImmVal
Definition: MCInst.h:54
bool isInst() const
Definition: MCInst.h:70
bool isReg() const
Definition: MCInst.h:65
void setInst(const MCInst *Val)
Definition: MCInst.h:133
MCRegister getReg() const
Returns the register number.
Definition: MCInst.h:73
void setSFPImm(uint32_t Val)
Definition: MCInst.h:99
uint64_t FPImmVal
Definition: MCInst.h:56
LLVM_ABI bool evaluateAsConstantImm(int64_t &Imm) const
Definition: MCInst.cpp:57
bool isDFPImm() const
Definition: MCInst.h:68
LLVM_ABI void dump() const
Definition: MCInst.cpp:75
const MCInst * getInst() const
Definition: MCInst.h:128
bool isValid() const
Definition: MCInst.h:64
void setDFPImm(uint64_t Val)
Definition: MCInst.h:109
const MCExpr * getExpr() const
Definition: MCInst.h:118
void setFPImm(double Val)
Definition: MCInst.h:113
uint32_t getSFPImm() const
Definition: MCInst.h:94
static MCOperand createDFPImm(uint64_t Val)
Definition: MCInst.h:159
uint64_t getDFPImm() const
Definition: MCInst.h:104
bool isExpr() const
Definition: MCInst.h:69
static MCOperand createInst(const MCInst *Val)
Definition: MCInst.h:173
const MCInst * InstVal
Definition: MCInst.h:58
Wrapper class representing physical registers. Should be passed by value.
Definition: MCRegister.h:33
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition: Pass.cpp:140
Represents a location in source code.
Definition: SMLoc.h:23
typename SuperClass::const_iterator const_iterator
Definition: SmallVector.h:579
typename SuperClass::iterator iterator
Definition: SmallVector.h:578
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
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.
Definition: AddressRanges.h:18
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:312