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/APFloat.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/System/DataTypes.h"
30 /// MCOperand - Instances of this class represent operands of the MCInst class.
31 /// This is a simple discriminated union.
33 enum MachineOperandType {
34 kInvalid, ///< Uninitialized.
35 kRegister, ///< Register operand.
36 kImmediate, ///< Immediate operand.
37 kFPImmediate, ///< Floating-point immediate operand.
38 kExpr ///< Relocatable immediate operand.
45 const MCExpr *ExprVal;
47 // This can't go in the union due to the non-trivial copy constructor
48 // of APFloat. It's still only valid for Kind == kFPImmediate, though.
52 MCOperand() : Kind(kInvalid), FPImmVal(0.0) {}
54 bool isValid() const { return Kind != kInvalid; }
55 bool isReg() const { return Kind == kRegister; }
56 bool isImm() const { return Kind == kImmediate; }
57 bool isFPImm() const { return Kind == kFPImmediate; }
58 bool isExpr() const { return Kind == kExpr; }
60 /// getReg - Returns the register number.
61 unsigned getReg() const {
62 assert(isReg() && "This is not a register operand!");
66 /// setReg - Set the register number.
67 void setReg(unsigned Reg) {
68 assert(isReg() && "This is not a register operand!");
72 int64_t getImm() const {
73 assert(isImm() && "This is not an immediate");
76 void setImm(int64_t Val) {
77 assert(isImm() && "This is not an immediate");
81 const APFloat &getFPImm() const {
82 assert(isFPImm() && "This is not an FP immediate");
86 void setFPImm(const APFloat &Val) {
87 assert(isFPImm() && "This is not an FP immediate");
91 const MCExpr *getExpr() const {
92 assert(isExpr() && "This is not an expression");
95 void setExpr(const MCExpr *Val) {
96 assert(isExpr() && "This is not an expression");
100 static MCOperand CreateReg(unsigned Reg) {
106 static MCOperand CreateImm(int64_t Val) {
108 Op.Kind = kImmediate;
112 static MCOperand CreateFPImm(const APFloat &Val) {
114 Op.Kind = kFPImmediate;
118 static MCOperand CreateExpr(const MCExpr *Val) {
125 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
130 /// MCInst - Instances of this class represent a single low-level machine
134 SmallVector<MCOperand, 8> Operands;
136 MCInst() : Opcode(0) {}
138 void setOpcode(unsigned Op) { Opcode = Op; }
140 unsigned getOpcode() const { return Opcode; }
142 const MCOperand &getOperand(unsigned i) const { return Operands[i]; }
143 MCOperand &getOperand(unsigned i) { return Operands[i]; }
144 unsigned getNumOperands() const { return Operands.size(); }
146 void addOperand(const MCOperand &Op) {
147 Operands.push_back(Op);
150 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
153 /// \brief Dump the MCInst as prettily as possible using the additional MC
154 /// structures, if given. Operators are separated by the \arg Separator
156 void dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI = 0,
157 const MCInstPrinter *Printer = 0,
158 StringRef Separator = " ") const;
162 } // end namespace llvm