1 //===- NonRelocatableStringpool.h -------------------------------*- 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_CODEGEN_NONRELOCATABLESTRINGPOOL_H 10 #define LLVM_CODEGEN_NONRELOCATABLESTRINGPOOL_H 11 12 #include "llvm/CodeGen/DwarfStringPoolEntry.h" 13 #include "llvm/Support/Allocator.h" 14 #include "llvm/Support/Compiler.h" 15 #include <cstdint> 16 #include <vector> 17 18 namespace llvm { 19 20 /// A string table that doesn't need relocations. 21 /// 22 /// Use this class when a string table doesn't need relocations. 23 /// This class provides this ability by just associating offsets with strings. 24 class NonRelocatableStringpool { 25 public: 26 /// Entries are stored into the StringMap and simply linked together through 27 /// the second element of this pair in order to keep track of insertion 28 /// order. 29 using MapTy = StringMap<DwarfStringPoolEntry, BumpPtrAllocator>; 30 31 NonRelocatableStringpool(bool PutEmptyString = false) { 32 if (PutEmptyString) 33 getEntry(""); 34 } 35 36 LLVM_ABI DwarfStringPoolEntryRef getEntry(StringRef S); 37 38 /// Get the offset of string \p S in the string table. This can insert a new 39 /// element or return the offset of a pre-existing one. getStringOffset(StringRef S)40 uint64_t getStringOffset(StringRef S) { return getEntry(S).getOffset(); } 41 42 /// Get permanent storage for \p S (but do not necessarily emit \p S in the 43 /// output section). A latter call to getStringOffset() with the same string 44 /// will chain it though. 45 /// 46 /// \returns The StringRef that points to permanent storage to use 47 /// in place of \p S. 48 LLVM_ABI StringRef internString(StringRef S); 49 getSize()50 uint64_t getSize() { return CurrentEndOffset; } 51 52 /// Return the list of strings to be emitted. This does not contain the 53 /// strings which were added via internString only. 54 LLVM_ABI std::vector<DwarfStringPoolEntryRef> getEntriesForEmission() const; 55 56 private: 57 MapTy Strings; 58 uint64_t CurrentEndOffset = 0; 59 unsigned NumEntries = 0; 60 }; 61 62 /// Helper for making strong types. 63 template <typename T, typename S> class StrongType : public T { 64 public: 65 template <typename... Args> StrongType(Args...A)66 explicit StrongType(Args... A) : T(std::forward<Args>(A)...) {} 67 }; 68 69 /// It's very easy to introduce bugs by passing the wrong string pool. 70 /// By using strong types the interface enforces that the right 71 /// kind of pool is used. 72 struct UniqueTag {}; 73 struct OffsetsTag {}; 74 using UniquingStringPool = StrongType<NonRelocatableStringpool, UniqueTag>; 75 using OffsetsStringPool = StrongType<NonRelocatableStringpool, OffsetsTag>; 76 77 } // end namespace llvm 78 79 #endif // LLVM_CODEGEN_NONRELOCATABLESTRINGPOOL_H 80