LLVM  10.0.0svn
ByteStreamer.h
Go to the documentation of this file.
1 //===-- llvm/CodeGen/ByteStreamer.h - ByteStreamer class --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains a class that can take bytes that would normally be
10 // streamed via the AsmPrinter.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
16 
17 #include "DIEHash.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/Support/LEB128.h"
21 #include <string>
22 
23 namespace llvm {
24 class ByteStreamer {
25  protected:
26  ~ByteStreamer() = default;
27  ByteStreamer(const ByteStreamer&) = default;
28  ByteStreamer() = default;
29 
30  public:
31  // For now we're just handling the calls we need for dwarf emission/hashing.
32  virtual void EmitInt8(uint8_t Byte, const Twine &Comment = "") = 0;
33  virtual void EmitSLEB128(uint64_t DWord, const Twine &Comment = "") = 0;
34  virtual void EmitULEB128(uint64_t DWord, const Twine &Comment = "", unsigned PadTo = 0) = 0;
35 };
36 
37 class APByteStreamer final : public ByteStreamer {
38 private:
39  AsmPrinter &AP;
40 
41 public:
42  APByteStreamer(AsmPrinter &Asm) : AP(Asm) {}
43  void EmitInt8(uint8_t Byte, const Twine &Comment) override {
44  AP.OutStreamer->AddComment(Comment);
45  AP.emitInt8(Byte);
46  }
47  void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
48  AP.OutStreamer->AddComment(Comment);
49  AP.EmitSLEB128(DWord);
50  }
51  void EmitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override {
52  AP.OutStreamer->AddComment(Comment);
53  AP.EmitULEB128(DWord);
54  }
55 };
56 
57 class HashingByteStreamer final : public ByteStreamer {
58  private:
59  DIEHash &Hash;
60  public:
61  HashingByteStreamer(DIEHash &H) : Hash(H) {}
62  void EmitInt8(uint8_t Byte, const Twine &Comment) override {
63  Hash.update(Byte);
64  }
65  void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
66  Hash.addSLEB128(DWord);
67  }
68  void EmitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override {
69  Hash.addULEB128(DWord);
70  }
71 };
72 
73 class BufferByteStreamer final : public ByteStreamer {
74 private:
75  SmallVectorImpl<char> &Buffer;
76  std::vector<std::string> &Comments;
77 
78 public:
79  /// 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  const bool GenerateComments;
83 
85  std::vector<std::string> &Comments, bool GenerateComments)
86  : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {
87  }
88  void EmitInt8(uint8_t Byte, const Twine &Comment) override {
89  Buffer.push_back(Byte);
90  if (GenerateComments)
91  Comments.push_back(Comment.str());
92  }
93  void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
94  raw_svector_ostream OSE(Buffer);
95  unsigned Length = encodeSLEB128(DWord, OSE);
96  if (GenerateComments) {
97  Comments.push_back(Comment.str());
98  // Add some empty comments to keep the Buffer and Comments vectors aligned
99  // with each other.
100  for (size_t i = 1; i < Length; ++i)
101  Comments.push_back("");
102 
103  }
104  }
105  void EmitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override {
106  raw_svector_ostream OSE(Buffer);
107  unsigned Length = encodeULEB128(DWord, OSE, PadTo);
108  if (GenerateComments) {
109  Comments.push_back(Comment.str());
110  // Add some empty comments to keep the Buffer and Comments vectors aligned
111  // with each other.
112  for (size_t i = 1; i < Length; ++i)
113  Comments.push_back("");
114 
115  }
116  }
117 };
118 
119 }
120 
121 #endif
An object containing the capability of hashing and adding hash attributes onto a DIE.
Definition: DIEHash.h:27
std::unique_ptr< MCStreamer > OutStreamer
This is the MCStreamer object for the file we are generating.
Definition: AsmPrinter.h:93
void EmitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override
Definition: ByteStreamer.h:68
This class represents lattice values for constants.
Definition: AllocatorList.h:23
void EmitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:65
void EmitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:62
void push_back(const T &Elt)
Definition: SmallVector.h:211
void addULEB128(uint64_t Value)
Encodes and adds.
Definition: DIEHash.cpp:54
void EmitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override
Definition: ByteStreamer.h:51
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:530
void EmitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:88
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
virtual void EmitInt8(uint8_t Byte, const Twine &Comment="")=0
BufferByteStreamer(SmallVectorImpl< char > &Buffer, std::vector< std::string > &Comments, bool GenerateComments)
Definition: ByteStreamer.h:84
void EmitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:47
ByteStreamer()=default
void EmitSLEB128(uint64_t DWord, const Twine &Comment) override
Definition: ByteStreamer.h:93
void EmitULEB128(uint64_t DWord, const Twine &Comment, unsigned PadTo) override
Definition: ByteStreamer.h:105
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:57
HashingByteStreamer(DIEHash &H)
Definition: ByteStreamer.h:61
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
virtual void EmitULEB128(uint64_t DWord, const Twine &Comment="", unsigned PadTo=0)=0
void EmitSLEB128(int64_t Value, const char *Desc=nullptr) const
Emit the specified signed leb128 value.
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:80
unsigned 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
virtual void EmitSLEB128(uint64_t DWord, const Twine &Comment="")=0
void update(uint8_t Value)
Adds.
Definition: DIEHash.h:57
~ByteStreamer()=default
void EmitInt8(uint8_t Byte, const Twine &Comment) override
Definition: ByteStreamer.h:43
void addSLEB128(int64_t Value)
Encodes and adds.
Definition: DIEHash.cpp:65
std::string str() const
Return the twine contents as a std::string.
Definition: Twine.cpp:17
const bool GenerateComments
Only verbose textual output needs comments.
Definition: ByteStreamer.h:82
void emitInt8(int Value) const
Emit a byte directive and value.
APByteStreamer(AsmPrinter &Asm)
Definition: ByteStreamer.h:42