1 //===-- llvm/CodeGen/MachineRelocation.h - Target Relocation ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
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 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.
49 /// Offset - This is the offset from the start of the code buffer of the
50 /// relocation to perform.
53 /// ConstantVal - A field that may be used by the target relocation type.
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
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?
71 /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
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;
89 /// MachineRelocation::getBB - Return a relocation entry for a BB.
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;
105 /// MachineRelocation::getExtSym - Return a relocation entry for an external
106 /// symbol, like "free".
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;
123 /// MachineRelocation::getConstPool - Return a relocation entry for a constant
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;
140 /// MachineRelocation::getJumpTable - Return a relocation entry for a jump
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;
157 /// getMachineCodeOffset - Return the offset into the code buffer that the
158 /// relocation should be performed.
159 intptr_t getMachineCodeOffset() const {
163 /// getRelocationType - Return the target-specific relocation ID for this
165 unsigned getRelocationType() const {
166 return TargetReloType;
169 /// getConstantVal - Get the constant value associated with this relocation.
170 /// This is often an offset from the symbol.
172 intptr_t getConstantVal() const {
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;
182 /// isBasicBlock - Return true if this relocation is a basic block reference.
184 bool isBasicBlock() const {
185 return AddrType == isBB;
188 /// isString - Return true if this is a constant string.
190 bool isString() const {
191 return AddrType == isExtSym;
194 /// isConstantPoolIndex - Return true if this is a constant pool reference.
196 bool isConstantPoolIndex() const {
197 return AddrType == isConstPool;
200 /// isJumpTableIndex - Return true if this is a jump table reference.
202 bool isJumpTableIndex() const {
203 return AddrType == isJumpTable;
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 {
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;
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!");
228 MachineBasicBlock *getBasicBlock() const {
229 assert(isBasicBlock() && "This is not a basic block reference!");
233 /// getString - If this is a string value, return the string reference.
235 const char *getString() const {
236 assert(isString() && "This is not a string reference!");
237 return Target.ExtSym;
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!");
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!");
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;
261 /// setResultPointer - Set the result to the specified pointer value.
263 void setResultPointer(void *Ptr) {
268 /// setGOTIndex - Set the GOT index to a specific value.
269 void setGOTIndex(unsigned idx) {
270 AddrType = isGOTIndex;
271 Target.GOTIndex = idx;
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
277 unsigned getGOTIndex() const {
278 assert(AddrType == isGOTIndex);
279 return Target.GOTIndex;