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/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/raw_ostream.h"
19 // Sentinel value for the absolute pseudo fragment.
20 MCFragment *MCSymbol::AbsolutePseudoFragment =
21 reinterpret_cast<MCFragment *>(4);
23 void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
25 // We may need more space for a Name to account for alignment. So allocate
26 // space for the storage type and not the name pointer.
27 size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
29 // For safety, ensure that the alignment of a pointer is enough for an
30 // MCSymbol. This also ensures we don't need padding between the name and
32 static_assert((unsigned)AlignOf<MCSymbol>::Alignment <=
33 AlignOf<NameEntryStorageTy>::Alignment,
34 "Bad alignment of MCSymbol");
35 void *Storage = Ctx.allocate(Size, alignOf<NameEntryStorageTy>());
36 NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
37 NameEntryStorageTy *End = Start + (Name ? 1 : 0);
41 void MCSymbol::setVariableValue(const MCExpr *Value) {
42 assert(!IsUsed && "Cannot set a variable that has already been used.");
43 assert(Value && "Invalid variable value!");
44 assert((SymbolContents == SymContentsUnset ||
45 SymbolContents == SymContentsVariable) &&
46 "Cannot give common/offset symbol a variable value");
48 SymbolContents = SymContentsVariable;
52 void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
53 // The name for this MCSymbol is required to be a valid target name. However,
54 // some targets support quoting names with funny characters. If the name
55 // contains a funny character, then print it quoted.
56 StringRef Name = getName();
57 if (!MAI || MAI->isValidUnquotedName(Name)) {
62 if (MAI && !MAI->supportsNameQuoting())
63 report_fatal_error("Symbol name with unsupported characters");
77 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
78 void MCSymbol::dump() const { dbgs() << *this; }