P4C
The P4 Compiler
Loading...
Searching...
No Matches
helpers.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 BACKENDS_BMV2_COMMON_HELPERS_H_
18#define BACKENDS_BMV2_COMMON_HELPERS_H_
19
20#include "JsonObjects.h"
21#include "backends/common/programStructure.h"
22#include "controlFlowGraph.h"
23#include "expression.h"
24#include "frontends/common/model.h"
25#include "frontends/p4/fromv1.0/v1model.h"
26#include "ir/ir.h"
27#include "lib/cstring.h"
28#include "lib/json.h"
29#include "lib/ordered_map.h"
30
31namespace P4::BMV2 {
32
33using namespace P4::literals;
34
35#ifndef UNUSED
36#define UNUSED __attribute__((__unused__))
37#endif
38
40 public:
42 static const cstring rangeMatchTypeName;
43 static const cstring optionalMatchTypeName;
44};
45
47 public:
48 static const unsigned defaultTableSize;
49};
50
52 public:
53 static const cstring jsonMetadataParameterName;
54
58 static const cstring validField;
59};
60
61// V1Model-specific blocks.
62enum class BlockConverted {
63 None,
64 Parser,
65 Ingress,
66 Egress,
67 Deparser,
68 ChecksumCompute,
69 ChecksumVerify
70};
71
72namespace Standard {
73
82enum class Arch { V1MODEL, PSA, PNA, V1MODEL2020 };
83
86template <Arch arch>
88
89template <>
90struct ActionProfileTraits<Arch::V1MODEL> {
91 static const cstring name() { return "action profile"_cs; }
92 static const cstring propertyName() {
93 return P4V1::V1Model::instance.tableAttributes.tableImplementation.name;
94 }
95 static const cstring typeName() { return P4V1::V1Model::instance.action_profile.name; }
96 static const cstring sizeParamName() { return "size"_cs; }
97};
98
99template <>
100struct ActionProfileTraits<Arch::V1MODEL2020> : public ActionProfileTraits<Arch::V1MODEL> {};
101
102template <>
103struct ActionProfileTraits<Arch::PSA> {
104 static const cstring name() { return "action profile"_cs; }
105 static const cstring propertyName() { return "implementation"_cs; }
106 static const cstring typeName() { return "ActionProfile"_cs; }
107 static const cstring sizeParamName() { return "size"_cs; }
108};
109
110template <>
111struct ActionProfileTraits<Arch::PNA> : public ActionProfileTraits<Arch::PSA> {};
112
115template <Arch arch>
117
118template <>
119struct ActionSelectorTraits<Arch::V1MODEL> : public ActionProfileTraits<Arch::V1MODEL> {
120 static const cstring name() { return "action selector"_cs; }
121 static const cstring typeName() { return P4V1::V1Model::instance.action_selector.name; }
122};
123
124template <>
125struct ActionSelectorTraits<Arch::V1MODEL2020> : public ActionProfileTraits<Arch::V1MODEL2020> {};
126
127template <>
128struct ActionSelectorTraits<Arch::PSA> : public ActionProfileTraits<Arch::PSA> {
129 static const cstring name() { return "action selector"_cs; }
130 static const cstring typeName() { return "ActionSelector"_cs; }
131};
132
133template <>
134struct ActionSelectorTraits<Arch::PNA> : public ActionSelectorTraits<Arch::PSA> {};
135
137template <Arch arch>
139
140template <>
141struct RegisterTraits<Arch::V1MODEL> {
142 static const cstring name() { return "register"_cs; }
143 static const cstring typeName() { return P4V1::V1Model::instance.registers.name; }
144 static const cstring sizeParamName() { return "size"_cs; }
147 static size_t dataTypeParamIdx() { return 0; }
148 static std::optional<size_t> indexTypeParamIdx() { return std::nullopt; }
149};
150
151template <>
152struct RegisterTraits<Arch::V1MODEL2020> : public RegisterTraits<Arch::V1MODEL> {
153 static std::optional<size_t> indexTypeParamIdx() { return 1; }
154};
155
156template <>
157struct RegisterTraits<Arch::PSA> {
158 static const cstring name() { return "register"_cs; }
159 static const cstring typeName() { return "Register"_cs; }
160 static const cstring sizeParamName() { return "size"_cs; }
161 static size_t dataTypeParamIdx() { return 0; }
164 static std::optional<size_t> indexTypeParamIdx() { return 1; }
165};
166
167template <>
168struct RegisterTraits<Arch::PNA> : public RegisterTraits<Arch::PSA> {};
169
170template <Arch arch>
172template <Arch arch>
173struct MeterExtern {};
174
175} // namespace Standard
176
177namespace Helpers {
178
179template <typename Kind>
181
195
197template <>
198struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL>> {
199 static const cstring name() { return "counter"_cs; }
200 static const cstring directPropertyName() {
201 return P4V1::V1Model::instance.tableAttributes.counters.name;
202 }
203 static const cstring typeName() { return P4V1::V1Model::instance.counter.name; }
204 static const cstring directTypeName() { return P4V1::V1Model::instance.directCounter.name; }
205 static const cstring sizeParamName() { return "size"_cs; }
206 static std::optional<size_t> indexTypeParamIdx() { return std::nullopt; }
207};
208
209template <>
210struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL2020>> {
211 static const cstring name() { return "counter"_cs; }
212 static const cstring directPropertyName() {
213 return P4V1::V1Model::instance.tableAttributes.counters.name;
214 }
215 static const cstring typeName() { return P4V1::V1Model::instance.counter.name; }
216 static const cstring directTypeName() { return P4V1::V1Model::instance.directCounter.name; }
217 static const cstring sizeParamName() { return "size"_cs; }
218 static std::optional<size_t> indexTypeParamIdx() { return 0; }
219};
220
222template <>
223struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PSA>> {
224 static const cstring name() { return "counter"_cs; }
225 static const cstring directPropertyName() { return "psa_direct_counter"_cs; }
226 static const cstring typeName() { return "Counter"_cs; }
227 static const cstring directTypeName() { return "DirectCounter"_cs; }
228 static const cstring sizeParamName() { return "n_counters"_cs; }
231 static std::optional<size_t> indexTypeParamIdx() { return 1; }
232};
233
235template <>
236struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PNA>>
237 : public CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PSA>> {
238 static const cstring directPropertyName() { return "pna_direct_counter"_cs; }
239};
240
242template <>
243struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL>> {
244 static const cstring name() { return "meter"_cs; }
245 static const cstring directPropertyName() {
246 return P4V1::V1Model::instance.tableAttributes.meters.name;
247 }
248 static const cstring typeName() { return P4V1::V1Model::instance.meter.name; }
249 static const cstring directTypeName() { return P4V1::V1Model::instance.directMeter.name; }
250 static const cstring sizeParamName() { return "size"_cs; }
251 static std::optional<size_t> indexTypeParamIdx() { return std::nullopt; }
252};
253
254template <>
255struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL2020>> {
256 static const cstring name() { return "meter"_cs; }
257 static const cstring directPropertyName() {
258 return P4V1::V1Model::instance.tableAttributes.meters.name;
259 }
260 static const cstring typeName() { return P4V1::V1Model::instance.meter.name; }
261 static const cstring directTypeName() { return P4V1::V1Model::instance.directMeter.name; }
262 static const cstring sizeParamName() { return "size"_cs; }
263 static std::optional<size_t> indexTypeParamIdx() { return 0; }
264};
265
267template <>
268struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PSA>> {
269 static const cstring name() { return "meter"_cs; }
270 static const cstring directPropertyName() { return "psa_direct_meter"_cs; }
271 static const cstring typeName() { return "Meter"_cs; }
272 static const cstring directTypeName() { return "DirectMeter"_cs; }
273 static const cstring sizeParamName() { return "n_meters"_cs; }
276 static std::optional<size_t> indexTypeParamIdx() { return 0; }
277};
278
280template <>
281struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PNA>>
282 : public CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PSA>> {
283 static const cstring directPropertyName() { return "pna_direct_meter"_cs; }
284};
285
286} // namespace Helpers
287
288using BlockTypeMap = std::map<const IR::Block *, const IR::Type *>;
289
295using SelectorInput = std::vector<const IR::Expression *>;
296
300 P4::TypeMap *typeMap;
301 const IR::ToplevelBlock *toplevel;
303 BlockConverted blockConverted;
310
313
314 std::map<const IR::Declaration_Instance *, SelectorInput> selector_input_map;
315
316 const SelectorInput *get_selector_input(const IR::Declaration_Instance *selector) {
317 auto it = selector_input_map.find(selector);
318 if (it == selector_input_map.end()) return nullptr; // selector never used
319 return &it->second;
320 }
321
323 const IR::ToplevelBlock *toplevel, P4::ProgramStructure *structure,
325 : refMap(refMap),
326 typeMap(typeMap),
327 toplevel(toplevel),
328 blockConverted(BlockConverted::None),
330 conv(conv),
331 json(json) {}
332
333 void addToFieldList(const IR::Expression *expr, Util::JsonArray *fl);
334 int createFieldList(const IR::Expression *expr, cstring listName, bool learn = false);
335 cstring createCalculation(cstring algo, const IR::Expression *fields,
336 Util::JsonArray *calculations, bool usePayload, const IR::Node *node);
337 static void modelError(const char *format, const IR::Node *place);
338};
339
340Util::IJson *nodeName(const CFG::Node *node);
341Util::JsonArray *mkArrayField(Util::JsonObject *parent, cstring name);
342Util::JsonArray *mkParameters(Util::JsonObject *object);
343Util::JsonObject *mkPrimitive(cstring name, Util::JsonArray *appendTo);
344Util::JsonObject *mkPrimitive(cstring name);
345cstring stringRepr(big_int value, unsigned bytes = 0);
346unsigned nextId(cstring group);
348const IR::ListExpression *convertToList(const IR::Expression *expr, P4::TypeMap *typeMap);
349
350} // namespace P4::BMV2
351
352#endif /* BACKENDS_BMV2_COMMON_HELPERS_H_ */
Definition expression.h:51
Definition JsonObjects.h:27
Definition helpers.h:39
static const cstring selectorMatchTypeName
constant definition for bmv2
Definition helpers.h:41
Definition helpers.h:46
Definition helpers.h:51
static const cstring validField
Definition helpers.h:58
Definition node.h:95
Definition backends/common/programStructure.h:32
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition json.h:40
Definition json.h:115
Definition json.h:164
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:21
std::vector< const IR::Expression * > SelectorInput
Definition helpers.h:295
const IR::ListExpression * convertToList(const IR::Expression *expr, P4::TypeMap *typeMap)
Converts expr into a ListExpression or returns nullptr if not possible.
Definition helpers.cpp:184
Definition cstring.h:80
Definition helpers.h:297
ExpressionConverter * conv
Expression converter is used in many places.
Definition helpers.h:307
P4::ReferenceMap * refMap
context
Definition helpers.h:299
P4::ProgramStructure * structure
ProgramStructure pointer.
Definition helpers.h:305
BMV2::JsonObjects * json
Final json output.
Definition helpers.h:309
BlockConverted blockConverted
Block currently being converted.
Definition helpers.h:303
Util::JsonArray * action_profiles
For action profile conversion.
Definition helpers.h:312
static std::optional< size_t > indexTypeParamIdx()
Definition helpers.h:231
static std::optional< size_t > indexTypeParamIdx()
Definition helpers.h:276
Definition helpers.h:180
Definition helpers.h:171
Definition helpers.h:173
static std::optional< size_t > indexTypeParamIdx()
Definition helpers.h:164
static size_t dataTypeParamIdx()
Definition helpers.h:147
Traits for the register extern, must be specialized for v1model and PSA.
Definition helpers.h:138