| 1 | //===----------------------------------------------------------------------===// |
| 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 | |
| 9 | #include "TypePool.h" |
| 10 | #include "llvm/ADT/SmallVector.h" |
| 11 | #include <cstring> |
| 12 | |
| 13 | using namespace llvm; |
| 14 | using namespace dwarf_linker; |
| 15 | using namespace dwarf_linker::parallel; |
| 16 | |
| 17 | void TypeKey::appendRef(const TypeEntry *Entry) { |
| 18 | assert(Entry && "null reference" ); |
| 19 | Bytes.push_back(Elt: RefMarker); |
| 20 | const char *EntryBytes = reinterpret_cast<const char *>(&Entry); |
| 21 | Bytes.append(in_start: EntryBytes, in_end: EntryBytes + sizeof(Entry)); |
| 22 | } |
| 23 | |
| 24 | TypeKey::Part TypeKey::takePart(StringRef &Key) { |
| 25 | assert(!Key.empty() && "no part to take" ); |
| 26 | if (Key.front() != RefMarker) { |
| 27 | StringRef Text = Key.take_front(N: Key.find(C: RefMarker)); |
| 28 | Key = Key.drop_front(N: Text.size()); |
| 29 | return Text; |
| 30 | } |
| 31 | assert(Key.size() >= RefSize && "truncated reference" ); |
| 32 | const TypeEntry *Ref; |
| 33 | std::memcpy(dest: &Ref, src: Key.data() + 1, n: sizeof(Ref)); |
| 34 | Key = Key.drop_front(N: RefSize); |
| 35 | return Ref; |
| 36 | } |
| 37 | |
| 38 | namespace { |
| 39 | /// Walks a type key as if every reference were replaced by its key. |
| 40 | class ExpandedKeyCursor { |
| 41 | public: |
| 42 | ExpandedKeyCursor(StringRef Key) { Pending.push_back(Elt: Key); } |
| 43 | |
| 44 | bool atEnd() { |
| 45 | if (const auto *Text = std::get_if<StringRef>(ptr: &Head); |
| 46 | !Text || !Text->empty()) |
| 47 | return false; |
| 48 | while (!Pending.empty() && Pending.back().empty()) |
| 49 | Pending.pop_back(); |
| 50 | if (Pending.empty()) |
| 51 | return true; |
| 52 | Head = TypeKey::takePart(Key&: Pending.back()); |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | /// \pre !atEnd() |
| 57 | const TypeKey::Part &peek() const { return Head; } |
| 58 | |
| 59 | /// \pre peek() is text of at least \p N bytes. |
| 60 | void consumeText(size_t N) { |
| 61 | StringRef &Text = std::get<StringRef>(v&: Head); |
| 62 | Text = Text.drop_front(N); |
| 63 | } |
| 64 | |
| 65 | /// \pre peek() is a reference. |
| 66 | void skipRef() { Head = {}; } |
| 67 | |
| 68 | /// Replace the reference at the cursor by the key it refers to. |
| 69 | /// \pre peek() is a reference. |
| 70 | void enterRef() { |
| 71 | Pending.push_back(Elt: std::get<const TypeEntry *>(v&: Head)->getKey()); |
| 72 | Head = {}; |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | /// The undecoded rest of each key being expanded, innermost last. |
| 77 | SmallVector<StringRef, 8> Pending; |
| 78 | /// Decoded once, so consuming it piecemeal does not rescan it. Empty text |
| 79 | /// until the next part is decoded. |
| 80 | TypeKey::Part Head; |
| 81 | }; |
| 82 | } // namespace |
| 83 | |
| 84 | /// Order keys by their text runs and references, recursively. Unlike |
| 85 | /// expansion this is a total order on distinct keys. |
| 86 | static int compareTypeKeyStructure(StringRef LHS, StringRef RHS) { |
| 87 | while (!LHS.empty() && !RHS.empty()) { |
| 88 | TypeKey::Part L = TypeKey::takePart(Key&: LHS); |
| 89 | TypeKey::Part R = TypeKey::takePart(Key&: RHS); |
| 90 | if (L.index() != R.index()) |
| 91 | return std::holds_alternative<const TypeEntry *>(v: L) ? -1 : 1; |
| 92 | if (L == R) |
| 93 | continue; |
| 94 | if (const auto *LText = std::get_if<StringRef>(ptr: &L)) |
| 95 | return LText->compare(RHS: std::get<StringRef>(v&: R)); |
| 96 | if (int Cmp = |
| 97 | compareTypeKeyStructure(LHS: std::get<const TypeEntry *>(v&: L)->getKey(), |
| 98 | RHS: std::get<const TypeEntry *>(v&: R)->getKey())) |
| 99 | return Cmp; |
| 100 | } |
| 101 | return LHS.empty() == RHS.empty() ? 0 : (LHS.empty() ? -1 : 1); |
| 102 | } |
| 103 | |
| 104 | int TypeKey::compare(StringRef LHS, StringRef RHS) { |
| 105 | ExpandedKeyCursor L(LHS), R(RHS); |
| 106 | while (true) { |
| 107 | bool LEnd = L.atEnd(); |
| 108 | bool REnd = R.atEnd(); |
| 109 | // Distinct keys expand equally if a name contains key syntax. Break the |
| 110 | // tie without depending on pointer values. |
| 111 | if (LEnd && REnd) |
| 112 | return LHS == RHS ? 0 : compareTypeKeyStructure(LHS, RHS); |
| 113 | if (LEnd || REnd) |
| 114 | return LEnd ? -1 : 1; |
| 115 | |
| 116 | const TypeKey::Part &LPart = L.peek(); |
| 117 | const TypeKey::Part &RPart = R.peek(); |
| 118 | bool LRef = std::holds_alternative<const TypeEntry *>(v: LPart); |
| 119 | bool RRef = std::holds_alternative<const TypeEntry *>(v: RPart); |
| 120 | // Equal references refer to equal keys, so they need no expanding. |
| 121 | if (LRef && LPart == RPart) { |
| 122 | L.skipRef(); |
| 123 | R.skipRef(); |
| 124 | continue; |
| 125 | } |
| 126 | if (LRef || RRef) { |
| 127 | if (LRef) |
| 128 | L.enterRef(); |
| 129 | if (RRef) |
| 130 | R.enterRef(); |
| 131 | continue; |
| 132 | } |
| 133 | |
| 134 | StringRef LText = std::get<StringRef>(v: LPart); |
| 135 | StringRef RText = std::get<StringRef>(v: RPart); |
| 136 | size_t N = std::min(a: LText.size(), b: RText.size()); |
| 137 | if (int Cmp = LText.take_front(N).compare(RHS: RText.take_front(N))) |
| 138 | return Cmp; |
| 139 | L.consumeText(N); |
| 140 | R.consumeText(N); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | std::string TypeKey::expand(StringRef Key) { |
| 145 | std::string Result; |
| 146 | ExpandedKeyCursor Cursor(Key); |
| 147 | while (!Cursor.atEnd()) { |
| 148 | const auto *Text = std::get_if<StringRef>(ptr: &Cursor.peek()); |
| 149 | if (!Text) { |
| 150 | Cursor.enterRef(); |
| 151 | continue; |
| 152 | } |
| 153 | Result += *Text; |
| 154 | Cursor.consumeText(N: Text->size()); |
| 155 | } |
| 156 | return Result; |
| 157 | } |
| 158 | |