1 //===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
24 // Only the address of this fragment is ever actually used.
25 static MCDummyFragment
SentinelFragment(nullptr);
27 // Sentinel value for the absolute pseudo fragment.
28 MCFragment
*MCSymbol::AbsolutePseudoFragment
= &SentinelFragment
;
30 void *MCSymbol::operator new(size_t s
, const StringMapEntry
<bool> *Name
,
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);
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
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);
47 void MCSymbol::setVariableValue(const MCExpr
*Value
) {
48 assert(!IsUsed
&& "Cannot set a variable that has already been used.");
49 assert(Value
&& "Invalid variable value!");
50 assert((SymbolContents
== SymContentsUnset
||
51 SymbolContents
== SymContentsVariable
) &&
52 "Cannot give common/offset symbol a variable value");
54 SymbolContents
= SymContentsVariable
;
58 void 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.
62 StringRef Name
= getName();
63 if (!MAI
|| MAI
->isValidUnquotedName(Name
)) {
68 if (MAI
&& !MAI
->supportsNameQuoting())
69 report_fatal_error("Symbol name with unsupported characters");
83 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
84 LLVM_DUMP_METHOD
void MCSymbol::dump() const {