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