Revert 76177 for now. It's messing up ARM asm printing. Also this significant debate...
[oota-llvm.git] / utils / TableGen / AsmWriterEmitter.cpp
index 4ba22758a600e17658e2a60ab9f5677e945d8fc8..d1cb7022d06d070acec843bca13334ef697013f1 100644 (file)
@@ -19,6 +19,7 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/MathExtras.h"
 #include <algorithm>
+#include <iostream>
 using namespace llvm;
 
 static bool isIdentChar(char C) {
@@ -45,7 +46,10 @@ namespace llvm {
     /// an operand, specified with syntax like ${opname:modifier}.
     std::string MiModifier;
 
-    AsmWriterOperand(const std::string &LitStr)
+    // To make VS STL happy
+    AsmWriterOperand():OperandType(isLiteralTextOperand) {}
+
+    explicit AsmWriterOperand(const std::string &LitStr)
       : OperandType(isLiteralTextOperand), Str(LitStr) {}
 
     AsmWriterOperand(const std::string &Printer, unsigned OpNo, 
@@ -127,11 +131,20 @@ AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
 
     // Emit a constant string fragment.
     if (DollarPos != LastEmitted) {
-      // TODO: this should eventually handle escaping.
-      if (CurVariant == Variant || CurVariant == ~0U)
-        AddLiteralString(std::string(AsmString.begin()+LastEmitted,
-                                     AsmString.begin()+DollarPos));
-      LastEmitted = DollarPos;
+      if (CurVariant == Variant || CurVariant == ~0U) {
+        for (; LastEmitted != DollarPos; ++LastEmitted)
+          switch (AsmString[LastEmitted]) {
+          case '\n': AddLiteralString("\\n"); break;
+          case '\t': AddLiteralString("\\t"); break;
+          case '"': AddLiteralString("\\\""); break;
+          case '\\': AddLiteralString("\\\\"); break;
+          default:
+            AddLiteralString(std::string(1, AsmString[LastEmitted]));
+            break;
+          }
+      } else {
+        LastEmitted = DollarPos;
+      }
     } else if (AsmString[DollarPos] == '\\') {
       if (DollarPos+1 != AsmString.size() &&
           (CurVariant == Variant || CurVariant == ~0U)) {
@@ -246,8 +259,6 @@ AsmWriterInst::AsmWriterInst(const CodeGenInstruction &CGI, unsigned Variant) {
       LastEmitted = VarEnd;
     }
   }
-
-  AddLiteralString("\\n");
 }
 
 /// MatchesAllButOneOp - If this instruction is exactly identical to the
@@ -270,7 +281,7 @@ unsigned AsmWriterInst::MatchesAllButOneOp(const AsmWriterInst &Other)const{
 }
 
 static void PrintCases(std::vector<std::pair<std::string,
-                       AsmWriterOperand> > &OpsToPrint, std::ostream &O) {
+                       AsmWriterOperand> > &OpsToPrint, raw_ostream &O) {
   O << "    case " << OpsToPrint.back().first << ": ";
   AsmWriterOperand TheOp = OpsToPrint.back().second;
   OpsToPrint.pop_back();
@@ -292,7 +303,7 @@ static void PrintCases(std::vector<std::pair<std::string,
 /// EmitInstructions - Emit the last instruction in the vector and any other
 /// instructions that are suitably similar to it.
 static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
-                             std::ostream &O) {
+                             raw_ostream &O) {
   AsmWriterInst FirstInst = Insts.back();
   Insts.pop_back();
 
@@ -344,7 +355,6 @@ static void EmitInstructions(std::vector<AsmWriterInst> &Insts,
     }
     O << "\n";
   }
-
   O << "    break;\n";
 }
 
@@ -363,7 +373,7 @@ FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
   
   for (unsigned i = 0, e = NumberedInstructions.size(); i != e; ++i) {
     const AsmWriterInst *Inst = getAsmWriterInstByID(i);
-    if (Inst == 0) continue;  // PHI, INLINEASM, LABEL, etc.
+    if (Inst == 0) continue;  // PHI, INLINEASM, DBG_LABEL, etc.
     
     std::string Command;
     if (Inst->Operands.empty())
@@ -372,8 +382,12 @@ FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
     Command = "    " + Inst->Operands[0].getCode() + "\n";
 
     // If this is the last operand, emit a return.
-    if (Inst->Operands.size() == 1)
+    if (Inst->Operands.size() == 1) {
+      Command += "    EmitComments(*MI);\n";
+      // Print the final newline
+      Command += "    O << \"\\n\";\n";
       Command += "    return true;\n";
+    }
     
     // Check to see if we already have 'Command' in UniqueOperandCommands.
     // If not, add it.
@@ -439,8 +453,12 @@ FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
       std::string Command = "    " + FirstInst->Operands[Op].getCode() + "\n";
       
       // If this is the last operand, emit a return after the code.
-      if (FirstInst->Operands.size() == Op+1)
+      if (FirstInst->Operands.size() == Op+1) {
+        Command += "    EmitComments(*MI);\n";
+        // Print the final newline
+        Command += "    O << \"\\n\";\n";
         Command += "    return true;\n";
+      }
       
       UniqueOperandCommands[CommandIdx] += Command;
       InstOpsUsed[CommandIdx]++;
@@ -463,7 +481,7 @@ FindUniqueOperandCommands(std::vector<std::string> &UniqueOperandCommands,
 
 
 
-void AsmWriterEmitter::run(std::ostream &O) {
+void AsmWriterEmitter::run(raw_ostream &O) {
   EmitSourceFileHeader("Assembly Writer Source Fragment", O);
 
   CodeGenTarget Target;
@@ -536,7 +554,7 @@ void AsmWriterEmitter::run(std::ostream &O) {
   }
   
   // Figure out how many bits we used for the string index.
-  unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx);
+  unsigned AsmStrBits = Log2_32_Ceil(MaxStringIdx+1);
   
   // To reduce code size, we compactify common instructions into a few bits
   // in the opcode-indexed table.
@@ -551,10 +569,11 @@ void AsmWriterEmitter::run(std::ostream &O) {
     // For the first operand check, add a default value for instructions with
     // just opcode strings to use.
     if (isFirst) {
-      UniqueOperandCommands.push_back("    return true;\n");
+      // Do the post instruction processing and print the final newline
+      UniqueOperandCommands.push_back("    EmitComments(*MI);\n    O << \"\\n\";\n    return true;\n");
       isFirst = false;
     }
-    
+
     std::vector<unsigned> InstIdxs;
     std::vector<unsigned> NumInstOpsHandled;
     FindUniqueOperandCommands(UniqueOperandCommands, InstIdxs,
@@ -637,11 +656,15 @@ void AsmWriterEmitter::run(std::ostream &O) {
   }
   O << "\";\n\n";
 
+  O << "  processDebugLoc(MI->getDebugLoc());\n\n";
+
+  O << "\n#ifndef NO_ASM_WRITER_BOILERPLATE\n";
+  
   O << "  if (MI->getOpcode() == TargetInstrInfo::INLINEASM) {\n"
     << "    O << \"\\t\";\n"
     << "    printInlineAsm(MI);\n"
     << "    return true;\n"
-    << "  } else if (MI->getOpcode() == TargetInstrInfo::LABEL) {\n"
+    << "  } else if (MI->isLabel()) {\n"
     << "    printLabel(MI);\n"
     << "    return true;\n"
     << "  } else if (MI->getOpcode() == TargetInstrInfo::DECLARE) {\n"
@@ -651,7 +674,9 @@ void AsmWriterEmitter::run(std::ostream &O) {
     << "    printImplicitDef(MI);\n"
     << "    return true;\n"
     << "  }\n\n";
-  
+
+  O << "\n#endif\n";
+
   O << "  O << \"\\t\";\n\n";
 
   O << "  // Emit the opcode for the instruction.\n"
@@ -720,6 +745,9 @@ void AsmWriterEmitter::run(std::ostream &O) {
       EmitInstructions(Instructions, O);
 
     O << "  }\n";
+    O << "  EmitComments(*MI);\n";
+    // Print the final newline
+    O << "  O << \"\\n\";\n";
     O << "  return true;\n";
   }