2017-02-11 08:27:28 +08:00
|
|
|
//===- StringTableBuilder.cpp - String table building utility -------------===//
|
2014-05-01 00:25:02 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-05-01 00:25:02 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/ADT/CachedHashString.h"
|
2016-10-18 06:24:36 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/COFF.h"
|
2014-09-30 06:43:20 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2016-10-05 06:43:25 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-11 08:27:28 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
|
|
|
#include <utility>
|
2015-10-23 00:42:31 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2014-05-01 00:25:02 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-02-11 08:27:28 +08:00
|
|
|
StringTableBuilder::~StringTableBuilder() = default;
|
2016-10-05 06:43:25 +08:00
|
|
|
|
|
|
|
void StringTableBuilder::initSize() {
|
2016-01-29 08:49:42 +08:00
|
|
|
// Account for leading bytes in table so that offsets returned from add are
|
|
|
|
// correct.
|
|
|
|
switch (K) {
|
|
|
|
case RAW:
|
2018-02-07 04:29:21 +08:00
|
|
|
case DWARF:
|
2016-01-29 08:49:42 +08:00
|
|
|
Size = 0;
|
|
|
|
break;
|
|
|
|
case MachO:
|
|
|
|
case ELF:
|
2016-10-05 06:43:25 +08:00
|
|
|
// Start the table with a NUL byte.
|
2016-01-29 08:49:42 +08:00
|
|
|
Size = 1;
|
|
|
|
break;
|
|
|
|
case WinCOFF:
|
2016-10-05 06:43:25 +08:00
|
|
|
// Make room to write the table size later.
|
2016-01-29 08:49:42 +08:00
|
|
|
Size = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-10-24 05:48:05 +08:00
|
|
|
|
2016-10-05 06:43:25 +08: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());
|
2017-04-06 08:10:17 +08:00
|
|
|
write((uint8_t *)Data.data());
|
2016-10-05 06:43:25 +08:00
|
|
|
OS << Data;
|
|
|
|
}
|
|
|
|
|
2017-04-27 06:31:39 +08:00
|
|
|
using StringPair = std::pair<CachedHashStringRef, size_t>;
|
2016-10-05 06:43:25 +08:00
|
|
|
|
|
|
|
void StringTableBuilder::write(uint8_t *Buf) const {
|
|
|
|
assert(isFinalized());
|
|
|
|
for (const StringPair &P : StringIndexMap) {
|
|
|
|
StringRef Data = P.first.val();
|
2016-10-06 00:33:03 +08:00
|
|
|
if (!Data.empty())
|
|
|
|
memcpy(Buf + P.second, Data.data(), Data.size());
|
2016-10-05 06:43:25 +08:00
|
|
|
}
|
|
|
|
if (K != WinCOFF)
|
|
|
|
return;
|
|
|
|
support::endian::write32le(Buf, Size);
|
|
|
|
}
|
2015-10-27 03:58:29 +08:00
|
|
|
|
|
|
|
// Returns the character at Pos from end of a string.
|
|
|
|
static int charTailAt(StringPair *P, size_t Pos) {
|
2016-10-05 06:43:25 +08:00
|
|
|
StringRef S = P->first.val();
|
2015-10-27 03:58:29 +08: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.
|
2017-10-04 07:12:01 +08:00
|
|
|
static void multikeySort(MutableArrayRef<StringPair *> Vec, int Pos) {
|
2015-10-27 03:58:29 +08:00
|
|
|
tailcall:
|
2017-10-04 07:12:01 +08:00
|
|
|
if (Vec.size() <= 1)
|
2015-10-27 03:58:29 +08:00
|
|
|
return;
|
|
|
|
|
2017-10-04 07:12:01 +08:00
|
|
|
// Partition items so that items in [0, I) are greater than the pivot,
|
|
|
|
// [I, J) are the same as the pivot, and [J, Vec.size()) are less than
|
|
|
|
// the pivot.
|
|
|
|
int Pivot = charTailAt(Vec[0], Pos);
|
|
|
|
size_t I = 0;
|
|
|
|
size_t J = Vec.size();
|
|
|
|
for (size_t K = 1; K < J;) {
|
|
|
|
int C = charTailAt(Vec[K], Pos);
|
2015-10-27 03:58:29 +08:00
|
|
|
if (C > Pivot)
|
2017-10-04 07:12:01 +08:00
|
|
|
std::swap(Vec[I++], Vec[K++]);
|
2015-10-27 03:58:29 +08:00
|
|
|
else if (C < Pivot)
|
2017-10-04 07:12:01 +08:00
|
|
|
std::swap(Vec[--J], Vec[K]);
|
2015-10-27 03:58:29 +08:00
|
|
|
else
|
2017-10-04 07:12:01 +08:00
|
|
|
K++;
|
2015-10-27 03:58:29 +08:00
|
|
|
}
|
|
|
|
|
2017-10-04 07:12:01 +08:00
|
|
|
multikeySort(Vec.slice(0, I), Pos);
|
|
|
|
multikeySort(Vec.slice(J), Pos);
|
|
|
|
|
|
|
|
// multikeySort(Vec.slice(I, J - I), Pos + 1), but with
|
|
|
|
// tail call optimization.
|
2015-10-27 03:58:29 +08:00
|
|
|
if (Pivot != -1) {
|
2017-10-04 07:12:01 +08:00
|
|
|
Vec = Vec.slice(I, J - I);
|
2015-10-27 03:58:29 +08:00
|
|
|
++Pos;
|
|
|
|
goto tailcall;
|
2014-09-25 04:37:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 05:48:05 +08:00
|
|
|
void StringTableBuilder::finalize() {
|
2018-02-07 04:29:21 +08:00
|
|
|
assert(K != DWARF);
|
2016-01-29 08:49:42 +08:00
|
|
|
finalizeStringTable(/*Optimize=*/true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::finalizeInOrder() {
|
|
|
|
finalizeStringTable(/*Optimize=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::finalizeStringTable(bool Optimize) {
|
2016-10-05 06:43:25 +08:00
|
|
|
Finalized = true;
|
2014-05-01 00:25:02 +08:00
|
|
|
|
2016-10-05 06:43:25 +08:00
|
|
|
if (Optimize) {
|
|
|
|
std::vector<StringPair *> Strings;
|
|
|
|
Strings.reserve(StringIndexMap.size());
|
|
|
|
for (StringPair &P : StringIndexMap)
|
|
|
|
Strings.push_back(&P);
|
2014-05-01 00:25:02 +08:00
|
|
|
|
2017-10-04 07:12:01 +08:00
|
|
|
multikeySort(Strings, 0);
|
2016-10-05 06:43:25 +08: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;
|
|
|
|
}
|
|
|
|
}
|
2016-01-29 08:49:42 +08:00
|
|
|
|
2016-10-05 06:43:25 +08:00
|
|
|
Size = alignTo(Size, Alignment);
|
|
|
|
P->second = Size;
|
2014-09-30 06:43:20 +08:00
|
|
|
|
2016-10-05 06:43:25 +08:00
|
|
|
Size += S.size();
|
|
|
|
if (K != RAW)
|
|
|
|
++Size;
|
|
|
|
Previous = S;
|
|
|
|
}
|
2014-09-30 06:43:20 +08:00
|
|
|
}
|
2015-10-24 05:48:05 +08:00
|
|
|
|
2016-10-05 06:43:25 +08:00
|
|
|
if (K == MachO)
|
|
|
|
Size = alignTo(Size, 4); // Pad to multiple of 4.
|
2019-03-22 18:28:56 +08:00
|
|
|
|
|
|
|
// The first byte in an ELF string table must be null, according to the ELF
|
|
|
|
// specification. In 'initSize()' we reserved the first byte to hold null for
|
|
|
|
// this purpose and here we actually add the string to allow 'getOffset()' to
|
|
|
|
// be called on an empty string.
|
|
|
|
if (K == ELF)
|
|
|
|
StringIndexMap[CachedHashStringRef("")] = 0;
|
2014-09-30 06:43:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::clear() {
|
2016-10-05 06:43:25 +08:00
|
|
|
Finalized = false;
|
2014-09-30 06:43:20 +08:00
|
|
|
StringIndexMap.clear();
|
2014-05-01 00:25:02 +08:00
|
|
|
}
|
2015-10-23 02:32:06 +08:00
|
|
|
|
2016-10-18 06:24:36 +08:00
|
|
|
size_t StringTableBuilder::getOffset(CachedHashStringRef S) const {
|
2015-10-23 02:32:06 +08:00
|
|
|
assert(isFinalized());
|
2015-10-24 04:15:35 +08:00
|
|
|
auto I = StringIndexMap.find(S);
|
2015-10-23 02:32:06 +08:00
|
|
|
assert(I != StringIndexMap.end() && "String is not in table!");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2016-10-18 06:24:36 +08:00
|
|
|
size_t StringTableBuilder::add(CachedHashStringRef S) {
|
2016-10-05 06:43:25 +08:00
|
|
|
if (K == WinCOFF)
|
|
|
|
assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
|
|
|
|
|
2015-10-23 02:32:06 +08:00
|
|
|
assert(!isFinalized());
|
2016-10-15 01:01:39 +08:00
|
|
|
auto P = StringIndexMap.insert(std::make_pair(S, 0));
|
|
|
|
if (P.second) {
|
|
|
|
size_t Start = alignTo(Size, Alignment);
|
|
|
|
P.first->second = Start;
|
2016-02-19 22:13:52 +08:00
|
|
|
Size = Start + S.size() + (K != RAW);
|
2016-10-15 01:01:39 +08:00
|
|
|
}
|
2015-10-24 05:48:05 +08:00
|
|
|
return P.first->second;
|
2015-10-23 02:32:06 +08:00
|
|
|
}
|