[CodeView] Refactor / Rewrite TypeSerializer and TypeTableBuilder.
The motivation behind this patch is that future directions require us to
be able to compute the hash value of records independently of actually
using them for de-duplication.
The current structure of TypeSerializer / TypeTableBuilder being a
single entry point that takes an unserialized type record, and then
hashes and de-duplicates it is not flexible enough to allow this.
At the same time, the existing TypeSerializer is already extremely
complex for this very reason -- it tries to be too many things. In
addition to serializing, hashing, and de-duplicating, ti also supports
splitting up field list records and adding continuations. All of this
functionality crammed into this one class makes it very complicated to
work with and hard to maintain.
To solve all of these problems, I've re-written everything from scratch
and split the functionality into separate pieces that can easily be
reused. The end result is that one class TypeSerializer is turned into 3
new classes SimpleTypeSerializer, ContinuationRecordBuilder, and
TypeTableBuilder, each of which in isolation is simple and
straightforward.
A quick summary of these new classes and their responsibilities are:
- SimpleTypeSerializer : Turns a non-FieldList leaf type into a series of
bytes. Does not do any hashing. Every time you call it, it will
re-serialize and return bytes again. The same instance can be re-used
over and over to avoid re-allocations, and in exchange for this
optimization the bytes returned by the serializer only live until the
caller attempts to serialize a new record.
- ContinuationRecordBuilder : Turns a FieldList-like record into a series
of fragments. Does not do any hashing. Like SimpleTypeSerializer,
returns references to privately owned bytes, so the storage is
invalidated as soon as the caller tries to re-use the instance. Works
equally well for LF_FIELDLIST as it does for LF_METHODLIST, solving a
long-standing theoretical limitation of the previous implementation.
- TypeTableBuilder : Accepts sequences of bytes that the user has already
serialized, and inserts them by de-duplicating with a hash table. For
the sake of convenience and efficiency, this class internally stores a
SimpleTypeSerializer so that it can accept unserialized records. The
same is not true of ContinuationRecordBuilder. The user is required to
create their own instance of ContinuationRecordBuilder.
Differential Revision: https://reviews.llvm.org/D40518
llvm-svn: 319198
2017-11-28 19:33:17 +01:00
|
|
|
//===- ContinuationRecordBuilder.h ------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
[CodeView] Refactor / Rewrite TypeSerializer and TypeTableBuilder.
The motivation behind this patch is that future directions require us to
be able to compute the hash value of records independently of actually
using them for de-duplication.
The current structure of TypeSerializer / TypeTableBuilder being a
single entry point that takes an unserialized type record, and then
hashes and de-duplicates it is not flexible enough to allow this.
At the same time, the existing TypeSerializer is already extremely
complex for this very reason -- it tries to be too many things. In
addition to serializing, hashing, and de-duplicating, ti also supports
splitting up field list records and adding continuations. All of this
functionality crammed into this one class makes it very complicated to
work with and hard to maintain.
To solve all of these problems, I've re-written everything from scratch
and split the functionality into separate pieces that can easily be
reused. The end result is that one class TypeSerializer is turned into 3
new classes SimpleTypeSerializer, ContinuationRecordBuilder, and
TypeTableBuilder, each of which in isolation is simple and
straightforward.
A quick summary of these new classes and their responsibilities are:
- SimpleTypeSerializer : Turns a non-FieldList leaf type into a series of
bytes. Does not do any hashing. Every time you call it, it will
re-serialize and return bytes again. The same instance can be re-used
over and over to avoid re-allocations, and in exchange for this
optimization the bytes returned by the serializer only live until the
caller attempts to serialize a new record.
- ContinuationRecordBuilder : Turns a FieldList-like record into a series
of fragments. Does not do any hashing. Like SimpleTypeSerializer,
returns references to privately owned bytes, so the storage is
invalidated as soon as the caller tries to re-use the instance. Works
equally well for LF_FIELDLIST as it does for LF_METHODLIST, solving a
long-standing theoretical limitation of the previous implementation.
- TypeTableBuilder : Accepts sequences of bytes that the user has already
serialized, and inserts them by de-duplicating with a hash table. For
the sake of convenience and efficiency, this class internally stores a
SimpleTypeSerializer so that it can accept unserialized records. The
same is not true of ContinuationRecordBuilder. The user is required to
create their own instance of ContinuationRecordBuilder.
Differential Revision: https://reviews.llvm.org/D40518
llvm-svn: 319198
2017-11-28 19:33:17 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_DEBUGINFO_CODEVIEW_CONTINUATIONRECORDBUILDER_H
|
|
|
|
#define LLVM_DEBUGINFO_CODEVIEW_CONTINUATIONRECORDBUILDER_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/DebugInfo/CodeView/CodeView.h"
|
|
|
|
#include "llvm/DebugInfo/CodeView/RecordSerialization.h"
|
|
|
|
#include "llvm/DebugInfo/CodeView/TypeIndex.h"
|
|
|
|
#include "llvm/DebugInfo/CodeView/TypeRecord.h"
|
|
|
|
#include "llvm/DebugInfo/CodeView/TypeRecordMapping.h"
|
|
|
|
#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
|
|
|
|
#include "llvm/Support/Allocator.h"
|
|
|
|
#include "llvm/Support/BinaryByteStream.h"
|
|
|
|
#include "llvm/Support/BinaryStreamWriter.h"
|
|
|
|
#include "llvm/Support/Error.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace codeview {
|
|
|
|
enum class ContinuationRecordKind { FieldList, MethodOverloadList };
|
|
|
|
|
|
|
|
class ContinuationRecordBuilder {
|
|
|
|
SmallVector<uint32_t, 4> SegmentOffsets;
|
|
|
|
Optional<ContinuationRecordKind> Kind;
|
|
|
|
AppendingBinaryByteStream Buffer;
|
|
|
|
BinaryStreamWriter SegmentWriter;
|
|
|
|
TypeRecordMapping Mapping;
|
|
|
|
ArrayRef<uint8_t> InjectedSegmentBytes;
|
|
|
|
|
|
|
|
uint32_t getCurrentSegmentLength() const;
|
|
|
|
|
|
|
|
void insertSegmentEnd(uint32_t Offset);
|
|
|
|
CVType createSegmentRecord(uint32_t OffBegin, uint32_t OffEnd,
|
|
|
|
Optional<TypeIndex> RefersTo);
|
|
|
|
|
|
|
|
public:
|
|
|
|
ContinuationRecordBuilder();
|
|
|
|
~ContinuationRecordBuilder();
|
|
|
|
|
|
|
|
void begin(ContinuationRecordKind RecordKind);
|
|
|
|
|
|
|
|
// This template is explicitly instantiated in the implementation file for all
|
|
|
|
// supported types. The method itself is ugly, so inlining it into the header
|
|
|
|
// file clutters an otherwise straightforward interface.
|
|
|
|
template <typename RecordType> void writeMemberType(RecordType &Record);
|
|
|
|
|
|
|
|
std::vector<CVType> end(TypeIndex Index);
|
|
|
|
};
|
|
|
|
} // namespace codeview
|
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
#endif
|