c43fcb64e0c3cbe74da40b21a0892f2b3e9d6497
[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
23 /// MachineRelocation - This represents a target-specific relocation value,
24 /// produced by the code emitter.  This relocation is resolved after the has
25 /// been emitted, either to an object file or to memory, when the target of the
26 /// relocation can be resolved.
27 ///
28 /// A relocation is made up of the following logical portions:
29 ///   1. An offset in the machine code buffer, the location to modify.
30 ///   2. A target specific relocation type (a number from 0 to 63).
31 ///   3. A symbol being referenced, either as a GlobalValue* or as a string.
32 ///   4. An optional constant value to be added to the reference.
33 ///   5. A bit, CanRewrite, which indicates to the JIT that a function stub is
34 ///      not needed for the relocation.
35 ///   6. An index into the GOT, if the target uses a GOT
36 ///
37 class MachineRelocation {
38   enum AddressType {
39     isResult,         // Relocation has be transformed into its result pointer.
40     isGV,             // The Target.GV field is valid.
41     isExtSym,         // The Target.ExtSym field is valid.
42     isConstPool,      // The Target.ConstPool field is valid.
43     isGOTIndex        // The Target.GOTIndex field is valid.
44   };
45   
46   /// Offset - This is the offset from the start of the code buffer of the
47   /// relocation to perform.
48   unsigned Offset;
49   
50   /// ConstantVal - A field that may be used by the target relocation type.
51   intptr_t ConstantVal;
52
53   union {
54     void *Result;        // If this has been resolved to a resolved pointer
55     GlobalValue *GV;     // If this is a pointer to an LLVM global
56     const char *ExtSym;  // If this is a pointer to a named symbol
57     unsigned ConstPool;  // In this is a pointer to a constant pool entry
58     unsigned GOTIndex;   // Index in the GOT of this symbol/global
59   } Target;
60
61   unsigned TargetReloType : 6; // The target relocation ID.
62   AddressType AddrType    : 3; // The field of Target to use.
63   bool DoesntNeedFnStub   : 1; // True if we don't need a fn stub.
64   bool GOTRelative        : 1; // Should this relocation be relative to the GOT?
65
66 public:
67   MachineRelocation(unsigned offset, unsigned RelocationType, GlobalValue *GV,
68                     intptr_t cst = 0, bool DoesntNeedFunctionStub = 0,
69                     bool GOTrelative = 0)
70     : Offset(offset), ConstantVal(cst), TargetReloType(RelocationType),
71       AddrType(isGV), DoesntNeedFnStub(DoesntNeedFunctionStub),
72       GOTRelative(GOTrelative){
73     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
74     Target.GV = GV;
75   }
76
77   MachineRelocation(unsigned offset, unsigned RelocationType, const char *ES,
78                     intptr_t cst = 0, bool GOTrelative = 0)
79     : Offset(offset), ConstantVal(cst), TargetReloType(RelocationType),
80       AddrType(isExtSym), DoesntNeedFnStub(false), GOTRelative(GOTrelative) {
81     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
82     Target.ExtSym = ES;
83   }
84
85   MachineRelocation(unsigned offset, unsigned RelocationType, unsigned CPI,
86                     intptr_t cst = 0)
87     : Offset(offset), ConstantVal(cst), TargetReloType(RelocationType),
88       AddrType(isConstPool), DoesntNeedFnStub(false), GOTRelative(0) {
89     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
90     Target.ConstPool = CPI;
91   }
92
93   /// getMachineCodeOffset - Return the offset into the code buffer that the
94   /// relocation should be performed.
95   unsigned getMachineCodeOffset() const {
96     return Offset;
97   }
98
99   /// getRelocationType - Return the target-specific relocation ID for this
100   /// relocation.
101   unsigned getRelocationType() const {
102     return TargetReloType;
103   }
104
105   /// getConstantVal - Get the constant value associated with this relocation.
106   /// This is often an offset from the symbol.
107   ///
108   intptr_t getConstantVal() const {
109     return ConstantVal;
110   }
111
112   /// isGlobalValue - Return true if this relocation is a GlobalValue, as
113   /// opposed to a constant string.
114   bool isGlobalValue() const {
115     return AddrType == isGV;
116   }
117
118   /// isString - Return true if this is a constant string.
119   ///
120   bool isString() const {
121     return AddrType == isExtSym;
122   }
123
124   /// isConstantPoolIndex - Return true if this is a constant pool reference.
125   ///
126   bool isConstantPoolIndex() const {
127     return AddrType == isConstPool;
128   }
129
130   /// isGOTRelative - Return true the target wants the index into the GOT of
131   /// the symbol rather than the address of the symbol.
132   bool isGOTRelative() const {
133     return GOTRelative;
134   }
135
136   /// doesntNeedFunctionStub - This function returns true if the JIT for this
137   /// target is capable of directly handling the relocated instruction without
138   /// using a stub function.  It is always conservatively correct for this flag
139   /// to be false, but targets can improve their compilation callback functions
140   /// to handle more general cases if they want improved performance.
141   bool doesntNeedFunctionStub() const {
142     return DoesntNeedFnStub;
143   }
144
145   /// getGlobalValue - If this is a global value reference, return the
146   /// referenced global.
147   GlobalValue *getGlobalValue() const {
148     assert(isGlobalValue() && "This is not a global value reference!");
149     return Target.GV;
150   }
151
152   /// getString - If this is a string value, return the string reference.
153   ///
154   const char *getString() const {
155     assert(isString() && "This is not a string reference!");
156     return Target.ExtSym;
157   }
158
159   /// getConstantPoolIndex - If this is a const pool reference, return
160   /// the index into the constant pool.
161   unsigned getConstantPoolIndex() const {
162     assert(isConstantPoolIndex() && "This is not a constant pool reference!");
163     return Target.ConstPool;
164   }
165
166   /// getResultPointer - Once this has been resolved to point to an actual
167   /// address, this returns the pointer.
168   void *getResultPointer() const {
169     assert(AddrType == isResult && "Result pointer isn't set yet!");
170     return Target.Result;
171   }
172
173   /// setResultPointer - Set the result to the specified pointer value.
174   ///
175   void setResultPointer(void *Ptr) {
176     Target.Result = Ptr;
177     AddrType = isResult;
178   }
179
180   /// setGOTIndex - Set the GOT index to a specific value.
181   void setGOTIndex(unsigned idx) {
182     AddrType = isGOTIndex;
183     Target.GOTIndex = idx;
184   }
185
186   /// getGOTIndex - Once this has been resolved to an entry in the GOT,
187   /// this returns that index.  The index is from the lowest address entry
188   /// in the GOT.
189   unsigned getGOTIndex() const {
190     assert(AddrType == isGOTIndex);
191     return Target.GOTIndex;
192   }
193 };
194 }
195
196 #endif