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 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, 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
112template <Arch arch>
114
115template <>
116struct ActionSelectorTraits<Arch::V1MODEL> : public ActionProfileTraits<Arch::V1MODEL> {
117 static const cstring name() { return "action selector"_cs; }
118 static const cstring typeName() { return P4V1::V1Model::instance.action_selector.name; }
119};
120
121template <>
122struct ActionSelectorTraits<Arch::V1MODEL2020> : public ActionProfileTraits<Arch::V1MODEL2020> {};
123
124template <>
125struct ActionSelectorTraits<Arch::PSA> : public ActionProfileTraits<Arch::PSA> {
126 static const cstring name() { return "action selector"_cs; }
127 static const cstring typeName() { return "ActionSelector"_cs; }
128};
129
131template <Arch arch>
133
134template <>
135struct RegisterTraits<Arch::V1MODEL> {
136 static const cstring name() { return "register"_cs; }
137 static const cstring typeName() { return P4V1::V1Model::instance.registers.name; }
138 static const cstring sizeParamName() { return "size"_cs; }
141 static size_t dataTypeParamIdx() { return 0; }
142 static std::optional<size_t> indexTypeParamIdx() { return std::nullopt; }
143};
144
145template <>
146struct RegisterTraits<Arch::V1MODEL2020> : public RegisterTraits<Arch::V1MODEL> {
147 static std::optional<size_t> indexTypeParamIdx() { return 1; }
148};
149
150template <>
151struct RegisterTraits<Arch::PSA> {
152 static const cstring name() { return "register"_cs; }
153 static const cstring typeName() { return "Register"_cs; }
154 static const cstring sizeParamName() { return "size"_cs; }
155 static size_t dataTypeParamIdx() { return 0; }
158 static std::optional<size_t> indexTypeParamIdx() { return 1; }
159};
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::MeterExtern<Standard::Arch::V1MODEL>> {
228 static const cstring name() { return "meter"_cs; }
229 static const cstring directPropertyName() {
230 return P4V1::V1Model::instance.tableAttributes.meters.name;
231 }
232 static const cstring typeName() { return P4V1::V1Model::instance.meter.name; }
233 static const cstring directTypeName() { return P4V1::V1Model::instance.directMeter.name; }
234 static const cstring sizeParamName() { return "size"_cs; }
235 static std::optional<size_t> indexTypeParamIdx() { return std::nullopt; }
236};
237
238template <>
239struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::V1MODEL2020>> {
240 static const cstring name() { return "meter"_cs; }
241 static const cstring directPropertyName() {
242 return P4V1::V1Model::instance.tableAttributes.meters.name;
243 }
244 static const cstring typeName() { return P4V1::V1Model::instance.meter.name; }
245 static const cstring directTypeName() { return P4V1::V1Model::instance.directMeter.name; }
246 static const cstring sizeParamName() { return "size"_cs; }
247 static std::optional<size_t> indexTypeParamIdx() { return 0; }
248};
249
251template <>
252struct CounterlikeTraits<Standard::MeterExtern<Standard::Arch::PSA>> {
253 static const cstring name() { return "meter"_cs; }
254 static const cstring directPropertyName() { return "psa_direct_meter"_cs; }
255 static const cstring typeName() { return "Meter"_cs; }
256 static const cstring directTypeName() { return "DirectMeter"_cs; }
257 static const cstring sizeParamName() { return "n_meters"_cs; }
260 static std::optional<size_t> indexTypeParamIdx() { return 0; }
261};
262
263} // namespace Helpers
264
265using BlockTypeMap = std::map<const IR::Block *, const IR::Type *>;
266
272using SelectorInput = std::vector<const IR::Expression *>;
273
277 P4::TypeMap *typeMap;
278 const IR::ToplevelBlock *toplevel;
280 BlockConverted blockConverted;
287
290
291 std::map<const IR::Declaration_Instance *, SelectorInput> selector_input_map;
292
293 const SelectorInput *get_selector_input(const IR::Declaration_Instance *selector) {
294 auto it = selector_input_map.find(selector);
295 if (it == selector_input_map.end()) return nullptr; // selector never used
296 return &it->second;
297 }
298
300 const IR::ToplevelBlock *toplevel, P4::ProgramStructure *structure,
302 : refMap(refMap),
303 typeMap(typeMap),
304 toplevel(toplevel),
305 blockConverted(BlockConverted::None),
307 conv(conv),
308 json(json) {}
309
310 void addToFieldList(const IR::Expression *expr, Util::JsonArray *fl);
311 int createFieldList(const IR::Expression *expr, cstring listName, bool learn = false);
312 cstring createCalculation(cstring algo, const IR::Expression *fields,
313 Util::JsonArray *calculations, bool usePayload, const IR::Node *node);
314 static void modelError(const char *format, const IR::Node *place);
315};
316
317Util::IJson *nodeName(const CFG::Node *node);
318Util::JsonArray *mkArrayField(Util::JsonObject *parent, cstring name);
319Util::JsonArray *mkParameters(Util::JsonObject *object);
320Util::JsonObject *mkPrimitive(cstring name, Util::JsonArray *appendTo);
321Util::JsonObject *mkPrimitive(cstring name);
322cstring stringRepr(big_int value, unsigned bytes = 0);
323unsigned nextId(cstring group);
325const IR::ListExpression *convertToList(const IR::Expression *expr, P4::TypeMap *typeMap);
326
327} // namespace BMV2
328
329#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:93
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:80
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:272
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:76
Definition helpers.h:274
BlockConverted blockConverted
Block currently being converted.
Definition helpers.h:280
ExpressionConverter * conv
Expression converter is used in many places.
Definition helpers.h:284
BMV2::JsonObjects * json
Final json output.
Definition helpers.h:286
P4::ProgramStructure * structure
ProgramStructure pointer.
Definition helpers.h:282
P4::ReferenceMap * refMap
context
Definition helpers.h:276
Util::JsonArray * action_profiles
For action profile conversion.
Definition helpers.h:289
static std::optional< size_t > indexTypeParamIdx()
Definition helpers.h:222
static std::optional< size_t > indexTypeParamIdx()
Definition helpers.h:260
Definition helpers.h:171
Definition helpers.h:113
Definition helpers.h:162
Definition helpers.h:164
static std::optional< size_t > indexTypeParamIdx()
Definition helpers.h:158
static size_t dataTypeParamIdx()
Definition helpers.h:141
Traits for the register extern, must be specialized for v1model and PSA.
Definition helpers.h:132