17#ifndef BACKENDS_BMV2_COMMON_HELPERS_H_
18#define BACKENDS_BMV2_COMMON_HELPERS_H_
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-14/fromv1.0/v1model.h"
27#include "lib/cstring.h"
29#include "lib/ordered_map.h"
36#define UNUSED __attribute__((__unused__))
42 static const cstring rangeMatchTypeName;
43 static const cstring optionalMatchTypeName;
48 static const unsigned defaultTableSize;
53 static const cstring jsonMetadataParameterName;
62enum class BlockConverted {
82enum class Arch { V1MODEL, PSA, PNA, V1MODEL2020 };
91 static const cstring name() {
return "action profile"_cs; }
92 static const cstring propertyName() {
93 return P4V1::V1Model::instance.tableAttributes.tableImplementation.name;
95 static const cstring typeName() {
return P4V1::V1Model::instance.action_profile.name; }
96 static const cstring sizeParamName() {
return "size"_cs; }
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; }
120 static const cstring name() {
return "action selector"_cs; }
121 static const cstring typeName() {
return P4V1::V1Model::instance.action_selector.name; }
129 static const cstring name() {
return "action selector"_cs; }
130 static const cstring typeName() {
return "ActionSelector"_cs; }
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; }
148 static std::optional<size_t> indexTypeParamIdx() {
return std::nullopt; }
153 static std::optional<size_t> indexTypeParamIdx() {
return 1; }
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; }
179template <
typename Kind>
199 static const cstring name() {
return "counter"_cs; }
200 static const cstring directPropertyName() {
201 return P4V1::V1Model::instance.tableAttributes.counters.name;
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; }
211 static const cstring name() {
return "counter"_cs; }
212 static const cstring directPropertyName() {
213 return P4V1::V1Model::instance.tableAttributes.counters.name;
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; }
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; }
238 static const cstring directPropertyName() {
return "pna_direct_counter"_cs; }
244 static const cstring name() {
return "meter"_cs; }
245 static const cstring directPropertyName() {
246 return P4V1::V1Model::instance.tableAttributes.meters.name;
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; }
256 static const cstring name() {
return "meter"_cs; }
257 static const cstring directPropertyName() {
258 return P4V1::V1Model::instance.tableAttributes.meters.name;
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; }
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; }
283 static const cstring directPropertyName() {
return "pna_direct_meter"_cs; }
288using BlockTypeMap = std::map<const IR::Block *, const IR::Type *>;
301 const IR::ToplevelBlock *toplevel;
314 std::map<const IR::Declaration_Instance *, SelectorInput> selector_input_map;
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;
334 int createFieldList(
const IR::Expression *expr,
cstring listName,
bool learn =
false);
335 cstring createCalculation(
cstring algo,
const IR::Expression *fields,
337 static void modelError(
const char *format,
const IR::Node *place);
345cstring stringRepr(big_int value,
unsigned bytes = 0);
Definition expression.h:51
Definition JsonObjects.h:27
Definition bmv2/common/helpers.h:39
static const cstring selectorMatchTypeName
constant definition for bmv2
Definition bmv2/common/helpers.h:41
Definition bmv2/common/helpers.h:46
Definition bmv2/common/helpers.h:51
static const cstring validField
Definition bmv2/common/helpers.h:58
Definition backends/common/programStructure.h:32
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:21
std::vector< const IR::Expression * > SelectorInput
Definition bmv2/common/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 bmv2/common/helpers.cpp:184
Definition tofino/bf-p4c/control-plane/bfruntime_arch_handler.h:78
Definition bmv2/common/helpers.h:297
ExpressionConverter * conv
Expression converter is used in many places.
Definition bmv2/common/helpers.h:307
P4::ReferenceMap * refMap
context
Definition bmv2/common/helpers.h:299
P4::ProgramStructure * structure
ProgramStructure pointer.
Definition bmv2/common/helpers.h:305
BMV2::JsonObjects * json
Final json output.
Definition bmv2/common/helpers.h:309
BlockConverted blockConverted
Block currently being converted.
Definition bmv2/common/helpers.h:303
Util::JsonArray * action_profiles
For action profile conversion.
Definition bmv2/common/helpers.h:312
static std::optional< size_t > indexTypeParamIdx()
Definition bmv2/common/helpers.h:231
static std::optional< size_t > indexTypeParamIdx()
Definition bmv2/common/helpers.h:276
Definition bmv2/common/helpers.h:180
Definition bmv2/common/helpers.h:87
Definition bmv2/common/helpers.h:116
Definition bmv2/common/helpers.h:171
Definition bmv2/common/helpers.h:173
static std::optional< size_t > indexTypeParamIdx()
Definition bmv2/common/helpers.h:164
static size_t dataTypeParamIdx()
Definition bmv2/common/helpers.h:147
Traits for the register extern, must be specialized for v1model and PSA.
Definition bmv2/common/helpers.h:138