Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 15 | #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H |
| 16 | #define LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 17 | |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 18 | #include "DIEHash.h" |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/AsmPrinter.h" |
| 20 | #include "llvm/MC/MCStreamer.h" |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 21 | #include "llvm/Support/LEB128.h" |
| 22 | #include <string> |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
| 25 | class ByteStreamer { |
David Blaikie | e44a8a7 | 2015-08-03 20:12:58 +0000 | [diff] [blame] | 26 | protected: |
| 27 | ~ByteStreamer() = default; |
| 28 | ByteStreamer(const ByteStreamer&) = default; |
| 29 | ByteStreamer() = default; |
Eric Christopher | 1d6d1c8 | 2014-03-07 22:53:36 +0000 | [diff] [blame] | 30 | |
David Blaikie | e44a8a7 | 2015-08-03 20:12:58 +0000 | [diff] [blame] | 31 | public: |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 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 | |
David Blaikie | e44a8a7 | 2015-08-03 20:12:58 +0000 | [diff] [blame] | 38 | class APByteStreamer final : public ByteStreamer { |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 39 | private: |
| 40 | AsmPrinter &AP; |
| 41 | |
| 42 | public: |
| 43 | APByteStreamer(AsmPrinter &Asm) : AP(Asm) {} |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 44 | void EmitInt8(uint8_t Byte, const Twine &Comment) override { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 45 | AP.OutStreamer->AddComment(Comment); |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 46 | AP.EmitInt8(Byte); |
| 47 | } |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 48 | void EmitSLEB128(uint64_t DWord, const Twine &Comment) override { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 49 | AP.OutStreamer->AddComment(Comment); |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 50 | AP.EmitSLEB128(DWord); |
| 51 | } |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 52 | void EmitULEB128(uint64_t DWord, const Twine &Comment) override { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 53 | AP.OutStreamer->AddComment(Comment); |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 54 | AP.EmitULEB128(DWord); |
| 55 | } |
| 56 | }; |
| 57 | |
David Blaikie | e44a8a7 | 2015-08-03 20:12:58 +0000 | [diff] [blame] | 58 | class HashingByteStreamer final : public ByteStreamer { |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 59 | private: |
| 60 | DIEHash &Hash; |
| 61 | public: |
| 62 | HashingByteStreamer(DIEHash &H) : Hash(H) {} |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 63 | void EmitInt8(uint8_t Byte, const Twine &Comment) override { |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 64 | Hash.update(Byte); |
| 65 | } |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 66 | void EmitSLEB128(uint64_t DWord, const Twine &Comment) override { |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 67 | Hash.addSLEB128(DWord); |
| 68 | } |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 69 | void EmitULEB128(uint64_t DWord, const Twine &Comment) override { |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 70 | Hash.addULEB128(DWord); |
| 71 | } |
| 72 | }; |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 73 | |
David Blaikie | e44a8a7 | 2015-08-03 20:12:58 +0000 | [diff] [blame] | 74 | class BufferByteStreamer final : public ByteStreamer { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 75 | private: |
| 76 | SmallVectorImpl<char> &Buffer; |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 77 | SmallVectorImpl<std::string> &Comments; |
| 78 | |
Pete Cooper | a05c082 | 2015-05-20 22:51:27 +0000 | [diff] [blame] | 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 | |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 84 | public: |
| 85 | BufferByteStreamer(SmallVectorImpl<char> &Buffer, |
Pete Cooper | a05c082 | 2015-05-20 22:51:27 +0000 | [diff] [blame] | 86 | SmallVectorImpl<std::string> &Comments, |
| 87 | bool GenerateComments) |
| 88 | : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {} |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 89 | void EmitInt8(uint8_t Byte, const Twine &Comment) override { |
| 90 | Buffer.push_back(Byte); |
Pete Cooper | a05c082 | 2015-05-20 22:51:27 +0000 | [diff] [blame] | 91 | if (GenerateComments) |
| 92 | Comments.push_back(Comment.str()); |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 93 | } |
| 94 | void EmitSLEB128(uint64_t DWord, const Twine &Comment) override { |
| 95 | raw_svector_ostream OSE(Buffer); |
| 96 | encodeSLEB128(DWord, OSE); |
Pete Cooper | a05c082 | 2015-05-20 22:51:27 +0000 | [diff] [blame] | 97 | if (GenerateComments) |
| 98 | Comments.push_back(Comment.str()); |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 99 | } |
| 100 | void EmitULEB128(uint64_t DWord, const Twine &Comment) override { |
| 101 | raw_svector_ostream OSE(Buffer); |
| 102 | encodeULEB128(DWord, OSE); |
Pete Cooper | a05c082 | 2015-05-20 22:51:27 +0000 | [diff] [blame] | 103 | if (GenerateComments) |
| 104 | Comments.push_back(Comment.str()); |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 105 | } |
| 106 | }; |
| 107 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 108 | } |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 109 | |
| 110 | #endif |