Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 1 | //===- ARMJITInfo.h - ARM implementation of the JIT interface --*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains the declaration of the ARMJITInfo class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef ARMJITINFO_H |
| 15 | #define ARMJITINFO_H |
| 16 | |
Evan Cheng | f1bbb95 | 2008-11-08 00:51:41 +0000 | [diff] [blame] | 17 | #include "ARMMachineFunctionInfo.h" |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineConstantPool.h" |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFunction.h" |
| 20 | #include "llvm/CodeGen/MachineJumpTableInfo.h" |
Evan Cheng | f1bbb95 | 2008-11-08 00:51:41 +0000 | [diff] [blame] | 21 | #include "llvm/Target/TargetJITInfo.h" |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/DenseMap.h" |
Evan Cheng | 938b9d8 | 2008-10-31 19:55:13 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallVector.h" |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 24 | |
| 25 | namespace llvm { |
| 26 | class ARMTargetMachine; |
| 27 | |
| 28 | class ARMJITInfo : public TargetJITInfo { |
Evan Cheng | 0f28243 | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 29 | // ConstPoolId2AddrMap - A map from constant pool ids to the corresponding |
| 30 | // CONSTPOOL_ENTRY addresses. |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 31 | SmallVector<intptr_t, 16> ConstPoolId2AddrMap; |
| 32 | |
| 33 | // JumpTableId2AddrMap - A map from inline jumptable ids to the |
| 34 | // corresponding inline jump table bases. |
| 35 | SmallVector<intptr_t, 16> JumpTableId2AddrMap; |
Evan Cheng | 0f28243 | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 36 | |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 37 | // PCLabelMap - A map from PC labels to addresses. |
| 38 | DenseMap<unsigned, intptr_t> PCLabelMap; |
| 39 | |
Evan Cheng | 588920b | 2008-11-10 23:14:47 +0000 | [diff] [blame] | 40 | // Sym2IndirectSymMap - A map from symbol (GlobalValue and ExternalSymbol) |
| 41 | // addresses to their indirect symbol addresses. |
| 42 | DenseMap<void*, intptr_t> Sym2IndirectSymMap; |
| 43 | |
Evan Cheng | 3cc8223 | 2008-11-08 07:38:22 +0000 | [diff] [blame] | 44 | // IsPIC - True if the relocation model is PIC. This is used to determine |
| 45 | // how to codegen function stubs. |
| 46 | bool IsPIC; |
| 47 | |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 48 | public: |
Evan Cheng | 3cc8223 | 2008-11-08 07:38:22 +0000 | [diff] [blame] | 49 | explicit ARMJITInfo() : IsPIC(false) { useGOT = false; } |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 50 | |
| 51 | /// replaceMachineCodeForFunction - Make it so that calling the function |
| 52 | /// whose machine code is at OLD turns into a call to NEW, perhaps by |
| 53 | /// overwriting OLD with a branch to NEW. This is used for self-modifying |
| 54 | /// code. |
| 55 | /// |
| 56 | virtual void replaceMachineCodeForFunction(void *Old, void *New); |
| 57 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame^] | 58 | /// emitGlobalValueIndirectSym - Use the specified JITCodeEmitter object |
Evan Cheng | 9ed2f80 | 2008-11-10 01:08:07 +0000 | [diff] [blame] | 59 | /// to emit an indirect symbol which contains the address of the specified |
| 60 | /// ptr. |
| 61 | virtual void *emitGlobalValueIndirectSym(const GlobalValue* GV, void *ptr, |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame^] | 62 | JITCodeEmitter &JCE); |
Evan Cheng | e96a490 | 2008-11-08 01:31:27 +0000 | [diff] [blame] | 63 | |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame^] | 64 | /// emitFunctionStub - Use the specified JITCodeEmitter object to emit a |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 65 | /// small native function that simply calls the function at the specified |
| 66 | /// address. |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 67 | virtual void *emitFunctionStub(const Function* F, void *Fn, |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame^] | 68 | JITCodeEmitter &JCE); |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 69 | |
| 70 | /// getLazyResolverFunction - Expose the lazy resolver to the JIT. |
| 71 | virtual LazyResolverFn getLazyResolverFunction(JITCompilerFn); |
| 72 | |
| 73 | /// relocate - Before the JIT can run a block of code that has been emitted, |
| 74 | /// it must rewrite the code to contain the actual addresses of any |
| 75 | /// referenced global symbols. |
| 76 | virtual void relocate(void *Function, MachineRelocation *MR, |
| 77 | unsigned NumRelocs, unsigned char* GOTBase); |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 78 | |
Jim Grosbach | bc6d876 | 2008-10-28 18:25:49 +0000 | [diff] [blame] | 79 | /// hasCustomConstantPool - Allows a target to specify that constant |
| 80 | /// pool address resolution is handled by the target. |
| 81 | virtual bool hasCustomConstantPool() const { return true; } |
| 82 | |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 83 | /// hasCustomJumpTables - Allows a target to specify that jumptables |
| 84 | /// are emitted by the target. |
| 85 | virtual bool hasCustomJumpTables() const { return true; } |
| 86 | |
Evan Cheng | b0b5349 | 2008-11-04 09:30:48 +0000 | [diff] [blame] | 87 | /// allocateSeparateGVMemory - If true, globals should be placed in |
| 88 | /// separately allocated heap memory rather than in the same |
Bruno Cardoso Lopes | a3f99f9 | 2009-05-30 20:51:52 +0000 | [diff] [blame^] | 89 | /// code memory allocated by JITCodeEmitter. |
Evan Cheng | b0b5349 | 2008-11-04 09:30:48 +0000 | [diff] [blame] | 90 | virtual bool allocateSeparateGVMemory() const { |
| 91 | #ifdef __APPLE__ |
| 92 | return true; |
| 93 | #else |
| 94 | return false; |
| 95 | #endif |
| 96 | } |
| 97 | |
Evan Cheng | 3cc8223 | 2008-11-08 07:38:22 +0000 | [diff] [blame] | 98 | /// Initialize - Initialize internal stage for the function being JITted. |
| 99 | /// Resize constant pool ids to CONSTPOOL_ENTRY addresses map; resize |
| 100 | /// jump table ids to jump table bases map; remember if codegen relocation |
| 101 | /// model is PIC. |
| 102 | void Initialize(const MachineFunction &MF, bool isPIC) { |
Evan Cheng | f1bbb95 | 2008-11-08 00:51:41 +0000 | [diff] [blame] | 103 | const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>(); |
| 104 | ConstPoolId2AddrMap.resize(AFI->getNumConstPoolEntries()); |
| 105 | JumpTableId2AddrMap.resize(AFI->getNumJumpTables()); |
Evan Cheng | 3cc8223 | 2008-11-08 07:38:22 +0000 | [diff] [blame] | 106 | IsPIC = isPIC; |
Evan Cheng | 938b9d8 | 2008-10-31 19:55:13 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Evan Cheng | 0f28243 | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 109 | /// getConstantPoolEntryAddr - The ARM target puts all constant |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 110 | /// pool entries into constant islands. This returns the address of the |
| 111 | /// constant pool entry of the specified index. |
Evan Cheng | 938b9d8 | 2008-10-31 19:55:13 +0000 | [diff] [blame] | 112 | intptr_t getConstantPoolEntryAddr(unsigned CPI) const { |
| 113 | assert(CPI < ConstPoolId2AddrMap.size()); |
| 114 | return ConstPoolId2AddrMap[CPI]; |
Evan Cheng | 0f28243 | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 115 | } |
Jim Grosbach | bc6d876 | 2008-10-28 18:25:49 +0000 | [diff] [blame] | 116 | |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 117 | /// addConstantPoolEntryAddr - Map a Constant Pool Index to the address |
Jim Grosbach | bc6d876 | 2008-10-28 18:25:49 +0000 | [diff] [blame] | 118 | /// where its associated value is stored. When relocations are processed, |
| 119 | /// this value will be used to resolve references to the constant. |
Evan Cheng | 938b9d8 | 2008-10-31 19:55:13 +0000 | [diff] [blame] | 120 | void addConstantPoolEntryAddr(unsigned CPI, intptr_t Addr) { |
| 121 | assert(CPI < ConstPoolId2AddrMap.size()); |
| 122 | ConstPoolId2AddrMap[CPI] = Addr; |
Evan Cheng | 0f28243 | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 123 | } |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 124 | |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 125 | /// getJumpTableBaseAddr - The ARM target inline all jump tables within |
| 126 | /// text section of the function. This returns the address of the base of |
| 127 | /// the jump table of the specified index. |
| 128 | intptr_t getJumpTableBaseAddr(unsigned JTI) const { |
| 129 | assert(JTI < JumpTableId2AddrMap.size()); |
| 130 | return JumpTableId2AddrMap[JTI]; |
| 131 | } |
| 132 | |
| 133 | /// addJumpTableBaseAddr - Map a jump table index to the address where |
| 134 | /// the corresponding inline jump table is emitted. When relocations are |
| 135 | /// processed, this value will be used to resolve references to the |
| 136 | /// jump table. |
| 137 | void addJumpTableBaseAddr(unsigned JTI, intptr_t Addr) { |
| 138 | assert(JTI < JumpTableId2AddrMap.size()); |
| 139 | JumpTableId2AddrMap[JTI] = Addr; |
| 140 | } |
| 141 | |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 142 | /// getPCLabelAddr - Retrieve the address of the PC label of the specified id. |
| 143 | intptr_t getPCLabelAddr(unsigned Id) const { |
| 144 | DenseMap<unsigned, intptr_t>::const_iterator I = PCLabelMap.find(Id); |
| 145 | assert(I != PCLabelMap.end()); |
| 146 | return I->second; |
| 147 | } |
| 148 | |
| 149 | /// addPCLabelAddr - Remember the address of the specified PC label. |
| 150 | void addPCLabelAddr(unsigned Id, intptr_t Addr) { |
| 151 | PCLabelMap.insert(std::make_pair(Id, Addr)); |
| 152 | } |
| 153 | |
Evan Cheng | 588920b | 2008-11-10 23:14:47 +0000 | [diff] [blame] | 154 | /// getIndirectSymAddr - Retrieve the address of the indirect symbol of the |
| 155 | /// specified symbol located at address. Returns 0 if the indirect symbol |
| 156 | /// has not been emitted. |
| 157 | intptr_t getIndirectSymAddr(void *Addr) const { |
| 158 | DenseMap<void*,intptr_t>::const_iterator I= Sym2IndirectSymMap.find(Addr); |
| 159 | if (I != Sym2IndirectSymMap.end()) |
| 160 | return I->second; |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | /// addIndirectSymAddr - Add a mapping from address of an emitted symbol to |
| 165 | /// its indirect symbol address. |
| 166 | void addIndirectSymAddr(void *SymAddr, intptr_t IndSymAddr) { |
| 167 | Sym2IndirectSymMap.insert(std::make_pair(SymAddr, IndSymAddr)); |
| 168 | } |
| 169 | |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 170 | private: |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 171 | /// resolveRelocDestAddr - Resolve the resulting address of the relocation |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 172 | /// if it's not already solved. Constantpool entries must be resolved by |
| 173 | /// ARM target. |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 174 | intptr_t resolveRelocDestAddr(MachineRelocation *MR) const; |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 175 | }; |
| 176 | } |
| 177 | |
| 178 | #endif |