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: |
| 25 | enum Kind { ELF, WinCOFF, MachO, RAW, DWARF }; |
| 26 | |
| 27 | private: |
| 28 | DenseMap<CachedHashStringRef, size_t> StringIndexMap; |
| 29 | size_t Size = 0; |
| 30 | Kind K; |
| 31 | unsigned Alignment; |
| 32 | bool Finalized = false; |
| 33 | |
| 34 | void finalizeStringTable(bool Optimize); |
| 35 | void initSize(); |
| 36 | |
| 37 | public: |
| 38 | StringTableBuilder(Kind K, unsigned Alignment = 1); |
| 39 | ~StringTableBuilder(); |
| 40 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 41 | /// 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] | 42 | /// table. The position will be changed if finalize is used. |
| 43 | /// Can only be used before the table is finalized. |
| 44 | size_t add(CachedHashStringRef S); |
| 45 | size_t add(StringRef S) { return add(CachedHashStringRef(S)); } |
| 46 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 47 | /// Analyze the strings and build the final table. No more strings can |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 48 | /// be added after this point. |
| 49 | void finalize(); |
| 50 | |
| 51 | /// Finalize the string table without reording it. In this mode, offsets |
| 52 | /// returned by add will still be valid. |
| 53 | void finalizeInOrder(); |
| 54 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 55 | /// 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] | 56 | /// after the table is finalized. |
| 57 | size_t getOffset(CachedHashStringRef S) const; |
| 58 | size_t getOffset(StringRef S) const { |
| 59 | return getOffset(CachedHashStringRef(S)); |
| 60 | } |
| 61 | |
| 62 | size_t getSize() const { return Size; } |
| 63 | void clear(); |
| 64 | |
| 65 | void write(raw_ostream &OS) const; |
| 66 | void write(uint8_t *Buf) const; |
| 67 | |
| 68 | private: |
| 69 | bool isFinalized() const { return Finalized; } |
| 70 | }; |
| 71 | |
| 72 | } // end namespace llvm |
| 73 | |
| 74 | #endif // LLVM_MC_STRINGTABLEBUILDER_H |