blob: aaf6180c9404bb3c0973731858f60d91627a42df [file] [log] [blame]
Eric Christopher6fbb7bb2014-03-07 22:43:09 +00001//===-- 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 Kramera7c40ef2014-08-13 16:26:38 +000015#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
16#define LLVM_LIB_CODEGEN_ASMPRINTER_BYTESTREAMER_H
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000017
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "DIEHash.h"
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000019#include "llvm/CodeGen/AsmPrinter.h"
20#include "llvm/MC/MCStreamer.h"
Adrian Prantl92da14b2015-03-02 22:02:33 +000021#include "llvm/Support/LEB128.h"
22#include <string>
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000023
24namespace llvm {
25class ByteStreamer {
David Blaikiee44a8a72015-08-03 20:12:58 +000026 protected:
27 ~ByteStreamer() = default;
28 ByteStreamer(const ByteStreamer&) = default;
29 ByteStreamer() = default;
Eric Christopher1d6d1c82014-03-07 22:53:36 +000030
David Blaikiee44a8a72015-08-03 20:12:58 +000031 public:
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000032 // 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 Blaikiee44a8a72015-08-03 20:12:58 +000038class APByteStreamer final : public ByteStreamer {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000039private:
40 AsmPrinter &AP;
41
42public:
43 APByteStreamer(AsmPrinter &Asm) : AP(Asm) {}
Craig Topper7b883b32014-03-08 06:31:39 +000044 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
Lang Hames9ff69c82015-04-24 19:11:51 +000045 AP.OutStreamer->AddComment(Comment);
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000046 AP.EmitInt8(Byte);
47 }
Craig Topper7b883b32014-03-08 06:31:39 +000048 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
Lang Hames9ff69c82015-04-24 19:11:51 +000049 AP.OutStreamer->AddComment(Comment);
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000050 AP.EmitSLEB128(DWord);
51 }
Craig Topper7b883b32014-03-08 06:31:39 +000052 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
Lang Hames9ff69c82015-04-24 19:11:51 +000053 AP.OutStreamer->AddComment(Comment);
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000054 AP.EmitULEB128(DWord);
55 }
56};
57
David Blaikiee44a8a72015-08-03 20:12:58 +000058class HashingByteStreamer final : public ByteStreamer {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000059 private:
60 DIEHash &Hash;
61 public:
62 HashingByteStreamer(DIEHash &H) : Hash(H) {}
Craig Topper7b883b32014-03-08 06:31:39 +000063 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000064 Hash.update(Byte);
65 }
Craig Topper7b883b32014-03-08 06:31:39 +000066 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000067 Hash.addSLEB128(DWord);
68 }
Craig Topper7b883b32014-03-08 06:31:39 +000069 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000070 Hash.addULEB128(DWord);
71 }
72};
Adrian Prantl92da14b2015-03-02 22:02:33 +000073
David Blaikiee44a8a72015-08-03 20:12:58 +000074class BufferByteStreamer final : public ByteStreamer {
Adrian Prantl92da14b2015-03-02 22:02:33 +000075private:
76 SmallVectorImpl<char> &Buffer;
Adrian Prantl92da14b2015-03-02 22:02:33 +000077 SmallVectorImpl<std::string> &Comments;
78
Pete Coopera05c0822015-05-20 22:51:27 +000079 /// \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 Prantl92da14b2015-03-02 22:02:33 +000084public:
85 BufferByteStreamer(SmallVectorImpl<char> &Buffer,
Pete Coopera05c0822015-05-20 22:51:27 +000086 SmallVectorImpl<std::string> &Comments,
87 bool GenerateComments)
88 : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {}
Adrian Prantl92da14b2015-03-02 22:02:33 +000089 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
90 Buffer.push_back(Byte);
Pete Coopera05c0822015-05-20 22:51:27 +000091 if (GenerateComments)
92 Comments.push_back(Comment.str());
Adrian Prantl92da14b2015-03-02 22:02:33 +000093 }
94 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
95 raw_svector_ostream OSE(Buffer);
96 encodeSLEB128(DWord, OSE);
Pete Coopera05c0822015-05-20 22:51:27 +000097 if (GenerateComments)
98 Comments.push_back(Comment.str());
Adrian Prantl92da14b2015-03-02 22:02:33 +000099 }
100 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
101 raw_svector_ostream OSE(Buffer);
102 encodeULEB128(DWord, OSE);
Pete Coopera05c0822015-05-20 22:51:27 +0000103 if (GenerateComments)
104 Comments.push_back(Comment.str());
Adrian Prantl92da14b2015-03-02 22:02:33 +0000105 }
106};
107
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000108}
Eric Christopher6fbb7bb2014-03-07 22:43:09 +0000109
110#endif