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"
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"
19#include <cassert>
20#include <cstddef>
21
22using namespace llvm;
23
24// Only the address of this fragment is ever actually used.
25static MCDummyFragment SentinelFragment;
26
27// Sentinel value for the absolute pseudo fragment.
28MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
29
30void *MCSymbol::operator new(size_t s, const MCSymbolTableEntry *Name,
31 MCContext &Ctx) {
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);
35
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
38 // symbol.
39 static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
40 "Bad alignment of MCSymbol");
41 void *Storage = Ctx.allocate(Size, Align: alignof(NameEntryStorageTy));
42 NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
43 NameEntryStorageTy *End = Start + (Name ? 1 : 0);
44 return End;
45}
46
47void 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");
53 this->Value = Value;
54 SymbolContents = SymContentsVariable;
55 setUndefined();
56}
57
58void 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)) {
64 OS << Name;
65 return;
66 }
67
68 if (MAI && !MAI->supportsNameQuoting())
69 report_fatal_error(reason: "Symbol name with unsupported characters");
70
71 OS << '"';
72 for (char C : Name) {
73 if (C == '\n')
74 OS << "\\n";
75 else if (C == '"')
76 OS << "\\\"";
77 else
78 OS << C;
79 }
80 OS << '"';
81}
82
83#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
84LLVM_DUMP_METHOD void MCSymbol::dump() const {
85 dbgs() << *this;
86}
87#endif
88