1198090Srdivacky//===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===// 2198090Srdivacky// 3198090Srdivacky// The LLVM Compiler Infrastructure 4198090Srdivacky// 5198090Srdivacky// This file is distributed under the University of Illinois Open Source 6198090Srdivacky// License. See LICENSE.TXT for details. 7198090Srdivacky// 8198090Srdivacky//===----------------------------------------------------------------------===// 9198090Srdivacky 10198090Srdivacky#include "llvm/MC/MCSymbol.h" 11208599Srdivacky#include "llvm/MC/MCExpr.h" 12202375Srdivacky#include "llvm/Support/Debug.h" 13198090Srdivacky#include "llvm/Support/raw_ostream.h" 14198090Srdivackyusing namespace llvm; 15198090Srdivacky 16198090Srdivacky// Sentinel value for the absolute pseudo section. 17198090Srdivackyconst MCSection *MCSymbol::AbsolutePseudoSection = 18198090Srdivacky reinterpret_cast<const MCSection *>(1); 19198090Srdivacky 20198090Srdivackystatic bool isAcceptableChar(char C) { 21198090Srdivacky if ((C < 'a' || C > 'z') && 22198090Srdivacky (C < 'A' || C > 'Z') && 23198090Srdivacky (C < '0' || C > '9') && 24198090Srdivacky C != '_' && C != '$' && C != '.' && C != '@') 25198090Srdivacky return false; 26198090Srdivacky return true; 27198090Srdivacky} 28198090Srdivacky 29245431Sdim/// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be 30202878Srdivacky/// syntactically correct. 31202878Srdivackystatic bool NameNeedsQuoting(StringRef Str) { 32198090Srdivacky assert(!Str.empty() && "Cannot create an empty MCSymbol"); 33245431Sdim 34198090Srdivacky // If any of the characters in the string is an unacceptable character, force 35198090Srdivacky // quotes. 36198090Srdivacky for (unsigned i = 0, e = Str.size(); i != e; ++i) 37198090Srdivacky if (!isAcceptableChar(Str[i])) 38198090Srdivacky return true; 39198090Srdivacky return false; 40198090Srdivacky} 41198090Srdivacky 42218893Sdimconst MCSymbol &MCSymbol::AliasedSymbol() const { 43218893Sdim const MCSymbol *S = this; 44218893Sdim while (S->isVariable()) { 45218893Sdim const MCExpr *Value = S->getVariableValue(); 46218893Sdim if (Value->getKind() != MCExpr::SymbolRef) 47218893Sdim return *S; 48218893Sdim const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value); 49218893Sdim S = &Ref->getSymbol(); 50218893Sdim } 51218893Sdim return *S; 52218893Sdim} 53218893Sdim 54208599Srdivackyvoid MCSymbol::setVariableValue(const MCExpr *Value) { 55218893Sdim assert(!IsUsed && "Cannot set a variable that has already been used."); 56208599Srdivacky assert(Value && "Invalid variable value!"); 57208599Srdivacky this->Value = Value; 58208599Srdivacky 59221345Sdim // Variables should always be marked as in the same "section" as the value. 60221345Sdim const MCSection *Section = Value->FindAssociatedSection(); 61235633Sdim if (Section) 62221345Sdim setSection(*Section); 63235633Sdim else 64221345Sdim setUndefined(); 65208599Srdivacky} 66208599Srdivacky 67202878Srdivackyvoid MCSymbol::print(raw_ostream &OS) const { 68202878Srdivacky // The name for this MCSymbol is required to be a valid target name. However, 69202878Srdivacky // some targets support quoting names with funny characters. If the name 70202878Srdivacky // contains a funny character, then print it quoted. 71263509Sdim StringRef Name = getName(); 72263509Sdim if (!NameNeedsQuoting(Name)) { 73263509Sdim OS << Name; 74198090Srdivacky return; 75198090Srdivacky } 76245431Sdim 77263509Sdim OS << '"'; 78263509Sdim for (unsigned I = 0, E = Name.size(); I != E; ++I) { 79263509Sdim char C = Name[I]; 80263509Sdim if (C == '\n') 81263509Sdim OS << "\\n"; 82263509Sdim else if (C == '"') 83263509Sdim OS << "\\\""; 84263509Sdim else 85263509Sdim OS << C; 86263509Sdim } 87263509Sdim OS << '"'; 88198090Srdivacky} 89198090Srdivacky 90245431Sdim#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 91198090Srdivackyvoid MCSymbol::dump() const { 92202878Srdivacky print(dbgs()); 93198090Srdivacky} 94245431Sdim#endif 95