LLVM  15.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  virtual unsigned emitDIERef(const DIE &D) = 0;
37 };
38 
39 class APByteStreamer final : public ByteStreamer {
40 private:
41  AsmPrinter &AP;
42 
43 public:
45  void emitInt8(uint8_t Byte, const Twine &Comment) override {
46  AP.OutStreamer->AddComment(Comment);
47  AP.emitInt8(Byte);
48  }
49  void emitSLEB128(uint64_t DWord, const Twine &Comment) override {
50  AP.OutStreamer->AddComment(Comment);
51  AP.emitSLEB128(DWord);
52  }
53  void emitULEB128(uint64_t DWord, const Twine &Comment,
54  unsigned PadTo) override {
55  AP.OutStreamer->AddComment(Comment);
56  AP.emitULEB128(DWord, nullptr, PadTo);
57  }
58  unsigned emitDIERef(const DIE &D) override {
59  uint64_t Offset = D.getOffset();
60  static constexpr unsigned ULEB128PadSize = 4;
61  assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit");
62  emitULEB128(Offset, "", ULEB128PadSize);
63  // Return how many comments to skip in DwarfDebug::emitDebugLocEntry to keep
64  // comments aligned with debug loc entries.
65  return ULEB128PadSize;
66  }
67 };
68 
69 class HashingByteStreamer final : public ByteStreamer {
70  private:
71  DIEHash &Hash;
72  public:
74  void emitInt8(uint8_t Byte, const Twine &Comment) override {
75  Hash.update(Byte);
76  }
77  void emitSLEB128(uint64_t DWord, const Twine &Comment) override {
78  Hash.addSLEB128(DWord);
79  }
80  void emitULEB128(uint64_t DWord, const Twine &Comment,
81  unsigned PadTo) override {
82  Hash.addULEB128(DWord);
83  }
84  unsigned emitDIERef(const DIE &D) override {
85  Hash.hashRawTypeReference(D);
86  return 0; // Only used together with the APByteStreamer.
87  }
88 };
89 
90 class BufferByteStreamer final : public ByteStreamer {
91 private:
92  SmallVectorImpl<char> &Buffer;
93  std::vector<std::string> &Comments;
94 
95 public:
96  /// Only verbose textual output needs comments. This will be set to
97  /// true for that case, and false otherwise. If false, comments passed in to
98  /// the emit methods will be ignored.
99  const bool GenerateComments;
100 
102  std::vector<std::string> &Comments, bool GenerateComments)
103  : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {
104  }
105  void emitInt8(uint8_t Byte, const Twine &Comment) override {
106  Buffer.push_back(Byte);
107  if (GenerateComments)
108  Comments.push_back(Comment.str());
109  }
110  void emitSLEB128(uint64_t DWord, const Twine &Comment) override {
111  raw_svector_ostream OSE(Buffer);
112  unsigned Length = encodeSLEB128(DWord, OSE);
113  if (GenerateComments) {
114  Comments.push_back(Comment.str());
115  // Add some empty comments to keep the Buffer and Comments vectors aligned
116  // with each other.
117  for (size_t i = 1; i < Length; ++i)
118  Comments.push_back("");
119 
120  }
121  }
122  void emitULEB128(uint64_t DWord, const Twine &Comment,
123  unsigned PadTo) override {
124  raw_svector_ostream OSE(Buffer);
125  unsigned Length = encodeULEB128(DWord, OSE, PadTo);
126  if (GenerateComments) {
127  Comments.push_back(Comment.str());
128  // Add some empty comments to keep the Buffer and Comments vectors aligned
129  // with each other.
130  for (size_t i = 1; i < Length; ++i)
131  Comments.push_back("");
132  }
133  }
134  unsigned emitDIERef(const DIE &D) override {
135  uint64_t Offset = D.getOffset();
136  static constexpr unsigned ULEB128PadSize = 4;
137  assert(Offset < (1ULL << (ULEB128PadSize * 7)) && "Offset wont fit");
138  emitULEB128(Offset, "", ULEB128PadSize);
139  return 0; // Only used together with the APByteStreamer.
140  }
141 };
142 
143 }
144 
145 #endif
i
i
Definition: README.txt:29
AsmPrinter.h
llvm::HashingByteStreamer::emitSLEB128
void emitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:77
llvm::DIEHash::hashRawTypeReference
void hashRawTypeReference(const DIE &Entry)
Definition: DIEHash.cpp:209
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
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:43
llvm::HashingByteStreamer::emitInt8
void emitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:74
llvm::APByteStreamer::emitDIERef
unsigned emitDIERef(const DIE &D) override
Definition: ByteStreamer.h:58
ULEB128PadSize
static constexpr unsigned ULEB128PadSize
Definition: DwarfDebug.cpp:171
llvm::AsmPrinter::emitSLEB128
void emitSLEB128(int64_t Value, const char *Desc=nullptr) const
Emit the specified signed leb128 value.
Definition: AsmPrinterDwarf.cpp:36
llvm::APByteStreamer
Definition: ByteStreamer.h:39
llvm::DIE
A structured debug information entry.
Definition: DIE.h:739
llvm::DIEHash::addULEB128
void addULEB128(uint64_t Value)
Encodes and adds.
Definition: DIEHash.cpp:52
llvm::DIEHash::addSLEB128
void addSLEB128(int64_t Value)
Encodes and adds.
Definition: DIEHash.cpp:63
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:99
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:96
llvm::HashingByteStreamer::emitULEB128
void emitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override
Definition: ByteStreamer.h:80
llvm::BufferByteStreamer::BufferByteStreamer
BufferByteStreamer(SmallVectorImpl< char > &Buffer, std::vector< std::string > &Comments, bool GenerateComments)
Definition: ByteStreamer.h:101
llvm::AsmPrinter::emitInt8
void emitInt8(int Value) const
Emit a byte directive and value.
Definition: AsmPrinter.cpp:2601
llvm::APByteStreamer::APByteStreamer
APByteStreamer(AsmPrinter &Asm)
Definition: ByteStreamer.h:44
llvm::BufferByteStreamer::emitULEB128
void emitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override
Definition: ByteStreamer.h:122
llvm::DIEHash::update
void update(uint8_t Value)
Adds.
Definition: DIEHash.h:57
llvm::HashingByteStreamer
Definition: ByteStreamer.h:69
uint64_t
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
LEB128.h
llvm::ByteStreamer::emitDIERef
virtual unsigned emitDIERef(const DIE &D)=0
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
DIEHash.h
llvm::BufferByteStreamer::emitDIERef
unsigned emitDIERef(const DIE &D) override
Definition: ByteStreamer.h:134
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:53
llvm::HashingByteStreamer::emitDIERef
unsigned emitDIERef(const DIE &D) override
Definition: ByteStreamer.h:84
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:49
H
#define H(x, y, z)
Definition: MD5.cpp:57
llvm::HashingByteStreamer::HashingByteStreamer
HashingByteStreamer(DIEHash &H)
Definition: ByteStreamer.h:73
llvm::AsmPrinter
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:81
llvm::BufferByteStreamer
Definition: ByteStreamer.h:90
llvm::BufferByteStreamer::emitInt8
void emitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:105
llvm::ByteStreamer::~ByteStreamer
~ByteStreamer()=default
llvm::APByteStreamer::emitInt8
void emitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:45
MCStreamer.h
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:662
llvm::BufferByteStreamer::emitSLEB128
void emitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:110
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