LLVM  6.0.0svn
ByteStreamer.h
Go to the documentation of this file.
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"
20 #include "llvm/MC/MCStreamer.h"
21 #include "llvm/Support/LEB128.h"
22 #include <string>
23 
24 namespace llvm {
25 class ByteStreamer {
26  protected:
27  ~ByteStreamer() = default;
28  ByteStreamer(const ByteStreamer&) = default;
29  ByteStreamer() = default;
30 
31  public:
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;
36 };
37 
38 class APByteStreamer final : public ByteStreamer {
39 private:
40  AsmPrinter &AP;
41 
42 public:
43  APByteStreamer(AsmPrinter &Asm) : AP(Asm) {}
44  void EmitInt8(uint8_t Byte, const Twine &Comment) override {
45  AP.OutStreamer->AddComment(Comment);
46  AP.EmitInt8(Byte);
47  }
48  void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
49  AP.OutStreamer->AddComment(Comment);
50  AP.EmitSLEB128(DWord);
51  }
52  void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
53  AP.OutStreamer->AddComment(Comment);
54  AP.EmitULEB128(DWord);
55  }
56 };
57 
58 class HashingByteStreamer final : public ByteStreamer {
59  private:
60  DIEHash &Hash;
61  public:
62  HashingByteStreamer(DIEHash &H) : Hash(H) {}
63  void EmitInt8(uint8_t Byte, const Twine &Comment) override {
64  Hash.update(Byte);
65  }
66  void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
67  Hash.addSLEB128(DWord);
68  }
69  void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
70  Hash.addULEB128(DWord);
71  }
72 };
73 
74 class BufferByteStreamer final : public ByteStreamer {
75 private:
76  SmallVectorImpl<char> &Buffer;
78 
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;
83 
84 public:
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);
91  if (GenerateComments)
92  Comments.push_back(Comment.str());
93  }
94  void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
95  raw_svector_ostream OSE(Buffer);
96  encodeSLEB128(DWord, OSE);
97  if (GenerateComments)
98  Comments.push_back(Comment.str());
99  }
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());
105  }
106 };
107 
108 }
109 
110 #endif
void push_back(const T &Elt)
Definition: SmallVector.h:212
An object containing the capability of hashing and adding hash attributes onto a DIE.
Definition: DIEHash.h:28
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:92
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void EmitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:66
void EmitInt8(int Value) const
Emit a byte directive and value.
void EmitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:63
void addULEB128(uint64_t Value)
Encodes and adds.
Definition: DIEHash.cpp:55
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:489
void EmitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:89
BufferByteStreamer(SmallVectorImpl< char > &Buffer, SmallVectorImpl< std::string > &Comments, bool GenerateComments)
Definition: ByteStreamer.h:85
virtual void EmitULEB128(uint64_t DWord, const Twine &Comment="")=0
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
void EmitULEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:69
virtual void EmitInt8(uint8_t Byte, const Twine &Comment="")=0
void EmitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:48
ByteStreamer()=default
void EmitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:94
void encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:77
#define H(x, y, z)
Definition: MD5.cpp:57
HashingByteStreamer(DIEHash &H)
Definition: ByteStreamer.h:62
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:77
void encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
void EmitSLEB128(int64_t Value, const char *Desc=nullptr) const
Emit the specified signed leb128 value.
virtual void EmitSLEB128(uint64_t DWord, const Twine &Comment="")=0
void update(uint8_t Value)
Adds.
Definition: DIEHash.h:58
~ByteStreamer()=default
void EmitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:44
void addSLEB128(int64_t Value)
Encodes and adds.
Definition: DIEHash.cpp:66
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:17
void EmitULEB128(uint64_t Value, const char *Desc=nullptr) const
Emit the specified unsigned leb128 value.
APByteStreamer(AsmPrinter &Asm)
Definition: ByteStreamer.h:43
void EmitULEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:100
void EmitULEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:52