Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/ByteStreamer.h - ByteStreamer class --------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 14 | #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H |
| 15 | #define LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 16 | |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 17 | #include "DIEHash.h" |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/AsmPrinter.h" |
| 19 | #include "llvm/MC/MCStreamer.h" |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 20 | #include "llvm/Support/LEB128.h" |
| 21 | #include <string> |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 22 | |
| 23 | namespace llvm { |
| 24 | class ByteStreamer { |
David Blaikie | e44a8a7 | 2015-08-03 20:12:58 +0000 | [diff] [blame] | 25 | protected: |
| 26 | ~ByteStreamer() = default; |
| 27 | ByteStreamer(const ByteStreamer&) = default; |
| 28 | ByteStreamer() = default; |
Eric Christopher | 1d6d1c8 | 2014-03-07 22:53:36 +0000 | [diff] [blame] | 29 | |
David Blaikie | e44a8a7 | 2015-08-03 20:12:58 +0000 | [diff] [blame] | 30 | public: |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 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 = "") = 0; |
| 35 | }; |
| 36 | |
David Blaikie | e44a8a7 | 2015-08-03 20:12:58 +0000 | [diff] [blame] | 37 | class APByteStreamer final : public ByteStreamer { |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 38 | private: |
| 39 | AsmPrinter &AP; |
| 40 | |
| 41 | public: |
| 42 | APByteStreamer(AsmPrinter &Asm) : AP(Asm) {} |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 43 | void EmitInt8(uint8_t Byte, const Twine &Comment) override { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 44 | AP.OutStreamer->AddComment(Comment); |
Rafael Espindola | 4b4d85f | 2018-03-29 23:32:54 +0000 | [diff] [blame] | 45 | AP.emitInt8(Byte); |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 46 | } |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 47 | void EmitSLEB128(uint64_t DWord, const Twine &Comment) override { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 48 | AP.OutStreamer->AddComment(Comment); |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 49 | AP.EmitSLEB128(DWord); |
| 50 | } |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 51 | void EmitULEB128(uint64_t DWord, const Twine &Comment) override { |
Lang Hames | 9ff69c8 | 2015-04-24 19:11:51 +0000 | [diff] [blame] | 52 | AP.OutStreamer->AddComment(Comment); |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 53 | AP.EmitULEB128(DWord); |
| 54 | } |
| 55 | }; |
| 56 | |
David Blaikie | e44a8a7 | 2015-08-03 20:12:58 +0000 | [diff] [blame] | 57 | class HashingByteStreamer final : public ByteStreamer { |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 58 | private: |
| 59 | DIEHash &Hash; |
| 60 | public: |
| 61 | HashingByteStreamer(DIEHash &H) : Hash(H) {} |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 62 | void EmitInt8(uint8_t Byte, const Twine &Comment) override { |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 63 | Hash.update(Byte); |
| 64 | } |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 65 | void EmitSLEB128(uint64_t DWord, const Twine &Comment) override { |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 66 | Hash.addSLEB128(DWord); |
| 67 | } |
Craig Topper | 7b883b3 | 2014-03-08 06:31:39 +0000 | [diff] [blame] | 68 | void EmitULEB128(uint64_t DWord, const Twine &Comment) override { |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 69 | Hash.addULEB128(DWord); |
| 70 | } |
| 71 | }; |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 72 | |
David Blaikie | e44a8a7 | 2015-08-03 20:12:58 +0000 | [diff] [blame] | 73 | class BufferByteStreamer final : public ByteStreamer { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 74 | private: |
| 75 | SmallVectorImpl<char> &Buffer; |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 76 | SmallVectorImpl<std::string> &Comments; |
| 77 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 78 | /// Only verbose textual output needs comments. This will be set to |
Pete Cooper | a05c082 | 2015-05-20 22:51:27 +0000 | [diff] [blame] | 79 | /// true for that case, and false otherwise. If false, comments passed in to |
| 80 | /// the emit methods will be ignored. |
| 81 | bool GenerateComments; |
| 82 | |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 83 | public: |
| 84 | BufferByteStreamer(SmallVectorImpl<char> &Buffer, |
Pete Cooper | a05c082 | 2015-05-20 22:51:27 +0000 | [diff] [blame] | 85 | SmallVectorImpl<std::string> &Comments, |
| 86 | bool GenerateComments) |
| 87 | : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {} |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 88 | void EmitInt8(uint8_t Byte, const Twine &Comment) override { |
| 89 | Buffer.push_back(Byte); |
Pete Cooper | a05c082 | 2015-05-20 22:51:27 +0000 | [diff] [blame] | 90 | if (GenerateComments) |
| 91 | Comments.push_back(Comment.str()); |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 92 | } |
| 93 | void EmitSLEB128(uint64_t DWord, const Twine &Comment) override { |
| 94 | raw_svector_ostream OSE(Buffer); |
Bjorn Pettersson | 5ffb1c0 | 2018-01-05 22:20:30 +0000 | [diff] [blame] | 95 | unsigned Length = encodeSLEB128(DWord, OSE); |
| 96 | if (GenerateComments) { |
Pete Cooper | a05c082 | 2015-05-20 22:51:27 +0000 | [diff] [blame] | 97 | Comments.push_back(Comment.str()); |
Bjorn Pettersson | 5ffb1c0 | 2018-01-05 22:20:30 +0000 | [diff] [blame] | 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 | } |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 104 | } |
| 105 | void EmitULEB128(uint64_t DWord, const Twine &Comment) override { |
| 106 | raw_svector_ostream OSE(Buffer); |
Bjorn Pettersson | 5ffb1c0 | 2018-01-05 22:20:30 +0000 | [diff] [blame] | 107 | unsigned Length = encodeULEB128(DWord, OSE); |
| 108 | if (GenerateComments) { |
Pete Cooper | a05c082 | 2015-05-20 22:51:27 +0000 | [diff] [blame] | 109 | Comments.push_back(Comment.str()); |
Bjorn Pettersson | 5ffb1c0 | 2018-01-05 22:20:30 +0000 | [diff] [blame] | 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 | } |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 116 | } |
| 117 | }; |
| 118 | |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 119 | } |
Eric Christopher | 6fbb7bb | 2014-03-07 22:43:09 +0000 | [diff] [blame] | 120 | |
| 121 | #endif |