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 isGVLazyPtr, // Relocation of a lazily resolved GV address.
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 lazy 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?
72 // Relocation types used in a generic implementation. Currently, relocation
73 // entries for all things use the generic VANILLA type until they are refined
74 // into target relocation types.
79 /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
81 static MachineRelocation getGV(intptr_t offset, unsigned RelocationType,
82 GlobalValue *GV, intptr_t cst = 0,
84 bool GOTrelative = 0) {
85 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
86 MachineRelocation Result;
87 Result.Offset = offset;
88 Result.ConstantVal = cst;
89 Result.TargetReloType = RelocationType;
90 Result.AddrType = isGV;
91 Result.NeedStub = NeedStub;
92 Result.GOTRelative = GOTrelative;
93 Result.Target.GV = GV;
97 /// MachineRelocation::getGVLazyPtr - Return a relocation entry for a
98 /// lazily resolved GlobalValue address.
99 static MachineRelocation getGVLazyPtr(intptr_t offset,
100 unsigned RelocationType,
101 GlobalValue *GV, intptr_t cst = 0,
103 bool GOTrelative = 0) {
104 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
105 MachineRelocation Result;
106 Result.Offset = offset;
107 Result.ConstantVal = cst;
108 Result.TargetReloType = RelocationType;
109 Result.AddrType = isGVLazyPtr;
110 Result.NeedStub = NeedStub;
111 Result.GOTRelative = GOTrelative;
112 Result.Target.GV = GV;
116 /// MachineRelocation::getBB - Return a relocation entry for a BB.
118 static MachineRelocation getBB(intptr_t offset,unsigned RelocationType,
119 MachineBasicBlock *MBB, intptr_t cst = 0) {
120 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
121 MachineRelocation Result;
122 Result.Offset = offset;
123 Result.ConstantVal = cst;
124 Result.TargetReloType = RelocationType;
125 Result.AddrType = isBB;
126 Result.NeedStub = false;
127 Result.GOTRelative = false;
128 Result.Target.MBB = MBB;
132 /// MachineRelocation::getExtSym - Return a relocation entry for an external
133 /// symbol, like "free".
135 static MachineRelocation getExtSym(intptr_t offset, unsigned RelocationType,
136 const char *ES, intptr_t cst = 0,
137 bool GOTrelative = 0) {
138 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
139 MachineRelocation Result;
140 Result.Offset = offset;
141 Result.ConstantVal = cst;
142 Result.TargetReloType = RelocationType;
143 Result.AddrType = isExtSym;
144 Result.NeedStub = true;
145 Result.GOTRelative = GOTrelative;
146 Result.Target.ExtSym = ES;
150 /// MachineRelocation::getConstPool - Return a relocation entry for a constant
153 static MachineRelocation getConstPool(intptr_t offset,unsigned RelocationType,
154 unsigned CPI, intptr_t cst = 0) {
155 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
156 MachineRelocation Result;
157 Result.Offset = offset;
158 Result.ConstantVal = cst;
159 Result.TargetReloType = RelocationType;
160 Result.AddrType = isConstPool;
161 Result.NeedStub = false;
162 Result.GOTRelative = false;
163 Result.Target.Index = CPI;
167 /// MachineRelocation::getJumpTable - Return a relocation entry for a jump
170 static MachineRelocation getJumpTable(intptr_t offset,unsigned RelocationType,
171 unsigned JTI, intptr_t cst = 0) {
172 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
173 MachineRelocation Result;
174 Result.Offset = offset;
175 Result.ConstantVal = cst;
176 Result.TargetReloType = RelocationType;
177 Result.AddrType = isJumpTable;
178 Result.NeedStub = false;
179 Result.GOTRelative = false;
180 Result.Target.Index = JTI;
184 /// getMachineCodeOffset - Return the offset into the code buffer that the
185 /// relocation should be performed.
186 intptr_t getMachineCodeOffset() const {
190 /// getRelocationType - Return the target-specific relocation ID for this
192 unsigned getRelocationType() const {
193 return TargetReloType;
196 /// getConstantVal - Get the constant value associated with this relocation.
197 /// This is often an offset from the symbol.
199 intptr_t getConstantVal() const {
203 /// setConstantVal - Set the constant value associated with this relocation.
204 /// This is often an offset from the symbol.
206 void setConstantVal(intptr_t val) {
210 /// isGlobalValue - Return true if this relocation is a GlobalValue, as
211 /// opposed to a constant string.
212 bool isGlobalValue() const {
213 return AddrType == isGV;
216 /// isGlobalValueVLazyPtr - Return true if this relocation is the address
217 /// of a lazily resolved GlobalValue.
218 bool isGlobalValueLazyPtr() const {
219 return AddrType == isGVLazyPtr;
222 /// isBasicBlock - Return true if this relocation is a basic block reference.
224 bool isBasicBlock() const {
225 return AddrType == isBB;
228 /// isString - Return true if this is a constant string.
230 bool isString() const {
231 return AddrType == isExtSym;
234 /// isConstantPoolIndex - Return true if this is a constant pool reference.
236 bool isConstantPoolIndex() const {
237 return AddrType == isConstPool;
240 /// isJumpTableIndex - Return true if this is a jump table reference.
242 bool isJumpTableIndex() const {
243 return AddrType == isJumpTable;
246 /// isGOTRelative - Return true the target wants the index into the GOT of
247 /// the symbol rather than the address of the symbol.
248 bool isGOTRelative() const {
252 /// doesntNeedStub - This function returns true if the JIT for this target
253 /// target is capable of directly handling the relocated GlobalValue reference
254 /// without using either a stub function or issuing an extra load to get the
256 bool doesntNeedStub() const {
260 /// getGlobalValue - If this is a global value reference, return the
261 /// referenced global.
262 GlobalValue *getGlobalValue() const {
263 assert((isGlobalValue() || isGlobalValueLazyPtr()) &&
264 "This is not a global value reference!");
268 MachineBasicBlock *getBasicBlock() const {
269 assert(isBasicBlock() && "This is not a basic block reference!");
273 /// getString - If this is a string value, return the string reference.
275 const char *getString() const {
276 assert(isString() && "This is not a string reference!");
277 return Target.ExtSym;
280 /// getConstantPoolIndex - If this is a const pool reference, return
281 /// the index into the constant pool.
282 unsigned getConstantPoolIndex() const {
283 assert(isConstantPoolIndex() && "This is not a constant pool reference!");
287 /// getJumpTableIndex - If this is a jump table reference, return
288 /// the index into the jump table.
289 unsigned getJumpTableIndex() const {
290 assert(isJumpTableIndex() && "This is not a jump table reference!");
294 /// getResultPointer - Once this has been resolved to point to an actual
295 /// address, this returns the pointer.
296 void *getResultPointer() const {
297 assert(AddrType == isResult && "Result pointer isn't set yet!");
298 return Target.Result;
301 /// setResultPointer - Set the result to the specified pointer value.
303 void setResultPointer(void *Ptr) {
308 /// setGOTIndex - Set the GOT index to a specific value.
309 void setGOTIndex(unsigned idx) {
310 AddrType = isGOTIndex;
311 Target.GOTIndex = idx;
314 /// getGOTIndex - Once this has been resolved to an entry in the GOT,
315 /// this returns that index. The index is from the lowest address entry
317 unsigned getGOTIndex() const {
318 assert(AddrType == isGOTIndex);
319 return Target.GOTIndex;