mcize the non-gprel cases of AsmPrinter::printPICJumpTableEntry
[oota-llvm.git] / lib / CodeGen / AsmPrinter / AsmPrinter.cpp
index 7238a52f0392ed63dbb989f0580e1ee111b03fbe..d75210d07232306ef554e3df59ce8be062be2144 100644 (file)
@@ -115,11 +115,11 @@ bool AsmPrinter::doInitialization(Module &M) {
   // Allow the target to emit any magic that it wants at the start of the file.
   EmitStartOfAsmFile(M);
 
+  // Very minimal debug info. It is ignored if we emit actual debug info. If we
+  // don't, this at least helps the user find where a global came from.
   if (MAI->hasSingleParameterDotFile()) {
-    // Very minimal debug info. It is ignored if we emit actual
-    // debug info. If we don't, this at least helps the user find where
-    // a function came from.
-    O << "\t.file\t\"" << M.getModuleIdentifier() << "\"\n";
+    // .file "foo.c"
+    OutStreamer.EmitFileDirective(M.getModuleIdentifier());
   }
 
   GCModuleInfo *MI = getAnalysisIfAvailable<GCModuleInfo>();
@@ -156,13 +156,8 @@ void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
   MCSymbol *GVSym = GetGlobalValueSymbol(GV);
   printVisibility(GVSym, GV->getVisibility());
 
-  if (MAI->hasDotTypeDotSizeDirective()) {
-    O << "\t.type\t" << *GVSym;
-    if (MAI->getCommentString()[0] != '@')
-      O << ",@object\n";
-    else
-      O << ",%object\n";
-  }
+  if (MAI->hasDotTypeDotSizeDirective())
+    OutStreamer.EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
   
   SectionKind GVKind = TargetLoweringObjectFile::getKindForGlobal(GV, TM);
 
@@ -196,9 +191,9 @@ void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
       return;
     }
     
-    if (const char *LComm = MAI->getLCOMMDirective()) {
+    if (MAI->hasLCOMMDirective()) {
       // .lcomm _foo, 42
-      O << LComm << *GVSym << ',' << Size << '\n';
+      OutStreamer.EmitLocalCommonSymbol(GVSym, Size);
       return;
     }
     
@@ -241,6 +236,9 @@ void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
     } else if (const char *LinkOnce = MAI->getLinkOnceDirective()) {
       // .globl _foo
       OutStreamer.EmitSymbolAttribute(GVSym, MCSA_Global);
+      // FIXME: linkonce should be a section attribute, handled by COFF Section
+      // assignment.
+      // http://sourceware.org/binutils/docs-2.20/as/Linkonce.html#Linkonce
       // .linkonce same_size
       O << LinkOnce;
     } else {
@@ -275,7 +273,8 @@ void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
   EmitGlobalConstant(GV->getInitializer());
 
   if (MAI->hasDotTypeDotSizeDirective())
-    O << "\t.size\t" << *GVSym << ", " << Size << '\n';
+    // .size foo, 42
+    OutStreamer.EmitELFSize(GVSym, MCConstantExpr::Create(Size, OutContext));
   
   OutStreamer.AddBlankLine();
 }
@@ -345,11 +344,8 @@ bool AsmPrinter::doFinalization(Module &M) {
   // to be executable. Some targets have a directive to declare this.
   Function *InitTrampolineIntrinsic = M.getFunction("llvm.init.trampoline");
   if (!InitTrampolineIntrinsic || InitTrampolineIntrinsic->use_empty())
-    // FIXME: This is actually a section switch on linux/x86 and systemz, use
-    // switch section.
-    if (MAI->getNonexecutableStackDirective())
-      O << MAI->getNonexecutableStackDirective() << '\n';
-
+    if (MCSection *S = MAI->getNonexecutableStackSection(OutContext))
+      OutStreamer.SwitchSection(S);
   
   // Allow the target to emit any magic that it wants at the end of the file,
   // after everything else has gone out.
@@ -528,9 +524,18 @@ void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
 
     OutStreamer.EmitLabel(GetJTISymbol(i));
 
-    for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
-      printPICJumpTableEntry(MJTI, JTBBs[ii], i);
-      O << '\n';
+    if (!IsPic) {
+      // In non-pic mode, the entries in the jump table are direct references
+      // to the basic blocks.
+      unsigned EntrySize = MJTI->getEntrySize();
+      for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii) {
+        MCSymbol *MBBSym = GetMBBSymbol(JTBBs[ii]->getNumber());
+        OutStreamer.EmitValue(MCSymbolRefExpr::Create(MBBSym, OutContext),
+                              EntrySize, /*addrspace*/0);
+      }      
+    } else {
+      for (unsigned ii = 0, ee = JTBBs.size(); ii != ee; ++ii)
+        printPICJumpTableEntry(MJTI, JTBBs[ii], i);
     }
   }
 }
