2014-11-24 09:08:18 +00: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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
2015-12-30 11:46:15 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2017-01-02 19:17:04 +00:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2015-01-18 16:17:27 +00:00
|
|
|
#include "llvm/Support/COFF.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
2017-01-02 19:17:04 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-11-24 09:08:18 +00:00
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
StringTableBuilder::~StringTableBuilder() {}
|
|
|
|
|
|
|
|
void StringTableBuilder::initSize() {
|
2016-07-23 20:41:05 +00:00
|
|
|
// 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:
|
2017-01-02 19:17:04 +00:00
|
|
|
// Start the table with a NUL byte.
|
2016-07-23 20:41:05 +00:00
|
|
|
Size = 1;
|
|
|
|
break;
|
|
|
|
case WinCOFF:
|
2017-01-02 19:17:04 +00:00
|
|
|
// Make room to write the table size later.
|
2016-07-23 20:41:05 +00:00
|
|
|
Size = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-12-30 11:46:15 +00:00
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
StringTableBuilder::StringTableBuilder(Kind K, unsigned Alignment)
|
|
|
|
: K(K), Alignment(Alignment) {
|
|
|
|
initSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::write(raw_ostream &OS) const {
|
|
|
|
assert(isFinalized());
|
|
|
|
SmallString<0> Data;
|
|
|
|
Data.resize(getSize());
|
|
|
|
write((uint8_t *)&Data[0]);
|
|
|
|
OS << Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef std::pair<CachedHashStringRef, size_t> StringPair;
|
|
|
|
|
|
|
|
void StringTableBuilder::write(uint8_t *Buf) const {
|
|
|
|
assert(isFinalized());
|
|
|
|
for (const StringPair &P : StringIndexMap) {
|
|
|
|
StringRef Data = P.first.val();
|
|
|
|
if (!Data.empty())
|
|
|
|
memcpy(Buf + P.second, Data.data(), Data.size());
|
|
|
|
}
|
|
|
|
if (K != WinCOFF)
|
|
|
|
return;
|
|
|
|
support::endian::write32le(Buf, Size);
|
|
|
|
}
|
2015-12-30 11:46:15 +00:00
|
|
|
|
|
|
|
// Returns the character at Pos from end of a string.
|
|
|
|
static int charTailAt(StringPair *P, size_t Pos) {
|
2017-01-02 19:17:04 +00:00
|
|
|
StringRef S = P->first.val();
|
2015-12-30 11:46:15 +00:00
|
|
|
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.
|
|
|
|
static void multikey_qsort(StringPair **Begin, StringPair **End, int Pos) {
|
|
|
|
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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
multikey_qsort(Begin, P, Pos);
|
|
|
|
multikey_qsort(Q, End, Pos);
|
|
|
|
if (Pivot != -1) {
|
|
|
|
// qsort(P, Q, Pos + 1), but with tail call optimization.
|
|
|
|
Begin = P;
|
|
|
|
End = Q;
|
|
|
|
++Pos;
|
|
|
|
goto tailcall;
|
2014-11-24 09:08:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
void StringTableBuilder::finalize() {
|
2016-07-23 20:41:05 +00:00
|
|
|
finalizeStringTable(/*Optimize=*/true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::finalizeInOrder() {
|
|
|
|
finalizeStringTable(/*Optimize=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::finalizeStringTable(bool Optimize) {
|
2017-01-02 19:17:04 +00:00
|
|
|
Finalized = true;
|
2014-11-24 09:08:18 +00:00
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
if (Optimize) {
|
|
|
|
std::vector<StringPair *> Strings;
|
|
|
|
Strings.reserve(StringIndexMap.size());
|
|
|
|
for (StringPair &P : StringIndexMap)
|
|
|
|
Strings.push_back(&P);
|
2014-11-24 09:08:18 +00:00
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
if (!Strings.empty()) {
|
|
|
|
// If we're optimizing, sort by name. If not, sort by previously assigned
|
|
|
|
// offset.
|
|
|
|
multikey_qsort(&Strings[0], &Strings[0] + Strings.size(), 0);
|
2016-07-23 20:41:05 +00:00
|
|
|
}
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
initSize();
|
|
|
|
|
|
|
|
StringRef Previous;
|
|
|
|
for (StringPair *P : Strings) {
|
|
|
|
StringRef S = P->first.val();
|
|
|
|
if (Previous.endswith(S)) {
|
|
|
|
size_t Pos = Size - S.size() - (K != RAW);
|
|
|
|
if (!(Pos & (Alignment - 1))) {
|
|
|
|
P->second = Pos;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2014-11-24 09:08:18 +00:00
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
Size = alignTo(Size, Alignment);
|
|
|
|
P->second = Size;
|
2015-01-18 16:17:27 +00:00
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
Size += S.size();
|
|
|
|
if (K != RAW)
|
|
|
|
++Size;
|
|
|
|
Previous = S;
|
|
|
|
}
|
2015-01-18 16:17:27 +00:00
|
|
|
}
|
2015-12-30 11:46:15 +00:00
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
if (K == MachO)
|
|
|
|
Size = alignTo(Size, 4); // Pad to multiple of 4.
|
2015-01-18 16:17:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::clear() {
|
2017-01-02 19:17:04 +00:00
|
|
|
Finalized = false;
|
2015-01-18 16:17:27 +00:00
|
|
|
StringIndexMap.clear();
|
2014-11-24 09:08:18 +00:00
|
|
|
}
|
2015-12-30 11:46:15 +00:00
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
size_t StringTableBuilder::getOffset(CachedHashStringRef S) const {
|
2015-12-30 11:46:15 +00:00
|
|
|
assert(isFinalized());
|
|
|
|
auto I = StringIndexMap.find(S);
|
|
|
|
assert(I != StringIndexMap.end() && "String is not in table!");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
size_t StringTableBuilder::add(CachedHashStringRef S) {
|
|
|
|
if (K == WinCOFF)
|
|
|
|
assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
|
|
|
|
|
2015-12-30 11:46:15 +00:00
|
|
|
assert(!isFinalized());
|
2017-01-02 19:17:04 +00:00
|
|
|
auto P = StringIndexMap.insert(std::make_pair(S, 0));
|
|
|
|
if (P.second) {
|
|
|
|
size_t Start = alignTo(Size, Alignment);
|
|
|
|
P.first->second = Start;
|
2016-07-23 20:41:05 +00:00
|
|
|
Size = Start + S.size() + (K != RAW);
|
2017-01-02 19:17:04 +00:00
|
|
|
}
|
2015-12-30 11:46:15 +00:00
|
|
|
return P.first->second;
|
|
|
|
}
|