P4C
The P4 Compiler
Loading...
Searching...
No Matches
v1_converters.h
1
19#ifndef BF_P4C_ARCH_FROMV1_0_V1_CONVERTERS_H_
20#define BF_P4C_ARCH_FROMV1_0_V1_CONVERTERS_H_
21
22#include <cmath>
23
24#include "frontends/p4/cloner.h"
25#include "frontends/p4/coreLibrary.h"
26#include "ir/ir.h"
27#include "lib/safe_vector.h"
28#include "v1_program_structure.h"
29
30namespace BFN {
31
32namespace V1 {
33
35 protected:
36 ProgramStructure *structure;
37
38 public:
39 explicit ExpressionConverter(ProgramStructure *structure) : structure(structure) {
40 CHECK_NULL(structure);
41 }
42 const IR::Expression *convert(const IR::Node *node) {
43 auto result = node->apply(*this);
44 return result->to<IR::Expression>();
45 }
46};
47
49 protected:
50 ProgramStructure *structure;
51
52 public:
53 explicit StatementConverter(ProgramStructure *structure) : structure(structure) {
54 CHECK_NULL(structure);
55 }
56 const IR::Statement *convert(const IR::Node *node) {
57 auto result = node->apply(*this);
58 return result->to<IR::Statement>();
59 }
60};
61
63 protected:
64 ProgramStructure *structure;
66 template <typename T>
67 const IR::Node *substitute(T *s) {
68 auto *orig = getOriginal<T>();
69 if (structure->_map.count(orig)) {
70 auto result = structure->_map.at(orig);
71 return result;
72 }
73 return s;
74 }
75
76 public:
77 explicit ControlConverter(ProgramStructure *structure) : structure(structure) {
78 CHECK_NULL(structure);
79 }
80
81 const IR::Node *postorder(IR::Declaration_Instance *node) {
82 return substitute<IR::Declaration_Instance>(node);
83 }
84
85 const IR::Node *postorder(IR::MethodCallStatement *node) {
86 return substitute<IR::MethodCallStatement>(node);
87 }
88
89 const IR::Node *postorder(IR::Property *node) { return substitute<IR::Property>(node); }
90
91 const IR::Node *postorder(IR::BFN::TnaControl *node) override;
92
93 const IR::P4Control *convert(const IR::Node *node) {
94 auto conv = node->apply(*this);
95 auto result = conv->to<IR::P4Control>();
96 BUG_CHECK(result != nullptr, "Conversion of %1% did not produce a control", node);
97 return result;
98 }
99};
100
102 public:
103 explicit IngressControlConverter(ProgramStructure *structure) : ControlConverter(structure) {
104 CHECK_NULL(structure);
105 }
106 const IR::Node *preorder(IR::P4Control *node) override;
107};
108
110 public:
111 explicit EgressControlConverter(ProgramStructure *structure) : ControlConverter(structure) {
112 CHECK_NULL(structure);
113 }
114 const IR::Node *preorder(IR::P4Control *node) override;
115};
116
118 public:
119 explicit IngressDeparserConverter(ProgramStructure *structure) : ControlConverter(structure) {
120 CHECK_NULL(structure);
121 }
122 const IR::Node *preorder(IR::P4Control *node) override;
123};
124
126 public:
127 explicit EgressDeparserConverter(ProgramStructure *structure) : ControlConverter(structure) {
128 CHECK_NULL(structure);
129 }
130 const IR::Node *preorder(IR::P4Control *node) override;
131};
132
134
136 protected:
137 ProgramStructure *structure;
139 template <typename T>
140 const IR::Node *substitute(T *s) {
141 auto *orig = getOriginal<T>();
142 if (structure->_map.count(orig)) {
143 auto result = structure->_map.at(orig);
144 return result;
145 }
146 return s;
147 }
148
149 public:
150 explicit ParserConverter(ProgramStructure *structure) : structure(structure) {
151 CHECK_NULL(structure);
152 }
153
154 const IR::Node *postorder(IR::AssignmentStatement *node) {
155 return substitute<IR::AssignmentStatement>(node);
156 }
157
158 const IR::Node *postorder(IR::SelectExpression *node) {
159 return substitute<IR::SelectExpression>(node);
160 }
161
162 const IR::Node *postorder(IR::Member *node) { return substitute<IR::Member>(node); }
163
164 const IR::P4Parser *convert(const IR::Node *node) {
165 auto conv = node->apply(*this);
166 auto result = conv->to<IR::P4Parser>();
167 BUG_CHECK(result != nullptr, "Conversion of %1% did not produce a parser", node);
168 return result;
169 }
170};
171
173 public:
174 explicit IngressParserConverter(ProgramStructure *structure) : ParserConverter(structure) {
175 CHECK_NULL(structure);
176 }
177 const IR::Node *postorder(IR::P4Parser *node) override;
178};
179
181 public:
182 explicit EgressParserConverter(ProgramStructure *structure) : ParserConverter(structure) {
183 CHECK_NULL(structure);
184 }
185 const IR::Node *postorder(IR::Declaration_Variable *node) override;
186 const IR::Node *postorder(IR::P4Parser *node) override;
187};
188
190
192 protected:
193 ProgramStructure *structure;
194
195 public:
196 explicit ExternConverter(ProgramStructure *structure) : structure(structure) {
197 CHECK_NULL(structure);
198 }
199 const IR::Node *postorder(IR::Member *node) override;
200 const IR::Declaration_Instance *convertExternInstance(const IR::Node *node) {
201 auto conv = node->apply(*this);
202 auto result = conv->to<IR::Declaration_Instance>();
203 BUG_CHECK(result != nullptr, "Conversion of %1% did not produce an extern instance", node);
204 return result;
205 }
206 const IR::MethodCallExpression *convertExternCall(const IR::Node *node) {
207 auto conv = node->apply(*this);
208 auto result = conv->to<IR::MethodCallExpression>();
209 BUG_CHECK(result != nullptr, "Conversion of %1% did not produce an extern method call",
210 node);
211 return result;
212 }
213
214 const IR::Statement *convertExternFunction(const IR::Node *node) {
215 auto conv = node->apply(*this);
216 auto result = conv->to<IR::Statement>();
217 BUG_CHECK(result != nullptr, "Conversion of %1% did not produce an extern method call",
218 node);
219 return result;
220 }
221
222 const IR::Node *convert(const IR::Node *node) {
223 auto conv = node->apply(*this);
224 return conv;
225 }
226};
227
229 const P4::ReferenceMap *refMap;
230 bool direct;
231
232 public:
233 explicit MeterConverter(ProgramStructure *structure, const P4::ReferenceMap *rm, bool direct)
234 : ExternConverter(structure), refMap(rm), direct(direct) {
235 CHECK_NULL(structure);
236 }
237 const IR::Node *postorder(IR::MethodCallStatement *node) override;
238 const IR::Expression *cast_if_needed(const IR::Expression *expr, int srcWidth, int dstWidth);
239};
240
242 public:
243 explicit RegisterConverter(ProgramStructure *structure) : ExternConverter(structure) {
244 CHECK_NULL(structure);
245 }
246 const IR::Node *postorder(IR::MethodCallStatement *node) override;
247};
248
250
252 // mapping enum name from v1model to tofino
253 ordered_map<cstring, cstring> enumsToTranslate = {
254 {"HashAlgorithm"_cs, "HashAlgorithm_t"_cs},
255 {"CounterType"_cs, "CounterType_t"_cs},
256 {"MeterType"_cs, "MeterType_t"_cs},
257 {"CloneType"_cs, nullptr}, // tofino has no mapping for CloneType
258 };
259 ordered_map<cstring, cstring> fieldsToTranslate = {
260 {"crc16"_cs, "CRC16"_cs},
261 {"csum16"_cs, "CSUM16"_cs},
262 {"packets"_cs, "PACKETS"_cs},
263 {"bytes"_cs, "BYTES"_cs},
264 {"packets_and_bytes"_cs, "PACKETS_AND_BYTES"_cs},
265 {"crc32"_cs, "CRC32"_cs},
266 {"identity"_cs, "IDENTITY"_cs},
267 {"random"_cs, "RANDOM"_cs}};
268
269 public:
271 : ExpressionConverter(structure) {
272 CHECK_NULL(structure);
273 }
274 const IR::Node *postorder(IR::Type_Name *node) override;
275 const IR::Node *postorder(IR::TypeNameExpression *node) override;
276 const IR::Node *postorder(IR::Member *node) override;
277};
278
280 public:
281 explicit PathExpressionConverter(ProgramStructure *structure) : ExpressionConverter(structure) {
282 CHECK_NULL(structure);
283 }
284 const IR::Node *postorder(IR::Member *node) override;
285 const IR::Node *postorder(IR::AssignmentStatement *node) override;
286};
287
289
291 public:
292 explicit ParserPriorityConverter(ProgramStructure *structure) : StatementConverter(structure) {
293 CHECK_NULL(structure);
294 }
295 const IR::Node *postorder(IR::AssignmentStatement *node) override;
296};
297
298} // namespace V1
299
300} // namespace BFN
301
302#endif /* BF_P4C_ARCH_FROMV1_0_V1_CONVERTERS_H_ */
Definition v1_converters.h:62
Definition v1_converters.h:109
const IR::Node * preorder(IR::P4Control *node) override
Definition v1_converters.cpp:151
Definition v1_converters.h:125
const IR::Node * preorder(IR::P4Control *node) override
Definition v1_converters.cpp:304
Definition v1_converters.h:180
Definition v1_converters.h:34
Definition v1_converters.h:191
Definition v1_converters.h:101
Definition v1_converters.h:117
Definition v1_converters.h:172
Definition v1_converters.h:228
Definition v1_converters.h:135
Definition v1_converters.h:290
Definition v1_converters.h:279
const IR::Node * postorder(IR::Member *node) override
map path expression
Definition v1_converters.cpp:576
Definition v1_converters.h:241
Definition v1_converters.h:48
Definition v1_converters.h:251
Definition cloner.h:26
Definition node.h:94
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition visitor.h:424
Definition ordered_map.h:32
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
Definition v1_program_structure.h:36
T * to() noexcept
Definition rtti.h:226