8#ifndef BACKENDS_BMV2_PSA_SWITCH_PSASWITCH_H_
9#define BACKENDS_BMV2_PSA_SWITCH_PSASWITCH_H_
11#include "backends/bmv2/portable_common/portable.h"
12#include "backends/common/psaProgramStructure.h"
16class PsaSwitchExpressionConverter :
public ExpressionConverter {
22 void modelError(
const char *format,
const cstring field) {
24 (
cstring(format) +
"\nInvalid metadata parameter value for PSA").c_str(),
28 Util::IJson *convertParam(UNUSED
const IR::Parameter *param,
cstring fieldName)
override {
29 cstring ptName = param->type->toString();
32 jsn->emplace(
"name", param->toString());
33 jsn->emplace(
"type",
"hexstr");
39 if (fieldName ==
"BYTES") {
40 cstring repr = BMV2::stringRepr(0, ROUNDUP(bitwidth, 32));
41 jsn->emplace(
"value", repr);
42 }
else if (fieldName ==
"PACKETS") {
43 cstring repr = BMV2::stringRepr(1, ROUNDUP(bitwidth, 32));
44 jsn->emplace(
"value", repr);
45 }
else if (fieldName ==
"PACKETS_AND_BYTES") {
46 cstring repr = BMV2::stringRepr(2, ROUNDUP(bitwidth, 32));
47 jsn->emplace(
"value", repr);
49 modelError(
"%1%: Exptected a PSA_CounterType_t", fieldName);
58 jsn->emplace(
"type",
"field");
59 auto a = mkArrayField(jsn,
"value"_cs);
60 a->append(ptName.exceptLast(2));
82class ConvertPsaToJson :
public Inspector {
86 const IR::ToplevelBlock *toplevel;
94 : refMap(refMap), typeMap(typeMap), toplevel(toplevel),
json(
json), structure(structure) {
99 CHECK_NULL(structure);
101 CHECK_NULL(codeGenerator);
104 void postorder(UNUSED
const IR::P4Program *program)
override {
105 cstring scalarsName =
"scalars"_cs;
109 codeGenerator->create(ctxt, structure);
113class PsaSwitchBackend :
public Backend {
117 void convert(
const IR::ToplevelBlock *tlb)
override;
119 P4::ConvertEnums::EnumMapping *enumMap)
120 : Backend(options, refMap, typeMap, enumMap), options(options) {}
123EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Hash)
124EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(InternetChecksum)
125EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Register)
Definition backends/bmv2/common/options.h:18
Definition expression.h:42
Definition JsonObjects.h:17
Definition psaSwitch.h:71
Definition psaSwitch.h:16
Definition portableProgramStructure.h:20
Definition backends/common/programStructure.h:23
Definition psaProgramStructure.h:26
static bool isCounterMetadata(cstring ptName)
Definition psaProgramStructure.h:37
static bool isStandardMetadata(cstring ptName)
Definition psaProgramStructure.h:41
Class used to encode maps from paths to declarations.
Definition referenceMap.h:67
Definition lib/json.h:177
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:9
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58
Definition bmv2/common/helpers.h:288