P4C
The P4 Compiler
Loading...
Searching...
No Matches
toP4.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef P4_TOP4_TOP4_H_
18#define P4_TOP4_TOP4_H_
19
20#include <filesystem>
21#include <iostream>
22#include <optional>
23#include <vector>
24
25#include "frontends/common/resolveReferences/resolveReferences.h"
26#include "ir/ir.h"
27#include "ir/visitor.h"
28#include "lib/sourceCodeBuilder.h"
29
30namespace P4 {
31
37 protected:
39 int expressionPrecedence = DBPrint::Prec_Low;
40 bool isDeclaration = true;
41 bool showIR;
42 bool withinArgument = false;
43 bool noIncludes = false;
45
46 struct VecPrint {
47 cstring separator;
48 cstring terminator;
49
50 VecPrint(const char *sep, const char *term) : separator(sep), terminator(term) {}
51 };
52
53 struct ListPrint {
54 cstring start;
55 cstring end;
56
57 ListPrint(const char *start, const char *end) : start(start), end(end) {}
58 };
59
60 // maintained as stacks
61 std::vector<VecPrint> vectorSeparator;
62 size_t vectorSeparator_init_apply_size = 0;
63 std::vector<ListPrint> listTerminators;
64 size_t listTerminators_init_apply_size = 0;
65
66 void setVecSep(const char *sep, const char *term = nullptr) {
67 vectorSeparator.push_back(VecPrint(sep, term));
68 }
69 void doneVec() {
70 BUG_CHECK(!vectorSeparator.empty(), "Empty vectorSeparator");
71 vectorSeparator.pop_back();
72 }
73 VecPrint getSep() {
74 BUG_CHECK(!vectorSeparator.empty(), "Empty vectorSeparator");
75 return vectorSeparator.back();
76 }
77
78 void doneList() {
79 BUG_CHECK(!listTerminators.empty(), "Empty listTerminators");
80 listTerminators.pop_back();
81 }
83 std::optional<cstring> ifSystemFile(const IR::Node *node);
85 void dump(unsigned depth, const IR::Node *node = nullptr, unsigned adjDepth = 0);
86 unsigned curDepth() const;
87
88 public:
89 // Output is constructed here
90 Util::SourceCodeBuilder &builder;
91 /* FIXME -- simplify this by getting rid of the 'builder' object and just emitting
92 * directly to the ostream. The SourceCodeBuilder object does not appear to add any
93 * useful functionality the ostream does not already provide; it just serves to
94 * obfuscate the code */
95 std::ostream *outStream = nullptr;
99 std::optional<std::filesystem::path> mainFile;
100
101 ToP4(Util::SourceCodeBuilder &builder, bool showIR) : showIR(showIR), builder(builder) {
102 visitDagOnce = false;
103 setName("ToP4");
104 }
105
106 ToP4(std::ostream *outStream, bool showIR) : ToP4(*new Util::SourceCodeBuilder(), showIR) {
107 this->outStream = outStream;
108 }
109
110 ToP4(Util::SourceCodeBuilder &builder, bool showIR, std::filesystem::path mainFile)
111 : ToP4(builder, showIR) {
112 this->mainFile = mainFile;
113 }
114 ToP4(std::ostream *outStream, bool showIR, std::filesystem::path mainFile)
115 : ToP4(outStream, showIR) {
116 this->mainFile = mainFile;
117 }
118
119 ToP4() : ToP4(*new Util::SourceCodeBuilder(), false) {}
120
121 using Inspector::preorder;
122
123 void setnoIncludesArg(bool condition) { noIncludes = condition; }
124
125 void setListTerm(const char *start, const char *end) {
126 listTerminators.push_back(ListPrint(start, end));
127 }
128 Visitor::profile_t init_apply(const IR::Node *node) override;
129 void end_apply(const IR::Node *node) override;
130
131 bool process(const IR::Type_StructLike *t, const char *name);
132 // types
133 bool preorder(const IR::Type_Boolean *t) override;
134 bool preorder(const IR::Type_Varbits *t) override;
135 bool preorder(const IR::Type_Bits *t) override;
136 bool preorder(const IR::Type_InfInt *t) override;
137 bool preorder(const IR::Type_String *t) override;
138 bool preorder(const IR::Type_Var *t) override;
139 bool preorder(const IR::Type_Dontcare *t) override;
140 bool preorder(const IR::Type_Void *t) override;
141 bool preorder(const IR::Type_Error *t) override;
142 bool preorder(const IR::Type_Struct *t) override { return process(t, "struct"); }
143 bool preorder(const IR::Type_Header *t) override { return process(t, "header"); }
144 bool preorder(const IR::Type_HeaderUnion *t) override { return process(t, "header_union"); }
145 bool preorder(const IR::Type_Package *t) override;
146 bool preorder(const IR::Type_Parser *t) override;
147 bool preorder(const IR::Type_Control *t) override;
148 bool preorder(const IR::Type_Name *t) override;
149 bool preorder(const IR::Type_Stack *t) override;
150 bool preorder(const IR::Type_Specialized *t) override;
151 bool preorder(const IR::Type_Enum *t) override;
152 bool preorder(const IR::Type_SerEnum *t) override;
153 bool preorder(const IR::Type_Typedef *t) override;
154 bool preorder(const IR::Type_Newtype *t) override;
155 bool preorder(const IR::Type_Extern *t) override;
156 bool preorder(const IR::Type_Unknown *t) override;
157 bool preorder(const IR::Type_BaseList *t) override;
158 bool preorder(const IR::Type *t) override {
159 builder.append(t->toString());
160 return false;
161 }
162 bool preorder(const IR::Type_SpecializedCanonical *t) override {
163 BUG("%1%: specialized canonical type in IR tree", t);
164 return false;
165 }
166
167 // declarations
168 bool preorder(const IR::Declaration_Constant *cst) override;
169 bool preorder(const IR::Declaration_Variable *v) override;
170 bool preorder(const IR::Declaration_Instance *t) override;
171 bool preorder(const IR::Declaration_MatchKind *d) override;
172
173 // expressions
174 bool preorder(const IR::Dots *e) override;
175 bool preorder(const IR::NamedDots *e) override;
176 bool preorder(const IR::Constant *c) override;
177 bool preorder(const IR::AbstractSlice *slice) override;
178 bool preorder(const IR::BoolLiteral *b) override;
179 bool preorder(const IR::StringLiteral *s) override;
180 bool preorder(const IR::PathExpression *p) override;
181 bool preorder(const IR::Cast *c) override;
182 bool preorder(const IR::Operation_Binary *b) override;
183 bool preorder(const IR::Operation_Unary *u) override;
184 bool preorder(const IR::ArrayIndex *a) override;
185 bool preorder(const IR::TypeNameExpression *e) override;
186 bool preorder(const IR::Mux *a) override;
187 bool preorder(const IR::ConstructorCallExpression *e) override;
188 bool preorder(const IR::Member *e) override;
189 bool preorder(const IR::SelectCase *e) override;
190 bool preorder(const IR::SelectExpression *e) override;
191 bool preorder(const IR::ListExpression *e) override;
192 bool preorder(const IR::P4ListExpression *e) override;
193 bool preorder(const IR::StructExpression *e) override;
194 bool preorder(const IR::Invalid *e) override;
195 bool preorder(const IR::InvalidHeader *e) override;
196 bool preorder(const IR::InvalidHeaderUnion *e) override;
197 bool preorder(const IR::HeaderStackExpression *e) override;
198 bool preorder(const IR::MethodCallExpression *e) override;
199 bool preorder(const IR::DefaultExpression *e) override;
200 bool preorder(const IR::This *e) override;
201
202 // vectors
203 bool preorder(const IR::Vector<IR::ActionListElement> *v) override;
204 bool preorder(const IR::Vector<IR::Annotation> *v) override;
205 bool preorder(const IR::Vector<IR::Entry> *v) override;
206 bool preorder(const IR::Vector<IR::Expression> *v) override;
207 bool preorder(const IR::Vector<IR::Argument> *v) override;
208 bool preorder(const IR::Vector<IR::KeyElement> *v) override;
209 bool preorder(const IR::Vector<IR::Method> *v) override;
210 bool preorder(const IR::Vector<IR::Node> *v) override;
211 bool preorder(const IR::Vector<IR::SelectCase> *v) override;
212 bool preorder(const IR::Vector<IR::SwitchCase> *v) override;
213 bool preorder(const IR::Vector<IR::Type> *v) override;
214 bool preorder(const IR::IndexedVector<IR::Declaration_ID> *v) override;
215 bool preorder(const IR::IndexedVector<IR::Declaration> *v) override;
216 bool preorder(const IR::IndexedVector<IR::Node> *v) override;
217 bool preorder(const IR::IndexedVector<IR::ParserState> *v) override;
218 bool preorder(const IR::IndexedVector<IR::StatOrDecl> *v) override;
219
220 // statements
221 bool preorder(const IR::AssignmentStatement *s) override;
222 bool preorder(const IR::BlockStatement *s) override;
223 bool preorder(const IR::MethodCallStatement *s) override;
224 bool preorder(const IR::EmptyStatement *s) override;
225 bool preorder(const IR::ReturnStatement *s) override;
226 bool preorder(const IR::BreakStatement *s) override;
227 bool preorder(const IR::ContinueStatement *s) override;
228 bool preorder(const IR::ExitStatement *s) override;
229 bool preorder(const IR::SwitchCase *s) override;
230 bool preorder(const IR::SwitchStatement *s) override;
231 bool preorder(const IR::IfStatement *s) override;
232 bool preorder(const IR::ForStatement *s) override;
233 bool preorder(const IR::ForInStatement *s) override;
234
235 // misc
236 bool preorder(const IR::NamedExpression *ne) override;
237 bool preorder(const IR::Argument *arg) override;
238 bool preorder(const IR::Path *p) override;
239 bool preorder(const IR::Parameter *p) override;
240 bool preorder(const IR::Annotations *a) override;
241 bool preorder(const IR::Annotation *a) override;
242 bool preorder(const IR::P4Program *program) override;
243 bool preorder(const IR::P4Control *c) override;
244 bool preorder(const IR::P4Action *c) override;
245 bool preorder(const IR::ParserState *s) override;
246 bool preorder(const IR::P4Parser *c) override;
247 bool preorder(const IR::TypeParameters *p) override;
248 bool preorder(const IR::ParameterList *p) override;
249 bool preorder(const IR::Method *p) override;
250 bool preorder(const IR::Function *function) override;
251
252 bool preorder(const IR::ExpressionValue *v) override;
253 bool preorder(const IR::ActionListElement *ale) override;
254 bool preorder(const IR::ActionList *v) override;
255 bool preorder(const IR::Key *v) override;
256 bool preorder(const IR::Property *p) override;
257 bool preorder(const IR::TableProperties *t) override;
258 bool preorder(const IR::EntriesList *l) override;
259 bool preorder(const IR::Entry *e) override;
260 bool preorder(const IR::P4Table *c) override;
261 bool preorder(const IR::P4ValueSet *c) override;
262
263 // in case it is accidentally called on a V1Program
264 bool preorder(const IR::V1Program *) override { return false; }
265};
266
267std::string toP4(const IR::INode *node);
268void dumpP4(const IR::INode *node);
269
270} // namespace P4
271
272#endif /* P4_TOP4_TOP4_H_ */
Definition visitor.h:400
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Definition toP4.h:36
void dump(unsigned depth, const IR::Node *node=nullptr, unsigned adjDepth=0)
dump node IR tree up to depth - in the form of a comment
Definition toP4.cpp:131
bool noIncludes
if true we are within a method call argument
Definition toP4.h:43
bool showIR
current type is a declaration
Definition toP4.h:41
bool withinArgument
if true dump IR as comments
Definition toP4.h:42
std::optional< cstring > ifSystemFile(const IR::Node *node)
Definition toP4.cpp:49
int expressionPrecedence
precedence of current IR::Operation
Definition toP4.h:39
std::optional< std::filesystem::path > mainFile
Definition toP4.h:99
Definition sourceCodeBuilder.h:29
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
Definition toP4.h:53
Definition toP4.h:46