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"
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.
39 kInst ///< Sub-instruction operand.
47 const MCExpr *ExprVal;
48 const MCInst *InstVal;
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; }
59 bool isInst() const { return Kind == kInst; }
61 /// getReg - Returns the register number.
62 unsigned getReg() const {
63 assert(isReg() && "This is not a register operand!");
67 /// setReg - Set the register number.
68 void setReg(unsigned Reg) {
69 assert(isReg() && "This is not a register operand!");
73 int64_t getImm() const {
74 assert(isImm() && "This is not an immediate");
77 void setImm(int64_t Val) {
78 assert(isImm() && "This is not an immediate");
82 double getFPImm() const {
83 assert(isFPImm() && "This is not an FP immediate");
87 void setFPImm(double Val) {
88 assert(isFPImm() && "This is not an FP immediate");
92 const MCExpr *getExpr() const {
93 assert(isExpr() && "This is not an expression");
96 void setExpr(const MCExpr *Val) {
97 assert(isExpr() && "This is not an expression");
101 const MCInst *getInst() const {
102 assert(isInst() && "This is not a sub-instruction");
105 void setInst(const MCInst *Val) {
106 assert(isInst() && "This is not a sub-instruction");
110 static MCOperand CreateReg(unsigned Reg) {
116 static MCOperand CreateImm(int64_t Val) {
118 Op.Kind = kImmediate;
122 static MCOperand CreateFPImm(double Val) {
124 Op.Kind = kFPImmediate;
128 static MCOperand CreateExpr(const MCExpr *Val) {
134 static MCOperand CreateInst(const MCInst *Val) {
141 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
145 template <> struct isPodLike<MCOperand> { static const bool value = true; };
147 /// MCInst - Instances of this class represent a single low-level machine
151 SmallVector<MCOperand, 8> Operands;
153 MCInst() : Opcode(0) {}
155 void setOpcode(unsigned Op) { Opcode = Op; }
157 unsigned getOpcode() const { return Opcode; }
159 const MCOperand &getOperand(unsigned i) const { return Operands[i]; }
160 MCOperand &getOperand(unsigned i) { return Operands[i]; }
161 unsigned getNumOperands() const { return Operands.size(); }
163 void addOperand(const MCOperand &Op) {
164 Operands.push_back(Op);
167 void clear() { Operands.clear(); }
168 size_t size() { return Operands.size(); }
170 typedef SmallVector<MCOperand, 8>::iterator iterator;
171 iterator begin() { return Operands.begin(); }
172 iterator end() { return Operands.end(); }
173 iterator insert(iterator I, const MCOperand &Op) {
174 return Operands.insert(I, Op);
177 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
180 /// \brief Dump the MCInst as prettily as possible using the additional MC
181 /// structures, if given. Operators are separated by the \arg Separator
183 void dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI = 0,
184 const MCInstPrinter *Printer = 0,
185 StringRef Separator = " ") const;
188 inline raw_ostream& operator<<(raw_ostream &OS, const MCOperand &MO) {
193 inline raw_ostream& operator<<(raw_ostream &OS, const MCInst &MI) {
198 } // end namespace llvm