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
9#include "llvm/DebugInfo/CodeView/SimpleTypeSerializer.h"
10#include "llvm/DebugInfo/CodeView/CVRecord.h"
11#include "llvm/DebugInfo/CodeView/RecordSerialization.h"
12#include "llvm/DebugInfo/CodeView/TypeRecordMapping.h"
13#include "llvm/Support/BinaryStreamWriter.h"
14#include "llvm/Support/Compiler.h"
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(Err: Writer.writeInteger(Value: Pad));
28 --PaddingBytes;
29 }
30}
31
32SimpleTypeSerializer::SimpleTypeSerializer() : ScratchBuffer(MaxRecordLength) {}
33
34SimpleTypeSerializer::~SimpleTypeSerializer() = default;
35
36template <typename T>
37ArrayRef<uint8_t> SimpleTypeSerializer::serialize(T &Record) {
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(Err: Writer.writeObject(Obj: DummyPrefix));
44
45 RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(ScratchBuffer.data());
46 CVType CVT(Prefix, sizeof(RecordPrefix));
47
48 cantFail(Err: Mapping.visitTypeBegin(Record&: CVT));
49 cantFail(Mapping.visitKnownRecord(CVT, Record));
50 cantFail(Err: Mapping.visitTypeEnd(Record&: 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"
70