Subzero: Refactor newline emission for Inst::emit().

The (final) newline is emitted by the caller of emit(), instead of
by all the emit() implementations.  This sets the stage for being
able to add useful comments to the textual asm, such as annotating
which registers became free after the instruction.

BUG= none
R=jvoung@chromium.org

Review URL: https://codereview.chromium.org/681783002
diff --git a/src/IceCfgNode.cpp b/src/IceCfgNode.cpp
index a1804d5..4243648 100644
--- a/src/IceCfgNode.cpp
+++ b/src/IceCfgNode.cpp
@@ -510,6 +510,7 @@
       I->emitIAS(Func);
     } else {
       I->emit(Func);
+      Str << "\n";
     }
     // Update emitted instruction count, plus fill/spill count for
     // Variable operands without a physical register.
diff --git a/src/IceInst.cpp b/src/IceInst.cpp
index 4aa6e69..c9eb95e 100644
--- a/src/IceInst.cpp
+++ b/src/IceInst.cpp
@@ -709,7 +709,6 @@
   getDest()->emit(Func);
   Str << " = def.pseudo ";
   emitSources(Func);
-  Str << "\n";
 }
 
 void InstFakeDef::dump(const Cfg *Func) const {
@@ -724,7 +723,6 @@
   Str << "\t# ";
   Str << "use.pseudo ";
   emitSources(Func);
-  Str << "\n";
 }
 
 void InstFakeUse::dump(const Cfg *Func) const {
@@ -740,7 +738,6 @@
     Str << "// ";
   Str << "kill.pseudo ";
   emitSources(Func);
-  Str << "\n";
 }
 
 void InstFakeKill::dump(const Cfg *Func) const {
diff --git a/src/IceInstX8632.cpp b/src/IceInstX8632.cpp
index ae4a30d..23948e4 100644
--- a/src/IceInstX8632.cpp
+++ b/src/IceInstX8632.cpp
@@ -410,7 +410,7 @@
 
 void InstX8632Label::emit(const Cfg *Func) const {
   Ostream &Str = Func->getContext()->getStrEmit();
-  Str << getName(Func) << ":\n";
+  Str << getName(Func) << ":";
 }
 
 void InstX8632Label::emitIAS(const Cfg *Func) const {
@@ -438,14 +438,14 @@
   }
 
   if (Label) {
-    Str << "\t" << Label->getName(Func) << "\n";
+    Str << "\t" << Label->getName(Func);
   } else {
     if (Condition == CondX86::Br_None) {
-      Str << "\t" << getTargetFalse()->getAsmName() << "\n";
+      Str << "\t" << getTargetFalse()->getAsmName();
     } else {
-      Str << "\t" << getTargetTrue()->getAsmName() << "\n";
+      Str << "\t" << getTargetTrue()->getAsmName();
       if (getTargetFalse()) {
-        Str << "\tjmp\t" << getTargetFalse()->getAsmName() << "\n";
+        Str << "\n\tjmp\t" << getTargetFalse()->getAsmName();
       }
     }
   }
@@ -518,7 +518,6 @@
   assert(getSrcSize() == 1);
   Str << "\tcall\t";
   getCallTarget()->emit(Func);
-  Str << "\n";
   Func->getTarget()->resetStackAdjustment();
 }
 
@@ -562,6 +561,7 @@
     // instruction sizes/positions are correct for jumps.
     // For now, fall back to the regular .s emission, after filling the buffer.
     emit(Func);
+    Func->getContext()->getStrEmit() << "\n";
   } else {
     emitIASBytes(Func, Asm, StartPosition);
   }
@@ -600,7 +600,6 @@
   }
   if (!EmittedSrc1)
     Inst->getSrc(1)->emit(Func);
