Reimplement rip-relative addressing in the X86-64 backend. The new
[oota-llvm.git] / lib / Target / PowerPC / PPCCodeEmitter.cpp
index c0621a074f342df2973f6e9129e7edb97d1967f8..aa3dce19e505ce0bc1c6dc7d7fb511a09155fb3b 100644 (file)
 #include "llvm/Module.h"
 #include "llvm/PassManager.h"
 #include "llvm/CodeGen/MachineCodeEmitter.h"
+#include "llvm/CodeGen/JITCodeEmitter.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/CodeGen/Passes.h"
-#include "llvm/Support/Debug.h"                   
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Target/TargetOptions.h"
 using namespace llvm;
 
 namespace {
-  class VISIBILITY_HIDDEN PPCCodeEmitter : public MachineFunctionPass {
+  class PPCCodeEmitter {
     TargetMachine &TM;
     MachineCodeEmitter &MCE;
+  public:
+    PPCCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce):
+        TM(tm), MCE(mce) {}
+
+    /// getBinaryCodeForInstr - This function, generated by the
+    /// CodeEmitterGenerator using TableGen, produces the binary encoding for
+    /// machine instructions.
+
+    unsigned getBinaryCodeForInstr(const MachineInstr &MI);
+
+    /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
+
+    unsigned getMachineOpValue(const MachineInstr &MI,
+                               const MachineOperand &MO);
 
     /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
     /// its address in the function into this pointer.
+
     void *MovePCtoLROffset;
-    
-    /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
-    ///
-    int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
-    
+  };
+
+  template <class CodeEmitter>
+  class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
+      public PPCCodeEmitter
+  {
+    TargetMachine &TM;
+    CodeEmitter &MCE;
+
     void getAnalysisUsage(AnalysisUsage &AU) const {
       AU.addRequired<MachineModuleInfo>();
       MachineFunctionPass::getAnalysisUsage(AU);
@@ -47,8 +67,8 @@ namespace {
 
   public:
     static char ID;
-    PPCCodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
-      : MachineFunctionPass((intptr_t)&ID), TM(T), MCE(M) {}
+    Emitter(TargetMachine &tm, CodeEmitter &mce)
+      : MachineFunctionPass(&ID), PPCCodeEmitter(tm, mce), TM(tm), MCE(mce) {}
 
     const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
 
@@ -63,28 +83,26 @@ namespace {
     /// getValueBit - return the particular bit of Val
     ///
     unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
-
-    /// getBinaryCodeForInstr - This function, generated by the
-    /// CodeEmitterGenerator using TableGen, produces the binary encoding for
-    /// machine instructions.
-    ///
-    unsigned getBinaryCodeForInstr(MachineInstr &MI);
   };
-  char PPCCodeEmitter::ID = 0;
-}
 
+  template <class CodeEmitter>
+    char Emitter<CodeEmitter>::ID = 0;
+}
+       
 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
 /// to the specified MCE object.
 FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
                                              MachineCodeEmitter &MCE) {
-  return new PPCCodeEmitter(TM, MCE);
+  return new Emitter<MachineCodeEmitter>(TM, MCE);
 }
 
-#ifdef __APPLE__ 
-extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
-#endif
+FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
+                                                JITCodeEmitter &JCE) {
+  return new Emitter<JITCodeEmitter>(TM, JCE);
+}
 
-bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
+template <class CodeEmitter>
+bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
   assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
           MF.getTarget().getRelocationModel() != Reloc::Static) &&
          "JIT relocation model must be set to static or default!");
@@ -100,16 +118,18 @@ bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
   return false;
 }
 
-void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
+template <class CodeEmitter>
+void Emitter<CodeEmitter>::emitBasicBlock(MachineBasicBlock &MBB) {
   MCE.StartMachineBasicBlock(&MBB);
   
   for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
-    MachineInstr &MI = *I;
+    const MachineInstr &MI = *I;
     switch (MI.getOpcode()) {
     default:
-      MCE.emitWordBE(getBinaryCodeForInstr(*I));
+      MCE.emitWordBE(getBinaryCodeForInstr(MI));
       break;
-    case TargetInstrInfo::LABEL:
+    case TargetInstrInfo::DBG_LABEL:
+    case TargetInstrInfo::EH_LABEL:
       MCE.emitLabel(MI.getOperand(0).getImm());
       break;
     case TargetInstrInfo::IMPLICIT_DEF:
@@ -124,11 +144,12 @@ void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
   }
 }
 
-int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
+unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
+                                           const MachineOperand &MO) {
 
-  intptr_t rv = 0; // Return value; defaults to 0 for unhandled cases
+  unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
                    // or things that get fixed up later by the JIT.
-  if (MO.isRegister()) {
+  if (MO.isReg()) {
     rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
 
     // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
@@ -137,10 +158,10 @@ int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
         (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
       rv = 0x80 >> rv;
     }
-  } else if (MO.isImmediate()) {
+  } else if (MO.isImm()) {
     rv = MO.getImm();
-  } else if (MO.isGlobalAddress() || MO.isExternalSymbol() ||
-             MO.isConstantPoolIndex() || MO.isJumpTableIndex()) {
+  } else if (MO.isGlobal() || MO.isSymbol() ||
+             MO.isCPI() || MO.isJTI()) {
     unsigned Reloc = 0;
     if (MI.getOpcode() == PPC::BL_Macho || MI.getOpcode() == PPC::BL8_Macho ||
         MI.getOpcode() == PPC::BL_ELF || MI.getOpcode() == PPC::BL8_ELF ||
@@ -195,18 +216,18 @@ int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
     }
     
     MachineRelocation R;
-    if (MO.isGlobalAddress()) {
+    if (MO.isGlobal()) {
       R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
                                    MO.getGlobal(), 0,
                                    isa<Function>(MO.getGlobal()));
-    } else if (MO.isExternalSymbol()) {
+    } else if (MO.isSymbol()) {
       R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
                                        Reloc, MO.getSymbolName(), 0);
-    } else if (MO.isConstantPoolIndex()) {
+    } else if (MO.isCPI()) {
       R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
                                           Reloc, MO.getIndex(), 0);
     } else {
-      assert(MO.isJumpTableIndex());
+      assert(MO.isJTI());
       R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
                                           Reloc, MO.getIndex(), 0);
     }
@@ -222,7 +243,7 @@ int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
     }
     MCE.addRelocation(R);
     
-  } else if (MO.isMachineBasicBlock()) {
+  } else if (MO.isMBB()) {
     unsigned Reloc = 0;
     unsigned Opcode = MI.getOpcode();
     if (Opcode == PPC::B || Opcode == PPC::BL_Macho ||