Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 1 | //===-- PPC32CodeEmitter.cpp - JIT Code Emitter for PowerPC32 -----*- C++ -*-=// |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 10 | // This file defines the PowerPC 32-bit CodeEmitter and associated machinery to |
| 11 | // JIT-compile bytecode to native PowerPC. |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 15 | #include "PPC32JITInfo.h" |
| 16 | #include "PPC32TargetMachine.h" |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 17 | #include "llvm/Module.h" |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 20 | #include "llvm/CodeGen/Passes.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Debug.h" |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
| 24 | |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 25 | namespace { |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 26 | class JITResolver { |
| 27 | MachineCodeEmitter &MCE; |
| 28 | |
| 29 | // LazyCodeGenMap - Keep track of call sites for functions that are to be |
| 30 | // lazily resolved. |
| 31 | std::map<unsigned, Function*> LazyCodeGenMap; |
| 32 | |
| 33 | // LazyResolverMap - Keep track of the lazy resolver created for a |
| 34 | // particular function so that we can reuse them if necessary. |
| 35 | std::map<Function*, unsigned> LazyResolverMap; |
| 36 | |
| 37 | public: |
| 38 | JITResolver(MachineCodeEmitter &mce) : MCE(mce) {} |
| 39 | unsigned getLazyResolver(Function *F); |
| 40 | unsigned addFunctionReference(unsigned Address, Function *F); |
| 41 | |
| 42 | private: |
| 43 | unsigned emitStubForFunction(Function *F); |
| 44 | static void CompilationCallback(); |
| 45 | unsigned resolveFunctionReference(unsigned RetAddr); |
| 46 | }; |
| 47 | |
| 48 | static JITResolver &getResolver(MachineCodeEmitter &MCE) { |
| 49 | static JITResolver *TheJITResolver = 0; |
| 50 | if (TheJITResolver == 0) |
| 51 | TheJITResolver = new JITResolver(MCE); |
| 52 | return *TheJITResolver; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | unsigned JITResolver::getLazyResolver(Function *F) { |
| 57 | std::map<Function*, unsigned>::iterator I = LazyResolverMap.lower_bound(F); |
| 58 | if (I != LazyResolverMap.end() && I->first == F) return I->second; |
| 59 | |
| 60 | unsigned Stub = emitStubForFunction(F); |
| 61 | LazyResolverMap.insert(I, std::make_pair(F, Stub)); |
| 62 | return Stub; |
| 63 | } |
| 64 | |
| 65 | /// addFunctionReference - This method is called when we need to emit the |
| 66 | /// address of a function that has not yet been emitted, so we don't know the |
| 67 | /// address. Instead, we emit a call to the CompilationCallback method, and |
| 68 | /// keep track of where we are. |
| 69 | /// |
| 70 | unsigned JITResolver::addFunctionReference(unsigned Address, Function *F) { |
| 71 | LazyCodeGenMap[Address] = F; |
| 72 | return (intptr_t)&JITResolver::CompilationCallback; |
| 73 | } |
| 74 | |
| 75 | unsigned JITResolver::resolveFunctionReference(unsigned RetAddr) { |
| 76 | std::map<unsigned, Function*>::iterator I = LazyCodeGenMap.find(RetAddr); |
| 77 | assert(I != LazyCodeGenMap.end() && "Not in map!"); |
| 78 | Function *F = I->second; |
| 79 | LazyCodeGenMap.erase(I); |
| 80 | return MCE.forceCompilationOf(F); |
| 81 | } |
| 82 | |
| 83 | /// emitStubForFunction - This method is used by the JIT when it needs to emit |
| 84 | /// the address of a function for a function whose code has not yet been |
| 85 | /// generated. In order to do this, it generates a stub which jumps to the lazy |
| 86 | /// function compiler, which will eventually get fixed to call the function |
| 87 | /// directly. |
| 88 | /// |
| 89 | unsigned JITResolver::emitStubForFunction(Function *F) { |
| 90 | std::cerr << "PPC32CodeEmitter::emitStubForFunction() unimplemented!\n"; |
| 91 | abort(); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | void JITResolver::CompilationCallback() { |
| 96 | std::cerr << "PPC32CodeEmitter: CompilationCallback() unimplemented!"; |
| 97 | abort(); |
| 98 | } |
| 99 | |
| 100 | namespace { |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 101 | class PPC32CodeEmitter : public MachineFunctionPass { |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 102 | TargetMachine &TM; |
| 103 | MachineCodeEmitter &MCE; |
| 104 | |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 105 | // Tracks which instruction references which BasicBlock |
| 106 | std::vector<std::pair<const BasicBlock*, |
| 107 | std::pair<unsigned*,MachineInstr*> > > BBRefs; |
| 108 | // Tracks where each BasicBlock starts |
| 109 | std::map<const BasicBlock*, long> BBLocations; |
| 110 | |
| 111 | /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr |
| 112 | /// |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 113 | int64_t getMachineOpValue(MachineInstr &MI, MachineOperand &MO); |
| 114 | |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 115 | unsigned getAddressOfExternalFunction(Function *F); |
| 116 | |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 117 | public: |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 118 | PPC32CodeEmitter(TargetMachine &T, MachineCodeEmitter &M) |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 119 | : TM(T), MCE(M) {} |
| 120 | |
| 121 | const char *getPassName() const { return "PowerPC Machine Code Emitter"; } |
| 122 | |
| 123 | /// runOnMachineFunction - emits the given MachineFunction to memory |
| 124 | /// |
| 125 | bool runOnMachineFunction(MachineFunction &MF); |
| 126 | |
| 127 | /// emitBasicBlock - emits the given MachineBasicBlock to memory |
| 128 | /// |
| 129 | void emitBasicBlock(MachineBasicBlock &MBB); |
| 130 | |
| 131 | /// emitWord - write a 32-bit word to memory at the current PC |
| 132 | /// |
| 133 | void emitWord(unsigned w) { MCE.emitWord(w); } |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 134 | |
| 135 | /// getValueBit - return the particular bit of Val |
| 136 | /// |
| 137 | unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; } |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 138 | |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 139 | /// getBinaryCodeForInstr - This function, generated by the |
| 140 | /// CodeEmitterGenerator using TableGen, produces the binary encoding for |
| 141 | /// machine instructions. |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 142 | /// |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 143 | unsigned getBinaryCodeForInstr(MachineInstr &MI); |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 144 | }; |
| 145 | } |
| 146 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 147 | /// addPassesToEmitMachineCode - Add passes to the specified pass manager to get |
| 148 | /// machine code emitted. This uses a MachineCodeEmitter object to handle |
| 149 | /// actually outputting the machine code and resolving things like the address |
| 150 | /// of functions. This method should returns true if machine code emission is |
| 151 | /// not supported. |
| 152 | /// |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 153 | bool PPC32TargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM, |
| 154 | MachineCodeEmitter &MCE) { |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 155 | // Keep as `true' until this is a functional JIT to allow llvm-gcc to build |
| 156 | return true; |
| 157 | |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 158 | // Machine code emitter pass for PowerPC |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 159 | MachineCodeEmitter *M = &MCE; |
| 160 | DEBUG(M = MachineCodeEmitter::createDebugEmitter(MCE)); |
| 161 | PM.add(new PPC32CodeEmitter(*this, *M)); |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 162 | // Delete machine code for this function after emitting it |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 163 | PM.add(createMachineCodeDeleter()); |
Misha Brukman | c982cfa | 2004-10-14 06:39:56 +0000 | [diff] [blame] | 164 | return false; |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 167 | bool PPC32CodeEmitter::runOnMachineFunction(MachineFunction &MF) { |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 168 | MCE.startFunction(MF); |
| 169 | MCE.emitConstantPool(MF.getConstantPool()); |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 170 | for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB) |
| 171 | emitBasicBlock(*BB); |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 172 | MCE.finishFunction(MF); |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 173 | |
| 174 | // Resolve branches to BasicBlocks for the entire function |
| 175 | for (unsigned i = 0, e = BBRefs.size(); i != e; ++i) { |
| 176 | long Location = BBLocations[BBRefs[i].first]; |
| 177 | unsigned *Ref = BBRefs[i].second.first; |
| 178 | MachineInstr *MI = BBRefs[i].second.second; |
| 179 | DEBUG(std::cerr << "Fixup @ " << std::hex << Ref << " to 0x" << Location |
| 180 | << " in instr: " << std::dec << *MI); |
| 181 | for (unsigned ii = 0, ee = MI->getNumOperands(); ii != ee; ++ii) { |
| 182 | MachineOperand &op = MI->getOperand(ii); |
| 183 | if (op.isPCRelativeDisp()) { |
| 184 | // the instruction's branch target is made such that it branches to |
| 185 | // PC + (branchTarget * 4), so undo that arithmetic here: |
| 186 | // Location is the target of the branch |
| 187 | // Ref is the location of the instruction, and hence the PC |
| 188 | int64_t branchTarget = (Location - (long)Ref) >> 2; |
| 189 | MI->SetMachineOperandConst(ii, MachineOperand::MO_SignExtendedImmed, |
| 190 | branchTarget); |
| 191 | unsigned fixedInstr = PPC32CodeEmitter::getBinaryCodeForInstr(*MI); |
| 192 | MCE.emitWordAt(fixedInstr, Ref); |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | BBRefs.clear(); |
| 198 | BBLocations.clear(); |
| 199 | |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 200 | return false; |
| 201 | } |
| 202 | |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 203 | void PPC32CodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) { |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 204 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I) |
| 205 | emitWord(getBinaryCodeForInstr(*I)); |
| 206 | } |
| 207 | |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 208 | unsigned PPC32CodeEmitter::getAddressOfExternalFunction(Function *F) { |
| 209 | static std::map<Function*, unsigned> ExternalFn2Addr; |
| 210 | std::map<Function*, unsigned>::iterator Addr = ExternalFn2Addr.find(F); |
| 211 | |
| 212 | if (Addr == ExternalFn2Addr.end()) |
| 213 | ExternalFn2Addr[F] = MCE.forceCompilationOf(F); |
| 214 | return ExternalFn2Addr[F]; |
| 215 | } |
| 216 | |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 217 | int64_t PPC32CodeEmitter::getMachineOpValue(MachineInstr &MI, |
| 218 | MachineOperand &MO) { |
Misha Brukman | c982cfa | 2004-10-14 06:39:56 +0000 | [diff] [blame] | 219 | int64_t rv = 0; // Return value; defaults to 0 for unhandled cases |
| 220 | // or things that get fixed up later by the JIT. |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 221 | if (MO.isRegister()) { |
Misha Brukman | c982cfa | 2004-10-14 06:39:56 +0000 | [diff] [blame] | 222 | rv = MO.getReg(); |
| 223 | } else if (MO.isImmediate()) { |
| 224 | rv = MO.getImmedValue(); |
Misha Brukman | c982cfa | 2004-10-14 06:39:56 +0000 | [diff] [blame] | 225 | } else if (MO.isGlobalAddress()) { |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 226 | GlobalValue *GV = MO.getGlobal(); |
| 227 | intptr_t Addr = (intptr_t)MCE.getGlobalValueAddress(GV); |
| 228 | if (Addr == 0) { |
| 229 | if (Function *F = dyn_cast<Function>(GV)) { |
| 230 | if (F->isExternal()) |
| 231 | rv = getAddressOfExternalFunction(F); |
| 232 | else { |
| 233 | // Function has not yet been code generated! |
| 234 | getResolver(MCE).addFunctionReference(MCE.getCurrentPCValue(), F); |
| 235 | // Delayed resolution... |
| 236 | return (intptr_t)getResolver(MCE).getLazyResolver(F); |
| 237 | } |
| 238 | } else if (GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV)) { |
| 239 | if (GVar->isExternal()) |
| 240 | rv = MCE.getGlobalValueAddress(MO.getSymbolName()); |
| 241 | else { |
| 242 | std::cerr << "PPC32CodeEmitter: External global addr not found: " |
| 243 | << *GVar; |
| 244 | abort(); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | if (MO.isPCRelative()) { // Global variable reference |
| 249 | rv = (Addr - MCE.getCurrentPCValue()) >> 2; |
| 250 | } |
Misha Brukman | c982cfa | 2004-10-14 06:39:56 +0000 | [diff] [blame] | 251 | } else if (MO.isMachineBasicBlock()) { |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 252 | const BasicBlock *BB = MO.getMachineBasicBlock()->getBasicBlock(); |
| 253 | unsigned* CurrPC = (unsigned*)(intptr_t)MCE.getCurrentPCValue(); |
| 254 | BBRefs.push_back(std::make_pair(BB, std::make_pair(CurrPC, &MI))); |
Misha Brukman | c982cfa | 2004-10-14 06:39:56 +0000 | [diff] [blame] | 255 | } else if (MO.isConstantPoolIndex()) { |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 256 | unsigned index = MO.getConstantPoolIndex(); |
| 257 | rv = MCE.getConstantPoolEntryAddress(index); |
| 258 | } else if (MO.isFrameIndex()) { |
| 259 | std::cerr << "PPC32CodeEmitter: error: Frame index unhandled!\n"; |
| 260 | abort(); |
Misha Brukman | c982cfa | 2004-10-14 06:39:56 +0000 | [diff] [blame] | 261 | } else { |
| 262 | std::cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n"; |
| 263 | abort(); |
| 264 | } |
| 265 | |
| 266 | return rv; |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 269 | |
Misha Brukman | 3070e2f | 2004-10-21 01:42:02 +0000 | [diff] [blame^] | 270 | void *PPC32JITInfo::getJITStubForFunction(Function *F, MachineCodeEmitter &MCE){ |
| 271 | return (void*)((unsigned long)getResolver(MCE).getLazyResolver(F)); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Misha Brukman | 3d9a6c2 | 2004-08-11 00:09:42 +0000 | [diff] [blame] | 274 | void PPC32JITInfo::replaceMachineCodeForFunction (void *Old, void *New) { |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 275 | std::cerr << "PPC32JITInfo::replaceMachineCodeForFunction not implemented\n"; |
| 276 | abort(); |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Misha Brukman | d37faba | 2004-10-14 06:07:25 +0000 | [diff] [blame] | 279 | #include "PPC32GenCodeEmitter.inc" |
Misha Brukman | b05daff | 2004-08-09 23:03:59 +0000 | [diff] [blame] | 280 | |
Misha Brukman | 5dfe3a9 | 2004-06-21 16:55:25 +0000 | [diff] [blame] | 281 | } // end llvm namespace |
| 282 | |