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" |
| 15 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame^] | 16 | static VM *TheVM = 0; |
| 17 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 18 | namespace { |
| 19 | Statistic<> NumBytes("jello", "Number of bytes of machine code compiled"); |
| 20 | |
| 21 | class Emitter : public MachineCodeEmitter { |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame^] | 22 | // CurBlock - The start of the current block of memory. CurByte - The |
| 23 | // current byte being emitted to. |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 24 | unsigned char *CurBlock, *CurByte; |
| 25 | |
| 26 | // When outputting a function stub in the context of some other function, we |
| 27 | // save CurBlock and CurByte here. |
| 28 | unsigned char *SavedCurBlock, *SavedCurByte; |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame^] | 29 | |
| 30 | // ConstantPoolAddresses - Contains the location for each entry in the |
| 31 | // constant pool. |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 32 | std::vector<void*> ConstantPoolAddresses; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 33 | public: |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame^] | 34 | Emitter(VM &vm) { TheVM = &vm; } |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 35 | |
| 36 | virtual void startFunction(MachineFunction &F); |
| 37 | virtual void finishFunction(MachineFunction &F); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 38 | virtual void emitConstantPool(MachineConstantPool *MCP); |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 39 | virtual void startFunctionStub(const Function &F, unsigned StubSize); |
| 40 | virtual void* finishFunctionStub(const Function &F); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 41 | virtual void emitByte(unsigned char B); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame^] | 42 | virtual void emitWord(unsigned W); |
| 43 | |
| 44 | virtual uint64_t getGlobalValueAddress(GlobalValue *V); |
| 45 | virtual uint64_t getGlobalValueAddress(const std::string &Name); |
| 46 | virtual uint64_t getConstantPoolEntryAddress(unsigned Entry); |
| 47 | virtual uint64_t getCurrentPCValue(); |
| 48 | |
| 49 | // forceCompilationOf - Force the compilation of the specified function, and |
| 50 | // return its address, because we REALLY need the address now. |
| 51 | // |
| 52 | // FIXME: This is JIT specific! |
| 53 | // |
| 54 | virtual uint64_t forceCompilationOf(Function *F); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 55 | }; |
| 56 | } |
| 57 | |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 58 | MachineCodeEmitter *VM::createX86Emitter(VM &V) { |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 59 | return new Emitter(V); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | #define _POSIX_MAPPED_FILES |
| 64 | #include <unistd.h> |
| 65 | #include <sys/mman.h> |
| 66 | |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 67 | // FIXME: This should be rewritten to support a real memory manager for |
| 68 | // executable memory pages! |
| 69 | static void *getMemory(unsigned NumPages) { |
| 70 | return mmap(0, 4096*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC, |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 71 | MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | void Emitter::startFunction(MachineFunction &F) { |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame^] | 76 | CurBlock = (unsigned char *)getMemory(16); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 77 | CurByte = CurBlock; // Start writing at the beginning of the fn. |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame^] | 78 | TheVM->addGlobalMapping(F.getFunction(), CurBlock); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void Emitter::finishFunction(MachineFunction &F) { |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 82 | ConstantPoolAddresses.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 83 | NumBytes += CurByte-CurBlock; |
| 84 | |
Chris Lattner | 910687e | 2003-01-29 18:02:02 +0000 | [diff] [blame] | 85 | DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex |
| 86 | << (unsigned)(intptr_t)CurBlock |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 87 | << std::dec << "] Function: " << F.getFunction()->getName() |
| 88 | << ": " << CurByte-CurBlock << " bytes of text\n"); |
| 89 | } |
| 90 | |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 91 | void Emitter::emitConstantPool(MachineConstantPool *MCP) { |
| 92 | const std::vector<Constant*> &Constants = MCP->getConstants(); |
| 93 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
| 94 | // For now we just allocate some memory on the heap, this can be |
| 95 | // dramatically improved. |
| 96 | const Type *Ty = ((Value*)Constants[i])->getType(); |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame^] | 97 | void *Addr = malloc(TheVM->getTargetData().getTypeSize(Ty)); |
| 98 | TheVM->InitializeMemory(Constants[i], Addr); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 99 | ConstantPoolAddresses.push_back(Addr); |
| 100 | } |
| 101 | } |
| 102 | |
Chris Lattner | 6125fdd | 2003-05-09 03:30:07 +0000 | [diff] [blame] | 103 | void Emitter::startFunctionStub(const Function &F, unsigned StubSize) { |
| 104 | SavedCurBlock = CurBlock; SavedCurByte = CurByte; |
| 105 | // FIXME: this is a huge waste of memory. |
| 106 | CurBlock = (unsigned char *)getMemory((StubSize+4095)/4096); |
| 107 | CurByte = CurBlock; // Start writing at the beginning of the fn. |
| 108 | } |
| 109 | |
| 110 | void *Emitter::finishFunctionStub(const Function &F) { |
| 111 | NumBytes += CurByte-CurBlock; |
| 112 | DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex |
| 113 | << (unsigned)(intptr_t)CurBlock |
| 114 | << std::dec << "] Function stub for: " << F.getName() |
| 115 | << ": " << CurByte-CurBlock << " bytes of text\n"); |
| 116 | std::swap(CurBlock, SavedCurBlock); |
| 117 | CurByte = SavedCurByte; |
| 118 | return SavedCurBlock; |
| 119 | } |
| 120 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 121 | void Emitter::emitByte(unsigned char B) { |
| 122 | *CurByte++ = B; // Write the byte to memory |
| 123 | } |
| 124 | |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame^] | 125 | void Emitter::emitWord(unsigned W) { |
| 126 | // FIXME: This won't work if the endianness of the host and target don't |
| 127 | // agree! (For a JIT this can't happen though. :) |
| 128 | *(unsigned*)CurByte = W; |
| 129 | CurByte += sizeof(unsigned); |
| 130 | } |
| 131 | |
| 132 | |
| 133 | uint64_t Emitter::getGlobalValueAddress(GlobalValue *V) { |
| 134 | // Try looking up the function to see if it is already compiled, if not return |
| 135 | // 0. |
| 136 | return (intptr_t)TheVM->getPointerToGlobalIfAvailable(V); |
| 137 | } |
| 138 | uint64_t Emitter::getGlobalValueAddress(const std::string &Name) { |
| 139 | return (intptr_t)TheVM->getPointerToNamedFunction(Name); |
| 140 | } |
| 141 | |
| 142 | // getConstantPoolEntryAddress - Return the address of the 'ConstantNum' entry |
| 143 | // in the constant pool that was last emitted with the 'emitConstantPool' |
| 144 | // method. |
| 145 | // |
| 146 | uint64_t Emitter::getConstantPoolEntryAddress(unsigned ConstantNum) { |
| 147 | assert(ConstantNum < ConstantPoolAddresses.size() && |
| 148 | "Invalid ConstantPoolIndex!"); |
| 149 | return (intptr_t)ConstantPoolAddresses[ConstantNum]; |
| 150 | } |
| 151 | |
| 152 | // getCurrentPCValue - This returns the address that the next emitted byte |
| 153 | // will be output to. |
| 154 | // |
| 155 | uint64_t Emitter::getCurrentPCValue() { |
| 156 | return (intptr_t)CurByte; |
| 157 | } |
| 158 | |
| 159 | uint64_t Emitter::forceCompilationOf(Function *F) { |
| 160 | return (intptr_t)TheVM->getPointerToFunction(F); |
| 161 | } |
| 162 | |
| 163 | #if 0 |
| 164 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 165 | |
| 166 | // emitPCRelativeDisp - For functions, just output a displacement that will |
| 167 | // cause a reference to the zero page, which will cause a seg-fault, causing |
| 168 | // things to get resolved on demand. Keep track of these markers. |
| 169 | // |
| 170 | // For basic block references, keep track of where the references are so they |
| 171 | // may be patched up when the basic block is defined. |
| 172 | // |
| 173 | void Emitter::emitPCRelativeDisp(Value *V) { |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 174 | BasicBlock *BB = cast<BasicBlock>(V); // Keep track of reference... |
| 175 | BBRefs.push_back(std::make_pair(BB, (unsigned*)CurByte)); |
| 176 | CurByte += 4; |
| 177 | } |
| 178 | |
| 179 | // emitAddress - Emit an address in either direct or PCRelative form... |
| 180 | // |
| 181 | void Emitter::emitAddress(void *Addr, bool isPCRelative) { |
| 182 | if (isPCRelative) { |
Chris Lattner | 910687e | 2003-01-29 18:02:02 +0000 | [diff] [blame] | 183 | *(intptr_t*)CurByte = (intptr_t)Addr - (intptr_t)CurByte-4; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 184 | } else { |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 185 | *(void**)CurByte = Addr; |
| 186 | } |
| 187 | CurByte += 4; |
| 188 | } |
| 189 | |
| 190 | void Emitter::emitGlobalAddress(GlobalValue *V, bool isPCRelative) { |
| 191 | if (isPCRelative) { // must be a call, this is a major hack! |
Chris Lattner | eb5a93b | 2003-05-08 21:44:21 +0000 | [diff] [blame] | 192 | // Try looking up the function to see if it is already compiled! |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame^] | 193 | if (void *Addr = TheVM->getPointerToGlobalIfAvailable(V)) { |
Chris Lattner | eb5a93b | 2003-05-08 21:44:21 +0000 | [diff] [blame] | 194 | emitAddress(Addr, isPCRelative); |
| 195 | } else { // Function has not yet been code generated! |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame^] | 196 | TheVM->addFunctionRef(CurByte, cast<Function>(V)); |
Chris Lattner | c309a76 | 2003-05-08 21:34:11 +0000 | [diff] [blame] | 197 | |
Chris Lattner | eb5a93b | 2003-05-08 21:44:21 +0000 | [diff] [blame] | 198 | // Delayed resolution... |
| 199 | emitAddress((void*)VM::CompilationCallback, isPCRelative); |
| 200 | } |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 201 | } else { |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame^] | 202 | emitAddress(TheVM->getPointerToGlobal(V), isPCRelative); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 206 | void Emitter::emitFunctionConstantValueAddress(unsigned ConstantNum, |
| 207 | int Offset) { |
| 208 | assert(ConstantNum < ConstantPoolAddresses.size() && |
| 209 | "Invalid ConstantPoolIndex!"); |
| 210 | *(void**)CurByte = (char*)ConstantPoolAddresses[ConstantNum]+Offset; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 211 | CurByte += 4; |
| 212 | } |
Chris Lattner | bba1b6d | 2003-06-01 23:24:36 +0000 | [diff] [blame^] | 213 | #endif |