1 //===- AppendingTypeTableBuilder.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/AppendingTypeTableBuilder.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 AppendingTypeTableBuilder::nextTypeIndex() const {
31   return TypeIndex::fromArrayIndex(SeenRecords.size());
32 }
33 
AppendingTypeTableBuilder(BumpPtrAllocator & Storage)34 AppendingTypeTableBuilder::AppendingTypeTableBuilder(BumpPtrAllocator &Storage)
35     : RecordStorage(Storage) {}
36 
37 AppendingTypeTableBuilder::~AppendingTypeTableBuilder() = default;
38 
getFirst()39 Optional<TypeIndex> AppendingTypeTableBuilder::getFirst() {
40   if (empty())
41     return None;
42 
43   return TypeIndex(TypeIndex::FirstNonSimpleIndex);
44 }
45 
getNext(TypeIndex Prev)46 Optional<TypeIndex> AppendingTypeTableBuilder::getNext(TypeIndex Prev) {
47   if (++Prev == nextTypeIndex())
48     return None;
49   return Prev;
50 }
51 
getType(TypeIndex Index)52 CVType AppendingTypeTableBuilder::getType(TypeIndex Index){
53   return CVType(SeenRecords[Index.toArrayIndex()]);
54 }
55 
getTypeName(TypeIndex Index)56 StringRef AppendingTypeTableBuilder::getTypeName(TypeIndex Index) {
57   llvm_unreachable("Method not implemented");
58 }
59 
contains(TypeIndex Index)60 bool AppendingTypeTableBuilder::contains(TypeIndex Index) {
61   if (Index.isSimple() || Index.isNoneType())
62     return false;
63 
64   return Index.toArrayIndex() < SeenRecords.size();
65 }
66 
size()67 uint32_t AppendingTypeTableBuilder::size() { return SeenRecords.size(); }
68 
capacity()69 uint32_t AppendingTypeTableBuilder::capacity() { return SeenRecords.size(); }
70 
records() const71 ArrayRef<ArrayRef<uint8_t>> AppendingTypeTableBuilder::records() const {
72   return SeenRecords;
73 }
74 
reset()75 void AppendingTypeTableBuilder::reset() { SeenRecords.clear(); }
76 
stabilize(BumpPtrAllocator & RecordStorage,ArrayRef<uint8_t> Record)77 static ArrayRef<uint8_t> stabilize(BumpPtrAllocator &RecordStorage,
78                                    ArrayRef<uint8_t> Record) {
79   uint8_t *Stable = RecordStorage.Allocate<uint8_t>(Record.size());
80   memcpy(Stable, Record.data(), Record.size());
81   return ArrayRef<uint8_t>(Stable, Record.size());
82 }
83 
84 TypeIndex
insertRecordBytes(ArrayRef<uint8_t> & Record)85 AppendingTypeTableBuilder::insertRecordBytes(ArrayRef<uint8_t> &Record) {
86   TypeIndex NewTI = nextTypeIndex();
87   Record = stabilize(RecordStorage, Record);
88   SeenRecords.push_back(Record);
89   return NewTI;
90 }
91 
92 TypeIndex
insertRecord(ContinuationRecordBuilder & Builder)93 AppendingTypeTableBuilder::insertRecord(ContinuationRecordBuilder &Builder) {
94   TypeIndex TI;
95   auto Fragments = Builder.end(nextTypeIndex());
96   assert(!Fragments.empty());
97   for (auto C : Fragments)
98     TI = insertRecordBytes(C.RecordData);
99   return TI;
100 }
101 
replaceType(TypeIndex & Index,CVType Data,bool Stabilize)102 bool AppendingTypeTableBuilder::replaceType(TypeIndex &Index, CVType Data,
103                                             bool Stabilize) {
104   assert(Index.toArrayIndex() < SeenRecords.size() &&
105          "This function cannot be used to insert records!");
106 
107   ArrayRef<uint8_t> Record = Data.data();
108   if (Stabilize)
109     Record = stabilize(RecordStorage, Record);
110   SeenRecords[Index.toArrayIndex()] = Record;
111   return true;
112 }
113