1 //===- StringTableBuilder.cpp - String table building utility -------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/StringTableBuilder.h"
10 #include "llvm/ADT/CachedHashString.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/BinaryFormat/COFF.h"
14 #include "llvm/Support/Endian.h"
15 #include "llvm/Support/MathExtras.h"
16 #include "llvm/Support/raw_ostream.h"
26 StringTableBuilder::~StringTableBuilder() = default;
28 void StringTableBuilder::initSize() {
29 // Account for leading bytes in table so that offsets returned from add are
38 // Start the table with a NUL byte.
42 // Make room to write the table size later.
48 StringTableBuilder::StringTableBuilder(Kind K, unsigned Alignment)
49 : K(K), Alignment(Alignment) {
53 void StringTableBuilder::write(raw_ostream &OS) const {
54 assert(isFinalized());
56 Data.resize(getSize());
57 write((uint8_t *)Data.data());
61 using StringPair = std::pair<CachedHashStringRef, size_t>;
63 void StringTableBuilder::write(uint8_t *Buf) const {
64 assert(isFinalized());
65 for (const StringPair &P : StringIndexMap) {
66 StringRef Data = P.first.val();
68 memcpy(Buf + P.second, Data.data(), Data.size());
72 support::endian::write32le(Buf, Size);
75 // Returns the character at Pos from end of a string.
76 static int charTailAt(StringPair *P, size_t Pos) {
77 StringRef S = P->first.val();
80 return (unsigned char)S[S.size() - Pos - 1];
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.
85 static void multikeySort(MutableArrayRef<StringPair *> Vec, int Pos) {
90 // Partition items so that items in [0, I) are greater than the pivot,
91 // [I, J) are the same as the pivot, and [J, Vec.size()) are less than
93 int Pivot = charTailAt(Vec[0], Pos);
95 size_t J = Vec.size();
96 for (size_t K = 1; K < J;) {
97 int C = charTailAt(Vec[K], Pos);
99 std::swap(Vec[I++], Vec[K++]);
101 std::swap(Vec[--J], Vec[K]);
106 multikeySort(Vec.slice(0, I), Pos);
107 multikeySort(Vec.slice(J), Pos);
109 // multikeySort(Vec.slice(I, J - I), Pos + 1), but with
110 // tail call optimization.
112 Vec = Vec.slice(I, J - I);
118 void StringTableBuilder::finalize() {
120 finalizeStringTable(/*Optimize=*/true);
123 void StringTableBuilder::finalizeInOrder() {
124 finalizeStringTable(/*Optimize=*/false);
127 void StringTableBuilder::finalizeStringTable(bool Optimize) {
131 std::vector<StringPair *> Strings;
132 Strings.reserve(StringIndexMap.size());
133 for (StringPair &P : StringIndexMap)
134 Strings.push_back(&P);
136 multikeySort(Strings, 0);
140 for (StringPair *P : Strings) {
141 StringRef S = P->first.val();
142 if (Previous.endswith(S)) {
143 size_t Pos = Size - S.size() - (K != RAW);
144 if (!(Pos & (Alignment - 1))) {
150 Size = alignTo(Size, Alignment);
161 Size = alignTo(Size, 4); // Pad to multiple of 4.
163 // The first byte in an ELF string table must be null, according to the ELF
164 // specification. In 'initSize()' we reserved the first byte to hold null for
165 // this purpose and here we actually add the string to allow 'getOffset()' to
166 // be called on an empty string.
168 StringIndexMap[CachedHashStringRef("")] = 0;
171 void StringTableBuilder::clear() {
173 StringIndexMap.clear();
176 size_t StringTableBuilder::getOffset(CachedHashStringRef S) const {
177 assert(isFinalized());
178 auto I = StringIndexMap.find(S);
179 assert(I != StringIndexMap.end() && "String is not in table!");
183 size_t StringTableBuilder::add(CachedHashStringRef S) {
185 assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
187 assert(!isFinalized());
188 auto P = StringIndexMap.insert(std::make_pair(S, 0));
190 size_t Start = alignTo(Size, Alignment);
191 P.first->second = Start;
192 Size = Start + S.size() + (K != RAW);
194 return P.first->second;