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"
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.
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.
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;
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
47 MachineRelocation(unsigned Offset, unsigned RelocationType, GlobalValue *GV,
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!");
55 MachineRelocation(unsigned Offset, unsigned RelocationType, const char *ES,
57 : OffsetTypeExternal(Offset + (1 << 24) + (RelocationType << 25)),
59 assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
60 assert((RelocationType & ~127) == 0 && "Relocation type too large!");
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);
70 /// getRelocationType - Return the target-specific relocation ID for this
72 unsigned getRelocationType() const {
73 return OffsetTypeExternal >> 25;
76 /// getConstantVal - Get the constant value associated with this relocation.
77 /// This is often an offset from the symbol.
79 intptr_t getConstantVal() const {
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;
89 /// isString - Return true if this is a constant string.
91 bool isString() const {
92 return !isGlobalValue();
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!");
102 /// getString - If this is a string value, return the string reference.
104 const char *getString() const {
105 assert(isString() && "This is not a string reference!");
106 return Target.ExtSym;
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;
115 /// setResultPointer - Set the result to the specified pointer value.
117 void setResultPointer(void *Ptr) {