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