Remove attribution from file headers, per discussion on llvmdev.
[oota-llvm.git] / lib / Target / Alpha / AlphaAsmPrinter.cpp
index 74af78e250a6595c7d7feff1421390825801a27e..a55dd7479a05973361761d9bd3ab345c630e3fee 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.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -12,6 +12,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#define DEBUG_TYPE "asm-printer"
 #include "Alpha.h"
 #include "AlphaInstrInfo.h"
 #include "AlphaTargetMachine.h"
 #include "llvm/Type.h"
 #include "llvm/Assembly/Writer.h"
 #include "llvm/CodeGen/AsmPrinter.h"
+#include "llvm/Target/TargetAsmInfo.h"
 #include "llvm/Target/TargetMachine.h"
+#include "llvm/Support/Compiler.h"
 #include "llvm/Support/Mangler.h"
 #include "llvm/ADT/Statistic.h"
-#include <iostream>
 using namespace llvm;
 
-namespace {
-  Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
+STATISTIC(EmittedInsts, "Number of machine instrs printed");
 
-  struct AlphaAsmPrinter : public AsmPrinter {
+namespace {
+  struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
 
     /// Unique incrementer for label values for referencing Global values.
     ///
-    unsigned LabelNumber;
 
-     AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
-       : AsmPrinter(o, tm), LabelNumber(0) {
-      AlignmentIsInBytes = false;
-      PrivateGlobalPrefix = "$";
+    AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
+      : AsmPrinter(o, tm, T) {
     }
 
-    /// We name each basic block in a Function with a unique number, so
-    /// that we can consistently refer to them later. This is cleared
-    /// at the beginning of each call to runOnMachineFunction().
-    ///
-    typedef std::map<const Value *, unsigned> ValueMapTy;
-    ValueMapTy NumberForBB;
-    std::string CurSection;
-
     virtual const char *getPassName() const {
       return "Alpha Assembly Printer";
     }
@@ -55,10 +46,16 @@ namespace {
     void printOp(const MachineOperand &MO, bool IsCallOp = false);
     void printOperand(const MachineInstr *MI, int opNum);
     void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
-    void printMachineInstruction(const MachineInstr *MI);
     bool runOnMachineFunction(MachineFunction &F);
     bool doInitialization(Module &M);
     bool doFinalization(Module &M);
+    
+    bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
+                         unsigned AsmVariant, const char *ExtraCode);
+    bool PrintAsmMemoryOperand(const MachineInstr *MI, 
+                               unsigned OpNo,
+                               unsigned AsmVariant, 
+                               const char *ExtraCode);
   };
 } // end of anonymous namespace
 
@@ -67,9 +64,9 @@ namespace {
 /// using the given target machine description.  This should work
 /// regardless of whether the function is in SSA form.
 ///
-FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
-                                                  TargetMachine &tm) {
-  return new AlphaAsmPrinter(o, tm);
+FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
+                                               TargetMachine &tm) {
+  return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
 }
 
 #include "AlphaGenAsmWriter.inc"
@@ -77,11 +74,12 @@ FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
 {
   const MachineOperand &MO = MI->getOperand(opNum);
-  if (MO.getType() == MachineOperand::MO_MachineRegister) {
+  if (MO.getType() == MachineOperand::MO_Register) {
     assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
     O << TM.getRegisterInfo()->get(MO.getReg()).Name;
   } else if (MO.isImmediate()) {
     O << MO.getImmedValue();
+    assert(MO.getImmedValue() < (1 << 30));
   } else {
     printOp(MO);
   }
@@ -90,28 +88,14 @@ void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
 
 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
   const MRegisterInfo &RI = *TM.getRegisterInfo();
-  int new_symbol;
 
   switch (MO.getType()) {
-  case MachineOperand::MO_VirtualRegister:
-    if (Value *V = MO.getVRegValueOrNull()) {
-      O << "<" << V->getName() << ">";
-      return;
-    }
-    // FALLTHROUGH
-  case MachineOperand::MO_MachineRegister:
-  case MachineOperand::MO_CCRegister:
+  case MachineOperand::MO_Register:
     O << RI.get(MO.getReg()).Name;
     return;
 
-  case MachineOperand::MO_SignExtendedImmed:
-  case MachineOperand::MO_UnextendedImmed:
-    std::cerr << "printOp() does not handle immediate values\n";
-    abort();
-    return;
-
-  case MachineOperand::MO_PCRelativeDisp:
-    std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
+  case MachineOperand::MO_Immediate:
+    cerr << "printOp() does not handle immediate values\n";
     abort();
     return;
 
@@ -120,7 +104,7 @@ void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
     return;
 
   case MachineOperand::MO_ConstantPoolIndex:
-    O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
+    O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
       << MO.getConstantPoolIndex();
     return;
 
@@ -128,13 +112,17 @@ void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
     O << MO.getSymbolName();
     return;
 
-  case MachineOperand::MO_GlobalAddress:
-    //Abuse PCrel to specify pcrel calls
-    //calls are the only thing that use this flag
-//     if (MO.isPCRelative())
-//       O << PrivateGlobalPrefix << Mang->getValueName(MO.getGlobal()) << "..ng";
-//     else
-      O << Mang->getValueName(MO.getGlobal());
+  case MachineOperand::MO_GlobalAddress: {
+    GlobalValue *GV = MO.getGlobal();
+    O << Mang->getValueName(GV);
+    if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
+      ExtWeakSymbols.insert(GV);
+    return;
+  }
+
+  case MachineOperand::MO_JumpTableIndex:
+    O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
+      << '_' << MO.getJumpTableIndex();
     return;
 
   default:
@@ -143,20 +131,6 @@ void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
   }
 }
 
-/// printMachineInstruction -- Print out a single Alpha MI to
-/// the current output stream.
-///
-void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
-  ++EmittedInsts;
-  if (printInstruction(MI))
-    return; // Printer was automatically generated
-
-  assert(0 && "Unhandled instruction in asm writer!");
-  abort();
-  return;
-}
-
-
 /// runOnMachineFunction - This uses the printMachineInstruction()
 /// method to print assembly for each instruction.
 ///
@@ -167,9 +141,13 @@ bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
   // Print out constants referenced by the function
   EmitConstantPool(MF.getConstantPool());
 
+  // Print out jump tables referenced by the function
+  EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
+
   // Print out labels for the function.
   const Function *F = MF.getFunction();
-  SwitchSection(".text", F);
+  SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
+  
   EmitAlignment(4, F);
   switch (F->getLinkage()) {
   default: assert(0 && "Unknown linkage type!");
@@ -180,7 +158,7 @@ bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
      break;
   case Function::WeakLinkage:
   case Function::LinkOnceLinkage:
-    O << "\t.weak " << CurrentFnName << "\n";
+    O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
     break;
   }
 
@@ -191,16 +169,21 @@ bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
   // Print out code for the function.
   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
        I != E; ++I) {
-    printBasicBlockLabel(I, true);
-    O << '\n';
+    if (I != MF.begin()) {
+      printBasicBlockLabel(I, true);
+      O << '\n';
+    }
     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
          II != E; ++II) {
       // Print the assembly for the instruction.
+      ++EmittedInsts;
       O << "\t";
-      printMachineInstruction(II);
+      if (!printInstruction(II)) {
+        assert(0 && "Unhandled instruction in asm writer!");
+        abort();
+      }
     }
   }
