Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- StringTableBuilder.h - String table building utility -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #ifndef LLVM_MC_STRINGTABLEBUILDER_H |
| 11 | #define LLVM_MC_STRINGTABLEBUILDER_H |
| 12 | |
| 13 | #include "llvm/ADT/CachedHashString.h" |
| 14 | #include "llvm/ADT/DenseMap.h" |
| 15 | #include "llvm/ADT/StringRef.h" |
| 16 | #include <cstddef> |
| 17 | #include <cstdint> |
| 18 | |
| 19 | namespace llvm { |
| 20 | |
| 21 | class raw_ostream; |
| 22 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 23 | /// Utility for building string tables with deduplicated suffixes. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 24 | class StringTableBuilder { |
| 25 | public: |
| 26 | enum Kind { ELF, WinCOFF, MachO, RAW, DWARF }; |
| 27 | |
| 28 | private: |
| 29 | DenseMap<CachedHashStringRef, size_t> StringIndexMap; |
| 30 | size_t Size = 0; |
| 31 | Kind K; |
| 32 | unsigned Alignment; |
| 33 | bool Finalized = false; |
| 34 | |
| 35 | void finalizeStringTable(bool Optimize); |
| 36 | void initSize(); |
| 37 | |
| 38 | public: |
| 39 | StringTableBuilder(Kind K, unsigned Alignment = 1); |
| 40 | ~StringTableBuilder(); |
| 41 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 42 | /// 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] | 43 | /// table. The position will be changed if finalize is used. |
| 44 | /// Can only be used before the table is finalized. |
| 45 | size_t add(CachedHashStringRef S); |
| 46 | size_t add(StringRef S) { return add(CachedHashStringRef(S)); } |
| 47 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 48 | /// Analyze the strings and build the final table. No more strings can |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 49 | /// be added after this point. |
| 50 | void finalize(); |
| 51 | |
| 52 | /// Finalize the string table without reording it. In this mode, offsets |
| 53 | /// returned by add will still be valid. |
| 54 | void finalizeInOrder(); |
| 55 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame^] | 56 | /// 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] | 57 | /// after the table is finalized. |
| 58 | size_t getOffset(CachedHashStringRef S) const; |
| 59 | size_t getOffset(StringRef S) const { |
| 60 | return getOffset(CachedHashStringRef(S)); |
| 61 | } |
| 62 | |
| 63 | size_t getSize() const { return Size; } |
| 64 | void clear(); |
| 65 | |
| 66 | void write(raw_ostream &OS) const; |
| 67 | void write(uint8_t *Buf) const; |
| 68 | |
| 69 | private: |
| 70 | bool isFinalized() const { return Finalized; } |
| 71 | }; |
| 72 | |
| 73 | } // end namespace llvm |
| 74 | |
| 75 | #endif // LLVM_MC_STRINGTABLEBUILDER_H |