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