Second try at making direct object emission produce the same results
as llc + llvm-mc. This time ELF is not changed and I tested that llvm-gcc
bootstrap on darwin10 using darwin9's assembler and linker.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121006 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/MC/MCAsmInfoDarwin.cpp b/lib/MC/MCAsmInfoDarwin.cpp
index 1147f02..a382741 100644
--- a/lib/MC/MCAsmInfoDarwin.cpp
+++ b/lib/MC/MCAsmInfoDarwin.cpp
@@ -40,7 +40,8 @@
 
   // FIXME: Darwin 10 and newer don't need this.
   LinkerRequiresNonEmptyDwarfLines = true;
-  
+
+  NeedsSetToChangeDiffSize = true;
   HiddenVisibilityAttr = MCSA_PrivateExtern;
   // Doesn't support protected visibility.
   ProtectedVisibilityAttr = MCSA_Global;
diff --git a/lib/MC/MCAsmStreamer.cpp b/lib/MC/MCAsmStreamer.cpp
index ab807ed..ce85e46 100644
--- a/lib/MC/MCAsmStreamer.cpp
+++ b/lib/MC/MCAsmStreamer.cpp
@@ -512,32 +512,6 @@
   EmitValue(MCConstantExpr::Create(Value, getContext()), Size, AddrSpace);
 }
 
-static bool hasSymbolDifference(const MCExpr *Value) {
-  switch (Value->getKind()) {
-  case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!");
-  case MCExpr::Constant:
-  case MCExpr::SymbolRef:
-    return false;
-  case MCExpr::Unary:
-    return hasSymbolDifference(cast<MCUnaryExpr>(Value)->getSubExpr());
-  case MCExpr::Binary: {
-    const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
-    if (BE->getOpcode() == MCBinaryExpr::Sub &&
-	BE->getLHS()->getKind() == MCExpr::SymbolRef &&
-	BE->getRHS()->getKind() == MCExpr::SymbolRef)
-      return true;
-    return hasSymbolDifference(BE->getLHS()) ||
-      hasSymbolDifference(BE->getRHS());
-  }
-  }
-  llvm_unreachable("Switch covers all cases");
-}
-
-bool MCAsmStreamer::needsSet(const MCExpr *Value) {
-  return getContext().getAsmInfo().needsSetToChangeDiffSize() &&
-    hasSymbolDifference(Value);
-}
-
 void MCAsmStreamer::EmitValue(const MCExpr *Value, unsigned Size,
                               unsigned AddrSpace) {
   assert(CurSection && "Cannot emit contents before setting section!");
@@ -565,14 +539,6 @@
   }
 
   assert(Directive && "Invalid size for machine code value!");
-  if (needsSet(Value)) {
-    MCSymbol *SetLabel = getContext().CreateTempSymbol();
-    EmitAssignment(SetLabel, Value);
-    OS << Directive << *SetLabel;
-    EmitEOL();
-    return;
-  }
-
   OS << Directive << *Value;
   EmitEOL();
 }
diff --git a/lib/MC/MCDwarf.cpp b/lib/MC/MCDwarf.cpp
index 53731dc..3e10f95 100644
--- a/lib/MC/MCDwarf.cpp
+++ b/lib/MC/MCDwarf.cpp
@@ -213,15 +213,8 @@
 
   // The first 4 bytes is the total length of the information for this
   // compilation unit (not including these 4 bytes for the length).
-  // FIXME: We create the dummy TotalLength variable because LineEndSym points
-  // to the end of the section and the darwin assembler doesn't consider that
-  // difference an assembly time constant. It might be better for this to be
-  // proected by a flag.
-  MCSymbol *TotalLength = MCOS->getContext().CreateTempSymbol();
-  MCOS->EmitAssignment(TotalLength,
-		       MakeStartMinusEndExpr(MCOS, LineStartSym, LineEndSym,
-					     4));
-  MCOS->EmitSymbolValue(TotalLength, 4, 0);
+  MCOS->EmitAbsValue(MakeStartMinusEndExpr(MCOS, LineStartSym, LineEndSym,4),
+                     4);
 
   // Next 2 bytes is the Version, which is Dwarf 2.
   MCOS->EmitIntValue(2, 2);
@@ -233,7 +226,7 @@
   // section to the end of the prologue.  Not including the 4 bytes for the
   // total length, the 2 bytes for the version, and these 4 bytes for the
   // length of the prologue.
-  MCOS->EmitValue(MakeStartMinusEndExpr(MCOS, LineStartSym, ProEndSym,
+  MCOS->EmitAbsValue(MakeStartMinusEndExpr(MCOS, LineStartSym, ProEndSym,
                                         (4 + 2 + 4)),
                   4, 0);
 
diff --git a/lib/MC/MCStreamer.cpp b/lib/MC/MCStreamer.cpp
index 6df4ae4..096cea7 100644
--- a/lib/MC/MCStreamer.cpp
+++ b/lib/MC/MCStreamer.cpp
@@ -7,6 +7,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/MC/MCAsmInfo.h"
 #include "llvm/MC/MCContext.h"
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCExpr.h"
@@ -72,6 +73,17 @@
   EmitBytes(OSE.str(), AddrSpace);
 }
 
+void MCStreamer::EmitAbsValue(const MCExpr *Value, unsigned Size,
+                              unsigned AddrSpace) {
+  if (!getContext().getAsmInfo().needsSetToChangeDiffSize()) {
+    EmitValue(Value, Size, AddrSpace);
+    return;
+  }
+  MCSymbol *ABS = getContext().CreateTempSymbol();
+  EmitAssignment(ABS, Value);
+  EmitSymbolValue(ABS, Size, AddrSpace);
+}
+
 void MCStreamer::EmitSymbolValue(const MCSymbol *Sym, unsigned Size,
                                  unsigned AddrSpace) {
   EmitValue(MCSymbolRefExpr::Create(Sym, getContext()), Size, AddrSpace);