Handle \n's in value names for more targets. The asm printers
[oota-llvm.git] / lib / Target / X86 / X86AsmPrinter.cpp
index c4d5958bfcaef76c0d2649383ed8f593340f3de5..9720100e3707fb7310307e078f92ef270af59c18 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.
 //
 //===----------------------------------------------------------------------===//
 //
@@ -25,6 +25,7 @@
 #include "llvm/Constants.h"
 #include "llvm/Module.h"
 #include "llvm/DerivedTypes.h"
+#include "llvm/ParameterAttributes.h"
 #include "llvm/Type.h"
 #include "llvm/Assembly/Writer.h"
 #include "llvm/Support/Mangler.h"
@@ -48,11 +49,19 @@ static X86MachineFunctionInfo calculateFunctionInfo(const Function *F,
     return Info;
   }
 
+  unsigned argNum = 1;
   for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
-       AI != AE; ++AI)
+       AI != AE; ++AI, ++argNum) {
+    const Type* Ty = AI->getType();
+
+    // 'Dereference' type in case of byval parameter attribute
+    if (F->paramHasAttr(argNum, ParamAttr::ByVal))
+      Ty = cast<PointerType>(Ty)->getElementType();
+
     // Size should be aligned to DWORD boundary
-    Size += ((TD->getTypeSize(AI->getType()) + 3)/4)*4;
-  
+    Size += ((TD->getABITypeSize(Ty) + 3)/4)*4;
+  }
+
   // We're not supporting tooooo huge arguments :)
   Info.setBytesToPopOnReturn((unsigned int)Size);
   return Info;
@@ -93,13 +102,13 @@ void X86SharedAsmPrinter::decorateName(std::string &Name,
   case StdCall:
     // "Pure" variadic functions do not receive @0 suffix.
     if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
-        (FT->getNumParams() == 1 && FT->isStructReturn())) 
+        (FT->getNumParams() == 1 && F->isStructReturn()))
       Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
     break;
   case FastCall:
     // "Pure" variadic functions do not receive @0 suffix.
     if (!FT->isVarArg() || (FT->getNumParams() == 0) ||
-        (FT->getNumParams() == 1 && FT->isStructReturn())) 
+        (FT->getNumParams() == 1 && F->isStructReturn()))
       Name += '@' + utostr_32(Info->getBytesToPopOnReturn());
 
     if (Name[0] == '_') {
@@ -129,6 +138,15 @@ bool X86SharedAsmPrinter::doInitialization(Module &M) {
   return Result;
 }
 
+/// PrintUnmangledNameSafely - Print out the printable characters in the name.
+/// Don't print things like \n or \0.
+static void PrintUnmangledNameSafely(const Value *V, std::ostream &OS) {
+  for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
+       Name != E; ++Name)
+    if (isprint(*Name))
+      OS << *Name;
+}
+
 bool X86SharedAsmPrinter::doFinalization(Module &M) {
   // Note: this code is not shared by the Intel printer as it is too different
   // from how MASM does things.  When making changes here don't forget to look
@@ -156,7 +174,7 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
     std::string name = Mang->getValueName(I);
     Constant *C = I->getInitializer();
     const Type *Type = C->getType();
-    unsigned Size = TD->getTypeSize(Type);
+    unsigned Size = TD->getABITypeSize(Type);
     unsigned Align = TD->getPreferredAlignmentLog(I);
 
     if (I->hasHiddenVisibility()) {
@@ -173,8 +191,8 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
     if (C->isNullValue() && !I->hasSection()) {
       if (I->hasExternalLinkage()) {
         if (const char *Directive = TAI->getZeroFillDirective()) {
-          O << "\t.globl\t" << name << "\n";
-          O << Directive << "__DATA__, __common, " << name << ", "
+          O << "\t.globl " << name << "\n";
+          O << Directive << "__DATA, __common, " << name << ", "
             << Size << ", " << Align << "\n";
           continue;
         }
@@ -193,8 +211,13 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
             O << TAI->getLCOMMDirective() << name << "," << Size;
             if (Subtarget->isTargetDarwin())
               O << "," << Align;
-          } else
+          } else {
             O << TAI->getCOMMDirective()  << name << "," << Size;
+            
+            // Leopard and above support aligned common symbols.
+            if (Subtarget->getDarwinVers() >= 9)
+              O << "," << Align;
+          }
         } else {
           if (!Subtarget->isTargetCygMing()) {
             if (I->hasInternalLinkage())
@@ -204,7 +227,9 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
           if (TAI->getCOMMDirectiveTakesAlignment())
             O << "," << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
         }
-        O << "\t\t" << TAI->getCommentString() << " " << I->getName() << "\n";
+        O << "\t\t" << TAI->getCommentString() << " ";
+        PrintUnmangledNameSafely(I, O);
+        O << "\n";
         continue;
       }
     }
