blob: df1997bcb72c6ce22f58404db60c7fb47b72f87c [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/ADT/ArrayRef.h"
20#include "llvm/CodeGen/AsmPrinter.h"
21#include "llvm/MC/MCStreamer.h"
Adrian Prantl92da14b2015-03-02 22:02:33 +000022#include "llvm/Support/LEB128.h"
23#include <string>
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000024
25namespace llvm {
26class ByteStreamer {
David Blaikiee44a8a72015-08-03 20:12:58 +000027 protected:
28 ~ByteStreamer() = default;
29 ByteStreamer(const ByteStreamer&) = default;
30 ByteStreamer() = default;
Eric Christopher1d6d1c82014-03-07 22:53:36 +000031
David Blaikiee44a8a72015-08-03 20:12:58 +000032 public:
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000033 // For now we're just handling the calls we need for dwarf emission/hashing.
34 virtual void EmitInt8(uint8_t Byte, const Twine &Comment = "") = 0;
35 virtual void EmitSLEB128(uint64_t DWord, const Twine &Comment = "") = 0;
36 virtual void EmitULEB128(uint64_t DWord, const Twine &Comment = "") = 0;
37};
38
David Blaikiee44a8a72015-08-03 20:12:58 +000039class APByteStreamer final : public ByteStreamer {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000040private:
41 AsmPrinter &AP;
42
43public:
44 APByteStreamer(AsmPrinter &Asm) : AP(Asm) {}
Craig Topper7b883b32014-03-08 06:31:39 +000045 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
Lang Hames9ff69c82015-04-24 19:11:51 +000046 AP.OutStreamer->AddComment(Comment);
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000047 AP.EmitInt8(Byte);
48 }
Craig Topper7b883b32014-03-08 06:31:39 +000049 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
Lang Hames9ff69c82015-04-24 19:11:51 +000050 AP.OutStreamer->AddComment(Comment);
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000051 AP.EmitSLEB128(DWord);
52 }
Craig Topper7b883b32014-03-08 06:31:39 +000053 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
Lang Hames9ff69c82015-04-24 19:11:51 +000054 AP.OutStreamer->AddComment(Comment);
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000055 AP.EmitULEB128(DWord);
56 }
57};
58
David Blaikiee44a8a72015-08-03 20:12:58 +000059class HashingByteStreamer final : public ByteStreamer {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000060 private:
61 DIEHash &Hash;
62 public:
63 HashingByteStreamer(DIEHash &H) : Hash(H) {}
Craig Topper7b883b32014-03-08 06:31:39 +000064 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000065 Hash.update(Byte);
66 }
Craig Topper7b883b32014-03-08 06:31:39 +000067 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000068 Hash.addSLEB128(DWord);
69 }
Craig Topper7b883b32014-03-08 06:31:39 +000070 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000071 Hash.addULEB128(DWord);
72 }
73};
Adrian Prantl92da14b2015-03-02 22:02:33 +000074
David Blaikiee44a8a72015-08-03 20:12:58 +000075class BufferByteStreamer final : public ByteStreamer {
Adrian Prantl92da14b2015-03-02 22:02:33 +000076private:
77 SmallVectorImpl<char> &Buffer;
Adrian Prantl92da14b2015-03-02 22:02:33 +000078 SmallVectorImpl<std::string> &Comments;
79
Pete Coopera05c0822015-05-20 22:51:27 +000080 /// \brief Only verbose textual output needs comments. This will be set to
81 /// true for that case, and false otherwise. If false, comments passed in to
82 /// the emit methods will be ignored.
83 bool GenerateComments;
84
Adrian Prantl92da14b2015-03-02 22:02:33 +000085public:
86 BufferByteStreamer(SmallVectorImpl<char> &Buffer,
Pete Coopera05c0822015-05-20 22:51:27 +000087 SmallVectorImpl<std::string> &Comments,
88 bool GenerateComments)
89 : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {}
Adrian Prantl92da14b2015-03-02 22:02:33 +000090 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
91 Buffer.push_back(Byte);
Pete Coopera05c0822015-05-20 22:51:27 +000092 if (GenerateComments)
93 Comments.push_back(Comment.str());
Adrian Prantl92da14b2015-03-02 22:02:33 +000094 }
95 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
96 raw_svector_ostream OSE(Buffer);
97 encodeSLEB128(DWord, OSE);
Pete Coopera05c0822015-05-20 22:51:27 +000098 if (GenerateComments)
99 Comments.push_back(Comment.str());
Adrian Prantl92da14b2015-03-02 22:02:33 +0000100 }
101 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
102 raw_svector_ostream OSE(Buffer);
103 encodeULEB128(DWord, OSE);
Pete Coopera05c0822015-05-20 22:51:27 +0000104 if (GenerateComments)
105 Comments.push_back(Comment.str());
Adrian Prantl92da14b2015-03-02 22:02:33 +0000106 }
107};
108
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000109}
Eric Christopher6fbb7bb2014-03-07 22:43:09 +0000110
111#endif