FreeTensor
Loading...
Searching...
No Matches
code_gen.h
Go to the documentation of this file.
1#ifndef FREE_TENSOR_CODE_GEN_H
2#define FREE_TENSOR_CODE_GEN_H
3
4#include <functional>
5#include <sstream>
6#include <string>
7#include <unordered_map>
8#include <unordered_set>
9#include <vector>
10
12#include <codegen/native_code.h>
13#include <container_utils.h>
14#include <visitor.h>
15
16namespace freetensor {
17
19 std::string name_;
20 std::ostringstream os_;
21 int nIndent_ = 0;
22 std::unordered_map<std::string, VarDef> useDefs_;
23 std::unordered_set<std::string> useIters_;
24
26};
27
28template <class Stream> class CodeGen : public SymbolTable<Visitor> {
29 protected:
32 std::vector<Stream> streamStack_, poppedStream_;
33
34 std::unordered_map<std::string, std::string>
35 var2Stream_; // var name -> stream name
36
37 void makeIndent();
38
39 template <class T> void printList(T &&list) {
40 for (auto &&[i, item] : views::enumerate(list)) {
41 if (i > 0) {
42 os() << (compact_ ? "," : ", ");
43 }
44 (*this)(item);
45 }
46 }
47
48 CodeGen(bool compact = false, int indentSize = 2);
49
50 void markDef(const VarDef &op);
51 void markUse(const std::string &name);
52 void markUndef(const VarDef &op);
53
54 void markDefIter(const For &op);
55 void markUseIter(const std::string &name);
56 void markUndefIter(const For &op);
57
58 void pushStream(const std::string &name);
59 void popStream();
60
61 std::ostream &os();
62 int &nIndent();
63
64 public:
65 void beginBlock();
66 void endBlock();
67
74 std::string
75 toString(const std::function<std::string(const Stream &)> &action);
76};
77
85NativeCode codeGen(const Func &func, const Ref<Target> &target);
86
87} // namespace freetensor
88
89#endif // FREE_TENSOR_CODE_GEN_H
Definition: code_gen.h:28
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 printList(T &&list)
Definition: code_gen.h:39
void markUndefIter(const For &op)
Definition: code_gen.h:71
void beginBlock()
Definition: code_gen.h:17
bool compact_
Definition: code_gen.h:30
int indentSize_
Definition: code_gen.h:31
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
std::unordered_map< std::string, std::string > var2Stream_
Definition: code_gen.h:35
std::vector< Stream > streamStack_
Definition: code_gen.h:32
std::vector< Stream > poppedStream_
Definition: code_gen.h:32
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
Definition: symbol_table.h:122
Definition: allocator.h:9
NativeCode codeGen(const Func &func, const Ref< Target > &target)
Definition: code_gen.cc:7
Ref< FuncNode > Func
Definition: func.h:64
Definition: code_gen.h:18
std::unordered_set< std::string > useIters_
Definition: code_gen.h:23
CodeGenStream()
Definition: code_gen.h:9
int nIndent_
Definition: code_gen.h:21
std::ostringstream os_
Definition: code_gen.h:20
std::string name_
Definition: code_gen.h:19
std::unordered_map< std::string, VarDef > useDefs_
Definition: code_gen.h:22