P4C
The P4 Compiler
Loading...
Searching...
No Matches
bmv2/common/helpers.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef BACKENDS_BMV2_COMMON_HELPERS_H_
9#define BACKENDS_BMV2_COMMON_HELPERS_H_
10
11#include "JsonObjects.h"
12#include "backends/common/programStructure.h"
13#include "controlFlowGraph.h"
14#include "expression.h"
15#include "frontends/common/model.h"
16#include "frontends/p4-14/fromv1.0/v1model.h"
17#include "ir/ir.h"
18#include "lib/cstring.h"
19#include "lib/json.h"
20#include "lib/ordered_map.h"
21
22namespace P4::BMV2 {
23
24using namespace P4::literals;
25
26#ifndef UNUSED
27#define UNUSED __attribute__((__unused__))
28#endif
29
31 public:
33 static const cstring rangeMatchTypeName;
34 static const cstring optionalMatchTypeName;
35};
36
38 public:
39 static const unsigned defaultTableSize;
40};
41
43 public:
44 static const cstring jsonMetadataParameterName;
45
49 static const cstring validField;
50};
51
52// V1Model-specific blocks.
53enum class BlockConverted {
54 None,
55 Parser,
56 Ingress,
57 Egress,
58 Deparser,
59 ChecksumCompute,
60 ChecksumVerify
61};
62
63namespace Standard {
64
73enum class Arch { V1MODEL, PSA, PNA, V1MODEL2020 };
74
77template <Arch arch>
79
80template <>
81struct ActionProfileTraits<Arch::V1MODEL> {
82 static const cstring name() { return "action profile"_cs; }
83 static const cstring propertyName() {
84 return P4V1::V1Model::instance.tableAttributes.tableImplementation.name;
85 }
86 static const cstring typeName() { return P4V1::V1Model::instance.action_profile.name; }
87 static const cstring sizeParamName() { return "size"_cs; }
88};
89
90template <>
91struct ActionProfileTraits<Arch::V1MODEL2020> : public ActionProfileTraits<Arch::V1MODEL> {};
92
93template <>
94struct ActionProfileTraits<Arch::PSA> {
95 static const cstring name() { return "action profile"_cs; }
96 static const cstring propertyName() { return "implementation"_cs; }
97 static const cstring typeName() { return "ActionProfile"_cs; }
98 static const cstring sizeParamName() { return "size"_cs; }
99};
100
101template <>
102struct ActionProfileTraits<Arch::PNA> : public ActionProfileTraits<Arch::PSA> {};
103
106template <Arch arch>
108
109template <>
110struct ActionSelectorTraits<Arch::V1MODEL> : public ActionProfileTraits<Arch::V1MODEL> {
111 static const cstring name() { return "action selector"_cs; }
112 static const cstring typeName() { return P4V1::V1Model::instance.action_selector.name; }
113};
114
115template <>
116struct ActionSelectorTraits<Arch::V1MODEL2020> : public ActionProfileTraits<Arch::V1MODEL2020> {};
117
118template <>
119struct ActionSelectorTraits<Arch::PSA> : public ActionProfileTraits<Arch::PSA> {
120 static const cstring name() { return "action selector"_cs; }
121 static const cstring typeName() { return "ActionSelector"_cs; }
122};
123
124template <>
125struct ActionSelectorTraits<Arch::PNA> : public ActionSelectorTraits<Arch::PSA> {};
126
128template <Arch arch>
130
131template <>
132struct RegisterTraits<Arch::V1MODEL> {
133 static const cstring name() { return "register"_cs; }
134 static const cstring typeName() { return P4V1::V1Model::instance.registers.name; }
135 static const cstring sizeParamName() { return "size"_cs; }
138 static size_t dataTypeParamIdx() { return 0; }
139 static std::optional<size_t> indexTypeParamIdx() { return std::nullopt; }
140};
141
142template <>
143struct RegisterTraits<Arch::V1MODEL2020> : public RegisterTraits<Arch::V1MODEL> {
144 static std::optional<size_t> indexTypeParamIdx() { return 1; }
145};
146
147template <>
148struct RegisterTraits<Arch::PSA> {
149 static const cstring name() { return "register"_cs; }
150 static const cstring typeName() { return "Register"_cs; }
151 static const cstring sizeParamName() { return "size"_cs; }
152 static size_t dataTypeParamIdx() { return 0; }
155 static std::optional<size_t> indexTypeParamIdx() { return 1; }
156};
157
158template <>
159struct RegisterTraits<Arch::PNA> : public RegisterTraits<Arch::PSA> {};
160
161template <Arch arch>
163template <Arch arch>
164struct MeterExtern {};
165
166} // namespace Standard
167
168namespace Helpers {
169
170template <typename Kind>
172
186
188template <>
189struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL>> {
190 static const cstring name() { return "counter"_cs; }
191 static const cstring directPropertyName() {
192 return P4V1::V1Model::instance.tableAttributes.counters.name;
193 }
194 static const cstring typeName() { return P4V1::V1Model::instance.counter.name; }
195 static const cstring directTypeName() { return P4V1::V1Model::instance.directCounter.name; }
196 static const cstring sizeParamName() { return "size"_cs; }
197 static std::optional<size_t> indexTypeParamIdx() { return std::nullopt; }
198};
199
200template <>
201struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::V1MODEL2020>> {
202 static const cstring name() { return "counter"_cs; }
203 static const cstring directPropertyName() {
204 return P4V1::V1Model::instance.tableAttributes.counters.name;
205 }
206 static const cstring typeName() { return P4V1::V1Model::instance.counter.name; }
207 static const cstring directTypeName() { return P4V1::V1Model::instance.directCounter.name; }
208 static const cstring sizeParamName() { return "size"_cs; }
209 static std::optional<size_t> indexTypeParamIdx() { return 0; }
210};
211
213template <>
214struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PSA>> {
215 static const cstring name() { return "counter"_cs; }
216 static const cstring directPropertyName() { return "psa_direct_counter"_cs; }
217 static const cstring typeName() { return "Counter"_cs; }
218 static const cstring directTypeName() { return "DirectCounter"_cs; }
219 static const cstring sizeParamName() { return "n_counters"_cs; }
222 static std::optional<size_t> indexTypeParamIdx() { return 1; }
223};
224
226template <>
227struct CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PNA>>
228 : public CounterlikeTraits<Standard::CounterExtern<Standard::Arch::PSA>> {
229 static const cstring directPropertyName() { return "pna_direct_counter"_cs; }
230};
231
233template <>
234struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL>> {
235 static const cstring name() { return "meter"_cs; }
236 static const cstring directPropertyName() {
237 return P4V1::V1Model::instance.tableAttributes.meters.name;
238 }
239 static const cstring typeName() { return P4V1::V1Model::instance.meter.name; }
240 static const cstring directTypeName() { return P4V1::V1Model::instance.directMeter.name; }
241 static const cstring sizeParamName() { return "size"_cs; }
242 static std::optional<size_t> indexTypeParamIdx() { return std::nullopt; }
243};
244
245template <>
246struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL2020>> {
247 static const cstring name() { return "meter"_cs; }
248 static const cstring directPropertyName() {
249 return P4V1::V1Model::instance.tableAttributes.meters.name;
250 }
251 static const cstring typeName() { return P4V1::V1Model::instance.meter.name; }
252 static const cstring directTypeName() { return P4V1::V1Model::instance.directMeter.name; }
253 static const cstring sizeParamName() { return "size"_cs; }
254 static std::optional<size_t> indexTypeParamIdx() { return 0; }
255};
256
258template <>
259struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PSA>> {
260 static const cstring name() { return "meter"_cs; }
261 static const cstring directPropertyName() { return "psa_direct_meter"_cs; }
262 static const cstring typeName() { return "Meter"_cs; }
263 static const cstring directTypeName() { return "DirectMeter"_cs; }
264 static const cstring sizeParamName() { return "n_meters"_cs; }
267 static std::optional<size_t> indexTypeParamIdx() { return 0; }
268};
269
271template <>
272struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PNA>>
273 : public CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PSA>> {
274 static const cstring directPropertyName() { return "pna_direct_meter"_cs; }
275};
276
277} // namespace Helpers
278
279using BlockTypeMap = std::map<const IR::Block *, const IR::Type *>;
280
286using SelectorInput = std::vector<const IR::Expression *>;
287
288struct ConversionContext {
291 P4::TypeMap *typeMap;
292 const IR::ToplevelBlock *toplevel;
294 BlockConverted blockConverted;
301
304
305 std::map<const IR::Declaration_Instance *, SelectorInput> selector_input_map;
306
307 const SelectorInput *get_selector_input(const IR::Declaration_Instance *selector) {
308 auto it = selector_input_map.find(selector);
309 if (it == selector_input_map.end()) return nullptr; // selector never used
310 return &it->second;
311 }
312
314 const IR::ToplevelBlock *toplevel, P4::ProgramStructure *structure,
316 : refMap(refMap),
317 typeMap(typeMap),
318 toplevel(toplevel),
319 blockConverted(BlockConverted::None),
321 conv(conv),
322 json(json) {}
323
324 void addToFieldList(const IR::Expression *expr, Util::JsonArray *fl);
325 int createFieldList(const IR::Expression *expr, cstring listName, bool learn = false);
326 cstring createCalculation(cstring algo, const IR::Expression *fields,
327 Util::JsonArray *calculations, bool usePayload, const IR::Node *node);
328 static void modelError(const char *format, const IR::Node *place);
329};
330
331Util::IJson *nodeName(const CFG::Node *node);
332Util::JsonArray *mkArrayField(Util::JsonObject *parent, cstring name);
333Util::JsonArray *mkParameters(Util::JsonObject *object);
334Util::JsonObject *mkPrimitive(cstring name, Util::JsonArray *appendTo);
335Util::JsonObject *mkPrimitive(cstring name);
336cstring stringRepr(big_int value, unsigned bytes = 0);
337unsigned nextId(cstring group);
339const IR::ListExpression *convertToList(const IR::Expression *expr, P4::TypeMap *typeMap);
340
341} // namespace P4::BMV2
342
343#endif /* BACKENDS_BMV2_COMMON_HELPERS_H_ */
Definition expression.h:42
Definition JsonObjects.h:17
Definition bmv2/common/helpers.h:30
static const cstring selectorMatchTypeName
constant definition for bmv2
Definition bmv2/common/helpers.h:32
Definition bmv2/common/helpers.h:37
Definition bmv2/common/helpers.h:42
static const cstring validField
Definition bmv2/common/helpers.h:49
Definition node.h:53
Definition backends/common/programStructure.h:23
Class used to encode maps from paths to declarations.
Definition referenceMap.h:67
Definition typeMap.h:32
Definition lib/json.h:41
Definition lib/json.h:128
Definition lib/json.h:177
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:9
std::vector< const IR::Expression * > SelectorInput
Definition bmv2/common/helpers.h:286
const IR::ListExpression * convertToList(const IR::Expression *expr, P4::TypeMap *typeMap)
Converts expr into a ListExpression or returns nullptr if not possible.
Definition bmv2/common/helpers.cpp:173
Definition tofino/bf-p4c/control-plane/bfruntime_arch_handler.h:77
Definition bson.cpp:69
Definition bmv2/common/helpers.h:288
ExpressionConverter * conv
Expression converter is used in many places.
Definition bmv2/common/helpers.h:298
P4::ReferenceMap * refMap
context
Definition bmv2/common/helpers.h:290
P4::ProgramStructure * structure
ProgramStructure pointer.
Definition bmv2/common/helpers.h:296
BlockConverted blockConverted
Block currently being converted.
Definition bmv2/common/helpers.h:294
Util::JsonArray * action_profiles
For action profile conversion.
Definition bmv2/common/helpers.h:303
static std::optional< size_t > indexTypeParamIdx()
Definition bmv2/common/helpers.h:222
static std::optional< size_t > indexTypeParamIdx()
Definition bmv2/common/helpers.h:267
Definition bmv2/common/helpers.h:171
Definition bmv2/common/helpers.h:78
Definition bmv2/common/helpers.h:107
Definition bmv2/common/helpers.h:162
Definition bmv2/common/helpers.h:164
static std::optional< size_t > indexTypeParamIdx()
Definition bmv2/common/helpers.h:155
static size_t dataTypeParamIdx()
Definition bmv2/common/helpers.h:138
Traits for the register extern, must be specialized for v1model and PSA.
Definition bmv2/common/helpers.h:129