1 //===-- llvm/CodeGen/MachineRelocation.h - Target Relocation ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MachineRelocation class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINERELOCATION_H
15 #define LLVM_CODEGEN_MACHINERELOCATION_H
17 #include "llvm/Support/DataTypes.h"
22 class MachineBasicBlock;
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.
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
38 class MachineRelocation {
40 isResult, // Relocation has be transformed into its result pointer.
41 isGV, // The Target.GV field is valid.
42 isGVNonLazyPtr, // Relocation of a Mac OS X NonLazy indirect reference.
43 isBB, // Relocation of BB address.
44 isExtSym, // The Target.ExtSym field is valid.
45 isConstPool, // Relocation of constant pool address.
46 isJumpTable, // Relocation of jump table address.
47 isGOTIndex // The Target.GOTIndex field is valid.
50 /// Offset - This is the offset from the start of the code buffer of the
51 /// relocation to perform.
54 /// ConstantVal - A field that may be used by the target relocation type.
58 void *Result; // If this has been resolved to a resolved pointer
59 GlobalValue *GV; // If this is a pointer to a GV or a GV nonlazy ptr
60 MachineBasicBlock *MBB; // If this is a pointer to a LLVM BB
61 const char *ExtSym; // If this is a pointer to a named symbol
62 unsigned Index; // Constant pool / jump table index
63 unsigned GOTIndex; // Index in the GOT of this symbol/global
66 unsigned TargetReloType : 6; // The target relocation ID
67 AddressType AddrType : 4; // The field of Target to use
68 bool NeedStub : 1; // True if this relocation requires a stub
69 bool GOTRelative : 1; // Should this relocation be relative to the GOT?
70 bool TargetResolve : 1; // True if target should resolve the address
73 // Relocation types used in a generic implementation. Currently, relocation
74 // entries for all things use the generic VANILLA type until they are refined
75 // into target relocation types.
80 /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
82 static MachineRelocation getGV(intptr_t offset, unsigned RelocationType,
83 GlobalValue *GV, intptr_t cst = 0,
85 bool GOTrelative = 0) {
86 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
87 MachineRelocation Result;
88 Result.Offset = offset;
89 Result.ConstantVal = cst;
90 Result.TargetReloType = RelocationType;
91 Result.AddrType = isGV;
92 Result.NeedStub = NeedStub;
93 Result.GOTRelative = GOTrelative;
94 Result.TargetResolve = false;
95 Result.Target.GV = GV;
99 /// MachineRelocation::getGVNonLazyPtr - Return a relocation entry for a
100 /// Mac OS X non-lazy GlobalValue indirect reference.
101 static MachineRelocation getGVNonLazyPtr(intptr_t offset,
102 unsigned RelocationType,
103 GlobalValue *GV, intptr_t cst = 0,
105 bool GOTrelative = 0) {
106 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
107 MachineRelocation Result;
108 Result.Offset = offset;
109 Result.ConstantVal = cst;
110 Result.TargetReloType = RelocationType;
111 Result.AddrType = isGVNonLazyPtr;
112 Result.NeedStub = NeedStub;
113 Result.GOTRelative = GOTrelative;
114 Result.TargetResolve = false;
115 Result.Target.GV = GV;
119 /// MachineRelocation::getBB - Return a relocation entry for a BB.
121 static MachineRelocation getBB(intptr_t offset,unsigned RelocationType,
122 MachineBasicBlock *MBB, intptr_t cst = 0) {
123 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
124 MachineRelocation Result;
125 Result.Offset = offset;
126 Result.ConstantVal = cst;
127 Result.TargetReloType = RelocationType;
128 Result.AddrType = isBB;
129 Result.NeedStub = false;
130 Result.GOTRelative = false;
131 Result.TargetResolve = false;
132 Result.Target.MBB = MBB;
136 /// MachineRelocation::getExtSym - Return a relocation entry for an external
137 /// symbol, like "free".
139 static MachineRelocation getExtSym(intptr_t offset, unsigned RelocationType,
140 const char *ES, intptr_t cst = 0,
141 bool GOTrelative = 0) {
142 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
143 MachineRelocation Result;
144 Result.Offset = offset;
145 Result.ConstantVal = cst;
146 Result.TargetReloType = RelocationType;
147 Result.AddrType = isExtSym;
148 Result.NeedStub = true;
149 Result.GOTRelative = GOTrelative;
150 Result.TargetResolve = false;
151 Result.Target.ExtSym = ES;
155 /// MachineRelocation::getConstPool - Return a relocation entry for a constant
158 static MachineRelocation getConstPool(intptr_t offset,unsigned RelocationType,
159 unsigned CPI, intptr_t cst = 0,
160 bool letTargetResolve = false) {
161 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
162 MachineRelocation Result;
163 Result.Offset = offset;
164 Result.ConstantVal = cst;
165 Result.TargetReloType = RelocationType;
166 Result.AddrType = isConstPool;
167 Result.NeedStub = false;
168 Result.GOTRelative = false;
169 Result.TargetResolve = letTargetResolve;
170 Result.Target.Index = CPI;
174 /// MachineRelocation::getJumpTable - Return a relocation entry for a jump
177 static MachineRelocation getJumpTable(intptr_t offset,unsigned RelocationType,
178 unsigned JTI, intptr_t cst = 0,
179 bool letTargetResolve = false) {
180 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
181 MachineRelocation Result;
182 Result.Offset = offset;
183 Result.ConstantVal = cst;
184 Result.TargetReloType = RelocationType;
185 Result.AddrType = isJumpTable;
186 Result.NeedStub = false;
187 Result.GOTRelative = false;
188 Result.TargetResolve = letTargetResolve;
189 Result.Target.Index = JTI;
193 /// getMachineCodeOffset - Return the offset into the code buffer that the
194 /// relocation should be performed.
195 intptr_t getMachineCodeOffset() const {
199 /// getRelocationType - Return the target-specific relocation ID for this
201 unsigned getRelocationType() const {
202 return TargetReloType;
205 /// getConstantVal - Get the constant value associated with this relocation.
206 /// This is often an offset from the symbol.
208 intptr_t getConstantVal() const {
212 /// setConstantVal - Set the constant value associated with this relocation.
213 /// This is often an offset from the symbol.
215 void setConstantVal(intptr_t val) {
219 /// isGlobalValue - Return true if this relocation is a GlobalValue, as
220 /// opposed to a constant string.
221 bool isGlobalValue() const {
222 return AddrType == isGV;
225 /// isGlobalValueNonLazyPtr - Return true if this relocation is the address
226 /// of a Mac OS X non-lazy indirect reference.
227 bool isGlobalValueNonLazyPtr() const {
228 return AddrType == isGVNonLazyPtr;
231 /// isBasicBlock - Return true if this relocation is a basic block reference.
233 bool isBasicBlock() const {
234 return AddrType == isBB;
237 /// isString - Return true if this is a constant string.
239 bool isString() const {
240 return AddrType == isExtSym;
243 /// isConstantPoolIndex - Return true if this is a constant pool reference.
245 bool isConstantPoolIndex() const {
246 return AddrType == isConstPool;
249 /// isJumpTableIndex - Return true if this is a jump table reference.
251 bool isJumpTableIndex() const {
252 return AddrType == isJumpTable;
255 /// isGOTRelative - Return true the target wants the index into the GOT of
256 /// the symbol rather than the address of the symbol.
257 bool isGOTRelative() const {
261 /// doesntNeedStub - This function returns true if the JIT for this target
262 /// target is capable of directly handling the relocated GlobalValue reference
263 /// without using either a stub function or issuing an extra load to get the
265 bool doesntNeedStub() const {
269 /// letTargetResolve - Return true if the target JITInfo is usually
270 /// responsible for resolving the address of this relocation.
271 bool letTargetResolve() const {
272 return TargetResolve;
275 /// getGlobalValue - If this is a global value reference, return the
276 /// referenced global.
277 GlobalValue *getGlobalValue() const {
278 assert((isGlobalValue() || isGlobalValueNonLazyPtr()) &&
279 "This is not a global value reference!");
283 MachineBasicBlock *getBasicBlock() const {
284 assert(isBasicBlock() && "This is not a basic block reference!");
288 /// getString - If this is a string value, return the string reference.
290 const char *getString() const {
291 assert(isString() && "This is not a string reference!");
292 return Target.ExtSym;
295 /// getConstantPoolIndex - If this is a const pool reference, return
296 /// the index into the constant pool.
297 unsigned getConstantPoolIndex() const {
298 assert(isConstantPoolIndex() && "This is not a constant pool reference!");
302 /// getJumpTableIndex - If this is a jump table reference, return
303 /// the index into the jump table.
304 unsigned getJumpTableIndex() const {
305 assert(isJumpTableIndex() && "This is not a jump table reference!");
309 /// getResultPointer - Once this has been resolved to point to an actual
310 /// address, this returns the pointer.
311 void *getResultPointer() const {
312 assert(AddrType == isResult && "Result pointer isn't set yet!");
313 return Target.Result;
316 /// setResultPointer - Set the result to the specified pointer value.
318 void setResultPointer(void *Ptr) {
323 /// setGOTIndex - Set the GOT index to a specific value.
324 void setGOTIndex(unsigned idx) {
325 AddrType = isGOTIndex;
326 Target.GOTIndex = idx;
329 /// getGOTIndex - Once this has been resolved to an entry in the GOT,
330 /// this returns that index. The index is from the lowest address entry
332 unsigned getGOTIndex() const {
333 assert(AddrType == isGOTIndex);
334 return Target.GOTIndex;