refactor call operand handling to eliminate special cases from printOp.
[oota-llvm.git] / lib / Target / PowerPC / PPCAsmPrinter.cpp
index 39e2040389e23a0165715bf93823655e1a470190..7367c41274caca55b091f55564f093bac05e263a 100644 (file)
@@ -43,9 +43,11 @@ using namespace llvm;
 namespace {
   Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
 
-  struct PPCAsmPrinter : public AsmPrinter {
+  class PPCAsmPrinter : public AsmPrinter {
+    std::string CurSection;
+  public:
     std::set<std::string> FnStubs, GVStubs, LinkOnceStubs;
-
+    
     PPCAsmPrinter(std::ostream &O, TargetMachine &TM)
       : AsmPrinter(O, TM), FunctionNumber(0) {}
 
@@ -61,6 +63,24 @@ namespace {
       return static_cast<PPCTargetMachine&>(TM);
     }
 
+    /// SwitchSection - Switch to the specified section of the executable if we
+    /// are not already in it!
+    ///
+    void SwitchSection(const char *NewSection, const GlobalValue *GV) {
+      std::string NS;
+      
+      if (GV && GV->hasSection())
+        NS = ".section " + GV->getSection();
+      else
+        NS = NewSection;
+      
+      if (CurSection != NS) {
+        CurSection = NS;
+        if (!CurSection.empty())
+          O << "\t" << CurSection << "\n";
+      }
+    }
+    
     unsigned enumRegToMachineReg(unsigned enumReg) {
       switch (enumReg) {
       default: assert(0 && "Unhandled register!"); break;
@@ -83,7 +103,7 @@ namespace {
     bool printInstruction(const MachineInstr *MI);
 
     void printMachineInstruction(const MachineInstr *MI);
-    void printOp(const MachineOperand &MO, bool IsCallOp = false);
+    void printOp(const MachineOperand &MO);
 
     void printOperand(const MachineInstr *MI, unsigned OpNo, MVT::ValueType VT){
       const MachineOperand &MO = MI->getOperand(OpNo);
@@ -128,10 +148,31 @@ namespace {
       if (MI->getOperand(OpNo).isImmediate()) {
         O << "$+" << MI->getOperand(OpNo).getImmedValue();
       } else {
-        printOp(MI->getOperand(OpNo),
-                TM.getInstrInfo()->isCall(MI->getOpcode()));
+        printOp(MI->getOperand(OpNo));
+      }
+    }
+    void printCallOperand(const MachineInstr *MI, unsigned OpNo,
+                          MVT::ValueType VT) {
+      const MachineOperand &MO = MI->getOperand(OpNo);
+      if (MO.getType() == MachineOperand::MO_ExternalSymbol) {
+        std::string Name(GlobalPrefix); Name += MO.getSymbolName();
+        FnStubs.insert(Name);
+        O << "L" << Name << "$stub";
+      } else if (MO.getType() == MachineOperand::MO_GlobalAddress &&
+                 isa<Function>(MO.getGlobal()) && 
+                 cast<Function>(MO.getGlobal())->isExternal()) {
+        // Dynamically-resolved functions need a stub for the function.
+        std::string Name = Mang->getValueName(MO.getGlobal());
+        FnStubs.insert(Name);
+        O << "L" << Name << "$stub";
+      } else {
+        printOp(MI->getOperand(OpNo));
       }
     }
+    void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo,
+                             MVT::ValueType VT) {
+     O << (int)MI->getOperand(OpNo).getImmedValue()*4;
+    }
     void printPICLabel(const MachineInstr *MI, unsigned OpNo,
                        MVT::ValueType VT) {
       // FIXME: should probably be converted to cout.width and cout.fill
@@ -227,18 +268,6 @@ namespace {
   };
 } // end of anonymous namespace
 
-// SwitchSection - Switch to the specified section of the executable if we are
-// not already in it!
-//
-static void SwitchSection(std::ostream &OS, std::string &CurSection,
-                          const char *NewSection) {
-  if (CurSection != NewSection) {
-    CurSection = NewSection;
-    if (!CurSection.empty())
-      OS << "\t" << NewSection << "\n";
-  }
-}
-
 /// createDarwinAsmPrinterPass - Returns a pass that prints the PPC assembly
 /// code for a MachineFunction to the given output stream, in a format that the
 /// Darwin assembler can deal with.
@@ -258,7 +287,7 @@ FunctionPass *llvm::createAIXAsmPrinter(std::ostream &o, TargetMachine &tm) {
 // Include the auto-generated portion of the assembly writer
 #include "PPCGenAsmWriter.inc"
 
-void PPCAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
+void PPCAsmPrinter::printOp(const MachineOperand &MO) {
   const MRegisterInfo &RI = *TM.getRegisterInfo();
   int new_symbol;
 
@@ -297,12 +326,6 @@ void PPCAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
     return;
 
   case MachineOperand::MO_ExternalSymbol:
-    if (IsCallOp) {
-      std::string Name(GlobalPrefix); Name += MO.getSymbolName();
-      FnStubs.insert(Name);
-      O << "L" << Name << "$stub";
-      return;
-    }
     O << GlobalPrefix << MO.getSymbolName();
     return;
 
@@ -310,16 +333,6 @@ void PPCAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
     GlobalValue *GV = MO.getGlobal();
     std::string Name = Mang->getValueName(GV);
 
-    // Dynamically-resolved functions need a stub for the function.  Be
-    // wary however not to output $stub for external functions whose addresses
-    // are taken.  Those should be emitted as $non_lazy_ptr below.
-    Function *F = dyn_cast<Function>(GV);
-    if (F && IsCallOp && F->isExternal()) {
-      FnStubs.insert(Name);
-      O << "L" << Name << "$stub";
-      return;
-    }
-
     // External or weakly linked global variables need non-lazily-resolved stubs
     if ((GV->isExternal() || GV->hasWeakLinkage() || GV->hasLinkOnceLinkage())){
       if (GV->hasLinkOnceLinkage())
@@ -387,9 +400,10 @@ bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
   printConstantPool(MF.getConstantPool());
 
   // Print out labels for the function.
-  O << "\t.text\n";
-  emitAlignment(4);
-  if (!MF.getFunction()->hasInternalLinkage())
+  const Function *F = MF.getFunction();
+  SwitchSection(".text", F);
+  emitAlignment(4, F);
+  if (!F->hasInternalLinkage())
     O << "\t.globl\t" << CurrentFnName << "\n";
   O << CurrentFnName << ":\n";
 
@@ -428,7 +442,7 @@ void DarwinAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
   if (CP.empty()) return;
 
   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
-    O << "\t.const\n";
+    SwitchSection(".const", 0);
     // FIXME: force doubles to be naturally aligned.  We should handle this
     // more correctly in the future.
     if (Type::DoubleTy == CP[i]->getType())
@@ -444,6 +458,7 @@ void DarwinAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
 bool DarwinAsmPrinter::doInitialization(Module &M) {
   if (TM.getSubtarget<PPCSubtarget>().isGigaProcessor())
     O << "\t.machine ppc970\n";
+  SwitchSection("", 0);
   AsmPrinter::doInitialization(M);
   
   // Darwin wants symbols to be quoted if they have complex names.
@@ -453,10 +468,10 @@ bool DarwinAsmPrinter::doInitialization(Module &M) {
 
 bool DarwinAsmPrinter::doFinalization(Module &M) {
   const TargetData &TD = TM.getTargetData();
-  std::string CurSection;
 
   // Print out module-level global variables here.
-  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
+  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
+       I != E; ++I)
     if (I->hasInitializer()) {   // External global require no code
       O << '\n';
       std::string name = Mang->getValueName(I);
@@ -467,7 +482,7 @@ bool DarwinAsmPrinter::doFinalization(Module &M) {
       if (C->isNullValue() && /* FIXME: Verify correct */
           (I->hasInternalLinkage() || I->hasWeakLinkage() ||
            I->hasLinkOnceLinkage())) {
-        SwitchSection(O, CurSection, ".data");
+        SwitchSection(".data", I);
         if (Size == 0) Size = 1;   // .comm Foo, 0 is undefined, avoid it.
         if (I->hasInternalLinkage())
           O << ".lcomm " << name << "," << Size << "," << Align;
@@ -477,6 +492,7 @@ bool DarwinAsmPrinter::doFinalization(Module &M) {
       } else {
         switch (I->getLinkage()) {
         case GlobalValue::LinkOnceLinkage:
+          SwitchSection("", 0);
           O << ".section __TEXT,__textcoal_nt,coalesced,no_toc\n"
             << ".weak_definition " << name << '\n'
             << ".private_extern " << name << '\n'
@@ -495,14 +511,14 @@ bool DarwinAsmPrinter::doFinalization(Module &M) {
           O << "\t.globl " << name << "\n";
           // FALL THROUGH
         case GlobalValue::InternalLinkage:
-          SwitchSection(O, CurSection, ".data");
+          SwitchSection(".data", I);
           break;
-        case GlobalValue::GhostLinkage:
-          std::cerr << "Error: unmaterialized (GhostLinkage) function in asm!";
+        default:
+          std::cerr << "Unknown linkage type!";
           abort();
         }
 
-        emitAlignment(Align);
+        emitAlignment(Align, I);
         O << name << ":\t\t\t\t; '" << I->getName() << "'\n";
         emitGlobalConstant(C);
       }
@@ -639,7 +655,7 @@ void AIXAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
   if (CP.empty()) return;
 
   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
-    O << "\t.const\n";
+    SwitchSection(".const", 0);
     O << "\t.align " << (unsigned)TD.getTypeAlignment(CP[i]->getType())
       << "\n";
     O << "LCPI" << FunctionNumber << '_' << i << ":\t\t\t\t\t;"
@@ -649,8 +665,8 @@ void AIXAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
 }
 
 bool AIXAsmPrinter::doInitialization(Module &M) {
+  SwitchSection("", 0);
   const TargetData &TD = TM.getTargetData();
-  std::string CurSection;
 
   O << "\t.machine \"ppc64\"\n"
     << "\t.toc\n"