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 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" 18 #include <cassert> 19 #include <cstddef> 20 #include <cstdint> 21 #include <cstring> 22 #include <utility> 23 #include <vector> 24 25 using namespace llvm; 26 27 StringTableBuilder::~StringTableBuilder() = default; 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 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()); 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(std::vector<StringPair *> &Vec, size_t Begin, 86 size_t End, int Pos) { 87 tailcall: 88 if (End - Begin <= 1) 89 return; 90 91 // Partition items so that items in [Begin, P) are greater than the pivot, 92 // [P, Q) are the same as the pivot, and [Q, End) are less than the pivot. 93 int Pivot = charTailAt(Vec[Begin], Pos); 94 size_t P = Begin; 95 size_t Q = End; 96 for (size_t R = Begin + 1; R < Q;) { 97 int C = charTailAt(Vec[R], Pos); 98 if (C > Pivot) 99 std::swap(Vec[P++], Vec[R++]); 100 else if (C < Pivot) 101 std::swap(Vec[--Q], Vec[R]); 102 else 103 R++; 104 } 105 106 multikey_qsort(Vec, Begin, P, Pos); 107 multikey_qsort(Vec, Q, End, Pos); 108 if (Pivot != -1) { 109 // qsort(P, Q, Pos + 1), but with tail call optimization. 110 Begin = P; 111 End = Q; 112 ++Pos; 113 goto tailcall; 114 } 115 } 116 117 void StringTableBuilder::finalize() { 118 finalizeStringTable(/*Optimize=*/true); 119 } 120 121 void StringTableBuilder::finalizeInOrder() { 122 finalizeStringTable(/*Optimize=*/false); 123 } 124 125 void StringTableBuilder::finalizeStringTable(bool Optimize) { 126 Finalized = true; 127 128 if (Optimize) { 129 std::vector<StringPair *> Strings; 130 Strings.reserve(StringIndexMap.size()); 131 for (StringPair &P : StringIndexMap) 132 Strings.push_back(&P); 133 134 if (!Strings.empty()) { 135 // If we're optimizing, sort by name. If not, sort by previously assigned 136 // offset. 137 multikey_qsort(Strings, 0, Strings.size(), 0); 138 } 139 140 initSize(); 141 142 StringRef Previous; 143 for (StringPair *P : Strings) { 144 StringRef S = P->first.val(); 145 if (Previous.endswith(S)) { 146 size_t Pos = Size - S.size() - (K != RAW); 147 if (!(Pos & (Alignment - 1))) { 148 P->second = Pos; 149 continue; 150 } 151 } 152 153 Size = alignTo(Size, Alignment); 154 P->second = Size; 155 156 Size += S.size(); 157 if (K != RAW) 158 ++Size; 159 Previous = S; 160 } 161 } 162 163 if (K == MachO) 164 Size = alignTo(Size, 4); // Pad to multiple of 4. 165 } 166 167 void StringTableBuilder::clear() { 168 Finalized = false; 169 StringIndexMap.clear(); 170 } 171 172 size_t StringTableBuilder::getOffset(CachedHashStringRef S) const { 173 assert(isFinalized()); 174 auto I = StringIndexMap.find(S); 175 assert(I != StringIndexMap.end() && "String is not in table!"); 176 return I->second; 177 } 178 179 size_t StringTableBuilder::add(CachedHashStringRef S) { 180 if (K == WinCOFF) 181 assert(S.size() > COFF::NameSize && "Short string in COFF string table!"); 182 183 assert(!isFinalized()); 184 auto P = StringIndexMap.insert(std::make_pair(S, 0)); 185 if (P.second) { 186 size_t Start = alignTo(Size, Alignment); 187 P.first->second = Start; 188 Size = Start + S.size() + (K != RAW); 189 } 190 return P.first->second; 191 } 192