LLVM  3.7.0
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"
19 #include "llvm/ADT/ArrayRef.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:
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:
41  APByteStreamer(AsmPrinter &Asm) : AP(Asm) {}
42  void EmitInt8(uint8_t Byte, const Twine &Comment) override {
43  AP.OutStreamer->AddComment(Comment);
44  AP.EmitInt8(Byte);
45  }
46  void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
47  AP.OutStreamer->AddComment(Comment);
48  AP.EmitSLEB128(DWord);
49  }
50  void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
51  AP.OutStreamer->AddComment(Comment);
52  AP.EmitULEB128(DWord);
53  }
54 };
55 
57  private:
58  DIEHash &Hash;
59  public:
60  HashingByteStreamer(DIEHash &H) : Hash(H) {}
61  void EmitInt8(uint8_t Byte, const Twine &Comment) override {
62  Hash.update(Byte);
63  }
64  void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
65  Hash.addSLEB128(DWord);
66  }
67  void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
68  Hash.addULEB128(DWord);
69  }
70 };
71 
73 private:
74  SmallVectorImpl<char> &Buffer;
76 
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;
81 
82 public:
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);
89  if (GenerateComments)
90  Comments.push_back(Comment.str());
91  }
92  void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
93  raw_svector_ostream OSE(Buffer);
94  encodeSLEB128(DWord, OSE);
95  if (GenerateComments)
96  Comments.push_back(Comment.str());
97  }
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());
103  }
104 };
105 
106 }
107 
108 #endif
void push_back(const T &Elt)
Definition: SmallVector.h:222
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:83
void EmitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:64
void EmitInt8(int Value) const
Emit a byte directive and value.
void EmitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:61
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:488
void EmitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:87
BufferByteStreamer(SmallVectorImpl< char > &Buffer, SmallVectorImpl< std::string > &Comments, bool GenerateComments)
Definition: ByteStreamer.h:83
virtual void EmitULEB128(uint64_t DWord, const Twine &Comment="")=0
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:16
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
void EmitULEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:67
virtual void EmitInt8(uint8_t Byte, const Twine &Comment="")=0
void EmitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:46
void EmitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:92
void EmitULEB128(uint64_t Value, const char *Desc=nullptr, unsigned PadTo=0) const
Emit the specified unsigned leb128 value.
#define H(x, y, z)
Definition: MD5.cpp:53
HashingByteStreamer(DIEHash &H)
Definition: ByteStreamer.h:60
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:66
virtual void EmitSLEB128(uint64_t DWord, const Twine &Comment="")=0
void encodeSLEB128(int64_t Value, raw_ostream &OS)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
void update(uint8_t Value)
Adds.
Definition: DIEHash.h:110
void EmitSLEB128(int64_t Value, const char *Desc=nullptr) const
Emit the specified signed leb128 value.
void EmitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:42
void addSLEB128(int64_t Value)
Encodes and adds.
Definition: DIEHash.cpp:66
virtual ~ByteStreamer()
Definition: ByteStreamer.h:28
void encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned Padding=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:38
APByteStreamer(AsmPrinter &Asm)
Definition: ByteStreamer.h:41
void EmitULEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:98
void EmitULEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:50