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/MCExpr.h"
15 #include "llvm/MC/MCFragment.h"
16 #include "llvm/Support/Compiler.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/raw_ostream.h"
25 // Only the address of this fragment is ever actually used.
26 static MCDummyFragment
SentinelFragment(nullptr);
28 // Sentinel value for the absolute pseudo fragment.
29 MCFragment
*MCSymbol::AbsolutePseudoFragment
= &SentinelFragment
;
31 void *MCSymbol::operator new(size_t s
, const StringMapEntry
<bool> *Name
,
33 // We may need more space for a Name to account for alignment. So allocate
34 // space for the storage type and not the name pointer.
35 size_t Size
= s
+ (Name
? sizeof(NameEntryStorageTy
) : 0);
37 // For safety, ensure that the alignment of a pointer is enough for an
38 // MCSymbol. This also ensures we don't need padding between the name and
40 static_assert((unsigned)alignof(MCSymbol
) <= alignof(NameEntryStorageTy
),
41 "Bad alignment of MCSymbol");
42 void *Storage
= Ctx
.allocate(Size
, alignof(NameEntryStorageTy
));
43 NameEntryStorageTy
*Start
= static_cast<NameEntryStorageTy
*>(Storage
);
44 NameEntryStorageTy
*End
= Start
+ (Name
? 1 : 0);
48 void MCSymbol::setVariableValue(const MCExpr
*Value
) {
49 assert(!IsUsed
&& "Cannot set a variable that has already been used.");
50 assert(Value
&& "Invalid variable value!");
51 assert((SymbolContents
== SymContentsUnset
||
52 SymbolContents
== SymContentsVariable
) &&
53 "Cannot give common/offset symbol a variable value");
55 SymbolContents
= SymContentsVariable
;
59 void 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
)) {
69 if (MAI
&& !MAI
->supportsNameQuoting())
70 report_fatal_error("Symbol name with unsupported characters");
84 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
85 LLVM_DUMP_METHOD
void MCSymbol::dump() const {