blob: 7d41176b56197ec24fbd23bd17aeb2bbcdec986e [file] [log] [blame]
David Blaikie018cdc82016-05-23 16:32:11 +00001#ifndef TOOLS_LLVM_DWP_DWPSTRINGPOOL
2#define TOOLS_LLVM_DWP_DWPSTRINGPOOL
3
4#include "llvm/ADT/DenseMap.h"
5#include "llvm/MC/MCSection.h"
6#include "llvm/MC/MCStreamer.h"
7#include <cassert>
8
David Blaikie5131c262016-05-23 16:45:00 +00009namespace llvm {
David Blaikie018cdc82016-05-23 16:32:11 +000010class DWPStringPool {
11
12 struct CStrDenseMapInfo {
13 static inline const char *getEmptyKey() {
14 return reinterpret_cast<const char *>(~static_cast<uintptr_t>(0));
15 }
16 static inline const char *getTombstoneKey() {
17 return reinterpret_cast<const char *>(~static_cast<uintptr_t>(1));
18 }
19 static unsigned getHashValue(const char *Val) {
20 assert(Val != getEmptyKey() && "Cannot hash the empty key!");
21 assert(Val != getTombstoneKey() && "Cannot hash the tombstone key!");
22 return (unsigned)hash_value(StringRef(Val));
23 }
24 static bool isEqual(const char *LHS, const char *RHS) {
25 if (RHS == getEmptyKey())
26 return LHS == getEmptyKey();
27 if (RHS == getTombstoneKey())
28 return LHS == getTombstoneKey();
29 return strcmp(LHS, RHS) == 0;
30 }
31 };
32
33 MCStreamer &Out;
34 MCSection *Sec;
35 DenseMap<const char *, uint32_t, CStrDenseMapInfo> Pool;
36 uint32_t Offset = 0;
37
38public:
39 DWPStringPool(MCStreamer &Out, MCSection *Sec) : Out(Out), Sec(Sec) {}
40
41 uint32_t getOffset(const char *Str, unsigned Length) {
42 assert(strlen(Str) + 1 == Length && "Ensure length hint is correct");
43
44 auto Pair = Pool.insert(std::make_pair(Str, Offset));
45 if (Pair.second) {
46 Out.SwitchSection(Sec);
47 Out.EmitBytes(StringRef(Str, Length));
48 Offset += Length;
49 }
50
51 return Pair.first->second;
52 }
53};
David Blaikie5131c262016-05-23 16:45:00 +000054}
David Blaikie018cdc82016-05-23 16:32:11 +000055
56#endif