1 //===- StringTableBuilder.h - String table building utility -----*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_MC_STRINGTABLEBUILDER_H 10 #define LLVM_MC_STRINGTABLEBUILDER_H 11 12 #include "llvm/ADT/CachedHashString.h" 13 #include "llvm/ADT/DenseMap.h" 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/Support/Alignment.h" 16 #include "llvm/Support/Compiler.h" 17 #include <cstddef> 18 #include <cstdint> 19 20 namespace llvm { 21 22 class raw_ostream; 23 24 /// Utility for building string tables with deduplicated suffixes. 25 class StringTableBuilder { 26 public: 27 enum Kind { 28 ELF, 29 WinCOFF, 30 MachO, 31 MachO64, 32 MachOLinked, 33 MachO64Linked, 34 RAW, 35 DWARF, 36 XCOFF, 37 DXContainer 38 }; 39 40 private: 41 // Only non-zero priority will be recorded. 42 DenseMap<CachedHashStringRef, uint8_t> StringPriorityMap; 43 DenseMap<CachedHashStringRef, size_t> StringIndexMap; 44 size_t Size = 0; 45 Kind K; 46 Align Alignment; 47 bool Finalized = false; 48 49 void finalizeStringTable(bool Optimize); 50 void initSize(); 51 52 public: 53 LLVM_ABI StringTableBuilder(Kind K, Align Alignment = Align(1)); 54 LLVM_ABI ~StringTableBuilder(); 55 56 /// Add a string to the builder. Returns the position of S in the table. The 57 /// position will be changed if finalize is used. Can only be used before the 58 /// table is finalized. Priority is only useful with reordering. Strings with 59 /// the same priority will be put together. Strings with higher priority are 60 /// placed closer to the begin of string table. When adding same string with 61 /// different priority, the maximum priority win. 62 LLVM_ABI size_t add(CachedHashStringRef S, uint8_t Priority = 0); 63 size_t add(StringRef S, uint8_t Priority = 0) { 64 return add(CachedHashStringRef(S), Priority); 65 } 66 67 /// Analyze the strings and build the final table. No more strings can 68 /// be added after this point. 69 LLVM_ABI void finalize(); 70 71 /// Finalize the string table without reording it. In this mode, offsets 72 /// returned by add will still be valid. 73 LLVM_ABI void finalizeInOrder(); 74 75 /// Get the offest of a string in the string table. Can only be used 76 /// after the table is finalized. 77 LLVM_ABI size_t getOffset(CachedHashStringRef S) const; getOffset(StringRef S)78 size_t getOffset(StringRef S) const { 79 return getOffset(CachedHashStringRef(S)); 80 } 81 82 /// Check if a string is contained in the string table. Since this class 83 /// doesn't store the string values, this function can be used to check if 84 /// storage needs to be done prior to adding the string. contains(StringRef S)85 bool contains(StringRef S) const { return contains(CachedHashStringRef(S)); } contains(CachedHashStringRef S)86 bool contains(CachedHashStringRef S) const { return StringIndexMap.count(S); } 87 empty()88 bool empty() const { return StringIndexMap.empty(); } getSize()89 size_t getSize() const { return Size; } 90 LLVM_ABI void clear(); 91 92 LLVM_ABI void write(raw_ostream &OS) const; 93 LLVM_ABI void write(uint8_t *Buf) const; 94 isFinalized()95 bool isFinalized() const { return Finalized; } 96 }; 97 98 } // end namespace llvm 99 100 #endif // LLVM_MC_STRINGTABLEBUILDER_H 101