1 //===- StringTableBuilder.cpp - String table building utility -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/StringTableBuilder.h"
11 #include "llvm/ADT/CachedHashString.h"
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/BinaryFormat/COFF.h"
15 #include "llvm/Support/Endian.h"
16 #include "llvm/Support/MathExtras.h"
17 #include "llvm/Support/raw_ostream.h"
27 StringTableBuilder::~StringTableBuilder() = default;
29 void StringTableBuilder::initSize() {
30 // Account for leading bytes in table so that offsets returned from add are
39 // Start the table with a NUL byte.
43 // Make room to write the table size later.
49 StringTableBuilder::StringTableBuilder(Kind K
, unsigned Alignment
)
50 : K(K
), Alignment(Alignment
) {
54 void StringTableBuilder::write(raw_ostream
&OS
) const {
55 assert(isFinalized());
57 Data
.resize(getSize());
58 write((uint8_t *)Data
.data());
62 using StringPair
= std::pair
<CachedHashStringRef
, size_t>;
64 void StringTableBuilder::write(uint8_t *Buf
) const {
65 assert(isFinalized());
66 for (const StringPair
&P
: StringIndexMap
) {
67 StringRef Data
= P
.first
.val();
69 memcpy(Buf
+ P
.second
, Data
.data(), Data
.size());
73 support::endian::write32le(Buf
, Size
);
76 // Returns the character at Pos from end of a string.
77 static int charTailAt(StringPair
*P
, size_t Pos
) {
78 StringRef S
= P
->first
.val();
81 return (unsigned char)S
[S
.size() - Pos
- 1];
84 // Three-way radix quicksort. This is much faster than std::sort with strcmp
85 // because it does not compare characters that we already know the same.
86 static void multikeySort(MutableArrayRef
<StringPair
*> Vec
, int Pos
) {
91 // Partition items so that items in [0, I) are greater than the pivot,
92 // [I, J) are the same as the pivot, and [J, Vec.size()) are less than
94 int Pivot
= charTailAt(Vec
[0], Pos
);
96 size_t J
= Vec
.size();
97 for (size_t K
= 1; K
< J
;) {
98 int C
= charTailAt(Vec
[K
], Pos
);
100 std::swap(Vec
[I
++], Vec
[K
++]);
102 std::swap(Vec
[--J
], Vec
[K
]);
107 multikeySort(Vec
.slice(0, I
), Pos
);
108 multikeySort(Vec
.slice(J
), Pos
);
110 // multikeySort(Vec.slice(I, J - I), Pos + 1), but with
111 // tail call optimization.
113 Vec
= Vec
.slice(I
, J
- I
);
119 void StringTableBuilder::finalize() {
121 finalizeStringTable(/*Optimize=*/true);
124 void StringTableBuilder::finalizeInOrder() {
125 finalizeStringTable(/*Optimize=*/false);
128 void StringTableBuilder::finalizeStringTable(bool Optimize
) {
132 std::vector
<StringPair
*> Strings
;
133 Strings
.reserve(StringIndexMap
.size());
134 for (StringPair
&P
: StringIndexMap
)
135 Strings
.push_back(&P
);
137 multikeySort(Strings
, 0);
141 for (StringPair
*P
: Strings
) {
142 StringRef S
= P
->first
.val();
143 if (Previous
.endswith(S
)) {
144 size_t Pos
= Size
- S
.size() - (K
!= RAW
);
145 if (!(Pos
& (Alignment
- 1))) {
151 Size
= alignTo(Size
, Alignment
);
162 Size
= alignTo(Size
, 4); // Pad to multiple of 4.
165 void StringTableBuilder::clear() {
167 StringIndexMap
.clear();
170 size_t StringTableBuilder::getOffset(CachedHashStringRef S
) const {
171 assert(isFinalized());
172 auto I
= StringIndexMap
.find(S
);
173 assert(I
!= StringIndexMap
.end() && "String is not in table!");
177 size_t StringTableBuilder::add(CachedHashStringRef S
) {
179 assert(S
.size() > COFF::NameSize
&& "Short string in COFF string table!");
181 assert(!isFinalized());
182 auto P
= StringIndexMap
.insert(std::make_pair(S
, 0));
184 size_t Start
= alignTo(Size
, Alignment
);
185 P
.first
->second
= Start
;
186 Size
= Start
+ S
.size() + (K
!= RAW
);
188 return P
.first
->second
;