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