#include <cassert>
namespace llvm {
+class raw_ostream;
+
+class CachedHashString {
+ const char *P;
+ uint32_t Size;
+ uint32_t Hash;
+
+public:
+ CachedHashString(StringRef S)
+ : CachedHashString(S, DenseMapInfo<StringRef>::getHashValue(S)) {}
+ CachedHashString(StringRef S, uint32_t Hash)
+ : P(S.data()), Size(S.size()), Hash(Hash) {
+ assert(S.size() <= std::numeric_limits<uint32_t>::max());
+ }
+
+ StringRef val() const { return StringRef(P, Size); }
+ uint32_t hash() const { return Hash; }
+};
/// \brief Utility for building string tables with deduplicated suffixes.
class StringTableBuilder {
enum Kind { ELF, WinCOFF, MachO, RAW };
private:
- SmallString<256> StringTable;
- DenseMap<CachedHash<StringRef>, size_t> StringIndexMap;
+ DenseMap<CachedHashString, size_t> StringIndexMap;
size_t Size = 0;
Kind K;
unsigned Alignment;
+ bool Finalized = false;
void finalizeStringTable(bool Optimize);
+ void initSize();
public:
StringTableBuilder(Kind K, unsigned Alignment = 1);
+ ~StringTableBuilder();
/// \brief Add a string to the builder. Returns the position of S in the
/// table. The position will be changed if finalize is used.
/// returned by add will still be valid.
void finalizeInOrder();
- /// \brief Retrieve the string table data. Can only be used after the table
- /// is finalized.
- StringRef data() const {
- assert(isFinalized());
- return StringTable;
- }
-
/// \brief Get the offest of a string in the string table. Can only be used
/// after the table is finalized.
size_t getOffset(StringRef S) const;
- const DenseMap<CachedHash<StringRef>, size_t> &getMap() const {
- return StringIndexMap;
- }
-
size_t getSize() const { return Size; }
void clear();
+ void write(raw_ostream &OS) const;
+ void write(uint8_t *Buf) const;
+
private:
- bool isFinalized() const {
- return !StringTable.empty();
- }
+ bool isFinalized() const { return Finalized; }
};
} // end llvm namespace
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/COFF.h"
#include "llvm/Support/Endian.h"
+#include "llvm/Support/raw_ostream.h"
#include <vector>
using namespace llvm;
-StringTableBuilder::StringTableBuilder(Kind K, unsigned Alignment)
- : K(K), Alignment(Alignment) {
+namespace llvm {
+template <> struct DenseMapInfo<CachedHashString> {
+ static CachedHashString getEmptyKey() {
+ StringRef S = DenseMapInfo<StringRef>::getEmptyKey();
+ return {S, 0};
+ }
+ static CachedHashString getTombstoneKey() {
+ StringRef S = DenseMapInfo<StringRef>::getTombstoneKey();
+ return {S, 0};
+ }
+ static unsigned getHashValue(CachedHashString Val) {
+ assert(!isEqual(Val, getEmptyKey()) && "Cannot hash the empty key!");
+ assert(!isEqual(Val, getTombstoneKey()) &&
+ "Cannot hash the tombstone key!");
+ return Val.hash();
+ }
+ static bool isEqual(CachedHashString A, CachedHashString B) {
+ return DenseMapInfo<StringRef>::isEqual(A.val(), B.val());
+ }
+};
+}
+
+StringTableBuilder::~StringTableBuilder() {}
+
+void StringTableBuilder::initSize() {
// Account for leading bytes in table so that offsets returned from add are
// correct.
switch (K) {
break;
case MachO:
case ELF:
+ // Start the table with a NUL byte.
Size = 1;
break;
case WinCOFF:
+ // Make room to write the table size later.
Size = 4;
break;
}
}
-typedef std::pair<CachedHash<StringRef>, size_t> StringPair;
+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<CachedHashString, size_t> StringPair;
+
+void StringTableBuilder::write(uint8_t *Buf) const {
+ assert(isFinalized());
+ for (const StringPair &P : StringIndexMap) {
+ StringRef Data = P.first.val();
+ memcpy(Buf + P.second, Data.data(), Data.size());
+ }
+ if (K != WinCOFF)
+ return;
+ support::endian::write32le(Buf, Size);
+}
// Returns the character at Pos from end of a string.
static int charTailAt(StringPair *P, size_t Pos) {
- StringRef S = P->first.Val;
+ StringRef S = P->first.val();
if (Pos >= S.size())
return -1;
return (unsigned char)S[S.size() - Pos - 1];
}
void StringTableBuilder::finalizeStringTable(bool Optimize) {
- std::vector<StringPair *> Strings;
- Strings.reserve(StringIndexMap.size());
- for (StringPair &P : StringIndexMap)
- Strings.push_back(&P);
-
- 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 StringPair *LHS, const StringPair *RHS) {
- return LHS->second < RHS->second;
- });
- }
- }
+ Finalized = true;
- switch (K) {
- case RAW:
- break;
- case ELF:
- case MachO:
- // Start the table with a NUL byte.
- StringTable += '\x00';
- break;
- case WinCOFF:
- // Make room to write the table size later.
- StringTable.append(4, '\x00');
- break;
- }
+ if (Optimize) {
+ std::vector<StringPair *> Strings;
+ Strings.reserve(StringIndexMap.size());
+ for (StringPair &P : StringIndexMap)
+ Strings.push_back(&P);
- StringRef Previous;
- for (StringPair *P : Strings) {
- StringRef S = P->first.Val;
- if (K == WinCOFF)
- assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
-
- if (Optimize && Previous.endswith(S)) {
- size_t Pos = StringTable.size() - S.size() - (K != RAW);
- if (!(Pos & (Alignment - 1))) {
- P->second = Pos;
- continue;
- }
+ 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);
}
- if (Optimize) {
- size_t Start = alignTo(StringTable.size(), Alignment);
- P->second = Start;
- StringTable.append(Start - StringTable.size(), '\0');
- } else {
- assert(P->second == StringTable.size() &&
- "different strtab offset after finalization");
- }
+ 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;
+ }
+ }
- StringTable += S;
- if (K != RAW)
- StringTable += '\x00';
- Previous = S;
- }
+ Size = alignTo(Size, Alignment);
+ P->second = Size;
- switch (K) {
- case RAW:
- 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.
- assert(StringTable.size() <= std::numeric_limits<uint32_t>::max());
- uint32_t Size = static_cast<uint32_t>(StringTable.size());
- support::endian::write<uint32_t, support::little, support::unaligned>(
- StringTable.data(), Size);
- break;
+ Size += S.size();
+ if (K != RAW)
+ ++Size;
+ Previous = S;
+ }
}
- Size = StringTable.size();
+ if (K == MachO)
+ Size = alignTo(Size, 4); // Pad to multiple of 4.
}
void StringTableBuilder::clear() {
- StringTable.clear();
+ Finalized = false;
StringIndexMap.clear();
}
}
size_t StringTableBuilder::add(StringRef S) {
+ if (K == WinCOFF)
+ assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
+
assert(!isFinalized());
size_t Start = alignTo(Size, Alignment);
auto P = StringIndexMap.insert(std::make_pair(S, Start));
Expected += "foo";
Expected += '\x00';
- EXPECT_EQ(Expected, B.data());
+ SmallString<64> Data;
+ raw_svector_ostream OS(Data);
+ B.write(OS);
+
+ EXPECT_EQ(Expected, Data);
EXPECT_EQ(1U, B.getOffset("foobar"));
EXPECT_EQ(4U, B.getOffset("bar"));
EXPECT_EQ(8U, B.getOffset("foo"));
// size_field + "pygmy hippopotamus\0" + "river horse\0"
uint32_t ExpectedSize = 4 + 19 + 12;
- EXPECT_EQ(ExpectedSize, B.data().size());
+ EXPECT_EQ(ExpectedSize, B.getSize());
std::string Expected;
Expected += "river horse";
Expected += '\x00';
- EXPECT_EQ(Expected, B.data());
+ SmallString<64> Data;
+ raw_svector_ostream OS(Data);
+ B.write(OS);
+
+ EXPECT_EQ(Expected, Data);
EXPECT_EQ(4U, B.getOffset("pygmy hippopotamus"));
EXPECT_EQ(10U, B.getOffset("hippopotamus"));
EXPECT_EQ(23U, B.getOffset("river horse"));
Expected += "foobar";
Expected += '\x00';
- EXPECT_EQ(Expected, B.data());
+ SmallString<64> Data;
+ raw_svector_ostream OS(Data);
+ B.write(OS);
+
+ EXPECT_EQ(Expected, Data);
EXPECT_EQ(1U, B.getOffset("foo"));
EXPECT_EQ(5U, B.getOffset("bar"));
EXPECT_EQ(9U, B.getOffset("foobar"));