MCSymbol.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #include "llvm/MC/MCSymbol.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/Config/llvm-config.h"
  11. #include "llvm/MC/MCAsmInfo.h"
  12. #include "llvm/MC/MCContext.h"
  13. #include "llvm/MC/MCFragment.h"
  14. #include "llvm/Support/Compiler.h"
  15. #include "llvm/Support/Debug.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. #include <cassert>
  19. #include <cstddef>
  20. using namespace llvm;
  21. // Only the address of this fragment is ever actually used.
  22. static MCDummyFragment SentinelFragment(nullptr);
  23. // Sentinel value for the absolute pseudo fragment.
  24. MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
  25. void *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. // For safety, ensure that the alignment of a pointer is enough for an
  31. // MCSymbol. This also ensures we don't need padding between the name and
  32. // symbol.
  33. static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
  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);
  38. return End;
  39. }
  40. void MCSymbol::setVariableValue(const MCExpr *Value) {
  41. assert(!IsUsed && "Cannot set a variable that has already been used.");
  42. assert(Value && "Invalid variable value!");
  43. assert((SymbolContents == SymContentsUnset ||
  44. SymbolContents == SymContentsVariable) &&
  45. "Cannot give common/offset symbol a variable value");
  46. this->Value = Value;
  47. SymbolContents = SymContentsVariable;
  48. setUndefined();
  49. }
  50. void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
  51. // The name for this MCSymbol is required to be a valid target name. However,
  52. // some targets support quoting names with funny characters. If the name
  53. // contains a funny character, then print it quoted.
  54. StringRef Name = getName();
  55. if (!MAI || MAI->isValidUnquotedName(Name)) {
  56. OS << Name;
  57. return;
  58. }
  59. if (MAI && !MAI->supportsNameQuoting())
  60. report_fatal_error("Symbol name with unsupported characters");
  61. OS << '"';
  62. for (char C : Name) {
  63. if (C == '\n')
  64. OS << "\\n";
  65. else if (C == '"')
  66. OS << "\\\"";
  67. else
  68. OS << C;
  69. }
  70. OS << '"';
  71. }
  72. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  73. LLVM_DUMP_METHOD void MCSymbol::dump() const {
  74. dbgs() << *this;
  75. }
  76. #endif