| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 1 | //===-- JITEmitter.cpp - Write machine code to executable memory ----------===// | 
| John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 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. | 
|  | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 9 | // | 
| Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 10 | // This file defines a MachineCodeEmitter object that is used by the JIT to | 
|  | 11 | // write machine code to memory and remember where relocatable values are. | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 12 | // | 
|  | 13 | //===----------------------------------------------------------------------===// | 
|  | 14 |  | 
| Chris Lattner | 3785fad | 2003-08-05 17:00:32 +0000 | [diff] [blame] | 15 | #define DEBUG_TYPE "jit" | 
| Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 16 | #include "JIT.h" | 
| Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 17 | #include "llvm/Constant.h" | 
|  | 18 | #include "llvm/Module.h" | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineCodeEmitter.h" | 
|  | 20 | #include "llvm/CodeGen/MachineFunction.h" | 
| Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineConstantPool.h" | 
| Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineRelocation.h" | 
| Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" | 
| Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetJITInfo.h" | 
| Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" | 
|  | 26 | #include "llvm/ADT/Statistic.h" | 
| Reid Spencer | 52b0ba6 | 2004-09-11 04:31:03 +0000 | [diff] [blame] | 27 | #include "llvm/System/Memory.h" | 
| Chris Lattner | c19aade | 2003-12-08 08:06:28 +0000 | [diff] [blame] | 28 | using namespace llvm; | 
| Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 |  | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 30 | namespace { | 
| Chris Lattner | e738656 | 2003-10-20 05:45:49 +0000 | [diff] [blame] | 31 | Statistic<> NumBytes("jit", "Number of bytes of machine code compiled"); | 
| Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 32 | JIT *TheJIT = 0; | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 33 | } | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 34 |  | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 35 |  | 
|  | 36 | //===----------------------------------------------------------------------===// | 
|  | 37 | // JITMemoryManager code. | 
|  | 38 | // | 
|  | 39 | namespace { | 
| Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 40 | /// JITMemoryManager - Manage memory for the JIT code generation in a logical, | 
|  | 41 | /// sane way.  This splits a large block of MAP_NORESERVE'd memory into two | 
|  | 42 | /// sections, one for function stubs, one for the functions themselves.  We | 
|  | 43 | /// have to do this because we may need to emit a function stub while in the | 
|  | 44 | /// middle of emitting a function, and we don't know how large the function we | 
|  | 45 | /// are emitting is.  This never bothers to release the memory, because when | 
|  | 46 | /// we are ready to destroy the JIT, the program exits. | 
|  | 47 | class JITMemoryManager { | 
| Reid Spencer | 33189e7 | 2004-09-13 22:38:11 +0000 | [diff] [blame] | 48 | sys::MemoryBlock  MemBlock;  // Virtual memory block allocated RWX | 
| Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 49 | unsigned char *MemBase;      // Base of block of memory, start of stub mem | 
|  | 50 | unsigned char *FunctionBase; // Start of the function body area | 
|  | 51 | unsigned char *CurStubPtr, *CurFunctionPtr; | 
|  | 52 | public: | 
|  | 53 | JITMemoryManager(); | 
|  | 54 |  | 
|  | 55 | inline unsigned char *allocateStub(unsigned StubSize); | 
|  | 56 | inline unsigned char *startFunctionBody(); | 
|  | 57 | inline void endFunctionBody(unsigned char *FunctionEnd); | 
|  | 58 | }; | 
|  | 59 | } | 
|  | 60 |  | 
| Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 61 | JITMemoryManager::JITMemoryManager() { | 
|  | 62 | // Allocate a 16M block of memory... | 
| Reid Spencer | 33189e7 | 2004-09-13 22:38:11 +0000 | [diff] [blame] | 63 | MemBlock = sys::Memory::AllocateRWX((16 << 20)); | 
| Reid Spencer | 52b0ba6 | 2004-09-11 04:31:03 +0000 | [diff] [blame] | 64 | MemBase = reinterpret_cast<unsigned char*>(MemBlock.base()); | 
| Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 65 | FunctionBase = MemBase + 512*1024; // Use 512k for stubs | 
|  | 66 |  | 
|  | 67 | // Allocate stubs backwards from the function base, allocate functions forward | 
|  | 68 | // from the function base. | 
|  | 69 | CurStubPtr = CurFunctionPtr = FunctionBase; | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) { | 
|  | 73 | CurStubPtr -= StubSize; | 
|  | 74 | if (CurStubPtr < MemBase) { | 
|  | 75 | std::cerr << "JIT ran out of memory for function stubs!\n"; | 
|  | 76 | abort(); | 
|  | 77 | } | 
|  | 78 | return CurStubPtr; | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | unsigned char *JITMemoryManager::startFunctionBody() { | 
| Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 82 | // Round up to an even multiple of 8 bytes, this should eventually be target | 
| Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 83 | // specific. | 
| Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 84 | return (unsigned char*)(((intptr_t)CurFunctionPtr + 7) & ~7); | 
| Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
|  | 87 | void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) { | 
|  | 88 | assert(FunctionEnd > CurFunctionPtr); | 
|  | 89 | CurFunctionPtr = FunctionEnd; | 
|  | 90 | } | 
|  | 91 |  | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 92 | //===----------------------------------------------------------------------===// | 
|  | 93 | // JIT lazy compilation code. | 
|  | 94 | // | 
|  | 95 | namespace { | 
|  | 96 | /// JITResolver - Keep track of, and resolve, call sites for functions that | 
|  | 97 | /// have not yet been compiled. | 
|  | 98 | class JITResolver { | 
| Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 99 | /// MCE - The MachineCodeEmitter to use to emit stubs with. | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 100 | MachineCodeEmitter &MCE; | 
|  | 101 |  | 
| Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 102 | /// LazyResolverFn - The target lazy resolver function that we actually | 
|  | 103 | /// rewrite instructions to use. | 
|  | 104 | TargetJITInfo::LazyResolverFn LazyResolverFn; | 
|  | 105 |  | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 106 | // FunctionToStubMap - Keep track of the stub created for a particular | 
|  | 107 | // function so that we can reuse them if necessary. | 
|  | 108 | std::map<Function*, void*> FunctionToStubMap; | 
|  | 109 |  | 
|  | 110 | // StubToFunctionMap - Keep track of the function that each stub corresponds | 
|  | 111 | // to. | 
|  | 112 | std::map<void*, Function*> StubToFunctionMap; | 
|  | 113 |  | 
|  | 114 | public: | 
| Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 115 | JITResolver(MachineCodeEmitter &mce) : MCE(mce) { | 
|  | 116 | LazyResolverFn = | 
|  | 117 | TheJIT->getJITInfo().getLazyResolverFunction(JITCompilerFn); | 
|  | 118 | } | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 119 |  | 
|  | 120 | /// getFunctionStub - This returns a pointer to a function stub, creating | 
|  | 121 | /// one on demand as needed. | 
|  | 122 | void *getFunctionStub(Function *F); | 
|  | 123 |  | 
| Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 124 | /// AddCallbackAtLocation - If the target is capable of rewriting an | 
|  | 125 | /// instruction without the use of a stub, record the location of the use so | 
|  | 126 | /// we know which function is being used at the location. | 
|  | 127 | void *AddCallbackAtLocation(Function *F, void *Location) { | 
|  | 128 | /// Get the target-specific JIT resolver function. | 
|  | 129 | StubToFunctionMap[Location] = F; | 
|  | 130 | return (void*)LazyResolverFn; | 
|  | 131 | } | 
|  | 132 |  | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 133 | /// JITCompilerFn - This function is called to resolve a stub to a compiled | 
|  | 134 | /// address.  If the LLVM Function corresponding to the stub has not yet | 
|  | 135 | /// been compiled, this function compiles it first. | 
|  | 136 | static void *JITCompilerFn(void *Stub); | 
|  | 137 | }; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | /// getJITResolver - This function returns the one instance of the JIT resolver. | 
|  | 141 | /// | 
|  | 142 | static JITResolver &getJITResolver(MachineCodeEmitter *MCE = 0) { | 
|  | 143 | static JITResolver TheJITResolver(*MCE); | 
|  | 144 | return TheJITResolver; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | /// getFunctionStub - This returns a pointer to a function stub, creating | 
|  | 148 | /// one on demand as needed. | 
|  | 149 | void *JITResolver::getFunctionStub(Function *F) { | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 150 | // If we already have a stub for this function, recycle it. | 
|  | 151 | void *&Stub = FunctionToStubMap[F]; | 
|  | 152 | if (Stub) return Stub; | 
|  | 153 |  | 
| Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 154 | // Call the lazy resolver function unless we already KNOW it is an external | 
|  | 155 | // function, in which case we just skip the lazy resolution step. | 
|  | 156 | void *Actual = (void*)LazyResolverFn; | 
|  | 157 | if (F->hasExternalLinkage()) | 
|  | 158 | Actual = TheJIT->getPointerToFunction(F); | 
|  | 159 |  | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 160 | // Otherwise, codegen a new stub.  For now, the stub will call the lazy | 
|  | 161 | // resolver function. | 
| Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 162 | Stub = TheJIT->getJITInfo().emitFunctionStub(Actual, MCE); | 
|  | 163 |  | 
|  | 164 | if (F->hasExternalLinkage()) { | 
|  | 165 | // If we are getting the stub for an external function, we really want the | 
|  | 166 | // address of the stub in the GlobalAddressMap for the JIT, not the address | 
|  | 167 | // of the external function. | 
|  | 168 | TheJIT->updateGlobalMapping(F, Stub); | 
|  | 169 | } | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 170 |  | 
| Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 171 | DEBUG(std::cerr << "JIT: Stub emitted at [" << Stub << "] for function '" | 
| Chris Lattner | 6f71720 | 2004-11-22 21:48:33 +0000 | [diff] [blame] | 172 | << F->getName() << "'\n"); | 
| Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 173 |  | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 174 | // Finally, keep track of the stub-to-Function mapping so that the | 
|  | 175 | // JITCompilerFn knows which function to compile! | 
|  | 176 | StubToFunctionMap[Stub] = F; | 
|  | 177 | return Stub; | 
|  | 178 | } | 
|  | 179 |  | 
|  | 180 | /// JITCompilerFn - This function is called when a lazy compilation stub has | 
|  | 181 | /// been entered.  It looks up which function this stub corresponds to, compiles | 
|  | 182 | /// it if necessary, then returns the resultant function pointer. | 
|  | 183 | void *JITResolver::JITCompilerFn(void *Stub) { | 
|  | 184 | JITResolver &JR = getJITResolver(); | 
|  | 185 |  | 
|  | 186 | // The address given to us for the stub may not be exactly right, it might be | 
|  | 187 | // a little bit after the stub.  As such, use upper_bound to find it. | 
|  | 188 | std::map<void*, Function*>::iterator I = | 
|  | 189 | JR.StubToFunctionMap.upper_bound(Stub); | 
|  | 190 | assert(I != JR.StubToFunctionMap.begin() && "This is not a known stub!"); | 
|  | 191 | Function *F = (--I)->second; | 
|  | 192 |  | 
|  | 193 | // The target function will rewrite the stub so that the compilation callback | 
|  | 194 | // function is no longer called from this stub. | 
|  | 195 | JR.StubToFunctionMap.erase(I); | 
|  | 196 |  | 
| Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 197 | DEBUG(std::cerr << "JIT: Lazily resolving function '" << F->getName() | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 198 | << "' In stub ptr = " << Stub << " actual ptr = " | 
|  | 199 | << I->first << "\n"); | 
|  | 200 |  | 
|  | 201 | void *Result = TheJIT->getPointerToFunction(F); | 
|  | 202 |  | 
|  | 203 | // We don't need to reuse this stub in the future, as F is now compiled. | 
|  | 204 | JR.FunctionToStubMap.erase(F); | 
|  | 205 |  | 
|  | 206 | // FIXME: We could rewrite all references to this stub if we knew them. | 
|  | 207 | return Result; | 
|  | 208 | } | 
| Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 209 |  | 
|  | 210 |  | 
| Chris Lattner | e518b71 | 2004-12-05 07:19:16 +0000 | [diff] [blame] | 211 | // getPointerToFunctionOrStub - If the specified function has been | 
|  | 212 | // code-gen'd, return a pointer to the function.  If not, compile it, or use | 
|  | 213 | // a stub to implement lazy compilation if available. | 
|  | 214 | // | 
|  | 215 | void *JIT::getPointerToFunctionOrStub(Function *F) { | 
|  | 216 | // If we have already code generated the function, just return the address. | 
|  | 217 | if (void *Addr = getPointerToGlobalIfAvailable(F)) | 
|  | 218 | return Addr; | 
|  | 219 |  | 
|  | 220 | // Get a stub if the target supports it | 
|  | 221 | return getJITResolver(MCE).getFunctionStub(F); | 
|  | 222 | } | 
|  | 223 |  | 
|  | 224 |  | 
|  | 225 |  | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 226 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 227 | // JITEmitter code. | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 228 | // | 
| Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 229 | namespace { | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 230 | /// JITEmitter - The JIT implementation of the MachineCodeEmitter, which is | 
|  | 231 | /// used to output functions to memory for execution. | 
|  | 232 | class JITEmitter : public MachineCodeEmitter { | 
| Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 233 | JITMemoryManager MemMgr; | 
|  | 234 |  | 
| Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 235 | // CurBlock - The start of the current block of memory.  CurByte - The | 
|  | 236 | // current byte being emitted to. | 
| Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 237 | unsigned char *CurBlock, *CurByte; | 
|  | 238 |  | 
|  | 239 | // When outputting a function stub in the context of some other function, we | 
|  | 240 | // save CurBlock and CurByte here. | 
|  | 241 | unsigned char *SavedCurBlock, *SavedCurByte; | 
| Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 242 |  | 
|  | 243 | // ConstantPoolAddresses - Contains the location for each entry in the | 
|  | 244 | // constant pool. | 
| Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 245 | std::vector<void*> ConstantPoolAddresses; | 
| Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 246 |  | 
|  | 247 | /// Relocations - These are the relocations that the function needs, as | 
|  | 248 | /// emitted. | 
|  | 249 | std::vector<MachineRelocation> Relocations; | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 250 | public: | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 251 | JITEmitter(JIT &jit) { TheJIT = &jit; } | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 252 |  | 
|  | 253 | virtual void startFunction(MachineFunction &F); | 
|  | 254 | virtual void finishFunction(MachineFunction &F); | 
| Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 255 | virtual void emitConstantPool(MachineConstantPool *MCP); | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 256 | virtual void startFunctionStub(unsigned StubSize); | 
|  | 257 | virtual void* finishFunctionStub(const Function *F); | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 258 | virtual void emitByte(unsigned char B); | 
| Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 259 | virtual void emitWord(unsigned W); | 
| Brian Gaeke | aea1b58 | 2004-04-23 17:11:14 +0000 | [diff] [blame] | 260 | virtual void emitWordAt(unsigned W, unsigned *Ptr); | 
| Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 261 |  | 
| Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 262 | virtual void addRelocation(const MachineRelocation &MR) { | 
|  | 263 | Relocations.push_back(MR); | 
|  | 264 | } | 
|  | 265 |  | 
|  | 266 | virtual uint64_t getCurrentPCValue(); | 
|  | 267 | virtual uint64_t getCurrentPCOffset(); | 
| Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 268 | virtual uint64_t getConstantPoolEntryAddress(unsigned Entry); | 
| Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 269 |  | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 270 | private: | 
| Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 271 | void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub); | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 272 | }; | 
|  | 273 | } | 
|  | 274 |  | 
| Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 275 | MachineCodeEmitter *JIT::createEmitter(JIT &jit) { | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 276 | return new JITEmitter(jit); | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 277 | } | 
|  | 278 |  | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 279 | void *JITEmitter::getPointerToGlobal(GlobalValue *V, void *Reference, | 
|  | 280 | bool DoesntNeedStub) { | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 281 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { | 
|  | 282 | /// FIXME: If we straightened things out, this could actually emit the | 
|  | 283 | /// global immediately instead of queuing it for codegen later! | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 284 | return TheJIT->getOrEmitGlobalVariable(GV); | 
|  | 285 | } | 
|  | 286 |  | 
|  | 287 | // If we have already compiled the function, return a pointer to its body. | 
|  | 288 | Function *F = cast<Function>(V); | 
|  | 289 | void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F); | 
|  | 290 | if (ResultPtr) return ResultPtr; | 
|  | 291 |  | 
| Chris Lattner | 532343b | 2004-11-30 17:41:49 +0000 | [diff] [blame] | 292 | if (F->hasExternalLinkage() && F->isExternal()) { | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 293 | // If this is an external function pointer, we can force the JIT to | 
|  | 294 | // 'compile' it, which really just adds it to the map. | 
| Chris Lattner | b43dbdc | 2004-11-22 07:24:43 +0000 | [diff] [blame] | 295 | if (DoesntNeedStub) | 
|  | 296 | return TheJIT->getPointerToFunction(F); | 
|  | 297 |  | 
|  | 298 | return getJITResolver(this).getFunctionStub(F); | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 299 | } | 
|  | 300 |  | 
| Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 301 | // Okay, the function has not been compiled yet, if the target callback | 
|  | 302 | // mechanism is capable of rewriting the instruction directly, prefer to do | 
|  | 303 | // that instead of emitting a stub. | 
|  | 304 | if (DoesntNeedStub) | 
|  | 305 | return getJITResolver(this).AddCallbackAtLocation(F, Reference); | 
|  | 306 |  | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 307 | // Otherwise, we have to emit a lazy resolving stub. | 
|  | 308 | return getJITResolver(this).getFunctionStub(F); | 
|  | 309 | } | 
|  | 310 |  | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 311 | void JITEmitter::startFunction(MachineFunction &F) { | 
| Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 312 | CurByte = CurBlock = MemMgr.startFunctionBody(); | 
| Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 313 | TheJIT->addGlobalMapping(F.getFunction(), CurBlock); | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 314 | } | 
|  | 315 |  | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 316 | void JITEmitter::finishFunction(MachineFunction &F) { | 
| Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 317 | MemMgr.endFunctionBody(CurByte); | 
| Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 318 | ConstantPoolAddresses.clear(); | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 319 | NumBytes += CurByte-CurBlock; | 
|  | 320 |  | 
| Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 321 | if (!Relocations.empty()) { | 
|  | 322 | // Resolve the relocations to concrete pointers. | 
|  | 323 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { | 
|  | 324 | MachineRelocation &MR = Relocations[i]; | 
|  | 325 | void *ResultPtr; | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 326 | if (MR.isString()) | 
| Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 327 | ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString()); | 
| Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 328 | else | 
| Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame] | 329 | ResultPtr = getPointerToGlobal(MR.getGlobalValue(), | 
|  | 330 | CurBlock+MR.getMachineCodeOffset(), | 
|  | 331 | MR.doesntNeedFunctionStub()); | 
| Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 332 | MR.setResultPointer(ResultPtr); | 
|  | 333 | } | 
|  | 334 |  | 
|  | 335 | TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0], | 
|  | 336 | Relocations.size()); | 
|  | 337 | } | 
|  | 338 |  | 
| Chris Lattner | cb47941 | 2004-11-21 03:44:32 +0000 | [diff] [blame] | 339 | DEBUG(std::cerr << "JIT: Finished CodeGen of [" << (void*)CurBlock | 
| Misha Brukman | 1d44085 | 2003-06-06 06:52:35 +0000 | [diff] [blame] | 340 | << "] Function: " << F.getFunction()->getName() | 
| Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 341 | << ": " << CurByte-CurBlock << " bytes of text, " | 
|  | 342 | << Relocations.size() << " relocations\n"); | 
|  | 343 | Relocations.clear(); | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 344 | } | 
|  | 345 |  | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 346 | void JITEmitter::emitConstantPool(MachineConstantPool *MCP) { | 
| Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 347 | const std::vector<Constant*> &Constants = MCP->getConstants(); | 
| Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 348 | if (Constants.empty()) return; | 
|  | 349 |  | 
|  | 350 | std::vector<unsigned> ConstantOffset; | 
|  | 351 | ConstantOffset.reserve(Constants.size()); | 
|  | 352 |  | 
|  | 353 | // Calculate how much space we will need for all the constants, and the offset | 
|  | 354 | // each one will live in. | 
|  | 355 | unsigned TotalSize = 0; | 
| Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 356 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { | 
| Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 357 | const Type *Ty = Constants[i]->getType(); | 
| Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 358 | unsigned Size      = TheJIT->getTargetData().getTypeSize(Ty); | 
|  | 359 | unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty); | 
| Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 360 | // Make sure to take into account the alignment requirements of the type. | 
|  | 361 | TotalSize = (TotalSize + Alignment-1) & ~(Alignment-1); | 
|  | 362 |  | 
|  | 363 | // Remember the offset this element lives at. | 
|  | 364 | ConstantOffset.push_back(TotalSize); | 
|  | 365 | TotalSize += Size;   // Reserve space for the constant. | 
|  | 366 | } | 
|  | 367 |  | 
|  | 368 | // Now that we know how much memory to allocate, do so. | 
|  | 369 | char *Pool = new char[TotalSize]; | 
|  | 370 |  | 
|  | 371 | // Actually output all of the constants, and remember their addresses. | 
|  | 372 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { | 
|  | 373 | void *Addr = Pool + ConstantOffset[i]; | 
| Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 374 | TheJIT->InitializeMemory(Constants[i], Addr); | 
| Misha Brukman | 91de352 | 2003-11-30 00:50:53 +0000 | [diff] [blame] | 375 | ConstantPoolAddresses.push_back(Addr); | 
| Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 376 | } | 
|  | 377 | } | 
|  | 378 |  | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 379 | void JITEmitter::startFunctionStub(unsigned StubSize) { | 
| Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 380 | SavedCurBlock = CurBlock;  SavedCurByte = CurByte; | 
| Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 381 | CurByte = CurBlock = MemMgr.allocateStub(StubSize); | 
| Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 382 | } | 
|  | 383 |  | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 384 | void *JITEmitter::finishFunctionStub(const Function *F) { | 
| Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 385 | NumBytes += CurByte-CurBlock; | 
| Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 386 | std::swap(CurBlock, SavedCurBlock); | 
|  | 387 | CurByte = SavedCurByte; | 
|  | 388 | return SavedCurBlock; | 
|  | 389 | } | 
|  | 390 |  | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 391 | void JITEmitter::emitByte(unsigned char B) { | 
| Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 392 | *CurByte++ = B;   // Write the byte to memory | 
|  | 393 | } | 
|  | 394 |  | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 395 | void JITEmitter::emitWord(unsigned W) { | 
| Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 396 | // This won't work if the endianness of the host and target don't agree!  (For | 
|  | 397 | // a JIT this can't happen though.  :) | 
| Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 398 | *(unsigned*)CurByte = W; | 
|  | 399 | CurByte += sizeof(unsigned); | 
|  | 400 | } | 
|  | 401 |  | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 402 | void JITEmitter::emitWordAt(unsigned W, unsigned *Ptr) { | 
| Brian Gaeke | aea1b58 | 2004-04-23 17:11:14 +0000 | [diff] [blame] | 403 | *Ptr = W; | 
|  | 404 | } | 
|  | 405 |  | 
| Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 406 | // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry | 
|  | 407 | // in the constant pool that was last emitted with the 'emitConstantPool' | 
|  | 408 | // method. | 
|  | 409 | // | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 410 | uint64_t JITEmitter::getConstantPoolEntryAddress(unsigned ConstantNum) { | 
| Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 411 | assert(ConstantNum < ConstantPoolAddresses.size() && | 
|  | 412 | "Invalid ConstantPoolIndex!"); | 
|  | 413 | return (intptr_t)ConstantPoolAddresses[ConstantNum]; | 
|  | 414 | } | 
|  | 415 |  | 
|  | 416 | // getCurrentPCValue - This returns the address that the next emitted byte | 
|  | 417 | // will be output to. | 
|  | 418 | // | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 419 | uint64_t JITEmitter::getCurrentPCValue() { | 
| Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 420 | return (intptr_t)CurByte; | 
|  | 421 | } | 
|  | 422 |  | 
| Chris Lattner | 166f226 | 2004-11-22 22:00:25 +0000 | [diff] [blame] | 423 | uint64_t JITEmitter::getCurrentPCOffset() { | 
| Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 424 | return (intptr_t)CurByte-(intptr_t)CurBlock; | 
|  | 425 | } | 
|  | 426 |  | 
| Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 427 | // getPointerToNamedFunction - This function is used as a global wrapper to | 
| Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 428 | // JIT::getPointerToNamedFunction for the purpose of resolving symbols when | 
| Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 429 | // bugpoint is debugging the JIT. In that scenario, we are loading an .so and | 
|  | 430 | // need to resolve function(s) that are being mis-codegenerated, so we need to | 
|  | 431 | // resolve their addresses at runtime, and this is the way to do it. | 
|  | 432 | extern "C" { | 
|  | 433 | void *getPointerToNamedFunction(const char *Name) { | 
| Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 434 | Module &M = TheJIT->getModule(); | 
| Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 435 | if (Function *F = M.getNamedFunction(Name)) | 
| Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 436 | return TheJIT->getPointerToFunction(F); | 
|  | 437 | return TheJIT->getPointerToNamedFunction(Name); | 
| Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 438 | } | 
|  | 439 | } |