71bccad5c2536325858ef092d023d017e4030bd1
[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
19 namespace llvm {
20 class GlobalValue;
21
22 /// MachineRelocation - This represents a target-specific relocation value,
23 /// produced by the code emitter.  This relocation is resolved after the has
24 /// been emitted, either to an object file or to memory, when the target of the
25 /// relocation can be resolved.
26 ///
27 /// A relocation is made up of the following logical portions:
28 ///   1. An offset in the machine code buffer, the location to modify.
29 ///   2. A target specific relocation type (a number from 0 to 127).
30 ///   3. A symbol being referenced, either as a GlobalValue* or as a string.
31 ///   4. An optional constant value to be added to the reference.
32 ///
33 class MachineRelocation {
34   /// OffsetTypeExternal - The low 24-bits of this value is the offset from the
35   /// start of the code buffer of the relocation to perform.  Bit 24 of this is
36   /// set if Target should use ExtSym instead of GV, and the high 7 bits are to
37   /// hold the relocation type.
38   unsigned OffsetTypeExternal;
39   union {
40     GlobalValue *GV;     // If this is a pointer to an LLVM global
41     const char *ExtSym;  // If this is a pointer to a named symbol
42     void *Result;        // If this has been resolved to a resolved pointer
43   } Target;
44   intptr_t ConstantVal;
45 public:
46   MachineRelocation(unsigned Offset, unsigned RelocationType, GlobalValue *GV,
47              intptr_t cst = 0)
48     : OffsetTypeExternal(Offset + (RelocationType << 25)), ConstantVal(cst) {
49     assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
50     assert((RelocationType & ~127) == 0 && "Relocation type too large!");
51     Target.GV = GV;
52   }
53
54   MachineRelocation(unsigned Offset, unsigned RelocationType, const char *ES,
55              intptr_t cst = 0)
56     : OffsetTypeExternal(Offset + (1 << 24) + (RelocationType << 25)),
57     ConstantVal(cst) {
58     assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
59     assert((RelocationType & ~127) == 0 && "Relocation type too large!");
60     Target.ExtSym = ES;
61   }
62
63   /// getMachineCodeOffset - Return the offset into the code buffer that the
64   /// relocation should be performed.
65   unsigned getMachineCodeOffset() const {
66     return OffsetTypeExternal & ((1 << 24)-1);
67   }
68
69   /// getConstantVal - Get the constant value associated with this relocation.
70   /// This is often an offset from the symbol.
71   ///
72   intptr_t getConstantVal() const {
73     return ConstantVal;
74   }
75
76   /// isGlobalValue - Return true if this relocation is a GlobalValue, as
77   /// opposed to a constant string.
78   bool isGlobalValue() const {
79     return (OffsetTypeExternal & (1 << 24)) == 0;
80   }
81
82   /// isString - Return true if this is a constant string.
83   ///
84   bool isString() const {
85     return !isGlobalValue();
86   }
87
88   /// getGlobalValue - If this is a global value reference, return the
89   /// referenced global.
90   GlobalValue *getGlobalValue() const {
91     assert(isGlobalValue() && "This is not a global value reference!");
92     return Target.GV;
93   }
94
95   /// getString - If this is a string value, return the string reference.
96   ///
97   const char *getString() const {
98     assert(isString() && "This is not a string reference!");
99     return Target.ExtSym;
100   }
101
102   /// getResultPointer - Once this has been resolved to point to an actual
103   /// address, this returns the pointer.
104   void *getResultPointer() const {
105     return Target.Result;
106   }
107
108   /// setResultPointer - Set the result to the specified pointer value.
109   ///
110   void setResultPointer(void *Ptr) {
111     Target.Result = Ptr;
112   }
113 };
114
115 }
116
117 #endif