2014-04-30 16:25:02 +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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-07-03 02:01:39 +00:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
2015-10-22 15:15:44 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2014-09-29 22:43:20 +00:00
|
|
|
#include "llvm/Support/COFF.h"
|
|
|
|
#include "llvm/Support/Endian.h"
|
2014-04-30 16:25:02 +00:00
|
|
|
|
2015-10-22 16:42:31 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2014-04-30 16:25:02 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2015-10-22 18:32:06 +00:00
|
|
|
static int compareBySuffix(std::pair<StringRef, size_t> *const *AP,
|
|
|
|
std::pair<StringRef, size_t> *const *BP) {
|
2015-10-23 20:15:35 +00:00
|
|
|
StringRef A = (*AP)->first;
|
|
|
|
StringRef B = (*BP)->first;
|
|
|
|
size_t SizeA = A.size();
|
|
|
|
size_t SizeB = B.size();
|
|
|
|
size_t Len = std::min(SizeA, SizeB);
|
|
|
|
for (size_t I = 0; I < Len; ++I) {
|
|
|
|
char CA = A[SizeA - I - 1];
|
|
|
|
char CB = B[SizeB - I - 1];
|
|
|
|
if (CA != CB)
|
|
|
|
return CB - CA;
|
2014-09-24 20:37:14 +00:00
|
|
|
}
|
2015-10-23 20:15:35 +00:00
|
|
|
return SizeB - SizeA;
|
2014-09-24 20:37:14 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 20:15:35 +00:00
|
|
|
void StringTableBuilder::finalize(Kind K) {
|
2015-10-22 18:32:06 +00:00
|
|
|
std::vector<std::pair<StringRef, size_t> *> Strings;
|
2014-09-29 22:43:20 +00:00
|
|
|
Strings.reserve(StringIndexMap.size());
|
2015-10-22 18:32:06 +00:00
|
|
|
for (std::pair<StringRef, size_t> &P : StringIndexMap)
|
2015-10-22 15:26:35 +00:00
|
|
|
Strings.push_back(&P);
|
2014-04-30 16:25:02 +00:00
|
|
|
|
2015-10-22 15:15:44 +00:00
|
|
|
array_pod_sort(Strings.begin(), Strings.end(), compareBySuffix);
|
2014-04-30 16:25:02 +00:00
|
|
|
|
2015-10-23 20:15:35 +00:00
|
|
|
switch (K) {
|
2014-10-06 17:05:19 +00:00
|
|
|
case ELF:
|
|
|
|
case MachO:
|
2014-09-29 22:43:20 +00:00
|
|
|
// Start the table with a NUL byte.
|
|
|
|
StringTable += '\x00';
|
2014-10-06 17:05:19 +00:00
|
|
|
break;
|
|
|
|
case WinCOFF:
|
2014-09-29 22:43:20 +00:00
|
|
|
// Make room to write the table size later.
|
|
|
|
StringTable.append(4, '\x00');
|
2014-10-06 17:05:19 +00:00
|
|
|
break;
|
2014-09-29 22:43:20 +00:00
|
|
|
}
|
2014-04-30 16:25:02 +00:00
|
|
|
|
|
|
|
StringRef Previous;
|
2015-10-22 18:32:06 +00:00
|
|
|
for (std::pair<StringRef, size_t> *P : Strings) {
|
2015-10-23 20:15:35 +00:00
|
|
|
StringRef S = P->first;
|
|
|
|
if (K == WinCOFF)
|
|
|
|
assert(S.size() > COFF::NameSize && "Short string in COFF string table!");
|
2014-09-29 22:43:20 +00:00
|
|
|
|
2015-10-23 20:15:35 +00:00
|
|
|
if (Previous.endswith(S)) {
|
|
|
|
P->second = StringTable.size() - 1 - S.size();
|
2014-04-30 16:25:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-10-22 15:26:35 +00:00
|
|
|
P->second = StringTable.size();
|
2015-10-23 20:15:35 +00:00
|
|
|
StringTable += S;
|
2014-04-30 16:25:02 +00:00
|
|
|
StringTable += '\x00';
|
2015-10-23 20:15:35 +00:00
|
|
|
Previous = S;
|
2014-04-30 16:25:02 +00:00
|
|
|
}
|
2014-09-29 22:43:20 +00:00
|
|
|
|
2015-10-23 20:15:35 +00:00
|
|
|
switch (K) {
|
2014-10-06 17:05:19 +00:00
|
|
|
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.
|
2014-09-29 22:43:20 +00:00
|
|
|
assert(StringTable.size() <= std::numeric_limits<uint32_t>::max());
|
2015-10-23 20:15:35 +00:00
|
|
|
uint32_t Size = static_cast<uint32_t>(StringTable.size());
|
2014-09-29 22:43:20 +00:00
|
|
|
support::endian::write<uint32_t, support::little, support::unaligned>(
|
2015-10-23 20:15:35 +00:00
|
|
|
StringTable.data(), Size);
|
2014-10-06 17:05:19 +00:00
|
|
|
break;
|
2014-09-29 22:43:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringTableBuilder::clear() {
|
|
|
|
StringTable.clear();
|
|
|
|
StringIndexMap.clear();
|
2014-04-30 16:25:02 +00:00
|
|
|
}
|
2015-10-22 18:32:06 +00:00
|
|
|
|
2015-10-23 20:15:35 +00:00
|
|
|
size_t StringTableBuilder::getOffset(StringRef S) const {
|
2015-10-22 18:32:06 +00:00
|
|
|
assert(isFinalized());
|
2015-10-23 20:15:35 +00:00
|
|
|
auto I = StringIndexMap.find(S);
|
2015-10-22 18:32:06 +00:00
|
|
|
assert(I != StringIndexMap.end() && "String is not in table!");
|
|
|
|
return I->second;
|
|
|
|
}
|
|
|
|
|
2015-10-23 20:15:35 +00:00
|
|
|
void StringTableBuilder::add(StringRef S) {
|
2015-10-22 18:32:06 +00:00
|
|
|
assert(!isFinalized());
|
2015-10-23 20:15:35 +00:00
|
|
|
StringIndexMap.insert(std::make_pair(S, 0));
|
2015-10-22 18:32:06 +00:00
|
|
|
}
|