llvm-mc: Diagnose misuse (mix) of defined symbols and labels.
[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
26 /// MCOperand - Instances of this class represent operands of the MCInst class.
27 /// This is a simple discriminated union.
28 class MCOperand {
29   enum MachineOperandType {
30     kInvalid,                 ///< Uninitialized.
31     kRegister,                ///< Register operand.
32     kImmediate,               ///< Immediate operand.
33     kMBBLabel,                ///< Basic block label.
34     kMCValue
35   };
36   unsigned char Kind;
37   
38   union {
39     unsigned RegVal;
40     int64_t ImmVal;
41     MCValue MCValueVal;
42     struct {
43       unsigned FunctionNo;
44       unsigned BlockNo;
45     } MBBLabel;
46   };
47 public:
48   
49   MCOperand() : Kind(kInvalid) {}
50   MCOperand(const MCOperand &RHS) { *this = RHS; }
51
52   bool isReg() const { return Kind == kRegister; }
53   bool isImm() const { return Kind == kImmediate; }
54   bool isMBBLabel() const { return Kind == kMBBLabel; }
55   
56   /// getReg - Returns the register number.
57   unsigned getReg() const {
58     assert(isReg() && "This is not a register operand!");
59     return RegVal;
60   }
61
62   /// setReg - Set the register number.
63   void setReg(unsigned Reg) {
64     assert(isReg() && "This is not a register operand!");
65     RegVal = Reg;
66   }
67   
68   int64_t getImm() const {
69     assert(isImm() && "This is not an immediate");
70     return ImmVal;
71   }
72   void setImm(int64_t Val) {
73     assert(isImm() && "This is not an immediate");
74     ImmVal = Val;
75   }
76   
77   unsigned getMBBLabelFunction() const {
78     assert(isMBBLabel() && "Wrong accessor");
79     return MBBLabel.FunctionNo; 
80   }
81   unsigned getMBBLabelBlock() const {
82     assert(isMBBLabel() && "Wrong accessor");
83     return MBBLabel.BlockNo; 
84   }
85   
86   void MakeReg(unsigned Reg) {
87     Kind = kRegister;
88     RegVal = Reg;
89   }
90   void MakeImm(int64_t Val) {
91     Kind = kImmediate;
92     ImmVal = Val;
93   }
94   void MakeMBBLabel(unsigned Fn, unsigned MBB) {
95     Kind = kMBBLabel;
96     MBBLabel.FunctionNo = Fn;
97     MBBLabel.BlockNo = MBB;
98   }
99 };
100
101   
102 /// MCInst - Instances of this class represent a single low-level machine
103 /// instruction. 
104 class MCInst {
105   unsigned Opcode;
106   SmallVector<MCOperand, 8> Operands;
107 public:
108   MCInst() : Opcode(~0U) {}
109   
110   void setOpcode(unsigned Op) { Opcode = Op; }
111   
112   unsigned getOpcode() const { return Opcode; }
113   DebugLoc getDebugLoc() const { return DebugLoc(); }
114   
115   const MCOperand &getOperand(unsigned i) const { return Operands[i]; }
116   MCOperand &getOperand(unsigned i) { return Operands[i]; }
117   unsigned getNumOperands() const { return Operands.size(); }
118   
119   void addOperand(const MCOperand &Op) {
120     Operands.push_back(Op);
121   }
122   
123 };
124
125
126 } // end namespace llvm
127
128 #endif