1 //===-- llvm/MC/MCInst.h - MCInst class -------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_MC_MCINST_H
17 #define LLVM_MC_MCINST_H
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/SMLoc.h"
31 /// MCOperand - Instances of this class represent operands of the MCInst class.
32 /// This is a simple discriminated union.
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.
48 const MCExpr *ExprVal;
49 const MCInst *InstVal;
53 MCOperand() : Kind(kInvalid), FPImmVal(0.0) {}
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; }
62 /// getReg - Returns the register number.
63 unsigned getReg() const {
64 assert(isReg() && "This is not a register operand!");
68 /// setReg - Set the register number.
69 void setReg(unsigned Reg) {
70 assert(isReg() && "This is not a register operand!");
74 int64_t getImm() const {
75 assert(isImm() && "This is not an immediate");
78 void setImm(int64_t Val) {
79 assert(isImm() && "This is not an immediate");
83 double getFPImm() const {
84 assert(isFPImm() && "This is not an FP immediate");
88 void setFPImm(double Val) {
89 assert(isFPImm() && "This is not an FP immediate");
93 const MCExpr *getExpr() const {
94 assert(isExpr() && "This is not an expression");
97 void setExpr(const MCExpr *Val) {
98 assert(isExpr() && "This is not an expression");
102 const MCInst *getInst() const {
103 assert(isInst() && "This is not a sub-instruction");
106 void setInst(const MCInst *Val) {
107 assert(isInst() && "This is not a sub-instruction");
111 static MCOperand CreateReg(unsigned Reg) {
117 static MCOperand CreateImm(int64_t Val) {
119 Op.Kind = kImmediate;
123 static MCOperand CreateFPImm(double Val) {
125 Op.Kind = kFPImmediate;
129 static MCOperand CreateExpr(const MCExpr *Val) {
135 static MCOperand CreateInst(const MCInst *Val) {
142 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
146 template <> struct isPodLike<MCOperand> { static const bool value = true; };
148 /// MCInst - Instances of this class represent a single low-level machine
153 SmallVector<MCOperand, 8> Operands;
155 MCInst() : Opcode(0) {}
157 void setOpcode(unsigned Op) { Opcode = Op; }
158 unsigned getOpcode() const { return Opcode; }
160 void setLoc(SMLoc loc) { Loc = loc; }
161 SMLoc getLoc() const { return Loc; }
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(); }
167 void addOperand(const MCOperand &Op) {
168 Operands.push_back(Op);
171 void clear() { Operands.clear(); }
172 size_t size() { return Operands.size(); }
174 typedef SmallVector<MCOperand, 8>::iterator iterator;
175 iterator begin() { return Operands.begin(); }
176 iterator end() { return Operands.end(); }
177 iterator insert(iterator I, const MCOperand &Op) {
178 return Operands.insert(I, Op);
181 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
184 /// \brief Dump the MCInst as prettily as possible using the additional MC
185 /// structures, if given. Operators are separated by the \arg Separator
187 void dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI = 0,
188 const MCInstPrinter *Printer = 0,
189 StringRef Separator = " ") const;
192 inline raw_ostream& operator<<(raw_ostream &OS, const MCOperand &MO) {
197 inline raw_ostream& operator<<(raw_ostream &OS, const MCInst &MI) {
202 } // end namespace llvm