@@ -214,14 +239,14 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
     case GlobalValue::WeakLinkage:
       if (Subtarget->isTargetDarwin()) {
         O << "\t.globl " << name << "\n"
-          << "\t.weak_definition " << name << "\n";
-        SwitchToDataSection(".section __DATA,__const_coal,coalesced", I);
+          << TAI->getWeakDefDirective() << name << "\n";
+        SwitchToDataSection("\t.section __DATA,__datacoal_nt,coalesced", I);
       } else if (Subtarget->isTargetCygMing()) {
         std::string SectionName(".section\t.data$linkonce." +
                                 name +
                                 ",\"aw\"");
         SwitchToDataSection(SectionName.c_str(), I);
-        O << "\t.globl " << name << "\n"
+        O << "\t.globl\t" << name << "\n"
           << "\t.linkonce same_size\n";
       } else {
         std::string SectionName("\t.section\t.llvm.linkonce.d." +
@@ -261,7 +286,10 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
           assert(!Subtarget->isTargetDarwin());
           SectionName += ",\"aw\",@progbits";
         }
-
+        SwitchToDataSection(SectionName.c_str());
+      } else if (I->hasSection() && Subtarget->isTargetDarwin()) {
+        // Honor all section names on Darwin; ObjC uses this
+        std::string SectionName = ".section " + I->getSection();
         SwitchToDataSection(SectionName.c_str());
       } else {
         if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection())
@@ -302,8 +330,9 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
     }
 
     EmitAlignment(Align, I);
-    O << name << ":\t\t\t\t" << TAI->getCommentString() << " " << I->getName()
-      << "\n";
+    O << name << ":\t\t\t\t" << TAI->getCommentString() << " ";
+    PrintUnmangledNameSafely(I, O);
+    O << "\n";
     if (TAI->hasDotTypeDotSizeDirective())
       O << "\t.size\t" << name << ", " << Size << "\n";
     // If the initializer is a extern weak symbol, remember to emit the weak
@@ -316,7 +345,7 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
   }
   
   // Output linker support code for dllexported globals
-  if (DLLExportedGVs.begin() != DLLExportedGVs.end()) {
+  if (!DLLExportedGVs.empty()) {
     SwitchToDataSection(".section .drectve");
   }
 
@@ -326,7 +355,7 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
     O << "\t.ascii \" -export:" << *i << ",data\"\n";
   }    
 
-  if (DLLExportedFns.begin() != DLLExportedFns.end()) {
+  if (!DLLExportedFns.empty()) {
     SwitchToDataSection(".section .drectve");
   }
 
@@ -343,7 +372,7 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
     unsigned j = 1;
     for (std::set<std::string>::iterator i = FnStubs.begin(), e = FnStubs.end();
          i != e; ++i, ++j) {
-      SwitchToDataSection(".section __IMPORT,__jump_table,symbol_stubs,"
+      SwitchToDataSection("\t.section __IMPORT,__jump_table,symbol_stubs,"
                           "self_modifying_code+pure_instructions,5", 0);
       O << "L" << *i << "$stub:\n";
       O << "\t.indirect_symbol " << *i << "\n";
@@ -352,7 +381,8 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
 
     O << "\n";
 
-    if (ExceptionHandling && TAI->doesSupportExceptionHandling() && MMI) {
+    if (ExceptionHandling && TAI->doesSupportExceptionHandling() && MMI &&
+        !Subtarget->is64Bit()) {
       // Add the (possibly multiple) personalities to the set of global values.
       const std::vector<Function *>& Personalities = MMI->getPersonalities();
 
@@ -362,9 +392,9 @@ bool X86SharedAsmPrinter::doFinalization(Module &M) {
     }
 
     // Output stubs for external and common global variables.
-    if (GVStubs.begin() != GVStubs.end())
+    if (!GVStubs.empty())
       SwitchToDataSection(
-                    ".section __IMPORT,__pointers,non_lazy_symbol_pointers");
+                    "\t.section __IMPORT,__pointers,non_lazy_symbol_pointers");
     for (std::set<std::string>::iterator i = GVStubs.begin(), e = GVStubs.end();
          i != e; ++i) {
       O << "L" << *i << "$non_lazy_ptr:\n";