opening "-" automatically yields stdout.
[oota-llvm.git] / lib / CodeGen / AsmPrinter.cpp
index 69bf5c69a7080ca3ef9f9739c5770b24fcf8e405..16f26c429921319f512628b78417e2454615347b 100644 (file)
@@ -21,7 +21,6 @@
 #include "llvm/CodeGen/MachineConstantPool.h"
 #include "llvm/CodeGen/MachineJumpTableInfo.h"
 #include "llvm/CodeGen/MachineModuleInfo.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Mangler.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/Streams.h"
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetLowering.h"
 #include "llvm/Target/TargetMachine.h"
+#include "llvm/Target/TargetOptions.h"
 #include "llvm/Target/TargetRegisterInfo.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include <cerrno>
 using namespace llvm;
 
-static cl::opt<bool>
-AsmVerbose("asm-verbose", cl::Hidden, cl::desc("Add comments to directives."));
-
 char AsmPrinter::ID = 0;
 AsmPrinter::AsmPrinter(std::ostream &o, TargetMachine &tm,
                        const TargetAsmInfo *T)
@@ -296,13 +293,16 @@ void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
   // the appropriate section.
   TargetLowering *LoweringInfo = TM.getTargetLowering();
 
-  const char* JumpTableDataSection = TAI->getJumpTableDataSection();  
+  const char* JumpTableDataSection = TAI->getJumpTableDataSection();
+  const Function *F = MF.getFunction();
+  unsigned SectionFlags = TAI->SectionFlagsForGlobal(F);
   if ((IsPic && !(LoweringInfo && LoweringInfo->usesGlobalOffsetTable())) ||
-     !JumpTableDataSection) {
+     !JumpTableDataSection ||
+      SectionFlags & SectionFlags::Linkonce) {
     // In PIC mode, we need to emit the jump table to the same section as the
     // function body itself, otherwise the label differences won't make sense.
-    // We should also do if the section name is NULL.
-    const Function *F = MF.getFunction();
+    // We should also do if the section name is NULL or function is declared in
+    // discardable section.
     SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
   } else {
     SwitchToDataSection(JumpTableDataSection);
@@ -493,23 +493,12 @@ void AsmPrinter::PrintULEB128(unsigned Value) const {
   } while (Value);
 }
 
