LLVM  8.0.1
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/ADT/StringRef.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCFragment.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Debug.h"
21 #include <cassert>
22 #include <cstddef>
23 
24 using namespace llvm;
25 
26 // Only the address of this fragment is ever actually used.
27 static MCDummyFragment SentinelFragment(nullptr);
28 
29 // Sentinel value for the absolute pseudo fragment.
31 
32 void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
33  MCContext &Ctx) {
34  // We may need more space for a Name to account for alignment. So allocate
35  // space for the storage type and not the name pointer.
36  size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
37 
38  // For safety, ensure that the alignment of a pointer is enough for an
39  // MCSymbol. This also ensures we don't need padding between the name and
40  // symbol.
41  static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
42  "Bad alignment of MCSymbol");
43  void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy));
44  NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
45  NameEntryStorageTy *End = Start + (Name ? 1 : 0);
46  return End;
47 }
48 
50  assert(!IsUsed && "Cannot set a variable that has already been used.");
51  assert(Value && "Invalid variable value!");
54  "Cannot give common/offset symbol a variable value");
55  this->Value = Value;
57  setUndefined();
58 }
59 
60 void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
61  // The name for this MCSymbol is required to be a valid target name. However,
62  // some targets support quoting names with funny characters. If the name
63  // contains a funny character, then print it quoted.
65  if (!MAI || MAI->isValidUnquotedName(Name)) {
66  OS << Name;
67  return;
68  }
69 
70  if (MAI && !MAI->supportsNameQuoting())
71  report_fatal_error("Symbol name with unsupported characters");
72 
73  OS << '"';
74  for (char C : Name) {
75  if (C == '\n')
76  OS << "\\n";
77  else if (C == '"')
78  OS << "\\\"";
79  else
80  OS << C;
81  }
82  OS << '"';
83 }
84 
85 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
87  dbgs() << *this;
88 }
89 #endif
uint64_t CallInst * C
union { const StringMapEntry< bool > *NameEntry NameEntryStorageTy
The name for a symbol.
Definition: MCSymbol.h:149
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:140
This class represents lattice values for constants.
Definition: AllocatorList.h:24
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:126
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
bool supportsNameQuoting() const
Definition: MCAsmInfo.h:511
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
#define LLVM_DUMP_METHOD
Definition: Compiler.h:74
Context object for machine code objects.
Definition: MCContext.h:63
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
static MCFragment * AbsolutePseudoFragment
Definition: MCSymbol.h:64
virtual bool isValidUnquotedName(StringRef Name) const
Return true if the identifier Name does not need quotes to be syntactically correct.
Definition: MCAsmInfo.cpp:105
void setUndefined()
Mark the symbol as undefined.
Definition: MCSymbol.h:279
unsigned IsUsed
IsUsed - True if this symbol has been used.
Definition: MCSymbol.h:92
unsigned SymbolContents
This is actually a Contents enumerator, but is unsigned to avoid sign extension and achieve better bi...
Definition: MCSymbol.h:111
void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:49
void dump() const
dump - Print the value to stderr.
Definition: MCSymbol.cpp:86
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:133
uint32_t Size
Definition: Profile.cpp:47
static MCDummyFragment SentinelFragment(nullptr)
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:203
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:46
const MCExpr * Value
If non-null, the value for a variable symbol.
Definition: MCSymbol.h:137
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:60