-  ++LabelNumber;
 
   O << "\t.end " << CurrentFnName << "\n";
 
@@ -210,75 +193,103 @@ bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
 
 bool AlphaAsmPrinter::doInitialization(Module &M)
 {
-  AsmPrinter::doInitialization(M);
-  if(TM.getSubtarget<AlphaSubtarget>().hasF2I() 
-     || TM.getSubtarget<AlphaSubtarget>().hasCT())
-    O << "\t.arch ev6\n";
+  if(TM.getSubtarget<AlphaSubtarget>().hasCT())
+    O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
   else
-    O << "\t.arch ev56\n";
+    O << "\t.arch ev6\n";
   O << "\t.set noat\n";
-  return false;
+  return AsmPrinter::doInitialization(M);
 }
 
 bool AlphaAsmPrinter::doFinalization(Module &M) {
   const TargetData *TD = TM.getTargetData();
 
-  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
-    if (I->hasInitializer()) {   // External global require no code
-      // Check to see if this is a special global used by LLVM, if so, emit it.
-      if (EmitSpecialLLVMGlobal(I))
-        continue;
-      
-      O << "\n\n";
-      std::string name = Mang->getValueName(I);
-      Constant *C = I->getInitializer();
-      unsigned Size = TD->getTypeSize(C->getType());
-      //      unsigned Align = TD->getTypeAlignmentShift(C->getType());
-      unsigned Align = getPreferredAlignmentLog(I);
-
-      if (C->isNullValue() &&
-          (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
-           I->hasWeakLinkage() /* FIXME: Verify correct */)) {
-        SwitchSection("\t.section .data", I);
-        if (I->hasInternalLinkage())
-          O << "\t.local " << name << "\n";
-
-        O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
-          << "," << (1 << Align)
-          <<  "\n";
-      } else {
-        switch (I->getLinkage()) {
-        case GlobalValue::LinkOnceLinkage:
-        case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak.
-          // Nonnull linkonce -> weak
-          O << "\t.weak " << name << "\n";
-          O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
-          SwitchSection("", I);
-          break;
-        case GlobalValue::AppendingLinkage:
-          // FIXME: appending linkage variables should go into a section of
-          // their name or something.  For now, just emit them as external.
-        case GlobalValue::ExternalLinkage:
-          // If external or appending, declare as a global symbol
-          O << "\t.globl " << name << "\n";
-          // FALL THROUGH
-        case GlobalValue::InternalLinkage:
-          SwitchSection(C->isNullValue() ? "\t.section .bss" : 
-                        "\t.section .data", I);
-          break;
-        case GlobalValue::GhostLinkage:
-          std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
-          abort();
-        }
-
-        EmitAlignment(Align);
-        O << "\t.type " << name << ",@object\n";
-        O << "\t.size " << name << "," << Size << "\n";
-        O << name << ":\n";
-        EmitGlobalConstant(C);
-      }
+  for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) {
+
+    if (!I->hasInitializer()) continue;  // External global require no code
+    
+    // Check to see if this is a special global used by LLVM, if so, emit it.
+    if (EmitSpecialLLVMGlobal(I))
+      continue;
+    
+    std::string name = Mang->getValueName(I);
+    Constant *C = I->getInitializer();
+    unsigned Size = TD->getABITypeSize(C->getType());
+    unsigned Align = TD->getPreferredAlignmentLog(I);
+    
+    //1: hidden?
+    if (I->hasHiddenVisibility())
+      O << TAI->getHiddenDirective() << name << "\n";
+    
+    //2: kind
+    switch (I->getLinkage()) {
+    case GlobalValue::LinkOnceLinkage:
+    case GlobalValue::WeakLinkage:
+      O << TAI->getWeakRefDirective() << name << '\n';
+      break;
+    case GlobalValue::AppendingLinkage:
+    case GlobalValue::ExternalLinkage:
+      O << "\t.globl " << name << "\n";
+      break;
+    case GlobalValue::InternalLinkage:
+      break;
+    default:
+      assert(0 && "Unknown linkage type!");
+      cerr << "Unknown linkage type!\n";
+      abort();
+    }
+    
+    //3: Section (if changed)
+    if (I->hasSection() &&
+        (I->getSection() == ".ctors" ||
+         I->getSection() == ".dtors")) {
+      std::string SectionName = ".section\t" + I->getSection()
+        + ",\"aw\",@progbits";
+      SwitchToDataSection(SectionName.c_str());
+    } else {
+      if (C->isNullValue())
+        SwitchToDataSection("\t.section\t.bss", I);
+      else
+        SwitchToDataSection("\t.section\t.data", I);
     }
+    
+    //4: Type, Size, Align
+    O << "\t.type\t" << name << ", @object\n";
+    O << "\t.size\t" << name << ", " << Size << "\n";
+    EmitAlignment(Align, I);
+    
+    O << name << ":\n";
+    
+    // If the initializer is a extern weak symbol, remember to emit the weak
+    // reference!
+    if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
+      if (GV->hasExternalWeakLinkage())
+        ExtWeakSymbols.insert(GV);
+    
+    EmitGlobalConstant(C);
+    O << '\n';
+  }
+
+  return AsmPrinter::doFinalization(M);
+}
+
+/// PrintAsmOperand - Print out an operand for an inline asm expression.
+///
+bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
+                                      unsigned AsmVariant, 
+                                      const char *ExtraCode) {
+  printOperand(MI, OpNo);
+  return false;
+}
 
-  AsmPrinter::doFinalization(M);
+bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 
+                                            unsigned OpNo,
+                                            unsigned AsmVariant, 
+                                            const char *ExtraCode) {
+  if (ExtraCode && ExtraCode[0])
+    return true; // Unknown modifier.
+  O << "0(";
+  printOperand(MI, OpNo);
+  O << ")";
   return false;
 }