ByteStreamer.h revision 296417
1//===-- llvm/CodeGen/ByteStreamer.h - ByteStreamer class --------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file contains a class that can take bytes that would normally be 11// streamed via the AsmPrinter. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H 16#define LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H 17 18#include "DIEHash.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" 23#include <string> 24 25namespace llvm { 26class ByteStreamer { 27 protected: 28 ~ByteStreamer() = default; 29 ByteStreamer(const ByteStreamer&) = default; 30 ByteStreamer() = default; 31 32 public: 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; 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) override { 54 AP.OutStreamer->AddComment(Comment); 55 AP.EmitULEB128(DWord); 56 } 57}; 58 59class HashingByteStreamer final : public ByteStreamer { 60 private: 61 DIEHash &Hash; 62 public: 63 HashingByteStreamer(DIEHash &H) : Hash(H) {} 64 void EmitInt8(uint8_t Byte, const Twine &Comment) override { 65 Hash.update(Byte); 66 } 67 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override { 68 Hash.addSLEB128(DWord); 69 } 70 void EmitULEB128(uint64_t DWord, const Twine &Comment) override { 71 Hash.addULEB128(DWord); 72 } 73}; 74 75class BufferByteStreamer final : public ByteStreamer { 76private: 77 SmallVectorImpl<char> &Buffer; 78 SmallVectorImpl<std::string> &Comments; 79 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; 84 85public: 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); 92 if (GenerateComments) 93 Comments.push_back(Comment.str()); 94 } 95 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override { 96 raw_svector_ostream OSE(Buffer); 97 encodeSLEB128(DWord, OSE); 98 if (GenerateComments) 99 Comments.push_back(Comment.str()); 100 } 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()); 106 } 107}; 108 109} 110 111#endif 112