xref: /freebsd/contrib/llvm-project/llvm/lib/MC/MCSymbol.cpp (revision 9c77fb6aaa366cbabc80ee1b834bcfe4df135491)
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/Support/Compiler.h"
15 #include "llvm/Support/Debug.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <cassert>
19 #include <cstddef>
20 
21 using namespace llvm;
22 
23 // Only the address of this fragment is ever actually used.
24 static MCDataFragment SentinelFragment;
25 
26 // Sentinel value for the absolute pseudo fragment.
27 MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
28 
29 void *MCSymbol::operator new(size_t s, const MCSymbolTableEntry *Name,
30                              MCContext &Ctx) {
31   // We may need more space for a Name to account for alignment.  So allocate
32   // space for the storage type and not the name pointer.
33   size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
34 
35   // For safety, ensure that the alignment of a pointer is enough for an
36   // MCSymbol.  This also ensures we don't need padding between the name and
37   // symbol.
38   static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
39                 "Bad alignment of MCSymbol");
40   void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy));
41   NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
42   NameEntryStorageTy *End = Start + (Name ? 1 : 0);
43   return End;
44 }
45 
46 void MCSymbol::setVariableValue(const MCExpr *Value) {
47   assert(Value && "Invalid variable value!");
48   assert((SymbolContents == SymContentsUnset ||
49           SymbolContents == SymContentsVariable) &&
50          "Cannot give common/offset symbol a variable value");
51   this->Value = Value;
52   SymbolContents = SymContentsVariable;
53   setUndefined();
54 }
55 
56 void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
57   // The name for this MCSymbol is required to be a valid target name.  However,
58   // some targets support quoting names with funny characters.  If the name
59   // contains a funny character, then print it quoted.
60   StringRef Name = getName();
61   if (!MAI || MAI->isValidUnquotedName(Name)) {
62     OS << Name;
63     return;
64   }
65 
66   if (MAI && !MAI->supportsNameQuoting())
67     report_fatal_error("Symbol name with unsupported characters");
68 
69   OS << '"';
70   for (char C : Name) {
71     if (C == '\n')
72       OS << "\\n";
73     else if (C == '"')
74       OS << "\\\"";
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