blob: 7a2902d68070db60c1337663e2d28d07ade5b2cc [file] [log] [blame]
Eric Christopher6fbb7bb2014-03-07 22:43:09 +00001//===-- llvm/CodeGen/ByteStreamer.h - ByteStreamer class --------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Christopher6fbb7bb2014-03-07 22:43:09 +00006//
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 Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
15#define LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000016
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "DIEHash.h"
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000018#include "llvm/CodeGen/AsmPrinter.h"
19#include "llvm/MC/MCStreamer.h"
Adrian Prantl92da14b2015-03-02 22:02:33 +000020#include "llvm/Support/LEB128.h"
21#include <string>
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000022
23namespace llvm {
24class ByteStreamer {
David Blaikiee44a8a72015-08-03 20:12:58 +000025 protected:
26 ~ByteStreamer() = default;
27 ByteStreamer(const ByteStreamer&) = default;
28 ByteStreamer() = default;
Eric Christopher1d6d1c82014-03-07 22:53:36 +000029
David Blaikiee44a8a72015-08-03 20:12:58 +000030 public:
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000031 // 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 Blaikiee44a8a72015-08-03 20:12:58 +000037class APByteStreamer final : public ByteStreamer {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000038private:
39 AsmPrinter &AP;
40
41public:
42 APByteStreamer(AsmPrinter &Asm) : AP(Asm) {}
Craig Topper7b883b32014-03-08 06:31:39 +000043 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
Lang Hames9ff69c82015-04-24 19:11:51 +000044 AP.OutStreamer->AddComment(Comment);
Rafael Espindola4b4d85f2018-03-29 23:32:54 +000045 AP.emitInt8(Byte);
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000046 }
Craig Topper7b883b32014-03-08 06:31:39 +000047 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
Lang Hames9ff69c82015-04-24 19:11:51 +000048 AP.OutStreamer->AddComment(Comment);
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000049 AP.EmitSLEB128(DWord);
50 }
Craig Topper7b883b32014-03-08 06:31:39 +000051 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
Lang Hames9ff69c82015-04-24 19:11:51 +000052 AP.OutStreamer->AddComment(Comment);
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000053 AP.EmitULEB128(DWord);
54 }
55};
56
David Blaikiee44a8a72015-08-03 20:12:58 +000057class HashingByteStreamer final : public ByteStreamer {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000058 private:
59 DIEHash &Hash;
60 public:
61 HashingByteStreamer(DIEHash &H) : Hash(H) {}
Craig Topper7b883b32014-03-08 06:31:39 +000062 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000063 Hash.update(Byte);
64 }
Craig Topper7b883b32014-03-08 06:31:39 +000065 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000066 Hash.addSLEB128(DWord);
67 }
Craig Topper7b883b32014-03-08 06:31:39 +000068 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000069 Hash.addULEB128(DWord);
70 }
71};
Adrian Prantl92da14b2015-03-02 22:02:33 +000072
David Blaikiee44a8a72015-08-03 20:12:58 +000073class BufferByteStreamer final : public ByteStreamer {
Adrian Prantl92da14b2015-03-02 22:02:33 +000074private:
75 SmallVectorImpl<char> &Buffer;
Adrian Prantl92da14b2015-03-02 22:02:33 +000076 SmallVectorImpl<std::string> &Comments;
77
Adrian Prantl5f8f34e42018-05-01 15:54:18 +000078 /// Only verbose textual output needs comments. This will be set to
Pete Coopera05c0822015-05-20 22:51:27 +000079 /// 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 Prantl92da14b2015-03-02 22:02:33 +000083public:
84 BufferByteStreamer(SmallVectorImpl<char> &Buffer,
Pete Coopera05c0822015-05-20 22:51:27 +000085 SmallVectorImpl<std::string> &Comments,
86 bool GenerateComments)
87 : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {}
Adrian Prantl92da14b2015-03-02 22:02:33 +000088 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
89 Buffer.push_back(Byte);
Pete Coopera05c0822015-05-20 22:51:27 +000090 if (GenerateComments)
91 Comments.push_back(Comment.str());
Adrian Prantl92da14b2015-03-02 22:02:33 +000092 }
93 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
94 raw_svector_ostream OSE(Buffer);
Bjorn Pettersson5ffb1c02018-01-05 22:20:30 +000095 unsigned Length = encodeSLEB128(DWord, OSE);
96 if (GenerateComments) {
Pete Coopera05c0822015-05-20 22:51:27 +000097 Comments.push_back(Comment.str());
Bjorn Pettersson5ffb1c02018-01-05 22:20:30 +000098 // 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 Prantl92da14b2015-03-02 22:02:33 +0000104 }
105 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
106 raw_svector_ostream OSE(Buffer);
Bjorn Pettersson5ffb1c02018-01-05 22:20:30 +0000107 unsigned Length = encodeULEB128(DWord, OSE);
108 if (GenerateComments) {
Pete Coopera05c0822015-05-20 22:51:27 +0000109 Comments.push_back(Comment.str());
Bjorn Pettersson5ffb1c02018-01-05 22:20:30 +0000110 // 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 Prantl92da14b2015-03-02 22:02:33 +0000116 }
117};
118
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000119}
Eric Christopher6fbb7bb2014-03-07 22:43:09 +0000120
121#endif