1 //===- GlobalTypeTableBuilder.cpp -----------------------------------------===//
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 #include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/DenseSet.h"
12 #include "llvm/ADT/STLExtras.h"
13 #include "llvm/DebugInfo/CodeView/CodeView.h"
14 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
15 #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
16 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
17 #include "llvm/Support/Allocator.h"
18 #include "llvm/Support/BinaryByteStream.h"
19 #include "llvm/Support/BinaryStreamWriter.h"
20 #include "llvm/Support/Endian.h"
21 #include "llvm/Support/Error.h"
22 #include <algorithm>
23 #include <cassert>
24 #include <cstdint>
25 #include <cstring>
26
27 using namespace llvm;
28 using namespace llvm::codeview;
29
nextTypeIndex() const30 TypeIndex GlobalTypeTableBuilder::nextTypeIndex() const {
31 return TypeIndex::fromArrayIndex(SeenRecords.size());
32 }
33
GlobalTypeTableBuilder(BumpPtrAllocator & Storage)34 GlobalTypeTableBuilder::GlobalTypeTableBuilder(BumpPtrAllocator &Storage)
35 : RecordStorage(Storage) {
36 SeenRecords.reserve(4096);
37 }
38
39 GlobalTypeTableBuilder::~GlobalTypeTableBuilder() = default;
40
getFirst()41 Optional<TypeIndex> GlobalTypeTableBuilder::getFirst() {
42 if (empty())
43 return None;
44
45 return TypeIndex(TypeIndex::FirstNonSimpleIndex);
46 }
47
getNext(TypeIndex Prev)48 Optional<TypeIndex> GlobalTypeTableBuilder::getNext(TypeIndex Prev) {
49 if (++Prev == nextTypeIndex())
50 return None;
51 return Prev;
52 }
53
getType(TypeIndex Index)54 CVType GlobalTypeTableBuilder::getType(TypeIndex Index) {
55 CVType Type(SeenRecords[Index.toArrayIndex()]);
56 return Type;
57 }
58
getTypeName(TypeIndex Index)59 StringRef GlobalTypeTableBuilder::getTypeName(TypeIndex Index) {
60 llvm_unreachable("Method not implemented");
61 }
62
contains(TypeIndex Index)63 bool GlobalTypeTableBuilder::contains(TypeIndex Index) {
64 if (Index.isSimple() || Index.isNoneType())
65 return false;
66
67 return Index.toArrayIndex() < SeenRecords.size();
68 }
69
size()70 uint32_t GlobalTypeTableBuilder::size() { return SeenRecords.size(); }
71
capacity()72 uint32_t GlobalTypeTableBuilder::capacity() { return SeenRecords.size(); }
73
records() const74 ArrayRef<ArrayRef<uint8_t>> GlobalTypeTableBuilder::records() const {
75 return SeenRecords;
76 }
77
hashes() const78 ArrayRef<GloballyHashedType> GlobalTypeTableBuilder::hashes() const {
79 return SeenHashes;
80 }
81
reset()82 void GlobalTypeTableBuilder::reset() {
83 HashedRecords.clear();
84 SeenRecords.clear();
85 }
86
stabilize(BumpPtrAllocator & Alloc,ArrayRef<uint8_t> Data)87 static inline ArrayRef<uint8_t> stabilize(BumpPtrAllocator &Alloc,
88 ArrayRef<uint8_t> Data) {
89 uint8_t *Stable = Alloc.Allocate<uint8_t>(Data.size());
90 memcpy(Stable, Data.data(), Data.size());
91 return makeArrayRef(Stable, Data.size());
92 }
93
insertRecordBytes(ArrayRef<uint8_t> Record)94 TypeIndex GlobalTypeTableBuilder::insertRecordBytes(ArrayRef<uint8_t> Record) {
95 GloballyHashedType GHT =
96 GloballyHashedType::hashType(Record, SeenHashes, SeenHashes);
97 return insertRecordAs(GHT, Record.size(),
98 [Record](MutableArrayRef<uint8_t> Data) {
99 assert(Data.size() == Record.size());
100 ::memcpy(Data.data(), Record.data(), Record.size());
101 return Data;
102 });
103 }
104
105 TypeIndex
insertRecord(ContinuationRecordBuilder & Builder)106 GlobalTypeTableBuilder::insertRecord(ContinuationRecordBuilder &Builder) {
107 TypeIndex TI;
108 auto Fragments = Builder.end(nextTypeIndex());
109 assert(!Fragments.empty());
110 for (auto C : Fragments)
111 TI = insertRecordBytes(C.RecordData);
112 return TI;
113 }
114
replaceType(TypeIndex & Index,CVType Data,bool Stabilize)115 bool GlobalTypeTableBuilder::replaceType(TypeIndex &Index, CVType Data,
116 bool Stabilize) {
117 assert(Index.toArrayIndex() < SeenRecords.size() &&
118 "This function cannot be used to insert records!");
119
120 ArrayRef<uint8_t> Record = Data.data();
121 assert(Record.size() < UINT32_MAX && "Record too big");
122 assert(Record.size() % 4 == 0 &&
123 "The type record size is not a multiple of 4 bytes which will cause "
124 "misalignment in the output TPI stream!");
125
126 GloballyHashedType Hash =
127 GloballyHashedType::hashType(Record, SeenHashes, SeenHashes);
128 auto Result = HashedRecords.try_emplace(Hash, Index.toArrayIndex());
129 if (!Result.second) {
130 Index = Result.first->second;
131 return false; // The record is already there, at a different location
132 }
133
134 if (Stabilize)
135 Record = stabilize(RecordStorage, Record);
136
137 SeenRecords[Index.toArrayIndex()] = Record;
138 SeenHashes[Index.toArrayIndex()] = Hash;
139 return true;
140 }
141