-  Str << "\n";
 }
 
 void emitIASOpTyGPR(const Cfg *Func, Type Ty, const Operand *Op,
@@ -1147,7 +1146,6 @@
   getDest()->emit(Func);
   Str << ", ";
   getSrc(0)->emit(Func);
-  Str << "\n";
 }
 
 template <> void InstX8632Addss::emit(const Cfg *Func) const {
@@ -1236,7 +1234,6 @@
   assert(getSrcSize() == 3);
   Str << "\t" << Opcode << "\t";
   getSrc(1)->emit(Func);
-  Str << "\n";
 }
 
 template <> void InstX8632Div::emitIAS(const Cfg *Func) const {
@@ -1253,7 +1250,6 @@
   assert(getSrcSize() == 3);
   Str << "\t" << Opcode << "\t";
   getSrc(1)->emit(Func);
-  Str << "\n";
 }
 
 template <> void InstX8632Idiv::emitIAS(const Cfg *Func) const {
@@ -1278,7 +1274,6 @@
   Inst->getDest()->emit(Func);
   Str << ", ";
   Inst->getSrc(1)->emit(Func);
-  Str << "\n";
 }
 
 void
@@ -1332,7 +1327,6 @@
     assert(Src0 && Src0->getRegNum() == RegX8632::Reg_eax);
     Str << "\timul\t";
     getSrc(1)->emit(Func);
-    Str << "\n";
   } else if (llvm::isa<Constant>(getSrc(1))) {
     Str << "\timul\t";
     getDest()->emit(Func);
@@ -1340,7 +1334,6 @@
     getSrc(0)->emit(Func);
     Str << ", ";
     getSrc(1)->emit(Func);
-    Str << "\n";
   } else {
     emitTwoAddress("imul", this, Func);
   }
@@ -1397,15 +1390,15 @@
     break;
   case IceType_i8:
     assert(getDest()->getRegNum() == RegX8632::Reg_eax);
-    Str << "\tcbw\n";
+    Str << "\tcbw";
     break;
   case IceType_i16:
     assert(getDest()->getRegNum() == RegX8632::Reg_edx);
-    Str << "\tcwd\n";
+    Str << "\tcwd";
     break;
   case IceType_i32:
     assert(getDest()->getRegNum() == RegX8632::Reg_edx);
-    Str << "\tcdq\n";
+    Str << "\tcdq";
     break;
   }
 }
@@ -1445,7 +1438,6 @@
   assert(getDest()->getRegNum() == RegX8632::Reg_eax); // TODO: allow edx?
   Str << "\tmul\t";
   getSrc(1)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Mul::emitIAS(const Cfg *Func) const {
@@ -1483,7 +1475,6 @@
   } else {
     getSrc(2)->emit(Func);
   }
-  Str << "\n";
 }
 
 void InstX8632Shld::emitIAS(const Cfg *Func) const {
@@ -1520,7 +1511,6 @@
   } else {
     getSrc(2)->emit(Func);
   }
