2017-02-11 01:27:28 +01:00
|
|
|
//===- StringTableBuilder.cpp - String table building utility -------------===//
|
2014-04-30 18:25:02 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01: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-04-30 18:25:02 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
2017-02-11 01:27:28 +01:00
|
|
|
#include "llvm/ADT/CachedHashString.h"
|
2016-10-18 00:24:36 +02:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2017-02-11 01:27:28 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-06-07 05:48:56 +02:00
|
|
|
#include "llvm/BinaryFormat/COFF.h"
|
2014-09-30 00:43:20 +02:00
|
|
|
#include "llvm/Support/Endian.h"
|
2017-02-11 01:27:28 +01:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2016-10-05 00:43:25 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-11 01:27:28 +01:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
|
|
|
#include <utility>
|
2015-10-22 18:42:31 +02:00
|
|
|
#include <vector>
|
|
|
|
|
2014-04-30 18:25:02 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-02-11 01:27:28 +01:00
|
|
|
StringTableBuilder::~StringTableBuilder() = default;
|
2016-10-05 00:43:25 +02:00
|
|
|
|
|
|
|
void StringTableBuilder::initSize() {
|
2016-01-29 01:49:42 +01:00
|
|
|
// Account for leading bytes in table so that offsets returned from add are
|
|
|
|
// correct.
|
|
|
|
switch (K) {
|
|
|
|
case RAW:
|
2018-02-06 21:29:21 +01:00
|
|
|
case DWARF:
|
2016-01-29 01:49:42 +01:00
|
|
|
Size = 0;
|
|
|
|
break;
|
|
|
|
case MachO:
|
|
|
|
case ELF:
|
2016-10-05 00:43:25 +02:00
|
|
|
// Start the table with a NUL byte.
|
2016-01-29 01:49:42 +01:00
|
|
|
Size = 1;
|
|
|
|
break;
|
|
|
|
case WinCOFF:
|
2016-10-05 00:43:25 +02:00
|
|
|
// Make room to write the table size later.
|
2016-01-29 01:49:42 +01:00
|
|
|
Size = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-10-23 23:48:05 +02:00
|
|
|
|
2016-10-05 00:43:25 +02: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 02:10:17 +02:00
|
|
|
write((uint8_t *)Data.data());
|
2016-10-05 00:43:25 +02:00
|
|
|
OS << Data;
|
|
|
|
}
|
|
|
|
|
2017-04-27 00:31:39 +02:00
|
|
|
using StringPair = std::pair<CachedHashStringRef, size_t>;
|
2016-10-05 00:43:25 +02:00
|
|
|
|
|
|
|
void StringTableBuilder::write(uint8_t *Buf) const {
|
|
|
|
assert(isFinalized());
|
|
|
|
for (const StringPair &P : StringIndexMap) {
|
|
|
|
StringRef Data = P.first.val();
|
2016-10-05 18:33:03 +02:00
|
|
|
if (!Data.empty())
|
|
|
|
memcpy(Buf + P.second, Data.data(), Data.size());
|
2016-10-05 00:43:25 +02:00
|
|
|
}
|
|
|
|
if (K != WinCOFF)
|
|
|
|
return;
|
|
|
|
support::endian::write32le(Buf, Size);
|
|
|
|
}
|
2015-10-26 20:58:29 +01:00
|
|
|
|
|
|
|
// Returns the character at Pos from end of a string.
|
|
|
|
static int charTailAt(StringPair *P, size_t Pos) {
|
2016-10-05 00:43:25 +02:00
|
|
|
StringRef S = P->first.val();
|
2015-10-26 20:58:29 +01: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 01:12:01 +02:00
|
|
|
static void multikeySort(MutableArrayRef<StringPair *> Vec, int Pos) {
|
2015-10-26 20:58:29 +01:00
|
|
|
tailcall:
|
2017-10-04 01:12:01 +02:00
|
|
|
if (Vec.size() <= 1)
|
2015-10-26 20:58:29 +01:00
|
|
|
return;
|
|
|
|
|
2017-10-04 01:12:01 +02: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-26 20:58:29 +01:00
|
|
|
if (C > Pivot)
|
2017-10-04 01:12:01 +02:00
|
|
|
std::swap(Vec[I++], Vec[K++]);
|
2015-10-26 20:58:29 +01:00
|
|
|
else if (C < Pivot)
|
2017-10-04 01:12:01 +02:00
|
|
|
std::swap(Vec[--J], Vec[K]);
|
2015-10-26 20:58:29 +01:00
|
|
|
else
|
2017-10-04 01:12:01 +02:00
|
|
|
K++;
|
2015-10-26 20:58:29 +01:00
|
|
|
}
|
|
|
|
|
2017-10-04 01:12:01 +02: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-26 20:58:29 +01:00
|
|
|
if (Pivot != -1) {
|
2017-10-04 01:12:01 +02:00
|
|
|
Vec = Vec.slice(I, J - I);
|
2015-10-26 20:58:29 +01:00
|
|
|
++Pos;
|
|
|
|
goto tailcall;
|
2014-09-24 22:37:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-23 23:48:05 +02:00
|
|
|
void StringTableBuilder::finalize() {
|
2018-02-06 21:29:21 +01:00
|
|
|
assert(K != DWARF);
|
2016-01-29 01:49:42 +01:00
|
|
|
finalizeStringTable(/*Optimize=*/true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::finalizeInOrder() {
|
|
|
|
finalizeStringTable(/*Optimize=*/false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::finalizeStringTable(bool Optimize) {
|
2016-10-05 00:43:25 +02:00
|
|
|
Finalized = true;
|
2014-04-30 18:25:02 +02:00
|
|
|
|
2016-10-05 00:43:25 +02:00
|
|
|
if (Optimize) {
|
|
|
|
std::vector<StringPair *> Strings;
|
|
|
|
Strings.reserve(StringIndexMap.size());
|
|
|
|
for (StringPair &P : StringIndexMap)
|
|
|
|
Strings.push_back(&P);
|
2014-04-30 18:25:02 +02:00
|
|
|
|
2017-10-04 01:12:01 +02:00
|
|
|
multikeySort(Strings, 0);
|
2016-10-05 00:43:25 +02: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 01:49:42 +01:00
|
|
|
|
2016-10-05 00:43:25 +02:00
|
|
|
Size = alignTo(Size, Alignment);
|
|
|
|
P->second = Size;
|
2014-09-30 00:43:20 +02:00
|
|
|
|
2016-10-05 00:43:25 +02:00
|
|
|
Size += S.size();
|
|
|
|
if (K != RAW)
|
|
|
|
++Size;
|
|
|
|
Previous = S;
|
|
|
|
}
|
2014-09-30 00:43:20 +02:00
|
|
|
}
|
2015-10-23 23:48:05 +02:00
|
|
|
|
2016-10-05 00:43:25 +02:00
|
|
|
if (K == MachO)
|
|
|
|
Size = alignTo(Size, 4); // Pad to multiple of 4.
|
2019-03-22 11:28:56 +01: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 00:43:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::clear() {
|
2016-10-05 00:43:25 +02:00
|
|
|
Finalized = false;
|
2014-09-30 00:43:20 +02:00
|
|
|
StringIndexMap.clear();
|
2014-04-30 18:25:02 +02:00
|
|
|
}
|
2015-10-22 20:32:06 +02:00
|
|
|
|
2016-10-18 00:24:36 +02:00
|
|
|
size_t StringTableBuilder::getOffset(CachedHashStringRef 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;
|
|
|
|
}
|
|
|
|
|
2016-10-18 00:24:36 +02:00
|
|
|
size_t StringTableBuilder::add(CachedHashStringRef S) {
|
2016-10-05 00:43:25 +02:00
|
|
|
if (K == WinCOFF)
|
|
|
|
assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
|
|
|
|
|
2015-10-22 20:32:06 +02:00
|
|
|
assert(!isFinalized());
|
2016-10-14 19:01:39 +02: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 15:13:52 +01:00
|
|
|
Size = Start + S.size() + (K != RAW);
|
2016-10-14 19:01:39 +02:00
|
|
|
}
|
2015-10-23 23:48:05 +02:00
|
|
|
return P.first->second;
|
2015-10-22 20:32:06 +02:00
|
|
|
}
|