LLVM  5.0.0
StringTableBuilder.cpp
Go to the documentation of this file.
1 //===- StringTableBuilder.cpp - String table building utility -------------===//
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 
12 #include "llvm/ADT/SmallString.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/BinaryFormat/COFF.h"
15 #include "llvm/Support/Endian.h"
18 #include <cassert>
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstring>
22 #include <utility>
23 #include <vector>
24 
25 using namespace llvm;
26 
28 
29 void StringTableBuilder::initSize() {
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:
38  // Start the table with a NUL byte.
39  Size = 1;
40  break;
41  case WinCOFF:
42  // Make room to write the table size later.
43  Size = 4;
44  break;
45  }
46 }
47 
49  : K(K), Alignment(Alignment) {
50  initSize();
51 }
52 
54  assert(isFinalized());
56  Data.resize(getSize());
57  write((uint8_t *)Data.data());
58  OS << Data;
59 }
60 
61 using StringPair = std::pair<CachedHashStringRef, size_t>;
62 
63 void StringTableBuilder::write(uint8_t *Buf) const {
64  assert(isFinalized());
65  for (const StringPair &P : StringIndexMap) {
66  StringRef Data = P.first.val();
67  if (!Data.empty())
68  memcpy(Buf + P.second, Data.data(), Data.size());
69  }
70  if (K != WinCOFF)
71  return;
72  support::endian::write32le(Buf, Size);
73 }
74 
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();
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.
85 static void multikey_qsort(StringPair **Begin, StringPair **End, int Pos) {
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++;
103  }
104 
105  multikey_qsort(Begin, P, Pos);
106  multikey_qsort(Q, End, Pos);
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  }
114 }
115 
117  finalizeStringTable(/*Optimize=*/true);
118 }
119 
121  finalizeStringTable(/*Optimize=*/false);
122 }
123 
124 void StringTableBuilder::finalizeStringTable(bool Optimize) {
125  Finalized = true;
126 
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.
136  multikey_qsort(&Strings[0], &Strings[0] + Strings.size(), 0);
137  }
138 
139  initSize();
140 
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  }
150  }
151 
152  Size = alignTo(Size, Alignment);
153  P->second = Size;
154 
155  Size += S.size();
156  if (K != RAW)
157  ++Size;
158  Previous = S;
159  }
160  }
161 
162  if (K == MachO)
163  Size = alignTo(Size, 4); // Pad to multiple of 4.
164 }
165 
167  Finalized = false;
168  StringIndexMap.clear();
169 }
170 
172  assert(isFinalized());
173  auto I = StringIndexMap.find(S);
174  assert(I != StringIndexMap.end() && "String is not in table!");
175  return I->second;
176 }
177 
179  if (K == WinCOFF)
180  assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
181 
182  assert(!isFinalized());
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;
187  Size = Start + S.size() + (K != RAW);
188  }
189  return P.first->second;
190 }
A container which contains a StringRef plus a precomputed hash.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:279
void write32le(void *P, uint32_t V)
Definition: Endian.h:404
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:677
size_t add(CachedHashStringRef S)
Add a string to the builder.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
static int charTailAt(StringPair *P, size_t Pos)
#define P(N)
void finalizeInOrder()
Finalize the string table without reording it.
static const unsigned End
static void multikey_qsort(StringPair **Begin, StringPair **End, int Pos)
void finalize()
Analyze the strings and build the final table.
void write(raw_ostream &OS) const
size_t getOffset(CachedHashStringRef S) const
Get the offest of a string in the string table.
StringTableBuilder(Kind K, unsigned Alignment=1)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:923
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:142
#define C
Definition: LargeTest.cpp:25
#define I(x, y, z)
Definition: MD5.cpp:58
std::pair< CachedHashStringRef, size_t > StringPair
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void resize(size_type N)
Definition: SmallVector.h:352