-/// SizeULEB128 - Compute the number of bytes required for an unsigned leb128
-/// value.
-unsigned AsmPrinter::SizeULEB128(unsigned Value) {
-  unsigned Size = 0;
-  do {
-    Value >>= 7;
-    Size += sizeof(int8_t);
-  } while (Value);
-  return Size;
-}
-
 /// PrintSLEB128 - Print a series of hexidecimal values (separated by commas)
 /// representing a signed leb128 value.
 void AsmPrinter::PrintSLEB128(int Value) const {
   int Sign = Value >> (8 * sizeof(Value) - 1);
   bool IsMore;
-  
+
   do {
     unsigned Byte = Value & 0x7f;
     Value >>= 7;
@@ -520,22 +509,6 @@ void AsmPrinter::PrintSLEB128(int Value) const {
   } while (IsMore);
 }
 
-/// SizeSLEB128 - Compute the number of bytes required for a signed leb128
-/// value.
-unsigned AsmPrinter::SizeSLEB128(int Value) {
-  unsigned Size = 0;
-  int Sign = Value >> (8 * sizeof(Value) - 1);
-  bool IsMore;
-  
-  do {
-    unsigned Byte = Value & 0x7f;
-    Value >>= 7;
-    IsMore = Value != Sign || ((Byte ^ Sign) & 0x40) != 0;
-    Size += sizeof(int8_t);
-  } while (IsMore);
-  return Size;
-}
-
 //===--------------------------------------------------------------------===//
 // Emission and print routines
 //
@@ -551,8 +524,19 @@ void AsmPrinter::PrintHex(int Value) const {
 void AsmPrinter::EOL() const {
   O << '\n';
 }
+
 void AsmPrinter::EOL(const std::string &Comment) const {
-  if (AsmVerbose && !Comment.empty()) {
+  if (VerboseAsm && !Comment.empty()) {
+    O << '\t'
+      << TAI->getCommentString()
+      << ' '
+      << Comment;
+  }
+  O << '\n';
+}
+
+void AsmPrinter::EOL(const char* Comment) const {
+  if (VerboseAsm && *Comment) {
     O << '\t'
       << TAI->getCommentString()
       << ' '
@@ -810,12 +794,13 @@ void AsmPrinter::EmitConstantValueOnly(const Constant *CV) {
 
       // We can emit the pointer value into this slot if the slot is an
       // integer slot greater or equal to the size of the pointer.
-      if (Ty->isInteger() &&
-          TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
+      if (TD->getABITypeSize(Ty) >= TD->getABITypeSize(Op->getType()))
         return EmitConstantValueOnly(Op);
-      
-      assert(0 && "FIXME: Don't yet support this kind of constant cast expr");
+
+      O << "((";
       EmitConstantValueOnly(Op);
+      APInt ptrMask = APInt::getAllOnesValue(TD->getABITypeSizeInBits(Ty));
+      O << ") & " << ptrMask.toStringUnsigned() << ')';
       break;
     }
     case Instruction::Add:
@@ -1318,8 +1303,7 @@ void AsmPrinter::printImplicitDef(const MachineInstr *MI) const {
 /// printLabel - This method prints a local label used by debug and
 /// exception handling tables.
 void AsmPrinter::printLabel(const MachineInstr *MI) const {
-  O << TAI->getPrivateGlobalPrefix()
-    << "label" << MI->getOperand(0).getImm() << ":\n";
+  printLabel(MI->getOperand(0).getImm());
 }
 
 void AsmPrinter::printLabel(unsigned Id) const {
@@ -1370,7 +1354,7 @@ void AsmPrinter::printBasicBlockLabel(const MachineBasicBlock *MBB,
     O << ':';
   if (printComment && MBB->getBasicBlock())
     O << '\t' << TAI->getCommentString() << ' '
-      << MBB->getBasicBlock()->getName();
+      << MBB->getBasicBlock()->getNameStart();
 }
 
 /// printPICJumpTableSetLabel - This method prints a set label for the
@@ -1438,10 +1422,34 @@ void AsmPrinter::printDataDirective(const Type *type) {
   }
 }
 
-void AsmPrinter::printSuffixedName(std::string &Name, const char* Suffix) {
+void AsmPrinter::printSuffixedName(const char *Name, const char *Suffix,
+                                   const char *Prefix) {
+  if (Name[0]=='\"')
+    O << '\"';
+  O << TAI->getPrivateGlobalPrefix();
+  if (Prefix) O << Prefix;
+  if (Name[0]=='\"')
+    O << '\"';
   if (Name[0]=='\"')
-    O << '\"' << TAI->getPrivateGlobalPrefix() << 
-         Name.substr(1, Name.length()-2) << Suffix << '\"';
+    O << Name[1];
   else
-    O << TAI->getPrivateGlobalPrefix() << Name << Suffix;
+    O << Name;
+  O << Suffix;
+  if (Name[0]=='\"')
+    O << '\"';
+}
+
+void AsmPrinter::printSuffixedName(const std::string &Name, const char* Suffix) {
+  printSuffixedName(Name.c_str(), Suffix);
+}
+
+void AsmPrinter::printVisibility(const std::string& Name,
+                                 unsigned Visibility) const {
+  if (Visibility == GlobalValue::HiddenVisibility) {
+    if (const char *Directive = TAI->getHiddenDirective())
+      O << Directive << Name << '\n';
+  } else if (Visibility == GlobalValue::ProtectedVisibility) {
+    if (const char *Directive = TAI->getProtectedDirective())
+      O << Directive << Name << '\n';
+  }
 }