PPCCodeEmitter.cpp revision dac237e18209b697a8ba122d0ddd9cad4dfba1f8
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" 20b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman#include "llvm/CodeGen/MachineCodeEmitter.h" 21a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes#include "llvm/CodeGen/JITCodeEmitter.h" 22ac57e6e498abccb117e0d61c2fa0f733845e50cbBruno Cardoso Lopes#include "llvm/CodeGen/ObjectCodeEmitter.h" 23b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman#include "llvm/CodeGen/MachineFunctionPass.h" 24d4b4a99587a0856473b9334455f6cebcb4fe2583Misha Brukman#include "llvm/CodeGen/MachineInstrBuilder.h" 25afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray#include "llvm/CodeGen/MachineModuleInfo.h" 26b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman#include "llvm/CodeGen/Passes.h" 27434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes#include "llvm/Support/Debug.h" 28a4f0b3a084d120cfc5b5bb06f64b222f5cb72740Chris Lattner#include "llvm/Support/Compiler.h" 29dac237e18209b697a8ba122d0ddd9cad4dfba1f8Torok Edwin#include "llvm/Support/ErrorHandling.h" 30dac237e18209b697a8ba122d0ddd9cad4dfba1f8Torok Edwin#include "llvm/Support/raw_ostream.h" 31d2ee218b499fcd364aae7da031819b738f009cd1Evan Cheng#include "llvm/Target/TargetOptions.h" 32eea9b134fcd97aa6c11277864fecf2d30640d27fChris Lattnerusing namespace llvm; 335dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman 34b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukmannamespace { 35a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes class PPCCodeEmitter { 36b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman TargetMachine &TM; 37b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman MachineCodeEmitter &MCE; 38a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes public: 39434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes PPCCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce): 40434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes TM(tm), MCE(mce) {} 41a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 42a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// getBinaryCodeForInstr - This function, generated by the 43a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// CodeEmitterGenerator using TableGen, produces the binary encoding for 44a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// machine instructions. 45a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 46a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes unsigned getBinaryCodeForInstr(const MachineInstr &MI); 47a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 48a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr 49a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 50434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes unsigned getMachineOpValue(const MachineInstr &MI, 51434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes const MachineOperand &MO); 52b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 53e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record 54e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner /// its address in the function into this pointer. 55a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 56e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner void *MovePCtoLROffset; 57a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes }; 58a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 59434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes template <class CodeEmitter> 60a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass, 61a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes public PPCCodeEmitter 62a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes { 63a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes TargetMachine &TM; 64434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes CodeEmitter &MCE; 65a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 66afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray void getAnalysisUsage(AnalysisUsage &AU) const { 67afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray AU.addRequired<MachineModuleInfo>(); 68afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray MachineFunctionPass::getAnalysisUsage(AU); 69afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray } 703070e2ff790e106f5969f503487db5e4b8639ce4Misha Brukman 71b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman public: 721997473cf72957d0e70322e2fe6fe2ab141c58a6Devang Patel static char ID; 73434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes Emitter(TargetMachine &tm, CodeEmitter &mce) 74434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes : MachineFunctionPass(&ID), PPCCodeEmitter(tm, mce), TM(tm), MCE(mce) {} 75b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 76b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman const char *getPassName() const { return "PowerPC Machine Code Emitter"; } 77b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 78b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman /// runOnMachineFunction - emits the given MachineFunction to memory 79b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman /// 80b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman bool runOnMachineFunction(MachineFunction &MF); 81b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 82b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman /// emitBasicBlock - emits the given MachineBasicBlock to memory 83b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman /// 84b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman void emitBasicBlock(MachineBasicBlock &MBB); 85b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 86d37faba5b485eefe94e36e0f88da899b8fec42e5Misha Brukman /// getValueBit - return the particular bit of Val 87d37faba5b485eefe94e36e0f88da899b8fec42e5Misha Brukman /// 88d37faba5b485eefe94e36e0f88da899b8fec42e5Misha Brukman unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; } 89b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman }; 90b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 91434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes template <class CodeEmitter> 92434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes char Emitter<CodeEmitter>::ID = 0; 93a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes} 94a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 95eb883af3903286ac20f5bbf549c555c9ef961e14Nate Begeman/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code 96eb883af3903286ac20f5bbf549c555c9ef961e14Nate Begeman/// to the specified MCE object. 97ac57e6e498abccb117e0d61c2fa0f733845e50cbBruno Cardoso Lopes 98eb883af3903286ac20f5bbf549c555c9ef961e14Nate BegemanFunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM, 99434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes MachineCodeEmitter &MCE) { 100a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes return new Emitter<MachineCodeEmitter>(TM, MCE); 101a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes} 102a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 103a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso LopesFunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM, 104434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes JITCodeEmitter &JCE) { 105a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes return new Emitter<JITCodeEmitter>(TM, JCE); 106b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman} 107b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 108ac57e6e498abccb117e0d61c2fa0f733845e50cbBruno Cardoso LopesFunctionPass *llvm::createPPCObjectCodeEmitterPass(PPCTargetMachine &TM, 109ac57e6e498abccb117e0d61c2fa0f733845e50cbBruno Cardoso Lopes ObjectCodeEmitter &OCE) { 110ac57e6e498abccb117e0d61c2fa0f733845e50cbBruno Cardoso Lopes return new Emitter<ObjectCodeEmitter>(TM, OCE); 111ac57e6e498abccb117e0d61c2fa0f733845e50cbBruno Cardoso Lopes} 112ac57e6e498abccb117e0d61c2fa0f733845e50cbBruno Cardoso Lopes 113434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopestemplate <class CodeEmitter> 114434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopesbool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) { 1154c1aa866578f7a358407a22fe55b454f52a24325Evan Cheng assert((MF.getTarget().getRelocationModel() != Reloc::Default || 1164c1aa866578f7a358407a22fe55b454f52a24325Evan Cheng MF.getTarget().getRelocationModel() != Reloc::Static) && 1174c1aa866578f7a358407a22fe55b454f52a24325Evan Cheng "JIT relocation model must be set to static or default!"); 118afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 119afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>()); 12043b429b05989075b60693d57395c99b0ad789f8dChris Lattner do { 121e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner MovePCtoLROffset = 0; 12243b429b05989075b60693d57395c99b0ad789f8dChris Lattner MCE.startFunction(MF); 12343b429b05989075b60693d57395c99b0ad789f8dChris Lattner for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB) 12443b429b05989075b60693d57395c99b0ad789f8dChris Lattner emitBasicBlock(*BB); 12543b429b05989075b60693d57395c99b0ad789f8dChris Lattner } while (MCE.finishFunction(MF)); 1263070e2ff790e106f5969f503487db5e4b8639ce4Misha Brukman 127b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman return false; 128b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman} 129b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 130434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopestemplate <class CodeEmitter> 131434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopesvoid Emitter<CodeEmitter>::emitBasicBlock(MachineBasicBlock &MBB) { 132b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner MCE.StartMachineBasicBlock(&MBB); 133b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner 134a4df350ba1193b2f0421d06a135beb21afb80035Misha Brukman for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){ 135acff339e391d23e748b69f61ae5c27fd5620c69fEvan Cheng const MachineInstr &MI = *I; 136e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner switch (MI.getOpcode()) { 137e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner default: 138acff339e391d23e748b69f61ae5c27fd5620c69fEvan Cheng MCE.emitWordBE(getBinaryCodeForInstr(MI)); 139e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner break; 1404406604047423576e36657c7ede266ca42e79642Dan Gohman case TargetInstrInfo::DBG_LABEL: 1414406604047423576e36657c7ede266ca42e79642Dan Gohman case TargetInstrInfo::EH_LABEL: 142afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray MCE.emitLabel(MI.getOperand(0).getImm()); 143afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray break; 144d18330763965745fea05536939f5aadffcc6a5a6Evan Cheng case TargetInstrInfo::IMPLICIT_DEF: 145d18330763965745fea05536939f5aadffcc6a5a6Evan Cheng break; // pseudo opcode, no side effects 146e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner case PPC::MovePCtoLR: 1476a5339ba656805a9cd3bf7d884f99bb87ec84e98Chris Lattner case PPC::MovePCtoLR8: 148e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner assert(TM.getRelocationModel() == Reloc::PIC_); 149e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner MovePCtoLROffset = (void*)MCE.getCurrentPCValue(); 150e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner MCE.emitWordBE(0x48000005); // bl 1 151e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner break; 152e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner } 153a4df350ba1193b2f0421d06a135beb21afb80035Misha Brukman } 154b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman} 155b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 156acff339e391d23e748b69f61ae5c27fd5620c69fEvan Chengunsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI, 157acff339e391d23e748b69f61ae5c27fd5620c69fEvan Cheng const MachineOperand &MO) { 158b5f662fa0314f7e7e690aae8ebff7136cc3a5ab0Misha Brukman 159acff339e391d23e748b69f61ae5c27fd5620c69fEvan Cheng unsigned rv = 0; // Return value; defaults to 0 for unhandled cases 160b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner // or things that get fixed up later by the JIT. 161d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman if (MO.isReg()) { 162369503f8412bba4a0138074c97107c09cc4513e0Chris Lattner rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg()); 163f577c6122f8b51c4e4889f1e1c647ae210c501b8Chris Lattner 164adeb43ddf4eac9e75b7c8e79fa832f72922a2926Nate Begeman // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the 165f577c6122f8b51c4e4889f1e1c647ae210c501b8Chris Lattner // register, not the register number directly. 166adeb43ddf4eac9e75b7c8e79fa832f72922a2926Nate Begeman if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) && 167f577c6122f8b51c4e4889f1e1c647ae210c501b8Chris Lattner (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) { 168f577c6122f8b51c4e4889f1e1c647ae210c501b8Chris Lattner rv = 0x80 >> rv; 169f577c6122f8b51c4e4889f1e1c647ae210c501b8Chris Lattner } 170d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman } else if (MO.isImm()) { 1719a1ceaedc282f0cae31f2723f4d6c00c7b88fe90Chris Lattner rv = MO.getImm(); 172d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman } else if (MO.isGlobal() || MO.isSymbol() || 173d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman MO.isCPI() || MO.isJTI()) { 174d7fa35c6d2acc2fe95346b1b9eaa3c0a30342c85Chris Lattner unsigned Reloc = 0; 1752a9ddfb903ae3baede7282348afae1f750905248Tilmann Scheller if (MI.getOpcode() == PPC::BL_Darwin || MI.getOpcode() == PPC::BL8_Darwin || 1762a9ddfb903ae3baede7282348afae1f750905248Tilmann Scheller MI.getOpcode() == PPC::BL_SVR4 || MI.getOpcode() == PPC::BL8_ELF || 17730e62c098b5841259f8026df1c5c45c7c1182a38Arnold Schwaighofer MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8) 178e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner Reloc = PPC::reloc_pcrel_bx; 1795efb75daed48edfeb03ba62f3f0afe81b86f5d7fChris Lattner else { 180e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner if (TM.getRelocationModel() == Reloc::PIC_) { 181e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner assert(MovePCtoLROffset && "MovePCtoLR not seen yet?"); 182e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner } 1832497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman switch (MI.getOpcode()) { 184fae2c19de91ae9a31046aaed27d1e5afa8d3e158Chris Lattner default: MI.dump(); assert(0 && "Unknown instruction for relocation!"); 1852497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LIS: 1863bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::LIS8: 187eb883af3903286ac20f5bbf549c555c9ef961e14Nate Begeman case PPC::ADDIS: 1883bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::ADDIS8: 1896fcbd6961d9f14d75e2b314b06ca823d43a70353Nate Begeman Reloc = PPC::reloc_absolute_high; // Pointer to symbol 1902497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman break; 191ea4a9c575fa0a8c8c1dce5223a637a166e5c1f8cChris Lattner case PPC::LI: 1923bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::LI8: 1932497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LA: 1943bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner // Loads. 1952497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LBZ: 19634da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::LBZ8: 1972497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LHA: 19834da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::LHA8: 1992497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LHZ: 20034da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::LHZ8: 2012497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LWZ: 20234da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::LWZ8: 2032497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LFS: 2042497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::LFD: 2053bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner 2063bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner // Stores. 2072497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::STB: 20834da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::STB8: 2092497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::STH: 21034da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::STH8: 2112497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::STW: 21234da72645a82e11bc7008bc567d63426ec4c5949Jim Laskey case PPC::STW8: 2132497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::STFS: 2142497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman case PPC::STFD: 2156fcbd6961d9f14d75e2b314b06ca823d43a70353Nate Begeman Reloc = PPC::reloc_absolute_low; 2162497e6391f8df05926fe17b5cf08dad61c4797d2Nate Begeman break; 2173bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner 2183bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::LWA: 2193bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::LD: 2203bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::STD: 2213bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner case PPC::STD_32: 2223bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner Reloc = PPC::reloc_absolute_low_ix; 2233bc8a765a98015e6d55510b6ea6e387cbfd793cdChris Lattner break; 2245efb75daed48edfeb03ba62f3f0afe81b86f5d7fChris Lattner } 225e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner } 22657fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner 22757fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner MachineRelocation R; 228d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman if (MO.isGlobal()) { 22957fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc, 230165b60de0b4a17e7d270e63fd4223b6c1c318fddEvan Cheng MO.getGlobal(), 0, 231165b60de0b4a17e7d270e63fd4223b6c1c318fddEvan Cheng isa<Function>(MO.getGlobal())); 232d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman } else if (MO.isSymbol()) { 23357fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), 23457fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner Reloc, MO.getSymbolName(), 0); 235d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman } else if (MO.isCPI()) { 23657fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), 2378aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner Reloc, MO.getIndex(), 0); 23857fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner } else { 239d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman assert(MO.isJTI()); 24057fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), 2418aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner Reloc, MO.getIndex(), 0); 24257fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner } 24357fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner 24457fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner // If in PIC mode, we need to encode the negated address of the 24557fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner // 'movepctolr' into the unrelocated field. After relocation, we'll have 24657fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm 24757fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner // field, we get &gv. This doesn't happen for branch relocations, which are 24857fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner // always implicitly pc relative. 24957fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){ 25057fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner assert(MovePCtoLROffset && "MovePCtoLR not seen yet?"); 25157fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4); 25257fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner } 25357fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner MCE.addRelocation(R); 25457fc62c8d2bf056b0d2a0e3d1b82b3b787b899f8Chris Lattner 255d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman } else if (MO.isMBB()) { 256f141cc46faf6f0525f0baa10b6a6c976301874a5Evan Cheng unsigned Reloc = 0; 257f141cc46faf6f0525f0baa10b6a6c976301874a5Evan Cheng unsigned Opcode = MI.getOpcode(); 2582a9ddfb903ae3baede7282348afae1f750905248Tilmann Scheller if (Opcode == PPC::B || Opcode == PPC::BL_Darwin || 2592a9ddfb903ae3baede7282348afae1f750905248Tilmann Scheller Opcode == PPC::BLA_Darwin|| Opcode == PPC::BL_SVR4 || 2602a9ddfb903ae3baede7282348afae1f750905248Tilmann Scheller Opcode == PPC::BLA_SVR4) 261f141cc46faf6f0525f0baa10b6a6c976301874a5Evan Cheng Reloc = PPC::reloc_pcrel_bx; 262e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner else // BCC instruction 263f141cc46faf6f0525f0baa10b6a6c976301874a5Evan Cheng Reloc = PPC::reloc_pcrel_bcx; 264ac57e6e498abccb117e0d61c2fa0f733845e50cbBruno Cardoso Lopes 265f141cc46faf6f0525f0baa10b6a6c976301874a5Evan Cheng MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(), 2668aa797aa51cd4ea1ec6f46f4891a6897944b75b2Chris Lattner Reloc, MO.getMBB())); 267b9f26da5dd6c404e0a6eabcb60fb3c61ac0ee173Chris Lattner } else { 268dac237e18209b697a8ba122d0ddd9cad4dfba1f8Torok Edwin#ifndef NDEBUG 269f5da13367f88f06e3b585dc2263ab6e9ca6c4bf8Bill Wendling cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n"; 270dac237e18209b697a8ba122d0ddd9cad4dfba1f8Torok Edwin#endif 271dac237e18209b697a8ba122d0ddd9cad4dfba1f8Torok Edwin llvm_unreachable(); 272a4df350ba1193b2f0421d06a135beb21afb80035Misha Brukman } 273b9f26da5dd6c404e0a6eabcb60fb3c61ac0ee173Chris Lattner 274c982cfad875f64284ae0fb2f822b0867657171ebMisha Brukman return rv; 2755dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman} 2765dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman 2774c7b43b43fdf943c7298718e15ab5d6dfe345be7Chris Lattner#include "PPCGenCodeEmitter.inc" 278b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 279