@@ -538,36 +543,28 @@ void AsmPrinter::EmitJumpTableInfo(MachineJumpTableInfo *MJTI,
 void AsmPrinter::printPICJumpTableEntry(const MachineJumpTableInfo *MJTI,
                                         const MachineBasicBlock *MBB,
                                         unsigned uid)  const {
-  bool isPIC = TM.getRelocationModel() == Reloc::PIC_;
-  
-  // Use JumpTableDirective otherwise honor the entry size from the jump table
-  // info.
-  const char *JTEntryDirective = MAI->getJumpTableDirective(isPIC);
-  bool HadJTEntryDirective = JTEntryDirective != NULL;
-  if (!HadJTEntryDirective) {
-    JTEntryDirective = MJTI->getEntrySize() == 4 ?
-      MAI->getData32bitsDirective() : MAI->getData64bitsDirective();
+  // If the target supports GPRel, use it.
+  if (const char *GPRel32Dir = MAI->getGPRel32Directive()) {
+    O << GPRel32Dir << *GetMBBSymbol(MBB->getNumber()) << '\n';
+    return;
   }
-
-  O << JTEntryDirective << ' ';
-
+  
   // If we have emitted set directives for the jump table entries, print 
   // them rather than the entries themselves.  If we're emitting PIC, then
   // emit the table entries as differences between two text section labels.
-  // If we're emitting non-PIC code, then emit the entries as direct
-  // references to the target basic blocks.
-  if (!isPIC) {
-    O << *GetMBBSymbol(MBB->getNumber());
-  } else if (MAI->getSetDirective()) {
-    O << MAI->getPrivateGlobalPrefix() << getFunctionNumber()
-      << '_' << uid << "_set_" << MBB->getNumber();
+  const MCExpr *Val;
+  if (MAI->getSetDirective()) {
+    // If we used .set, reference the .set's symbol.
+    Val = MCSymbolRefExpr::Create(GetJTSetSymbol(uid, MBB->getNumber()),
+                                  OutContext);
   } else {
-    O << *GetMBBSymbol(MBB->getNumber());
-    // If the arch uses custom Jump Table directives, don't calc relative to
-    // JT.
-    if (!HadJTEntryDirective) 
-      O << '-' << *GetJTISymbol(uid);
+    // Otherwise, use the difference as the jump table entry.
+    Val = MCSymbolRefExpr::Create(GetMBBSymbol(MBB->getNumber()), OutContext);
+    const MCExpr *JTI = MCSymbolRefExpr::Create(GetJTISymbol(uid), OutContext);
+    Val = MCBinaryExpr::CreateSub(Val, JTI, OutContext);
   }
+  
+  OutStreamer.EmitValue(Val, MJTI->getEntrySize(), /*addrspace*/0);
 }
 
 
@@ -686,48 +683,6 @@ void AsmPrinter::EmitInt64(uint64_t Value) const {
   OutStreamer.EmitIntValue(Value, 8, 0/*addrspace*/);
 }
 
-
-/// toOctal - Convert the low order bits of X into an octal digit.
-///
-static inline char toOctal(int X) {
-  return (X&7)+'0';
-}
-
-/// printStringChar - Print a char, escaped if necessary.
-///
-static void printStringChar(formatted_raw_ostream &O, unsigned char C) {
-  if (C == '"') {
-    O << "\\\"";
-  } else if (C == '\\') {
-    O << "\\\\";
-  } else if (isprint((unsigned char)C)) {
-    O << C;
-  } else {
-    switch(C) {
-    case '\b': O << "\\b"; break;
-    case '\f': O << "\\f"; break;
-    case '\n': O << "\\n"; break;
-    case '\r': O << "\\r"; break;
-    case '\t': O << "\\t"; break;
-    default:
-      O << '\\';
-      O << toOctal(C >> 6);
-      O << toOctal(C >> 3);
-      O << toOctal(C >> 0);
-      break;
-    }
-  }
-}
-
-/// EmitFile - Emit a .file directive.
-void AsmPrinter::EmitFile(unsigned Number, StringRef Name) const {
-  O << "\t.file\t" << Number << " \"";
-  for (unsigned i = 0, N = Name.size(); i < N; ++i)
-    printStringChar(O, Name[i]);
-  O << '\"';
-}
-
-
 //===----------------------------------------------------------------------===//
 
 // EmitAlignment - Emit an alignment directive to the specified power of
@@ -1423,6 +1378,15 @@ MCSymbol *AsmPrinter::GetJTISymbol(unsigned JTID, bool isLinkerPrivate) const {
   return OutContext.GetOrCreateSymbol(Name.str());
 }
 
+/// GetJTSetSymbol - Return the symbol for the specified jump table .set
+/// FIXME: privatize to AsmPrinter.
+MCSymbol *AsmPrinter::GetJTSetSymbol(unsigned UID, unsigned MBBID) const {
+  SmallString<60> Name;
+  raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix()
+    << getFunctionNumber() << '_' << UID << "_set_" << MBBID;
+  return OutContext.GetOrCreateSymbol(Name.str());
+}
+
 /// GetGlobalValueSymbol - Return the MCSymbol for the specified global
 /// value.
 MCSymbol *AsmPrinter::GetGlobalValueSymbol(const GlobalValue *GV) const {
@@ -1570,22 +1534,8 @@ void AsmPrinter::printPICJumpTableSetLabel(unsigned uid,
     return;
   
   O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
-    << getFunctionNumber() << '_' << uid << "_set_" << MBB->getNumber() << ','
-    << *GetMBBSymbol(MBB->getNumber())
-    << '-' << *GetJTISymbol(uid) << '\n';
-}
-
-void AsmPrinter::printPICJumpTableSetLabel(unsigned uid, unsigned uid2,
-                                           const MachineBasicBlock *MBB) const {
-  if (!MAI->getSetDirective())
-    return;
-  
-  O << MAI->getSetDirective() << ' ' << MAI->getPrivateGlobalPrefix()
-    << getFunctionNumber() << '_' << uid << '_' << uid2
-    << "_set_" << MBB->getNumber() << ','
-    << *GetMBBSymbol(MBB->getNumber())
-    << '-' << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() 
-    << '_' << uid << '_' << uid2 << '\n';
+    << *GetJTSetSymbol(uid, MBB->getNumber()) << ','
+    << *GetMBBSymbol(MBB->getNumber()) << '-' << *GetJTISymbol(uid) << '\n';
 }
 
 void AsmPrinter::printVisibility(MCSymbol *Sym, unsigned Visibility) const {