LLVM API Documentation

PPCCodeEmitter.cpp

Go to the documentation of this file.
00001 //===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
00011 // JIT-compile bitcode to native PowerPC.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "PPCTargetMachine.h"
00016 #include "PPCRelocations.h"
00017 #include "PPC.h"
00018 #include "llvm/Module.h"
00019 #include "llvm/PassManager.h"
00020 #include "llvm/CodeGen/MachineCodeEmitter.h"
00021 #include "llvm/CodeGen/JITCodeEmitter.h"
00022 #include "llvm/CodeGen/ObjectCodeEmitter.h"
00023 #include "llvm/CodeGen/MachineFunctionPass.h"
00024 #include "llvm/CodeGen/MachineInstrBuilder.h"
00025 #include "llvm/CodeGen/MachineModuleInfo.h"
00026 #include "llvm/CodeGen/Passes.h"
00027 #include "llvm/Support/Debug.h"
00028 #include "llvm/Support/Compiler.h"
00029 #include "llvm/Support/ErrorHandling.h"
00030 #include "llvm/Support/raw_ostream.h"
00031 #include "llvm/Target/TargetOptions.h"
00032 using namespace llvm;
00033 
00034 namespace {
00035   class PPCCodeEmitter {
00036     TargetMachine &TM;
00037     MachineCodeEmitter &MCE;
00038   public:
00039     PPCCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce):
00040         TM(tm), MCE(mce) {}
00041 
00042     /// getBinaryCodeForInstr - This function, generated by the
00043     /// CodeEmitterGenerator using TableGen, produces the binary encoding for
00044     /// machine instructions.
00045 
00046     unsigned getBinaryCodeForInstr(const MachineInstr &MI);
00047 
00048     /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
00049 
00050     unsigned getMachineOpValue(const MachineInstr &MI,
00051                                const MachineOperand &MO);
00052 
00053     /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
00054     /// its address in the function into this pointer.
00055 
00056     void *MovePCtoLROffset;
00057   };
00058 
00059   template <class CodeEmitter>
00060   class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
00061       public PPCCodeEmitter
00062   {
00063     TargetMachine &TM;
00064     CodeEmitter &MCE;
00065 
00066     void getAnalysisUsage(AnalysisUsage &AU) const {
00067       AU.addRequired<MachineModuleInfo>();
00068       MachineFunctionPass::getAnalysisUsage(AU);
00069     }
00070 
00071   public:
00072     static char ID;
00073     Emitter(TargetMachine &tm, CodeEmitter &mce)
00074       : MachineFunctionPass(&ID), PPCCodeEmitter(tm, mce), TM(tm), MCE(mce) {}
00075 
00076     const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
00077 
00078     /// runOnMachineFunction - emits the given MachineFunction to memory
00079     ///
00080     bool runOnMachineFunction(MachineFunction &MF);
00081 
00082     /// emitBasicBlock - emits the given MachineBasicBlock to memory
00083     ///
00084     void emitBasicBlock(MachineBasicBlock &MBB);
00085 
00086     /// getValueBit - return the particular bit of Val
00087     ///
00088     unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
00089   };
00090 
00091   template <class CodeEmitter>
00092     char Emitter<CodeEmitter>::ID = 0;
00093 }
00094   
00095 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
00096 /// to the specified MCE object.
00097 
00098 FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
00099                                              MachineCodeEmitter &MCE) {
00100   return new Emitter<MachineCodeEmitter>(TM, MCE);
00101 }
00102 
00103 FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
00104                                                 JITCodeEmitter &JCE) {
00105   return new Emitter<JITCodeEmitter>(TM, JCE);
00106 }
00107 
00108 FunctionPass *llvm::createPPCObjectCodeEmitterPass(PPCTargetMachine &TM,
00109                                                    ObjectCodeEmitter &OCE) {
00110   return new Emitter<ObjectCodeEmitter>(TM, OCE);
00111 }
00112 
00113 template <class CodeEmitter>
00114 bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
00115   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
00116           MF.getTarget().getRelocationModel() != Reloc::Static) &&
00117          "JIT relocation model must be set to static or default!");
00118 
00119   MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
00120   do {
00121     MovePCtoLROffset = 0;
00122     MCE.startFunction(MF);
00123     for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
00124       emitBasicBlock(*BB);
00125   } while (MCE.finishFunction(MF));
00126 
00127   return false;
00128 }
00129 
00130 template <class CodeEmitter>
00131 void Emitter<CodeEmitter>::emitBasicBlock(MachineBasicBlock &MBB) {
00132   MCE.StartMachineBasicBlock(&MBB);
00133   
00134   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
00135     const MachineInstr &MI = *I;
00136     MCE.processDebugLoc(MI.getDebugLoc());
00137     switch (MI.getOpcode()) {
00138     default:
00139       MCE.emitWordBE(getBinaryCodeForInstr(MI));
00140       break;
00141     case TargetInstrInfo::DBG_LABEL:
00142     case TargetInstrInfo::EH_LABEL:
00143       MCE.emitLabel(MI.getOperand(0).getImm());
00144       break;
00145     case TargetInstrInfo::IMPLICIT_DEF:
00146       break; // pseudo opcode, no side effects
00147     case PPC::MovePCtoLR:
00148     case PPC::MovePCtoLR8:
00149       assert(TM.getRelocationModel() == Reloc::PIC_);
00150       MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
00151       MCE.emitWordBE(0x48000005);   // bl 1
00152       break;
00153     }
00154   }
00155 }
00156 
00157 unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
00158                                            const MachineOperand &MO) {
00159 
00160   unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
00161                    // or things that get fixed up later by the JIT.
00162   if (MO.isReg()) {
00163     rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
00164 
00165     // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
00166     // register, not the register number directly.
00167     if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
00168         (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
00169       rv = 0x80 >> rv;
00170     }
00171   } else if (MO.isImm()) {
00172     rv = MO.getImm();
00173   } else if (MO.isGlobal() || MO.isSymbol() ||
00174              MO.isCPI() || MO.isJTI()) {
00175     unsigned Reloc = 0;
00176     if (MI.getOpcode() == PPC::BL_Darwin || MI.getOpcode() == PPC::BL8_Darwin ||
00177         MI.getOpcode() == PPC::BL_SVR4 || MI.getOpcode() == PPC::BL8_ELF ||
00178         MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8)
00179       Reloc = PPC::reloc_pcrel_bx;
00180     else {
00181       if (TM.getRelocationModel() == Reloc::PIC_) {
00182         assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
00183       }
00184       switch (MI.getOpcode()) {
00185       default: MI.dump(); llvm_unreachable("Unknown instruction for relocation!");
00186       case PPC::LIS:
00187       case PPC::LIS8:
00188       case PPC::ADDIS:
00189       case PPC::ADDIS8:
00190         Reloc = PPC::reloc_absolute_high;       // Pointer to symbol
00191         break;
00192       case PPC::LI:
00193       case PPC::LI8:
00194       case PPC::LA:
00195       // Loads.
00196       case PPC::LBZ:
00197       case PPC::LBZ8:
00198       case PPC::LHA:
00199       case PPC::LHA8:
00200       case PPC::LHZ:
00201       case PPC::LHZ8:
00202       case PPC::LWZ:
00203       case PPC::LWZ8:
00204       case PPC::LFS:
00205       case PPC::LFD:
00206       
00207       // Stores.
00208       case PPC::STB:
00209       case PPC::STB8:
00210       case PPC::STH:
00211       case PPC::STH8:
00212       case PPC::STW:
00213       case PPC::STW8:
00214       case PPC::STFS:
00215       case PPC::STFD:
00216         Reloc = PPC::reloc_absolute_low;
00217         break;
00218 
00219       case PPC::LWA:
00220       case PPC::LD:
00221       case PPC::STD:
00222       case PPC::STD_32:
00223         Reloc = PPC::reloc_absolute_low_ix;
00224         break;
00225       }
00226     }
00227     
00228     MachineRelocation R;
00229     if (MO.isGlobal()) {
00230       R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
00231                                    MO.getGlobal(), 0,
00232                                    isa<Function>(MO.getGlobal()));
00233     } else if (MO.isSymbol()) {
00234       R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
00235                                        Reloc, MO.getSymbolName(), 0);
00236     } else if (MO.isCPI()) {
00237       R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
00238                                           Reloc, MO.getIndex(), 0);
00239     } else {
00240       assert(MO.isJTI());
00241       R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
00242                                           Reloc, MO.getIndex(), 0);
00243     }
00244     
00245     // If in PIC mode, we need to encode the negated address of the
00246     // 'movepctolr' into the unrelocated field.  After relocation, we'll have
00247     // &gv-&movepctolr-4 in the imm field.  Once &movepctolr is added to the imm
00248     // field, we get &gv.  This doesn't happen for branch relocations, which are
00249     // always implicitly pc relative.
00250     if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){
00251       assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
00252       R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
00253     }
00254     MCE.addRelocation(R);
00255     
00256   } else if (MO.isMBB()) {
00257     unsigned Reloc = 0;
00258     unsigned Opcode = MI.getOpcode();
00259     if (Opcode == PPC::B || Opcode == PPC::BL_Darwin ||
00260         Opcode == PPC::BLA_Darwin|| Opcode == PPC::BL_SVR4 ||
00261         Opcode == PPC::BLA_SVR4)
00262       Reloc = PPC::reloc_pcrel_bx;
00263     else // BCC instruction
00264       Reloc = PPC::reloc_pcrel_bcx;
00265 
00266     MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
00267                                                Reloc, MO.getMBB()));
00268   } else {
00269 #ifndef NDEBUG
00270     cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
00271 #endif
00272     llvm_unreachable(0);
00273   }
00274 
00275   return rv;
00276 }
00277 
00278 #include "PPCGenCodeEmitter.inc"