Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 1 | //===-- Emitter.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 | |
| 154 | // Otherwise, codegen a new stub. For now, the stub will call the lazy |
| 155 | // resolver function. |
| 156 | Stub = TheJIT->getJITInfo().emitFunctionStub((void*)LazyResolverFn, MCE); |
| 157 | |
| 158 | // Finally, keep track of the stub-to-Function mapping so that the |
| 159 | // JITCompilerFn knows which function to compile! |
| 160 | StubToFunctionMap[Stub] = F; |
| 161 | return Stub; |
| 162 | } |
| 163 | |
| 164 | /// JITCompilerFn - This function is called when a lazy compilation stub has |
| 165 | /// been entered. It looks up which function this stub corresponds to, compiles |
| 166 | /// it if necessary, then returns the resultant function pointer. |
| 167 | void *JITResolver::JITCompilerFn(void *Stub) { |
| 168 | JITResolver &JR = getJITResolver(); |
| 169 | |
| 170 | // The address given to us for the stub may not be exactly right, it might be |
| 171 | // a little bit after the stub. As such, use upper_bound to find it. |
| 172 | std::map<void*, Function*>::iterator I = |
| 173 | JR.StubToFunctionMap.upper_bound(Stub); |
| 174 | assert(I != JR.StubToFunctionMap.begin() && "This is not a known stub!"); |
| 175 | Function *F = (--I)->second; |
| 176 | |
| 177 | // The target function will rewrite the stub so that the compilation callback |
| 178 | // function is no longer called from this stub. |
| 179 | JR.StubToFunctionMap.erase(I); |
| 180 | |
| 181 | DEBUG(std::cerr << "Lazily resolving function '" << F->getName() |
| 182 | << "' In stub ptr = " << Stub << " actual ptr = " |
| 183 | << I->first << "\n"); |
| 184 | |
| 185 | void *Result = TheJIT->getPointerToFunction(F); |
| 186 | |
| 187 | // We don't need to reuse this stub in the future, as F is now compiled. |
| 188 | JR.FunctionToStubMap.erase(F); |
| 189 | |
| 190 | // FIXME: We could rewrite all references to this stub if we knew them. |
| 191 | return Result; |
| 192 | } |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 193 | |
| 194 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 195 | //===----------------------------------------------------------------------===// |
| 196 | // JIT MachineCodeEmitter code. |
| 197 | // |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 198 | namespace { |
Brian Gaeke | 6020ddd | 2003-10-16 23:33:38 +0000 | [diff] [blame] | 199 | /// Emitter - The JIT implementation of the MachineCodeEmitter, which is used |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 200 | /// to output functions to memory for execution. |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 201 | class Emitter : public MachineCodeEmitter { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 202 | JITMemoryManager MemMgr; |
| 203 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 204 | // CurBlock - The start of the current block of memory. CurByte - The |
| 205 | // current byte being emitted to. |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 206 | unsigned char *CurBlock, *CurByte; |
| 207 | |
| 208 | // When outputting a function stub in the context of some other function, we |
| 209 | // save CurBlock and CurByte here. |
| 210 | unsigned char *SavedCurBlock, *SavedCurByte; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 211 | |
| 212 | // ConstantPoolAddresses - Contains the location for each entry in the |
| 213 | // constant pool. |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 214 | std::vector<void*> ConstantPoolAddresses; |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 215 | |
| 216 | /// Relocations - These are the relocations that the function needs, as |
| 217 | /// emitted. |
| 218 | std::vector<MachineRelocation> Relocations; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 219 | public: |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 220 | Emitter(JIT &jit) { TheJIT = &jit; } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 221 | |
| 222 | virtual void startFunction(MachineFunction &F); |
| 223 | virtual void finishFunction(MachineFunction &F); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 224 | virtual void emitConstantPool(MachineConstantPool *MCP); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 225 | virtual void startFunctionStub(unsigned StubSize); |
| 226 | virtual void* finishFunctionStub(const Function *F); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 227 | virtual void emitByte(unsigned char B); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 228 | virtual void emitWord(unsigned W); |
Brian Gaeke | aea1b58 | 2004-04-23 17:11:14 +0000 | [diff] [blame] | 229 | virtual void emitWordAt(unsigned W, unsigned *Ptr); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 231 | virtual void addRelocation(const MachineRelocation &MR) { |
| 232 | Relocations.push_back(MR); |
| 233 | } |
| 234 | |
| 235 | virtual uint64_t getCurrentPCValue(); |
| 236 | virtual uint64_t getCurrentPCOffset(); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 237 | virtual uint64_t getGlobalValueAddress(GlobalValue *V); |
Chris Lattner | 45730d7 | 2004-11-19 20:56:46 +0000 | [diff] [blame] | 238 | virtual uint64_t getGlobalValueAddress(const char *Name); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 239 | virtual uint64_t getConstantPoolEntryAddress(unsigned Entry); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 240 | |
| 241 | // forceCompilationOf - Force the compilation of the specified function, and |
| 242 | // return its address, because we REALLY need the address now. |
| 243 | // |
| 244 | // FIXME: This is JIT specific! |
| 245 | // |
| 246 | virtual uint64_t forceCompilationOf(Function *F); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 247 | |
| 248 | private: |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame^] | 249 | void *getPointerToGlobal(GlobalValue *GV, void *Reference, bool NoNeedStub); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 250 | }; |
| 251 | } |
| 252 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 253 | MachineCodeEmitter *JIT::createEmitter(JIT &jit) { |
| 254 | return new Emitter(jit); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame^] | 257 | void *Emitter::getPointerToGlobal(GlobalValue *V, void *Reference, |
| 258 | bool DoesntNeedStub) { |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 259 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 260 | /// FIXME: If we straightened things out, this could actually emit the |
| 261 | /// global immediately instead of queuing it for codegen later! |
| 262 | GlobalVariable *GV = cast<GlobalVariable>(V); |
| 263 | return TheJIT->getOrEmitGlobalVariable(GV); |
| 264 | } |
| 265 | |
| 266 | // If we have already compiled the function, return a pointer to its body. |
| 267 | Function *F = cast<Function>(V); |
| 268 | void *ResultPtr = TheJIT->getPointerToGlobalIfAvailable(F); |
| 269 | if (ResultPtr) return ResultPtr; |
| 270 | |
| 271 | if (F->hasExternalLinkage()) { |
| 272 | // If this is an external function pointer, we can force the JIT to |
| 273 | // 'compile' it, which really just adds it to the map. |
| 274 | return TheJIT->getPointerToFunction(F); |
| 275 | } |
| 276 | |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame^] | 277 | // Okay, the function has not been compiled yet, if the target callback |
| 278 | // mechanism is capable of rewriting the instruction directly, prefer to do |
| 279 | // that instead of emitting a stub. |
| 280 | if (DoesntNeedStub) |
| 281 | return getJITResolver(this).AddCallbackAtLocation(F, Reference); |
| 282 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 283 | // Otherwise, we have to emit a lazy resolving stub. |
| 284 | return getJITResolver(this).getFunctionStub(F); |
| 285 | } |
| 286 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 287 | void Emitter::startFunction(MachineFunction &F) { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 288 | CurByte = CurBlock = MemMgr.startFunctionBody(); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 289 | TheJIT->addGlobalMapping(F.getFunction(), CurBlock); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | void Emitter::finishFunction(MachineFunction &F) { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 293 | MemMgr.endFunctionBody(CurByte); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 294 | ConstantPoolAddresses.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 295 | NumBytes += CurByte-CurBlock; |
| 296 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 297 | if (!Relocations.empty()) { |
| 298 | // Resolve the relocations to concrete pointers. |
| 299 | for (unsigned i = 0, e = Relocations.size(); i != e; ++i) { |
| 300 | MachineRelocation &MR = Relocations[i]; |
| 301 | void *ResultPtr; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 302 | if (MR.isString()) |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 303 | ResultPtr = TheJIT->getPointerToNamedFunction(MR.getString()); |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 304 | else |
Chris Lattner | 5e22558 | 2004-11-21 03:37:42 +0000 | [diff] [blame^] | 305 | ResultPtr = getPointerToGlobal(MR.getGlobalValue(), |
| 306 | CurBlock+MR.getMachineCodeOffset(), |
| 307 | MR.doesntNeedFunctionStub()); |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 308 | MR.setResultPointer(ResultPtr); |
| 309 | } |
| 310 | |
| 311 | TheJIT->getJITInfo().relocate(CurBlock, &Relocations[0], |
| 312 | Relocations.size()); |
| 313 | } |
| 314 | |
Brian Gaeke | 02c26b6 | 2003-06-30 18:06:20 +0000 | [diff] [blame] | 315 | DEBUG(std::cerr << "Finished CodeGen of [" << (void*)CurBlock |
Misha Brukman | 1d44085 | 2003-06-06 06:52:35 +0000 | [diff] [blame] | 316 | << "] Function: " << F.getFunction()->getName() |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 317 | << ": " << CurByte-CurBlock << " bytes of text, " |
| 318 | << Relocations.size() << " relocations\n"); |
| 319 | Relocations.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 322 | void Emitter::emitConstantPool(MachineConstantPool *MCP) { |
| 323 | const std::vector<Constant*> &Constants = MCP->getConstants(); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 324 | if (Constants.empty()) return; |
| 325 | |
| 326 | std::vector<unsigned> ConstantOffset; |
| 327 | ConstantOffset.reserve(Constants.size()); |
| 328 | |
| 329 | // Calculate how much space we will need for all the constants, and the offset |
| 330 | // each one will live in. |
| 331 | unsigned TotalSize = 0; |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 332 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 333 | const Type *Ty = Constants[i]->getType(); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 334 | unsigned Size = TheJIT->getTargetData().getTypeSize(Ty); |
| 335 | unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 336 | // Make sure to take into account the alignment requirements of the type. |
| 337 | TotalSize = (TotalSize + Alignment-1) & ~(Alignment-1); |
| 338 | |
| 339 | // Remember the offset this element lives at. |
| 340 | ConstantOffset.push_back(TotalSize); |
| 341 | TotalSize += Size; // Reserve space for the constant. |
| 342 | } |
| 343 | |
| 344 | // Now that we know how much memory to allocate, do so. |
| 345 | char *Pool = new char[TotalSize]; |
| 346 | |
| 347 | // Actually output all of the constants, and remember their addresses. |
| 348 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
| 349 | void *Addr = Pool + ConstantOffset[i]; |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 350 | TheJIT->InitializeMemory(Constants[i], Addr); |
Misha Brukman | 91de352 | 2003-11-30 00:50:53 +0000 | [diff] [blame] | 351 | ConstantPoolAddresses.push_back(Addr); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 355 | void Emitter::startFunctionStub(unsigned StubSize) { |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 356 | SavedCurBlock = CurBlock; SavedCurByte = CurByte; |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 357 | CurByte = CurBlock = MemMgr.allocateStub(StubSize); |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 360 | void *Emitter::finishFunctionStub(const Function *F) { |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 361 | NumBytes += CurByte-CurBlock; |
Chris Lattner | 5426652 | 2004-11-20 23:57:07 +0000 | [diff] [blame] | 362 | DEBUG(std::cerr << "Finished CodeGen of [0x" << (void*)CurBlock |
| 363 | << "] Function stub for: " << (F ? F->getName() : "") |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 364 | << ": " << CurByte-CurBlock << " bytes of text\n"); |
| 365 | std::swap(CurBlock, SavedCurBlock); |
| 366 | CurByte = SavedCurByte; |
| 367 | return SavedCurBlock; |
| 368 | } |
| 369 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 370 | void Emitter::emitByte(unsigned char B) { |
| 371 | *CurByte++ = B; // Write the byte to memory |
| 372 | } |
| 373 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 374 | void Emitter::emitWord(unsigned W) { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 375 | // This won't work if the endianness of the host and target don't agree! (For |
| 376 | // a JIT this can't happen though. :) |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 377 | *(unsigned*)CurByte = W; |
| 378 | CurByte += sizeof(unsigned); |
| 379 | } |
| 380 | |
Brian Gaeke | aea1b58 | 2004-04-23 17:11:14 +0000 | [diff] [blame] | 381 | void Emitter::emitWordAt(unsigned W, unsigned *Ptr) { |
| 382 | *Ptr = W; |
| 383 | } |
| 384 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 385 | uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) { |
| 386 | // Try looking up the function to see if it is already compiled, if not return |
| 387 | // 0. |
Chris Lattner | 02376e3 | 2004-11-15 23:20:04 +0000 | [diff] [blame] | 388 | if (Function *F = dyn_cast<Function>(V)) { |
| 389 | void *Addr = TheJIT->getPointerToGlobalIfAvailable(F); |
| 390 | if (Addr == 0 && F->hasExternalLinkage()) { |
| 391 | // Do not output stubs for external functions. |
| 392 | Addr = TheJIT->getPointerToFunction(F); |
| 393 | } |
| 394 | return (intptr_t)Addr; |
| 395 | } else { |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 396 | return (intptr_t)TheJIT->getOrEmitGlobalVariable(cast<GlobalVariable>(V)); |
| 397 | } |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 398 | } |
Chris Lattner | 45730d7 | 2004-11-19 20:56:46 +0000 | [diff] [blame] | 399 | uint64_t Emitter::getGlobalValueAddress(const char *Name) { |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 400 | return (intptr_t)TheJIT->getPointerToNamedFunction(Name); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry |
| 404 | // in the constant pool that was last emitted with the 'emitConstantPool' |
| 405 | // method. |
| 406 | // |
| 407 | uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) { |
| 408 | assert(ConstantNum < ConstantPoolAddresses.size() && |
| 409 | "Invalid ConstantPoolIndex!"); |
| 410 | return (intptr_t)ConstantPoolAddresses[ConstantNum]; |
| 411 | } |
| 412 | |
| 413 | // getCurrentPCValue - This returns the address that the next emitted byte |
| 414 | // will be output to. |
| 415 | // |
| 416 | uint64_t Emitter::getCurrentPCValue() { |
| 417 | return (intptr_t)CurByte; |
| 418 | } |
| 419 | |
Chris Lattner | 5be478f | 2004-11-20 03:46:14 +0000 | [diff] [blame] | 420 | uint64_t Emitter::getCurrentPCOffset() { |
| 421 | return (intptr_t)CurByte-(intptr_t)CurBlock; |
| 422 | } |
| 423 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 424 | uint64_t Emitter::forceCompilationOf(Function *F) { |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 425 | return (intptr_t)TheJIT->getPointerToFunction(F); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 428 | // getPointerToNamedFunction - This function is used as a global wrapper to |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 429 | // JIT::getPointerToNamedFunction for the purpose of resolving symbols when |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 430 | // bugpoint is debugging the JIT. In that scenario, we are loading an .so and |
| 431 | // need to resolve function(s) that are being mis-codegenerated, so we need to |
| 432 | // resolve their addresses at runtime, and this is the way to do it. |
| 433 | extern "C" { |
| 434 | void *getPointerToNamedFunction(const char *Name) { |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 435 | Module &M = TheJIT->getModule(); |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 436 | if (Function *F = M.getNamedFunction(Name)) |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 437 | return TheJIT->getPointerToFunction(F); |
| 438 | return TheJIT->getPointerToNamedFunction(Name); |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 439 | } |
| 440 | } |