LLVM API Documentation

ARMJITInfo.h
Go to the documentation of this file.
00001 //===-- ARMJITInfo.h - ARM implementation of the JIT interface  -*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains the declaration of the ARMJITInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef ARMJITINFO_H
00015 #define ARMJITINFO_H
00016 
00017 #include "ARMMachineFunctionInfo.h"
00018 #include "llvm/ADT/DenseMap.h"
00019 #include "llvm/ADT/SmallVector.h"
00020 #include "llvm/CodeGen/MachineConstantPool.h"
00021 #include "llvm/CodeGen/MachineFunction.h"
00022 #include "llvm/CodeGen/MachineJumpTableInfo.h"
00023 #include "llvm/Target/TargetJITInfo.h"
00024 
00025 namespace llvm {
00026   class ARMTargetMachine;
00027 
00028   class ARMJITInfo : public TargetJITInfo {
00029     // ConstPoolId2AddrMap - A map from constant pool ids to the corresponding
00030     // CONSTPOOL_ENTRY addresses.
00031     SmallVector<intptr_t, 16> ConstPoolId2AddrMap;
00032 
00033     // JumpTableId2AddrMap - A map from inline jumptable ids to the
00034     // corresponding inline jump table bases.
00035     SmallVector<intptr_t, 16> JumpTableId2AddrMap;
00036 
00037     // PCLabelMap - A map from PC labels to addresses.
00038     DenseMap<unsigned, intptr_t> PCLabelMap;
00039 
00040     // Sym2IndirectSymMap - A map from symbol (GlobalValue and ExternalSymbol)
00041     // addresses to their indirect symbol addresses.
00042     DenseMap<void*, intptr_t> Sym2IndirectSymMap;
00043 
00044     // IsPIC - True if the relocation model is PIC. This is used to determine
00045     // how to codegen function stubs.
00046     bool IsPIC;
00047 
00048   public:
00049     explicit ARMJITInfo() : IsPIC(false) { useGOT = false; }
00050 
00051     /// replaceMachineCodeForFunction - Make it so that calling the function
00052     /// whose machine code is at OLD turns into a call to NEW, perhaps by
00053     /// overwriting OLD with a branch to NEW.  This is used for self-modifying
00054     /// code.
00055     ///
00056     virtual void replaceMachineCodeForFunction(void *Old, void *New);
00057 
00058     /// emitGlobalValueIndirectSym - Use the specified JITCodeEmitter object
00059     /// to emit an indirect symbol which contains the address of the specified
00060     /// ptr.
00061     virtual void *emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr,
00062                                             JITCodeEmitter &JCE);
00063 
00064     // getStubLayout - Returns the size and alignment of the largest call stub
00065     // on ARM.
00066     virtual StubLayout getStubLayout();
00067 
00068     /// emitFunctionStub - Use the specified JITCodeEmitter object to emit a
00069     /// small native function that simply calls the function at the specified
00070     /// address.
00071     virtual void *emitFunctionStub(const Function* F, void *Fn,
00072                                    JITCodeEmitter &JCE);
00073 
00074     /// getLazyResolverFunction - Expose the lazy resolver to the JIT.
00075     virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn);
00076 
00077     /// relocate - Before the JIT can run a block of code that has been emitted,
00078     /// it must rewrite the code to contain the actual addresses of any
00079     /// referenced global symbols.
00080     virtual void relocate(void *Function, MachineRelocation *MR,
00081                           unsigned NumRelocs, unsigned char* GOTBase);
00082 
00083     /// hasCustomConstantPool - Allows a target to specify that constant
00084     /// pool address resolution is handled by the target.
00085     virtual bool hasCustomConstantPool() const { return true; }
00086 
00087     /// hasCustomJumpTables - Allows a target to specify that jumptables
00088     /// are emitted by the target.
00089     virtual bool hasCustomJumpTables() const { return true; }
00090 
00091     /// allocateSeparateGVMemory - If true, globals should be placed in
00092     /// separately allocated heap memory rather than in the same
00093     /// code memory allocated by JITCodeEmitter.
00094     virtual bool allocateSeparateGVMemory() const {
00095 #ifdef __APPLE__
00096       return true;
00097 #else
00098       return false;
00099 #endif
00100     }
00101 
00102     /// Initialize - Initialize internal stage for the function being JITted.
00103     /// Resize constant pool ids to CONSTPOOL_ENTRY addresses map; resize
00104     /// jump table ids to jump table bases map; remember if codegen relocation
00105     /// model is PIC.
00106     void Initialize(const MachineFunction &MF, bool isPIC) {
00107       const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
00108       ConstPoolId2AddrMap.resize(AFI->getNumPICLabels());
00109       JumpTableId2AddrMap.resize(AFI->getNumJumpTables());
00110       IsPIC = isPIC;
00111     }
00112 
00113     /// getConstantPoolEntryAddr - The ARM target puts all constant
00114     /// pool entries into constant islands. This returns the address of the
00115     /// constant pool entry of the specified index.
00116     intptr_t getConstantPoolEntryAddr(unsigned CPI) const {
00117       assert(CPI < ConstPoolId2AddrMap.size());
00118       return ConstPoolId2AddrMap[CPI];
00119     }
00120 
00121     /// addConstantPoolEntryAddr - Map a Constant Pool Index to the address
00122     /// where its associated value is stored. When relocations are processed,
00123     /// this value will be used to resolve references to the constant.
00124     void addConstantPoolEntryAddr(unsigned CPI, intptr_t Addr) {
00125       assert(CPI < ConstPoolId2AddrMap.size());
00126       ConstPoolId2AddrMap[CPI] = Addr;
00127     }
00128 
00129     /// getJumpTableBaseAddr - The ARM target inline all jump tables within
00130     /// text section of the function. This returns the address of the base of
00131     /// the jump table of the specified index.
00132     intptr_t getJumpTableBaseAddr(unsigned JTI) const {
00133       assert(JTI < JumpTableId2AddrMap.size());
00134       return JumpTableId2AddrMap[JTI];
00135     }
00136 
00137     /// addJumpTableBaseAddr - Map a jump table index to the address where
00138     /// the corresponding inline jump table is emitted. When relocations are
00139     /// processed, this value will be used to resolve references to the
00140     /// jump table.
00141     void addJumpTableBaseAddr(unsigned JTI, intptr_t Addr) {
00142       assert(JTI < JumpTableId2AddrMap.size());
00143       JumpTableId2AddrMap[JTI] = Addr;
00144     }
00145 
00146     /// getPCLabelAddr - Retrieve the address of the PC label of the
00147     /// specified id.
00148     intptr_t getPCLabelAddr(unsigned Id) const {
00149       DenseMap<unsigned, intptr_t>::const_iterator I = PCLabelMap.find(Id);
00150       assert(I != PCLabelMap.end());
00151       return I->second;
00152     }
00153 
00154     /// addPCLabelAddr - Remember the address of the specified PC label.
00155     void addPCLabelAddr(unsigned Id, intptr_t Addr) {
00156       PCLabelMap.insert(std::make_pair(Id, Addr));
00157     }
00158 
00159     /// getIndirectSymAddr - Retrieve the address of the indirect symbol of the
00160     /// specified symbol located at address. Returns 0 if the indirect symbol
00161     /// has not been emitted.
00162     intptr_t getIndirectSymAddr(void *Addr) const {
00163       DenseMap<void*,intptr_t>::const_iterator I= Sym2IndirectSymMap.find(Addr);
00164       if (I != Sym2IndirectSymMap.end())
00165         return I->second;
00166       return 0;
00167     }
00168 
00169     /// addIndirectSymAddr - Add a mapping from address of an emitted symbol to
00170     /// its indirect symbol address.
00171     void addIndirectSymAddr(void *SymAddr, intptr_t IndSymAddr) {
00172       Sym2IndirectSymMap.insert(std::make_pair(SymAddr, IndSymAddr));
00173     }
00174 
00175   private:
00176     /// resolveRelocDestAddr - Resolve the resulting address of the relocation
00177     /// if it's not already solved. Constantpool entries must be resolved by
00178     /// ARM target.
00179     intptr_t resolveRelocDestAddr(MachineRelocation *MR) const;
00180   };
00181 }
00182 
00183 #endif