xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCSymbol.cpp (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
1 //===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/MC/MCSymbol.h"
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/Config/llvm-config.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCFragment.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <cassert>
20 #include <cstddef>
21 
22 using namespace llvm;
23 
24 // Only the address of this fragment is ever actually used.
25 static MCDummyFragment SentinelFragment(nullptr);
26 
27 // Sentinel value for the absolute pseudo fragment.
28 MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
29 
30 void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
31                              MCContext &Ctx) {
32   // We may need more space for a Name to account for alignment.  So allocate
33   // space for the storage type and not the name pointer.
34   size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
35 
36   // For safety, ensure that the alignment of a pointer is enough for an
37   // MCSymbol.  This also ensures we don't need padding between the name and
38   // symbol.
39   static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
40                 "Bad alignment of MCSymbol");
41   void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy));
42   NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
43   NameEntryStorageTy *End = Start + (Name ? 1 : 0);
44   return End;
45 }
46 
47 void MCSymbol::setVariableValue(const MCExpr *Value) {
48   assert(!IsUsed && "Cannot set a variable that has already been used.");
49   assert(Value && "Invalid variable value!");
50   assert((SymbolContents == SymContentsUnset ||
51           SymbolContents == SymContentsVariable) &&
52          "Cannot give common/offset symbol a variable value");
53   this->Value = Value;
54   SymbolContents = SymContentsVariable;
55   setUndefined();
56 }
57 
58 void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
59   // The name for this MCSymbol is required to be a valid target name.  However,
60   // some targets support quoting names with funny characters.  If the name
61   // contains a funny character, then print it quoted.
62   StringRef Name = getName();
63   if (!MAI || MAI->isValidUnquotedName(Name)) {
64     OS << Name;
65     return;
66   }
67 
68   if (MAI && !MAI->supportsNameQuoting())
69     report_fatal_error("Symbol name with unsupported characters");
70 
71   OS << '"';
72   for (char C : Name) {
73     if (C == '\n')
74       OS << "\\n";
75     else if (C == '"')
76       OS << "\\\"";
77     else
78       OS << C;
79   }
80   OS << '"';
81 }
82 
83 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
84 LLVM_DUMP_METHOD void MCSymbol::dump() const {
85   dbgs() << *this;
86 }
87 #endif
88