1 //===- GlobalTypeTableBuilder.cpp -----------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/CodeView/GlobalTypeTableBuilder.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/DebugInfo/CodeView/CodeView.h"
12 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
13 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
14 #include "llvm/Support/Allocator.h"
15 #include "llvm/Support/ErrorHandling.h"
21 using namespace llvm::codeview
;
23 TypeIndex
GlobalTypeTableBuilder::nextTypeIndex() const {
24 return TypeIndex::fromArrayIndex(SeenRecords
.size());
27 GlobalTypeTableBuilder::GlobalTypeTableBuilder(BumpPtrAllocator
&Storage
)
28 : RecordStorage(Storage
) {
29 SeenRecords
.reserve(4096);
32 GlobalTypeTableBuilder::~GlobalTypeTableBuilder() = default;
34 std::optional
<TypeIndex
> GlobalTypeTableBuilder::getFirst() {
38 return TypeIndex(TypeIndex::FirstNonSimpleIndex
);
41 std::optional
<TypeIndex
> GlobalTypeTableBuilder::getNext(TypeIndex Prev
) {
42 if (++Prev
== nextTypeIndex())
47 CVType
GlobalTypeTableBuilder::getType(TypeIndex Index
) {
48 CVType
Type(SeenRecords
[Index
.toArrayIndex()]);
52 StringRef
GlobalTypeTableBuilder::getTypeName(TypeIndex Index
) {
53 llvm_unreachable("Method not implemented");
56 bool GlobalTypeTableBuilder::contains(TypeIndex Index
) {
57 if (Index
.isSimple() || Index
.isNoneType())
60 return Index
.toArrayIndex() < SeenRecords
.size();
63 uint32_t GlobalTypeTableBuilder::size() { return SeenRecords
.size(); }
65 uint32_t GlobalTypeTableBuilder::capacity() { return SeenRecords
.size(); }
67 ArrayRef
<ArrayRef
<uint8_t>> GlobalTypeTableBuilder::records() const {
71 ArrayRef
<GloballyHashedType
> GlobalTypeTableBuilder::hashes() const {
75 void GlobalTypeTableBuilder::reset() {
76 HashedRecords
.clear();
80 static inline ArrayRef
<uint8_t> stabilize(BumpPtrAllocator
&Alloc
,
81 ArrayRef
<uint8_t> Data
) {
82 uint8_t *Stable
= Alloc
.Allocate
<uint8_t>(Data
.size());
83 memcpy(Stable
, Data
.data(), Data
.size());
84 return ArrayRef(Stable
, Data
.size());
87 TypeIndex
GlobalTypeTableBuilder::insertRecordBytes(ArrayRef
<uint8_t> Record
) {
88 GloballyHashedType GHT
=
89 GloballyHashedType::hashType(Record
, SeenHashes
, SeenHashes
);
90 return insertRecordAs(GHT
, Record
.size(),
91 [Record
](MutableArrayRef
<uint8_t> Data
) {
92 assert(Data
.size() == Record
.size());
93 ::memcpy(Data
.data(), Record
.data(), Record
.size());
99 GlobalTypeTableBuilder::insertRecord(ContinuationRecordBuilder
&Builder
) {
101 auto Fragments
= Builder
.end(nextTypeIndex());
102 assert(!Fragments
.empty());
103 for (auto C
: Fragments
)
104 TI
= insertRecordBytes(C
.RecordData
);
108 bool GlobalTypeTableBuilder::replaceType(TypeIndex
&Index
, CVType Data
,
110 assert(Index
.toArrayIndex() < SeenRecords
.size() &&
111 "This function cannot be used to insert records!");
113 ArrayRef
<uint8_t> Record
= Data
.data();
114 assert(Record
.size() < UINT32_MAX
&& "Record too big");
115 assert(Record
.size() % 4 == 0 &&
116 "The type record size is not a multiple of 4 bytes which will cause "
117 "misalignment in the output TPI stream!");
119 GloballyHashedType Hash
=
120 GloballyHashedType::hashType(Record
, SeenHashes
, SeenHashes
);
121 auto Result
= HashedRecords
.try_emplace(Hash
, Index
.toArrayIndex());
122 if (!Result
.second
) {
123 Index
= Result
.first
->second
;
124 return false; // The record is already there, at a different location
128 Record
= stabilize(RecordStorage
, Record
);
130 SeenRecords
[Index
.toArrayIndex()] = Record
;
131 SeenHashes
[Index
.toArrayIndex()] = Hash
;