Jonas Devlieghere | e993fb5 | 2018-03-01 10:05:54 +0000 | [diff] [blame] | 1 | //===- NonRelocatableStringpool.cpp - A simple stringpool ----------------===// |
| 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 "NonRelocatableStringpool.h" |
| 11 | |
| 12 | namespace llvm { |
| 13 | namespace dsymutil { |
| 14 | |
| 15 | DwarfStringPoolEntryRef NonRelocatableStringpool::getEntry(StringRef S) { |
| 16 | if (S.empty() && !Strings.empty()) |
| 17 | return EmptyString; |
| 18 | |
Jonas Devlieghere | 20767d1 | 2019-01-07 23:27:25 +0000 | [diff] [blame] | 19 | if (Translator) |
| 20 | S = Translator(S); |
Jonas Devlieghere | e993fb5 | 2018-03-01 10:05:54 +0000 | [diff] [blame] | 21 | auto I = Strings.insert({S, DwarfStringPoolEntry()}); |
| 22 | auto &Entry = I.first->second; |
Pavel Labath | 23332c5 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 23 | if (I.second || !Entry.isIndexed()) { |
Jonas Devlieghere | e993fb5 | 2018-03-01 10:05:54 +0000 | [diff] [blame] | 24 | Entry.Index = NumEntries++; |
| 25 | Entry.Offset = CurrentEndOffset; |
| 26 | Entry.Symbol = nullptr; |
| 27 | CurrentEndOffset += S.size() + 1; |
| 28 | } |
Pavel Labath | 23332c5 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 29 | return DwarfStringPoolEntryRef(*I.first, true); |
Jonas Devlieghere | e993fb5 | 2018-03-01 10:05:54 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | StringRef NonRelocatableStringpool::internString(StringRef S) { |
Pavel Labath | 23332c5 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 33 | DwarfStringPoolEntry Entry{nullptr, 0, DwarfStringPoolEntry::NotIndexed}; |
Jonas Devlieghere | 20767d1 | 2019-01-07 23:27:25 +0000 | [diff] [blame] | 34 | |
| 35 | if (Translator) |
| 36 | S = Translator(S); |
| 37 | |
Jonas Devlieghere | e993fb5 | 2018-03-01 10:05:54 +0000 | [diff] [blame] | 38 | auto InsertResult = Strings.insert({S, Entry}); |
| 39 | return InsertResult.first->getKey(); |
| 40 | } |
| 41 | |
| 42 | std::vector<DwarfStringPoolEntryRef> |
Pavel Labath | 23332c5 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 43 | NonRelocatableStringpool::getEntriesForEmission() const { |
Jonas Devlieghere | e993fb5 | 2018-03-01 10:05:54 +0000 | [diff] [blame] | 44 | std::vector<DwarfStringPoolEntryRef> Result; |
| 45 | Result.reserve(Strings.size()); |
| 46 | for (const auto &E : Strings) |
Pavel Labath | 23332c5 | 2018-08-07 09:54:52 +0000 | [diff] [blame] | 47 | if (E.getValue().isIndexed()) |
| 48 | Result.emplace_back(E, true); |
Fangrui Song | c466264 | 2018-09-30 22:31:29 +0000 | [diff] [blame] | 49 | llvm::sort(Result, [](const DwarfStringPoolEntryRef A, |
| 50 | const DwarfStringPoolEntryRef B) { |
| 51 | return A.getIndex() < B.getIndex(); |
| 52 | }); |
Jonas Devlieghere | e993fb5 | 2018-03-01 10:05:54 +0000 | [diff] [blame] | 53 | return Result; |
| 54 | } |
| 55 | |
| 56 | } // namespace dsymutil |
| 57 | } // namespace llvm |