Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- StringTableBuilder.h - String table building utility -----*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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 <cstddef> |
| 16 | #include <cstdint> |
| 17 | |
| 18 | namespace llvm { |
| 19 | |
| 20 | class raw_ostream; |
| 21 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 22 | /// Utility for building string tables with deduplicated suffixes. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 23 | class StringTableBuilder { |
| 24 | public: |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 25 | enum Kind { |
| 26 | ELF, |
| 27 | WinCOFF, |
| 28 | MachO, |
| 29 | MachO64, |
| 30 | MachOLinked, |
| 31 | MachO64Linked, |
| 32 | RAW, |
| 33 | DWARF, |
| 34 | XCOFF |
| 35 | }; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 36 | |
| 37 | private: |
| 38 | DenseMap<CachedHashStringRef, size_t> StringIndexMap; |
| 39 | size_t Size = 0; |
| 40 | Kind K; |
| 41 | unsigned Alignment; |
| 42 | bool Finalized = false; |
| 43 | |
| 44 | void finalizeStringTable(bool Optimize); |
| 45 | void initSize(); |
| 46 | |
| 47 | public: |
| 48 | StringTableBuilder(Kind K, unsigned Alignment = 1); |
| 49 | ~StringTableBuilder(); |
| 50 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 51 | /// Add a string to the builder. Returns the position of S in the |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 52 | /// table. The position will be changed if finalize is used. |
| 53 | /// Can only be used before the table is finalized. |
| 54 | size_t add(CachedHashStringRef S); |
| 55 | size_t add(StringRef S) { return add(CachedHashStringRef(S)); } |
| 56 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 57 | /// Analyze the strings and build the final table. No more strings can |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 58 | /// be added after this point. |
| 59 | void finalize(); |
| 60 | |
| 61 | /// Finalize the string table without reording it. In this mode, offsets |
| 62 | /// returned by add will still be valid. |
| 63 | void finalizeInOrder(); |
| 64 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 65 | /// Get the offest of a string in the string table. Can only be used |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 66 | /// after the table is finalized. |
| 67 | size_t getOffset(CachedHashStringRef S) const; |
| 68 | size_t getOffset(StringRef S) const { |
| 69 | return getOffset(CachedHashStringRef(S)); |
| 70 | } |
| 71 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 72 | /// Check if a string is contained in the string table. Since this class |
| 73 | /// doesn't store the string values, this function can be used to check if |
| 74 | /// storage needs to be done prior to adding the string. |
| 75 | bool contains(StringRef S) const { |
| 76 | return contains(CachedHashStringRef(S)); |
| 77 | } |
| 78 | bool contains(CachedHashStringRef S) const { |
| 79 | return StringIndexMap.count(S); |
| 80 | } |
| 81 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 82 | size_t getSize() const { return Size; } |
| 83 | void clear(); |
| 84 | |
| 85 | void write(raw_ostream &OS) const; |
| 86 | void write(uint8_t *Buf) const; |
| 87 | |
| 88 | private: |
| 89 | bool isFinalized() const { return Finalized; } |
| 90 | }; |
| 91 | |
| 92 | } // end namespace llvm |
| 93 | |
| 94 | #endif // LLVM_MC_STRINGTABLEBUILDER_H |