LLVM  14.0.0git
ByteStreamer.h
Go to the documentation of this file.
1 //===-- llvm/CodeGen/ByteStreamer.h - ByteStreamer class --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains a class that can take bytes that would normally be
10 // streamed via the AsmPrinter.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
16 
17 #include "DIEHash.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/Support/LEB128.h"
21 #include <string>
22 
23 namespace llvm {
24 class ByteStreamer {
25  protected:
26  ~ByteStreamer() = default;
27  ByteStreamer(const ByteStreamer&) = default;
28  ByteStreamer() = default;
29 
30  public:
31  // For now we're just handling the calls we need for dwarf emission/hashing.
32  virtual void emitInt8(uint8_t Byte, const Twine &Comment = "") = 0;
33  virtual void emitSLEB128(uint64_t DWord, const Twine &Comment = "") = 0;
34  virtual void emitULEB128(uint64_t DWord, const Twine &Comment = "",
35  unsigned PadTo = 0) = 0;
36 };
37 
38 class APByteStreamer final : public ByteStreamer {
39 private:
40  AsmPrinter &AP;
41 
42 public:
44  void emitInt8(uint8_t Byte, const Twine &Comment) override {
45  AP.OutStreamer->AddComment(Comment);
46  AP.emitInt8(Byte);
47  }
48  void emitSLEB128(uint64_t DWord, const Twine &Comment) override {
49  AP.OutStreamer->AddComment(Comment);
50  AP.emitSLEB128(DWord);
51  }
52  void emitULEB128(uint64_t DWord, const Twine &Comment,
53  unsigned PadTo) override {
54  AP.OutStreamer->AddComment(Comment);
55  AP.emitULEB128(DWord, nullptr, PadTo);
56  }
57 };
58 
59 class HashingByteStreamer final : public ByteStreamer {
60  private:
61  DIEHash &Hash;
62  public:
64  void emitInt8(uint8_t Byte, const Twine &Comment) override {
65  Hash.update(Byte);
66  }
67  void emitSLEB128(uint64_t DWord, const Twine &Comment) override {
68  Hash.addSLEB128(DWord);
69  }
70  void emitULEB128(uint64_t DWord, const Twine &Comment,
71  unsigned PadTo) override {
72  Hash.addULEB128(DWord);
73  }
74 };
75 
76 class BufferByteStreamer final : public ByteStreamer {
77 private:
78  SmallVectorImpl<char> &Buffer;
79  std::vector<std::string> &Comments;
80 
81 public:
82  /// Only verbose textual output needs comments. This will be set to
83  /// true for that case, and false otherwise. If false, comments passed in to
84  /// the emit methods will be ignored.
85  const bool GenerateComments;
86 
88  std::vector<std::string> &Comments, bool GenerateComments)
89  : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {
90  }
91  void emitInt8(uint8_t Byte, const Twine &Comment) override {
92  Buffer.push_back(Byte);
93  if (GenerateComments)
94  Comments.push_back(Comment.str());
95  }
96  void emitSLEB128(uint64_t DWord, const Twine &Comment) override {
97  raw_svector_ostream OSE(Buffer);
98  unsigned Length = encodeSLEB128(DWord, OSE);
99  if (GenerateComments) {
100  Comments.push_back(Comment.str());
101  // Add some empty comments to keep the Buffer and Comments vectors aligned
102  // with each other.
103  for (size_t i = 1; i < Length; ++i)
104  Comments.push_back("");
105 
106  }
107  }
108  void emitULEB128(uint64_t DWord, const Twine &Comment,
109  unsigned PadTo) override {
110  raw_svector_ostream OSE(Buffer);
111  unsigned Length = encodeULEB128(DWord, OSE, PadTo);
112  if (GenerateComments) {
113  Comments.push_back(Comment.str());
114  // Add some empty comments to keep the Buffer and Comments vectors aligned
115  // with each other.
116  for (size_t i = 1; i < Length; ++i)
117  Comments.push_back("");
118 
119  }
120  }
121 };
122 
123 }
124 
125 #endif
i
i
Definition: README.txt:29
AsmPrinter.h
llvm::HashingByteStreamer::emitSLEB128
void emitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:67
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::AsmPrinter::emitULEB128
void emitULEB128(uint64_t Value, const char *Desc=nullptr, unsigned PadTo=0) const
Emit the specified unsigned leb128 value.
Definition: AsmPrinterDwarf.cpp:47
llvm::HashingByteStreamer::emitInt8
void emitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:64
llvm::AsmPrinter::emitSLEB128
void emitSLEB128(int64_t Value, const char *Desc=nullptr) const
Emit the specified signed leb128 value.
Definition: AsmPrinterDwarf.cpp:40
llvm::APByteStreamer
Definition: ByteStreamer.h:38
llvm::DIEHash::addULEB128
void addULEB128(uint64_t Value)
Encodes and adds.
Definition: DIEHash.cpp:53
llvm::DIEHash::addSLEB128
void addSLEB128(int64_t Value)
Encodes and adds.
Definition: DIEHash.cpp:64
llvm::ByteStreamer::emitSLEB128
virtual void emitSLEB128(uint64_t DWord, const Twine &Comment="")=0
llvm::DIEHash
An object containing the capability of hashing and adding hash attributes onto a DIE.
Definition: DIEHash.h:26
llvm::BufferByteStreamer::GenerateComments
const bool GenerateComments
Only verbose textual output needs comments.
Definition: ByteStreamer.h:85
llvm::encodeSLEB128
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
llvm::AsmPrinter::OutStreamer
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:97
llvm::HashingByteStreamer::emitULEB128
void emitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override
Definition: ByteStreamer.h:70
llvm::BufferByteStreamer::BufferByteStreamer
BufferByteStreamer(SmallVectorImpl< char > &Buffer, std::vector< std::string > &Comments, bool GenerateComments)
Definition: ByteStreamer.h:87
llvm::AsmPrinter::emitInt8
void emitInt8(int Value) const
Emit a byte directive and value.
Definition: AsmPrinter.cpp:2401
llvm::Twine::str
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:17
llvm::APByteStreamer::APByteStreamer
APByteStreamer(AsmPrinter &Asm)
Definition: ByteStreamer.h:43
llvm::BufferByteStreamer::emitULEB128
void emitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override
Definition: ByteStreamer.h:108
llvm::DIEHash::update
void update(uint8_t Value)
Adds.
Definition: DIEHash.h:57
llvm::HashingByteStreamer
Definition: ByteStreamer.h:59
uint64_t
LEB128.h
DIEHash.h
llvm::ByteStreamer::emitInt8
virtual void emitInt8(uint8_t Byte, const Twine &Comment="")=0
llvm::APByteStreamer::emitULEB128
void emitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override
Definition: ByteStreamer.h:52
llvm::ByteStreamer::ByteStreamer
ByteStreamer()=default
llvm::ByteStreamer::emitULEB128
virtual void emitULEB128(uint64_t DWord, const Twine &Comment="", unsigned PadTo=0)=0
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::APByteStreamer::emitSLEB128
void emitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:48
H
#define H(x, y, z)
Definition: MD5.cpp:58
llvm::HashingByteStreamer::HashingByteStreamer
HashingByteStreamer(DIEHash &H)
Definition: ByteStreamer.h:63
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:82
llvm::BufferByteStreamer
Definition: ByteStreamer.h:76
llvm::BufferByteStreamer::emitInt8
void emitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:91
llvm::ByteStreamer::~ByteStreamer
~ByteStreamer()=default
llvm::APByteStreamer::emitInt8
void emitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:44
MCStreamer.h
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:658
llvm::BufferByteStreamer::emitSLEB128
void emitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:96
llvm::encodeULEB128
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80
llvm::SmallVectorImpl< char >
llvm::HexStyle::Asm
@ Asm
0ffh
Definition: MCInstPrinter.h:34
llvm::ByteStreamer
Definition: ByteStreamer.h:24