Remove a bunch more SparcV9 specific stuff
[oota-llvm.git] / include / llvm / CodeGen / MachineRelocation.h
1 //===-- llvm/CodeGen/MachineRelocation.h - Target Relocation ----*- C++ -*-===//
2 //
3 //                     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.
7 //
8 //===----------------------------------------------------------------------===//
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"
18 #include <cassert>
19
20 namespace llvm {
21 class 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.
30 ///   2. A target specific relocation type (a number from 0 to 63).
31 ///   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.
33 ///   5. A bit, CanRewrite, which indicates to the JIT that a function stub is
34 ///      not needed for the relocation.
35 ///   6. An index into the GOT, if the target uses a GOT
36 ///
37 class MachineRelocation {
38   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.
42     isConstPool,      // The Target.ConstPool field is valid.
43     isGOTIndex        // The Target.GOTIndex field is valid.
44   };
45   
46   /// Offset - This is the offset from the start of the code buffer of the
47   /// relocation to perform.
48   intptr_t Offset;
49   
50   /// ConstantVal - A field that may be used by the target relocation type.
51   intptr_t ConstantVal;
52
53   union {
54     void *Result;        // If this has been resolved to a resolved pointer
55     GlobalValue *GV;     // If this is a pointer to an LLVM global
56     const char *ExtSym;  // If this is a pointer to a named symbol
57     unsigned ConstPool;  // In this is a pointer to a constant pool entry
58     unsigned GOTIndex;   // Index in the GOT of this symbol/global
59   } Target;
60
61   unsigned TargetReloType : 6; // The target relocation ID.
62   AddressType AddrType    : 3; // The field of Target to use.
63   bool DoesntNeedFnStub   : 1; // True if we don't need a fn stub.
64   bool GOTRelative        : 1; // Should this relocation be relative to the GOT?
65
66 public:
67   /// MachineRelocation::getGV - Return a relocation entry for a GlobalValue.
68   ///
69   static MachineRelocation getGV(intptr_t offset, unsigned RelocationType, 
70                                  GlobalValue *GV, intptr_t cst = 0,
71                                  bool DoesntNeedFunctionStub = 0,
72                                  bool GOTrelative = 0) {
73     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
74     MachineRelocation Result;
75     Result.Offset = offset;
76     Result.ConstantVal = cst;
77     Result.TargetReloType = RelocationType;
78     Result.AddrType = isGV;
79     Result.DoesntNeedFnStub = DoesntNeedFunctionStub;
80     Result.GOTRelative = GOTrelative;
81     Result.Target.GV = GV;
82     return Result;
83   }
84
85   /// MachineRelocation::getExtSym - Return a relocation entry for an external
86   /// symbol, like "free".
87   ///
88   static MachineRelocation getExtSym(intptr_t offset, unsigned RelocationType, 
89                                      const char *ES, intptr_t cst = 0,
90                                      bool GOTrelative = 0) {
91     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
92     MachineRelocation Result;
93     Result.Offset = offset;
94     Result.ConstantVal = cst;
95     Result.TargetReloType = RelocationType;
96     Result.AddrType = isExtSym;
97     Result.DoesntNeedFnStub = false;
98     Result.GOTRelative = GOTrelative;
99     Result.Target.ExtSym = ES;
100     return Result;
101   }
102
103   /// MachineRelocation::getConstPool - Return a relocation entry for a constant
104   /// pool entry.
105   ///
106   static MachineRelocation getConstPool(intptr_t offset,unsigned RelocationType,
107                                         unsigned CPI, intptr_t cst = 0) {
108     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
109     MachineRelocation Result;
110     Result.Offset = offset;
111     Result.ConstantVal = cst;
112     Result.TargetReloType = RelocationType;
113     Result.AddrType = isConstPool;
114     Result.DoesntNeedFnStub = false;
115     Result.GOTRelative = false;
116     Result.Target.ConstPool = CPI;
117     return Result;
118   }
119
120   /// getMachineCodeOffset - Return the offset into the code buffer that the
121   /// relocation should be performed.
122   intptr_t getMachineCodeOffset() const {
123     return Offset;
124   }
125
126   /// getRelocationType - Return the target-specific relocation ID for this
127   /// relocation.
128   unsigned getRelocationType() const {
129     return TargetReloType;
130   }
131
132   /// getConstantVal - Get the constant value associated with this relocation.
133   /// This is often an offset from the symbol.
134   ///
135   intptr_t getConstantVal() const {
136     return ConstantVal;
137   }
138
139   /// isGlobalValue - Return true if this relocation is a GlobalValue, as
140   /// opposed to a constant string.
141   bool isGlobalValue() const {
142     return AddrType == isGV;
143   }
144
145   /// isString - Return true if this is a constant string.
146   ///
147   bool isString() const {
148     return AddrType == isExtSym;
149   }
150
151   /// isConstantPoolIndex - Return true if this is a constant pool reference.
152   ///
153   bool isConstantPoolIndex() const {
154     return AddrType == isConstPool;
155   }
156
157   /// isGOTRelative - Return true the target wants the index into the GOT of
158   /// the symbol rather than the address of the symbol.
159   bool isGOTRelative() const {
160     return GOTRelative;
161   }
162
163   /// doesntNeedFunctionStub - This function returns true if the JIT for this
164   /// target is capable of directly handling the relocated instruction without
165   /// using a stub function.  It is always conservatively correct for this flag
166   /// to be false, but targets can improve their compilation callback functions
167   /// to handle more general cases if they want improved performance.
168   bool doesntNeedFunctionStub() const {
169     return DoesntNeedFnStub;
170   }
171
172   /// getGlobalValue - If this is a global value reference, return the
173   /// referenced global.
174   GlobalValue *getGlobalValue() const {
175     assert(isGlobalValue() && "This is not a global value reference!");
176     return Target.GV;
177   }
178
179   /// getString - If this is a string value, return the string reference.
180   ///
181   const char *getString() const {
182     assert(isString() && "This is not a string reference!");
183     return Target.ExtSym;
184   }
185
186   /// getConstantPoolIndex - If this is a const pool reference, return
187   /// the index into the constant pool.
188   unsigned getConstantPoolIndex() const {
189     assert(isConstantPoolIndex() && "This is not a constant pool reference!");
190     return Target.ConstPool;
191   }
192
193   /// getResultPointer - Once this has been resolved to point to an actual
194   /// address, this returns the pointer.
195   void *getResultPointer() const {
196     assert(AddrType == isResult && "Result pointer isn't set yet!");
197     return Target.Result;
198   }
199
200   /// setResultPointer - Set the result to the specified pointer value.
201   ///
202   void setResultPointer(void *Ptr) {
203     Target.Result = Ptr;
204     AddrType = isResult;
205   }
206
207   /// setGOTIndex - Set the GOT index to a specific value.
208   void setGOTIndex(unsigned idx) {
209     AddrType = isGOTIndex;
210     Target.GOTIndex = idx;
211   }
212
213   /// getGOTIndex - Once this has been resolved to an entry in the GOT,
214   /// this returns that index.  The index is from the lowest address entry
215   /// in the GOT.
216   unsigned getGOTIndex() const {
217     assert(AddrType == isGOTIndex);
218     return Target.GOTIndex;
219   }
220 };
221 }
222
223 #endif