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 | |
| 16 | namespace { |
| 17 | Statistic<> NumBytes("jello", "Number of bytes of machine code compiled"); |
| 18 | |
| 19 | class Emitter : public MachineCodeEmitter { |
| 20 | VM &TheVM; |
| 21 | |
| 22 | unsigned char *CurBlock; |
| 23 | unsigned char *CurByte; |
| 24 | |
| 25 | std::vector<std::pair<BasicBlock*, unsigned *> > BBRefs; |
| 26 | std::map<BasicBlock*, unsigned> BBLocations; |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 27 | std::vector<void*> ConstantPoolAddresses; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 28 | public: |
| 29 | Emitter(VM &vm) : TheVM(vm) {} |
| 30 | |
| 31 | virtual void startFunction(MachineFunction &F); |
| 32 | virtual void finishFunction(MachineFunction &F); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 33 | virtual void emitConstantPool(MachineConstantPool *MCP); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 34 | virtual void startBasicBlock(MachineBasicBlock &BB); |
| 35 | virtual void emitByte(unsigned char B); |
| 36 | virtual void emitPCRelativeDisp(Value *V); |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 37 | virtual void emitGlobalAddress(GlobalValue *V, bool isPCRelative); |
| 38 | virtual void emitGlobalAddress(const std::string &Name, bool isPCRelative); |
| 39 | virtual void emitFunctionConstantValueAddress(unsigned ConstantNum, |
| 40 | int Offset); |
| 41 | private: |
| 42 | void emitAddress(void *Addr, bool isPCRelative); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 43 | }; |
| 44 | } |
| 45 | |
| 46 | MachineCodeEmitter *VM::createEmitter(VM &V) { |
| 47 | return new Emitter(V); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | #define _POSIX_MAPPED_FILES |
| 52 | #include <unistd.h> |
| 53 | #include <sys/mman.h> |
| 54 | |
| 55 | static void *getMemory() { |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 56 | return mmap(0, 4096*8, PROT_READ|PROT_WRITE|PROT_EXEC, |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 57 | MAP_PRIVATE|MAP_ANONYMOUS, 0, 0); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | void Emitter::startFunction(MachineFunction &F) { |
| 62 | CurBlock = (unsigned char *)getMemory(); |
| 63 | CurByte = CurBlock; // Start writing at the beginning of the fn. |
| 64 | TheVM.addGlobalMapping(F.getFunction(), CurBlock); |
| 65 | } |
| 66 | |
| 67 | void Emitter::finishFunction(MachineFunction &F) { |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 68 | ConstantPoolAddresses.clear(); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 69 | for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) { |
| 70 | unsigned Location = BBLocations[BBRefs[i].first]; |
| 71 | unsigned *Ref = BBRefs[i].second; |
| 72 | *Ref = Location-(unsigned)Ref-4; |
| 73 | } |
| 74 | BBRefs.clear(); |
| 75 | BBLocations.clear(); |
| 76 | |
| 77 | NumBytes += CurByte-CurBlock; |
| 78 | |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 79 | DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex << (unsigned)CurBlock |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 80 | << std::dec << "] Function: " << F.getFunction()->getName() |
| 81 | << ": " << CurByte-CurBlock << " bytes of text\n"); |
| 82 | } |
| 83 | |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 84 | void Emitter::emitConstantPool(MachineConstantPool *MCP) { |
| 85 | const std::vector<Constant*> &Constants = MCP->getConstants(); |
| 86 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
| 87 | // For now we just allocate some memory on the heap, this can be |
| 88 | // dramatically improved. |
| 89 | const Type *Ty = ((Value*)Constants[i])->getType(); |
| 90 | void *Addr = malloc(TheVM.getTargetData().getTypeSize(Ty)); |
| 91 | TheVM.InitializeMemory(Constants[i], Addr); |
| 92 | ConstantPoolAddresses.push_back(Addr); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 97 | void Emitter::startBasicBlock(MachineBasicBlock &BB) { |
| 98 | BBLocations[BB.getBasicBlock()] = (unsigned)CurByte; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | void Emitter::emitByte(unsigned char B) { |
| 103 | *CurByte++ = B; // Write the byte to memory |
| 104 | } |
| 105 | |
| 106 | |
| 107 | // emitPCRelativeDisp - For functions, just output a displacement that will |
| 108 | // cause a reference to the zero page, which will cause a seg-fault, causing |
| 109 | // things to get resolved on demand. Keep track of these markers. |
| 110 | // |
| 111 | // For basic block references, keep track of where the references are so they |
| 112 | // may be patched up when the basic block is defined. |
| 113 | // |
| 114 | void Emitter::emitPCRelativeDisp(Value *V) { |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 115 | BasicBlock *BB = cast<BasicBlock>(V); // Keep track of reference... |
| 116 | BBRefs.push_back(std::make_pair(BB, (unsigned*)CurByte)); |
| 117 | CurByte += 4; |
| 118 | } |
| 119 | |
| 120 | // emitAddress - Emit an address in either direct or PCRelative form... |
| 121 | // |
| 122 | void Emitter::emitAddress(void *Addr, bool isPCRelative) { |
| 123 | if (isPCRelative) { |
| 124 | *(unsigned*)CurByte = (unsigned)Addr - (unsigned)CurByte-4; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 125 | } else { |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 126 | *(void**)CurByte = Addr; |
| 127 | } |
| 128 | CurByte += 4; |
| 129 | } |
| 130 | |
| 131 | void Emitter::emitGlobalAddress(GlobalValue *V, bool isPCRelative) { |
| 132 | if (isPCRelative) { // must be a call, this is a major hack! |
| 133 | TheVM.addFunctionRef(CurByte, cast<Function>(V)); |
| 134 | emitAddress(0, isPCRelative); // Delayed resolution... |
| 135 | } else { |
| 136 | emitAddress(TheVM.getPointerToGlobal(V), isPCRelative); |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Chris Lattner | 1cc0838 | 2003-01-13 01:00:12 +0000 | [diff] [blame] | 140 | void Emitter::emitGlobalAddress(const std::string &Name, bool isPCRelative) { |
| 141 | emitAddress(TheVM.getPointerToNamedFunction(Name), isPCRelative); |
| 142 | } |
| 143 | |
| 144 | void Emitter::emitFunctionConstantValueAddress(unsigned ConstantNum, |
| 145 | int Offset) { |
| 146 | assert(ConstantNum < ConstantPoolAddresses.size() && |
| 147 | "Invalid ConstantPoolIndex!"); |
| 148 | *(void**)CurByte = (char*)ConstantPoolAddresses[ConstantNum]+Offset; |
Chris Lattner | bd199fb | 2002-12-24 00:01:05 +0000 | [diff] [blame] | 149 | CurByte += 4; |
| 150 | } |