1 //===- MergingTypeTableBuilder.cpp ----------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/DebugInfo/CodeView/MergingTypeTableBuilder.h"
11 #include "llvm/ADT/ArrayRef.h"
12 #include "llvm/ADT/DenseSet.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/DebugInfo/CodeView/CodeView.h"
15 #include "llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h"
16 #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
17 #include "llvm/DebugInfo/CodeView/TypeIndex.h"
18 #include "llvm/Support/Allocator.h"
19 #include "llvm/Support/BinaryByteStream.h"
20 #include "llvm/Support/BinaryStreamWriter.h"
21 #include "llvm/Support/Endian.h"
22 #include "llvm/Support/Error.h"
29 using namespace llvm::codeview
;
31 TypeIndex
MergingTypeTableBuilder::nextTypeIndex() const {
32 return TypeIndex::fromArrayIndex(SeenRecords
.size());
35 MergingTypeTableBuilder::MergingTypeTableBuilder(BumpPtrAllocator
&Storage
)
36 : RecordStorage(Storage
) {
37 SeenRecords
.reserve(4096);
40 MergingTypeTableBuilder::~MergingTypeTableBuilder() = default;
42 Optional
<TypeIndex
> MergingTypeTableBuilder::getFirst() {
46 return TypeIndex(TypeIndex::FirstNonSimpleIndex
);
49 Optional
<TypeIndex
> MergingTypeTableBuilder::getNext(TypeIndex Prev
) {
50 if (++Prev
== nextTypeIndex())
55 CVType
MergingTypeTableBuilder::getType(TypeIndex Index
) {
57 Type
.RecordData
= SeenRecords
[Index
.toArrayIndex()];
58 const RecordPrefix
*P
=
59 reinterpret_cast<const RecordPrefix
*>(Type
.RecordData
.data());
60 Type
.Type
= static_cast<TypeLeafKind
>(uint16_t(P
->RecordKind
));
64 StringRef
MergingTypeTableBuilder::getTypeName(TypeIndex Index
) {
65 llvm_unreachable("Method not implemented");
68 bool MergingTypeTableBuilder::contains(TypeIndex Index
) {
69 if (Index
.isSimple() || Index
.isNoneType())
72 return Index
.toArrayIndex() < SeenRecords
.size();
75 uint32_t MergingTypeTableBuilder::size() { return SeenRecords
.size(); }
77 uint32_t MergingTypeTableBuilder::capacity() { return SeenRecords
.size(); }
79 ArrayRef
<ArrayRef
<uint8_t>> MergingTypeTableBuilder::records() const {
83 void MergingTypeTableBuilder::reset() {
84 HashedRecords
.clear();
88 static inline ArrayRef
<uint8_t> stabilize(BumpPtrAllocator
&Alloc
,
89 ArrayRef
<uint8_t> Data
) {
90 uint8_t *Stable
= Alloc
.Allocate
<uint8_t>(Data
.size());
91 memcpy(Stable
, Data
.data(), Data
.size());
92 return makeArrayRef(Stable
, Data
.size());
95 TypeIndex
MergingTypeTableBuilder::insertRecordAs(hash_code Hash
,
96 ArrayRef
<uint8_t> &Record
) {
97 assert(Record
.size() < UINT32_MAX
&& "Record too big");
98 assert(Record
.size() % 4 == 0 && "Record is not aligned to 4 bytes!");
100 LocallyHashedType WeakHash
{Hash
, Record
};
101 auto Result
= HashedRecords
.try_emplace(WeakHash
, nextTypeIndex());
104 ArrayRef
<uint8_t> RecordData
= stabilize(RecordStorage
, Record
);
105 Result
.first
->first
.RecordData
= RecordData
;
106 SeenRecords
.push_back(RecordData
);
109 // Update the caller's copy of Record to point a stable copy.
110 TypeIndex ActualTI
= Result
.first
->second
;
111 Record
= SeenRecords
[ActualTI
.toArrayIndex()];
116 MergingTypeTableBuilder::insertRecordBytes(ArrayRef
<uint8_t> &Record
) {
117 return insertRecordAs(hash_value(Record
), Record
);
121 MergingTypeTableBuilder::insertRecord(ContinuationRecordBuilder
&Builder
) {
123 auto Fragments
= Builder
.end(nextTypeIndex());
124 assert(!Fragments
.empty());
125 for (auto C
: Fragments
)
126 TI
= insertRecordBytes(C
.RecordData
);