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 25 namespace llvm { 26 class ByteStreamer { 27 public: ~ByteStreamer()28 virtual ~ByteStreamer() {} 29 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; 34 }; 35 36 class APByteStreamer : public ByteStreamer { 37 private: 38 AsmPrinter &AP; 39 40 public: APByteStreamer(AsmPrinter & Asm)41 APByteStreamer(AsmPrinter &Asm) : AP(Asm) {} EmitInt8(uint8_t Byte,const Twine & Comment)42 void EmitInt8(uint8_t Byte, const Twine &Comment) override { 43 AP.OutStreamer.AddComment(Comment); 44 AP.EmitInt8(Byte); 45 } EmitSLEB128(uint64_t DWord,const Twine & Comment)46 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override { 47 AP.OutStreamer.AddComment(Comment); 48 AP.EmitSLEB128(DWord); 49 } EmitULEB128(uint64_t DWord,const Twine & Comment)50 void EmitULEB128(uint64_t DWord, const Twine &Comment) override { 51 AP.OutStreamer.AddComment(Comment); 52 AP.EmitULEB128(DWord); 53 } 54 }; 55 56 class HashingByteStreamer : public ByteStreamer { 57 private: 58 DIEHash &Hash; 59 public: HashingByteStreamer(DIEHash & H)60 HashingByteStreamer(DIEHash &H) : Hash(H) {} EmitInt8(uint8_t Byte,const Twine & Comment)61 void EmitInt8(uint8_t Byte, const Twine &Comment) override { 62 Hash.update(Byte); 63 } EmitSLEB128(uint64_t DWord,const Twine & Comment)64 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override { 65 Hash.addSLEB128(DWord); 66 } EmitULEB128(uint64_t DWord,const Twine & Comment)67 void EmitULEB128(uint64_t DWord, const Twine &Comment) override { 68 Hash.addULEB128(DWord); 69 } 70 }; 71 72 class BufferByteStreamer : public ByteStreamer { 73 private: 74 SmallVectorImpl<char> &Buffer; 75 // FIXME: This is actually only needed for textual asm output. 76 SmallVectorImpl<std::string> &Comments; 77 78 public: BufferByteStreamer(SmallVectorImpl<char> & Buffer,SmallVectorImpl<std::string> & Comments)79 BufferByteStreamer(SmallVectorImpl<char> &Buffer, 80 SmallVectorImpl<std::string> &Comments) 81 : Buffer(Buffer), Comments(Comments) {} EmitInt8(uint8_t Byte,const Twine & Comment)82 void EmitInt8(uint8_t Byte, const Twine &Comment) override { 83 Buffer.push_back(Byte); 84 Comments.push_back(Comment.str()); 85 } EmitSLEB128(uint64_t DWord,const Twine & Comment)86 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override { 87 raw_svector_ostream OSE(Buffer); 88 encodeSLEB128(DWord, OSE); 89 Comments.push_back(Comment.str()); 90 } EmitULEB128(uint64_t DWord,const Twine & Comment)91 void EmitULEB128(uint64_t DWord, const Twine &Comment) override { 92 raw_svector_ostream OSE(Buffer); 93 encodeULEB128(DWord, OSE); 94 Comments.push_back(Comment.str()); 95 } 96 }; 97 98 } 99 100 #endif 101