xref: /freebsd/contrib/llvm-project/llvm/include/llvm/DebugInfo/CodeView/SymbolSerializer.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
10b57cec5SDimitry Andric //===- SymbolSerializer.h ---------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #ifndef LLVM_DEBUGINFO_CODEVIEW_SYMBOLSERIALIZER_H
100b57cec5SDimitry Andric #define LLVM_DEBUGINFO_CODEVIEW_SYMBOLSERIALIZER_H
110b57cec5SDimitry Andric 
1281ad6265SDimitry Andric #include "llvm/DebugInfo/CodeView/CVRecord.h"
130b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h"
140b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
150b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolRecordMapping.h"
160b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/SymbolVisitorCallbacks.h"
170b57cec5SDimitry Andric #include "llvm/Support/Allocator.h"
180b57cec5SDimitry Andric #include "llvm/Support/BinaryByteStream.h"
190b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
2081ad6265SDimitry Andric #include "llvm/Support/Endian.h"
210b57cec5SDimitry Andric #include "llvm/Support/Error.h"
2281ad6265SDimitry Andric #include <array>
230b57cec5SDimitry Andric #include <cstdint>
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric namespace llvm {
260b57cec5SDimitry Andric namespace codeview {
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric class SymbolSerializer : public SymbolVisitorCallbacks {
290b57cec5SDimitry Andric   BumpPtrAllocator &Storage;
300b57cec5SDimitry Andric   // Since this is a fixed size buffer, use a stack allocated buffer.  This
310b57cec5SDimitry Andric   // yields measurable performance increase over the repeated heap allocations
320b57cec5SDimitry Andric   // when serializing many independent records via writeOneSymbol.
330b57cec5SDimitry Andric   std::array<uint8_t, MaxRecordLength> RecordBuffer;
340b57cec5SDimitry Andric   MutableBinaryByteStream Stream;
350b57cec5SDimitry Andric   BinaryStreamWriter Writer;
360b57cec5SDimitry Andric   SymbolRecordMapping Mapping;
37*bdd1243dSDimitry Andric   std::optional<SymbolKind> CurrentSymbol;
380b57cec5SDimitry Andric 
writeRecordPrefix(SymbolKind Kind)390b57cec5SDimitry Andric   Error writeRecordPrefix(SymbolKind Kind) {
400b57cec5SDimitry Andric     RecordPrefix Prefix;
410b57cec5SDimitry Andric     Prefix.RecordKind = Kind;
420b57cec5SDimitry Andric     Prefix.RecordLen = 0;
430b57cec5SDimitry Andric     if (auto EC = Writer.writeObject(Prefix))
440b57cec5SDimitry Andric       return EC;
450b57cec5SDimitry Andric     return Error::success();
460b57cec5SDimitry Andric   }
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric public:
490b57cec5SDimitry Andric   SymbolSerializer(BumpPtrAllocator &Storage, CodeViewContainer Container);
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   template <typename SymType>
writeOneSymbol(SymType & Sym,BumpPtrAllocator & Storage,CodeViewContainer Container)520b57cec5SDimitry Andric   static CVSymbol writeOneSymbol(SymType &Sym, BumpPtrAllocator &Storage,
530b57cec5SDimitry Andric                                  CodeViewContainer Container) {
540b57cec5SDimitry Andric     RecordPrefix Prefix{uint16_t(Sym.Kind)};
550b57cec5SDimitry Andric     CVSymbol Result(&Prefix, sizeof(Prefix));
560b57cec5SDimitry Andric     SymbolSerializer Serializer(Storage, Container);
570b57cec5SDimitry Andric     consumeError(Serializer.visitSymbolBegin(Result));
580b57cec5SDimitry Andric     consumeError(Serializer.visitKnownRecord(Result, Sym));
590b57cec5SDimitry Andric     consumeError(Serializer.visitSymbolEnd(Result));
600b57cec5SDimitry Andric     return Result;
610b57cec5SDimitry Andric   }
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   Error visitSymbolBegin(CVSymbol &Record) override;
640b57cec5SDimitry Andric   Error visitSymbolEnd(CVSymbol &Record) override;
650b57cec5SDimitry Andric 
660b57cec5SDimitry Andric #define SYMBOL_RECORD(EnumName, EnumVal, Name)                                 \
670b57cec5SDimitry Andric   Error visitKnownRecord(CVSymbol &CVR, Name &Record) override {               \
680b57cec5SDimitry Andric     return visitKnownRecordImpl(CVR, Record);                                  \
690b57cec5SDimitry Andric   }
700b57cec5SDimitry Andric #define SYMBOL_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
710b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeViewSymbols.def"
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric private:
740b57cec5SDimitry Andric   template <typename RecordKind>
visitKnownRecordImpl(CVSymbol & CVR,RecordKind & Record)750b57cec5SDimitry Andric   Error visitKnownRecordImpl(CVSymbol &CVR, RecordKind &Record) {
760b57cec5SDimitry Andric     return Mapping.visitKnownRecord(CVR, Record);
770b57cec5SDimitry Andric   }
780b57cec5SDimitry Andric };
790b57cec5SDimitry Andric 
800b57cec5SDimitry Andric } // end namespace codeview
810b57cec5SDimitry Andric } // end namespace llvm
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric #endif // LLVM_DEBUGINFO_CODEVIEW_SYMBOLSERIALIZER_H
84