llir-opt  0.0.1
Low-Level Post-Link Optimiser for OCaml and C
bitcode.h
1 // This file if part of the llir-opt project.
2 // Licensing information can be found in the LICENSE file.
3 // (C) 2018 Nandor Licker. All rights reserved.
4 
5 #pragma once
6 
7 #include <tuple>
8 #include <vector>
9 
10 #include <llvm/Support/raw_ostream.h>
11 #include <llvm/Support/MemoryBuffer.h>
12 
13 #include "core/util.h"
14 #include "core/inst.h"
15 
16 class Block;
17 class Data;
18 class Expr;
19 class Extern;
20 class Func;
21 class Global;
22 class Inst;
23 class PhiInst;
24 class Prog;
25 class Value;
26 class Object;
27 class Atom;
28 class Annot;
29 class AnnotSet;
30 class Xtor;
31 
32 
36 class BitcodeReader final {
37 public:
38  BitcodeReader(llvm::StringRef buf) : buf_(buf), offset_(0) {}
39 
41  std::unique_ptr<Prog> Read();
42 
43 private:
45  void Read(Func &func);
47  void Read(Atom &atom);
49  void Read(Extern &ext);
50 
52  template<typename T> T ReadData();
54  template<typename T> std::optional<T> ReadOptional();
56  std::string ReadString();
58  Inst *ReadInst(
59  const std::vector<Ref<Inst>> &map,
60  std::vector<std::tuple<PhiInst *, Block *, unsigned>> &fixups
61  );
63  Expr *ReadExpr();
65  Ref<Value> ReadValue(const std::vector<Ref<Inst>> &map);
67  Block *ReadBlock(const std::vector<Ref<Inst>> &map);
69  Ref<Inst> ReadInst(const std::vector<Ref<Inst>> &map);
71  Ref<Constant> ReadConst();
73  void ReadAnnot(AnnotSet &annots);
75  Xtor *ReadXtor();
77  Type ReadType();
79  TypeFlag ReadTypeFlag();
81  FlaggedType ReadFlaggedType();
82 
83 private:
85  llvm::StringRef buf_;
87  uint64_t offset_;
89  std::vector<Global *> globals_;
90 };
91 
92 
96 class BitcodeWriter final {
97 public:
98  BitcodeWriter(llvm::raw_pwrite_stream &os) : os_(os) {}
99 
101  void Write(const Prog &prog);
102 
103 private:
105  void Write(const Func &prog);
107  void Write(const Atom &atom);
109  void Write(const Extern &ext);
111  void Write(
112  const Inst &inst,
113  const std::unordered_map<ConstRef<Inst>, unsigned> &map
114  );
116  void Write(
117  ConstRef<Value> value,
118  const std::unordered_map<ConstRef<Inst>, unsigned> &map
119  );
121  void Write(
122  ConstRef<Inst> value,
123  const std::unordered_map<ConstRef<Inst>, unsigned> &map
124  );
126  void Write(
127  const Block *value,
128  const std::unordered_map<ConstRef<Inst>, unsigned> &map
129  );
131  void Write(ConstRef<Constant> value);
133  void Write(const Expr &expr);
135  void Write(const Global &global);
137  void Write(const Annot &annot);
139  void Write(const Xtor &xtor);
141  void Write(Type type);
143  void Write(const TypeFlag &type);
145  void Write(const FlaggedType &type);
146 
148  void Emit(llvm::StringRef str);
150  void Emit(const std::string &str) { return Emit(llvm::StringRef(str)); }
152  template<typename T> void Emit(T t);
153 
154 private:
156  std::unordered_map<const Global *, unsigned> symbols_;
158  llvm::raw_pwrite_stream &os_;
159 };
Inst
Definition: inst.h:53
Func
Definition: func.h:30
ConstRef< Inst >
Atom
Definition: atom.h:23
Value
Definition: value.h:22
Expr
Definition: expr.h:19
Data
Definition: data.h:47
BitcodeReader::Read
std::unique_ptr< Prog > Read()
Read a program from the stream.
Definition: bitcode_reader.cpp:58
FlaggedType
Definition: type.h:77
BitcodeReader
Definition: bitcode.h:36
AnnotSet
Definition: annot.h:69
Object
Definition: object.h:43
Xtor
Constructor/Destructor information.
Definition: xtor.h:20
BitcodeWriter::Write
void Write(const Prog &prog)
Write a program to the stream.
Definition: bitcode_writer.cpp:41
TypeFlag
Definition: type.h:32
Prog
Definition: prog.h:33
BitcodeWriter
Definition: bitcode.h:96
Ref
Definition: ref.h:63
Block
Definition: block.h:29
Extern
Definition: extern.h:20
PhiInst
Definition: phi.h:14
Annot
Definition: annot.h:15
Global
Definition: global.h:23