Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 1 | //===- StringTableBuilder.cpp - String table building utility -------------===// |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 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 | |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/CachedHashString.h" |
Justin Lebar | ee34a73 | 2016-10-17 22:24:36 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/StringRef.h" |
| 13 | #include "llvm/MC/StringTableBuilder.h" |
Hans Wennborg | f26bfc1 | 2014-09-29 22:43:20 +0000 | [diff] [blame] | 14 | #include "llvm/Support/COFF.h" |
| 15 | #include "llvm/Support/Endian.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 16 | #include "llvm/Support/MathExtras.h" |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 18 | #include <cassert> |
| 19 | #include <cstddef> |
| 20 | #include <cstdint> |
| 21 | #include <cstring> |
| 22 | #include <utility> |
Zachary Turner | c55a504 | 2015-10-22 16:42:31 +0000 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Eugene Zelenko | d3a6c89 | 2017-02-11 00:27:28 +0000 | [diff] [blame] | 27 | StringTableBuilder::~StringTableBuilder() = default; |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 28 | |
| 29 | void StringTableBuilder::initSize() { |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 30 | // Account for leading bytes in table so that offsets returned from add are |
| 31 | // correct. |
| 32 | switch (K) { |
| 33 | case RAW: |
| 34 | Size = 0; |
| 35 | break; |
| 36 | case MachO: |
| 37 | case ELF: |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 38 | // Start the table with a NUL byte. |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 39 | Size = 1; |
| 40 | break; |
| 41 | case WinCOFF: |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 42 | // Make room to write the table size later. |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 43 | Size = 4; |
| 44 | break; |
| 45 | } |
| 46 | } |
Rafael Espindola | 21956e4 | 2015-10-23 21:48:05 +0000 | [diff] [blame] | 47 | |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 48 | StringTableBuilder::StringTableBuilder(Kind K, unsigned Alignment) |
| 49 | : K(K), Alignment(Alignment) { |
| 50 | initSize(); |
| 51 | } |
| 52 | |
| 53 | void StringTableBuilder::write(raw_ostream &OS) const { |
| 54 | assert(isFinalized()); |
| 55 | SmallString<0> Data; |
| 56 | Data.resize(getSize()); |
Peter Collingbourne | 28a2ede | 2017-04-06 00:10:17 +0000 | [diff] [blame] | 57 | write((uint8_t *)Data.data()); |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 58 | OS << Data; |
| 59 | } |
| 60 | |
Eugene Zelenko | 7975b99 | 2017-04-26 22:31:39 +0000 | [diff] [blame] | 61 | using StringPair = std::pair<CachedHashStringRef, size_t>; |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 62 | |
| 63 | void StringTableBuilder::write(uint8_t *Buf) const { |
| 64 | assert(isFinalized()); |
| 65 | for (const StringPair &P : StringIndexMap) { |
| 66 | StringRef Data = P.first.val(); |
Rafael Espindola | 24db10d | 2016-10-05 16:33:03 +0000 | [diff] [blame] | 67 | if (!Data.empty()) |
| 68 | memcpy(Buf + P.second, Data.data(), Data.size()); |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 69 | } |
| 70 | if (K != WinCOFF) |
| 71 | return; |
| 72 | support::endian::write32le(Buf, Size); |
| 73 | } |
Rui Ueyama | df94852 | 2015-10-26 19:58:29 +0000 | [diff] [blame] | 74 | |
| 75 | // Returns the character at Pos from end of a string. |
| 76 | static int charTailAt(StringPair *P, size_t Pos) { |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 77 | StringRef S = P->first.val(); |
Rui Ueyama | df94852 | 2015-10-26 19:58:29 +0000 | [diff] [blame] | 78 | if (Pos >= S.size()) |
| 79 | return -1; |
| 80 | return (unsigned char)S[S.size() - Pos - 1]; |
| 81 | } |
| 82 | |
| 83 | // Three-way radix quicksort. This is much faster than std::sort with strcmp |
| 84 | // because it does not compare characters that we already know the same. |
Rui Ueyama | 5579e0b | 2015-10-27 16:57:50 +0000 | [diff] [blame] | 85 | static void multikey_qsort(StringPair **Begin, StringPair **End, int Pos) { |
Rui Ueyama | df94852 | 2015-10-26 19:58:29 +0000 | [diff] [blame] | 86 | tailcall: |
| 87 | if (End - Begin <= 1) |
| 88 | return; |
| 89 | |
| 90 | // Partition items. Items in [Begin, P) are greater than the pivot, |
| 91 | // [P, Q) are the same as the pivot, and [Q, End) are less than the pivot. |
| 92 | int Pivot = charTailAt(*Begin, Pos); |
| 93 | StringPair **P = Begin; |
| 94 | StringPair **Q = End; |
| 95 | for (StringPair **R = Begin + 1; R < Q;) { |
| 96 | int C = charTailAt(*R, Pos); |
| 97 | if (C > Pivot) |
| 98 | std::swap(*P++, *R++); |
| 99 | else if (C < Pivot) |
| 100 | std::swap(*--Q, *R); |
| 101 | else |
| 102 | R++; |
Akira Hatanaka | 8e77dbb | 2014-09-24 20:37:14 +0000 | [diff] [blame] | 103 | } |
Rui Ueyama | df94852 | 2015-10-26 19:58:29 +0000 | [diff] [blame] | 104 | |
Rui Ueyama | 5579e0b | 2015-10-27 16:57:50 +0000 | [diff] [blame] | 105 | multikey_qsort(Begin, P, Pos); |
| 106 | multikey_qsort(Q, End, Pos); |
Rui Ueyama | df94852 | 2015-10-26 19:58:29 +0000 | [diff] [blame] | 107 | if (Pivot != -1) { |
| 108 | // qsort(P, Q, Pos + 1), but with tail call optimization. |
| 109 | Begin = P; |
| 110 | End = Q; |
| 111 | ++Pos; |
| 112 | goto tailcall; |
| 113 | } |
Akira Hatanaka | 8e77dbb | 2014-09-24 20:37:14 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Rafael Espindola | 21956e4 | 2015-10-23 21:48:05 +0000 | [diff] [blame] | 116 | void StringTableBuilder::finalize() { |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 117 | finalizeStringTable(/*Optimize=*/true); |
| 118 | } |
| 119 | |
| 120 | void StringTableBuilder::finalizeInOrder() { |
| 121 | finalizeStringTable(/*Optimize=*/false); |
| 122 | } |
| 123 | |
| 124 | void StringTableBuilder::finalizeStringTable(bool Optimize) { |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 125 | Finalized = true; |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 126 | |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 127 | if (Optimize) { |
| 128 | std::vector<StringPair *> Strings; |
| 129 | Strings.reserve(StringIndexMap.size()); |
| 130 | for (StringPair &P : StringIndexMap) |
| 131 | Strings.push_back(&P); |
| 132 | |
| 133 | if (!Strings.empty()) { |
| 134 | // If we're optimizing, sort by name. If not, sort by previously assigned |
| 135 | // offset. |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 136 | multikey_qsort(&Strings[0], &Strings[0] + Strings.size(), 0); |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 137 | } |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 138 | |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 139 | initSize(); |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 140 | |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 141 | StringRef Previous; |
| 142 | for (StringPair *P : Strings) { |
| 143 | StringRef S = P->first.val(); |
| 144 | if (Previous.endswith(S)) { |
| 145 | size_t Pos = Size - S.size() - (K != RAW); |
| 146 | if (!(Pos & (Alignment - 1))) { |
| 147 | P->second = Pos; |
| 148 | continue; |
| 149 | } |
Rafael Espindola | 758de9c | 2016-02-19 14:13:52 +0000 | [diff] [blame] | 150 | } |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 151 | |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 152 | Size = alignTo(Size, Alignment); |
| 153 | P->second = Size; |
Reid Kleckner | 2214ed8 | 2016-01-29 00:49:42 +0000 | [diff] [blame] | 154 | |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 155 | Size += S.size(); |
| 156 | if (K != RAW) |
| 157 | ++Size; |
| 158 | Previous = S; |
| 159 | } |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 160 | } |
Hans Wennborg | f26bfc1 | 2014-09-29 22:43:20 +0000 | [diff] [blame] | 161 | |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 162 | if (K == MachO) |
| 163 | Size = alignTo(Size, 4); // Pad to multiple of 4. |
Hans Wennborg | f26bfc1 | 2014-09-29 22:43:20 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void StringTableBuilder::clear() { |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 167 | Finalized = false; |
Hans Wennborg | f26bfc1 | 2014-09-29 22:43:20 +0000 | [diff] [blame] | 168 | StringIndexMap.clear(); |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 169 | } |
Rafael Espindola | fc063e8 | 2015-10-22 18:32:06 +0000 | [diff] [blame] | 170 | |
Justin Lebar | ee34a73 | 2016-10-17 22:24:36 +0000 | [diff] [blame] | 171 | size_t StringTableBuilder::getOffset(CachedHashStringRef S) const { |
Rafael Espindola | fc063e8 | 2015-10-22 18:32:06 +0000 | [diff] [blame] | 172 | assert(isFinalized()); |
Rafael Espindola | a9b3944 | 2015-10-23 20:15:35 +0000 | [diff] [blame] | 173 | auto I = StringIndexMap.find(S); |
Rafael Espindola | fc063e8 | 2015-10-22 18:32:06 +0000 | [diff] [blame] | 174 | assert(I != StringIndexMap.end() && "String is not in table!"); |
| 175 | return I->second; |
| 176 | } |
| 177 | |
Justin Lebar | ee34a73 | 2016-10-17 22:24:36 +0000 | [diff] [blame] | 178 | size_t StringTableBuilder::add(CachedHashStringRef S) { |
Rafael Espindola | 39751af | 2016-10-04 22:43:25 +0000 | [diff] [blame] | 179 | if (K == WinCOFF) |
| 180 | assert(S.size() > COFF::NameSize && "Short string in COFF string table!"); |
| 181 | |
Rafael Espindola | fc063e8 | 2015-10-22 18:32:06 +0000 | [diff] [blame] | 182 | assert(!isFinalized()); |
Rafael Espindola | 4a2ef91 | 2016-10-14 17:01:39 +0000 | [diff] [blame] | 183 | auto P = StringIndexMap.insert(std::make_pair(S, 0)); |
| 184 | if (P.second) { |
| 185 | size_t Start = alignTo(Size, Alignment); |
| 186 | P.first->second = Start; |
Rafael Espindola | 758de9c | 2016-02-19 14:13:52 +0000 | [diff] [blame] | 187 | Size = Start + S.size() + (K != RAW); |
Rafael Espindola | 4a2ef91 | 2016-10-14 17:01:39 +0000 | [diff] [blame] | 188 | } |
Rafael Espindola | 21956e4 | 2015-10-23 21:48:05 +0000 | [diff] [blame] | 189 | return P.first->second; |
Rafael Espindola | fc063e8 | 2015-10-22 18:32:06 +0000 | [diff] [blame] | 190 | } |