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