LLVM 22.0.0git
SimpleTypeSerializer.cpp
Go to the documentation of this file.
1//===- SimpleTypeSerializer.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
15
16using namespace llvm;
17using namespace llvm::codeview;
18
19static void addPadding(BinaryStreamWriter &Writer) {
20 uint32_t Align = Writer.getOffset() % 4;
21 if (Align == 0)
22 return;
23
24 int PaddingBytes = 4 - Align;
25 while (PaddingBytes > 0) {
26 uint8_t Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
27 cantFail(Writer.writeInteger(Pad));
28 --PaddingBytes;
29 }
30}
31
33
35
36template <typename T>
38 BinaryStreamWriter Writer(ScratchBuffer, llvm::endianness::little);
39 TypeRecordMapping Mapping(Writer);
40
41 // Write the record prefix first with a dummy length but real kind.
42 RecordPrefix DummyPrefix(uint16_t(Record.getKind()));
43 cantFail(Writer.writeObject(DummyPrefix));
44
45 RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(ScratchBuffer.data());
46 CVType CVT(Prefix, sizeof(RecordPrefix));
47
48 cantFail(Mapping.visitTypeBegin(CVT));
49 cantFail(Mapping.visitKnownRecord(CVT, Record));
50 cantFail(Mapping.visitTypeEnd(CVT));
51
52 addPadding(Writer);
53
54 // Update the size and kind after serialization.
55 Prefix->RecordKind = CVT.kind();
56 Prefix->RecordLen = Writer.getOffset() - sizeof(uint16_t);
57
58 return {ScratchBuffer.data(), static_cast<size_t>(Writer.getOffset())};
59}
60
61// Explicitly instantiate the member function for each known type so that we can
62// implement this in the cpp file.
63#define TYPE_RECORD(EnumName, EnumVal, Name) \
64 template LLVM_ABI ArrayRef<uint8_t> \
65 llvm::codeview::SimpleTypeSerializer::serialize(Name##Record &Record);
66#define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
67#define MEMBER_RECORD(EnumName, EnumVal, Name)
68#define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
69#include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
static void addPadding(BinaryStreamWriter &Writer)
#define T
static void addPadding(BinaryStreamWriter &Writer)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
Provides write only access to a subclass of WritableBinaryStream.
Error writeInteger(T Value)
Write the integer Value to the underlying stream in the specified endianness.
Error writeObject(const T &Obj)
Writes the object Obj to the underlying stream, as if by using memcpy.
ArrayRef< uint8_t > serialize(T &Record)
Error visitTypeBegin(CVType &Record) override
Paired begin/end actions for all types.
Error visitTypeEnd(CVType &Record) override
CVRecord< TypeLeafKind > CVType
Definition CVRecord.h:64
This is an optimization pass for GlobalISel generic memory operations.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
Definition Error.h:769
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39