LLVM API Documentation

MCSymbol.cpp
Go to the documentation of this file.
00001 //===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/MC/MCSymbol.h"
00011 #include "llvm/MC/MCExpr.h"
00012 #include "llvm/Support/Debug.h"
00013 #include "llvm/Support/raw_ostream.h"
00014 using namespace llvm;
00015 
00016 // Sentinel value for the absolute pseudo section.
00017 const MCSection *MCSymbol::AbsolutePseudoSection =
00018   reinterpret_cast<const MCSection *>(1);
00019 
00020 static bool isAcceptableChar(char C) {
00021   if ((C < 'a' || C > 'z') &&
00022       (C < 'A' || C > 'Z') &&
00023       (C < '0' || C > '9') &&
00024       C != '_' && C != '$' && C != '.' && C != '@')
00025     return false;
00026   return true;
00027 }
00028 
00029 /// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be
00030 /// syntactically correct.
00031 static bool NameNeedsQuoting(StringRef Str) {
00032   assert(!Str.empty() && "Cannot create an empty MCSymbol");
00033 
00034   // If any of the characters in the string is an unacceptable character, force
00035   // quotes.
00036   for (unsigned i = 0, e = Str.size(); i != e; ++i)
00037     if (!isAcceptableChar(Str[i]))
00038       return true;
00039   return false;
00040 }
00041 
00042 const MCSymbol &MCSymbol::AliasedSymbol() const {
00043   const MCSymbol *S = this;
00044   while (S->isVariable()) {
00045     const MCExpr *Value = S->getVariableValue();
00046     if (Value->getKind() != MCExpr::SymbolRef)
00047       return *S;
00048     const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
00049     S = &Ref->getSymbol();
00050   }
00051   return *S;
00052 }
00053 
00054 void MCSymbol::setVariableValue(const MCExpr *Value) {
00055   assert(!IsUsed && "Cannot set a variable that has already been used.");
00056   assert(Value && "Invalid variable value!");
00057   this->Value = Value;
00058 
00059   // Variables should always be marked as in the same "section" as the value.
00060   const MCSection *Section = Value->FindAssociatedSection();
00061   if (Section)
00062     setSection(*Section);
00063   else
00064     setUndefined();
00065 }
00066 
00067 void MCSymbol::print(raw_ostream &OS) const {
00068   // The name for this MCSymbol is required to be a valid target name.  However,
00069   // some targets support quoting names with funny characters.  If the name
00070   // contains a funny character, then print it quoted.
00071   StringRef Name = getName();
00072   if (!NameNeedsQuoting(Name)) {
00073     OS << Name;
00074     return;
00075   }
00076 
00077   OS << '"';
00078   for (unsigned I = 0, E = Name.size(); I != E; ++I) {
00079     char C = Name[I];
00080     if (C == '\n')
00081       OS << "\\n";
00082     else if (C == '"')
00083       OS << "\\\"";
00084     else
00085       OS << C;
00086   }
00087   OS << '"';
00088 }
00089 
00090 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
00091 void MCSymbol::dump() const {
00092   print(dbgs());
00093 }
00094 #endif