1 //===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCSymbol.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Config/llvm-config.h"
13 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCContext.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCFragment.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.h"
26 // Only the address of this fragment is ever actually used.
27 static MCDummyFragment
SentinelFragment(nullptr);
29 // Sentinel value for the absolute pseudo fragment.
30 MCFragment
*MCSymbol::AbsolutePseudoFragment
= &SentinelFragment
;
32 void *MCSymbol::operator new(size_t s
, const StringMapEntry
<bool> *Name
,
34 // We may need more space for a Name to account for alignment. So allocate
35 // space for the storage type and not the name pointer.
36 size_t Size
= s
+ (Name
? sizeof(NameEntryStorageTy
) : 0);
38 // For safety, ensure that the alignment of a pointer is enough for an
39 // MCSymbol. This also ensures we don't need padding between the name and
41 static_assert((unsigned)alignof(MCSymbol
) <= alignof(NameEntryStorageTy
),
42 "Bad alignment of MCSymbol");
43 void *Storage
= Ctx
.allocate(Size
, alignof(NameEntryStorageTy
));
44 NameEntryStorageTy
*Start
= static_cast<NameEntryStorageTy
*>(Storage
);
45 NameEntryStorageTy
*End
= Start
+ (Name
? 1 : 0);
49 void MCSymbol::setVariableValue(const MCExpr
*Value
) {
50 assert(!IsUsed
&& "Cannot set a variable that has already been used.");
51 assert(Value
&& "Invalid variable value!");
52 assert((SymbolContents
== SymContentsUnset
||
53 SymbolContents
== SymContentsVariable
) &&
54 "Cannot give common/offset symbol a variable value");
56 SymbolContents
= SymContentsVariable
;
60 void 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
)) {
70 if (MAI
&& !MAI
->supportsNameQuoting())
71 report_fatal_error("Symbol name with unsupported characters");
85 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
86 LLVM_DUMP_METHOD
void MCSymbol::dump() const {