blob: 0cc829fffc544d0afead6770a73de36ad64ec0fc [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 {
27 public:
Eric Christopher1d6d1c82014-03-07 22:53:36 +000028 virtual ~ByteStreamer() {}
29
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000030 // For now we're just handling the calls we need for dwarf emission/hashing.
31 virtual void EmitInt8(uint8_t Byte, const Twine &Comment = "") = 0;
32 virtual void EmitSLEB128(uint64_t DWord, const Twine &Comment = "") = 0;
33 virtual void EmitULEB128(uint64_t DWord, const Twine &Comment = "") = 0;
34};
35
36class APByteStreamer : public ByteStreamer {
37private:
38 AsmPrinter &AP;
39
40public:
41 APByteStreamer(AsmPrinter &Asm) : AP(Asm) {}
Craig Topper7b883b32014-03-08 06:31:39 +000042 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
Lang Hames9ff69c82015-04-24 19:11:51 +000043 AP.OutStreamer->AddComment(Comment);
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000044 AP.EmitInt8(Byte);
45 }
Craig Topper7b883b32014-03-08 06:31:39 +000046 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
Lang Hames9ff69c82015-04-24 19:11:51 +000047 AP.OutStreamer->AddComment(Comment);
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000048 AP.EmitSLEB128(DWord);
49 }
Craig Topper7b883b32014-03-08 06:31:39 +000050 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
Lang Hames9ff69c82015-04-24 19:11:51 +000051 AP.OutStreamer->AddComment(Comment);
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000052 AP.EmitULEB128(DWord);
53 }
54};
55
56class HashingByteStreamer : public ByteStreamer {
57 private:
58 DIEHash &Hash;
59 public:
60 HashingByteStreamer(DIEHash &H) : Hash(H) {}
Craig Topper7b883b32014-03-08 06:31:39 +000061 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000062 Hash.update(Byte);
63 }
Craig Topper7b883b32014-03-08 06:31:39 +000064 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000065 Hash.addSLEB128(DWord);
66 }
Craig Topper7b883b32014-03-08 06:31:39 +000067 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
Eric Christopher6fbb7bb2014-03-07 22:43:09 +000068 Hash.addULEB128(DWord);
69 }
70};
Adrian Prantl92da14b2015-03-02 22:02:33 +000071
72class BufferByteStreamer : public ByteStreamer {
73private:
74 SmallVectorImpl<char> &Buffer;
Adrian Prantl92da14b2015-03-02 22:02:33 +000075 SmallVectorImpl<std::string> &Comments;
76
Pete Coopera05c0822015-05-20 22:51:27 +000077 /// \brief Only verbose textual output needs comments. This will be set to
78 /// true for that case, and false otherwise. If false, comments passed in to
79 /// the emit methods will be ignored.
80 bool GenerateComments;
81
Adrian Prantl92da14b2015-03-02 22:02:33 +000082public:
83 BufferByteStreamer(SmallVectorImpl<char> &Buffer,
Pete Coopera05c0822015-05-20 22:51:27 +000084 SmallVectorImpl<std::string> &Comments,
85 bool GenerateComments)
86 : Buffer(Buffer), Comments(Comments), GenerateComments(GenerateComments) {}
Adrian Prantl92da14b2015-03-02 22:02:33 +000087 void EmitInt8(uint8_t Byte, const Twine &Comment) override {
88 Buffer.push_back(Byte);
Pete Coopera05c0822015-05-20 22:51:27 +000089 if (GenerateComments)
90 Comments.push_back(Comment.str());
Adrian Prantl92da14b2015-03-02 22:02:33 +000091 }
92 void EmitSLEB128(uint64_t DWord, const Twine &Comment) override {
93 raw_svector_ostream OSE(Buffer);
94 encodeSLEB128(DWord, OSE);
Pete Coopera05c0822015-05-20 22:51:27 +000095 if (GenerateComments)
96 Comments.push_back(Comment.str());
Adrian Prantl92da14b2015-03-02 22:02:33 +000097 }
98 void EmitULEB128(uint64_t DWord, const Twine &Comment) override {
99 raw_svector_ostream OSE(Buffer);
100 encodeULEB128(DWord, OSE);
Pete Coopera05c0822015-05-20 22:51:27 +0000101 if (GenerateComments)
102 Comments.push_back(Comment.str());
Adrian Prantl92da14b2015-03-02 22:02:33 +0000103 }
104};
105
Eric Christopher6fbb7bb2014-03-07 22:43:09 +0000106}
107
108#endif