LLVM  3.7.0
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/MCAsmInfo.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/Support/Debug.h"
17 using namespace llvm;
18 
19 // Sentinel value for the absolute pseudo section.
20 MCSection *MCSymbol::AbsolutePseudoSection = reinterpret_cast<MCSection *>(1);
21 
22 void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
23  MCContext &Ctx) {
24  // We may need more space for a Name to account for alignment. So allocate
25  // space for the storage type and not the name pointer.
26  size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
27 
28  // For safety, ensure that the alignment of a pointer is enough for an
29  // MCSymbol. This also ensures we don't need padding between the name and
30  // symbol.
31  static_assert((unsigned)AlignOf<MCSymbol>::Alignment <=
33  "Bad alignment of MCSymbol");
34  void *Storage = Ctx.allocate(Size, alignOf<NameEntryStorageTy>());
35  NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
36  NameEntryStorageTy *End = Start + (Name ? 1 : 0);
37  return End;
38 }
39 
41  assert(!IsUsed && "Cannot set a variable that has already been used.");
42  assert(Value && "Invalid variable value!");
43  assert((SymbolContents == SymContentsUnset ||
45  "Cannot give common/offset symbol a variable value");
46  this->Value = Value;
48  setUndefined();
49 }
50 
51 void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
52  // The name for this MCSymbol is required to be a valid target name. However,
53  // some targets support quoting names with funny characters. If the name
54  // contains a funny character, then print it quoted.
56  if (!MAI || MAI->isValidUnquotedName(Name)) {
57  OS << Name;
58  return;
59  }
60 
61  if (MAI && !MAI->supportsNameQuoting())
62  report_fatal_error("Symbol name with unsupported characters");
63 
64  OS << '"';
65  for (char C : Name) {
66  if (C == '\n')
67  OS << "\\n";
68  else if (C == '"')
69  OS << "\\\"";
70  else
71  OS << C;
72  }
73  OS << '"';
74 }
75 
76 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
77 void MCSymbol::dump() const { dbgs() << *this; }
78 #endif
virtual bool isValidUnquotedName(StringRef Name) const
Return true if the identifier Name does not need quotes to be syntactically correct.
Definition: MCAsmInfo.cpp:147
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:48
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:51
AlignOf - A templated class that contains an enum value representing the alignment of the template ar...
Definition: AlignOf.h:46
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
Context object for machine code objects.
Definition: MCContext.h:48
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:58
void setUndefined()
Mark the symbol as undefined.
Definition: MCSymbol.h:278
unsigned IsUsed
IsUsed - True if this symbol has been used.
Definition: MCSymbol.h:91
static MCSection * AbsolutePseudoSection
Definition: MCSymbol.h:62
unsigned SymbolContents
This is actually a Contents enumerator, but is unsigned to avoid sign extension and achieve better bi...
Definition: MCSymbol.h:110
void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:40
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
bool supportsNameQuoting() const
Definition: MCAsmInfo.h:467
LLVM Value Representation.
Definition: Value.h:69
The name for a symbol.
Definition: MCSymbol.h:147
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
const MCExpr * Value
If non-null, the value for a variable symbol.
Definition: MCSymbol.h:136
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
void dump() const
dump - Print the value to stderr.
Definition: MCSymbol.cpp:77