Rewrite tblgen handling of subtarget features so
[oota-llvm.git] / lib / Target / PowerPC / PPCAsmPrinter.cpp
index bac735a17b6b6ac6a031525e84a4675d950df77a..605dc128db4491a9e2d62c35c1b0ca65bbc571f9 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
 #include "llvm/CodeGen/MachineModuleInfo.h"
 #include "llvm/CodeGen/MachineFunctionPass.h"
 #include "llvm/CodeGen/MachineInstr.h"
+#include "llvm/CodeGen/MachineInstrBuilder.h"
 #include "llvm/Support/Mangler.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/Compiler.h"
 #include "llvm/Target/TargetAsmInfo.h"
-#include "llvm/Target/MRegisterInfo.h"
+#include "llvm/Target/TargetRegisterInfo.h"
 #include "llvm/Target/TargetInstrInfo.h"
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/ADT/Statistic.h"
@@ -104,7 +105,7 @@ namespace {
     ///
     void printRegister(const MachineOperand &MO, bool R0AsZero) {
       unsigned RegNo = MO.getReg();
-      assert(MRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
+      assert(TargetRegisterInfo::isPhysicalRegister(RegNo) && "Not physreg??");
       
       // If we should use 0 for R0.
       if (R0AsZero && RegNo == PPC::R0) {
@@ -124,7 +125,7 @@ namespace {
       if (MO.isRegister()) {
         printRegister(MO, false);
       } else if (MO.isImmediate()) {
-        O << MO.getImmedValue();
+        O << MO.getImm();
       } else {
         printOp(MO);
       }
@@ -137,29 +138,29 @@ namespace {
     
     
     void printS5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
-      char value = MI->getOperand(OpNo).getImmedValue();
+      char value = MI->getOperand(OpNo).getImm();
       value = (value << (32-5)) >> (32-5);
       O << (int)value;
     }
     void printU5ImmOperand(const MachineInstr *MI, unsigned OpNo) {
-      unsigned char value = MI->getOperand(OpNo).getImmedValue();
+      unsigned char value = MI->getOperand(OpNo).getImm();
       assert(value <= 31 && "Invalid u5imm argument!");
       O << (unsigned int)value;
     }
     void printU6ImmOperand(const MachineInstr *MI, unsigned OpNo) {
-      unsigned char value = MI->getOperand(OpNo).getImmedValue();
+      unsigned char value = MI->getOperand(OpNo).getImm();
       assert(value <= 63 && "Invalid u6imm argument!");
       O << (unsigned int)value;
     }
     void printS16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
-      O << (short)MI->getOperand(OpNo).getImmedValue();
+      O << (short)MI->getOperand(OpNo).getImm();
     }
     void printU16ImmOperand(const MachineInstr *MI, unsigned OpNo) {
-      O << (unsigned short)MI->getOperand(OpNo).getImmedValue();
+      O << (unsigned short)MI->getOperand(OpNo).getImm();
     }
     void printS16X4ImmOperand(const MachineInstr *MI, unsigned OpNo) {
       if (MI->getOperand(OpNo).isImmediate()) {
-        O << (short)(MI->getOperand(OpNo).getImmedValue()*4);
+        O << (short)(MI->getOperand(OpNo).getImm()*4);
       } else {
         O << "lo16(";
         printOp(MI->getOperand(OpNo));
@@ -173,7 +174,7 @@ namespace {
       // Branches can take an immediate operand.  This is used by the branch
       // selection pass to print $+8, an eight byte displacement from the PC.
       if (MI->getOperand(OpNo).isImmediate()) {
-        O << "$+" << MI->getOperand(OpNo).getImmedValue()*4;
+        O << "$+" << MI->getOperand(OpNo).getImm()*4;
       } else {
         printOp(MI->getOperand(OpNo));
       }
@@ -205,7 +206,7 @@ namespace {
       printOp(MI->getOperand(OpNo));
     }
     void printAbsAddrOperand(const MachineInstr *MI, unsigned OpNo) {
-     O << (int)MI->getOperand(OpNo).getImmedValue()*4;
+     O << (int)MI->getOperand(OpNo).getImm()*4;
     }
     void printPICLabel(const MachineInstr *MI, unsigned OpNo) {
       O << "\"L" << getFunctionNumber() << "$pb\"\n";
@@ -360,16 +361,16 @@ void PPCAsmPrinter::printOp(const MachineOperand &MO) {
     return;
 
   case MachineOperand::MO_MachineBasicBlock:
-    printBasicBlockLabel(MO.getMachineBasicBlock());
+    printBasicBlockLabel(MO.getMBB());
     return;
   case MachineOperand::MO_JumpTableIndex:
     O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
-      << '_' << MO.getJumpTableIndex();
+      << '_' << MO.getIndex();
     // FIXME: PIC relocation model
     return;
   case MachineOperand::MO_ConstantPoolIndex:
     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber()
-      << '_' << MO.getConstantPoolIndex();
+      << '_' << MO.getIndex();
     return;
   case MachineOperand::MO_ExternalSymbol:
     // Computing the address of an external symbol, not calling it.
@@ -509,14 +510,14 @@ void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
   // Check for slwi/srwi mnemonics.
   if (MI->getOpcode() == PPC::RLWINM) {
     bool FoundMnemonic = false;
-    unsigned char SH = MI->getOperand(2).getImmedValue();
-    unsigned char MB = MI->getOperand(3).getImmedValue();
-    unsigned char ME = MI->getOperand(4).getImmedValue();
+    unsigned char SH = MI->getOperand(2).getImm();
+    unsigned char MB = MI->getOperand(3).getImm();
+    unsigned char ME = MI->getOperand(4).getImm();
     if (SH <= 31 && MB == 0 && ME == (31-SH)) {
-      O << "slwi "; FoundMnemonic = true;
+      O << "\tslwi "; FoundMnemonic = true;
     }
     if (SH <= 31 && MB == (32-SH) && ME == 31) {
-      O << "srwi "; FoundMnemonic = true;
+      O << "\tsrwi "; FoundMnemonic = true;
       SH = 32-SH;
     }
     if (FoundMnemonic) {
@@ -528,7 +529,7 @@ void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
     }
   } else if (MI->getOpcode() == PPC::OR || MI->getOpcode() == PPC::OR8) {
     if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) {
-      O << "mr ";
+      O << "\tmr ";
       printOperand(MI, 0);
       O << ", ";
       printOperand(MI, 1);
@@ -536,11 +537,11 @@ void PPCAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
       return;
     }
   } else if (MI->getOpcode() == PPC::RLDICR) {
-    unsigned char SH = MI->getOperand(2).getImmedValue();
-    unsigned char ME = MI->getOperand(3).getImmedValue();
+    unsigned char SH = MI->getOperand(2).getImm();
+    unsigned char ME = MI->getOperand(3).getImm();
     // rldicr RA, RS, SH, 63-SH == sldi RA, RS, SH
     if (63-SH == ME) {
-      O << "sldi ";
+      O << "\tsldi ";
       printOperand(MI, 0);
       O << ", ";
       printOperand(MI, 1);
@@ -609,7 +610,6 @@ bool LinuxAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
          II != E; ++II) {
       // Print the assembly for the instruction.
-      O << "\t";
       printMachineInstruction(II);
     }
   }
@@ -767,7 +767,7 @@ std::string DarwinAsmPrinter::getSectionForFunction(const Function &F) const {
   case Function::InternalLinkage: return TAI->getTextSection();
   case Function::WeakLinkage:
   case Function::LinkOnceLinkage:
-    return ".section __TEXT,__textcoal_nt,coalesced,pure_instructions";
+    return "\t.section __TEXT,__textcoal_nt,coalesced,pure_instructions";
   }
 }
 
@@ -813,6 +813,14 @@ bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
   // Emit pre-function debug information.
   DW.BeginFunction(&MF);
 
+  // If the function is empty, then we need to emit *something*. Otherwise, the
+  // function's label might be associated with something that it wasn't meant to
+  // be associated with. We emit a noop in this situation.
+  MachineFunction::iterator I = MF.begin();
+
+  if (++I == MF.end() && MF.front().empty())
+    O << "\tnop\n";
+
   // Print out code for the function.
   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
        I != E; ++I) {
@@ -821,10 +829,9 @@ bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
       printBasicBlockLabel(I, true);
       O << '\n';
     }
-    for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
-         II != E; ++II) {
+    for (MachineBasicBlock::const_iterator II = I->begin(), IE = I->end();
+         II != IE; ++II) {
       // Print the assembly for the instruction.
-      O << "\t";
       printMachineInstruction(II);
     }
   }
@@ -842,6 +849,7 @@ bool DarwinAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
 
 bool DarwinAsmPrinter::doInitialization(Module &M) {
   static const char *CPUDirectives[] = {
+    "",
     "ppc",
     "ppc601",
     "ppc602",
@@ -869,13 +877,13 @@ bool DarwinAsmPrinter::doInitialization(Module &M) {
   
   // Prime text sections so they are adjacent.  This reduces the likelihood a
   // large data or debug section causes a branch to exceed 16M limit.
-  SwitchToTextSection(".section __TEXT,__textcoal_nt,coalesced,"
+  SwitchToTextSection("\t.section __TEXT,__textcoal_nt,coalesced,"
                       "pure_instructions");
   if (TM.getRelocationModel() == Reloc::PIC_) {
-    SwitchToTextSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
+    SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
                           "pure_instructions,32");
   } else if (TM.getRelocationModel() == Reloc::DynamicNoPIC) {
-    SwitchToTextSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
+    SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
                         "pure_instructions,16");
   }
   SwitchToTextSection(TAI->getTextSection());
@@ -930,6 +938,9 @@ bool DarwinAsmPrinter::doFinalization(Module &M) {
       } else {
         SwitchToDataSection("\t.data", I);
         O << ".comm " << name << "," << Size;
+        // Darwin 9 and above support aligned common data.
+        if (Subtarget.isDarwin9())
+          O << "," << Align;
       }
       O << "\t\t" << TAI->getCommentString() << " '" << I->getName() << "'\n";
     } else {
@@ -938,7 +949,7 @@ bool DarwinAsmPrinter::doFinalization(Module &M) {
       case GlobalValue::WeakLinkage:
         O << "\t.globl " << name << '\n'
           << "\t.weak_definition " << name << '\n';
-        SwitchToDataSection(".section __DATA,__datacoal_nt,coalesced", I);
+        SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
         break;
       case GlobalValue::AppendingLinkage:
         // FIXME: appending linkage variables should go into a section of
@@ -955,8 +966,11 @@ bool DarwinAsmPrinter::doFinalization(Module &M) {
             break;
           }
         }
-
-        if (!I->isConstant())
+        if (I->hasSection()) {
+          // Honor all section names on Darwin; ObjC uses this
+          std::string SectionName = ".section " + I->getSection();
+          SwitchToDataSection(SectionName.c_str());
+        } else if (!I->isConstant())
           SwitchToDataSection(TAI->getDataSection(), I);
         else {
           // Read-only data.
@@ -1005,7 +1019,7 @@ bool DarwinAsmPrinter::doFinalization(Module &M) {
   if (TM.getRelocationModel() == Reloc::PIC_) {
     for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
          i != e; ++i) {
-      SwitchToTextSection(".section __TEXT,__picsymbolstub1,symbol_stubs,"
+      SwitchToTextSection("\t.section __TEXT,__picsymbolstub1,symbol_stubs,"
                           "pure_instructions,32");
       EmitAlignment(4);
       O << "L" << *i << "$stub:\n";
@@ -1033,7 +1047,7 @@ bool DarwinAsmPrinter::doFinalization(Module &M) {
   } else {
     for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
          i != e; ++i) {
-      SwitchToTextSection(".section __TEXT,__symbol_stub1,symbol_stubs,"
+      SwitchToTextSection("\t.section __TEXT,__symbol_stub1,symbol_stubs,"
                           "pure_instructions,16");
       EmitAlignment(4);
       O << "L" << *i << "$stub:\n";