Correctly handle an ELF symbol defined with "a = b + expr".
[oota-llvm.git] / lib / MC / MCSymbol.cpp
1 //===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/MC/MCSymbol.h"
11 #include "llvm/MC/MCExpr.h"
12 #include "llvm/MC/MCValue.h"
13 #include "llvm/Support/Debug.h"
14 #include "llvm/Support/raw_ostream.h"
15 using namespace llvm;
16
17 // Sentinel value for the absolute pseudo section.
18 const MCSection *MCSymbol::AbsolutePseudoSection =
19   reinterpret_cast<const MCSection *>(1);
20
21 static bool isAcceptableChar(char C) {
22   if ((C < 'a' || C > 'z') &&
23       (C < 'A' || C > 'Z') &&
24       (C < '0' || C > '9') &&
25       C != '_' && C != '$' && C != '.' && C != '@')
26     return false;
27   return true;
28 }
29
30 /// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be
31 /// syntactically correct.
32 static bool NameNeedsQuoting(StringRef Str) {
33   assert(!Str.empty() && "Cannot create an empty MCSymbol");
34
35   // If any of the characters in the string is an unacceptable character, force
36   // quotes.
37   for (unsigned i = 0, e = Str.size(); i != e; ++i)
38     if (!isAcceptableChar(Str[i]))
39       return true;
40   return false;
41 }
42
43 const MCSymbol &MCSymbol::AliasedSymbol() const {
44   const MCSymbol *S = this;
45   while (S->isVariable()) {
46     const MCExpr *Value = S->getVariableValue();
47     if (Value->getKind() != MCExpr::SymbolRef)
48       return *S;
49     const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
50     S = &Ref->getSymbol();
51   }
52   return *S;
53 }
54
55 const MCSymbol *MCSymbol::getBaseSymbol(const MCAsmLayout &Layout) const {
56   if (!isVariable())
57     return this;
58
59   const MCExpr *Expr = getVariableValue();
60   MCValue Value;
61   if (!Expr->EvaluateAsRelocatable(Value, &Layout))
62     return nullptr;
63
64   if (Value.getSymB())
65     return nullptr;
66   if (const MCSymbolRefExpr *A = Value.getSymA())
67     return &A->getSymbol();
68   return nullptr;
69 }
70
71 void MCSymbol::setVariableValue(const MCExpr *Value) {
72   assert(!IsUsed && "Cannot set a variable that has already been used.");
73   assert(Value && "Invalid variable value!");
74   this->Value = Value;
75
76   // Variables should always be marked as in the same "section" as the value.
77   const MCSection *Section = Value->FindAssociatedSection();
78   if (Section)
79     setSection(*Section);
80   else
81     setUndefined();
82 }
83
84 void MCSymbol::print(raw_ostream &OS) const {
85   // The name for this MCSymbol is required to be a valid target name.  However,
86   // some targets support quoting names with funny characters.  If the name
87   // contains a funny character, then print it quoted.
88   StringRef Name = getName();
89   if (!NameNeedsQuoting(Name)) {
90     OS << Name;
91     return;
92   }
93
94   OS << '"';
95   for (unsigned I = 0, E = Name.size(); I != E; ++I) {
96     char C = Name[I];
97     if (C == '\n')
98       OS << "\\n";
99     else if (C == '"')
100       OS << "\\\"";
101     else
102       OS << C;
103   }
104   OS << '"';
105 }
106
107 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
108 void MCSymbol::dump() const {
109   print(dbgs());
110 }
111 #endif