LLVM 24.0.0git
MCSymbol.cpp
Go to the documentation of this file.
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"
15#include "llvm/Support/Debug.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.
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, alignof(NameEntryStorageTy));
46 NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
47 NameEntryStorageTy *End = Start + (Name ? 1 : 0);
48 return End;
49}
50
52 assert(Value && "Invalid equated expression");
54 "Cannot equate a common symbol");
55 this->Value = Value;
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("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()) {
101 if (Fragment->isLinkerRelaxable())
102 return true;
103 }
104 return false;
105}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:690
static char SentinelFragment[sizeof(MCFragment)]
Definition MCSymbol.cpp:28
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:67
bool supportsNameQuoting() const
Definition MCAsmInfo.h:597
bool isValidUnquotedName(StringRef Name) const
Return true if the identifier Name does not need quotes to be syntactically correct.
Definition MCAsmInfo.cpp:93
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition MCSymbol.h:42
MCSymbol(const MCSymbolTableEntry *Name, bool isTemporary)
Definition MCSymbol.h:146
LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition MCSymbol.cpp:60
friend class MCExpr
Definition MCSymbol.h:134
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
friend class MCContext
Definition MCSymbol.h:135
LLVM_ABI void setVariableValue(const MCExpr *Value)
Definition MCSymbol.cpp:51
MCFragment * Fragment
If a symbol has a Fragment, the section is implied, so we only need one pointer.
Definition MCSymbol.h:65
unsigned kind
The symbol kind.
Definition MCSymbol.h:69
static LLVM_ABI MCFragment * AbsolutePseudoFragment
Definition MCSymbol.h:52
LLVM_ABI void dump() const
dump - Print the value to stderr.
Definition MCSymbol.cpp:88
union { const MCSymbolTableEntry *NameEntry; uint64_t AlignmentPadding; } NameEntryStorageTy
The name for a symbol.
Definition MCSymbol.h:141
const MCExpr * Value
If non-null, the value for a variable symbol.
Definition MCSymbol.h:130
MCFragment * getFragment() const
Definition MCSymbol.h:345
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool isRangeRelaxable(const MCSymbol *Begin, const MCSymbol *End)
Definition MCSymbol.cpp:95
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
StringMapEntry< MCSymbolTableValue > MCSymbolTableEntry
MCContext stores MCSymbolTableValue in a string map (see MCSymbol::operator new).
@ End
Run the machine verifier once, at the end of the pipeline.
Definition CodeGen.h:320