Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:40

0001 //===- SymbolSerializer.h ---------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_DEBUGINFO_CODEVIEW_SYMBOLSERIALIZER_H
0010 #define LLVM_DEBUGINFO_CODEVIEW_SYMBOLSERIALIZER_H
0011 
0012 #include "llvm/DebugInfo/CodeView/CVRecord.h"
0013 #include "llvm/DebugInfo/CodeView/CodeView.h"
0014 #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
0015 #include "llvm/DebugInfo/CodeView/SymbolRecordMapping.h"
0016 #include "llvm/DebugInfo/CodeView/SymbolVisitorCallbacks.h"
0017 #include "llvm/Support/Allocator.h"
0018 #include "llvm/Support/BinaryByteStream.h"
0019 #include "llvm/Support/BinaryStreamWriter.h"
0020 #include "llvm/Support/Endian.h"
0021 #include "llvm/Support/Error.h"
0022 #include <array>
0023 #include <cstdint>
0024 
0025 namespace llvm {
0026 namespace codeview {
0027 
0028 class SymbolSerializer : public SymbolVisitorCallbacks {
0029   BumpPtrAllocator &Storage;
0030   // Since this is a fixed size buffer, use a stack allocated buffer.  This
0031   // yields measurable performance increase over the repeated heap allocations
0032   // when serializing many independent records via writeOneSymbol.
0033   std::array<uint8_t, MaxRecordLength> RecordBuffer;
0034   MutableBinaryByteStream Stream;
0035   BinaryStreamWriter Writer;
0036   SymbolRecordMapping Mapping;
0037   std::optional<SymbolKind> CurrentSymbol;
0038 
0039   Error writeRecordPrefix(SymbolKind Kind) {
0040     RecordPrefix Prefix;
0041     Prefix.RecordKind = Kind;
0042     Prefix.RecordLen = 0;
0043     if (auto EC = Writer.writeObject(Prefix))
0044       return EC;
0045     return Error::success();
0046   }
0047 
0048 public:
0049   SymbolSerializer(BumpPtrAllocator &Storage, CodeViewContainer Container);
0050 
0051   template <typename SymType>
0052   static CVSymbol writeOneSymbol(SymType &Sym, BumpPtrAllocator &Storage,
0053                                  CodeViewContainer Container) {
0054     RecordPrefix Prefix{uint16_t(Sym.Kind)};
0055     CVSymbol Result(&Prefix, sizeof(Prefix));
0056     SymbolSerializer Serializer(Storage, Container);
0057     consumeError(Serializer.visitSymbolBegin(Result));
0058     consumeError(Serializer.visitKnownRecord(Result, Sym));
0059     consumeError(Serializer.visitSymbolEnd(Result));
0060     return Result;
0061   }
0062 
0063   Error visitSymbolBegin(CVSymbol &Record) override;
0064   Error visitSymbolEnd(CVSymbol &Record) override;
0065 
0066 #define SYMBOL_RECORD(EnumName, EnumVal, Name)                                 \
0067   Error visitKnownRecord(CVSymbol &CVR, Name &Record) override {               \
0068     return visitKnownRecordImpl(CVR, Record);                                  \
0069   }
0070 #define SYMBOL_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
0071 #include "llvm/DebugInfo/CodeView/CodeViewSymbols.def"
0072 
0073 private:
0074   template <typename RecordKind>
0075   Error visitKnownRecordImpl(CVSymbol &CVR, RecordKind &Record) {
0076     return Mapping.visitKnownRecord(CVR, Record);
0077   }
0078 };
0079 
0080 } // end namespace codeview
0081 } // end namespace llvm
0082 
0083 #endif // LLVM_DEBUGINFO_CODEVIEW_SYMBOLSERIALIZER_H