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 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.
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;
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
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!");
55 if (DoesntNeedFunctionStub)
56 OffsetTypeExternal |= 1 << 25;
59 MachineRelocation(unsigned Offset, unsigned RelocationType, const char *ES,
61 : OffsetTypeExternal(Offset + (1 << 24) + (RelocationType << 26)),
63 assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
64 assert((RelocationType & ~63) == 0 && "Relocation type too large!");
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);
74 /// getRelocationType - Return the target-specific relocation ID for this
76 unsigned getRelocationType() const {
77 return OffsetTypeExternal >> 26;
80 /// getConstantVal - Get the constant value associated with this relocation.
81 /// This is often an offset from the symbol.
83 intptr_t getConstantVal() const {
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;
93 /// isString - Return true if this is a constant string.
95 bool isString() const {
96 return !isGlobalValue();
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;
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!");
115 /// getString - If this is a string value, return the string reference.
117 const char *getString() const {
118 assert(isString() && "This is not a string reference!");
119 return Target.ExtSym;
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;
128 /// setResultPointer - Set the result to the specified pointer value.
130 void setResultPointer(void *Ptr) {