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 virtual ~ByteStreamer() {}
30 // For now we're just handling the calls we need for dwarf emission/hashing.
31 virtual void EmitInt8(uint8_t Byte, const Twine &Comment = "") = 0;
32 virtual void EmitSLEB128(uint64_t DWord, const Twine &Comment = "") = 0;
33 virtual void EmitULEB128(uint64_t DWord, const Twine &Comment = "") = 0;
36 class APByteStreamer : public ByteStreamer {
41 APByteStreamer(AsmPrinter &Asm) : AP(Asm) {}
42 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
43 AP.OutStreamer->AddComment(Comment);
46 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
47 AP.OutStreamer->AddComment(Comment);
48 AP.EmitSLEB128(DWord);
50 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
51 AP.OutStreamer->AddComment(Comment);
52 AP.EmitULEB128(DWord);
56 class HashingByteStreamer : public ByteStreamer {
60 HashingByteStreamer(DIEHash &H) : Hash(H) {}
61 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
64 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
65 Hash.addSLEB128(DWord);
67 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
68 Hash.addULEB128(DWord);
72 class BufferByteStreamer : public ByteStreamer {
74 SmallVectorImpl<char> &Buffer;
75 SmallVectorImpl<std::string> &Comments;
77 /// \brief Only verbose textual output needs comments. This will be set to
78 /// true for that case, and false otherwise. If false, comments passed in to
79 /// the emit methods will be ignored.
80 bool GenerateComments;
83 BufferByteStreamer(SmallVectorImpl<char> &Buffer,
84 SmallVectorImpl<std::string> &Comments,
85 bool GenerateComments)
86 : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {}
87 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
88 Buffer.push_back(Byte);
90 Comments.push_back(Comment.str());
92 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
93 raw_svector_ostream OSE(Buffer);
94 encodeSLEB128(DWord, OSE);
96 Comments.push_back(Comment.str());
98 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
99 raw_svector_ostream OSE(Buffer);
100 encodeULEB128(DWord, OSE);
101 if (GenerateComments)
102 Comments.push_back(Comment.str());