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 // 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.
78 /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
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;
96 /// MachineRelocation::getBB - Return a relocation entry for a BB.
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;
112 /// MachineRelocation::getExtSym - Return a relocation entry for an external
113 /// symbol, like "free".
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;
130 /// MachineRelocation::getConstPool - Return a relocation entry for a constant
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;
147 /// MachineRelocation::getJumpTable - Return a relocation entry for a jump
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;
164 /// getMachineCodeOffset - Return the offset into the code buffer that the
165 /// relocation should be performed.
166 intptr_t getMachineCodeOffset() const {
170 /// getRelocationType - Return the target-specific relocation ID for this
172 unsigned getRelocationType() const {
173 return TargetReloType;
176 /// getConstantVal - Get the constant value associated with this relocation.
177 /// This is often an offset from the symbol.
179 intptr_t getConstantVal() const {
183 /// setConstantVal - Set the constant value associated with this relocation.
184 /// This is often an offset from the symbol.
186 void setConstantVal(intptr_t val) {
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;
196 /// isBasicBlock - Return true if this relocation is a basic block reference.
198 bool isBasicBlock() const {
199 return AddrType == isBB;
202 /// isString - Return true if this is a constant string.
204 bool isString() const {
205 return AddrType == isExtSym;
208 /// isConstantPoolIndex - Return true if this is a constant pool reference.
210 bool isConstantPoolIndex() const {
211 return AddrType == isConstPool;
214 /// isJumpTableIndex - Return true if this is a jump table reference.
216 bool isJumpTableIndex() const {
217 return AddrType == isJumpTable;
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 {
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;
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!");
242 MachineBasicBlock *getBasicBlock() const {
243 assert(isBasicBlock() && "This is not a basic block reference!");
247 /// getString - If this is a string value, return the string reference.
249 const char *getString() const {
250 assert(isString() && "This is not a string reference!");
251 return Target.ExtSym;
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!");
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!");
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;
275 /// setResultPointer - Set the result to the specified pointer value.
277 void setResultPointer(void *Ptr) {
282 /// setGOTIndex - Set the GOT index to a specific value.
283 void setGOTIndex(unsigned idx) {
284 AddrType = isGOTIndex;
285 Target.GOTIndex = idx;
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
291 unsigned getGOTIndex() const {
292 assert(AddrType == isGOTIndex);
293 return Target.GOTIndex;