P4C
The P4 Compiler
Loading...
Searching...
No Matches
psa_converters.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_ARCH_PSA_PSA_CONVERTERS_H_
20#define BACKENDS_TOFINO_BF_P4C_ARCH_PSA_PSA_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 "programStructure.h"
29
30namespace BFN {
31
32namespace PSA {
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
62class ExternConverter : public Transform {
63 protected:
64 ProgramStructure *structure;
65
66 public:
67 explicit ExternConverter(ProgramStructure *structure) : structure(structure) {
68 CHECK_NULL(structure);
69 }
70 const IR::Node *convert(const IR::Node *node) {
71 auto conv = node->apply(*this);
72 return conv;
73 }
74};
75
77 public:
78 explicit HashConverter(ProgramStructure *structure) : ExternConverter(structure) {
79 CHECK_NULL(structure);
80 }
81 const IR::Node *postorder(IR::MethodCallExpression *node) override;
82};
83
85 public:
86 explicit RandomConverter(ProgramStructure *structure) : ExternConverter(structure) {
87 CHECK_NULL(structure);
88 }
89 const IR::Node *postorder(IR::MethodCallExpression *node) override;
90};
91
93 public:
94 explicit MeterConverter(ProgramStructure *structure) : ExternConverter(structure) {
95 CHECK_NULL(structure);
96 }
97 const IR::Node *postorder(IR::MethodCallExpression *node) override;
98};
99
101 protected:
102 ProgramStructure *structure;
104 template <typename T>
105 const IR::Node *substitute(T *s) {
106 auto *orig = getOriginal<T>();
107 if (structure->_map.count(orig)) {
108 auto result = structure->_map.at(orig);
109 return result;
110 }
111 return s;
112 }
113
114 public:
115 explicit ParserConverter(ProgramStructure *structure) : structure(structure) {
116 CHECK_NULL(structure);
117 }
118 const IR::Node *postorder(IR::Declaration_Instance *decl) override;
119 const IR::Node *postorder(IR::StatOrDecl *node) override;
120 const IR::Node *postorder(IR::MethodCallExpression *node) override;
121 const IR::P4Parser *convert(const IR::Node *node) {
122 auto conv = node->apply(*this);
123 auto result = conv->to<IR::P4Parser>();
124 BUG_CHECK(result != nullptr, "Conversion of %1% did not produce a parser", node);
125 return result;
126 }
127};
128
130 public:
131 explicit IngressParserConverter(ProgramStructure *structure) : ParserConverter(structure) {
132 CHECK_NULL(structure);
133 }
134 const IR::Node *postorder(IR::P4Parser *node) override;
135};
136
138 public:
139 explicit EgressParserConverter(ProgramStructure *structure) : ParserConverter(structure) {
140 CHECK_NULL(structure);
141 }
142 const IR::Node *postorder(IR::P4Parser *node) override;
143};
144
146 protected:
147 ProgramStructure *structure;
149 template <typename T>
150 const IR::Node *substitute(T *s) {
151 auto *orig = getOriginal<T>();
152 if (structure->_map.count(orig)) {
153 auto result = structure->_map.at(orig);
154 return result;
155 }
156 return s;
157 }
158
159 public:
160 explicit ControlConverter(ProgramStructure *structure) : structure(structure) {
161 CHECK_NULL(structure);
162 }
163 const IR::Node *postorder(IR::MethodCallExpression *node) override;
164 const IR::Node *postorder(IR::Declaration_Instance *node) override;
165 const IR::Node *postorder(IR::IfStatement *node) override;
166 const IR::Node *postorder(IR::StatOrDecl *node) override;
167 const IR::Node *postorder(IR::Property *node) override;
168 const IR::Node *postorder(IR::Member *node) override;
169 const IR::P4Control *convert(const IR::Node *node) {
170 auto conv = node->apply(*this);
171 auto result = conv->to<IR::P4Control>();
172 BUG_CHECK(result != nullptr, "Conversion of %1% did not produce a control", node);
173 return result;
174 }
175};
176
178 public:
179 explicit IngressControlConverter(ProgramStructure *structure) : ControlConverter(structure) {
180 CHECK_NULL(structure);
181 }
182 const IR::Node *postorder(IR::P4Control *node) override;
183};
184
186 public:
187 explicit EgressControlConverter(ProgramStructure *structure) : ControlConverter(structure) {
188 CHECK_NULL(structure);
189 }
190 const IR::Node *postorder(IR::P4Control *node) override;
191};
192
194 public:
195 explicit IngressDeparserConverter(ProgramStructure *structure) : ControlConverter(structure) {
196 CHECK_NULL(structure);
197 }
198 const IR::Node *postorder(IR::P4Control *node) override;
199};
200
202 public:
203 explicit EgressDeparserConverter(ProgramStructure *structure) : ControlConverter(structure) {
204 CHECK_NULL(structure);
205 }
206 const IR::Node *postorder(IR::P4Control *node) override;
207};
208
210 // mapping enum name from psa to tofino
211 ordered_map<cstring, std::pair<cstring, bool /* isSerEnums */>> enumsToTranslate = {
212 {"PSA_HashAlgorithm_t"_cs, std::make_pair("HashAlgorithm_t"_cs, false)},
213 {"PSA_CounterType_t"_cs, std::make_pair("CounterType_t"_cs, false)},
214 {"PSA_MeterType_t"_cs, std::make_pair("MeterType_t"_cs, false)},
215 {"PSA_MeterColor_t"_cs, std::make_pair("MeterColor_t"_cs, true)}};
216
217 ordered_map<cstring, int> serEnumWidth = {{"MeterColor_t"_cs, 8}};
218
219 public:
221 : ExpressionConverter(structure) {
222 CHECK_NULL(structure);
223 }
224 const IR::Node *postorder(IR::TypeNameExpression *node) override;
225 const IR::Node *postorder(IR::Member *mem) override;
226};
227
229 protected:
230 ProgramStructure *structure;
231 // mapping enum name from psa to tofino
232 ordered_map<cstring, std::pair<cstring, bool /* isSerEnums */>> enumsToTranslate = {
233 {"PSA_HashAlgorithm_t"_cs, std::make_pair("HashAlgorithm_t"_cs, false)},
234 {"PSA_CounterType_t"_cs, std::make_pair("CounterType_t"_cs, false)},
235 {"PSA_MeterType_t"_cs, std::make_pair("MeterType_t"_cs, false)},
236 {"PSA_MeterColor_t"_cs, std::make_pair("MeterColor_t"_cs, true)}};
237
238 public:
239 explicit TypeNameConverter(ProgramStructure *structure) : structure(structure) {
240 CHECK_NULL(structure);
241 }
242 const IR::Node *postorder(IR::Type_Name *node) override;
243};
244
246 public:
247 explicit PathExpressionConverter(ProgramStructure *structure) : ExpressionConverter(structure) {
248 CHECK_NULL(structure);
249 }
250 const IR::Node *postorder(IR::Member *node) override;
251};
252
253} // namespace PSA
254
255} // namespace BFN
256
257#endif /* BACKENDS_TOFINO_BF_P4C_ARCH_PSA_PSA_CONVERTERS_H_ */
Definition psa_converters.h:145
Definition psa_converters.h:185
const IR::Node * postorder(IR::P4Control *node) override
Definition psa_converters.cpp:349
Definition psa_converters.h:201
Definition psa_converters.h:137
const IR::Node * postorder(IR::P4Parser *node) override
Definition psa_converters.cpp:212
Definition psa_converters.h:34
Definition psa_converters.h:62
Definition psa_converters.h:76
Definition psa_converters.h:177
Definition psa_converters.h:193
Definition psa_converters.h:129
Definition psa_converters.h:92
Definition psa_converters.h:100
Definition psa_converters.h:245
const IR::Node * postorder(IR::Member *node) override
map path expression
Definition psa_converters.cpp:631
Definition psa_converters.h:84
Definition psa_converters.h:48
Definition psa_converters.h:228
Definition psa_converters.h:209
Definition cloner.h:26
Definition node.h:95
Definition visitor.h:424
Definition cstring.h:85
Definition ordered_map.h:32
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
Definition backends/tofino/bf-p4c/arch/psa/programStructure.h:99
T * to() noexcept
Definition rtti.h:226