2014-04-30 18:25:02 +02:00
|
|
|
//===-- StringTableBuilder.cpp - String table building utility ------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-07-03 04:01:39 +02:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
2015-10-22 17:15:44 +02:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2014-09-30 00:43:20 +02:00
|
|
|
#include "llvm/Support/COFF.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
2014-04-30 18:25:02 +02:00
|
|
|
|
2015-10-22 18:42:31 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2014-04-30 18:25:02 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2016-01-29 01:49:42 +01:00
|
|
|
StringTableBuilder::StringTableBuilder(Kind K) : K(K) {
|
|
|
|
// Account for leading bytes in table so that offsets returned from add are
|
|
|
|
// correct.
|
|
|
|
switch (K) {
|
|
|
|
case RAW:
|
|
|
|
Size = 0;
|
|
|
|
break;
|
|
|
|
case MachO:
|
|
|
|
case ELF:
|
|
|
|
Size = 1;
|
|
|
|
break;
|
|
|
|
case WinCOFF:
|
|
|
|
Size = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-10-23 23:48:05 +02:00
|
|
|
|
2015-10-26 20:58:29 +01:00
|
|
|
typedef std::pair<StringRef, size_t> StringPair;
|
|
|
|
|
|
|
|
// Returns the character at Pos from end of a string.
|
|
|
|
static int charTailAt(StringPair *P, size_t Pos) {
|
|
|
|
StringRef S = P->first;
|
|
|
|
if (Pos >= S.size())
|
|
|
|
return -1;
|
|
|
|
return (unsigned char)S[S.size() - Pos - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Three-way radix quicksort. This is much faster than std::sort with strcmp
|
|
|
|
// because it does not compare characters that we already know the same.
|
2015-10-27 17:57:50 +01:00
|
|
|
static void multikey_qsort(StringPair **Begin, StringPair **End, int Pos) {
|
2015-10-26 20:58:29 +01:00
|
|
|
tailcall:
|
|
|
|
if (End - Begin <= 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Partition items. Items in [Begin, P) are greater than the pivot,
|
|
|
|
// [P, Q) are the same as the pivot, and [Q, End) are less than the pivot.
|
|
|
|
int Pivot = charTailAt(*Begin, Pos);
|
|
|
|
StringPair **P = Begin;
|
|
|
|
StringPair **Q = End;
|
|
|
|
for (StringPair **R = Begin + 1; R < Q;) {
|
|
|
|
int C = charTailAt(*R, Pos);
|
|
|
|
if (C > Pivot)
|
|
|
|
std::swap(*P++, *R++);
|
|
|
|
else if (C < Pivot)
|
|
|
|
std::swap(*--Q, *R);
|
|
|
|
else
|
|
|
|
R++;
|
|
|
|
}
|
|
|
|
|
2015-10-27 17:57:50 +01:00
|
|
|
multikey_qsort(Begin, P, Pos);
|
|
|
|
multikey_qsort(Q, End, Pos);
|
2015-10-26 20:58:29 +01:00
|
|
|
if (Pivot != -1) {
|
|
|
|
// qsort(P, Q, Pos + 1), but with tail call optimization.
|
|
|
|
Begin = P;
|
|
|
|
End = Q;
|
|
|
|
++Pos;
|
|
|
|
goto tailcall;
|
2014-09-24 22:37:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 23:48:05 +02:00
|
|
|
void StringTableBuilder::finalize() {
|
2016-01-29 01:49:42 +01:00
|
|
|
finalizeStringTable(/*Optimize=*/true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::finalizeInOrder() {
|
|
|
|
finalizeStringTable(/*Optimize=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::finalizeStringTable(bool Optimize) {
|
|
|
|
typedef std::pair<StringRef, size_t> StringOffsetPair;
|
|
|
|
std::vector<StringOffsetPair *> Strings;
|
2014-09-30 00:43:20 +02:00
|
|
|
Strings.reserve(StringIndexMap.size());
|
2016-01-29 01:49:42 +01:00
|
|
|
for (StringOffsetPair &P : StringIndexMap)
|
2015-10-22 17:26:35 +02:00
|
|
|
Strings.push_back(&P);
|
2014-04-30 18:25:02 +02:00
|
|
|
|
2016-01-29 01:49:42 +01:00
|
|
|
if (!Strings.empty()) {
|
|
|
|
// If we're optimizing, sort by name. If not, sort by previously assigned
|
|
|
|
// offset.
|
|
|
|
if (Optimize) {
|
|
|
|
multikey_qsort(&Strings[0], &Strings[0] + Strings.size(), 0);
|
|
|
|
} else {
|
|
|
|
std::sort(Strings.begin(), Strings.end(),
|
|
|
|
[](const StringOffsetPair *LHS, const StringOffsetPair *RHS) {
|
|
|
|
return LHS->second < RHS->second;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-04-30 18:25:02 +02:00
|
|
|
|
2015-10-23 22:15:35 +02:00
|
|
|
switch (K) {
|
2015-10-23 23:48:05 +02:00
|
|
|
case RAW:
|
|
|
|
break;
|
2014-10-06 19:05:19 +02:00
|
|
|
case ELF:
|
|
|
|
case MachO:
|
2014-09-30 00:43:20 +02:00
|
|
|
// Start the table with a NUL byte.
|
|
|
|
StringTable += '\x00';
|
2014-10-06 19:05:19 +02:00
|
|
|
break;
|
|
|
|
case WinCOFF:
|
2014-09-30 00:43:20 +02:00
|
|
|
// Make room to write the table size later.
|
|
|
|
StringTable.append(4, '\x00');
|
2014-10-06 19:05:19 +02:00
|
|
|
break;
|
2014-09-30 00:43:20 +02:00
|
|
|
}
|
2014-04-30 18:25:02 +02:00
|
|
|
|
|
|
|
StringRef Previous;
|
2016-01-29 01:49:42 +01:00
|
|
|
for (StringOffsetPair *P : Strings) {
|
2015-10-23 22:15:35 +02:00
|
|
|
StringRef S = P->first;
|
|
|
|
if (K == WinCOFF)
|
|
|
|
assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
|
2014-09-30 00:43:20 +02:00
|
|
|
|
2016-01-29 01:49:42 +01:00
|
|
|
if (Optimize && Previous.endswith(S)) {
|
2015-10-23 23:48:05 +02:00
|
|
|
P->second = StringTable.size() - S.size() - (K != RAW);
|
2014-04-30 18:25:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-01-29 01:49:42 +01:00
|
|
|
if (Optimize)
|
|
|
|
P->second = StringTable.size();
|
|
|
|
else
|
|
|
|
assert(P->second == StringTable.size() &&
|
|
|
|
"different strtab offset after finalization");
|
|
|
|
|
2015-10-23 22:15:35 +02:00
|
|
|
StringTable += S;
|
2015-10-23 23:48:05 +02:00
|
|
|
if (K != RAW)
|
|
|
|
StringTable += '\x00';
|
2015-10-23 22:15:35 +02:00
|
|
|
Previous = S;
|
2014-04-30 18:25:02 +02:00
|
|
|
}
|
2014-09-30 00:43:20 +02:00
|
|
|
|
2015-10-23 22:15:35 +02:00
|
|
|
switch (K) {
|
2015-10-23 23:48:05 +02:00
|
|
|
case RAW:
|
2014-10-06 19:05:19 +02:00
|
|
|
case ELF:
|
|
|
|
break;
|
|
|
|
case MachO:
|
|
|
|
// Pad to multiple of 4.
|
|
|
|
while (StringTable.size() % 4)
|
|
|
|
StringTable += '\x00';
|
|
|
|
break;
|
|
|
|
case WinCOFF:
|
|
|
|
// Write the table size in the first word.
|
2014-09-30 00:43:20 +02:00
|
|
|
assert(StringTable.size() <= std::numeric_limits<uint32_t>::max());
|
2015-10-23 22:15:35 +02:00
|
|
|
uint32_t Size = static_cast<uint32_t>(StringTable.size());
|
2014-09-30 00:43:20 +02:00
|
|
|
support::endian::write<uint32_t, support::little, support::unaligned>(
|
2015-10-23 22:15:35 +02:00
|
|
|
StringTable.data(), Size);
|
2014-10-06 19:05:19 +02:00
|
|
|
break;
|
2014-09-30 00:43:20 +02:00
|
|
|
}
|
2015-10-23 23:48:05 +02:00
|
|
|
|
|
|
|
Size = StringTable.size();
|
2014-09-30 00:43:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::clear() {
|
|
|
|
StringTable.clear();
|
|
|
|
StringIndexMap.clear();
|
2014-04-30 18:25:02 +02:00
|
|
|
}
|
2015-10-22 20:32:06 +02:00
|
|
|
|
2015-10-23 22:15:35 +02:00
|
|
|
size_t StringTableBuilder::getOffset(StringRef S) const {
|
2015-10-22 20:32:06 +02:00
|
|
|
assert(isFinalized());
|
2015-10-23 22:15:35 +02:00
|
|
|
auto I = StringIndexMap.find(S);
|
2015-10-22 20:32:06 +02:00
|
|
|
assert(I != StringIndexMap.end() && "String is not in table!");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2015-10-23 23:48:05 +02:00
|
|
|
size_t StringTableBuilder::add(StringRef S) {
|
2015-10-22 20:32:06 +02:00
|
|
|
assert(!isFinalized());
|
2015-10-23 23:48:05 +02:00
|
|
|
auto P = StringIndexMap.insert(std::make_pair(S, Size));
|
|
|
|
if (P.second)
|
|
|
|
Size += S.size() + (K != RAW);
|
|
|
|
return P.first->second;
|
2015-10-22 20:32:06 +02:00
|
|
|
}
|