Support standard DWARF TLS opcode; Darwin and PS4 use it.
[oota-llvm.git] / lib / MC / MCSymbol.cpp
index 68ab24d9523a37b1cb0a7d959bb4b7923f86ebc0..24165254e56a67d115c700f3f797df7f03086459 100644 (file)
@@ -8,7 +8,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/MC/MCSymbol.h"
-#include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCExpr.h"
+#include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
 using namespace llvm;
 
@@ -16,42 +17,78 @@ using namespace llvm;
 const MCSection *MCSymbol::AbsolutePseudoSection =
   reinterpret_cast<const MCSection *>(1);
 
-/// ShouldQuoteIdentifier - Return true if the identifier \arg Str needs quotes
-/// for this assembler.
-static bool ShouldQuoteIdentifier(const StringRef &Str, const MCAsmInfo &MAI) {
-  // If the assembler doesn't support quotes, never use them.
-  if (!MAI.doesAllowQuotesInName())
+static bool isAcceptableChar(char C) {
+  if ((C < 'a' || C > 'z') &&
+      (C < 'A' || C > 'Z') &&
+      (C < '0' || C > '9') &&
+      C != '_' && C != '$' && C != '.' && C != '@')
     return false;
-  
-  // If empty, we need quotes.
-  if (Str.empty())
-    return true;
-  
-  // If the first character is a number, we need quotes.
-  if (Str[0] >= '0' && Str[0] <= '9')
-    return true;
+  return true;
+}
+
+/// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be
+/// syntactically correct.
+static bool NameNeedsQuoting(StringRef Str) {
+  assert(!Str.empty() && "Cannot create an empty MCSymbol");
 
   // If any of the characters in the string is an unacceptable character, force
   // quotes.
-  for (unsigned i = 0, e = Str.size(); i != e; ++i) {
-    char C = Str[i];
-  
-    if ((C < 'a' || C > 'z') &&
-        (C < 'A' || C > 'Z') &&
-        (C < '0' || C > '9') &&
-        C != '_' && C != '$' && C != '.')
+  for (unsigned i = 0, e = Str.size(); i != e; ++i)
+    if (!isAcceptableChar(Str[i]))
       return true;
-  }
   return false;
 }
 
-void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
-  if (!MAI || ShouldQuoteIdentifier(getName(), *MAI))
-    OS << '"' << getName() << '"';
+const MCSymbol &MCSymbol::AliasedSymbol() const {
+  const MCSymbol *S = this;
+  while (S->isVariable()) {
+    const MCExpr *Value = S->getVariableValue();
+    if (Value->getKind() != MCExpr::SymbolRef)
+      return *S;
+    const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
+    S = &Ref->getSymbol();
+  }
+  return *S;
+}
+
+void MCSymbol::setVariableValue(const MCExpr *Value) {
+  assert(!IsUsed && "Cannot set a variable that has already been used.");
+  assert(Value && "Invalid variable value!");
+  this->Value = Value;
+
+  // Variables should always be marked as in the same "section" as the value.
+  const MCSection *Section = Value->FindAssociatedSection();
+  if (Section)
+    setSection(*Section);
   else
-    OS << getName();
+    setUndefined();
+}
+
+void MCSymbol::print(raw_ostream &OS) const {
+  // The name for this MCSymbol is required to be a valid target name.  However,
+  // some targets support quoting names with funny characters.  If the name
+  // contains a funny character, then print it quoted.
+  StringRef Name = getName();
+  if (!NameNeedsQuoting(Name)) {
+    OS << Name;
+    return;
+  }
+
+  OS << '"';
+  for (unsigned I = 0, E = Name.size(); I != E; ++I) {
+    char C = Name[I];
+    if (C == '\n')
+      OS << "\\n";
+    else if (C == '"')
+      OS << "\\\"";
+    else
+      OS << C;
+  }
+  OS << '"';
 }
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 void MCSymbol::dump() const {
-  print(errs(), 0);
+  print(dbgs());
 }
+#endif