1 //===-- llvm/CodeGen/ByteStreamer.h - ByteStreamer class --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains a class that can take bytes that would normally be
10 // streamed via the AsmPrinter.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
18 #include "llvm/CodeGen/AsmPrinter.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/Support/LEB128.h"
26 ~ByteStreamer() = default;
27 ByteStreamer(const ByteStreamer
&) = default;
28 ByteStreamer() = default;
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
= "", unsigned PadTo
= 0) = 0;
37 class APByteStreamer final
: public ByteStreamer
{
42 APByteStreamer(AsmPrinter
&Asm
) : AP(Asm
) {}
43 void EmitInt8(uint8_t Byte
, const Twine
&Comment
) override
{
44 AP
.OutStreamer
->AddComment(Comment
);
47 void EmitSLEB128(uint64_t DWord
, const Twine
&Comment
) override
{
48 AP
.OutStreamer
->AddComment(Comment
);
49 AP
.EmitSLEB128(DWord
);
51 void EmitULEB128(uint64_t DWord
, const Twine
&Comment
, unsigned PadTo
) override
{
52 AP
.OutStreamer
->AddComment(Comment
);
53 AP
.EmitULEB128(DWord
);
57 class HashingByteStreamer final
: public ByteStreamer
{
61 HashingByteStreamer(DIEHash
&H
) : Hash(H
) {}
62 void EmitInt8(uint8_t Byte
, const Twine
&Comment
) override
{
65 void EmitSLEB128(uint64_t DWord
, const Twine
&Comment
) override
{
66 Hash
.addSLEB128(DWord
);
68 void EmitULEB128(uint64_t DWord
, const Twine
&Comment
, unsigned PadTo
) override
{
69 Hash
.addULEB128(DWord
);
73 class BufferByteStreamer final
: public ByteStreamer
{
75 SmallVectorImpl
<char> &Buffer
;
76 std::vector
<std::string
> &Comments
;
79 /// Only verbose textual output needs comments. This will be set to
80 /// true for that case, and false otherwise. If false, comments passed in to
81 /// the emit methods will be ignored.
82 const bool GenerateComments
;
84 BufferByteStreamer(SmallVectorImpl
<char> &Buffer
,
85 std::vector
<std::string
> &Comments
, bool GenerateComments
)
86 : Buffer(Buffer
), Comments(Comments
), GenerateComments(GenerateComments
) {
88 void EmitInt8(uint8_t Byte
, const Twine
&Comment
) override
{
89 Buffer
.push_back(Byte
);
91 Comments
.push_back(Comment
.str());
93 void EmitSLEB128(uint64_t DWord
, const Twine
&Comment
) override
{
94 raw_svector_ostream
OSE(Buffer
);
95 unsigned Length
= encodeSLEB128(DWord
, OSE
);
96 if (GenerateComments
) {
97 Comments
.push_back(Comment
.str());
98 // Add some empty comments to keep the Buffer and Comments vectors aligned
100 for (size_t i
= 1; i
< Length
; ++i
)
101 Comments
.push_back("");
105 void EmitULEB128(uint64_t DWord
, const Twine
&Comment
, unsigned PadTo
) override
{
106 raw_svector_ostream
OSE(Buffer
);
107 unsigned Length
= encodeULEB128(DWord
, OSE
, PadTo
);
108 if (GenerateComments
) {
109 Comments
.push_back(Comment
.str());
110 // Add some empty comments to keep the Buffer and Comments vectors aligned
112 for (size_t i
= 1; i
< Length
; ++i
)
113 Comments
.push_back("");