xref: /llvm-project/llvm/lib/MC/MCSymbol.cpp (revision 8c209aa8779de57771b64896f805e14cc0016dcd)
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"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/raw_ostream.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.
23 MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
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 
42 void MCSymbol::setVariableValue(const MCExpr *Value) {
43   assert(!IsUsed && "Cannot set a variable that has already been used.");
44   assert(Value && "Invalid variable value!");
45   assert((SymbolContents == SymContentsUnset ||
46           SymbolContents == SymContentsVariable) &&
47          "Cannot give common/offset symbol a variable value");
48   this->Value = Value;
49   SymbolContents = SymContentsVariable;
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.
57   StringRef Name = getName();
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 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
79 LLVM_DUMP_METHOD void MCSymbol::dump() const {
80   dbgs() << *this;
81 }
82 #endif
83