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 bool printAnnotations(const IR::IAnnotated *ann);
88
89 public:
90 // Output is constructed here
91 Util::SourceCodeBuilder &builder;
92 /* FIXME -- simplify this by getting rid of the 'builder' object and just emitting
93 * directly to the ostream. The SourceCodeBuilder object does not appear to add any
94 * useful functionality the ostream does not already provide; it just serves to
95 * obfuscate the code */
96 std::ostream *outStream = nullptr;
100 std::optional<std::filesystem::path> mainFile;
101
102 ToP4(Util::SourceCodeBuilder &builder, bool showIR) : showIR(showIR), builder(builder) {
103 visitDagOnce = false;
104 setName("ToP4");
105 }
106
107 ToP4(std::ostream *outStream, bool showIR) : ToP4(*new Util::SourceCodeBuilder(), showIR) {
108 this->outStream = outStream;
109 }
110
111 ToP4(Util::SourceCodeBuilder &builder, bool showIR, std::filesystem::path mainFile)
112 : ToP4(builder, showIR) {
113 this->mainFile = mainFile;
114 }
115 ToP4(std::ostream *outStream, bool showIR, std::filesystem::path mainFile)
116 : ToP4(outStream, showIR) {
117 this->mainFile = mainFile;
118 }
119
120 ToP4() : ToP4(*new Util::SourceCodeBuilder(), false) {}
121
122 using Inspector::preorder;
123
124 void setnoIncludesArg(bool condition) { noIncludes = condition; }
125
126 void setListTerm(const char *start, const char *end) {
127 listTerminators.push_back(ListPrint(start, end));
128 }
129 Visitor::profile_t init_apply(const IR::Node *node) override;
130 void end_apply(const IR::Node *node) override;
131
132 bool process(const IR::Type_StructLike *t, const char *name);
133 // types
134 bool preorder(const IR::Type_Boolean *t) override;
135 bool preorder(const IR::Type_Varbits *t) override;
136 bool preorder(const IR::Type_Bits *t) override;
137 bool preorder(const IR::Type_InfInt *t) override;
138 bool preorder(const IR::Type_String *t) override;
139 bool preorder(const IR::Type_Var *t) override;
140 bool preorder(const IR::Type_Dontcare *t) override;
141 bool preorder(const IR::Type_Void *t) override;
142 bool preorder(const IR::Type_Error *t) override;
143 bool preorder(const IR::Type_Struct *t) override { return process(t, "struct"); }
144 bool preorder(const IR::Type_Header *t) override { return process(t, "header"); }
145 bool preorder(const IR::Type_HeaderUnion *t) override { return process(t, "header_union"); }
146 bool preorder(const IR::Type_Package *t) override;
147 bool preorder(const IR::Type_Parser *t) override;
148 bool preorder(const IR::Type_Control *t) override;
149 bool preorder(const IR::Type_Name *t) override;
150 bool preorder(const IR::Type_Stack *t) override;
151 bool preorder(const IR::Type_Specialized *t) override;
152 bool preorder(const IR::Type_Enum *t) override;
153 bool preorder(const IR::Type_SerEnum *t) override;
154 bool preorder(const IR::Type_Typedef *t) override;
155 bool preorder(const IR::Type_Newtype *t) override;
156 bool preorder(const IR::Type_Extern *t) override;
157 bool preorder(const IR::Type_Unknown *t) override;
158 bool preorder(const IR::Type_BaseList *t) override;
159 bool preorder(const IR::Type *t) override {
160 builder.append(t->toString());
161 return false;
162 }
163 bool preorder(const IR::Type_SpecializedCanonical *t) override {
164 BUG("%1%: specialized canonical type in IR tree", t);
165 return false;
166 }
167
168 // declarations
169 bool preorder(const IR::Declaration_Constant *cst) override;
170 bool preorder(const IR::Declaration_Variable *v) override;
171 bool preorder(const IR::Declaration_Instance *t) override;
172 bool preorder(const IR::Declaration_MatchKind *d) override;
173
174 // expressions
175 bool preorder(const IR::Dots *e) override;
176 bool preorder(const IR::NamedDots *e) override;
177 bool preorder(const IR::Constant *c) override;
178 bool preorder(const IR::AbstractSlice *slice) override;
179 bool preorder(const IR::BoolLiteral *b) override;
180 bool preorder(const IR::StringLiteral *s) override;
181 bool preorder(const IR::PathExpression *p) override;
182 bool preorder(const IR::Cast *c) override;
183 bool preorder(const IR::Operation_Binary *b) override;
184 bool preorder(const IR::Operation_Unary *u) override;
185 bool preorder(const IR::ArrayIndex *a) override;
186 bool preorder(const IR::TypeNameExpression *e) override;
187 bool preorder(const IR::Mux *a) override;
188 bool preorder(const IR::ConstructorCallExpression *e) override;
189 bool preorder(const IR::Member *e) override;
190 bool preorder(const IR::SelectCase *e) override;
191 bool preorder(const IR::SelectExpression *e) override;
192 bool preorder(const IR::ListExpression *e) override;
193 bool preorder(const IR::P4ListExpression *e) override;
194 bool preorder(const IR::StructExpression *e) override;
195 bool preorder(const IR::Invalid *e) override;
196 bool preorder(const IR::InvalidHeader *e) override;
197 bool preorder(const IR::InvalidHeaderUnion *e) override;
198 bool preorder(const IR::HeaderStackExpression *e) override;
199 bool preorder(const IR::MethodCallExpression *e) override;
200 bool preorder(const IR::DefaultExpression *e) override;
201 bool preorder(const IR::This *e) override;
202
203 // vectors
204 bool preorder(const IR::Vector<IR::ActionListElement> *v) override;
205 bool preorder(const IR::Vector<IR::Annotation> *v) override;
206 bool preorder(const IR::Vector<IR::Entry> *v) override;
207 bool preorder(const IR::Vector<IR::Expression> *v) override;
208 bool preorder(const IR::Vector<IR::Argument> *v) override;
209 bool preorder(const IR::Vector<IR::KeyElement> *v) override;
210 bool preorder(const IR::Vector<IR::Method> *v) override;
211 bool preorder(const IR::Vector<IR::Node> *v) override;
212 bool preorder(const IR::Vector<IR::SelectCase> *v) override;
213 bool preorder(const IR::Vector<IR::SwitchCase> *v) override;
214 bool preorder(const IR::Vector<IR::Type> *v) override;
215 bool preorder(const IR::IndexedVector<IR::Declaration_ID> *v) override;
216 bool preorder(const IR::IndexedVector<IR::Declaration> *v) override;
217 bool preorder(const IR::IndexedVector<IR::Node> *v) override;
218 bool preorder(const IR::IndexedVector<IR::ParserState> *v) override;
219 bool preorder(const IR::IndexedVector<IR::StatOrDecl> *v) override;
220
221 // statements
222 bool preorder(const IR::AssignmentStatement *s) override;
223 bool preorder(const IR::BlockStatement *s) override;
224 bool preorder(const IR::MethodCallStatement *s) override;
225 bool preorder(const IR::EmptyStatement *s) override;
226 bool preorder(const IR::ReturnStatement *s) override;
227 bool preorder(const IR::BreakStatement *s) override;
228 bool preorder(const IR::ContinueStatement *s) override;
229 bool preorder(const IR::ExitStatement *s) override;
230 bool preorder(const IR::SwitchCase *s) override;
231 bool preorder(const IR::SwitchStatement *s) override;
232 bool preorder(const IR::IfStatement *s) override;
233 bool preorder(const IR::ForStatement *s) override;
234 bool preorder(const IR::ForInStatement *s) override;
235
236 // misc
237 bool preorder(const IR::NamedExpression *ne) override;
238 bool preorder(const IR::Argument *arg) override;
239 bool preorder(const IR::Path *p) override;
240 bool preorder(const IR::Parameter *p) 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:100
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