SymbolSerializer.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- SymbolSerializer.h ---------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_DEBUGINFO_CODEVIEW_SYMBOLSERIALIZER_H
  14. #define LLVM_DEBUGINFO_CODEVIEW_SYMBOLSERIALIZER_H
  15. #include "llvm/ADT/Optional.h"
  16. #include "llvm/DebugInfo/CodeView/CodeView.h"
  17. #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
  18. #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
  19. #include "llvm/DebugInfo/CodeView/SymbolRecordMapping.h"
  20. #include "llvm/DebugInfo/CodeView/SymbolVisitorCallbacks.h"
  21. #include "llvm/Support/Allocator.h"
  22. #include "llvm/Support/BinaryByteStream.h"
  23. #include "llvm/Support/BinaryStreamWriter.h"
  24. #include "llvm/Support/Error.h"
  25. #include <cstdint>
  26. namespace llvm {
  27. namespace codeview {
  28. class SymbolSerializer : public SymbolVisitorCallbacks {
  29. BumpPtrAllocator &Storage;
  30. // Since this is a fixed size buffer, use a stack allocated buffer. This
  31. // yields measurable performance increase over the repeated heap allocations
  32. // when serializing many independent records via writeOneSymbol.
  33. std::array<uint8_t, MaxRecordLength> RecordBuffer;
  34. MutableBinaryByteStream Stream;
  35. BinaryStreamWriter Writer;
  36. SymbolRecordMapping Mapping;
  37. Optional<SymbolKind> CurrentSymbol;
  38. Error writeRecordPrefix(SymbolKind Kind) {
  39. RecordPrefix Prefix;
  40. Prefix.RecordKind = Kind;
  41. Prefix.RecordLen = 0;
  42. if (auto EC = Writer.writeObject(Prefix))
  43. return EC;
  44. return Error::success();
  45. }
  46. public:
  47. SymbolSerializer(BumpPtrAllocator &Storage, CodeViewContainer Container);
  48. template <typename SymType>
  49. static CVSymbol writeOneSymbol(SymType &Sym, BumpPtrAllocator &Storage,
  50. CodeViewContainer Container) {
  51. RecordPrefix Prefix{uint16_t(Sym.Kind)};
  52. CVSymbol Result(&Prefix, sizeof(Prefix));
  53. SymbolSerializer Serializer(Storage, Container);
  54. consumeError(Serializer.visitSymbolBegin(Result));
  55. consumeError(Serializer.visitKnownRecord(Result, Sym));
  56. consumeError(Serializer.visitSymbolEnd(Result));
  57. return Result;
  58. }
  59. Error visitSymbolBegin(CVSymbol &Record) override;
  60. Error visitSymbolEnd(CVSymbol &Record) override;
  61. #define SYMBOL_RECORD(EnumName, EnumVal, Name) \
  62. Error visitKnownRecord(CVSymbol &CVR, Name &Record) override { \
  63. return visitKnownRecordImpl(CVR, Record); \
  64. }
  65. #define SYMBOL_RECORD_ALIAS(EnumName, EnumVal, Name, AliasName)
  66. #include "llvm/DebugInfo/CodeView/CodeViewSymbols.def"
  67. private:
  68. template <typename RecordKind>
  69. Error visitKnownRecordImpl(CVSymbol &CVR, RecordKind &Record) {
  70. return Mapping.visitKnownRecord(CVR, Record);
  71. }
  72. };
  73. } // end namespace codeview
  74. } // end namespace llvm
  75. #endif // LLVM_DEBUGINFO_CODEVIEW_SYMBOLSERIALIZER_H
  76. #ifdef __GNUC__
  77. #pragma GCC diagnostic pop
  78. #endif