PPCCodeEmitter.cpp revision 7431beaba2a01c3fe299c861b2ec85cbf1dc81c4
121e463b2bf864671a87ebe386cb100ef9349a540Nate Begeman//===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=// 2b5f662fa0314f7e7e690aae8ebff7136cc3a5ab0Misha Brukman// 35dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman// The LLVM Compiler Infrastructure 45dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman// 54ee451de366474b9c228b4e5fa573795a715216dChris Lattner// This file is distributed under the University of Illinois Open Source 64ee451de366474b9c228b4e5fa573795a715216dChris Lattner// License. See LICENSE.TXT for details. 7b5f662fa0314f7e7e690aae8ebff7136cc3a5ab0Misha Brukman// 85dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman//===----------------------------------------------------------------------===// 9b5f662fa0314f7e7e690aae8ebff7136cc3a5ab0Misha Brukman// 10d37faba5b485eefe94e36e0f88da899b8fec42e5Misha Brukman// This file defines the PowerPC 32-bit CodeEmitter and associated machinery to 11a99be51bf5cdac1438069d4b01766c47704961c8Gabor Greif// JIT-compile bitcode to native PowerPC. 125dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman// 135dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman//===----------------------------------------------------------------------===// 145dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman 1516e71f2f70811c69c56052dd146324fe20e31db5Chris Lattner#include "PPCTargetMachine.h" 1616e71f2f70811c69c56052dd146324fe20e31db5Chris Lattner#include "PPCRelocations.h" 172668959b8879097db368aec7d76c455260abc75bChris Lattner#include "PPC.h" 183070e2ff790e106f5969f503487db5e4b8639ce4Misha Brukman#include "llvm/Module.h" 19de123822e5cc9a7d09b12166439cfd35a6c9ed62Chris Lattner#include "llvm/PassManager.h" 20a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes#include "llvm/CodeGen/JITCodeEmitter.h" 21b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman#include "llvm/CodeGen/MachineFunctionPass.h" 22d4b4a99587a0856473b9334455f6cebcb4fe2583Misha Brukman#include "llvm/CodeGen/MachineInstrBuilder.h" 23afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray#include "llvm/CodeGen/MachineModuleInfo.h" 24dac237e18209b697a8ba122d0ddd9cad4dfba1f8Torok Edwin#include "llvm/Support/ErrorHandling.h" 25dac237e18209b697a8ba122d0ddd9cad4dfba1f8Torok Edwin#include "llvm/Support/raw_ostream.h" 26d2ee218b499fcd364aae7da031819b738f009cd1Evan Cheng#include "llvm/Target/TargetOptions.h" 27eea9b134fcd97aa6c11277864fecf2d30640d27fChris Lattnerusing namespace llvm; 285dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman 29b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukmannamespace { 30fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner class PPCCodeEmitter : public MachineFunctionPass { 31b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman TargetMachine &TM; 32fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner JITCodeEmitter &MCE; 331611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner MachineModuleInfo *MMI; 34fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner 35fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner void getAnalysisUsage(AnalysisUsage &AU) const { 36fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner AU.addRequired<MachineModuleInfo>(); 37fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner MachineFunctionPass::getAnalysisUsage(AU); 38fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner } 39fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner 40fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner static char ID; 41fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner 42fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record 43fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner /// its address in the function into this pointer. 44fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner void *MovePCtoLROffset; 45a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes public: 46fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner 47fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner PPCCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce) 48fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner : MachineFunctionPass(&ID), TM(tm), MCE(mce) {} 49a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 50a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// getBinaryCodeForInstr - This function, generated by the 51a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// CodeEmitterGenerator using TableGen, produces the binary encoding for 52a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// machine instructions. 53a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 54a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes unsigned getBinaryCodeForInstr(const MachineInstr &MI); 55a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 56a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr 57a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 58434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes unsigned getMachineOpValue(const MachineInstr &MI, 59434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes const MachineOperand &MO); 60b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 61b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman const char *getPassName() const { return "PowerPC Machine Code Emitter"; } 62b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 63b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman /// runOnMachineFunction - emits the given MachineFunction to memory 64b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman /// 65b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman bool runOnMachineFunction(MachineFunction &MF); 66b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 67b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman /// emitBasicBlock - emits the given MachineBasicBlock to memory 68b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman /// 69b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman void emitBasicBlock(MachineBasicBlock &MBB); 70b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 71d37faba5b485eefe94e36e0f88da899b8fec42e5Misha Brukman /// getValueBit - return the particular bit of Val 72d37faba5b485eefe94e36e0f88da899b8fec42e5Misha Brukman /// 73d37faba5b485eefe94e36e0f88da899b8fec42e5Misha Brukman unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; } 74b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman }; 75a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes} 76a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar 77fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattnerchar PPCCodeEmitter::ID = 0; 78fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner 79eb883af3903286ac20f5bbf549c555c9ef961e14Nate Begeman/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code 80eb883af3903286ac20f5bbf549c555c9ef961e14Nate Begeman/// to the specified MCE object. 81a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso LopesFunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM, 82434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes JITCodeEmitter &JCE) { 83fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner return new PPCCodeEmitter(TM, JCE); 84b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman} 85b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 86fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattnerbool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) { 874c1aa866578f7a358407a22fe55b454f52a24325Evan Cheng assert((MF.getTarget().getRelocationModel() != Reloc::Default || 884c1aa866578f7a358407a22fe55b454f52a24325Evan Cheng MF.getTarget().getRelocationModel() != Reloc::Static) && 894c1aa866578f7a358407a22fe55b454f52a24325Evan Cheng "JIT relocation model must be set to static or default!"); 90afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 911611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner MMI = &getAnalysis<MachineModuleInfo>(); 921611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner MCE.setModuleInfo(MMI); 9343b429b05989075b60693d57395c99b0ad789f8dChris Lattner do { 94e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner MovePCtoLROffset = 0; 9543b429b05989075b60693d57395c99b0ad789f8dChris Lattner MCE.startFunction(MF); 9643b429b05989075b60693d57395c99b0ad789f8dChris Lattner for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB) 9743b429b05989075b60693d57395c99b0ad789f8dChris Lattner emitBasicBlock(*BB); 9843b429b05989075b60693d57395c99b0ad789f8dChris Lattner } while (MCE.finishFunction(MF)); 993070e2ff790e106f5969f503487db5e4b8639ce4Misha Brukman 100b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman return false; 101b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman} 102b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 103fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattnervoid PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) { 104b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner MCE.StartMachineBasicBlock(&MBB); 105a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar 106a4df350ba1193b2f0421d06a135beb21afb80035Misha Brukman for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){ 107acff339e391d23e748b69f61ae5c27fd5620c69fEvan Cheng const MachineInstr &MI = *I; 108af0e2726835e096e32c30c1b88cc7a6232a6ef69Devang Patel MCE.processDebugLoc(MI.getDebugLoc(), true); 109e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner switch (MI.getOpcode()) { 110e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner default: 111acff339e391d23e748b69f61ae5c27fd5620c69fEvan Cheng MCE.emitWordBE(getBinaryCodeForInstr(MI)); 112e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner break; 1137431beaba2a01c3fe299c861b2ec85cbf1dc81c4Bill Wendling case TargetOpcode::PROLOG_LABEL: 1147561d480953e0a2faa4af9be0a00b1180097c4bdChris Lattner case TargetOpcode::EH_LABEL: 1157561d480953e0a2faa4af9be0a00b1180097c4bdChris Lattner MCE.emitLabel(MI.getOperand(0).getMCSymbol()); 1167561d480953e0a2faa4af9be0a00b1180097c4bdChris Lattner break; 117518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner case TargetOpcode::IMPLICIT_DEF: 118518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner case TargetOpcode::KILL: 119d18330763965745fea05536939f5aadffcc6a5a6Evan Cheng break; // pseudo opcode, no side effects 120e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner case PPC::MovePCtoLR: 1216a5339ba656805a9cd3bf7d884f99bb87ec84e98Chris Lattner case PPC::MovePCtoLR8: 122e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner assert(TM.getRelocationModel() == Reloc::PIC_); 123e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner MovePCtoLROffset = (void*)MCE.getCurrentPCValue(); 124e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner MCE.emitWordBE(0x48000005); // bl 1 125e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner break; 126e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner } 127af0e2726835e096e32c30c1b88cc7a6232a6ef69Devang Patel MCE.processDebugLoc(MI.getDebugLoc(), false); 128a4df350ba1193b2f0421d06a135beb21afb80035Misha Brukman } 129b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman} 130b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 131acff339e391d23e748b69f61ae5c27fd5620c69fEvan Chengunsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI, 132acff339e391d23e748b69f61ae5c27fd5620c69fEvan Cheng const MachineOperand &MO) { 133b5f662fa0314f7e7e690aae8ebff7136cc3a5ab0Misha Brukman 134acff339e391d23e748b69f61ae5c27fd5620c69fEvan Cheng unsigned rv = 0; // Return value; defaults to 0 for unhandled cases 135b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner // or things that get fixed up later by the JIT. 136d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman if (MO.isReg()) { 137369503f8412bba4a0138074c97107c09cc4513e0Chris Lattner rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg()); 138f577c6122f8b51c4e4889f1e1c647ae210c501b8Chris Lattner 139adeb43ddf4eac9e75b7c8e79fa832f72922a2926Nate Begeman // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the 140f577c6122f8b51c4e4889f1e1c647ae210c501b8Chris Lattner // register, not the register number directly. 141adeb43ddf4eac9e75b7c8e79fa832f72922a2926Nate Begeman if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) && 142f577c6122f8b51c4e4889f1e1c647ae210c501b8Chris Lattner (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) { 143f577c6122f8b51c4e4889f1e1c647ae210c501b8Chris Lattner rv = 0x80 >> rv; 144f577c6122f8b51c4e4889f1e1c647ae210c501b8Chris Lattner } 145d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman } else if (MO.isImm()) { 1469a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner rv = MO.getImm(); 147d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman } else if (MO.isGlobal() || MO.isSymbol() || 148d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman MO.isCPI() || MO.isJTI()) { 149d7fa35c6d2acc2fe95346b1b9eaa3c0a30342c85Chris Lattner unsigned Reloc = 0; 1502a9ddfb903ae3baede7282348afae1f750905248Tilmann Scheller if (MI.getOpcode() == PPC::BL_Darwin || MI.getOpcode() == PPC::BL8_Darwin || 1512a9ddfb903ae3baede7282348afae1f750905248Tilmann Scheller MI.getOpcode() == PPC::BL_SVR4 || MI.getOpcode() == PPC::BL8_ELF || 15230e62c098b5841259f8026df1c5c45c7c1182a38Arnold Schwaighofer MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8) 153e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner Reloc = PPC::reloc_pcrel_bx; 1545efb75daed48edfeb03ba62f3f0afe81b86f5d7fChris Lattner else { 155e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner if (TM.getRelocationModel() == Reloc::PIC_) { 156e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner assert(MovePCtoLROffset && "MovePCtoLR not seen yet?"); 157e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner } 1582497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman switch (MI.getOpcode()) { 159c23197a26f34f559ea9797de51e187087c039c42Torok Edwin default: MI.dump(); llvm_unreachable("Unknown instruction for relocation!"); 1602497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LIS: 1613bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::LIS8: 162eb883af3903286ac20f5bbf549c555c9ef961e14Nate Begeman case PPC::ADDIS: 1633bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::ADDIS8: 1646fcbd6961d9f14d75e2b314b06ca823d43a70353Nate Begeman Reloc = PPC::reloc_absolute_high; // Pointer to symbol 1652497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman break; 166ea4a9c575fa0a8c8c1dce5223a637a166e5c1f8cChris Lattner case PPC::LI: 1673bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::LI8: 1682497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LA: 1693bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner // Loads. 1702497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LBZ: 17134da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::LBZ8: 1722497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LHA: 17334da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::LHA8: 1742497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LHZ: 17534da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::LHZ8: 1762497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LWZ: 17734da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::LWZ8: 1782497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LFS: 1792497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LFD: 180a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar 1813bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner // Stores. 1822497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::STB: 18334da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::STB8: 1842497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::STH: 18534da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::STH8: 1862497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::STW: 18734da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::STW8: 1882497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::STFS: 1892497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::STFD: 1906fcbd6961d9f14d75e2b314b06ca823d43a70353Nate Begeman Reloc = PPC::reloc_absolute_low; 1912497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman break; 1923bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner 1933bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::LWA: 1943bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::LD: 1953bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::STD: 1963bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::STD_32: 1973bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner Reloc = PPC::reloc_absolute_low_ix; 1983bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner break; 1995efb75daed48edfeb03ba62f3f0afe81b86f5d7fChris Lattner } 200e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner } 201a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar 20257fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner MachineRelocation R; 203d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman if (MO.isGlobal()) { 20457fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, 20546510a73e977273ec67747eb34cbdb43f815e451Dan Gohman const_cast<GlobalValue *>(MO.getGlobal()), 0, 206165b60de0b4a17e7d270e63fd4223b6c1c318fddEvan Cheng isa<Function>(MO.getGlobal())); 207d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman } else if (MO.isSymbol()) { 20857fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), 20957fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner Reloc, MO.getSymbolName(), 0); 210d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman } else if (MO.isCPI()) { 21157fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), 2128aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner Reloc, MO.getIndex(), 0); 21357fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner } else { 214d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman assert(MO.isJTI()); 21557fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), 2168aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner Reloc, MO.getIndex(), 0); 21757fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner } 218a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar 21957fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner // If in PIC mode, we need to encode the negated address of the 22057fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner // 'movepctolr' into the unrelocated field. After relocation, we'll have 22157fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm 22257fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner // field, we get &gv. This doesn't happen for branch relocations, which are 22357fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner // always implicitly pc relative. 22457fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){ 22557fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner assert(MovePCtoLROffset && "MovePCtoLR not seen yet?"); 22657fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4); 22757fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner } 22857fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner MCE.addRelocation(R); 229a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar 230d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman } else if (MO.isMBB()) { 231f141cc46faf6f0525f0baa10b6a6c976301874a5Evan Cheng unsigned Reloc = 0; 232f141cc46faf6f0525f0baa10b6a6c976301874a5Evan Cheng unsigned Opcode = MI.getOpcode(); 2332a9ddfb903ae3baede7282348afae1f750905248Tilmann Scheller if (Opcode == PPC::B || Opcode == PPC::BL_Darwin || 2342a9ddfb903ae3baede7282348afae1f750905248Tilmann Scheller Opcode == PPC::BLA_Darwin|| Opcode == PPC::BL_SVR4 || 2352a9ddfb903ae3baede7282348afae1f750905248Tilmann Scheller Opcode == PPC::BLA_SVR4) 236f141cc46faf6f0525f0baa10b6a6c976301874a5Evan Cheng Reloc = PPC::reloc_pcrel_bx; 237e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner else // BCC instruction 238f141cc46faf6f0525f0baa10b6a6c976301874a5Evan Cheng Reloc = PPC::reloc_pcrel_bcx; 239ac57e6e498abccb117e0d61c2fa0f733845e50cbBruno Cardoso Lopes 240f141cc46faf6f0525f0baa10b6a6c976301874a5Evan Cheng MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(), 2418aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner Reloc, MO.getMBB())); 242b9f26da5dd6c404e0a6eabcb60fb3c61ac0ee173Chris Lattner } else { 243dac237e18209b697a8ba122d0ddd9cad4dfba1f8Torok Edwin#ifndef NDEBUG 244705e07f578e2b3af47ddab610feb4e7f2d3063a5Chris Lattner errs() << "ERROR: Unknown type of MachineOperand: " << MO << "\n"; 245dac237e18209b697a8ba122d0ddd9cad4dfba1f8Torok Edwin#endif 246c23197a26f34f559ea9797de51e187087c039c42Torok Edwin llvm_unreachable(0); 247a4df350ba1193b2f0421d06a135beb21afb80035Misha Brukman } 248b9f26da5dd6c404e0a6eabcb60fb3c61ac0ee173Chris Lattner 249c982cfad875f64284ae0fb2f822b0867657171ebMisha Brukman return rv; 2505dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman} 2515dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman 2524c7b43b43fdf943c7298718e15ab5d6dfe345be7Chris Lattner#include "PPCGenCodeEmitter.inc" 253