1 //===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
17 // Sentinel value for the absolute pseudo section.
18 const MCSection *MCSymbol::AbsolutePseudoSection =
19 reinterpret_cast<const MCSection *>(1);
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 != '@')
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");
35 // If any of the characters in the string is an unacceptable character, force
37 for (unsigned i = 0, e = Str.size(); i != e; ++i)
38 if (!isAcceptableChar(Str[i]))
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)
49 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
50 S = &Ref->getSymbol();
55 const MCSymbol *MCSymbol::getBaseSymbol(const MCAsmLayout &Layout) const {
59 const MCExpr *Expr = getVariableValue();
61 if (!Expr->EvaluateAsRelocatable(Value, &Layout))
66 if (const MCSymbolRefExpr *A = Value.getSymA())
67 return &A->getSymbol();
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!");
76 // Variables should always be marked as in the same "section" as the value.
77 const MCSection *Section = Value->FindAssociatedSection();
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)) {
95 for (unsigned I = 0, E = Name.size(); I != E; ++I) {
107 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
108 void MCSymbol::dump() const {