131d157ae1ac2cd9c787dc3c1d28e64c682803844Jia Liu//===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC -----------------===// 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) 4890c579de5a383cee278acc3f7e7b9d0a656e6a35Owen Anderson : 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. 534f8dc7b17accf4f2ec953b80b2cc79786207492eOwen Anderson uint64_t getBinaryCodeForInstr(const MachineInstr &MI) const; 54a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes 55a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner 56a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner MachineRelocation GetRelocation(const MachineOperand &MO, 57a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner unsigned RelocID) const; 58a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner 59a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr 60434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes unsigned getMachineOpValue(const MachineInstr &MI, 61bade37bb8b83deefa166776b1b5185c237a42e71Jim Grosbach const MachineOperand &MO) const; 62b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 637192eb873201ff201681fefd1f5bf6ca2b2bc98eChris Lattner unsigned get_crbitm_encoding(const MachineInstr &MI, unsigned OpNo) const; 648d70411dcd5e1af47c3f4fddb993bb93c8eed6d0Chris Lattner unsigned getDirectBrEncoding(const MachineInstr &MI, unsigned OpNo) const; 658d70411dcd5e1af47c3f4fddb993bb93c8eed6d0Chris Lattner unsigned getCondBrEncoding(const MachineInstr &MI, unsigned OpNo) const; 6685cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner 6785cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner unsigned getHA16Encoding(const MachineInstr &MI, unsigned OpNo) const; 6885cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner unsigned getLO16Encoding(const MachineInstr &MI, unsigned OpNo) const; 69b7035d04421112a4585245f67bc564170ec45b29Chris Lattner unsigned getMemRIEncoding(const MachineInstr &MI, unsigned OpNo) const; 7017e2c188359769a1df18c42593a94ce0fc2a9a75Chris Lattner unsigned getMemRIXEncoding(const MachineInstr &MI, unsigned OpNo) const; 7185cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner 72b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman const char *getPassName() const { return "PowerPC Machine Code Emitter"; } 73b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 74b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman /// runOnMachineFunction - emits the given MachineFunction to memory 75b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman /// 76b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman bool runOnMachineFunction(MachineFunction &MF); 77b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 78b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman /// emitBasicBlock - emits the given MachineBasicBlock to memory 79b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman /// 80b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman void emitBasicBlock(MachineBasicBlock &MBB); 81b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman }; 82a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso Lopes} 83a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar 84fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattnerchar PPCCodeEmitter::ID = 0; 85fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner 86eb883af3903286ac20f5bbf549c555c9ef961e14Nate Begeman/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code 87eb883af3903286ac20f5bbf549c555c9ef961e14Nate Begeman/// to the specified MCE object. 88a3f99f90338d89354384ca25f53ca4450a1a9d18Bruno Cardoso LopesFunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM, 89434dd4fd94f5f248492c675e4285e7d67342d4c4Bruno Cardoso Lopes JITCodeEmitter &JCE) { 90fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattner return new PPCCodeEmitter(TM, JCE); 91b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman} 92b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 93fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattnerbool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) { 944c1aa866578f7a358407a22fe55b454f52a24325Evan Cheng assert((MF.getTarget().getRelocationModel() != Reloc::Default || 954c1aa866578f7a358407a22fe55b454f52a24325Evan Cheng MF.getTarget().getRelocationModel() != Reloc::Static) && 964c1aa866578f7a358407a22fe55b454f52a24325Evan Cheng "JIT relocation model must be set to static or default!"); 97afe6c2b001a924cd74bd0aacfed5984d9af004b0Nicolas Geoffray 981611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner MMI = &getAnalysis<MachineModuleInfo>(); 991611273351d75b5cbe2a67485bb9831d5916fe26Chris Lattner MCE.setModuleInfo(MMI); 10043b429b05989075b60693d57395c99b0ad789f8dChris Lattner do { 101e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner MovePCtoLROffset = 0; 10243b429b05989075b60693d57395c99b0ad789f8dChris Lattner MCE.startFunction(MF); 10343b429b05989075b60693d57395c99b0ad789f8dChris Lattner for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB) 10443b429b05989075b60693d57395c99b0ad789f8dChris Lattner emitBasicBlock(*BB); 10543b429b05989075b60693d57395c99b0ad789f8dChris Lattner } while (MCE.finishFunction(MF)); 1063070e2ff790e106f5969f503487db5e4b8639ce4Misha Brukman 107b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman return false; 108b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman} 109b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 110fc89bc903ca068eac7d311158d19ddee25db7154Chris Lattnervoid PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) { 111b4432f3d4754e16c918428d34a9d8ec18ab79204Chris Lattner MCE.StartMachineBasicBlock(&MBB); 112a279bc3da55691784064cb47200a1c584408b8abDaniel Dunbar 113a4df350ba1193b2f0421d06a135beb21afb80035Misha Brukman for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){ 114acff339e391d23e748b69f61ae5c27fd5620c69fEvan Cheng const MachineInstr &MI = *I; 115af0e2726835e096e32c30c1b88cc7a6232a6ef69Devang Patel MCE.processDebugLoc(MI.getDebugLoc(), true); 116e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner switch (MI.getOpcode()) { 117e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner default: 118acff339e391d23e748b69f61ae5c27fd5620c69fEvan Cheng MCE.emitWordBE(getBinaryCodeForInstr(MI)); 119e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner break; 1207431beaba2a01c3fe299c861b2ec85cbf1dc81c4Bill Wendling case TargetOpcode::PROLOG_LABEL: 1217561d480953e0a2faa4af9be0a00b1180097c4bdChris Lattner case TargetOpcode::EH_LABEL: 1227561d480953e0a2faa4af9be0a00b1180097c4bdChris Lattner MCE.emitLabel(MI.getOperand(0).getMCSymbol()); 1237561d480953e0a2faa4af9be0a00b1180097c4bdChris Lattner break; 124518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner case TargetOpcode::IMPLICIT_DEF: 125518bb53485df640d7b7e3f6b0544099020c42aa7Chris Lattner case TargetOpcode::KILL: 126d18330763965745fea05536939f5aadffcc6a5a6Evan Cheng break; // pseudo opcode, no side effects 127e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner case PPC::MovePCtoLR: 1286a5339ba656805a9cd3bf7d884f99bb87ec84e98Chris Lattner case PPC::MovePCtoLR8: 129e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner assert(TM.getRelocationModel() == Reloc::PIC_); 130e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner MovePCtoLROffset = (void*)MCE.getCurrentPCValue(); 131e150b8eb873fc1bdde17d8ecfd3c38168a5cdceeChris Lattner MCE.emitWordBE(0x48000005); // bl 1 132e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner break; 133e94c517bb52095f1ad070bb029617d532af405b8Chris Lattner } 134af0e2726835e096e32c30c1b88cc7a6232a6ef69Devang Patel MCE.processDebugLoc(MI.getDebugLoc(), false); 135a4df350ba1193b2f0421d06a135beb21afb80035Misha Brukman } 136b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman} 137b05daff7faf694a8d18f00ef00132384c8207cd4Misha Brukman 1387192eb873201ff201681fefd1f5bf6ca2b2bc98eChris Lattnerunsigned PPCCodeEmitter::get_crbitm_encoding(const MachineInstr &MI, 1397192eb873201ff201681fefd1f5bf6ca2b2bc98eChris Lattner unsigned OpNo) const { 1407192eb873201ff201681fefd1f5bf6ca2b2bc98eChris Lattner const MachineOperand &MO = MI.getOperand(OpNo); 141234bb38d6c421ea22229087a9835afe99e531276Hal Finkel assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MTCRF8 || 142234bb38d6c421ea22229087a9835afe99e531276Hal Finkel MI.getOpcode() == PPC::MFOCRF) && 1437192eb873201ff201681fefd1f5bf6ca2b2bc98eChris Lattner (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)); 144966aeb5788c242cfaca35c56c0ddc0ff778d4376Evan Cheng return 0x80 >> getPPCRegisterNumbering(MO.getReg()); 1457192eb873201ff201681fefd1f5bf6ca2b2bc98eChris Lattner} 1467192eb873201ff201681fefd1f5bf6ca2b2bc98eChris Lattner 147a9d9ab9673ec73817f3059ea430f1930a5b14948Chris LattnerMachineRelocation PPCCodeEmitter::GetRelocation(const MachineOperand &MO, 148a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner unsigned RelocID) const { 14985cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner // If in PIC mode, we need to encode the negated address of the 15085cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner // 'movepctolr' into the unrelocated field. After relocation, we'll have 15185cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm 15285cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner // field, we get &gv. This doesn't happen for branch relocations, which are 15385cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner // always implicitly pc relative. 15485cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner intptr_t Cst = 0; 15585cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner if (TM.getRelocationModel() == Reloc::PIC_) { 15685cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner assert(MovePCtoLROffset && "MovePCtoLR not seen yet?"); 15785cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner Cst = -(intptr_t)MovePCtoLROffset - 4; 15885cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner } 15985cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner 160a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner if (MO.isGlobal()) 161a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner return MachineRelocation::getGV(MCE.getCurrentPCOffset(), RelocID, 16285cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner const_cast<GlobalValue *>(MO.getGlobal()), 16385cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner Cst, isa<Function>(MO.getGlobal())); 164a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner if (MO.isSymbol()) 165a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner return MachineRelocation::getExtSym(MCE.getCurrentPCOffset(), 16685cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner RelocID, MO.getSymbolName(), Cst); 167a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner if (MO.isCPI()) 168a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner return MachineRelocation::getConstPool(MCE.getCurrentPCOffset(), 16985cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner RelocID, MO.getIndex(), Cst); 170a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner 171a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner if (MO.isMBB()) 172bc4434135fca0e0feff89cf6fe3baa25c6bd4ee0Chris Lattner return MachineRelocation::getBB(MCE.getCurrentPCOffset(), 173bc4434135fca0e0feff89cf6fe3baa25c6bd4ee0Chris Lattner RelocID, MO.getMBB()); 174a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner 175a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner assert(MO.isJTI()); 176a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner return MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(), 17785cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner RelocID, MO.getIndex(), Cst); 178a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner} 179a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner 1808d70411dcd5e1af47c3f4fddb993bb93c8eed6d0Chris Lattnerunsigned PPCCodeEmitter::getDirectBrEncoding(const MachineInstr &MI, 1818d70411dcd5e1af47c3f4fddb993bb93c8eed6d0Chris Lattner unsigned OpNo) const { 182a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner const MachineOperand &MO = MI.getOperand(OpNo); 183a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO); 184a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner 185a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bx)); 186a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner return 0; 187a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner} 188a9d9ab9673ec73817f3059ea430f1930a5b14948Chris Lattner 1898d70411dcd5e1af47c3f4fddb993bb93c8eed6d0Chris Lattnerunsigned PPCCodeEmitter::getCondBrEncoding(const MachineInstr &MI, 1908d70411dcd5e1af47c3f4fddb993bb93c8eed6d0Chris Lattner unsigned OpNo) const { 1918d70411dcd5e1af47c3f4fddb993bb93c8eed6d0Chris Lattner const MachineOperand &MO = MI.getOperand(OpNo); 1928d70411dcd5e1af47c3f4fddb993bb93c8eed6d0Chris Lattner MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bcx)); 1938d70411dcd5e1af47c3f4fddb993bb93c8eed6d0Chris Lattner return 0; 1948d70411dcd5e1af47c3f4fddb993bb93c8eed6d0Chris Lattner} 1958d70411dcd5e1af47c3f4fddb993bb93c8eed6d0Chris Lattner 19685cf7d737dd26e984974e072d28225bd00c625c2Chris Lattnerunsigned PPCCodeEmitter::getHA16Encoding(const MachineInstr &MI, 19785cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner unsigned OpNo) const { 19885cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner const MachineOperand &MO = MI.getOperand(OpNo); 19985cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO); 20085cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner 20185cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_high)); 20285cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner return 0; 20385cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner} 20485cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner 20585cf7d737dd26e984974e072d28225bd00c625c2Chris Lattnerunsigned PPCCodeEmitter::getLO16Encoding(const MachineInstr &MI, 20685cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner unsigned OpNo) const { 20785cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner const MachineOperand &MO = MI.getOperand(OpNo); 20885cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO); 20985cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner 21085cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low)); 21185cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner return 0; 21285cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner} 21385cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner 214b7035d04421112a4585245f67bc564170ec45b29Chris Lattnerunsigned PPCCodeEmitter::getMemRIEncoding(const MachineInstr &MI, 215b7035d04421112a4585245f67bc564170ec45b29Chris Lattner unsigned OpNo) const { 216b7035d04421112a4585245f67bc564170ec45b29Chris Lattner // Encode (imm, reg) as a memri, which has the low 16-bits as the 217b7035d04421112a4585245f67bc564170ec45b29Chris Lattner // displacement and the next 5 bits as the register #. 218b7035d04421112a4585245f67bc564170ec45b29Chris Lattner assert(MI.getOperand(OpNo+1).isReg()); 219b7035d04421112a4585245f67bc564170ec45b29Chris Lattner unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 16; 220b7035d04421112a4585245f67bc564170ec45b29Chris Lattner 221b7035d04421112a4585245f67bc564170ec45b29Chris Lattner const MachineOperand &MO = MI.getOperand(OpNo); 222b7035d04421112a4585245f67bc564170ec45b29Chris Lattner if (MO.isImm()) 223b7035d04421112a4585245f67bc564170ec45b29Chris Lattner return (getMachineOpValue(MI, MO) & 0xFFFF) | RegBits; 224b7035d04421112a4585245f67bc564170ec45b29Chris Lattner 225b7035d04421112a4585245f67bc564170ec45b29Chris Lattner // Add a fixup for the displacement field. 226b7035d04421112a4585245f67bc564170ec45b29Chris Lattner MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low)); 227b7035d04421112a4585245f67bc564170ec45b29Chris Lattner return RegBits; 228b7035d04421112a4585245f67bc564170ec45b29Chris Lattner} 229b7035d04421112a4585245f67bc564170ec45b29Chris Lattner 23017e2c188359769a1df18c42593a94ce0fc2a9a75Chris Lattnerunsigned PPCCodeEmitter::getMemRIXEncoding(const MachineInstr &MI, 23117e2c188359769a1df18c42593a94ce0fc2a9a75Chris Lattner unsigned OpNo) const { 23217e2c188359769a1df18c42593a94ce0fc2a9a75Chris Lattner // Encode (imm, reg) as a memrix, which has the low 14-bits as the 23317e2c188359769a1df18c42593a94ce0fc2a9a75Chris Lattner // displacement and the next 5 bits as the register #. 23417e2c188359769a1df18c42593a94ce0fc2a9a75Chris Lattner assert(MI.getOperand(OpNo+1).isReg()); 23517e2c188359769a1df18c42593a94ce0fc2a9a75Chris Lattner unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1)) << 14; 23617e2c188359769a1df18c42593a94ce0fc2a9a75Chris Lattner 23785cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner const MachineOperand &MO = MI.getOperand(OpNo); 23817e2c188359769a1df18c42593a94ce0fc2a9a75Chris Lattner if (MO.isImm()) 23917e2c188359769a1df18c42593a94ce0fc2a9a75Chris Lattner return (getMachineOpValue(MI, MO) & 0x3FFF) | RegBits; 24085cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner 24185cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner MCE.addRelocation(GetRelocation(MO, PPC::reloc_absolute_low_ix)); 24217e2c188359769a1df18c42593a94ce0fc2a9a75Chris Lattner return RegBits; 24385cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner} 24485cf7d737dd26e984974e072d28225bd00c625c2Chris Lattner 2457192eb873201ff201681fefd1f5bf6ca2b2bc98eChris Lattner 246acff339e391d23e748b69f61ae5c27fd5620c69fEvan Chengunsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI, 247bade37bb8b83deefa166776b1b5185c237a42e71Jim Grosbach const MachineOperand &MO) const { 248b5f662fa0314f7e7e690aae8ebff7136cc3a5ab0Misha Brukman 249d735b8019b0f297d7c14b55adcd887af24d8e602Dan Gohman if (MO.isReg()) { 250b69cdfa6f3d5a0661354e744a11caa7b3342b83eChris Lattner // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand. 251b69cdfa6f3d5a0661354e744a11caa7b3342b83eChris Lattner // The GPR operand should come through here though. 252234bb38d6c421ea22229087a9835afe99e531276Hal Finkel assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MTCRF8 && 253234bb38d6c421ea22229087a9835afe99e531276Hal Finkel MI.getOpcode() != PPC::MFOCRF) || 254b69cdfa6f3d5a0661354e744a11caa7b3342b83eChris Lattner MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7); 255966aeb5788c242cfaca35c56c0ddc0ff778d4376Evan Cheng return getPPCRegisterNumbering(MO.getReg()); 2567192eb873201ff201681fefd1f5bf6ca2b2bc98eChris Lattner } 2577192eb873201ff201681fefd1f5bf6ca2b2bc98eChris Lattner 258b7035d04421112a4585245f67bc564170ec45b29Chris Lattner assert(MO.isImm() && 259b7035d04421112a4585245f67bc564170ec45b29Chris Lattner "Relocation required in an instruction that we cannot encode!"); 260b7035d04421112a4585245f67bc564170ec45b29Chris Lattner return MO.getImm(); 2615dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman} 2625dfe3a9c3bd9091f9adecc909665d52bdd4edd8cMisha Brukman 2634c7b43b43fdf943c7298718e15ab5d6dfe345be7Chris Lattner#include "PPCGenCodeEmitter.inc" 264