03dafc1112d3342266e0e87f3ff44814eabb825b
[oota-llvm.git] / include / llvm / CodeGen / MachineRelocation.h
1 //===-- llvm/CodeGen/MachineRelocation.h - Target Relocation ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the MachineRelocation class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_CODEGEN_MACHINERELOCATION_H
15 #define LLVM_CODEGEN_MACHINERELOCATION_H
16
17 #include "llvm/Support/DataTypes.h"
18 #include <cassert>
19
20 namespace llvm {
21 class GlobalValue;
22 class MachineBasicBlock;
23
24 /// MachineRelocation - This represents a target-specific relocation value,
25 /// produced by the code emitter.  This relocation is resolved after the has
26 /// been emitted, either to an object file or to memory, when the target of the
27 /// relocation can be resolved.
28 ///
29 /// A relocation is made up of the following logical portions:
30 ///   1. An offset in the machine code buffer, the location to modify.
31 ///   2. A target specific relocation type (a number from 0 to 63).
32 ///   3. A symbol being referenced, either as a GlobalValue* or as a string.
33 ///   4. An optional constant value to be added to the reference.
34 ///   5. A bit, CanRewrite, which indicates to the JIT that a function stub is
35 ///      not needed for the relocation.
36 ///   6. An index into the GOT, if the target uses a GOT
37 ///
38 class MachineRelocation {
39   enum AddressType {
40     isResult,         // Relocation has be transformed into its result pointer.
41     isGV,             // The Target.GV field is valid.
42     isBB,             // Relocation of BB address.
43     isExtSym,         // The Target.ExtSym field is valid.
44     isConstPool,      // Relocation of constant pool address.
45     isJumpTable,      // Relocation of jump table address.
46     isGOTIndex        // The Target.GOTIndex field is valid.
47   };
48   
49   /// Offset - This is the offset from the start of the code buffer of the
50   /// relocation to perform.
51   intptr_t Offset;
52   
53   /// ConstantVal - A field that may be used by the target relocation type.
54   intptr_t ConstantVal;
55
56   union {
57     void *Result;           // If this has been resolved to a resolved pointer
58     GlobalValue *GV;        // If this is a pointer to an LLVM global
59     MachineBasicBlock *MBB; // If this is a pointer to a LLVM BB
60     const char *ExtSym;     // If this is a pointer to a named symbol
61     unsigned Index;         // Constant pool / jump table index
62     unsigned GOTIndex;      // Index in the GOT of this symbol/global
63   } Target;
64
65   unsigned TargetReloType : 6; // The target relocation ID.
66   AddressType AddrType    : 3; // The field of Target to use.
67   bool DoesntNeedFnStub   : 1; // True if we don't need a fn stub.
68   bool GOTRelative        : 1; // Should this relocation be relative to the GOT?
69
70 public:
71   /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
72   ///
73   static MachineRelocation getGV(intptr_t offset, unsigned RelocationType, 
74                                  GlobalValue *GV, intptr_t cst = 0,
75                                  bool DoesntNeedFunctionStub = 0,
76                                  bool GOTrelative = 0) {
77     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
78     MachineRelocation Result;
79     Result.Offset = offset;
80     Result.ConstantVal = cst;
81     Result.TargetReloType = RelocationType;
82     Result.AddrType = isGV;
83     Result.DoesntNeedFnStub = DoesntNeedFunctionStub;
84     Result.GOTRelative = GOTrelative;
85     Result.Target.GV = GV;
86     return Result;
87   }
88
89   /// MachineRelocation::getBB - Return a relocation entry for a BB.
90   ///
91   static MachineRelocation getBB(intptr_t offset,unsigned RelocationType,
92                                  MachineBasicBlock *MBB, intptr_t cst = 0) {
93     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
94     MachineRelocation Result;
95     Result.Offset = offset;
96     Result.ConstantVal = cst;
97     Result.TargetReloType = RelocationType;
98     Result.AddrType = isBB;
99     Result.DoesntNeedFnStub = false;
100     Result.GOTRelative = false;
101     Result.Target.MBB = MBB;
102     return Result;
103   }
104
105   /// MachineRelocation::getExtSym - Return a relocation entry for an external
106   /// symbol, like "free".
107   ///
108   static MachineRelocation getExtSym(intptr_t offset, unsigned RelocationType, 
109                                      const char *ES, intptr_t cst = 0,
110                                      bool GOTrelative = 0) {
111     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
112     MachineRelocation Result;
113     Result.Offset = offset;
114     Result.ConstantVal = cst;
115     Result.TargetReloType = RelocationType;
116     Result.AddrType = isExtSym;
117     Result.DoesntNeedFnStub = false;
118     Result.GOTRelative = GOTrelative;
119     Result.Target.ExtSym = ES;
120     return Result;
121   }
122
123   /// MachineRelocation::getConstPool - Return a relocation entry for a constant
124   /// pool entry.
125   ///
126   static MachineRelocation getConstPool(intptr_t offset,unsigned RelocationType,
127                                         unsigned CPI, intptr_t cst = 0) {
128     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
129     MachineRelocation Result;
130     Result.Offset = offset;
131     Result.ConstantVal = cst;
132     Result.TargetReloType = RelocationType;
133     Result.AddrType = isConstPool;
134     Result.DoesntNeedFnStub = false;
135     Result.GOTRelative = false;
136     Result.Target.Index = CPI;
137     return Result;
138   }
139
140   /// MachineRelocation::getJumpTable - Return a relocation entry for a jump
141   /// table entry.
142   ///
143   static MachineRelocation getJumpTable(intptr_t offset,unsigned RelocationType,
144                                         unsigned JTI, intptr_t cst = 0) {
145     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
146     MachineRelocation Result;
147     Result.Offset = offset;
148     Result.ConstantVal = cst;
149     Result.TargetReloType = RelocationType;
150     Result.AddrType = isJumpTable;
151     Result.DoesntNeedFnStub = false;
152     Result.GOTRelative = false;
153     Result.Target.Index = JTI;
154     return Result;
155   }
156
157   /// getMachineCodeOffset - Return the offset into the code buffer that the
158   /// relocation should be performed.
159   intptr_t getMachineCodeOffset() const {
160     return Offset;
161   }
162
163   /// getRelocationType - Return the target-specific relocation ID for this
164   /// relocation.
165   unsigned getRelocationType() const {
166     return TargetReloType;
167   }
168
169   /// getConstantVal - Get the constant value associated with this relocation.
170   /// This is often an offset from the symbol.
171   ///
172   intptr_t getConstantVal() const {
173     return ConstantVal;
174   }
175
176   /// isGlobalValue - Return true if this relocation is a GlobalValue, as
177   /// opposed to a constant string.
178   bool isGlobalValue() const {
179     return AddrType == isGV;
180   }
181
182   /// isBasicBlock - Return true if this relocation is a basic block reference.
183   ///
184   bool isBasicBlock() const {
185     return AddrType == isBB;
186   }
187
188   /// isString - Return true if this is a constant string.
189   ///
190   bool isString() const {
191     return AddrType == isExtSym;
192   }
193
194   /// isConstantPoolIndex - Return true if this is a constant pool reference.
195   ///
196   bool isConstantPoolIndex() const {
197     return AddrType == isConstPool;
198   }
199
200   /// isJumpTableIndex - Return true if this is a jump table reference.
201   ///
202   bool isJumpTableIndex() const {
203     return AddrType == isJumpTable;
204   }
205
206   /// isGOTRelative - Return true the target wants the index into the GOT of
207   /// the symbol rather than the address of the symbol.
208   bool isGOTRelative() const {
209     return GOTRelative;
210   }
211
212   /// doesntNeedFunctionStub - This function returns true if the JIT for this
213   /// target is capable of directly handling the relocated instruction without
214   /// using a stub function.  It is always conservatively correct for this flag
215   /// to be false, but targets can improve their compilation callback functions
216   /// to handle more general cases if they want improved performance.
217   bool doesntNeedFunctionStub() const {
218     return DoesntNeedFnStub;
219   }
220
221   /// getGlobalValue - If this is a global value reference, return the
222   /// referenced global.
223   GlobalValue *getGlobalValue() const {
224     assert(isGlobalValue() && "This is not a global value reference!");
225     return Target.GV;
226   }
227
228   MachineBasicBlock *getBasicBlock() const {
229     assert(isBasicBlock() && "This is not a basic block reference!");
230     return Target.MBB;
231   }
232
233   /// getString - If this is a string value, return the string reference.
234   ///
235   const char *getString() const {
236     assert(isString() && "This is not a string reference!");
237     return Target.ExtSym;
238   }
239
240   /// getConstantPoolIndex - If this is a const pool reference, return
241   /// the index into the constant pool.
242   unsigned getConstantPoolIndex() const {
243     assert(isConstantPoolIndex() && "This is not a constant pool reference!");
244     return Target.Index;
245   }
246
247   /// getJumpTableIndex - If this is a jump table reference, return
248   /// the index into the jump table.
249   unsigned getJumpTableIndex() const {
250     assert(isJumpTableIndex() && "This is not a jump table reference!");
251     return Target.Index;
252   }
253
254   /// getResultPointer - Once this has been resolved to point to an actual
255   /// address, this returns the pointer.
256   void *getResultPointer() const {
257     assert(AddrType == isResult && "Result pointer isn't set yet!");
258     return Target.Result;
259   }
260
261   /// setResultPointer - Set the result to the specified pointer value.
262   ///
263   void setResultPointer(void *Ptr) {
264     Target.Result = Ptr;
265     AddrType = isResult;
266   }
267
268   /// setGOTIndex - Set the GOT index to a specific value.
269   void setGOTIndex(unsigned idx) {
270     AddrType = isGOTIndex;
271     Target.GOTIndex = idx;
272   }
273
274   /// getGOTIndex - Once this has been resolved to an entry in the GOT,
275   /// this returns that index.  The index is from the lowest address entry
276   /// in the GOT.
277   unsigned getGOTIndex() const {
278     assert(AddrType == isGOTIndex);
279     return Target.GOTIndex;
280   }
281 };
282 }
283
284 #endif