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 | |
| 24 | class Emitter : public MachineCodeEmitter { |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 25 | // CurBlock - The start of the current block of memory. CurByte - The |
| 26 | // current byte being emitted to. |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 27 | unsigned char *CurBlock, *CurByte; |
| 28 | |
| 29 | // When outputting a function stub in the context of some other function, we |
| 30 | // save CurBlock and CurByte here. |
| 31 | unsigned char *SavedCurBlock, *SavedCurByte; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 32 | |
| 33 | // ConstantPoolAddresses - Contains the location for each entry in the |
| 34 | // constant pool. |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 35 | std::vector<void*> ConstantPoolAddresses; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 36 | public: |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 37 | Emitter(VM &vm) { TheVM = &vm; } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 38 | |
| 39 | virtual void startFunction(MachineFunction &F); |
| 40 | virtual void finishFunction(MachineFunction &F); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 41 | virtual void emitConstantPool(MachineConstantPool *MCP); |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 42 | virtual void startFunctionStub(const Function &F, unsigned StubSize); |
| 43 | virtual void* finishFunctionStub(const Function &F); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 44 | virtual void emitByte(unsigned char B); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 45 | virtual void emitWord(unsigned W); |
| 46 | |
| 47 | virtual uint64_t getGlobalValueAddress(GlobalValue *V); |
| 48 | virtual uint64_t getGlobalValueAddress(const std::string &Name); |
| 49 | virtual uint64_t getConstantPoolEntryAddress(unsigned Entry); |
| 50 | virtual uint64_t getCurrentPCValue(); |
| 51 | |
| 52 | // forceCompilationOf - Force the compilation of the specified function, and |
| 53 | // return its address, because we REALLY need the address now. |
| 54 | // |
| 55 | // FIXME: This is JIT specific! |
| 56 | // |
| 57 | virtual uint64_t forceCompilationOf(Function *F); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 58 | }; |
| 59 | } |
| 60 | |
Misha Brukman | 906f5fa | 2003-06-02 03:23:16 +0000 | [diff] [blame] | 61 | MachineCodeEmitter *VM::createEmitter(VM &V) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 62 | return new Emitter(V); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | #define _POSIX_MAPPED_FILES |
| 67 | #include <unistd.h> |
| 68 | #include <sys/mman.h> |
| 69 | |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 70 | // FIXME: This should be rewritten to support a real memory manager for |
| 71 | // executable memory pages! |
| 72 | static void *getMemory(unsigned NumPages) { |
Misha Brukman | 906f5fa | 2003-06-02 03:23:16 +0000 | [diff] [blame] | 73 | void *pa; |
| 74 | if (NumPages == 0) return 0; |
Misha Brukman | 3a55e8a | 2003-06-04 19:45:25 +0000 | [diff] [blame] | 75 | static const long pageSize = sysconf(_SC_PAGESIZE); |
| 76 | |
| 77 | #if defined(i386) || defined(__i386__) || defined(__x86__) |
Misha Brukman | fd31a78 | 2003-07-28 19:26:19 +0000 | [diff] [blame] | 78 | /* Linux and *BSD tend to have these flags named differently. */ |
Brian Gaeke | 4399a49 | 2003-06-17 23:14:06 +0000 | [diff] [blame] | 79 | #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS) |
| 80 | # define MAP_ANONYMOUS MAP_ANON |
Misha Brukman | 89e8be0 | 2003-07-29 16:57:16 +0000 | [diff] [blame] | 81 | #endif /* defined(MAP_ANON) && !defined(MAP_ANONYMOUS) */ |
| 82 | #define fd 0 |
Misha Brukman | 3a55e8a | 2003-06-04 19:45:25 +0000 | [diff] [blame] | 83 | #elif defined(sparc) || defined(__sparc__) || defined(__sparcv9) |
Misha Brukman | 89e8be0 | 2003-07-29 16:57:16 +0000 | [diff] [blame] | 84 | #define fd -1 |
Misha Brukman | 3a55e8a | 2003-06-04 19:45:25 +0000 | [diff] [blame] | 85 | #else |
Misha Brukman | 89e8be0 | 2003-07-29 16:57:16 +0000 | [diff] [blame] | 86 | std::cerr << "This architecture is not supported by the JIT!\n"; |
Misha Brukman | 3a55e8a | 2003-06-04 19:45:25 +0000 | [diff] [blame] | 87 | abort(); |
| 88 | #endif |
Misha Brukman | 89e8be0 | 2003-07-29 16:57:16 +0000 | [diff] [blame] | 89 | pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC, |
| 90 | MAP_PRIVATE|MAP_ANONYMOUS, fd, 0); |
Misha Brukman | 906f5fa | 2003-06-02 03:23:16 +0000 | [diff] [blame] | 91 | if (pa == MAP_FAILED) { |
| 92 | perror("mmap"); |
| 93 | abort(); |
| 94 | } |
| 95 | return pa; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | |
| 99 | void Emitter::startFunction(MachineFunction &F) { |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 100 | CurBlock = (unsigned char *)getMemory(16); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 101 | CurByte = CurBlock; // Start writing at the beginning of the fn. |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 102 | TheVM->addGlobalMapping(F.getFunction(), CurBlock); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | void Emitter::finishFunction(MachineFunction &F) { |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 106 | ConstantPoolAddresses.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 107 | NumBytes += CurByte-CurBlock; |
| 108 | |
Brian Gaeke | 02c26b6 | 2003-06-30 18:06:20 +0000 | [diff] [blame] | 109 | DEBUG(std::cerr << "Finished CodeGen of [" << (void*)CurBlock |
Misha Brukman | 1d44085 | 2003-06-06 06:52:35 +0000 | [diff] [blame] | 110 | << "] Function: " << F.getFunction()->getName() |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 111 | << ": " << CurByte-CurBlock << " bytes of text\n"); |
| 112 | } |
| 113 | |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 114 | void Emitter::emitConstantPool(MachineConstantPool *MCP) { |
| 115 | const std::vector<Constant*> &Constants = MCP->getConstants(); |
| 116 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
| 117 | // For now we just allocate some memory on the heap, this can be |
| 118 | // dramatically improved. |
| 119 | const Type *Ty = ((Value*)Constants[i])->getType(); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 120 | void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty)); |
| 121 | TheVM->InitializeMemory(Constants[i], Addr); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 122 | ConstantPoolAddresses.push_back(Addr); |
| 123 | } |
| 124 | } |
| 125 | |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 126 | void Emitter::startFunctionStub(const Function &F, unsigned StubSize) { |
Misha Brukman | 3a55e8a | 2003-06-04 19:45:25 +0000 | [diff] [blame] | 127 | static const long pageSize = sysconf(_SC_PAGESIZE); |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 128 | SavedCurBlock = CurBlock; SavedCurByte = CurByte; |
| 129 | // FIXME: this is a huge waste of memory. |
Misha Brukman | 3a55e8a | 2003-06-04 19:45:25 +0000 | [diff] [blame] | 130 | CurBlock = (unsigned char *)getMemory((StubSize+pageSize-1)/pageSize); |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 131 | CurByte = CurBlock; // Start writing at the beginning of the fn. |
| 132 | } |
| 133 | |
| 134 | void *Emitter::finishFunctionStub(const Function &F) { |
| 135 | NumBytes += CurByte-CurBlock; |
| 136 | DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex |
| 137 | << (unsigned)(intptr_t)CurBlock |
| 138 | << std::dec << "] Function stub for: " << F.getName() |
| 139 | << ": " << CurByte-CurBlock << " bytes of text\n"); |
| 140 | std::swap(CurBlock, SavedCurBlock); |
| 141 | CurByte = SavedCurByte; |
| 142 | return SavedCurBlock; |
| 143 | } |
| 144 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 145 | void Emitter::emitByte(unsigned char B) { |
| 146 | *CurByte++ = B; // Write the byte to memory |
| 147 | } |
| 148 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame] | 149 | void Emitter::emitWord(unsigned W) { |
| 150 | // FIXME: This won't work if the endianness of the host and target don't |
| 151 | // agree! (For a JIT this can't happen though. :) |
| 152 | *(unsigned*)CurByte = W; |
| 153 | CurByte += sizeof(unsigned); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) { |
| 158 | // Try looking up the function to see if it is already compiled, if not return |
| 159 | // 0. |
| 160 | return (intptr_t)TheVM->getPointerToGlobalIfAvailable(V); |
| 161 | } |
| 162 | uint64_t Emitter::getGlobalValueAddress(const std::string &Name) { |
| 163 | return (intptr_t)TheVM->getPointerToNamedFunction(Name); |
| 164 | } |
| 165 | |
| 166 | // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry |
| 167 | // in the constant pool that was last emitted with the 'emitConstantPool' |
| 168 | // method. |
| 169 | // |
| 170 | uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) { |
| 171 | assert(ConstantNum < ConstantPoolAddresses.size() && |
| 172 | "Invalid ConstantPoolIndex!"); |
| 173 | return (intptr_t)ConstantPoolAddresses[ConstantNum]; |
| 174 | } |
| 175 | |
| 176 | // getCurrentPCValue - This returns the address that the next emitted byte |
| 177 | // will be output to. |
| 178 | // |
| 179 | uint64_t Emitter::getCurrentPCValue() { |
| 180 | return (intptr_t)CurByte; |
| 181 | } |
| 182 | |
| 183 | uint64_t Emitter::forceCompilationOf(Function *F) { |
| 184 | return (intptr_t)TheVM->getPointerToFunction(F); |
| 185 | } |
| 186 | |
Misha Brukman | d69c1e6 | 2003-07-28 19:09:06 +0000 | [diff] [blame] | 187 | // getPointerToNamedFunction - This function is used as a global wrapper to |
| 188 | // VM::getPointerToNamedFunction for the purpose of resolving symbols when |
| 189 | // bugpoint is debugging the JIT. In that scenario, we are loading an .so and |
| 190 | // need to resolve function(s) that are being mis-codegenerated, so we need to |
| 191 | // resolve their addresses at runtime, and this is the way to do it. |
| 192 | extern "C" { |
| 193 | void *getPointerToNamedFunction(const char *Name) { |
| 194 | Module &M = TheVM->getModule(); |
| 195 | if (Function *F = M.getNamedFunction(Name)) |
| 196 | return TheVM->getPointerToFunction(F); |
| 197 | return TheVM->getPointerToNamedFunction(Name); |
| 198 | } |
| 199 | } |