blob: 58b4f87bc5326cdd5e85de11b8b10c1a2a5a3fb5 [file] [log] [blame]
Chris Lattnerb89df9c2004-11-20 03:05:50 +00001//===-- llvm/CodeGen/MachineRelocation.h - Target Relocation ----*- C++ -*-===//
Misha Brukmanea61c352005-04-21 20:39:54 +00002//
Chris Lattnerb89df9c2004-11-20 03:05:50 +00003// 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.
Misha Brukmanea61c352005-04-21 20:39:54 +00007//
Chris Lattnerb89df9c2004-11-20 03:05:50 +00008//===----------------------------------------------------------------------===//
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"
Chris Lattner4e2239d2004-11-20 23:40:54 +000018#include <cassert>
Chris Lattnerb89df9c2004-11-20 03:05:50 +000019
20namespace llvm {
21class 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.
Chris Lattner765da912004-11-21 03:27:13 +000030/// 2. A target specific relocation type (a number from 0 to 63).
Chris Lattnerb89df9c2004-11-20 03:05:50 +000031/// 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.
Chris Lattner765da912004-11-21 03:27:13 +000033/// 5. A bit, CanRewrite, which indicates to the JIT that a function stub is
34/// not needed for the relocation.
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +000035/// 6. An index into the GOT, if the target uses a GOT
Chris Lattnerb89df9c2004-11-20 03:05:50 +000036///
37class MachineRelocation {
Chris Lattner1e3822c2006-05-03 18:52:31 +000038 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.
Evan Cheng52b510b2006-06-23 01:02:37 +000042 isConstPool, // Relocation of constant pool address.
43 isJumpTable, // Relocation of jump table address.
Chris Lattner1e3822c2006-05-03 18:52:31 +000044 isGOTIndex // The Target.GOTIndex field is valid.
45 };
46
47 /// Offset - This is the offset from the start of the code buffer of the
48 /// relocation to perform.
Chris Lattner5a032de2006-05-03 20:30:20 +000049 intptr_t Offset;
Chris Lattner1e3822c2006-05-03 18:52:31 +000050
51 /// ConstantVal - A field that may be used by the target relocation type.
52 intptr_t ConstantVal;
53
Chris Lattnerb89df9c2004-11-20 03:05:50 +000054 union {
Chris Lattner1e3822c2006-05-03 18:52:31 +000055 void *Result; // If this has been resolved to a resolved pointer
Chris Lattnerb89df9c2004-11-20 03:05:50 +000056 GlobalValue *GV; // If this is a pointer to an LLVM global
57 const char *ExtSym; // If this is a pointer to a named symbol
Evan Cheng52b510b2006-06-23 01:02:37 +000058 unsigned Index; // Constant pool / jump table index
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +000059 unsigned GOTIndex; // Index in the GOT of this symbol/global
Chris Lattnerb89df9c2004-11-20 03:05:50 +000060 } Target;
Chris Lattner1e3822c2006-05-03 18:52:31 +000061
62 unsigned TargetReloType : 6; // The target relocation ID.
63 AddressType AddrType : 3; // The field of Target to use.
64 bool DoesntNeedFnStub : 1; // True if we don't need a fn stub.
65 bool GOTRelative : 1; // Should this relocation be relative to the GOT?
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +000066
Chris Lattnerb89df9c2004-11-20 03:05:50 +000067public:
Chris Lattner5a032de2006-05-03 20:30:20 +000068 /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
69 ///
70 static MachineRelocation getGV(intptr_t offset, unsigned RelocationType,
71 GlobalValue *GV, intptr_t cst = 0,
72 bool DoesntNeedFunctionStub = 0,
73 bool GOTrelative = 0) {
Chris Lattner765da912004-11-21 03:27:13 +000074 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
Chris Lattner5a032de2006-05-03 20:30:20 +000075 MachineRelocation Result;
76 Result.Offset = offset;
77 Result.ConstantVal = cst;
78 Result.TargetReloType = RelocationType;
79 Result.AddrType = isGV;
80 Result.DoesntNeedFnStub = DoesntNeedFunctionStub;
81 Result.GOTRelative = GOTrelative;
82 Result.Target.GV = GV;
83 return Result;
Chris Lattnerb89df9c2004-11-20 03:05:50 +000084 }
85
Chris Lattner5a032de2006-05-03 20:30:20 +000086 /// MachineRelocation::getExtSym - Return a relocation entry for an external
87 /// symbol, like "free".
88 ///
89 static MachineRelocation getExtSym(intptr_t offset, unsigned RelocationType,
90 const char *ES, intptr_t cst = 0,
91 bool GOTrelative = 0) {
Chris Lattner765da912004-11-21 03:27:13 +000092 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
Chris Lattner5a032de2006-05-03 20:30:20 +000093 MachineRelocation Result;
94 Result.Offset = offset;
95 Result.ConstantVal = cst;
96 Result.TargetReloType = RelocationType;
97 Result.AddrType = isExtSym;
98 Result.DoesntNeedFnStub = false;
99 Result.GOTRelative = GOTrelative;
100 Result.Target.ExtSym = ES;
101 return Result;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000102 }
103
Chris Lattner5a032de2006-05-03 20:30:20 +0000104 /// MachineRelocation::getConstPool - Return a relocation entry for a constant
105 /// pool entry.
106 ///
107 static MachineRelocation getConstPool(intptr_t offset,unsigned RelocationType,
108 unsigned CPI, intptr_t cst = 0) {
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000109 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
Chris Lattner5a032de2006-05-03 20:30:20 +0000110 MachineRelocation Result;
111 Result.Offset = offset;
112 Result.ConstantVal = cst;
113 Result.TargetReloType = RelocationType;
114 Result.AddrType = isConstPool;
115 Result.DoesntNeedFnStub = false;
116 Result.GOTRelative = false;
Evan Cheng52b510b2006-06-23 01:02:37 +0000117 Result.Target.Index = CPI;
118 return Result;
119 }
120
121 /// MachineRelocation::getJumpTable - Return a relocation entry for a jump
122 /// table entry.
123 ///
124 static MachineRelocation getJumpTable(intptr_t offset,unsigned RelocationType,
125 unsigned JTI, intptr_t cst = 0) {
126 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
127 MachineRelocation Result;
128 Result.Offset = offset;
129 Result.ConstantVal = cst;
130 Result.TargetReloType = RelocationType;
131 Result.AddrType = isJumpTable;
132 Result.DoesntNeedFnStub = false;
133 Result.GOTRelative = false;
134 Result.Target.Index = JTI;
Chris Lattner5a032de2006-05-03 20:30:20 +0000135 return Result;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000136 }
137
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000138 /// getMachineCodeOffset - Return the offset into the code buffer that the
139 /// relocation should be performed.
Chris Lattner5a032de2006-05-03 20:30:20 +0000140 intptr_t getMachineCodeOffset() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000141 return Offset;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000142 }
143
Chris Lattnerfab11a72004-11-20 03:43:50 +0000144 /// getRelocationType - Return the target-specific relocation ID for this
145 /// relocation.
146 unsigned getRelocationType() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000147 return TargetReloType;
Chris Lattnerfab11a72004-11-20 03:43:50 +0000148 }
149
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000150 /// getConstantVal - Get the constant value associated with this relocation.
151 /// This is often an offset from the symbol.
152 ///
153 intptr_t getConstantVal() const {
154 return ConstantVal;
155 }
156
157 /// isGlobalValue - Return true if this relocation is a GlobalValue, as
158 /// opposed to a constant string.
159 bool isGlobalValue() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000160 return AddrType == isGV;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000161 }
162
163 /// isString - Return true if this is a constant string.
164 ///
165 bool isString() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000166 return AddrType == isExtSym;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000167 }
168
169 /// isConstantPoolIndex - Return true if this is a constant pool reference.
170 ///
171 bool isConstantPoolIndex() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000172 return AddrType == isConstPool;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000173 }
174
Evan Cheng52b510b2006-06-23 01:02:37 +0000175 /// isJumpTableIndex - Return true if this is a jump table reference.
176 ///
177 bool isJumpTableIndex() const {
178 return AddrType == isJumpTable;
179 }
180
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000181 /// isGOTRelative - Return true the target wants the index into the GOT of
182 /// the symbol rather than the address of the symbol.
183 bool isGOTRelative() const {
184 return GOTRelative;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000185 }
186
Chris Lattner765da912004-11-21 03:27:13 +0000187 /// doesntNeedFunctionStub - This function returns true if the JIT for this
188 /// target is capable of directly handling the relocated instruction without
189 /// using a stub function. It is always conservatively correct for this flag
190 /// to be false, but targets can improve their compilation callback functions
191 /// to handle more general cases if they want improved performance.
192 bool doesntNeedFunctionStub() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000193 return DoesntNeedFnStub;
Chris Lattner765da912004-11-21 03:27:13 +0000194 }
195
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000196 /// getGlobalValue - If this is a global value reference, return the
197 /// referenced global.
198 GlobalValue *getGlobalValue() const {
199 assert(isGlobalValue() && "This is not a global value reference!");
200 return Target.GV;
201 }
202
203 /// getString - If this is a string value, return the string reference.
204 ///
205 const char *getString() const {
206 assert(isString() && "This is not a string reference!");
207 return Target.ExtSym;
208 }
209
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000210 /// getConstantPoolIndex - If this is a const pool reference, return
211 /// the index into the constant pool.
212 unsigned getConstantPoolIndex() const {
213 assert(isConstantPoolIndex() && "This is not a constant pool reference!");
Evan Cheng52b510b2006-06-23 01:02:37 +0000214 return Target.Index;
215 }
216
217 /// getJumpTableIndex - If this is a jump table reference, return
218 /// the index into the jump table.
219 unsigned getJumpTableIndex() const {
220 assert(isJumpTableIndex() && "This is not a jump table reference!");
221 return Target.Index;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000222 }
223
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000224 /// getResultPointer - Once this has been resolved to point to an actual
225 /// address, this returns the pointer.
226 void *getResultPointer() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000227 assert(AddrType == isResult && "Result pointer isn't set yet!");
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000228 return Target.Result;
229 }
230
231 /// setResultPointer - Set the result to the specified pointer value.
232 ///
233 void setResultPointer(void *Ptr) {
234 Target.Result = Ptr;
Chris Lattner1e3822c2006-05-03 18:52:31 +0000235 AddrType = isResult;
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000236 }
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000237
238 /// setGOTIndex - Set the GOT index to a specific value.
239 void setGOTIndex(unsigned idx) {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000240 AddrType = isGOTIndex;
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000241 Target.GOTIndex = idx;
242 }
243
244 /// getGOTIndex - Once this has been resolved to an entry in the GOT,
Jeff Cohen9eb59ec2005-07-27 05:53:44 +0000245 /// this returns that index. The index is from the lowest address entry
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000246 /// in the GOT.
247 unsigned getGOTIndex() const {
Chris Lattner1e3822c2006-05-03 18:52:31 +0000248 assert(AddrType == isGOTIndex);
Andrew Lenharth6a6b2db2005-07-22 20:46:42 +0000249 return Target.GOTIndex;
250 }
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000251};
Chris Lattnerb89df9c2004-11-20 03:05:50 +0000252}
253
254#endif