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 | // |
| 10 | // This file defines a MachineCodeEmitter object that is used by Jello to write |
| 11 | // machine code to memory and remember where relocatable values lie. |
| 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" |
| 22 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame^] | 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/ADT/Statistic.h" |
| 25 | #include "llvm/Support/SystemUtils.h" |
Chris Lattner | c19aade | 2003-12-08 08:06:28 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 27 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 28 | namespace { |
Chris Lattner | e738656 | 2003-10-20 05:45:49 +0000 | [diff] [blame] | 29 | Statistic<> NumBytes("jit", "Number of bytes of machine code compiled"); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 30 | JIT *TheJIT = 0; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 32 | /// JITMemoryManager - Manage memory for the JIT code generation in a logical, |
| 33 | /// sane way. This splits a large block of MAP_NORESERVE'd memory into two |
| 34 | /// sections, one for function stubs, one for the functions themselves. We |
| 35 | /// have to do this because we may need to emit a function stub while in the |
| 36 | /// middle of emitting a function, and we don't know how large the function we |
| 37 | /// are emitting is. This never bothers to release the memory, because when |
| 38 | /// we are ready to destroy the JIT, the program exits. |
| 39 | class JITMemoryManager { |
| 40 | unsigned char *MemBase; // Base of block of memory, start of stub mem |
| 41 | unsigned char *FunctionBase; // Start of the function body area |
| 42 | unsigned char *CurStubPtr, *CurFunctionPtr; |
| 43 | public: |
| 44 | JITMemoryManager(); |
| 45 | |
| 46 | inline unsigned char *allocateStub(unsigned StubSize); |
| 47 | inline unsigned char *startFunctionBody(); |
| 48 | inline void endFunctionBody(unsigned char *FunctionEnd); |
| 49 | }; |
| 50 | } |
| 51 | |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 52 | JITMemoryManager::JITMemoryManager() { |
| 53 | // Allocate a 16M block of memory... |
Chris Lattner | 0cb162b | 2004-05-28 00:57:27 +0000 | [diff] [blame] | 54 | MemBase = (unsigned char*)AllocateRWXMemory(16 << 20); |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 55 | FunctionBase = MemBase + 512*1024; // Use 512k for stubs |
| 56 | |
| 57 | // Allocate stubs backwards from the function base, allocate functions forward |
| 58 | // from the function base. |
| 59 | CurStubPtr = CurFunctionPtr = FunctionBase; |
| 60 | } |
| 61 | |
| 62 | unsigned char *JITMemoryManager::allocateStub(unsigned StubSize) { |
| 63 | CurStubPtr -= StubSize; |
| 64 | if (CurStubPtr < MemBase) { |
| 65 | std::cerr << "JIT ran out of memory for function stubs!\n"; |
| 66 | abort(); |
| 67 | } |
| 68 | return CurStubPtr; |
| 69 | } |
| 70 | |
| 71 | unsigned char *JITMemoryManager::startFunctionBody() { |
| 72 | // Round up to an even multiple of 4 bytes, this should eventually be target |
| 73 | // specific. |
| 74 | return (unsigned char*)(((intptr_t)CurFunctionPtr + 3) & ~3); |
| 75 | } |
| 76 | |
| 77 | void JITMemoryManager::endFunctionBody(unsigned char *FunctionEnd) { |
| 78 | assert(FunctionEnd > CurFunctionPtr); |
| 79 | CurFunctionPtr = FunctionEnd; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | |
| 84 | namespace { |
Brian Gaeke | 6020ddd | 2003-10-16 23:33:38 +0000 | [diff] [blame] | 85 | /// Emitter - The JIT implementation of the MachineCodeEmitter, which is used |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 86 | /// to output functions to memory for execution. |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 87 | class Emitter : public MachineCodeEmitter { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 88 | JITMemoryManager MemMgr; |
| 89 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 90 | // CurBlock - The start of the current block of memory. CurByte - The |
| 91 | // current byte being emitted to. |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 92 | unsigned char *CurBlock, *CurByte; |
| 93 | |
| 94 | // When outputting a function stub in the context of some other function, we |
| 95 | // save CurBlock and CurByte here. |
| 96 | unsigned char *SavedCurBlock, *SavedCurByte; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 97 | |
| 98 | // ConstantPoolAddresses - Contains the location for each entry in the |
| 99 | // constant pool. |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 100 | std::vector<void*> ConstantPoolAddresses; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 101 | public: |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 102 | Emitter(JIT &jit) { TheJIT = &jit; } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 103 | |
| 104 | virtual void startFunction(MachineFunction &F); |
| 105 | virtual void finishFunction(MachineFunction &F); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 106 | virtual void emitConstantPool(MachineConstantPool *MCP); |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 107 | virtual void startFunctionStub(const Function &F, unsigned StubSize); |
| 108 | virtual void* finishFunctionStub(const Function &F); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 109 | virtual void emitByte(unsigned char B); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 110 | virtual void emitWord(unsigned W); |
Brian Gaeke | aea1b58 | 2004-04-23 17:11:14 +0000 | [diff] [blame] | 111 | virtual void emitWordAt(unsigned W, unsigned *Ptr); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 112 | |
| 113 | virtual uint64_t getGlobalValueAddress(GlobalValue *V); |
| 114 | virtual uint64_t getGlobalValueAddress(const std::string &Name); |
| 115 | virtual uint64_t getConstantPoolEntryAddress(unsigned Entry); |
| 116 | virtual uint64_t getCurrentPCValue(); |
| 117 | |
| 118 | // forceCompilationOf - Force the compilation of the specified function, and |
| 119 | // return its address, because we REALLY need the address now. |
| 120 | // |
| 121 | // FIXME: This is JIT specific! |
| 122 | // |
| 123 | virtual uint64_t forceCompilationOf(Function *F); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 124 | }; |
| 125 | } |
| 126 | |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 127 | MachineCodeEmitter *JIT::createEmitter(JIT &jit) { |
| 128 | return new Emitter(jit); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 131 | void Emitter::startFunction(MachineFunction &F) { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 132 | CurByte = CurBlock = MemMgr.startFunctionBody(); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 133 | TheJIT->addGlobalMapping(F.getFunction(), CurBlock); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void Emitter::finishFunction(MachineFunction &F) { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 137 | MemMgr.endFunctionBody(CurByte); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 138 | ConstantPoolAddresses.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 139 | NumBytes += CurByte-CurBlock; |
| 140 | |
Brian Gaeke | 02c26b6 | 2003-06-30 18:06:20 +0000 | [diff] [blame] | 141 | DEBUG(std::cerr << "Finished CodeGen of [" << (void*)CurBlock |
Misha Brukman | 1d44085 | 2003-06-06 06:52:35 +0000 | [diff] [blame] | 142 | << "] Function: " << F.getFunction()->getName() |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 143 | << ": " << CurByte-CurBlock << " bytes of text\n"); |
| 144 | } |
| 145 | |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 146 | void Emitter::emitConstantPool(MachineConstantPool *MCP) { |
| 147 | const std::vector<Constant*> &Constants = MCP->getConstants(); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 148 | if (Constants.empty()) return; |
| 149 | |
| 150 | std::vector<unsigned> ConstantOffset; |
| 151 | ConstantOffset.reserve(Constants.size()); |
| 152 | |
| 153 | // Calculate how much space we will need for all the constants, and the offset |
| 154 | // each one will live in. |
| 155 | unsigned TotalSize = 0; |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 156 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 157 | const Type *Ty = Constants[i]->getType(); |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 158 | unsigned Size = TheJIT->getTargetData().getTypeSize(Ty); |
| 159 | unsigned Alignment = TheJIT->getTargetData().getTypeAlignment(Ty); |
Chris Lattner | 2c0a6a1 | 2003-11-30 04:23:21 +0000 | [diff] [blame] | 160 | // Make sure to take into account the alignment requirements of the type. |
| 161 | TotalSize = (TotalSize + Alignment-1) & ~(Alignment-1); |
| 162 | |
| 163 | // Remember the offset this element lives at. |
| 164 | ConstantOffset.push_back(TotalSize); |
| 165 | TotalSize += Size; // Reserve space for the constant. |
| 166 | } |
| 167 | |
| 168 | // Now that we know how much memory to allocate, do so. |
| 169 | char *Pool = new char[TotalSize]; |
| 170 | |
| 171 | // Actually output all of the constants, and remember their addresses. |
| 172 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
| 173 | void *Addr = Pool + ConstantOffset[i]; |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 174 | TheJIT->InitializeMemory(Constants[i], Addr); |
Misha Brukman | 91de352 | 2003-11-30 00:50:53 +0000 | [diff] [blame] | 175 | ConstantPoolAddresses.push_back(Addr); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 179 | void Emitter::startFunctionStub(const Function &F, unsigned StubSize) { |
| 180 | SavedCurBlock = CurBlock; SavedCurByte = CurByte; |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 181 | CurByte = CurBlock = MemMgr.allocateStub(StubSize); |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void *Emitter::finishFunctionStub(const Function &F) { |
| 185 | NumBytes += CurByte-CurBlock; |
| 186 | DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex |
| 187 | << (unsigned)(intptr_t)CurBlock |
| 188 | << std::dec << "] Function stub for: " << F.getName() |
| 189 | << ": " << CurByte-CurBlock << " bytes of text\n"); |
| 190 | std::swap(CurBlock, SavedCurBlock); |
| 191 | CurByte = SavedCurByte; |
| 192 | return SavedCurBlock; |
| 193 | } |
| 194 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 195 | void Emitter::emitByte(unsigned char B) { |
| 196 | *CurByte++ = B; // Write the byte to memory |
| 197 | } |
| 198 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 199 | void Emitter::emitWord(unsigned W) { |
Chris Lattner | 688506d | 2003-08-14 18:35:27 +0000 | [diff] [blame] | 200 | // This won't work if the endianness of the host and target don't agree! (For |
| 201 | // a JIT this can't happen though. :) |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 202 | *(unsigned*)CurByte = W; |
| 203 | CurByte += sizeof(unsigned); |
| 204 | } |
| 205 | |
Brian Gaeke | aea1b58 | 2004-04-23 17:11:14 +0000 | [diff] [blame] | 206 | void Emitter::emitWordAt(unsigned W, unsigned *Ptr) { |
| 207 | *Ptr = W; |
| 208 | } |
| 209 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 210 | uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) { |
| 211 | // Try looking up the function to see if it is already compiled, if not return |
| 212 | // 0. |
Chris Lattner | c07ed13 | 2003-12-20 03:36:47 +0000 | [diff] [blame] | 213 | if (isa<Function>(V)) |
| 214 | return (intptr_t)TheJIT->getPointerToGlobalIfAvailable(V); |
| 215 | else { |
| 216 | return (intptr_t)TheJIT->getOrEmitGlobalVariable(cast<GlobalVariable>(V)); |
| 217 | } |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 218 | } |
| 219 | uint64_t Emitter::getGlobalValueAddress(const std::string &Name) { |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 220 | return (intptr_t)TheJIT->getPointerToNamedFunction(Name); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry |
| 224 | // in the constant pool that was last emitted with the 'emitConstantPool' |
| 225 | // method. |
| 226 | // |
| 227 | uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) { |
| 228 | assert(ConstantNum < ConstantPoolAddresses.size() && |
| 229 | "Invalid ConstantPoolIndex!"); |
| 230 | return (intptr_t)ConstantPoolAddresses[ConstantNum]; |
| 231 | } |
| 232 | |
| 233 | // getCurrentPCValue - This returns the address that the next emitted byte |
| 234 | // will be output to. |
| 235 | // |
| 236 | uint64_t Emitter::getCurrentPCValue() { |
| 237 | return (intptr_t)CurByte; |
| 238 | } |
| 239 | |
| 240 | uint64_t Emitter::forceCompilationOf(Function *F) { |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 241 | return (intptr_t)TheJIT->getPointerToFunction(F); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 244 | // getPointerToNamedFunction - This function is used as a global wrapper to |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 245 | // JIT::getPointerToNamedFunction for the purpose of resolving symbols when |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 246 | // bugpoint is debugging the JIT. In that scenario, we are loading an .so and |
| 247 | // need to resolve function(s) that are being mis-codegenerated, so we need to |
| 248 | // resolve their addresses at runtime, and this is the way to do it. |
| 249 | extern "C" { |
| 250 | void *getPointerToNamedFunction(const char *Name) { |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 251 | Module &M = TheJIT->getModule(); |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 252 | if (Function *F = M.getNamedFunction(Name)) |
Chris Lattner | 4d326fa | 2003-12-20 01:46:27 +0000 | [diff] [blame] | 253 | return TheJIT->getPointerToFunction(F); |
| 254 | return TheJIT->getPointerToNamedFunction(Name); |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 255 | } |
| 256 | } |