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/CodeGen/AsmPrinter.h"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/Support/LEB128.h"
27 ~ByteStreamer() = default;
28 ByteStreamer(const ByteStreamer&) = default;
29 ByteStreamer() = default;
32 // For now we're just handling the calls we need for dwarf emission/hashing.
33 virtual void EmitInt8(uint8_t Byte, const Twine &Comment = "") = 0;
34 virtual void EmitSLEB128(uint64_t DWord, const Twine &Comment = "") = 0;
35 virtual void EmitULEB128(uint64_t DWord, const Twine &Comment = "") = 0;
38 class APByteStreamer final : public ByteStreamer {
43 APByteStreamer(AsmPrinter &Asm) : AP(Asm) {}
44 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
45 AP.OutStreamer->AddComment(Comment);
48 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
49 AP.OutStreamer->AddComment(Comment);
50 AP.EmitSLEB128(DWord);
52 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
53 AP.OutStreamer->AddComment(Comment);
54 AP.EmitULEB128(DWord);
58 class HashingByteStreamer final : public ByteStreamer {
62 HashingByteStreamer(DIEHash &H) : Hash(H) {}
63 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
66 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
67 Hash.addSLEB128(DWord);
69 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
70 Hash.addULEB128(DWord);
74 class BufferByteStreamer final : public ByteStreamer {
76 SmallVectorImpl<char> &Buffer;
77 SmallVectorImpl<std::string> &Comments;
79 /// \brief 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 bool GenerateComments;
85 BufferByteStreamer(SmallVectorImpl<char> &Buffer,
86 SmallVectorImpl<std::string> &Comments,
87 bool GenerateComments)
88 : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {}
89 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
90 Buffer.push_back(Byte);
92 Comments.push_back(Comment.str());
94 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
95 raw_svector_ostream OSE(Buffer);
96 encodeSLEB128(DWord, OSE);
98 Comments.push_back(Comment.str());
100 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
101 raw_svector_ostream OSE(Buffer);
102 encodeULEB128(DWord, OSE);
103 if (GenerateComments)
104 Comments.push_back(Comment.str());