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