ByteStreamer.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //===-- llvm/CodeGen/ByteStreamer.h - ByteStreamer class --------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains a class that can take bytes that would normally be
  11. // streamed via the AsmPrinter.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
  15. #define LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
  16. #include "DIEHash.h"
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/CodeGen/AsmPrinter.h"
  19. #include "llvm/MC/MCStreamer.h"
  20. #include "llvm/Support/LEB128.h"
  21. #include <string>
  22. namespace llvm {
  23. class ByteStreamer {
  24. public:
  25. virtual ~ByteStreamer() {}
  26. // For now we're just handling the calls we need for dwarf emission/hashing.
  27. virtual void EmitInt8(uint8_t Byte, const Twine &Comment = "") = 0;
  28. virtual void EmitSLEB128(uint64_t DWord, const Twine &Comment = "") = 0;
  29. virtual void EmitULEB128(uint64_t DWord, const Twine &Comment = "") = 0;
  30. };
  31. class APByteStreamer : public ByteStreamer {
  32. private:
  33. AsmPrinter &AP;
  34. public:
  35. APByteStreamer(AsmPrinter &Asm) : AP(Asm) {}
  36. void EmitInt8(uint8_t Byte, const Twine &Comment) override {
  37. AP.OutStreamer->AddComment(Comment);
  38. AP.EmitInt8(Byte);
  39. }
  40. void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
  41. AP.OutStreamer->AddComment(Comment);
  42. AP.EmitSLEB128(DWord);
  43. }
  44. void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
  45. AP.OutStreamer->AddComment(Comment);
  46. AP.EmitULEB128(DWord);
  47. }
  48. };
  49. class HashingByteStreamer : public ByteStreamer {
  50. private:
  51. DIEHash &Hash;
  52. public:
  53. HashingByteStreamer(DIEHash &H) : Hash(H) {}
  54. void EmitInt8(uint8_t Byte, const Twine &Comment) override {
  55. Hash.update(Byte);
  56. }
  57. void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
  58. Hash.addSLEB128(DWord);
  59. }
  60. void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
  61. Hash.addULEB128(DWord);
  62. }
  63. };
  64. class BufferByteStreamer : public ByteStreamer {
  65. private:
  66. SmallVectorImpl<char> &Buffer;
  67. SmallVectorImpl<std::string> &Comments;
  68. /// \brief Only verbose textual output needs comments. This will be set to
  69. /// true for that case, and false otherwise. If false, comments passed in to
  70. /// the emit methods will be ignored.
  71. bool GenerateComments;
  72. public:
  73. BufferByteStreamer(SmallVectorImpl<char> &Buffer,
  74. SmallVectorImpl<std::string> &Comments,
  75. bool GenerateComments)
  76. : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {}
  77. void EmitInt8(uint8_t Byte, const Twine &Comment) override {
  78. Buffer.push_back(Byte);
  79. if (GenerateComments)
  80. Comments.push_back(Comment.str());
  81. }
  82. void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
  83. raw_svector_ostream OSE(Buffer);
  84. encodeSLEB128(DWord, OSE);
  85. if (GenerateComments)
  86. Comments.push_back(Comment.str());
  87. }
  88. void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
  89. raw_svector_ostream OSE(Buffer);
  90. encodeULEB128(DWord, OSE);
  91. if (GenerateComments)
  92. Comments.push_back(Comment.str());
  93. }
  94. };
  95. }
  96. #endif