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