LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

MachineRelocation.h

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/MachineRelocation.h - Target Relocation ----*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the MachineRelocation class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CODEGEN_MACHINERELOCATION_H
00015 #define LLVM_CODEGEN_MACHINERELOCATION_H
00016 
00017 #include "llvm/Support/DataTypes.h"
00018 #include <cassert>
00019 
00020 namespace llvm {
00021 class GlobalValue;
00022 
00023 /// MachineRelocation - This represents a target-specific relocation value,
00024 /// produced by the code emitter.  This relocation is resolved after the has
00025 /// been emitted, either to an object file or to memory, when the target of the
00026 /// relocation can be resolved.
00027 ///
00028 /// A relocation is made up of the following logical portions:
00029 ///   1. An offset in the machine code buffer, the location to modify.
00030 ///   2. A target specific relocation type (a number from 0 to 63).
00031 ///   3. A symbol being referenced, either as a GlobalValue* or as a string.
00032 ///   4. An optional constant value to be added to the reference.
00033 ///   5. A bit, CanRewrite, which indicates to the JIT that a function stub is
00034 ///      not needed for the relocation.
00035 ///
00036 class MachineRelocation {
00037   /// OffsetTypeExternal - The low 24-bits of this value is the offset from the
00038   /// start of the code buffer of the relocation to perform.  Bit 24 of this is
00039   /// set if Target should use ExtSym instead of GV, Bit 25 is the CanRewrite
00040   /// bit, and the high 6 bits hold the relocation type.
00041   unsigned OffsetTypeExternal;
00042   union {
00043     GlobalValue *GV;     // If this is a pointer to an LLVM global
00044     const char *ExtSym;  // If this is a pointer to a named symbol
00045     void *Result;        // If this has been resolved to a resolved pointer
00046   } Target;
00047   intptr_t ConstantVal;
00048 public:
00049   MachineRelocation(unsigned Offset, unsigned RelocationType, GlobalValue *GV,
00050                     intptr_t cst = 0, bool DoesntNeedFunctionStub = 0)
00051     : OffsetTypeExternal(Offset + (RelocationType << 26)), ConstantVal(cst) {
00052     assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
00053     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
00054     Target.GV = GV;
00055     if (DoesntNeedFunctionStub)
00056       OffsetTypeExternal |= 1 << 25;
00057   }
00058 
00059   MachineRelocation(unsigned Offset, unsigned RelocationType, const char *ES,
00060                     intptr_t cst = 0)
00061     : OffsetTypeExternal(Offset + (1 << 24) + (RelocationType << 26)),
00062     ConstantVal(cst) {
00063     assert((Offset & ~((1 << 24)-1)) == 0 && "Code offset too large!");
00064     assert((RelocationType & ~63) == 0 && "Relocation type too large!");
00065     Target.ExtSym = ES;
00066   }
00067 
00068   /// getMachineCodeOffset - Return the offset into the code buffer that the
00069   /// relocation should be performed.
00070   unsigned getMachineCodeOffset() const {
00071     return OffsetTypeExternal & ((1 << 24)-1);
00072   }
00073 
00074   /// getRelocationType - Return the target-specific relocation ID for this
00075   /// relocation.
00076   unsigned getRelocationType() const {
00077     return OffsetTypeExternal >> 26;
00078   }
00079 
00080   /// getConstantVal - Get the constant value associated with this relocation.
00081   /// This is often an offset from the symbol.
00082   ///
00083   intptr_t getConstantVal() const {
00084     return ConstantVal;
00085   }
00086 
00087   /// isGlobalValue - Return true if this relocation is a GlobalValue, as
00088   /// opposed to a constant string.
00089   bool isGlobalValue() const {
00090     return (OffsetTypeExternal & (1 << 24)) == 0;
00091   }
00092 
00093   /// isString - Return true if this is a constant string.
00094   ///
00095   bool isString() const {
00096     return !isGlobalValue();
00097   }
00098 
00099   /// doesntNeedFunctionStub - This function returns true if the JIT for this
00100   /// target is capable of directly handling the relocated instruction without
00101   /// using a stub function.  It is always conservatively correct for this flag
00102   /// to be false, but targets can improve their compilation callback functions
00103   /// to handle more general cases if they want improved performance.
00104   bool doesntNeedFunctionStub() const {
00105     return (OffsetTypeExternal & (1 << 25)) != 0;
00106   }
00107 
00108   /// getGlobalValue - If this is a global value reference, return the
00109   /// referenced global.
00110   GlobalValue *getGlobalValue() const {
00111     assert(isGlobalValue() && "This is not a global value reference!");
00112     return Target.GV;
00113   }
00114 
00115   /// getString - If this is a string value, return the string reference.
00116   ///
00117   const char *getString() const {
00118     assert(isString() && "This is not a string reference!");
00119     return Target.ExtSym;
00120   }
00121 
00122   /// getResultPointer - Once this has been resolved to point to an actual
00123   /// address, this returns the pointer.
00124   void *getResultPointer() const {
00125     return Target.Result;
00126   }
00127 
00128   /// setResultPointer - Set the result to the specified pointer value.
00129   ///
00130   void setResultPointer(void *Ptr) {
00131     Target.Result = Ptr;
00132   }
00133 };
00134 
00135 }
00136 
00137 #endif