• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "llvm/DebugInfo/CodeView/SimpleTypeSerializer.h"
2 
3 using namespace llvm;
4 using namespace llvm::codeview;
5 
writeRecordPrefix(BinaryStreamWriter & Writer,TypeLeafKind Kind)6 static void writeRecordPrefix(BinaryStreamWriter &Writer, TypeLeafKind Kind) {
7   RecordPrefix Prefix;
8   Prefix.RecordKind = Kind;
9   Prefix.RecordLen = 0;
10   cantFail(Writer.writeObject(Prefix));
11 }
12 
addPadding(BinaryStreamWriter & Writer)13 static void addPadding(BinaryStreamWriter &Writer) {
14   uint32_t Align = Writer.getOffset() % 4;
15   if (Align == 0)
16     return;
17 
18   int PaddingBytes = 4 - Align;
19   while (PaddingBytes > 0) {
20     uint8_t Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
21     cantFail(Writer.writeInteger(Pad));
22     --PaddingBytes;
23   }
24 }
25 
SimpleTypeSerializer()26 SimpleTypeSerializer::SimpleTypeSerializer() : ScratchBuffer(MaxRecordLength) {}
27 
~SimpleTypeSerializer()28 SimpleTypeSerializer::~SimpleTypeSerializer() {}
29 
30 template <typename T>
serialize(T & Record)31 ArrayRef<uint8_t> SimpleTypeSerializer::serialize(T &Record) {
32   BinaryStreamWriter Writer(ScratchBuffer, support::little);
33   TypeRecordMapping Mapping(Writer);
34 
35   CVType CVT;
36   CVT.Type = static_cast<TypeLeafKind>(Record.getKind());
37 
38   writeRecordPrefix(Writer, CVT.Type);
39 
40   cantFail(Mapping.visitTypeBegin(CVT));
41   cantFail(Mapping.visitKnownRecord(CVT, Record));
42   cantFail(Mapping.visitTypeEnd(CVT));
43 
44   addPadding(Writer);
45 
46   RecordPrefix *Prefix = reinterpret_cast<RecordPrefix *>(ScratchBuffer.data());
47 
48   Prefix->RecordKind = CVT.kind();
49   Prefix->RecordLen = Writer.getOffset() - sizeof(uint16_t);
50 
51   return {ScratchBuffer.data(), Writer.getOffset()};
52 }
53 
54 // Explicitly instantiate the member function for each known type so that we can
55 // implement this in the cpp file.
56 #define TYPE_RECORD(EnumName, EnumVal, Name)                                   \
57   template ArrayRef<uint8_t> llvm::codeview::SimpleTypeSerializer::serialize(  \
58       Name##Record &Record);
59 #define TYPE_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
60 #define MEMBER_RECORD(EnumName, EnumVal, Name)
61 #define MEMBER_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
62 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
63