Don't attribute in file headers anymore. See llvmdev for the
[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 is distributed under the University of Illinois Open Source
6 // 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    : 4; // 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  // Relocation types used in a generic implementation.  Currently, relocation
72  // entries for all things use the generic VANILLA type until they are refined
73  // into target relocation types.
74   enum RelocationType {
75     VANILLA
76   };
77   
78   /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
79   ///
80   static MachineRelocation getGV(intptr_t offset, unsigned RelocationType, 
81                                  GlobalValue *GV, intptr_t cst = 0,
82                                  bool DoesntNeedFunctionStub = 0,
83                                  bool GOTrelative = 0) {
84     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
85     MachineRelocation Result;
86     Result.Offset = offset;
87     Result.ConstantVal = cst;
88     Result.TargetReloType = RelocationType;
89     Result.AddrType = isGV;
90     Result.DoesntNeedFnStub = DoesntNeedFunctionStub;
91     Result.GOTRelative = GOTrelative;
92     Result.Target.GV = GV;
93     return Result;
94   }
95
96   /// MachineRelocation::getBB - Return a relocation entry for a BB.
97   ///
98   static MachineRelocation getBB(intptr_t offset,unsigned RelocationType,
99                                  MachineBasicBlock *MBB, intptr_t cst = 0) {
100     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
101     MachineRelocation Result;
102     Result.Offset = offset;
103     Result.ConstantVal = cst;
104     Result.TargetReloType = RelocationType;
105     Result.AddrType = isBB;
106     Result.DoesntNeedFnStub = false;
107     Result.GOTRelative = false;
108     Result.Target.MBB = MBB;
109     return Result;
110   }
111
112   /// MachineRelocation::getExtSym - Return a relocation entry for an external
113   /// symbol, like "free".
114   ///
115   static MachineRelocation getExtSym(intptr_t offset, unsigned RelocationType, 
116                                      const char *ES, intptr_t cst = 0,
117                                      bool GOTrelative = 0) {
118     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
119     MachineRelocation Result;
120     Result.Offset = offset;
121     Result.ConstantVal = cst;
122     Result.TargetReloType = RelocationType;
123     Result.AddrType = isExtSym;
124     Result.DoesntNeedFnStub = false;
125     Result.GOTRelative = GOTrelative;
126     Result.Target.ExtSym = ES;
127     return Result;
128   }
129
130   /// MachineRelocation::getConstPool - Return a relocation entry for a constant
131   /// pool entry.
132   ///
133   static MachineRelocation getConstPool(intptr_t offset,unsigned RelocationType,
134                                         unsigned CPI, intptr_t cst = 0) {
135     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
136     MachineRelocation Result;
137     Result.Offset = offset;
138     Result.ConstantVal = cst;
139     Result.TargetReloType = RelocationType;
140     Result.AddrType = isConstPool;
141     Result.DoesntNeedFnStub = false;
142     Result.GOTRelative = false;
143     Result.Target.Index = CPI;
144     return Result;
145   }
146
147   /// MachineRelocation::getJumpTable - Return a relocation entry for a jump
148   /// table entry.
149   ///
150   static MachineRelocation getJumpTable(intptr_t offset,unsigned RelocationType,
151                                         unsigned JTI, intptr_t cst = 0) {
152     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
153     MachineRelocation Result;
154     Result.Offset = offset;
155     Result.ConstantVal = cst;
156     Result.TargetReloType = RelocationType;
157     Result.AddrType = isJumpTable;
158     Result.DoesntNeedFnStub = false;
159     Result.GOTRelative = false;
160     Result.Target.Index = JTI;
161     return Result;
162   }
163
164   /// getMachineCodeOffset - Return the offset into the code buffer that the
165   /// relocation should be performed.
166   intptr_t getMachineCodeOffset() const {
167     return Offset;
168   }
169
170   /// getRelocationType - Return the target-specific relocation ID for this
171   /// relocation.
172   unsigned getRelocationType() const {
173     return TargetReloType;
174   }
175
176   /// getConstantVal - Get the constant value associated with this relocation.
177   /// This is often an offset from the symbol.
178   ///
179   intptr_t getConstantVal() const {
180     return ConstantVal;
181   }
182
183   /// setConstantVal - Set the constant value associated with this relocation.
184   /// This is often an offset from the symbol.
185   ///
186   void setConstantVal(intptr_t val) {
187     ConstantVal = val;
188   }
189
190   /// isGlobalValue - Return true if this relocation is a GlobalValue, as
191   /// opposed to a constant string.
192   bool isGlobalValue() const {
193     return AddrType == isGV;
194   }
195
196   /// isBasicBlock - Return true if this relocation is a basic block reference.
197   ///
198   bool isBasicBlock() const {
199     return AddrType == isBB;
200   }
201
202   /// isString - Return true if this is a constant string.
203   ///
204   bool isString() const {
205     return AddrType == isExtSym;
206   }
207
208   /// isConstantPoolIndex - Return true if this is a constant pool reference.
209   ///
210   bool isConstantPoolIndex() const {
211     return AddrType == isConstPool;
212   }
213
214   /// isJumpTableIndex - Return true if this is a jump table reference.
215   ///
216   bool isJumpTableIndex() const {
217     return AddrType == isJumpTable;
218   }
219
220   /// isGOTRelative - Return true the target wants the index into the GOT of
221   /// the symbol rather than the address of the symbol.
222   bool isGOTRelative() const {
223     return GOTRelative;
224   }
225
226   /// doesntNeedFunctionStub - This function returns true if the JIT for this
227   /// target is capable of directly handling the relocated instruction without
228   /// using a stub function.  It is always conservatively correct for this flag
229   /// to be false, but targets can improve their compilation callback functions
230   /// to handle more general cases if they want improved performance.
231   bool doesntNeedFunctionStub() const {
232     return DoesntNeedFnStub;
233   }
234
235   /// getGlobalValue - If this is a global value reference, return the
236   /// referenced global.
237   GlobalValue *getGlobalValue() const {
238     assert(isGlobalValue() && "This is not a global value reference!");
239     return Target.GV;
240   }
241
242   MachineBasicBlock *getBasicBlock() const {
243     assert(isBasicBlock() && "This is not a basic block reference!");
244     return Target.MBB;
245   }
246
247   /// getString - If this is a string value, return the string reference.
248   ///
249   const char *getString() const {
250     assert(isString() && "This is not a string reference!");
251     return Target.ExtSym;
252   }
253
254   /// getConstantPoolIndex - If this is a const pool reference, return
255   /// the index into the constant pool.
256   unsigned getConstantPoolIndex() const {
257     assert(isConstantPoolIndex() && "This is not a constant pool reference!");
258     return Target.Index;
259   }
260
261   /// getJumpTableIndex - If this is a jump table reference, return
262   /// the index into the jump table.
263   unsigned getJumpTableIndex() const {
264     assert(isJumpTableIndex() && "This is not a jump table reference!");
265     return Target.Index;
266   }
267
268   /// getResultPointer - Once this has been resolved to point to an actual
269   /// address, this returns the pointer.
270   void *getResultPointer() const {
271     assert(AddrType == isResult && "Result pointer isn't set yet!");
272     return Target.Result;
273   }
274
275   /// setResultPointer - Set the result to the specified pointer value.
276   ///
277   void setResultPointer(void *Ptr) {
278     Target.Result = Ptr;
279     AddrType = isResult;
280   }
281
282   /// setGOTIndex - Set the GOT index to a specific value.
283   void setGOTIndex(unsigned idx) {
284     AddrType = isGOTIndex;
285     Target.GOTIndex = idx;
286   }
287
288   /// getGOTIndex - Once this has been resolved to an entry in the GOT,
289   /// this returns that index.  The index is from the lowest address entry
290   /// in the GOT.
291   unsigned getGOTIndex() const {
292     assert(AddrType == isGOTIndex);
293     return Target.GOTIndex;
294   }
295 };
296 }
297
298 #endif