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.
28static MCFragment SentinelFragment;
29
30// Sentinel value for the absolute pseudo fragment.
31MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
32
33void *MCSymbol::operator new(size_t s, const MCSymbolTableEntry *Name,
34 MCContext &Ctx) {
35 // We may need more space for a Name to account for alignment. So allocate
36 // space for the storage type and not the name pointer.
37 size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
38
39 // For safety, ensure that the alignment of a pointer is enough for an
40 // MCSymbol. This also ensures we don't need padding between the name and
41 // symbol.
42 static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
43 "Bad alignment of MCSymbol");
44 void *Storage = Ctx.allocate(Size, Align: alignof(NameEntryStorageTy));
45 NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
46 NameEntryStorageTy *End = Start + (Name ? 1 : 0);
47 return End;
48}
49
50void MCSymbol::setVariableValue(const MCExpr *Value) {
51 assert(Value && "Invalid equated expression");
52 assert((kind == Kind::Regular || kind == Kind::Equated) &&
53 "Cannot equate a common symbol");
54 this->Value = Value;
55 kind = Kind::Equated;
56 Fragment = nullptr;
57}
58
59void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
60 // The name for this MCSymbol is required to be a valid target name. However,
61 // some targets support quoting names with funny characters. If the name
62 // contains a funny character, then print it quoted.
63 StringRef Name = getName();
64 if (!MAI || MAI->isValidUnquotedName(Name)) {
65 OS << Name;
66 return;
67 }
68
69 if (MAI && !MAI->supportsNameQuoting())
70 report_fatal_error(reason: "Symbol name with unsupported characters");
71
72 OS << '"';
73 for (char C : Name) {
74 if (C == '\n')
75 OS << "\\n";
76 else if (C == '"')
77 OS << "\\\"";
78 else if (C == '\\')
79 OS << "\\\\";
80 else
81 OS << C;
82 }
83 OS << '"';
84}
85
86#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
87LLVM_DUMP_METHOD void MCSymbol::dump() const { dbgs() << *this; }
88#endif
89
90// Determine whether the offset between two labels can change at link time.
91// Currently, this function is used only in DWARF info emission logic, where it
92// helps generate more optimal debug info when the offset between labels is
93// constant at link time.
94bool llvm::isRangeRelaxable(const MCSymbol *Begin, const MCSymbol *End) {
95 assert(Begin && "Range without a begin symbol?");
96 assert(End && "Range without an end symbol?");
97 for (const auto *Fragment = Begin->getFragment();
98 Fragment != End->getFragment(); Fragment = Fragment->getNext()) {
99 assert(Fragment);
100 if (Fragment->isLinkerRelaxable())
101 return true;
102 }
103 return false;
104}
105