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
21using namespace llvm;
22
23// There are numerous MCSymbol objects, so keeping sizeof(MCSymbol) small is
24// crucial for minimizing peak memory usage.
25static_assert(sizeof(MCSymbol) <= 24, "Keep the base symbol small");
26
27// Only the address of this fragment is ever actually used.
28alignas(MCFragment) static char SentinelFragment[sizeof(MCFragment)];
29
30// Sentinel value for the absolute pseudo fragment.
31MCFragment *MCSymbol::AbsolutePseudoFragment =
32 reinterpret_cast<MCFragment *>(SentinelFragment);
33
34void *MCSymbol::operator new(size_t s, const MCSymbolTableEntry *Name,
35 MCContext &Ctx) {
36 // We may need more space for a Name to account for alignment. So allocate
37 // space for the storage type and not the name pointer.
38 size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
39
40 // For safety, ensure that the alignment of a pointer is enough for an
41 // MCSymbol. This also ensures we don't need padding between the name and
42 // symbol.
43 static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
44 "Bad alignment of MCSymbol");
45 void *Storage = Ctx.allocate(Size, Align: alignof(NameEntryStorageTy));
46 NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
47 NameEntryStorageTy *End = Start + (Name ? 1 : 0);
48 return End;
49}
50
51void MCSymbol::setVariableValue(const MCExpr *Value) {
52 assert(Value && "Invalid equated expression");
53 assert((kind == Kind::Regular || kind == Kind::Equated) &&
54 "Cannot equate a common symbol");
55 this->Value = Value;
56 kind = Kind::Equated;
57 Fragment = nullptr;
58}
59
60void 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(reason: "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 if (C == '\\')
80 OS << "\\\\";
81 else
82 OS << C;
83 }
84 OS << '"';
85}
86
87#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
88LLVM_DUMP_METHOD void MCSymbol::dump() const { dbgs() << *this; }
89#endif
90
91// Determine whether the offset between two labels can change at link time.
92// Currently, this function is used only in DWARF info emission logic, where it
93// helps generate more optimal debug info when the offset between labels is
94// constant at link time.
95bool llvm::isRangeRelaxable(const MCSymbol *Begin, const MCSymbol *End) {
96 assert(Begin && "Range without a begin symbol?");
97 assert(End && "Range without an end symbol?");
98 for (const auto *Fragment = Begin->getFragment();
99 Fragment != End->getFragment(); Fragment = Fragment->getNext()) {
100 assert(Fragment);
101 if (Fragment->isLinkerRelaxable())
102 return true;
103 }
104 return false;
105}
106