1 //===-- llvm/CodeGen/ByteStreamer.h - ByteStreamer class --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a class that can take bytes that would normally be
11 // streamed via the AsmPrinter.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
16 #define LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/CodeGen/AsmPrinter.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/Support/LEB128.h"
28 ~ByteStreamer() = default;
29 ByteStreamer(const ByteStreamer&) = default;
30 ByteStreamer() = default;
33 // For now we're just handling the calls we need for dwarf emission/hashing.
34 virtual void EmitInt8(uint8_t Byte, const Twine &Comment = "") = 0;
35 virtual void EmitSLEB128(uint64_t DWord, const Twine &Comment = "") = 0;
36 virtual void EmitULEB128(uint64_t DWord, const Twine &Comment = "") = 0;
39 class APByteStreamer final : public ByteStreamer {
44 APByteStreamer(AsmPrinter &Asm) : AP(Asm) {}
45 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
46 AP.OutStreamer->AddComment(Comment);
49 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
50 AP.OutStreamer->AddComment(Comment);
51 AP.EmitSLEB128(DWord);
53 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
54 AP.OutStreamer->AddComment(Comment);
55 AP.EmitULEB128(DWord);
59 class HashingByteStreamer final : public ByteStreamer {
63 HashingByteStreamer(DIEHash &H) : Hash(H) {}
64 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
67 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
68 Hash.addSLEB128(DWord);
70 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
71 Hash.addULEB128(DWord);
75 class BufferByteStreamer final : public ByteStreamer {
77 SmallVectorImpl<char> &Buffer;
78 SmallVectorImpl<std::string> &Comments;
80 /// \brief Only verbose textual output needs comments. This will be set to
81 /// true for that case, and false otherwise. If false, comments passed in to
82 /// the emit methods will be ignored.
83 bool GenerateComments;
86 BufferByteStreamer(SmallVectorImpl<char> &Buffer,
87 SmallVectorImpl<std::string> &Comments,
88 bool GenerateComments)
89 : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {}
90 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
91 Buffer.push_back(Byte);
93 Comments.push_back(Comment.str());
95 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
96 raw_svector_ostream OSE(Buffer);
97 encodeSLEB128(DWord, OSE);
99 Comments.push_back(Comment.str());
101 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
102 raw_svector_ostream OSE(Buffer);
103 encodeULEB128(DWord, OSE);
104 if (GenerateComments)
105 Comments.push_back(Comment.str());