Simplify operand padding by keying off tabs in the asm stream.  If
padding is disabled, tabs get replaced by spaces except in the case of
the first operand, where the tab is output to line up the operands after
the mnemonics.

Add some better comments and eliminate redundant code.

Fix some testcases to not assume tabs.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77740 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/llvm/CodeGen/AsmPrinter.h b/include/llvm/CodeGen/AsmPrinter.h
index e3d6074..7cdf0a7 100644
--- a/include/llvm/CodeGen/AsmPrinter.h
+++ b/include/llvm/CodeGen/AsmPrinter.h
@@ -226,11 +226,10 @@
                                        unsigned AsmVariant, 
                                        const char *ExtraCode);
     
-    
     /// PrintGlobalVariable - Emit the specified global variable and its
     /// initializer to the output stream.
     virtual void PrintGlobalVariable(const GlobalVariable *GV) = 0;
-    
+
     /// SetupMachineFunction - This should be called when a new MachineFunction
     /// is being processed from runOnMachineFunction.
     void SetupMachineFunction(MachineFunction &MF);
@@ -357,6 +356,13 @@
     void EmitComments(const MCInst &MI) const;
 
   protected:
+    /// PadToColumn - This gets called every time a tab is emitted.  If
+    /// column padding is turned on, we replace the tab with the
+    /// appropriate amount of padding.  If not, we replace the tab with a
+    /// space, except for the first operand so that initial operands are
+    /// always lined up by tabs.
+    void PadToColumn(unsigned Operand) const;
+
     /// EmitZeros - Emit a block of zeros.
     ///
     void EmitZeros(uint64_t NumZeros, unsigned AddrSpace = 0) const;
diff --git a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 077d72e..e19631f 100644
--- a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -847,8 +847,28 @@
   }
   O << '\n';
 }
-
     
