LLVM 20.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"
14#include "llvm/MC/MCFragment.h"
16#include "llvm/Support/Debug.h"
19#include <cassert>
20#include <cstddef>
21
22using namespace llvm;
23
24// Only the address of this fragment is ever actually used.
26
27// Sentinel value for the absolute pseudo fragment.
29
30void *MCSymbol::operator new(size_t s, const MCSymbolTableEntry *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
48 assert(!IsUsed && "Cannot set a variable that has already been used.");
49 assert(Value && "Invalid variable value!");
52 "Cannot give common/offset symbol a variable value");
53 this->Value = Value;
56}
57
58void 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.
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)
85 dbgs() << *this;
86}
87#endif
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:533
std::string Name
uint64_t Size
bool End
Definition: ELF_riscv.cpp:480
static MCDummyFragment SentinelFragment
Definition: MCSymbol.cpp:25
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
bool supportsNameQuoting() const
Definition: MCAsmInfo.h:661
virtual bool isValidUnquotedName(StringRef Name) const
Return true if the identifier Name does not need quotes to be syntactically correct.
Definition: MCAsmInfo.cpp:108
Context object for machine code objects.
Definition: MCContext.h:83
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:34
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:58
unsigned IsUsed
IsUsed - True if this symbol has been used.
Definition: MCSymbol.h:94
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:205
union { const MCSymbolTableEntry *NameEntry NameEntryStorageTy
The name for a symbol.
Definition: MCSymbol.h:160
void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:47
unsigned SymbolContents
This is actually a Contents enumerator, but is unsigned to avoid sign extension and achieve better bi...
Definition: MCSymbol.h:117
@ SymContentsUnset
Definition: MCSymbol.h:58
@ SymContentsVariable
Definition: MCSymbol.h:60
void setUndefined()
Mark the symbol as undefined.
Definition: MCSymbol.h:281
static MCFragment * AbsolutePseudoFragment
Definition: MCSymbol.h:66
void dump() const
dump - Print the value to stderr.
Definition: MCSymbol.cpp:84
const MCExpr * Value
If non-null, the value for a variable symbol.
Definition: MCSymbol.h:148
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
LLVM Value Representation.
Definition: Value.h:74
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167