LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MCSymbol.cpp
Go to the documentation of this file.
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/Support/Debug.h"
14 using namespace llvm;
15 
16 // Sentinel value for the absolute pseudo section.
17 const MCSection *MCSymbol::AbsolutePseudoSection =
18  reinterpret_cast<const MCSection *>(1);
19 
20 static bool isAcceptableChar(char C) {
21  if ((C < 'a' || C > 'z') &&
22  (C < 'A' || C > 'Z') &&
23  (C < '0' || C > '9') &&
24  C != '_' && C != '$' && C != '.' && C != '@')
25  return false;
26  return true;
27 }
28 
29 /// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be
30 /// syntactically correct.
31 static bool NameNeedsQuoting(StringRef Str) {
32  assert(!Str.empty() && "Cannot create an empty MCSymbol");
33 
34  // If any of the characters in the string is an unacceptable character, force
35  // quotes.
36  for (unsigned i = 0, e = Str.size(); i != e; ++i)
37  if (!isAcceptableChar(Str[i]))
38  return true;
39  return false;
40 }
41 
43  const MCSymbol *S = this;
44  while (S->isVariable()) {
45  const MCExpr *Value = S->getVariableValue();
46  if (Value->getKind() != MCExpr::SymbolRef)
47  return *S;
48  const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
49  S = &Ref->getSymbol();
50  }
51  return *S;
52 }
53 
55  assert(!IsUsed && "Cannot set a variable that has already been used.");
56  assert(Value && "Invalid variable value!");
57  this->Value = Value;
58 
59  // Variables should always be marked as in the same "section" as the value.
60  const MCSection *Section = Value->FindAssociatedSection();
61  if (Section)
62  setSection(*Section);
63  else
64  setUndefined();
65 }
66 
67 void MCSymbol::print(raw_ostream &OS) const {
68  // The name for this MCSymbol is required to be a valid target name. However,
69  // some targets support quoting names with funny characters. If the name
70  // contains a funny character, then print it quoted.
71  StringRef Name = getName();
72  if (!NameNeedsQuoting(Name)) {
73  OS << Name;
74  return;
75  }
76 
77  OS << '"';
78  for (unsigned I = 0, E = Name.size(); I != E; ++I) {
79  char C = Name[I];
80  if (C == '\n')
81  OS << "\\n";
82  else if (C == '"')
83  OS << "\\\"";
84  else
85  OS << C;
86  }
87  OS << '"';
88 }
89 
90 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
91 void MCSymbol::dump() const {
92  print(dbgs());
93 }
94 #endif
const MCSymbol & getSymbol() const
Definition: MCExpr.h:283
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
ExprKind getKind() const
Definition: MCExpr.h:61
static bool isAcceptableChar(char C)
Definition: MCSymbol.cpp:20
const MCExpr * getVariableValue() const
getVariableValue() - Get the value for variable symbols.
Definition: MCSymbol.h:137
static bool NameNeedsQuoting(StringRef Str)
Definition: MCSymbol.cpp:31
const MCSection * FindAssociatedSection() const
Definition: MCExpr.cpp:743
void setUndefined()
setUndefined - Mark the symbol as undefined.
Definition: MCSymbol.h:120
void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:54
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:70
const MCSymbol & AliasedSymbol() const
Definition: MCSymbol.cpp:42
#define I(x, y, z)
Definition: MD5.cpp:54
void setSection(const MCSection &S)
setSection - Mark the symbol as defined in the section S.
Definition: MCSymbol.h:117
References to labels and assigned expressions.
Definition: MCExpr.h:36
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition: MCSymbol.h:132
LLVM Value Representation.
Definition: Value.h:66
void print(raw_ostream &OS) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:67
void dump() const
dump - Print the value to stderr.
Definition: MCSymbol.cpp:91
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110