1 //===-- llvm/CodeGen/MachineRelocation.h - Target Relocation ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file defines the MachineRelocation class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINERELOCATION_H
15 #define LLVM_CODEGEN_MACHINERELOCATION_H
17 #include "llvm/Support/DataTypes.h"
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.
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.
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;
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
46 MachineRelocation(unsigned Offset, unsigned RelocationType, GlobalValue *GV,
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!");
54 MachineRelocation(unsigned Offset, unsigned RelocationType, const char *ES,
56 : OffsetTypeExternal(Offset + (1 << 24) + (RelocationType << 25)),
58 assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
59 assert((RelocationType & ~127) == 0 && "Relocation type too large!");
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);
69 /// getConstantVal - Get the constant value associated with this relocation.
70 /// This is often an offset from the symbol.
72 intptr_t getConstantVal() const {
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;
82 /// isString - Return true if this is a constant string.
84 bool isString() const {
85 return !isGlobalValue();
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!");
95 /// getString - If this is a string value, return the string reference.
97 const char *getString() const {
98 assert(isString() && "This is not a string reference!");
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;
108 /// setResultPointer - Set the result to the specified pointer value.
110 void setResultPointer(void *Ptr) {