a72f29dd0f3ebb81ee6a3c3bb42560f66f8dd0eb
[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 ///
36 class MachineRelocation {
37   /// OffsetTypeExternal - The low 24-bits of this value is the offset from the
38   /// start of the code buffer of the relocation to perform.  Bit 24 of this is
39   /// set if Target should use ExtSym instead of GV, Bit 25 is the CanRewrite
40   /// bit, and the high 6 bits hold the relocation type.
41   unsigned OffsetTypeExternal;
42   union {
43     GlobalValue *GV;     // If this is a pointer to an LLVM global
44     const char *ExtSym;  // If this is a pointer to a named symbol
45     void *Result;        // If this has been resolved to a resolved pointer
46   } Target;
47   intptr_t ConstantVal;
48 public:
49   MachineRelocation(unsigned Offset, unsigned RelocationType, GlobalValue *GV,
50                     intptr_t cst = 0, bool DoesntNeedFunctionStub = 0)
51     : OffsetTypeExternal(Offset + (RelocationType << 26)), ConstantVal(cst) {
52     assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
53     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
54     Target.GV = GV;
55     if (DoesntNeedFunctionStub)
56       OffsetTypeExternal |= 1 << 25;
57   }
58
59   MachineRelocation(unsigned Offset, unsigned RelocationType, const char *ES,
60                     intptr_t cst = 0)
61     : OffsetTypeExternal(Offset + (1 << 24) + (RelocationType << 26)),
62     ConstantVal(cst) {
63     assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
64     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
65     Target.ExtSym = ES;
66   }
67
68   /// getMachineCodeOffset - Return the offset into the code buffer that the
69   /// relocation should be performed.
70   unsigned getMachineCodeOffset() const {
71     return OffsetTypeExternal & ((1 << 24)-1);
72   }
73
74   /// getRelocationType - Return the target-specific relocation ID for this
75   /// relocation.
76   unsigned getRelocationType() const {
77     return OffsetTypeExternal >> 26;
78   }
79
80   /// getConstantVal - Get the constant value associated with this relocation.
81   /// This is often an offset from the symbol.
82   ///
83   intptr_t getConstantVal() const {
84     return ConstantVal;
85   }
86
87   /// isGlobalValue - Return true if this relocation is a GlobalValue, as
88   /// opposed to a constant string.
89   bool isGlobalValue() const {
90     return (OffsetTypeExternal & (1 << 24)) == 0;
91   }
92
93   /// isString - Return true if this is a constant string.
94   ///
95   bool isString() const {
96     return !isGlobalValue();
97   }
98
99   /// doesntNeedFunctionStub - This function returns true if the JIT for this
100   /// target is capable of directly handling the relocated instruction without
101   /// using a stub function.  It is always conservatively correct for this flag
102   /// to be false, but targets can improve their compilation callback functions
103   /// to handle more general cases if they want improved performance.
104   bool doesntNeedFunctionStub() const {
105     return (OffsetTypeExternal & (1 << 25)) != 0;
106   }
107
108   /// getGlobalValue - If this is a global value reference, return the
109   /// referenced global.
110   GlobalValue *getGlobalValue() const {
111     assert(isGlobalValue() && "This is not a global value reference!");
112     return Target.GV;
113   }
114
115   /// getString - If this is a string value, return the string reference.
116   ///
117   const char *getString() const {
118     assert(isString() && "This is not a string reference!");
119     return Target.ExtSym;
120   }
121
122   /// getResultPointer - Once this has been resolved to point to an actual
123   /// address, this returns the pointer.
124   void *getResultPointer() const {
125     return Target.Result;
126   }
127
128   /// setResultPointer - Set the result to the specified pointer value.
129   ///
130   void setResultPointer(void *Ptr) {
131     Target.Result = Ptr;
132   }
133 };
134
135 }
136
137 #endif