Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 1 | //===-- SparcEmitter.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" |
| 11 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 12 | #include "llvm/CodeGen/MachineInstr.h" |
| 13 | #include "llvm/Target/TargetData.h" |
| 14 | #include "llvm/Function.h" |
| 15 | #include "Support/Statistic.h" |
| 16 | // FIXME |
| 17 | #include "../../../lib/Target/Sparc/SparcV9CodeEmitter.h" |
| 18 | |
| 19 | namespace { |
| 20 | Statistic<> NumBytes("jello", "Number of bytes of machine code compiled"); |
| 21 | |
| 22 | class SparcEmitter : public MachineCodeEmitter { |
| 23 | VM &TheVM; |
| 24 | |
| 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; |
| 30 | |
| 31 | std::vector<std::pair<BasicBlock*, |
| 32 | std::pair<unsigned*,MachineInstr*> > > BBRefs; |
Misha Brukman | f86d635 | 2003-05-31 06:26:48 +0000 | [diff] [blame] | 33 | std::map<BasicBlock*, long> BBLocations; |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 34 | std::vector<void*> ConstantPoolAddresses; |
| 35 | public: |
| 36 | SparcEmitter(VM &vm) : TheVM(vm) {} |
| 37 | |
| 38 | virtual void startFunction(MachineFunction &F); |
| 39 | virtual void finishFunction(MachineFunction &F); |
| 40 | virtual void emitConstantPool(MachineConstantPool *MCP); |
| 41 | virtual void startBasicBlock(MachineBasicBlock &BB); |
| 42 | virtual void startFunctionStub(const Function &F, unsigned StubSize); |
| 43 | virtual void* finishFunctionStub(const Function &F); |
| 44 | virtual void emitByte(unsigned char B); |
| 45 | virtual void emitPCRelativeDisp(Value *V); |
| 46 | virtual void emitGlobalAddress(GlobalValue *V, bool isPCRelative); |
| 47 | virtual void emitGlobalAddress(const std::string &Name, bool isPCRelative); |
| 48 | virtual void emitFunctionConstantValueAddress(unsigned ConstantNum, |
| 49 | int Offset); |
| 50 | |
| 51 | virtual void saveBBreference(BasicBlock *BB, MachineInstr &MI); |
Misha Brukman | 8e5bf70 | 2003-05-28 18:44:38 +0000 | [diff] [blame] | 52 | |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 53 | |
| 54 | private: |
| 55 | void emitAddress(void *Addr, bool isPCRelative); |
Misha Brukman | 8e5bf70 | 2003-05-28 18:44:38 +0000 | [diff] [blame] | 56 | void* getMemory(unsigned NumPages); |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 57 | }; |
| 58 | } |
| 59 | |
| 60 | MachineCodeEmitter *VM::createSparcEmitter(VM &V) { |
| 61 | return new SparcEmitter(V); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | #define _POSIX_MAPPED_FILES |
| 66 | #include <unistd.h> |
| 67 | #include <sys/mman.h> |
| 68 | |
| 69 | // FIXME: This should be rewritten to support a real memory manager for |
| 70 | // executable memory pages! |
Misha Brukman | 417a7c0 | 2003-05-30 20:39:37 +0000 | [diff] [blame] | 71 | void* SparcEmitter::getMemory(unsigned NumPages) { |
Brian Gaeke | 6607fbe | 2003-05-30 03:37:13 +0000 | [diff] [blame] | 72 | void *pa; |
| 73 | if (NumPages == 0) return 0; |
| 74 | static const long pageSize = sysconf (_SC_PAGESIZE); |
| 75 | pa = mmap(0, pageSize*NumPages, PROT_READ|PROT_WRITE|PROT_EXEC, |
Misha Brukman | 417a7c0 | 2003-05-30 20:39:37 +0000 | [diff] [blame] | 76 | MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
Misha Brukman | 8e5bf70 | 2003-05-28 18:44:38 +0000 | [diff] [blame] | 77 | if (pa == MAP_FAILED) { |
| 78 | perror("mmap"); |
| 79 | abort(); |
| 80 | } |
Misha Brukman | 8e5bf70 | 2003-05-28 18:44:38 +0000 | [diff] [blame] | 81 | return pa; |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | |
| 85 | void SparcEmitter::startFunction(MachineFunction &F) { |
Misha Brukman | 8e5bf70 | 2003-05-28 18:44:38 +0000 | [diff] [blame] | 86 | std::cerr << "Starting function " << F.getFunction()->getName() << "\n"; |
Misha Brukman | 417a7c0 | 2003-05-30 20:39:37 +0000 | [diff] [blame] | 87 | CurBlock = (unsigned char *)getMemory(8); |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 88 | CurByte = CurBlock; // Start writing at the beginning of the fn. |
| 89 | TheVM.addGlobalMapping(F.getFunction(), CurBlock); |
| 90 | } |
| 91 | |
| 92 | void SparcEmitter::finishFunction(MachineFunction &F) { |
Misha Brukman | 417a7c0 | 2003-05-30 20:39:37 +0000 | [diff] [blame] | 93 | std::cerr << "Finishing function " << F.getFunction()->getName() << "\n"; |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 94 | ConstantPoolAddresses.clear(); |
Misha Brukman | 8e5bf70 | 2003-05-28 18:44:38 +0000 | [diff] [blame] | 95 | // Re-write branches to BasicBlocks for the entire function |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 96 | for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) { |
Misha Brukman | f86d635 | 2003-05-31 06:26:48 +0000 | [diff] [blame] | 97 | long Location = BBLocations[BBRefs[i].first]; |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 98 | unsigned *Ref = BBRefs[i].second.first; |
| 99 | MachineInstr *MI = BBRefs[i].second.second; |
Misha Brukman | 417a7c0 | 2003-05-30 20:39:37 +0000 | [diff] [blame] | 100 | std::cerr << "attempting to resolve BB: " << i << "\n"; |
Brian Gaeke | 63b99f9 | 2003-06-01 22:08:29 +0000 | [diff] [blame] | 101 | for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) { |
| 102 | MachineOperand &op = MI->getOperand(ii); |
Misha Brukman | 417a7c0 | 2003-05-30 20:39:37 +0000 | [diff] [blame] | 103 | if (op.isPCRelativeDisp()) { |
Misha Brukman | f86d635 | 2003-05-31 06:26:48 +0000 | [diff] [blame] | 104 | // the instruction's branch target is made such that it branches to |
| 105 | // PC + (br target * 4), so undo that arithmetic here: |
| 106 | // Location is the target of the branch |
| 107 | // Ref is the location of the instruction, and hence the PC |
| 108 | unsigned branchTarget = (Location - (long)Ref) >> 2; |
Brian Gaeke | 24a26e5 | 2003-06-02 02:10:31 +0000 | [diff] [blame] | 109 | // Save the flags. |
| 110 | bool loBits32=false, hiBits32=false, loBits64=false, hiBits64=false; |
| 111 | if (op.opLoBits32()) { loBits32=true; } |
| 112 | if (op.opHiBits32()) { hiBits32=true; } |
| 113 | if (op.opLoBits64()) { loBits64=true; } |
| 114 | if (op.opHiBits64()) { hiBits64=true; } |
Brian Gaeke | 63b99f9 | 2003-06-01 22:08:29 +0000 | [diff] [blame] | 115 | MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed, |
Misha Brukman | f86d635 | 2003-05-31 06:26:48 +0000 | [diff] [blame] | 116 | branchTarget); |
Brian Gaeke | 24a26e5 | 2003-06-02 02:10:31 +0000 | [diff] [blame] | 117 | if (loBits32) { MI->setOperandLo32(ii); } |
| 118 | else if (hiBits32) { MI->setOperandHi32(ii); } |
| 119 | else if (loBits64) { MI->setOperandLo64(ii); } |
| 120 | else if (hiBits64) { MI->setOperandHi64(ii); } |
Misha Brukman | 417a7c0 | 2003-05-30 20:39:37 +0000 | [diff] [blame] | 121 | std::cerr << "Rewrote BB ref: "; |
| 122 | unsigned fixedInstr = SparcV9CodeEmitter::getBinaryCodeForInstr(*MI); |
| 123 | *Ref = fixedInstr; |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 124 | break; |
| 125 | } |
| 126 | } |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 127 | } |
| 128 | BBRefs.clear(); |
| 129 | BBLocations.clear(); |
| 130 | |
| 131 | NumBytes += CurByte-CurBlock; |
| 132 | |
| 133 | DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex |
| 134 | << (unsigned)(intptr_t)CurBlock |
| 135 | << std::dec << "] Function: " << F.getFunction()->getName() |
| 136 | << ": " << CurByte-CurBlock << " bytes of text\n"); |
| 137 | } |
| 138 | |
| 139 | void SparcEmitter::emitConstantPool(MachineConstantPool *MCP) { |
| 140 | const std::vector<Constant*> &Constants = MCP->getConstants(); |
| 141 | for (unsigned i = 0, e = Constants.size(); i != e; ++i) { |
| 142 | // For now we just allocate some memory on the heap, this can be |
| 143 | // dramatically improved. |
| 144 | const Type *Ty = ((Value*)Constants[i])->getType(); |
| 145 | void *Addr = malloc(TheVM.getTargetData().getTypeSize(Ty)); |
| 146 | TheVM.InitializeMemory(Constants[i], Addr); |
| 147 | ConstantPoolAddresses.push_back(Addr); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void SparcEmitter::startBasicBlock(MachineBasicBlock &BB) { |
Misha Brukman | f86d635 | 2003-05-31 06:26:48 +0000 | [diff] [blame] | 153 | BBLocations[BB.getBasicBlock()] = (long)(intptr_t)CurByte; |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
| 157 | void SparcEmitter::startFunctionStub(const Function &F, unsigned StubSize) { |
| 158 | SavedCurBlock = CurBlock; SavedCurByte = CurByte; |
| 159 | // FIXME: this is a huge waste of memory. |
| 160 | CurBlock = (unsigned char *)getMemory((StubSize+4095)/4096); |
| 161 | CurByte = CurBlock; // Start writing at the beginning of the fn. |
| 162 | } |
| 163 | |
| 164 | void *SparcEmitter::finishFunctionStub(const Function &F) { |
| 165 | NumBytes += CurByte-CurBlock; |
| 166 | DEBUG(std::cerr << "Finished CodeGen of [0x" << std::hex |
| 167 | << (unsigned)(intptr_t)CurBlock |
| 168 | << std::dec << "] Function stub for: " << F.getName() |
| 169 | << ": " << CurByte-CurBlock << " bytes of text\n"); |
| 170 | std::swap(CurBlock, SavedCurBlock); |
| 171 | CurByte = SavedCurByte; |
| 172 | return SavedCurBlock; |
| 173 | } |
| 174 | |
| 175 | void SparcEmitter::emitByte(unsigned char B) { |
| 176 | *CurByte++ = B; // Write the byte to memory |
| 177 | } |
| 178 | |
| 179 | // BasicBlock -> pair<memloc, MachineInstr> |
| 180 | // when the BB is emitted, machineinstr is modified with then-currbyte, |
| 181 | // processed with MCE, and written out at memloc. |
Misha Brukman | 8e5bf70 | 2003-05-28 18:44:38 +0000 | [diff] [blame] | 182 | // Should be called by the emitter if its outputting a PCRelative disp |
Misha Brukman | abb027c | 2003-05-27 21:40:39 +0000 | [diff] [blame] | 183 | void SparcEmitter::saveBBreference(BasicBlock *BB, MachineInstr &MI) { |
| 184 | BBRefs.push_back(std::make_pair(BB, std::make_pair((unsigned*)CurByte, &MI))); |
| 185 | } |
| 186 | |
| 187 | |
| 188 | // emitPCRelativeDisp - For functions, just output a displacement that will |
| 189 | // cause a reference to the zero page, which will cause a seg-fault, causing |
| 190 | // things to get resolved on demand. Keep track of these markers. |
| 191 | // |
| 192 | // For basic block references, keep track of where the references are so they |
| 193 | // may be patched up when the basic block is defined. |
| 194 | // |
| 195 | // BasicBlock -> pair<memloc, MachineInstr> |
| 196 | // when the BB is emitted, machineinstr is modified with then-currbyte, |
| 197 | // processed with MCE, and written out at memloc. |
| 198 | |
| 199 | void SparcEmitter::emitPCRelativeDisp(Value *V) { |
| 200 | #if 0 |
| 201 | BasicBlock *BB = cast<BasicBlock>(V); // Keep track of reference... |
| 202 | BBRefs.push_back(std::make_pair(BB, (unsigned*)CurByte)); |
| 203 | CurByte += 4; |
| 204 | #endif |
| 205 | } |
| 206 | |
| 207 | // emitAddress - Emit an address in either direct or PCRelative form... |
| 208 | // |
| 209 | void SparcEmitter::emitAddress(void *Addr, bool isPCRelative) { |
| 210 | #if 0 |
| 211 | if (isPCRelative) { |
| 212 | *(intptr_t*)CurByte = (intptr_t)Addr - (intptr_t)CurByte-4; |
| 213 | } else { |
| 214 | *(void**)CurByte = Addr; |
| 215 | } |
| 216 | CurByte += 4; |
| 217 | #endif |
| 218 | } |
| 219 | |
| 220 | void SparcEmitter::emitGlobalAddress(GlobalValue *V, bool isPCRelative) { |
| 221 | if (isPCRelative) { // must be a call, this is a major hack! |
| 222 | // Try looking up the function to see if it is already compiled! |
| 223 | if (void *Addr = TheVM.getPointerToGlobalIfAvailable(V)) { |
| 224 | emitAddress(Addr, isPCRelative); |
| 225 | } else { // Function has not yet been code generated! |
| 226 | TheVM.addFunctionRef(CurByte, cast<Function>(V)); |
| 227 | |
| 228 | // Delayed resolution... |
| 229 | emitAddress((void*)VM::CompilationCallback, isPCRelative); |
| 230 | } |
| 231 | } else { |
| 232 | emitAddress(TheVM.getPointerToGlobal(V), isPCRelative); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | void SparcEmitter::emitGlobalAddress(const std::string &Name, bool isPCRelative) |
| 237 | { |
| 238 | #if 0 |
| 239 | emitAddress(TheVM.getPointerToNamedFunction(Name), isPCRelative); |
| 240 | #endif |
| 241 | } |
| 242 | |
| 243 | void SparcEmitter::emitFunctionConstantValueAddress(unsigned ConstantNum, |
| 244 | int Offset) { |
| 245 | assert(ConstantNum < ConstantPoolAddresses.size() && |
| 246 | "Invalid ConstantPoolIndex!"); |
| 247 | *(void**)CurByte = (char*)ConstantPoolAddresses[ConstantNum]+Offset; |
| 248 | CurByte += 4; |
| 249 | } |