Add support for the R_ARM_TARGET1 relocation, which should be given to relocations...
[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/ADT/StringRef.h"
21 #include "llvm/Support/DataTypes.h"
22
23 namespace llvm {
24 class raw_ostream;
25 class MCAsmInfo;
26 class MCInstPrinter;
27 class MCExpr;
28 class MCInst;
29
30 /// MCOperand - Instances of this class represent operands of the MCInst class.
31 /// This is a simple discriminated union.
32 class MCOperand {
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.
40   };
41   unsigned char Kind;
42
43   union {
44     unsigned RegVal;
45     int64_t ImmVal;
46     double FPImmVal;
47     const MCExpr *ExprVal;
48     const MCInst *InstVal;
49   };
50 public:
51
52   MCOperand() : Kind(kInvalid), FPImmVal(0.0) {}
53
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; }
60
61   /// getReg - Returns the register number.
62   unsigned getReg() const {
63     assert(isReg() && "This is not a register operand!");
64     return RegVal;
65   }
66
67   /// setReg - Set the register number.
68   void setReg(unsigned Reg) {
69     assert(isReg() && "This is not a register operand!");
70     RegVal = Reg;
71   }
72
73   int64_t getImm() const {
74     assert(isImm() && "This is not an immediate");
75     return ImmVal;
76   }
77   void setImm(int64_t Val) {
78     assert(isImm() && "This is not an immediate");
79     ImmVal = Val;
80   }
81
82   double getFPImm() const {
83     assert(isFPImm() && "This is not an FP immediate");
84     return FPImmVal;
85   }
86
87   void setFPImm(double Val) {
88     assert(isFPImm() && "This is not an FP immediate");
89     FPImmVal = Val;
90   }
91
92   const MCExpr *getExpr() const {
93     assert(isExpr() && "This is not an expression");
94     return ExprVal;
95   }
96   void setExpr(const MCExpr *Val) {
97     assert(isExpr() && "This is not an expression");
98     ExprVal = Val;
99   }
100
101   const MCInst *getInst() const {
102     assert(isInst() && "This is not a sub-instruction");
103     return InstVal;
104   }
105   void setInst(const MCInst *Val) {
106     assert(isInst() && "This is not a sub-instruction");
107     InstVal = Val;
108   }
109
110   static MCOperand CreateReg(unsigned Reg) {
111     MCOperand Op;
112     Op.Kind = kRegister;
113     Op.RegVal = Reg;
114     return Op;
115   }
116   static MCOperand CreateImm(int64_t Val) {
117     MCOperand Op;
118     Op.Kind = kImmediate;
119     Op.ImmVal = Val;
120     return Op;
121   }
122   static MCOperand CreateFPImm(double Val) {
123     MCOperand Op;
124     Op.Kind = kFPImmediate;
125     Op.FPImmVal = Val;
126     return Op;
127   }
128   static MCOperand CreateExpr(const MCExpr *Val) {
129     MCOperand Op;
130     Op.Kind = kExpr;
131     Op.ExprVal = Val;
132     return Op;
133   }
134   static MCOperand CreateInst(const MCInst *Val) {
135     MCOperand Op;
136     Op.Kind = kInst;
137     Op.InstVal = Val;
138     return Op;
139   }
140
141   void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
142   void dump() const;
143 };
144
145 template <> struct isPodLike<MCOperand> { static const bool value = true; };
146
147 /// MCInst - Instances of this class represent a single low-level machine
148 /// instruction.
149 class MCInst {
150   unsigned Opcode;
151   SmallVector<MCOperand, 8> Operands;
152 public:
153   MCInst() : Opcode(0) {}
154
155   void setOpcode(unsigned Op) { Opcode = Op; }
156
157   unsigned getOpcode() const { return Opcode; }
158
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(); }
162
163   void addOperand(const MCOperand &Op) {
164     Operands.push_back(Op);
165   }
166
167   void clear() { Operands.clear(); }
168   size_t size() { return Operands.size(); }
169
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);
175   }
176
177   void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
178   void dump() const;
179
180   /// \brief Dump the MCInst as prettily as possible using the additional MC
181   /// structures, if given. Operators are separated by the \arg Separator
182   /// string.
183   void dump_pretty(raw_ostream &OS, const MCAsmInfo *MAI = 0,
184                    const MCInstPrinter *Printer = 0,
185                    StringRef Separator = " ") const;
186 };
187
188 inline raw_ostream& operator<<(raw_ostream &OS, const MCOperand &MO) {
189   MO.print(OS, 0);
190   return OS;
191 }
192
193 inline raw_ostream& operator<<(raw_ostream &OS, const MCInst &MI) {
194   MI.print(OS, 0);
195   return OS;
196 }
197
198 } // end namespace llvm
199
200 #endif