-  Str << "\n";
 }
 
 void InstX8632Shrd::emitIAS(const Cfg *Func) const {
@@ -1550,7 +1540,6 @@
   getDest()->emit(Func);
   Str << ", ";
   getSrc(1)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Cmov::emitIAS(const Cfg *Func) const {
@@ -1587,7 +1576,6 @@
   getDest()->emit(Func);
   Str << ", ";
   getSrc(1)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Cmpps::emitIAS(const Cfg *Func) const {
@@ -1630,7 +1618,6 @@
   getSrc(0)->emit(Func);
   Str << ", ";
   getSrc(2)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Cmpxchg::emitIAS(const Cfg *Func) const {
@@ -1670,7 +1657,6 @@
   }
   Str << "\tcmpxchg8b\t";
   getSrc(0)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Cmpxchg8b::emitIAS(const Cfg *Func) const {
@@ -1707,7 +1693,6 @@
   getDest()->emit(Func);
   Str << ", ";
   getSrc(0)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Cvt::emitIAS(const Cfg *Func) const {
@@ -1787,7 +1772,6 @@
   getSrc(0)->emit(Func);
   Str << ", ";
   getSrc(1)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Icmp::emitIAS(const Cfg *Func) const {
@@ -1824,7 +1808,6 @@
   getSrc(0)->emit(Func);
   Str << ", ";
   getSrc(1)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Ucomiss::emitIAS(const Cfg *Func) const {
@@ -1849,7 +1832,7 @@
 void InstX8632UD2::emit(const Cfg *Func) const {
   Ostream &Str = Func->getContext()->getStrEmit();
   assert(getSrcSize() == 0);
-  Str << "\tud2\n";
+  Str << "\tud2";
 }
 
 void InstX8632UD2::emitIAS(const Cfg *Func) const {
@@ -1871,7 +1854,6 @@
   getSrc(0)->emit(Func);
   Str << ", ";
   getSrc(1)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Test::emitIAS(const Cfg *Func) const {
@@ -1905,7 +1887,7 @@
 void InstX8632Mfence::emit(const Cfg *Func) const {
   Ostream &Str = Func->getContext()->getStrEmit();
   assert(getSrcSize() == 0);
-  Str << "\tmfence\n";
+  Str << "\tmfence";
 }
 
 void InstX8632Mfence::emitIAS(const Cfg *Func) const {
@@ -1928,7 +1910,6 @@
   getSrc(1)->emit(Func);
   Str << ", ";
   getSrc(0)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Store::emitIAS(const Cfg *Func) const {
@@ -1978,7 +1959,6 @@
   getSrc(1)->emit(Func);
   Str << ", ";
   getSrc(0)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632StoreP::emitIAS(const Cfg *Func) const {
@@ -2011,7 +1991,6 @@
   getSrc(1)->emit(Func);
   Str << ", ";
   getSrc(0)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632StoreQ::emitIAS(const Cfg *Func) const {
@@ -2051,7 +2030,6 @@
   } else {
     Src0->emit(Func);
   }
-  Str << "\n";
 }
 
 template <> void InstX8632Mov::emit(const Cfg *Func) const {
@@ -2095,12 +2073,11 @@
     Str << ", %";
     getDest()->emit(Func);
     Str << "\n";
-    Str << ".intel_syntax\n";
+    Str << ".intel_syntax";
   } else {
     getDest()->asType(Src->getType()).emit(Func);
     Str << ", ";
     Src->emit(Func);
-    Str << "\n";
   }
 }
 
@@ -2221,7 +2198,6 @@
   getDest()->emit(Func);
   Str << ", ";
   getSrc(0)->emit(Func);
-  Str << "\n";
 }
 
 template <> void InstX8632Movp::emitIAS(const Cfg *Func) const {
@@ -2245,7 +2221,6 @@
   getDest()->emit(Func);
   Str << ", ";
   getSrc(0)->emit(Func);
-  Str << "\n";
 }
 
 template <> void InstX8632Movq::emitIAS(const Cfg *Func) const {
@@ -2301,7 +2276,7 @@
 void InstX8632Nop::emit(const Cfg *Func) const {
   Ostream &Str = Func->getContext()->getStrEmit();
   // TODO: Emit the right code for each variant.
-  Str << "\tnop\t# variant = " << Variant << "\n";
+  Str << "\tnop\t# variant = " << Variant;
 }
 
 void InstX8632Nop::emitIAS(const Cfg *Func) const {
@@ -2332,12 +2307,11 @@
     Var->emit(Func);
     Str << "\n";
     Str << "\tfld\t" << TypeX8632Attributes[Ty].WidthString << " [esp]\n";
-    Str << "\tadd\tesp, " << Width << "\n";
+    Str << "\tadd\tesp, " << Width;
     return;
   }
   Str << "\tfld\t";
   getSrc(0)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Fld::emitIAS(const Cfg *Func) const {
@@ -2386,13 +2360,12 @@
   // Even if Dest is unused, the fstp should be kept for the SideEffects
   // of popping the stack.
   if (getDest() == NULL) {
-    Str << "\tfstp\tst(0)\n";
+    Str << "\tfstp\tst(0)";
     return;
   }
   if (!getDest()->hasReg()) {
     Str << "\tfstp\t";
     getDest()->emit(Func);
-    Str << "\n";
     return;
   }
   // Dest is a physical (xmm) register, so st(0) needs to go through
@@ -2406,7 +2379,7 @@
   Str << "\tmov" << TypeX8632Attributes[Ty].SdSsString << "\t";
   getDest()->emit(Func);
   Str << ", " << TypeX8632Attributes[Ty].WidthString << " [esp]\n";
-  Str << "\tadd\tesp, " << Width << "\n";
+  Str << "\tadd\tesp, " << Width;
 }
 
 void InstX8632Fstp::emitIAS(const Cfg *Func) const {
@@ -2484,7 +2457,6 @@
   getSrc(0)->emit(Func);
   Str << ", ";
   getSrc(1)->emit(Func);
-  Str << "\n";
 }
 
 template <> void InstX8632Pextr::emitIAS(const Cfg *Func) const {
@@ -2534,7 +2506,6 @@
   }
   Str << ", ";
   getSrc(2)->emit(Func);
-  Str << "\n";
 }
 
 template <> void InstX8632Pinsr::emitIAS(const Cfg *Func) const {
@@ -2588,7 +2559,6 @@
   assert(getSrcSize() == 0);
   Str << "\tpop\t";
   getDest()->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Pop::emitIAS(const Cfg *Func) const {
@@ -2612,7 +2582,7 @@
 
 void InstX8632AdjustStack::emit(const Cfg *Func) const {
   Ostream &Str = Func->getContext()->getStrEmit();
-  Str << "\tsub\tesp, " << Amount << "\n";
+  Str << "\tsub\tesp, " << Amount;
   Func->getTarget()->updateStackAdjustment(Amount);
 }
 
@@ -2637,7 +2607,6 @@
   assert(Var->hasReg());
   Str << "\tpush\t";
   Var->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Push::emitIAS(const Cfg *Func) const {
@@ -2681,7 +2650,7 @@
 
 void InstX8632Ret::emit(const Cfg *Func) const {
   Ostream &Str = Func->getContext()->getStrEmit();
-  Str << "\tret\n";
+  Str << "\tret";
 }
 
 void InstX8632Ret::emitIAS(const Cfg *Func) const {
@@ -2707,7 +2676,6 @@
   getSrc(0)->emit(Func);
   Str << ", ";
   getSrc(1)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Xadd::emitIAS(const Cfg *Func) const {
@@ -2745,7 +2713,6 @@
   getSrc(0)->emit(Func);
   Str << ", ";
   getSrc(1)->emit(Func);
-  Str << "\n";
 }
 
 void InstX8632Xchg::emitIAS(const Cfg *Func) const {
diff --git a/src/IceInstX8632.h b/src/IceInstX8632.h
index 1778437..21a95ba 100644
--- a/src/IceInstX8632.h
+++ b/src/IceInstX8632.h
@@ -464,7 +464,6 @@
     assert(getSrcSize() == 1);
     Str << "\t" << Opcode << "\t";
     getSrc(0)->emit(Func);
-    Str << "\n";
   }
   void emitIAS(const Cfg *Func) const override {
     assert(getSrcSize() == 1);
@@ -515,7 +514,6 @@
     getDest()->emit(Func);
     Str << ", ";
     getSrc(0)->emit(Func);
-    Str << "\n";
   }
   void emitIAS(const Cfg *Func) const override {
     assert(getSrcSize() == 1);
@@ -563,7 +561,6 @@
     getDest()->emit(Func);
     Str << ", ";
     getSrc(0)->emit(Func);
-    Str << "\n";
   }
   void emitIAS(const Cfg *Func) const override {
     Type Ty = getDest()->getType();
@@ -784,7 +781,6 @@
     getSrc(1)->emit(Func);
     Str << ", ";
     getSrc(2)->emit(Func);
-    Str << "\n";
   }
   void emitIAS(const Cfg *Func) const override;
   void dump(const Cfg *Func) const override {
@@ -827,7 +823,6 @@
     getSrc(0)->emit(Func);
     Str << ", ";
     getSrc(1)->emit(Func);
-    Str << "\n";
   }
   void emitIAS(const Cfg *Func) const override;
   void dump(const Cfg *Func) const override {