2
0

MCSymbol.cpp 2.7 KB

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