Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the PowerPC 32-bit CodeEmitter and associated machinery to |
| 11 | // JIT-compile bitcode to native PowerPC. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "PPCTargetMachine.h" |
| 16 | #include "PPCRelocations.h" |
| 17 | #include "PPC.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/PassManager.h" |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/JITCodeEmitter.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Nicolas Geoffray | 0e757e1 | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineModuleInfo.h" |
Edwin Török | 4d9756a | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ErrorHandling.h" |
| 25 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetOptions.h" |
| 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
Chris Lattner | 90ae6ff | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 30 | class PPCCodeEmitter : public MachineFunctionPass { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 31 | TargetMachine &TM; |
Chris Lattner | 90ae6ff | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 32 | JITCodeEmitter &MCE; |
Chris Lattner | e333017 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 33 | MachineModuleInfo *MMI; |
Chris Lattner | 90ae6ff | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 34 | |
| 35 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 36 | AU.addRequired<MachineModuleInfo>(); |
| 37 | MachineFunctionPass::getAnalysisUsage(AU); |
| 38 | } |
| 39 | |
| 40 | static char ID; |
| 41 | |
| 42 | /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record |
| 43 | /// its address in the function into this pointer. |
| 44 | void *MovePCtoLROffset; |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 45 | public: |
Chris Lattner | 90ae6ff | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 46 | |
| 47 | PPCCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce) |
| 48 | : MachineFunctionPass(&ID), TM(tm), MCE(mce) {} |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 49 | |
| 50 | /// getBinaryCodeForInstr - This function, generated by the |
| 51 | /// CodeEmitterGenerator using TableGen, produces the binary encoding for |
| 52 | /// machine instructions. |
| 53 | |
| 54 | unsigned getBinaryCodeForInstr(const MachineInstr &MI); |
| 55 | |
| 56 | /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr |
| 57 | |
Bruno Cardoso Lopes | 8e2537b | 2009-06-01 19:57:37 +0000 | [diff] [blame] | 58 | unsigned getMachineOpValue(const MachineInstr &MI, |
| 59 | const MachineOperand &MO); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 60 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 61 | const char *getPassName() const { return "PowerPC Machine Code Emitter"; } |
| 62 | |
| 63 | /// runOnMachineFunction - emits the given MachineFunction to memory |
| 64 | /// |
| 65 | bool runOnMachineFunction(MachineFunction &MF); |
| 66 | |
| 67 | /// emitBasicBlock - emits the given MachineBasicBlock to memory |
| 68 | /// |
| 69 | void emitBasicBlock(MachineBasicBlock &MBB); |
| 70 | |
| 71 | /// getValueBit - return the particular bit of Val |
| 72 | /// |
| 73 | unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 74 | }; |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 75 | } |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 90ae6ff | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 77 | char PPCCodeEmitter::ID = 0; |
| 78 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 79 | /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code |
| 80 | /// to the specified MCE object. |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 81 | FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM, |
Bruno Cardoso Lopes | 8e2537b | 2009-06-01 19:57:37 +0000 | [diff] [blame] | 82 | JITCodeEmitter &JCE) { |
Chris Lattner | 90ae6ff | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 83 | return new PPCCodeEmitter(TM, JCE); |
Bruno Cardoso Lopes | 1ea31ff | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | 90ae6ff | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 86 | bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 87 | assert((MF.getTarget().getRelocationModel() != Reloc::Default || |
| 88 | MF.getTarget().getRelocationModel() != Reloc::Static) && |
| 89 | "JIT relocation model must be set to static or default!"); |
Nicolas Geoffray | 0e757e1 | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 90 | |
Chris Lattner | e333017 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 91 | MMI = &getAnalysis<MachineModuleInfo>(); |
| 92 | MCE.setModuleInfo(MMI); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 93 | do { |
| 94 | MovePCtoLROffset = 0; |
| 95 | MCE.startFunction(MF); |
| 96 | for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB) |
| 97 | emitBasicBlock(*BB); |
| 98 | } while (MCE.finishFunction(MF)); |
| 99 | |
| 100 | return false; |
| 101 | } |
| 102 | |
Chris Lattner | 90ae6ff | 2010-02-02 21:55:58 +0000 | [diff] [blame] | 103 | void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 104 | MCE.StartMachineBasicBlock(&MBB); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 105 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 106 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){ |
Evan Cheng | 3ca8937 | 2008-09-02 06:51:36 +0000 | [diff] [blame] | 107 | const MachineInstr &MI = *I; |
Devang Patel | 5450fc1 | 2009-10-06 02:19:11 +0000 | [diff] [blame] | 108 | MCE.processDebugLoc(MI.getDebugLoc(), true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 109 | switch (MI.getOpcode()) { |
| 110 | default: |
Evan Cheng | 3ca8937 | 2008-09-02 06:51:36 +0000 | [diff] [blame] | 111 | MCE.emitWordBE(getBinaryCodeForInstr(MI)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 112 | break; |
Chris Lattner | 4052b29 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 113 | case TargetOpcode::DBG_LABEL: |
Chris Lattner | e333017 | 2010-03-14 01:41:15 +0000 | [diff] [blame] | 114 | MCE.emitLabel(MMI->getLabelSym(MI.getOperand(0).getImm())); |
Nicolas Geoffray | 0e757e1 | 2008-02-13 18:39:37 +0000 | [diff] [blame] | 115 | break; |
Chris Lattner | 8574ddd | 2010-03-14 02:33:54 +0000 | [diff] [blame^] | 116 | case TargetOpcode::EH_LABEL: |
| 117 | MCE.emitLabel(MI.getOperand(0).getMCSymbol()); |
| 118 | break; |
Chris Lattner | 4052b29 | 2010-02-09 19:54:29 +0000 | [diff] [blame] | 119 | case TargetOpcode::IMPLICIT_DEF: |
| 120 | case TargetOpcode::KILL: |
Evan Cheng | b74b4b6 | 2008-03-17 06:56:52 +0000 | [diff] [blame] | 121 | break; // pseudo opcode, no side effects |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 122 | case PPC::MovePCtoLR: |
| 123 | case PPC::MovePCtoLR8: |
| 124 | assert(TM.getRelocationModel() == Reloc::PIC_); |
| 125 | MovePCtoLROffset = (void*)MCE.getCurrentPCValue(); |
| 126 | MCE.emitWordBE(0x48000005); // bl 1 |
| 127 | break; |
| 128 | } |
Devang Patel | 5450fc1 | 2009-10-06 02:19:11 +0000 | [diff] [blame] | 129 | MCE.processDebugLoc(MI.getDebugLoc(), false); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Evan Cheng | 3ca8937 | 2008-09-02 06:51:36 +0000 | [diff] [blame] | 133 | unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI, |
| 134 | const MachineOperand &MO) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 135 | |
Evan Cheng | 3ca8937 | 2008-09-02 06:51:36 +0000 | [diff] [blame] | 136 | unsigned rv = 0; // Return value; defaults to 0 for unhandled cases |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 137 | // or things that get fixed up later by the JIT. |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 138 | if (MO.isReg()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 139 | rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg()); |
| 140 | |
| 141 | // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the |
| 142 | // register, not the register number directly. |
| 143 | if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) && |
| 144 | (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) { |
| 145 | rv = 0x80 >> rv; |
| 146 | } |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 147 | } else if (MO.isImm()) { |
Chris Lattner | a96056a | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 148 | rv = MO.getImm(); |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 149 | } else if (MO.isGlobal() || MO.isSymbol() || |
| 150 | MO.isCPI() || MO.isJTI()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 151 | unsigned Reloc = 0; |
Tilmann Scheller | 386330d | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 152 | if (MI.getOpcode() == PPC::BL_Darwin || MI.getOpcode() == PPC::BL8_Darwin || |
| 153 | MI.getOpcode() == PPC::BL_SVR4 || MI.getOpcode() == PPC::BL8_ELF || |
Arnold Schwaighofer | a003272 | 2008-04-30 09:16:33 +0000 | [diff] [blame] | 154 | MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 155 | Reloc = PPC::reloc_pcrel_bx; |
| 156 | else { |
| 157 | if (TM.getRelocationModel() == Reloc::PIC_) { |
| 158 | assert(MovePCtoLROffset && "MovePCtoLR not seen yet?"); |
| 159 | } |
| 160 | switch (MI.getOpcode()) { |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 161 | default: MI.dump(); llvm_unreachable("Unknown instruction for relocation!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 162 | case PPC::LIS: |
| 163 | case PPC::LIS8: |
| 164 | case PPC::ADDIS: |
| 165 | case PPC::ADDIS8: |
| 166 | Reloc = PPC::reloc_absolute_high; // Pointer to symbol |
| 167 | break; |
| 168 | case PPC::LI: |
| 169 | case PPC::LI8: |
| 170 | case PPC::LA: |
| 171 | // Loads. |
| 172 | case PPC::LBZ: |
| 173 | case PPC::LBZ8: |
| 174 | case PPC::LHA: |
| 175 | case PPC::LHA8: |
| 176 | case PPC::LHZ: |
| 177 | case PPC::LHZ8: |
| 178 | case PPC::LWZ: |
| 179 | case PPC::LWZ8: |
| 180 | case PPC::LFS: |
| 181 | case PPC::LFD: |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 182 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 183 | // Stores. |
| 184 | case PPC::STB: |
| 185 | case PPC::STB8: |
| 186 | case PPC::STH: |
| 187 | case PPC::STH8: |
| 188 | case PPC::STW: |
| 189 | case PPC::STW8: |
| 190 | case PPC::STFS: |
| 191 | case PPC::STFD: |
| 192 | Reloc = PPC::reloc_absolute_low; |
| 193 | break; |
| 194 | |
| 195 | case PPC::LWA: |
| 196 | case PPC::LD: |
| 197 | case PPC::STD: |
| 198 | case PPC::STD_32: |
| 199 | Reloc = PPC::reloc_absolute_low_ix; |
| 200 | break; |
| 201 | } |
| 202 | } |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 203 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 204 | MachineRelocation R; |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 205 | if (MO.isGlobal()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 206 | R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, |
Evan Cheng | 9f6942b | 2008-01-04 02:22:21 +0000 | [diff] [blame] | 207 | MO.getGlobal(), 0, |
| 208 | isa<Function>(MO.getGlobal())); |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 209 | } else if (MO.isSymbol()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 210 | R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), |
| 211 | Reloc, MO.getSymbolName(), 0); |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 212 | } else if (MO.isCPI()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 213 | R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 214 | Reloc, MO.getIndex(), 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 215 | } else { |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 216 | assert(MO.isJTI()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 217 | R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 218 | Reloc, MO.getIndex(), 0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 219 | } |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 220 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 221 | // If in PIC mode, we need to encode the negated address of the |
| 222 | // 'movepctolr' into the unrelocated field. After relocation, we'll have |
| 223 | // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm |
| 224 | // field, we get &gv. This doesn't happen for branch relocations, which are |
| 225 | // always implicitly pc relative. |
| 226 | if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){ |
| 227 | assert(MovePCtoLROffset && "MovePCtoLR not seen yet?"); |
| 228 | R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4); |
| 229 | } |
| 230 | MCE.addRelocation(R); |
Daniel Dunbar | 3be44e6 | 2009-09-20 02:20:51 +0000 | [diff] [blame] | 231 | |
Dan Gohman | b9f4fa7 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 232 | } else if (MO.isMBB()) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 233 | unsigned Reloc = 0; |
| 234 | unsigned Opcode = MI.getOpcode(); |
Tilmann Scheller | 386330d | 2009-07-03 06:47:08 +0000 | [diff] [blame] | 235 | if (Opcode == PPC::B || Opcode == PPC::BL_Darwin || |
| 236 | Opcode == PPC::BLA_Darwin|| Opcode == PPC::BL_SVR4 || |
| 237 | Opcode == PPC::BLA_SVR4) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 238 | Reloc = PPC::reloc_pcrel_bx; |
| 239 | else // BCC instruction |
| 240 | Reloc = PPC::reloc_pcrel_bcx; |
Bruno Cardoso Lopes | aabb9a5 | 2009-07-06 05:09:34 +0000 | [diff] [blame] | 241 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 242 | MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(), |
Chris Lattner | 6017d48 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 243 | Reloc, MO.getMBB())); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 244 | } else { |
Edwin Török | 4d9756a | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 245 | #ifndef NDEBUG |
Chris Lattner | d71b0b0 | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 246 | errs() << "ERROR: Unknown type of MachineOperand: " << MO << "\n"; |
Edwin Török | 4d9756a | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 247 | #endif |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 248 | llvm_unreachable(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | return rv; |
| 252 | } |
| 253 | |
| 254 | #include "PPCGenCodeEmitter.inc" |