summaryrefslogtreecommitdiffstats
path: root/contrib/llvm/lib/MC/StringTableBuilder.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/llvm/lib/MC/StringTableBuilder.cpp')
-rw-r--r--contrib/llvm/lib/MC/StringTableBuilder.cpp159
1 files changed, 76 insertions, 83 deletions
diff --git a/contrib/llvm/lib/MC/StringTableBuilder.cpp b/contrib/llvm/lib/MC/StringTableBuilder.cpp
index 9d95952..1a501bc 100644
--- a/contrib/llvm/lib/MC/StringTableBuilder.cpp
+++ b/contrib/llvm/lib/MC/StringTableBuilder.cpp
@@ -9,15 +9,18 @@
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallString.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) {
+StringTableBuilder::~StringTableBuilder() {}
+
+void StringTableBuilder::initSize() {
// Account for leading bytes in table so that offsets returned from add are
// correct.
switch (K) {
@@ -26,19 +29,46 @@ StringTableBuilder::StringTableBuilder(Kind K, unsigned Alignment)
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<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);
+}
// 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];
@@ -86,106 +116,69 @@ void StringTableBuilder::finalizeInOrder() {
}
void StringTableBuilder::finalizeStringTable(bool Optimize) {
- typedef std::pair<CachedHash<StringRef>, size_t> StringOffsetPair;
- std::vector<StringOffsetPair *> Strings;
- Strings.reserve(StringIndexMap.size());
- for (StringOffsetPair &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 StringOffsetPair *LHS, const StringOffsetPair *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 (StringOffsetPair *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::getOffset(StringRef S) const {
+size_t StringTableBuilder::getOffset(CachedHashStringRef S) const {
assert(isFinalized());
auto I = StringIndexMap.find(S);
assert(I != StringIndexMap.end() && "String is not in table!");
return I->second;
}
-size_t StringTableBuilder::add(StringRef S) {
+size_t StringTableBuilder::add(CachedHashStringRef 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));
- if (P.second)
+ auto P = StringIndexMap.insert(std::make_pair(S, 0));
+ if (P.second) {
+ size_t Start = alignTo(Size, Alignment);
+ P.first->second = Start;
Size = Start + S.size() + (K != RAW);
+ }
return P.first->second;
}
OpenPOWER on IntegriCloud