1#ifndef DETAIL_CODE_GEN_H
2#define DETAIL_CODE_GEN_H
11template <
class Stream>
13 : compact_(compact), indentSize_(compact ? 0 : indentSize) {
18 os() <<
"{" << std::endl;
25 os() <<
"}" << std::endl;
29 for (
int i = 0, iEnd = nIndent() * indentSize_; i < iEnd; i++) {
35 var2Stream_[op->
name_] = streamStack_.back().name_;
40 auto &&stream = var2Stream_.at(name);
42 for (
auto it = streamStack_.rbegin(); it != streamStack_.rend(); it++) {
43 if (it->name_ == stream) {
46 it->useDefs_[name] = d;
56 var2Stream_[op->
iter_] = streamStack_.back().name_;
60template <
class Stream>
62 auto &&stream = var2Stream_.at(name);
63 for (
auto it = streamStack_.rbegin(); it != streamStack_.rend(); it++) {
64 if (it->name_ == stream) {
67 it->useIters_.insert(name);
72 var2Stream_.erase(op->
iter_);
76template <
class Stream>
78 streamStack_.emplace_back();
79 streamStack_.back().name_ = name;
83 poppedStream_.emplace_back(std::move(streamStack_.back()));
84 streamStack_.pop_back();
88 return streamStack_.back().os_;
92 return streamStack_.back().nIndent_;
95template <
class Stream>
97 const std::function<std::string(
const Stream &)> &action) {
99 for (
auto &&stream : poppedStream_) {
100 ret += action(stream);
102 for (
auto it = streamStack_.rbegin(); it != streamStack_.rend(); it++) {
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