+/// PadToColumn - This gets called every time a tab is emitted.  If
+/// column padding is turned on, we replace the tab with the
+/// appropriate amount of padding.  If not, we replace the tab with a
+/// space, except for the first operand so that initial operands are
+/// always lined up by tabs.
+void AsmPrinter::PadToColumn(unsigned Operand) const {
+  if (TAI->getOperandColumn(Operand) > 0) {
+    O.PadToColumn(TAI->getOperandColumn(Operand), 1);
+  }
+  else {
+    if (Operand == 1) {
+      // Emit the tab after the mnemonic.
+      O << '\t';
+    }
+    else {
+      // Replace the tab with a space.
+      O << ' ';
+    }
+  }
+}
+
 /// EmitZeros - Emit a block of zeros.
 ///
 void AsmPrinter::EmitZeros(uint64_t NumZeros, unsigned AddrSpace) const {
diff --git a/test/CodeGen/X86/2009-04-17-tls-fast.ll b/test/CodeGen/X86/2009-04-17-tls-fast.ll
index 6a8054e..6e9485c 100644
--- a/test/CodeGen/X86/2009-04-17-tls-fast.ll
+++ b/test/CodeGen/X86/2009-04-17-tls-fast.ll
@@ -1,5 +1,5 @@
 ; RUN: llvm-as < %s | llc -march=x86-64 -mtriple=x86_64-linux-gnu -relocation-model=pic -regalloc=local > %t
-; RUN: grep {leaq	foo@TLSGD(%rip), %rdi} %t
+; RUN: grep {leaq foo@TLSGD(%rip), %rdi} %t
 
 @foo = internal thread_local global i32 100
 
diff --git a/test/CodeGen/X86/tls1-pic.ll b/test/CodeGen/X86/tls1-pic.ll
index e43bf7c..8f44357 100644
--- a/test/CodeGen/X86/tls1-pic.ll
+++ b/test/CodeGen/X86/tls1-pic.ll
@@ -1,9 +1,9 @@
 ; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu -relocation-model=pic > %t
 ; RUN: grep {leal	i@TLSGD(,%ebx), %eax} %t
-; RUN: grep {call	___tls_get_addr@PLT} %t
+; RUN: grep {call ___tls_get_addr@PLT} %t
 ; RUN: llvm-as < %s | llc -march=x86-64 -mtriple=x86_64-linux-gnu -relocation-model=pic > %t2
-; RUN: grep {leaq	i@TLSGD(%rip), %rdi} %t2
-; RUN: grep {call	__tls_get_addr@PLT} %t2
+; RUN: grep {leaq i@TLSGD(%rip), %rdi} %t2
+; RUN: grep {call __tls_get_addr@PLT} %t2
 
 @i = thread_local global i32 15
 
diff --git a/test/CodeGen/X86/tls2-pic.ll b/test/CodeGen/X86/tls2-pic.ll
index 6ab3ee0..f1492f4 100644
--- a/test/CodeGen/X86/tls2-pic.ll
+++ b/test/CodeGen/X86/tls2-pic.ll
@@ -1,9 +1,9 @@
 ; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu -relocation-model=pic > %t
 ; RUN: grep {leal	i@TLSGD(,%ebx), %eax} %t
-; RUN: grep {call	___tls_get_addr@PLT} %t
+; RUN: grep {call ___tls_get_addr@PLT} %t
 ; RUN: llvm-as < %s | llc -march=x86-64 -mtriple=x86_64-linux-gnu -relocation-model=pic > %t2
-; RUN: grep {leaq	i@TLSGD(%rip), %rdi} %t2
-; RUN: grep {call	__tls_get_addr@PLT} %t2
+; RUN: grep {leaq i@TLSGD(%rip), %rdi} %t2
+; RUN: grep {call __tls_get_addr@PLT} %t2
 
 @i = thread_local global i32 15
 
diff --git a/test/CodeGen/X86/tls3-pic.ll b/test/CodeGen/X86/tls3-pic.ll
index 8e6df29..27c24da 100644
--- a/test/CodeGen/X86/tls3-pic.ll
+++ b/test/CodeGen/X86/tls3-pic.ll
@@ -1,9 +1,9 @@
 ; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu -relocation-model=pic  > %t
 ; RUN: grep {leal	i@TLSGD(,%ebx), %eax} %t
-; RUN: grep {call	___tls_get_addr@PLT} %t
+; RUN: grep {call ___tls_get_addr@PLT} %t
 ; RUN: llvm-as < %s | llc -march=x86-64 -mtriple=x86_64-linux-gnu -relocation-model=pic > %t2
-; RUN: grep {leaq	i@TLSGD(%rip), %rdi} %t2
-; RUN: grep {call	__tls_get_addr@PLT} %t2
+; RUN: grep {leaq i@TLSGD(%rip), %rdi} %t2
+; RUN: grep {call __tls_get_addr@PLT} %t2
 
 @i = external thread_local global i32		; <i32*> [#uses=2]
 
diff --git a/test/CodeGen/X86/tls4-pic.ll b/test/CodeGen/X86/tls4-pic.ll
index 94de78f..6501054 100644
--- a/test/CodeGen/X86/tls4-pic.ll
+++ b/test/CodeGen/X86/tls4-pic.ll
@@ -1,9 +1,9 @@
 ; RUN: llvm-as < %s | llc -march=x86 -mtriple=i386-linux-gnu -relocation-model=pic > %t
 ; RUN: grep {leal	i@TLSGD(,%ebx), %eax} %t
-; RUN: grep {call	___tls_get_addr@PLT} %t
+; RUN: grep {call ___tls_get_addr@PLT} %t
 ; RUN: llvm-as < %s | llc -march=x86-64 -mtriple=x86_64-linux-gnu -relocation-model=pic > %t2
-; RUN: grep {leaq	i@TLSGD(%rip), %rdi} %t2
-; RUN: grep {call	__tls_get_addr@PLT} %t2
+; RUN: grep {leaq i@TLSGD(%rip), %rdi} %t2
+; RUN: grep {call __tls_get_addr@PLT} %t2
 
 @i = external thread_local global i32		; <i32*> [#uses=2]
 
diff --git a/utils/TableGen/AsmWriterEmitter.cpp b/utils/TableGen/AsmWriterEmitter.cpp
index 0695780..5f1d325 100644
--- a/utils/TableGen/AsmWriterEmitter.cpp
+++ b/utils/TableGen/AsmWriterEmitter.cpp
@@ -34,13 +34,19 @@
 namespace llvm {  
   struct AsmWriterOperand {
     enum OpType {
+      // Output this text surrounded by quotes to the asm.
       isLiteralTextOperand, 
+      // This is the name of a routine to call to print the operand.
       isMachineInstrOperand,
+      // Output this text verbatim to the asm writer.  It is code that
+      // will output some text to the asm.
       isLiteralStatementOperand
     } OperandType;
 
     /// Str - For isLiteralTextOperand, this IS the literal text.  For
-    /// isMachineInstrOperand, this is the PrinterMethodName for the operand.
+    /// isMachineInstrOperand, this is the PrinterMethodName for the operand..
+    /// For isLiteralStatementOperand, this is the code to insert verbatim 
+    /// into the asm writer.
     std::string Str;
 
     /// MiOpNo - For isMachineInstrOperand, this is the operand number of the
@@ -85,29 +91,6 @@
     std::vector<AsmWriterOperand> Operands;
     const CodeGenInstruction *CGI;
 
-    /// MAX_GROUP_NESTING_LEVEL - The maximum number of group nesting
-    /// levels we ever expect to see in an asm operand.
-    static const int MAX_GROUP_NESTING_LEVEL = 10;
-
-    /// GroupLevel - The level of nesting of the current operand
-    /// group, such as [reg + (reg + offset)].  -1 means we are not in
-    /// a group.
-    int GroupLevel;
-
-    /// GroupDelim - Remember the delimeter for a group operand.
-    char GroupDelim[MAX_GROUP_NESTING_LEVEL];
-
-    /// ReadingWhitespace - Tell whether we just read some whitespace.
-    bool ReadingWhitespace;
-
-    /// InGroup - Determine whether we are in the middle of an
-    /// operand group.
-    bool InGroup() const { return GroupLevel != -1; }
-
-    /// InWhitespace - Determine whether we are in the middle of
-    /// emitting whitespace.
-    bool InWhitespace() const { return ReadingWhitespace; }
-
     AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant);
 
     /// MatchesAllButOneOp - If this instruction is exactly identical to the
@@ -137,14 +120,6 @@
     return Str;
   }
 
-  if (OperandType == isLiteralStatementOperand) {
-    return Str;
-  }
-
-  if (OperandType == isLiteralStatementOperand) {
-    return Str;
-  }
-
   std::string Result = Str + "(MI";
   if (MIOpNo != ~0U)
     Result += ", " + utostr(MIOpNo);
@@ -157,8 +132,7 @@
 /// ParseAsmString - Parse the specified Instruction's AsmString into this
 /// AsmWriterInst.
 ///
-AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant)
-    : GroupLevel(-1), ReadingWhitespace(false) {
+AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
   this->CGI = &CGI;
   unsigned CurVariant = ~0U;  // ~0 if we are outside a {.|.|.} region, other #.
 
@@ -181,84 +155,22 @@
         for (; LastEmitted != DollarPos; ++LastEmitted)
           switch (AsmString[LastEmitted]) {
           case '\n':
-            assert(!InGroup() && "Missing matching group delimeter");
-            ReadingWhitespace = false;
             AddLiteralString("\\n");
             break;
           case '\t': 
-            if (!InGroup()) {
-              ReadingWhitespace = true;
-            }
-            AddLiteralString("\\t"); 
+            Operands.push_back(
+              // We recognize a tab as an operand delimeter.  Either
+              // output column padding if enabled or emit a space.
+              AsmWriterOperand("PadToColumn(OperandColumn++);\n",
+                               AsmWriterOperand::isLiteralStatementOperand));
             break;
           case '"':
-            if (InWhitespace() && !InGroup())
-              Operands.push_back(
-                AsmWriterOperand(
-                  "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
-                  AsmWriterOperand::isLiteralStatementOperand));
-            ReadingWhitespace = false;
             AddLiteralString("\\\"");
             break;
           case '\\':
-            if (InWhitespace() && !InGroup())
-              Operands.push_back(
-                AsmWriterOperand(
-                  "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
-                  AsmWriterOperand::isLiteralStatementOperand));
-            ReadingWhitespace = false;
             AddLiteralString("\\\\");
             break;
-
-          case '(':  // Fallthrough
-          case '[':
-            if (InWhitespace() && !InGroup())
-              Operands.push_back(
-                AsmWriterOperand(
-                  "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
-                  AsmWriterOperand::isLiteralStatementOperand));
-            ReadingWhitespace = false;
-
-            ++GroupLevel;
-            assert(GroupLevel < MAX_GROUP_NESTING_LEVEL
-                   && "Exceeded maximum operand group nesting level");
-            GroupDelim[GroupLevel] = AsmString[LastEmitted];
-            AddLiteralString(std::string(1, AsmString[LastEmitted]));
-            break;
-
-          case ')':  // Fallthrough
-          case ']':
-            if (InWhitespace() && !InGroup())
-              Operands.push_back(
-                AsmWriterOperand(
-                  "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
-                  AsmWriterOperand::isLiteralStatementOperand));
-            ReadingWhitespace = false;
-
-            if (AsmString[LastEmitted] == ')')
-              assert(GroupDelim[GroupLevel] == '(' && "Mismatched delimeters");
-            else
-              assert(GroupDelim[GroupLevel] == '[' && "Mismatched delimeters");
-            
-            --GroupLevel;
-            assert(GroupLevel > -2 && "Too many end delimeters!");
-            AddLiteralString(std::string(1, AsmString[LastEmitted]));
-            break;
-            
           default:
-            if (AsmString[LastEmitted] != ' ' &&
-                AsmString[LastEmitted] != '\t') {
-              if (!InGroup() && InWhitespace())
-                Operands.push_back(
-                  AsmWriterOperand(
-                    "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
-                    AsmWriterOperand::isLiteralStatementOperand));
-              ReadingWhitespace = false;
-            }
-            else 
-              if (!InGroup())
-                ReadingWhitespace = true;
-
             AddLiteralString(std::string(1, AsmString[LastEmitted]));
             break;
           }
@@ -269,33 +181,15 @@
       if (DollarPos+1 != AsmString.size() &&
           (CurVariant == Variant || CurVariant == ~0U)) {
         if (AsmString[DollarPos+1] == 'n') {
-          assert(!InGroup() && "Missing matching group delimeter");
-          ReadingWhitespace = false;
           AddLiteralString("\\n");
         } else if (AsmString[DollarPos+1] == 't') {
-          if (!InGroup()) {
-            ReadingWhitespace = true;
-          }
-          AddLiteralString("\\t");
+          Operands.push_back(
+            // We recognize a tab as an operand delimeter.  Either
+            // output column padding if enabled or emit a space.
+            AsmWriterOperand("PadToColumn(OperandColumn++);\n",
+                             AsmWriterOperand::isLiteralStatementOperand));
         } else if (std::string("${|}\\").find(AsmString[DollarPos+1]) 
                    != std::string::npos) {
-          if (InWhitespace() && !InGroup())
-            Operands.push_back(
-              AsmWriterOperand(
-                "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
-                AsmWriterOperand::isLiteralStatementOperand));
-          ReadingWhitespace = false;
-
-          if (AsmString[DollarPos+1] == '{') {
-            ++GroupLevel;
-            assert(GroupLevel < MAX_GROUP_NESTING_LEVEL
-                   && "Exceeded maximum operand group nesting level");
-            GroupDelim[GroupLevel] = AsmString[DollarPos+1];
-          } else if (AsmString[DollarPos+1] == '}') {
-            assert(GroupDelim[GroupLevel] == '{' && "Mismatched delimeters");
-            --GroupLevel;
-            assert(GroupLevel > -2 && "Too many end delimeters!");
-          }
           AddLiteralString(std::string(1, AsmString[DollarPos+1]));
         } else {
           throw "Non-supported escaped character found in instruction '" +
@@ -325,23 +219,10 @@
     } else if (DollarPos+1 != AsmString.size() &&
                AsmString[DollarPos+1] == '$') {
       if (CurVariant == Variant || CurVariant == ~0U) {
-        if (InWhitespace() && !InGroup())
-          Operands.push_back(
-            AsmWriterOperand(
-              "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
-              AsmWriterOperand::isLiteralStatementOperand));
-        ReadingWhitespace = false;
         AddLiteralString("$");  // "$$" -> $
       }
       LastEmitted = DollarPos+2;
     } else {
-      if (InWhitespace() && !InGroup())
-        Operands.push_back(
-          AsmWriterOperand(
-            "O.PadToColumn(TAI->getOperandColumn(OperandColumn++));\n",
-            AsmWriterOperand::isLiteralStatementOperand));
-      ReadingWhitespace = false;
-
       // Get the name of the variable.
       std::string::size_type VarEnd = DollarPos+1;
  
@@ -852,6 +733,8 @@
     << "  if (TAI->getOperandColumn(1) > 0) {\n"
     << "    // Don't emit trailing whitespace, let the column padding do it.  This\n"
     << "    // guarantees that a stray long opcode + tab won't upset the alignment.\n"
+    << "    // We need to handle this special case here because sometimes the initial\n"
+    << "    // mnemonic string includes a tab or space and sometimes it doesn't.\n"
     << "    unsigned OpLength = std::strlen(AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "));\n"
     << "    if (OpLength > 0 &&\n"
     << "        ((AsmStrs+(Bits & " << (1 << AsmStrBits)-1 << "))[OpLength-1] == ' ' ||\n"