1//===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
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"
17using namespace llvm;
18
19// Only the address of this fragment is ever actually used.
20static MCDummyFragment SentinelFragment(nullptr);
21
22// Sentinel value for the absolute pseudo fragment.
23MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
24
25void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
26                             MCContext &Ctx) {
27  // We may need more space for a Name to account for alignment.  So allocate
28  // space for the storage type and not the name pointer.
29  size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
30
31  // For safety, ensure that the alignment of a pointer is enough for an
32  // MCSymbol.  This also ensures we don't need padding between the name and
33  // symbol.
34  static_assert((unsigned)AlignOf<MCSymbol>::Alignment <=
35                AlignOf<NameEntryStorageTy>::Alignment,
36                "Bad alignment of MCSymbol");
37  void *Storage = Ctx.allocate(Size, alignOf<NameEntryStorageTy>());
38  NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
39  NameEntryStorageTy *End = Start + (Name ? 1 : 0);
40  return End;
41}
42
43void MCSymbol::setVariableValue(const MCExpr *Value) {
44  assert(!IsUsed && "Cannot set a variable that has already been used.");
45  assert(Value && "Invalid variable value!");
46  assert((SymbolContents == SymContentsUnset ||
47          SymbolContents == SymContentsVariable) &&
48         "Cannot give common/offset symbol a variable value");
49  this->Value = Value;
50  SymbolContents = SymContentsVariable;
51  setUndefined();
52}
53
54void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
55  // The name for this MCSymbol is required to be a valid target name.  However,
56  // some targets support quoting names with funny characters.  If the name
57  // contains a funny character, then print it quoted.
58  StringRef Name = getName();
59  if (!MAI || MAI->isValidUnquotedName(Name)) {
60    OS << Name;
61    return;
62  }
63
64  if (MAI && !MAI->supportsNameQuoting())
65    report_fatal_error("Symbol name with unsupported characters");
66
67  OS << '"';
68  for (char C : Name) {
69    if (C == '\n')
70      OS << "\\n";
71    else if (C == '"')
72      OS << "\\\"";
73    else
74      OS << C;
75  }
76  OS << '"';
77}
78
79#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
80void MCSymbol::dump() const { dbgs() << *this; }
81#endif
82