LLVM 17.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
23namespace llvm {
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
39class APByteStreamer final : public ByteStreamer {
40private:
41 AsmPrinter &AP;
42
43public:
44 APByteStreamer(AsmPrinter &Asm) : AP(Asm) {}
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");
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
69class 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 {
86 return 0; // Only used together with the APByteStreamer.
87 }
88};
89
90class BufferByteStreamer final : public ByteStreamer {
91private:
93 std::vector<std::string> &Comments;
94
95public:
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);
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");
139 return 0; // Only used together with the APByteStreamer.
140 }
141};
142
143}
144
145#endif
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static constexpr unsigned ULEB128PadSize
Definition: DwarfDebug.cpp:172
#define H(x, y, z)
Definition: MD5.cpp:57
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned emitDIERef(const DIE &D) override
Definition: ByteStreamer.h:58
APByteStreamer(AsmPrinter &Asm)
Definition: ByteStreamer.h:44
void emitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override
Definition: ByteStreamer.h:53
void emitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:49
void emitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:45
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:84
void emitULEB128(uint64_t Value, const char *Desc=nullptr, unsigned PadTo=0) const
Emit the specified unsigned leb128 value.
void emitInt8(int Value) const
Emit a byte directive and value.
void emitSLEB128(int64_t Value, const char *Desc=nullptr) const
Emit the specified signed leb128 value.
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:99
void emitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:105
unsigned emitDIERef(const DIE &D) override
Definition: ByteStreamer.h:134
void emitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override
Definition: ByteStreamer.h:122
void emitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:110
const bool GenerateComments
Only verbose textual output needs comments.
Definition: ByteStreamer.h:99
BufferByteStreamer(SmallVectorImpl< char > &Buffer, std::vector< std::string > &Comments, bool GenerateComments)
Definition: ByteStreamer.h:101
virtual void emitULEB128(uint64_t DWord, const Twine &Comment="", unsigned PadTo=0)=0
ByteStreamer()=default
virtual void emitSLEB128(uint64_t DWord, const Twine &Comment="")=0
virtual void emitInt8(uint8_t Byte, const Twine &Comment="")=0
virtual unsigned emitDIERef(const DIE &D)=0
ByteStreamer(const ByteStreamer &)=default
~ByteStreamer()=default
An object containing the capability of hashing and adding hash attributes onto a DIE.
Definition: DIEHash.h:26
void update(uint8_t Value)
Adds.
Definition: DIEHash.h:57
void hashRawTypeReference(const DIE &Entry)
Definition: DIEHash.cpp:209
void addSLEB128(int64_t Value)
Encodes and adds.
Definition: DIEHash.cpp:63
void addULEB128(uint64_t Value)
Encodes and adds.
Definition: DIEHash.cpp:52
A structured debug information entry.
Definition: DIE.h:739
HashingByteStreamer(DIEHash &H)
Definition: ByteStreamer.h:73
void emitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override
Definition: ByteStreamer.h:80
void emitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:77
void emitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:74
unsigned emitDIERef(const DIE &D) override
Definition: ByteStreamer.h:84
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:577
void push_back(const T &Elt)
Definition: SmallVector.h:416
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:672
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:406
@ Length
Definition: DWP.cpp:406
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
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