1 #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 
9 namespace llvm {
10 class DWPStringPool {
11 
12   struct CStrDenseMapInfo {
getEmptyKeyCStrDenseMapInfo13     static inline const char *getEmptyKey() {
14       return reinterpret_cast<const char *>(~static_cast<uintptr_t>(0));
15     }
getTombstoneKeyCStrDenseMapInfo16     static inline const char *getTombstoneKey() {
17       return reinterpret_cast<const char *>(~static_cast<uintptr_t>(1));
18     }
getHashValueCStrDenseMapInfo19     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     }
isEqualCStrDenseMapInfo24     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 
38 public:
DWPStringPool(MCStreamer & Out,MCSection * Sec)39   DWPStringPool(MCStreamer &Out, MCSection *Sec) : Out(Out), Sec(Sec) {}
40 
getOffset(const char * Str,unsigned Length)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 };
54 }
55 
56 #endif
57