• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- MergingTypeTableBuilder.cpp ----------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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"
23 #include <algorithm>
24 #include <cassert>
25 #include <cstdint>
26 #include <cstring>
27 
28 using namespace llvm;
29 using namespace llvm::codeview;
30 
nextTypeIndex() const31 TypeIndex MergingTypeTableBuilder::nextTypeIndex() const {
32   return TypeIndex::fromArrayIndex(SeenRecords.size());
33 }
34 
MergingTypeTableBuilder(BumpPtrAllocator & Storage)35 MergingTypeTableBuilder::MergingTypeTableBuilder(BumpPtrAllocator &Storage)
36     : RecordStorage(Storage) {
37   SeenRecords.reserve(4096);
38 }
39 
40 MergingTypeTableBuilder::~MergingTypeTableBuilder() = default;
41 
getFirst()42 Optional<TypeIndex> MergingTypeTableBuilder::getFirst() {
43   if (empty())
44     return None;
45 
46   return TypeIndex(TypeIndex::FirstNonSimpleIndex);
47 }
48 
getNext(TypeIndex Prev)49 Optional<TypeIndex> MergingTypeTableBuilder::getNext(TypeIndex Prev) {
50   if (++Prev == nextTypeIndex())
51     return None;
52   return Prev;
53 }
54 
getType(TypeIndex Index)55 CVType MergingTypeTableBuilder::getType(TypeIndex Index) {
56   CVType Type;
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));
61   return Type;
62 }
63 
getTypeName(TypeIndex Index)64 StringRef MergingTypeTableBuilder::getTypeName(TypeIndex Index) {
65   llvm_unreachable("Method not implemented");
66 }
67 
contains(TypeIndex Index)68 bool MergingTypeTableBuilder::contains(TypeIndex Index) {
69   if (Index.isSimple() || Index.isNoneType())
70     return false;
71 
72   return Index.toArrayIndex() < SeenRecords.size();
73 }
74 
size()75 uint32_t MergingTypeTableBuilder::size() { return SeenRecords.size(); }
76 
capacity()77 uint32_t MergingTypeTableBuilder::capacity() { return SeenRecords.size(); }
78 
records() const79 ArrayRef<ArrayRef<uint8_t>> MergingTypeTableBuilder::records() const {
80   return SeenRecords;
81 }
82 
reset()83 void MergingTypeTableBuilder::reset() {
84   HashedRecords.clear();
85   SeenRecords.clear();
86 }
87 
stabilize(BumpPtrAllocator & Alloc,ArrayRef<uint8_t> Data)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());
93 }
94 
insertRecordAs(hash_code Hash,ArrayRef<uint8_t> & Record)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!");
99 
100   LocallyHashedType WeakHash{Hash, Record};
101   auto Result = HashedRecords.try_emplace(WeakHash, nextTypeIndex());
102 
103   if (Result.second) {
104     ArrayRef<uint8_t> RecordData = stabilize(RecordStorage, Record);
105     Result.first->first.RecordData = RecordData;
106     SeenRecords.push_back(RecordData);
107   }
108 
109   // Update the caller's copy of Record to point a stable copy.
110   TypeIndex ActualTI = Result.first->second;
111   Record = SeenRecords[ActualTI.toArrayIndex()];
112   return ActualTI;
113 }
114 
115 TypeIndex
insertRecordBytes(ArrayRef<uint8_t> & Record)116 MergingTypeTableBuilder::insertRecordBytes(ArrayRef<uint8_t> &Record) {
117   return insertRecordAs(hash_value(Record), Record);
118 }
119 
120 TypeIndex
insertRecord(ContinuationRecordBuilder & Builder)121 MergingTypeTableBuilder::insertRecord(ContinuationRecordBuilder &Builder) {
122   TypeIndex TI;
123   auto Fragments = Builder.end(nextTypeIndex());
124   assert(!Fragments.empty());
125   for (auto C : Fragments)
126     TI = insertRecordBytes(C.RecordData);
127   return TI;
128 }
129