Update llvm-mc / MCAsmStreamer to print the instruction using the actual target
specific printer (this only works on x86, for now).
 - This makes it possible to do some correctness checking of the parsing and
   matching, since we can compare the results of 'as' on the original input, to
   those of 'as' on the output from llvm-mc.

 - In theory, we could now have an easy ATT -> Intel syntax converter. :)


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@78986 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/MC/MCAsmStreamer.cpp b/lib/MC/MCAsmStreamer.cpp
index 75390c6..ac2aabd 100644
--- a/lib/MC/MCAsmStreamer.cpp
+++ b/lib/MC/MCAsmStreamer.cpp
@@ -9,6 +9,7 @@
 
 #include "llvm/MC/MCStreamer.h"
 
+#include "llvm/CodeGen/AsmPrinter.h"
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCInst.h"
 #include "llvm/MC/MCSectionMachO.h"
@@ -23,12 +24,14 @@
 
   class MCAsmStreamer : public MCStreamer {
     raw_ostream &OS;
+    
+    AsmPrinter *Printer;
 
     MCSection *CurSection;
 
   public:
-    MCAsmStreamer(MCContext &Context, raw_ostream &_OS)
-      : MCStreamer(Context), OS(_OS), CurSection(0) {}
+    MCAsmStreamer(MCContext &Context, raw_ostream &_OS, AsmPrinter *_AsmPrinter)
+      : MCStreamer(Context), OS(_OS), Printer(_AsmPrinter), CurSection(0) {}
     ~MCAsmStreamer() {}
 
     /// @name MCStreamer Interface
@@ -75,50 +78,16 @@
 
 }
 
-/// NeedsQuoting - Return true if the string \arg Str needs quoting, i.e., it
-/// does not match [a-zA-Z_.][a-zA-Z0-9_.]*.
-//
-// FIXME: This could be more permissive, do we care?
-static inline bool NeedsQuoting(const StringRef &Str) {
-  if (Str.empty())
-    return true;
-
-  // Check that first character is in [a-zA-Z_.].
-  if (!((Str[0] >= 'a' && Str[0] <= 'z') ||
-        (Str[0] >= 'A' && Str[0] <= 'Z') ||
-        (Str[0] == '_' || Str[0] == '.')))
-    return true;
-
-  // Check subsequent characters are in [a-zA-Z0-9_.].
-  for (unsigned i = 1, e = Str.size(); i != e; ++i)
-    if (!((Str[i] >= 'a' && Str[i] <= 'z') ||
-          (Str[i] >= 'A' && Str[i] <= 'Z') ||
-          (Str[i] >= '0' && Str[i] <= '9') ||
-          (Str[i] == '_' || Str[i] == '.')))
-      return true;
-
-  return false;
-}
-
 /// Allow printing symbols directly to a raw_ostream with proper quoting.
 static inline raw_ostream &operator<<(raw_ostream &os, const MCSymbol *S) {
-  if (NeedsQuoting(S->getName()))
-    return os << '"' << S->getName() << '"';
-  return os << S->getName();
+  S->print(os);
+
+  return os;
 }
 
 /// Allow printing values directly to a raw_ostream.
 static inline raw_ostream &operator<<(raw_ostream &os, const MCValue &Value) {
-  if (Value.getSymA()) {
-    os << Value.getSymA();
-    if (Value.getSymB())
-      os << " - " << Value.getSymB();
-    if (Value.getConstant())
-      os << " + " << Value.getConstant();
-  } else {
-    assert(!Value.getSymB() && "Invalid machine code value!");
-    os << Value.getConstant();
-  }
+  Value.print(os);
 
   return os;
 }
@@ -300,7 +269,15 @@
 
 void MCAsmStreamer::EmitInstruction(const MCInst &Inst) {
   assert(CurSection && "Cannot emit contents before setting section!");
-  // FIXME: Implement proper printing.
+
+  // If we have an AsmPrinter, use that to print.
+  if (Printer) {
+    Printer->printMCInst(&Inst);
+    return;
+  }
+
+  // Otherwise fall back to a structural printing for now. Eventually we should
+  // always have access to the target specific printer.
   OS << "MCInst("
      << "opcode=" << Inst.getOpcode() << ", "
      << "operands=[";
@@ -316,6 +293,7 @@
   OS.flush();
 }
     
-MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS) {
-  return new MCAsmStreamer(Context, OS);
+MCStreamer *llvm::createAsmStreamer(MCContext &Context, raw_ostream &OS,
+                                    AsmPrinter *AP) {
+  return new MCAsmStreamer(Context, OS, AP);
 }