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