LLVM  4.0.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 // Only the address of this fragment is ever actually used.
20 static MCDummyFragment SentinelFragment(nullptr);
21 
22 // Sentinel value for the absolute pseudo fragment.
24 
25 void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
26  MCContext &Ctx) {
27  // We may need more space for a Name to account for alignment. So allocate
28  // space for the storage type and not the name pointer.
29  size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
30 
31  // For safety, ensure that the alignment of a pointer is enough for an
32  // MCSymbol. This also ensures we don't need padding between the name and
33  // symbol.
34  static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
35  "Bad alignment of MCSymbol");
36  void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy));
37  NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
38  NameEntryStorageTy *End = Start + (Name ? 1 : 0);
39  return End;
40 }
41 
43  assert(!IsUsed && "Cannot set a variable that has already been used.");
44  assert(Value && "Invalid variable value!");
47  "Cannot give common/offset symbol a variable value");
48  this->Value = Value;
50  setUndefined();
51 }
52 
53 void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
54  // The name for this MCSymbol is required to be a valid target name. However,
55  // some targets support quoting names with funny characters. If the name
56  // contains a funny character, then print it quoted.
58  if (!MAI || MAI->isValidUnquotedName(Name)) {
59  OS << Name;
60  return;
61  }
62 
63  if (MAI && !MAI->supportsNameQuoting())
64  report_fatal_error("Symbol name with unsupported characters");
65 
66  OS << '"';
67  for (char C : Name) {
68  if (C == '\n')
69  OS << "\\n";
70  else if (C == '"')
71  OS << "\\\"";
72  else
73  OS << C;
74  }
75  OS << '"';
76 }
77 
78 LLVM_DUMP_METHOD void MCSymbol::dump() const { dbgs() << *this; }
virtual bool isValidUnquotedName(StringRef Name) const
Return true if the identifier Name does not need quotes to be syntactically correct.
Definition: MCAsmInfo.cpp:148
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:53
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:450
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
void dump() const
dump - Print the value to stderr.
Definition: MCSymbol.cpp:78
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
Context object for machine code objects.
Definition: MCContext.h:51
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:57
static const unsigned End
static MCFragment * AbsolutePseudoFragment
Definition: MCSymbol.h:60
void setUndefined()
Mark the symbol as undefined.
Definition: MCSymbol.h:276
unsigned IsUsed
IsUsed - True if this symbol has been used.
Definition: MCSymbol.h:88
unsigned SymbolContents
This is actually a Contents enumerator, but is unsigned to avoid sign extension and achieve better bi...
Definition: MCSymbol.h:107
void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:42
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:199
bool supportsNameQuoting() const
Definition: MCAsmInfo.h:493
static MCDummyFragment SentinelFragment(nullptr)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:71
The name for a symbol.
Definition: MCSymbol.h:144
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
const MCExpr * Value
If non-null, the value for a variable symbol.
Definition: MCSymbol.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47