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