allow constants to be relocated like GV (necessary for alpha, as constants are reloca...
[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   /// OffsetTypeExternal - The low 24-bits of this value is the offset from the
39   /// start of the code buffer of the relocation to perform.  Bit 24 of this is
40   /// set if Target should use ExtSym instead of GV, Bit 25 is the CanRewrite
41   /// bit, and the high 6 bits hold the relocation type.
42   // FIXME: with the additional types of relocatable things, rearrange the
43   // storage of things to be a bit more effiecient
44   unsigned OffsetTypeExternal;
45   union {
46     GlobalValue *GV;     // If this is a pointer to an LLVM global
47     const char *ExtSym;  // If this is a pointer to a named symbol
48     void *Result;        // If this has been resolved to a resolved pointer
49     unsigned GOTIndex;   // Index in the GOT of this symbol/global
50     unsigned CPool;      // Index in the Constant Pool
51   } Target;
52   intptr_t ConstantVal;
53   bool GOTRelative; //out of bits in OffsetTypeExternal
54   bool isConstPool;
55
56 public:
57   MachineRelocation(unsigned Offset, unsigned RelocationType, GlobalValue *GV,
58                     intptr_t cst = 0, bool DoesntNeedFunctionStub = 0, 
59                     bool GOTrelative = 0)
60     : OffsetTypeExternal(Offset + (RelocationType << 26)), ConstantVal(cst),
61       GOTRelative(GOTrelative), isConstPool(0) {
62     assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
63     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
64     Target.GV = GV;
65     if (DoesntNeedFunctionStub)
66       OffsetTypeExternal |= 1 << 25;
67   }
68
69   MachineRelocation(unsigned Offset, unsigned RelocationType, const char *ES,
70                     intptr_t cst = 0, bool GOTrelative = 0)
71     : OffsetTypeExternal(Offset + (1 << 24) + (RelocationType << 26)),
72       ConstantVal(cst), GOTRelative(GOTrelative), isConstPool(0) {
73     assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
74     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
75     Target.ExtSym = ES;
76   }
77
78   MachineRelocation(unsigned Offset, unsigned RelocationType, unsigned CPI,
79                     intptr_t cst = 0)
80     : OffsetTypeExternal(Offset + (RelocationType << 26)),
81       ConstantVal(cst), GOTRelative(0), isConstPool(1) {
82     assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
83     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
84     Target.CPool = CPI;
85   }
86
87   /// getMachineCodeOffset - Return the offset into the code buffer that the
88   /// relocation should be performed.
89   unsigned getMachineCodeOffset() const {
90     return OffsetTypeExternal & ((1 << 24)-1);
91   }
92
93   /// getRelocationType - Return the target-specific relocation ID for this
94   /// relocation.
95   unsigned getRelocationType() const {
96     return OffsetTypeExternal >> 26;
97   }
98
99   /// getConstantVal - Get the constant value associated with this relocation.
100   /// This is often an offset from the symbol.
101   ///
102   intptr_t getConstantVal() const {
103     return ConstantVal;
104   }
105
106   /// isGlobalValue - Return true if this relocation is a GlobalValue, as
107   /// opposed to a constant string.
108   bool isGlobalValue() const {
109     return (OffsetTypeExternal & (1 << 24)) == 0 && !isConstantPoolIndex();
110   }
111
112   /// isString - Return true if this is a constant string.
113   ///
114   bool isString() const {
115     return !isGlobalValue() && !isConstantPoolIndex();
116   }
117
118   /// isConstantPoolIndex - Return true if this is a constant pool reference.
119   ///
120   bool isConstantPoolIndex() const {
121     return isConstPool;
122   }
123
124   /// isGOTRelative - Return true the target wants the index into the GOT of
125   /// the symbol rather than the address of the symbol.
126   bool isGOTRelative() const {
127     return GOTRelative;
128   }
129
130   /// doesntNeedFunctionStub - This function returns true if the JIT for this
131   /// target is capable of directly handling the relocated instruction without
132   /// using a stub function.  It is always conservatively correct for this flag
133   /// to be false, but targets can improve their compilation callback functions
134   /// to handle more general cases if they want improved performance.
135   bool doesntNeedFunctionStub() const {
136     return (OffsetTypeExternal & (1 << 25)) != 0;
137   }
138
139   /// getGlobalValue - If this is a global value reference, return the
140   /// referenced global.
141   GlobalValue *getGlobalValue() const {
142     assert(isGlobalValue() && "This is not a global value reference!");
143     return Target.GV;
144   }
145
146   /// getString - If this is a string value, return the string reference.
147   ///
148   const char *getString() const {
149     assert(isString() && "This is not a string reference!");
150     return Target.ExtSym;
151   }
152
153   /// getConstantPoolIndex - If this is a const pool reference, return
154   /// the index into the constant pool.
155   unsigned getConstantPoolIndex() const {
156     assert(isConstantPoolIndex() && "This is not a constant pool reference!");
157     return Target.CPool;
158   }
159
160   /// getResultPointer - Once this has been resolved to point to an actual
161   /// address, this returns the pointer.
162   void *getResultPointer() const {
163     return Target.Result;
164   }
165
166   /// setResultPointer - Set the result to the specified pointer value.
167   ///
168   void setResultPointer(void *Ptr) {
169     Target.Result = Ptr;
170   }
171
172   /// setGOTIndex - Set the GOT index to a specific value.
173   void setGOTIndex(unsigned idx) {
174     Target.GOTIndex = idx;
175   }
176
177   /// getGOTIndex - Once this has been resolved to an entry in the GOT,
178   /// this returns that index.  The index is from the lowest address entry 
179   /// in the GOT.
180   unsigned getGOTIndex() const {
181     return Target.GOTIndex;
182   }
183
184 };
185
186 }
187
188 #endif