9facf3bb5b54d40f46d94a736b911a9e50ab07ef
[oota-llvm.git] / include / llvm / MC / MCInst.h
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/Support/DataTypes.h"
21 #include "llvm/Support/DebugLoc.h"
22
23 namespace llvm {
24 class raw_ostream;
25 class MCAsmInfo;
26 class MCExpr;
27
28 /// MCOperand - Instances of this class represent operands of the MCInst class.
29 /// This is a simple discriminated union.
30 class MCOperand {
31   enum MachineOperandType {
32     kInvalid,                 ///< Uninitialized.
33     kRegister,                ///< Register operand.
34     kImmediate,               ///< Immediate operand.
35     kMBBLabel,                ///< Basic block label.
36     kExpr                     ///< Relocatable immediate operand.
37   };
38   unsigned char Kind;
39   
40   union {
41     unsigned RegVal;
42     int64_t ImmVal;
43     const MCExpr *ExprVal;
44     struct {
45       unsigned FunctionNo;
46       unsigned BlockNo;
47     } MBBLabel;
48   };
49 public:
50   
51   MCOperand() : Kind(kInvalid) {}
52   MCOperand(const MCOperand &RHS) { *this = RHS; }
53
54   bool isValid() const { return Kind != kInvalid; }
55   bool isReg() const { return Kind == kRegister; }
56   bool isImm() const { return Kind == kImmediate; }
57   bool isMBBLabel() const { return Kind == kMBBLabel; }
58   bool isExpr() const { return Kind == kExpr; }
59   
60   /// getReg - Returns the register number.
61   unsigned getReg() const {
62     assert(isReg() && "This is not a register operand!");
63     return RegVal;
64   }
65
66   /// setReg - Set the register number.
67   void setReg(unsigned Reg) {
68     assert(isReg() && "This is not a register operand!");
69     RegVal = Reg;
70   }
71   
72   int64_t getImm() const {
73     assert(isImm() && "This is not an immediate");
74     return ImmVal;
75   }
76   void setImm(int64_t Val) {
77     assert(isImm() && "This is not an immediate");
78     ImmVal = Val;
79   }
80   
81   unsigned getMBBLabelFunction() const {
82     assert(isMBBLabel() && "This is not a machine basic block");
83     return MBBLabel.FunctionNo; 
84   }
85   unsigned getMBBLabelBlock() const {
86     assert(isMBBLabel() && "This is not a machine basic block");
87     return MBBLabel.BlockNo; 
88   }
89
90   const MCExpr *getExpr() const {
91     assert(isExpr() && "This is not an expression");
92     return ExprVal;
93   }
94   void setExpr(const MCExpr *Val) {
95     assert(isExpr() && "This is not an expression");
96     ExprVal = Val;
97   }
98   
99   static MCOperand CreateReg(unsigned Reg) {
100     MCOperand Op;
101     Op.Kind = kRegister;
102     Op.RegVal = Reg;
103     return Op;
104   }
105   static MCOperand CreateImm(int64_t Val) {
106     MCOperand Op;
107     Op.Kind = kImmediate;
108     Op.ImmVal = Val;
109     return Op;
110   }
111   static MCOperand CreateMBBLabel(unsigned Fn, unsigned MBB) {
112     MCOperand Op;
113     Op.Kind = kMBBLabel;
114     Op.MBBLabel.FunctionNo = Fn;
115     Op.MBBLabel.BlockNo = MBB;
116     return Op;
117   }
118   static MCOperand CreateExpr(const MCExpr *Val) {
119     MCOperand Op;
120     Op.Kind = kExpr;
121     Op.ExprVal = Val;
122     return Op;
123   }
124
125   void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
126   void dump() const;
127 };
128
129   
130 /// MCInst - Instances of this class represent a single low-level machine
131 /// instruction. 
132 class MCInst {
133   unsigned Opcode;
134   SmallVector<MCOperand, 8> Operands;
135   
136   // FIXME: This is a hack!
137   DebugLoc Loc;
138 public:
139   MCInst() : Opcode(~0U) {}
140   
141   void setOpcode(unsigned Op) { Opcode = Op; }
142   
143   unsigned getOpcode() const { return Opcode; }
144
145   void setDebugLoc(DebugLoc L) { Loc = L; }
146   DebugLoc getDebugLoc() const { return Loc; }
147   
148   const MCOperand &getOperand(unsigned i) const { return Operands[i]; }
149   MCOperand &getOperand(unsigned i) { return Operands[i]; }
150   unsigned getNumOperands() const { return Operands.size(); }
151   
152   void addOperand(const MCOperand &Op) {
153     Operands.push_back(Op);
154   }
155
156   void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
157   void dump() const;
158 };
159
160
161 } // end namespace llvm
162
163 #endif