LLVM  8.0.1
MCInst.h
Go to the documentation of this file.
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 
16 #ifndef LLVM_MC_MCINST_H
17 #define LLVM_MC_MCINST_H
18 
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/SMLoc.h"
22 #include <cassert>
23 #include <cstddef>
24 #include <cstdint>
25 
26 namespace llvm {
27 
28 class MCExpr;
29 class MCInst;
30 class MCInstPrinter;
31 class raw_ostream;
32 
33 /// Instances of this class represent operands of the MCInst class.
34 /// This is a simple discriminated union.
35 class MCOperand {
36  enum MachineOperandType : unsigned char {
37  kInvalid, ///< Uninitialized.
38  kRegister, ///< Register operand.
39  kImmediate, ///< Immediate operand.
40  kFPImmediate, ///< Floating-point immediate operand.
41  kExpr, ///< Relocatable immediate operand.
42  kInst ///< Sub-instruction operand.
43  };
44  MachineOperandType Kind = kInvalid;
45 
46  union {
47  unsigned RegVal;
48  int64_t ImmVal;
49  double FPImmVal;
50  const MCExpr *ExprVal;
51  const MCInst *InstVal;
52  };
53 
54 public:
55  MCOperand() : FPImmVal(0.0) {}
56 
57  bool isValid() const { return Kind != kInvalid; }
58  bool isReg() const { return Kind == kRegister; }
59  bool isImm() const { return Kind == kImmediate; }
60  bool isFPImm() const { return Kind == kFPImmediate; }
61  bool isExpr() const { return Kind == kExpr; }
62  bool isInst() const { return Kind == kInst; }
63 
64  /// Returns the register number.
65  unsigned getReg() const {
66  assert(isReg() && "This is not a register operand!");
67  return RegVal;
68  }
69 
70  /// Set the register number.
71  void setReg(unsigned Reg) {
72  assert(isReg() && "This is not a register operand!");
73  RegVal = Reg;
74  }
75 
76  int64_t getImm() const {
77  assert(isImm() && "This is not an immediate");
78  return ImmVal;
79  }
80 
81  void setImm(int64_t Val) {
82  assert(isImm() && "This is not an immediate");
83  ImmVal = Val;
84  }
85 
86  double getFPImm() const {
87  assert(isFPImm() && "This is not an FP immediate");
88  return FPImmVal;
89  }
90 
91  void setFPImm(double Val) {
92  assert(isFPImm() && "This is not an FP immediate");
93  FPImmVal = Val;
94  }
95 
96  const MCExpr *getExpr() const {
97  assert(isExpr() && "This is not an expression");
98  return ExprVal;
99  }
100 
101  void setExpr(const MCExpr *Val) {
102  assert(isExpr() && "This is not an expression");
103  ExprVal = Val;
104  }
105 
106  const MCInst *getInst() const {
107  assert(isInst() && "This is not a sub-instruction");
108  return InstVal;
109  }
110 
111  void setInst(const MCInst *Val) {
112  assert(isInst() && "This is not a sub-instruction");
113  InstVal = Val;
114  }
115 
116  static MCOperand createReg(unsigned Reg) {
117  MCOperand Op;
118  Op.Kind = kRegister;
119  Op.RegVal = Reg;
120  return Op;
121  }
122 
123  static MCOperand createImm(int64_t Val) {
124  MCOperand Op;
125  Op.Kind = kImmediate;
126  Op.ImmVal = Val;
127  return Op;
128  }
129 
130  static MCOperand createFPImm(double Val) {
131  MCOperand Op;
132  Op.Kind = kFPImmediate;
133  Op.FPImmVal = Val;
134  return Op;
135  }
136 
137  static MCOperand createExpr(const MCExpr *Val) {
138  MCOperand Op;
139  Op.Kind = kExpr;
140  Op.ExprVal = Val;
141  return Op;
142  }
143 
144  static MCOperand createInst(const MCInst *Val) {
145  MCOperand Op;
146  Op.Kind = kInst;
147  Op.InstVal = Val;
148  return Op;
149  }
150 
151  void print(raw_ostream &OS) const;
152  void dump() const;
153  bool isBareSymbolRef() const;
154  bool evaluateAsConstantImm(int64_t &Imm) const;
155 };
156 
157 template <> struct isPodLike<MCOperand> { static const bool value = true; };
158 
159 /// Instances of this class represent a single low-level machine
160 /// instruction.
161 class MCInst {
162  unsigned Opcode = 0;
163  SMLoc Loc;
164  SmallVector<MCOperand, 8> Operands;
165  // These flags could be used to pass some info from one target subcomponent
166  // to another, for example, from disassembler to asm printer. The values of
167  // the flags have any sense on target level only (e.g. prefixes on x86).
168  unsigned Flags = 0;
169 
170 public:
171  MCInst() = default;
172 
173  void setOpcode(unsigned Op) { Opcode = Op; }
174  unsigned getOpcode() const { return Opcode; }
175 
176  void setFlags(unsigned F) { Flags = F; }
177  unsigned getFlags() const { return Flags; }
178 
179  void setLoc(SMLoc loc) { Loc = loc; }
180  SMLoc getLoc() const { return Loc; }
181 
182  const MCOperand &getOperand(unsigned i) const { return Operands[i]; }
183  MCOperand &getOperand(unsigned i) { return Operands[i]; }
184  unsigned getNumOperands() const { return Operands.size(); }
185 
186  void addOperand(const MCOperand &Op) { Operands.push_back(Op); }
187 
190 
191  void clear() { Operands.clear(); }
192  void erase(iterator I) { Operands.erase(I); }
193  size_t size() const { return Operands.size(); }
194  iterator begin() { return Operands.begin(); }
195  const_iterator begin() const { return Operands.begin(); }
196  iterator end() { return Operands.end(); }
197  const_iterator end() const { return Operands.end(); }
198 
200  return Operands.insert(I, Op);
201  }
202 
203  void print(raw_ostream &OS) const;
204  void dump() const;
205 
206  /// Dump the MCInst as prettily as possible using the additional MC
207  /// structures, if given. Operators are separated by the \p Separator
208  /// string.
209  void dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer = nullptr,
210  StringRef Separator = " ") const;
211  void dump_pretty(raw_ostream &OS, StringRef Name,
212  StringRef Separator = " ") const;
213 };
214 
215 inline raw_ostream& operator<<(raw_ostream &OS, const MCOperand &MO) {
216  MO.print(OS);
217  return OS;
218 }
219 
221  MI.print(OS);
222  return OS;
223 }
224 
225 } // end namespace llvm
226 
227 #endif // LLVM_MC_MCINST_H
iterator end()
Definition: MCInst.h:196
iterator begin()
Definition: MCInst.h:194
bool isImm() const
Definition: MCInst.h:59
This class represents lattice values for constants.
Definition: AllocatorList.h:24
void clear()
Definition: MCInst.h:191
unsigned RegVal
Definition: MCInst.h:47
static MCOperand createExpr(const MCExpr *Val)
Definition: MCInst.h:137
void setInst(const MCInst *Val)
Definition: MCInst.h:111
void print(raw_ostream &OS) const
Definition: MCInst.cpp:21
unsigned Reg
bool isReg() const
Definition: MCInst.h:58
F(f)
print alias Alias Set Printer
int64_t ImmVal
Definition: MCInst.h:48
MCOperand & getOperand(unsigned i)
Definition: MCInst.h:183
amdgpu Simplify well known AMD library false Value Value const Twine & Name
bool isBareSymbolRef() const
Definition: MCInst.cpp:48
static MCOperand createReg(unsigned Reg)
Definition: MCInst.h:116
double FPImmVal
Definition: MCInst.h:49
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
void erase(iterator I)
Definition: MCInst.h:192
void dump() const
Definition: MCInst.cpp:58
const_iterator begin() const
Definition: MCInst.h:195
void setFPImm(double Val)
Definition: MCInst.h:91
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:65
const MCInst * getInst() const
Definition: MCInst.h:106
const MCExpr * getExpr() const
Definition: MCInst.h:96
iterator insert(iterator I, const MCOperand &Op)
Definition: MCInst.h:199
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:161
int64_t getImm() const
Definition: MCInst.h:76
void setImm(int64_t Val)
Definition: MCInst.h:81
bool isInst() const
Definition: MCInst.h:62
unsigned getFlags() const
Definition: MCInst.h:177
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:129
bool isFPImm() const
Definition: MCInst.h:60
bool isExpr() const
Definition: MCInst.h:61
unsigned getNumOperands() const
Definition: MCInst.h:184
SmallVectorImpl< MCOperand >::iterator iterator
Definition: MCInst.h:188
void print(raw_ostream &OS) const
Definition: MCInst.cpp:64
const MCInst * InstVal
Definition: MCInst.h:51
iterator erase(const_iterator CI)
Definition: SmallVector.h:445
size_t size() const
Definition: SmallVector.h:53
void setLoc(SMLoc loc)
Definition: MCInst.h:179
void setFlags(unsigned F)
Definition: MCInst.h:176
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: ArrayRef.h:530
void setOpcode(unsigned Op)
Definition: MCInst.h:173
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:182
static MCOperand createFPImm(double Val)
Definition: MCInst.h:130
const_iterator end() const
Definition: MCInst.h:197
SmallVectorImpl< MCOperand >::const_iterator const_iterator
Definition: MCInst.h:189
SMLoc getLoc() const
Definition: MCInst.h:180
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:478
static MCOperand createInst(const MCInst *Val)
Definition: MCInst.h:144
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:133
This is an instance of a target assembly language printer that converts an MCInst to valid target ass...
Definition: MCInstPrinter.h:40
size_t size() const
Definition: MCInst.h:193
bool evaluateAsConstantImm(int64_t &Imm) const
Definition: MCInst.cpp:40
#define I(x, y, z)
Definition: MD5.cpp:58
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2039
void setReg(unsigned Reg)
Set the register number.
Definition: MCInst.h:71
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
IRTranslator LLVM IR MI
void addOperand(const MCOperand &Op)
Definition: MCInst.h:186
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool isValid() const
Definition: MCInst.h:57
Represents a location in source code.
Definition: SMLoc.h:24
unsigned getOpcode() const
Definition: MCInst.h:174
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:35
static MCOperand createImm(int64_t Val)
Definition: MCInst.h:123
double getFPImm() const
Definition: MCInst.h:86
const MCExpr * ExprVal
Definition: MCInst.h:50
void setExpr(const MCExpr *Val)
Definition: MCInst.h:101