NonRelocatableStringpool.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===-- NonRelocatableStringpool.cpp --------------------------------------===//
  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/CodeGen/NonRelocatableStringpool.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. namespace llvm {
  11. DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) {
  12. if (S.empty() && !Strings.empty())
  13. return EmptyString;
  14. if (Translator)
  15. S = Translator(S);
  16. auto I = Strings.insert({S, DwarfStringPoolEntry()});
  17. auto &Entry = I.first->second;
  18. if (I.second || !Entry.isIndexed()) {
  19. Entry.Index = NumEntries++;
  20. Entry.Offset = CurrentEndOffset;
  21. Entry.Symbol = nullptr;
  22. CurrentEndOffset += S.size() + 1;
  23. }
  24. return DwarfStringPoolEntryRef(*I.first, true);
  25. }
  26. StringRef NonRelocatableStringpool::internString(StringRef S) {
  27. DwarfStringPoolEntry Entry{nullptr, 0, DwarfStringPoolEntry::NotIndexed};
  28. if (Translator)
  29. S = Translator(S);
  30. auto InsertResult = Strings.insert({S, Entry});
  31. return InsertResult.first->getKey();
  32. }
  33. std::vector<DwarfStringPoolEntryRef>
  34. NonRelocatableStringpool::getEntriesForEmission() const {
  35. std::vector<DwarfStringPoolEntryRef> Result;
  36. Result.reserve(Strings.size());
  37. for (const auto &E : Strings)
  38. if (E.getValue().isIndexed())
  39. Result.emplace_back(E, true);
  40. llvm::sort(Result, [](const DwarfStringPoolEntryRef A,
  41. const DwarfStringPoolEntryRef B) {
  42. return A.getIndex() < B.getIndex();
  43. });
  44. return Result;
  45. }
  46. } // namespace llvm