FreeTensor
Loading...
Searching...
No Matches
code_gen.h
Go to the documentation of this file.
1#ifndef DETAIL_CODE_GEN_H
2#define DETAIL_CODE_GEN_H
3
4#include <codegen/code_gen.h>
6
7namespace freetensor {
8
10
11template <class Stream>
12CodeGen<Stream>::CodeGen(bool compact, int indentSize)
13 : compact_(compact), indentSize_(compact ? 0 : indentSize) {
14 pushStream("default");
15}
16
17template <class Stream> void CodeGen<Stream>::beginBlock() {
18 os() << "{" << std::endl;
19 nIndent()++;
20}
21
22template <class Stream> void CodeGen<Stream>::endBlock() {
23 nIndent()--;
24 makeIndent();
25 os() << "}" << std::endl;
26}
27
28template <class Stream> void CodeGen<Stream>::makeIndent() {
29 for (int i = 0, iEnd = nIndent() * indentSize_; i < iEnd; i++) {
30 os() << " ";
31 }
32}
33
34template <class Stream> void CodeGen<Stream>::markDef(const VarDef &op) {
35 var2Stream_[op->name_] = streamStack_.back().name_;
36 pushDef(op);
38
39template <class Stream> void CodeGen<Stream>::markUse(const std::string &name) {
40 auto &&stream = var2Stream_.at(name);
41 auto &&d = def(name);
42 for (auto it = streamStack_.rbegin(); it != streamStack_.rend(); it++) {
43 if (it->name_ == stream) {
44 break;
45 }
46 it->useDefs_[name] = d;
47 }
49
50template <class Stream> void CodeGen<Stream>::markUndef(const VarDef &op) {
51 var2Stream_.erase(op->name_);
52 popDef(op);
53}
55template <class Stream> void CodeGen<Stream>::markDefIter(const For &op) {
56 var2Stream_[op->iter_] = streamStack_.back().name_;
57 pushFor(op);
60template <class Stream>
61void CodeGen<Stream>::markUseIter(const std::string &name) {
62 auto &&stream = var2Stream_.at(name);
63 for (auto it = streamStack_.rbegin(); it != streamStack_.rend(); it++) {
64 if (it->name_ == stream) {
65 break;
66 }
67 it->useIters_.insert(name);
68 }
69}
70
71template <class Stream> void CodeGen<Stream>::markUndefIter(const For &op) {
72 var2Stream_.erase(op->iter_);
73 popFor(op);
74}
76template <class Stream>
77void CodeGen<Stream>::pushStream(const std::string &name) {
78 streamStack_.emplace_back();
79 streamStack_.back().name_ = name;
80}
81
82template <class Stream> void CodeGen<Stream>::popStream() {
83 poppedStream_.emplace_back(std::move(streamStack_.back()));
84 streamStack_.pop_back();
85}
86
87template <class Stream> std::ostream &CodeGen<Stream>::os() {
88 return streamStack_.back().os_;
89}
90
91template <class Stream> int &CodeGen<Stream>::nIndent() {
92 return streamStack_.back().nIndent_;
93}
94
95template <class Stream>
97 const std::function<std::string(const Stream &)> &action) {
98 std::string ret;
99 for (auto &&stream : poppedStream_) {
100 ret += action(stream);
101 }
102 for (auto it = streamStack_.rbegin(); it != streamStack_.rend(); it++) {
103 ret += action(*it);
104 }
105 return ret;
106}
107
108} // namespace freetensor
109
110#endif // DETAIL_CODE_GEN_H
std::string toString(const std::function< std::string(const Stream &)> &action)
Definition: code_gen.h:96
void pushStream(const std::string &name)
Definition: code_gen.h:77
void markDefIter(const For &op)
Definition: code_gen.h:55
void markUndefIter(const For &op)
Definition: code_gen.h:71
void beginBlock()
Definition: code_gen.h:17
CodeGen(bool compact=false, int indentSize=2)
Definition: code_gen.h:12
void markUseIter(const std::string &name)
Definition: code_gen.h:61
int & nIndent()
Definition: code_gen.h:91
void makeIndent()
Definition: code_gen.h:28
std::ostream & os()
Definition: code_gen.h:87
void markUndef(const VarDef &op)
Definition: code_gen.h:50
void endBlock()
Definition: code_gen.h:22
void markDef(const VarDef &op)
Definition: code_gen.h:34
void popStream()
Definition: code_gen.h:82
void markUse(const std::string &name)
Definition: code_gen.h:39
std::string iter_
Definition: stmt.h:289
std::string name_
Definition: stmt.h:85
Definition: allocator.h:9
std::function< std::ostream &(std::ostream &)> manipNoPrettyAST(bool flag)
Definition: print_ast.cc:814
CodeGenStream()
Definition: code_gen.h:9
std::ostringstream os_
Definition: code_gen.h:20