LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/DataTypes.h"
22 #include "llvm/Support/SMLoc.h"
23 
24 namespace llvm {
25 class raw_ostream;
26 class MCAsmInfo;
27 class MCInstPrinter;
28 class MCExpr;
29 class MCInst;
30 
31 /// MCOperand - Instances of this class represent operands of the MCInst class.
32 /// This is a simple discriminated union.
33 class MCOperand {
34  enum MachineOperandType {
35  kInvalid, ///< Uninitialized.
36  kRegister, ///< Register operand.
37  kImmediate, ///< Immediate operand.
38  kFPImmediate, ///< Floating-point immediate operand.
39  kExpr, ///< Relocatable immediate operand.
40  kInst ///< Sub-instruction operand.
41  };
42  unsigned char Kind;
43 
44  union {
45  unsigned RegVal;
46  int64_t ImmVal;
47  double FPImmVal;
48  const MCExpr *ExprVal;
49  const MCInst *InstVal;
50  };
51 public:
52 
53  MCOperand() : Kind(kInvalid), FPImmVal(0.0) {}
54 
55  bool isValid() const { return Kind != kInvalid; }
56  bool isReg() const { return Kind == kRegister; }
57  bool isImm() const { return Kind == kImmediate; }
58  bool isFPImm() const { return Kind == kFPImmediate; }
59  bool isExpr() const { return Kind == kExpr; }
60  bool isInst() const { return Kind == kInst; }
61 
62  /// getReg - Returns the register number.
63  unsigned getReg() const {
64  assert(isReg() && "This is not a register operand!");
65  return RegVal;
66  }
67 
68  /// setReg - Set the register number.
69  void setReg(unsigned Reg) {
70  assert(isReg() && "This is not a register operand!");
71  RegVal = Reg;
72  }
73 
74  int64_t getImm() const {
75  assert(isImm() && "This is not an immediate");
76  return ImmVal;
77  }
78  void setImm(int64_t Val) {
79  assert(isImm() && "This is not an immediate");
80  ImmVal = Val;
81  }
82 
83  double getFPImm() const {
84  assert(isFPImm() && "This is not an FP immediate");
85  return FPImmVal;
86  }
87 
88  void setFPImm(double Val) {
89  assert(isFPImm() && "This is not an FP immediate");
90  FPImmVal = Val;
91  }
92 
93  const MCExpr *getExpr() const {
94  assert(isExpr() && "This is not an expression");
95  return ExprVal;
96  }
97  void setExpr(const MCExpr *Val) {
98  assert(isExpr() && "This is not an expression");
99  ExprVal = Val;
100  }
101 
102  const MCInst *getInst() const {
103  assert(isInst() && "This is not a sub-instruction");
104  return InstVal;
105  }
106  void setInst(const MCInst *Val) {
107  assert(isInst() && "This is not a sub-instruction");
108  InstVal = Val;
109  }
110 
111  static MCOperand CreateReg(unsigned Reg) {
112  MCOperand Op;
113  Op.Kind = kRegister;
114  Op.RegVal = Reg;
115  return Op;
116  }
117  static MCOperand CreateImm(int64_t Val) {
118  MCOperand Op;
119  Op.Kind = kImmediate;
120  Op.ImmVal = Val;
121  return Op;
122  }
123  static MCOperand CreateFPImm(double Val) {
124  MCOperand Op;
125  Op.Kind = kFPImmediate;
126  Op.FPImmVal = Val;
127  return Op;
128  }
129  static MCOperand CreateExpr(const MCExpr *Val) {
130  MCOperand Op;
131  Op.Kind = kExpr;
132  Op.ExprVal = Val;
133  return Op;
134  }
135  static MCOperand CreateInst(const MCInst *Val) {
136  MCOperand Op;
137  Op.Kind = kInst;
138  Op.InstVal = Val;
139  return Op;
140  }
141 
142  void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
143  void dump() const;
144 };
145 
146 template <> struct isPodLike<MCOperand> { static const bool value = true; };
147 
148 /// MCInst - Instances of this class represent a single low-level machine
149 /// instruction.
150 class MCInst {
151  unsigned Opcode;
152  SMLoc Loc;
153  SmallVector<MCOperand, 8> Operands;
154 public:
155  MCInst() : Opcode(0) {}
156 
157  void setOpcode(unsigned Op) { Opcode = Op; }
158  unsigned getOpcode() const { return Opcode; }
159 
160  void setLoc(SMLoc loc) { Loc = loc; }
161  SMLoc getLoc() const { return Loc; }
162 
163  const MCOperand &getOperand(unsigned i) const { return Operands[i]; }
164  MCOperand &getOperand(unsigned i) { return Operands[i]; }
165  unsigned getNumOperands() const { return Operands.size(); }
166 
167  void addOperand(const MCOperand &Op) {
168  Operands.push_back(Op);
169  }
170 
171  void clear() { Operands.clear(); }
172  size_t size() { return Operands.size(); }
173 
175  iterator begin() { return Operands.begin(); }
176  iterator end() { return Operands.end(); }
178  return Operands.insert(I, Op);
179  }
180 
181  void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
182  void dump() const;
183 
184  /// \brief Dump the MCInst as prettily as possible using the additional MC
185  /// structures, if given. Operators are separated by the \p Separator
186  /// string.
187  void dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI = 0,
188  const MCInstPrinter *Printer = 0,
189  StringRef Separator = " ") const;
190 };
191 
192 inline raw_ostream& operator<<(raw_ostream &OS, const MCOperand &MO) {
193  MO.print(OS, 0);
194  return OS;
195 }
196 
198  MI.print(OS, 0);
199  return OS;
200 }
201 
202 } // end namespace llvm
203 
204 #endif
iterator end()
Definition: MCInst.h:176
iterator begin()
Definition: MCInst.h:175
bool isValid() const
Definition: MCInst.h:55
double getFPImm() const
Definition: MCInst.h:83
void clear()
Definition: MCInst.h:171
static MCOperand CreateReg(unsigned Reg)
Definition: MCInst.h:111
bool isReg() const
Definition: MCInst.h:56
unsigned RegVal
Definition: MCInst.h:45
void setInst(const MCInst *Val)
Definition: MCInst.h:106
static MCOperand CreateExpr(const MCExpr *Val)
Definition: MCInst.h:129
static const bool value
Definition: type_traits.h:74
print alias Alias Set Printer
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
Definition: MCInst.cpp:42
int64_t ImmVal
Definition: MCInst.h:46
MCOperand & getOperand(unsigned i)
Definition: MCInst.h:164
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
Definition: MCInst.cpp:18
double FPImmVal
Definition: MCInst.h:47
void setFPImm(double Val)
Definition: MCInst.h:88
unsigned getReg() const
getReg - Returns the register number.
Definition: MCInst.h:63
iterator insert(iterator I, const MCOperand &Op)
Definition: MCInst.h:177
bool isImm() const
Definition: MCInst.h:57
void dump() const
Definition: MCInst.cpp:68
SmallVectorImpl< MCOperand >::iterator iterator
Definition: MCInst.h:174
const MCExpr * getExpr() const
Definition: MCInst.h:93
void setImm(int64_t Val)
Definition: MCInst.h:78
size_t size()
Definition: MCInst.h:172
void dump() const
Definition: MCInst.cpp:36
bool isFPImm() const
Definition: MCInst.h:58
bool isExpr() const
Definition: MCInst.h:59
SMLoc getLoc() const
Definition: MCInst.h:161
const MCInst * InstVal
Definition: MCInst.h:49
bool isInst() const
Definition: MCInst.h:60
void setLoc(SMLoc loc)
Definition: MCInst.h:160
void setOpcode(unsigned Op)
Definition: MCInst.h:157
unsigned getOpcode() const
Definition: MCInst.h:158
int64_t getImm() const
Definition: MCInst.h:74
void dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI=0, const MCInstPrinter *Printer=0, StringRef Separator=" ") const
Dump the MCInst as prettily as possible using the additional MC structures, if given. Operators are separated by the Separator string.
Definition: MCInst.cpp:51
static MCOperand CreateImm(int64_t Val)
Definition: MCInst.h:117
unsigned getNumOperands() const
Definition: MCInst.h:165
#define I(x, y, z)
Definition: MD5.cpp:54
const MCInst * getInst() const
Definition: MCInst.h:102
static MCOperand CreateFPImm(double Val)
Definition: MCInst.h:123
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:1688
void setReg(unsigned Reg)
setReg - Set the register number.
Definition: MCInst.h:69
void addOperand(const MCOperand &Op)
Definition: MCInst.h:167
Represents a location in source code.
Definition: SMLoc.h:23
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:163
const MCExpr * ExprVal
Definition: MCInst.h:48
static MCOperand CreateInst(const MCInst *Val)
Definition: MCInst.h:135
void setExpr(const MCExpr *Val)
Definition: MCInst.h:97