MCSymbol.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/MCExpr.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. using namespace llvm;
  22. // Only the address of this fragment is ever actually used.
  23. static MCDummyFragment SentinelFragment(nullptr);
  24. // Sentinel value for the absolute pseudo fragment.
  25. MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
  26. void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
  27. MCContext &Ctx) {
  28. // We may need more space for a Name to account for alignment. So allocate
  29. // space for the storage type and not the name pointer.
  30. size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
  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) <= alignof(NameEntryStorageTy),
  35. "Bad alignment of MCSymbol");
  36. void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy));
  37. NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
  38. NameEntryStorageTy *End = Start + (Name ? 1 : 0);
  39. return End;
  40. }
  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");
  47. this->Value = Value;
  48. SymbolContents = SymContentsVariable;
  49. setUndefined();
  50. }
  51. void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
  52. // The name for this MCSymbol is required to be a valid target name. However,
  53. // some targets support quoting names with funny characters. If the name
  54. // contains a funny character, then print it quoted.
  55. StringRef Name = getName();
  56. if (!MAI || MAI->isValidUnquotedName(Name)) {
  57. OS << Name;
  58. return;
  59. }
  60. if (MAI && !MAI->supportsNameQuoting())
  61. report_fatal_error("Symbol name with unsupported characters");
  62. OS << '"';
  63. for (char C : Name) {
  64. if (C == '\n')
  65. OS << "\\n";
  66. else if (C == '"')
  67. OS << "\\\"";
  68. else
  69. OS << C;
  70. }
  71. OS << '"';
  72. }
  73. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  74. LLVM_DUMP_METHOD void MCSymbol::dump() const {
  75. dbgs() << *this;
  76. }
  77. #endif