aboutsummaryrefslogtreecommitdiff
path: root/include/llvm/DebugInfo/CodeView/TypeSerializer.h
blob: 988a2d4aa8346502ae20b2d8c476e5ed87eabac0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//===- TypeSerializer.h -----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_DEBUGINFO_CODEVIEW_TYPESERIALIZER_H
#define LLVM_DEBUGINFO_CODEVIEW_TYPESERIALIZER_H

#include "llvm/DebugInfo/CodeView/TypeRecordMapping.h"
#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
#include "llvm/Support/BinaryByteStream.h"
#include "llvm/Support/BinaryStreamWriter.h"

#include "llvm/ADT/Optional.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Error.h"

namespace llvm {

namespace codeview {

class TypeHasher;

class TypeSerializer : public TypeVisitorCallbacks {
  struct SubRecord {
    SubRecord(TypeLeafKind K, uint32_t S) : Kind(K), Size(S) {}

    TypeLeafKind Kind;
    uint32_t Size = 0;
  };
  struct RecordSegment {
    SmallVector<SubRecord, 16> SubRecords;

    uint32_t length() const {
      uint32_t L = sizeof(RecordPrefix);
      for (const auto &R : SubRecords) {
        L += R.Size;
      }
      return L;
    }
  };

  typedef SmallVector<MutableArrayRef<uint8_t>, 2> MutableRecordList;

  static constexpr uint8_t ContinuationLength = 8;
  BumpPtrAllocator &RecordStorage;
  RecordSegment CurrentSegment;
  MutableRecordList FieldListSegments;

  Optional<TypeLeafKind> TypeKind;
  Optional<TypeLeafKind> MemberKind;
  std::vector<uint8_t> RecordBuffer;
  MutableBinaryByteStream Stream;
  BinaryStreamWriter Writer;
  TypeRecordMapping Mapping;

  /// Private type record hashing implementation details are handled here.
  std::unique_ptr<TypeHasher> Hasher;

  /// Contains a list of all records indexed by TypeIndex.toArrayIndex().
  SmallVector<ArrayRef<uint8_t>, 2> SeenRecords;

  /// Temporary storage that we use to copy a record's data while re-writing
  /// its type indices.
  SmallVector<uint8_t, 256> RemapStorage;

  TypeIndex nextTypeIndex() const;

  bool isInFieldList() const;
  MutableArrayRef<uint8_t> getCurrentSubRecordData();
  MutableArrayRef<uint8_t> getCurrentRecordData();
  Error writeRecordPrefix(TypeLeafKind Kind);

  Expected<MutableArrayRef<uint8_t>>
  addPadding(MutableArrayRef<uint8_t> Record);

public:
  explicit TypeSerializer(BumpPtrAllocator &Storage, bool Hash = true);
  ~TypeSerializer();

  void reset();

  BumpPtrAllocator &getAllocator() { return RecordStorage; }

  ArrayRef<ArrayRef<uint8_t>> records() const;
  TypeIndex insertRecordBytes(ArrayRef<uint8_t> &Record);
  TypeIndex insertRecord(const RemappedType &Record);
  Expected<TypeIndex> visitTypeEndGetIndex(CVType &Record);

  using TypeVisitorCallbacks::visitTypeBegin;
  Error visitTypeBegin(CVType &Record) override;
  Error visitTypeEnd(CVType &Record) override;
  Error visitMemberBegin(CVMemberRecord &Record) override;
  Error visitMemberEnd(CVMemberRecord &Record) override;

#define TYPE_RECORD(EnumName, EnumVal, Name)                                   \
  virtual Error visitKnownRecord(CVType &CVR, Name##Record &Record) override { \
    return visitKnownRecordImpl(CVR, Record);                                  \
  }
#define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
#define MEMBER_RECORD(EnumName, EnumVal, Name)                                 \
  Error visitKnownMember(CVMemberRecord &CVR, Name##Record &Record) override { \
    return visitKnownMemberImpl<Name##Record>(CVR, Record);                    \
  }
#define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
#include "llvm/DebugInfo/CodeView/CodeViewTypes.def"

private:
  template <typename RecordKind>
  Error visitKnownRecordImpl(CVType &CVR, RecordKind &Record) {
    return Mapping.visitKnownRecord(CVR, Record);
  }

  template <typename RecordType>
  Error visitKnownMemberImpl(CVMemberRecord &CVR, RecordType &Record) {
    assert(CVR.Kind == static_cast<TypeLeafKind>(Record.getKind()));

    if (auto EC = Writer.writeEnum(CVR.Kind))
      return EC;

    if (auto EC = Mapping.visitKnownMember(CVR, Record))
      return EC;

    // Get all the data that was just written and is yet to be committed to
    // the current segment.  Then pad it to 4 bytes.
    MutableArrayRef<uint8_t> ThisRecord = getCurrentSubRecordData();
    auto ExpectedRecord = addPadding(ThisRecord);
    if (!ExpectedRecord)
      return ExpectedRecord.takeError();
    ThisRecord = *ExpectedRecord;

    CurrentSegment.SubRecords.emplace_back(CVR.Kind, ThisRecord.size());
    CVR.Data = ThisRecord;

    // Both the last subrecord and the total length of this segment should be
    // multiples of 4.
    assert(ThisRecord.size() % 4 == 0);
    assert(CurrentSegment.length() % 4 == 0);

    return Error::success();
  }
};
}
}

#endif