• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.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.
30 MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
31 
operator new(size_t s,const StringMapEntry<bool> * Name,MCContext & Ctx)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 
setVariableValue(const MCExpr * Value)49 void MCSymbol::setVariableValue(const MCExpr *Value) {
50   assert(!IsUsed && "Cannot set a variable that has already been used.");
51   assert(Value && "Invalid variable value!");
52   assert((SymbolContents == SymContentsUnset ||
53           SymbolContents == SymContentsVariable) &&
54          "Cannot give common/offset symbol a variable value");
55   this->Value = Value;
56   SymbolContents = SymContentsVariable;
57   setUndefined();
58 }
59 
print(raw_ostream & OS,const MCAsmInfo * MAI) const60 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.
64   StringRef Name = getName();
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)
dump() const86 LLVM_DUMP_METHOD void MCSymbol::dump() const {
87   dbgs() << *this;
88 }
89 #endif
90