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/ContinuationRecordBuilder.h"
12 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
13 #include "llvm/Support/Allocator.h"
14 #include "llvm/Support/ErrorHandling.h"
20 using namespace llvm::codeview
;
22 TypeIndex
GlobalTypeTableBuilder::nextTypeIndex() const {
23 return TypeIndex::fromArrayIndex(SeenRecords
.size());
26 GlobalTypeTableBuilder::GlobalTypeTableBuilder(BumpPtrAllocator
&Storage
)
27 : RecordStorage(Storage
) {
28 SeenRecords
.reserve(4096);
31 GlobalTypeTableBuilder::~GlobalTypeTableBuilder() = default;
33 std::optional
<TypeIndex
> GlobalTypeTableBuilder::getFirst() {
37 return TypeIndex(TypeIndex::FirstNonSimpleIndex
);
40 std::optional
<TypeIndex
> GlobalTypeTableBuilder::getNext(TypeIndex Prev
) {
41 if (++Prev
== nextTypeIndex())
46 CVType
GlobalTypeTableBuilder::getType(TypeIndex Index
) {
47 CVType
Type(SeenRecords
[Index
.toArrayIndex()]);
51 StringRef
GlobalTypeTableBuilder::getTypeName(TypeIndex Index
) {
52 llvm_unreachable("Method not implemented");
55 bool GlobalTypeTableBuilder::contains(TypeIndex Index
) {
56 if (Index
.isSimple() || Index
.isNoneType())
59 return Index
.toArrayIndex() < SeenRecords
.size();
62 uint32_t GlobalTypeTableBuilder::size() { return SeenRecords
.size(); }
64 uint32_t GlobalTypeTableBuilder::capacity() { return SeenRecords
.size(); }
66 ArrayRef
<ArrayRef
<uint8_t>> GlobalTypeTableBuilder::records() const {
70 ArrayRef
<GloballyHashedType
> GlobalTypeTableBuilder::hashes() const {
74 void GlobalTypeTableBuilder::reset() {
75 HashedRecords
.clear();
79 static inline ArrayRef
<uint8_t> stabilize(BumpPtrAllocator
&Alloc
,
80 ArrayRef
<uint8_t> Data
) {
81 uint8_t *Stable
= Alloc
.Allocate
<uint8_t>(Data
.size());
82 memcpy(Stable
, Data
.data(), Data
.size());
83 return ArrayRef(Stable
, Data
.size());
86 TypeIndex
GlobalTypeTableBuilder::insertRecordBytes(ArrayRef
<uint8_t> Record
) {
87 GloballyHashedType GHT
=
88 GloballyHashedType::hashType(Record
, SeenHashes
, SeenHashes
);
89 return insertRecordAs(GHT
, Record
.size(),
90 [Record
](MutableArrayRef
<uint8_t> Data
) {
91 assert(Data
.size() == Record
.size());
92 ::memcpy(Data
.data(), Record
.data(), Record
.size());
98 GlobalTypeTableBuilder::insertRecord(ContinuationRecordBuilder
&Builder
) {
100 auto Fragments
= Builder
.end(nextTypeIndex());
101 assert(!Fragments
.empty());
102 for (auto C
: Fragments
)
103 TI
= insertRecordBytes(C
.RecordData
);
107 bool GlobalTypeTableBuilder::replaceType(TypeIndex
&Index
, CVType Data
,
109 assert(Index
.toArrayIndex() < SeenRecords
.size() &&
110 "This function cannot be used to insert records!");
112 ArrayRef
<uint8_t> Record
= Data
.data();
113 assert(Record
.size() < UINT32_MAX
&& "Record too big");
114 assert(Record
.size() % 4 == 0 &&
115 "The type record size is not a multiple of 4 bytes which will cause "
116 "misalignment in the output TPI stream!");
118 GloballyHashedType Hash
=
119 GloballyHashedType::hashType(Record
, SeenHashes
, SeenHashes
);
120 auto Result
= HashedRecords
.try_emplace(Hash
, Index
.toArrayIndex());
121 if (!Result
.second
) {
122 Index
= Result
.first
->second
;
123 return false; // The record is already there, at a different location
127 Record
= stabilize(RecordStorage
, Record
);
129 SeenRecords
[Index
.toArrayIndex()] = Record
;
130 SeenHashes
[Index
.toArrayIndex()] = Hash
;