LLVM 22.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.
29
30// Sentinel value for the absolute pseudo fragment.
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, alignof(NameEntryStorageTy));
45 NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
46 NameEntryStorageTy *End = Start + (Name ? 1 : 0);
47 return End;
48}
49
51 assert(Value && "Invalid equated expression");
53 "Cannot equate a common symbol");
54 this->Value = Value;
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("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)
88 dbgs() << *this;
89}
90#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
static MCFragment SentinelFragment
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:64
bool supportsNameQuoting() const
Definition MCAsmInfo.h:572
virtual bool isValidUnquotedName(StringRef Name) const
Return true if the identifier Name does not need quotes to be syntactically correct.
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:59
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:50
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:87
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
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
StringMapEntry< MCSymbolTableValue > MCSymbolTableEntry
MCContext stores MCSymbolTableValue in a string map (see MCSymbol::operator new).