-
Notifications
You must be signed in to change notification settings - Fork 0
/
codeGen.h
140 lines (112 loc) · 4.66 KB
/
codeGen.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
Modifications copyright 2017 CESNET
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _BACKENDS_FPP_CODEGEN_H_
#define _BACKENDS_FPP_CODEGEN_H_
#include "ir/ir.h"
#include "lib/sourceCodeBuilder.h"
#include "target.h"
#include "frontends/p4/typeMap.h"
namespace P4 {
class ReferenceMap;
}
namespace FPP {
class CodeBuilder : public Util::SourceCodeBuilder {
public:
const Target* target;
explicit CodeBuilder(const Target* target) : target(target) {}
};
// Visitor for generating C
// This visitor is invoked on various subtrees
class CodeGenInspector : public Inspector {
protected:
CodeBuilder* builder;
P4::ReferenceMap* refMap;
P4::TypeMap* typeMap;
std::map<const IR::Parameter*, const IR::Parameter*> substitution;
public:
CodeGenInspector(P4::ReferenceMap* refMap, P4::TypeMap* typeMap) :
builder(nullptr), refMap(refMap), typeMap(typeMap) {
CHECK_NULL(refMap); CHECK_NULL(typeMap);
visitDagOnce = false;
}
void setBuilder(CodeBuilder* builder) {
CHECK_NULL(builder);
this->builder = builder;
}
void substitute(const IR::Parameter* p, const IR::Parameter* with);
void copySubstitutions(CodeGenInspector* other) {
for (auto s : other->substitution)
substitute(s.first, s.second);
}
bool notSupported(const IR::Expression* expression)
{ ::error("%1%: not yet implemented", expression); return false; }
bool preorder(const IR::Expression* expression) override
{ return notSupported(expression); }
bool preorder(const IR::Range* expression) override
{ return notSupported(expression); }
bool preorder(const IR::Mask* expression) override
{ return notSupported(expression); }
bool preorder(const IR::Slice* expression) override
{ return notSupported(expression); }
bool preorder(const IR::StringLiteral* expression) override;
bool preorder(const IR::ListExpression* expression) override;
bool preorder(const IR::PathExpression* expression) override;
bool preorder(const IR::Constant* expression) override;
bool preorder(const IR::Declaration_Variable* decl) override;
bool preorder(const IR::BoolLiteral* b) override;
bool preorder(const IR::Cast* c) override;
bool preorder(const IR::Operation_Binary* b) override;
bool preorder(const IR::Operation_Unary* u) override;
bool preorder(const IR::ArrayIndex* a) override;
bool preorder(const IR::Mux* a) override;
bool preorder(const IR::Member* e) override;
bool preorder(const IR::MethodCallExpression* expression) override;
bool comparison(const IR::Operation_Relation* comp);
bool preorder(const IR::Equ* e) override { return comparison(e); }
bool preorder(const IR::Neq* e) override { return comparison(e); }
bool preorder(const IR::IndexedVector<IR::StatOrDecl>* v) override;
bool preorder(const IR::Path* path) override;
bool preorder(const IR::Type_Typedef* type) override;
bool preorder(const IR::Type_Enum* type) override;
bool preorder(const IR::AssignmentStatement* s) override;
bool preorder(const IR::BlockStatement* s) override;
bool preorder(const IR::MethodCallStatement* s) override;
bool preorder(const IR::EmptyStatement* s) override;
bool preorder(const IR::ReturnStatement* s) override;
bool preorder(const IR::ExitStatement* s) override;
bool preorder(const IR::IfStatement* s) override;
void widthCheck(const IR::Node* node) const;
protected:
struct VecPrint {
cstring separator;
cstring terminator;
VecPrint(const char* sep, const char* term) :
separator(sep), terminator(term) {}
};
// maintained as stack
std::vector<VecPrint> vectorSeparator;
void setVecSep(const char* sep, const char* term = nullptr) {
vectorSeparator.push_back(VecPrint(sep, term));
}
void doneVec() {
BUG_CHECK(!vectorSeparator.empty(), "Empty vectorSeparator");
vectorSeparator.pop_back();
}
VecPrint getSep() {
BUG_CHECK(!vectorSeparator.empty(), "Empty vectorSeparator");
return vectorSeparator.back();
}
};
} // namespace FPP
#endif /* _BACKENDS_FPP_CODEGEN_H_ */