P4C
The P4 Compiler
Loading...
Searching...
No Matches
psaSwitch.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_PSA_SWITCH_PSASWITCH_H_
9#define BACKENDS_BMV2_PSA_SWITCH_PSASWITCH_H_
10
11#include "backends/bmv2/portable_common/portable.h"
12#include "backends/common/psaProgramStructure.h"
13
14namespace P4::BMV2 {
15
16class PsaSwitchExpressionConverter : public ExpressionConverter {
17 public:
18 PsaSwitchExpressionConverter(P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
19 P4::ProgramStructure *structure, cstring scalarsName)
20 : BMV2::ExpressionConverter(refMap, typeMap, structure, scalarsName) {}
21
22 void modelError(const char *format, const cstring field) {
23 ::P4::error(ErrorType::ERR_MODEL,
24 (cstring(format) + "\nInvalid metadata parameter value for PSA").c_str(),
25 field);
26 }
27
28 Util::IJson *convertParam(UNUSED const IR::Parameter *param, cstring fieldName) override {
29 cstring ptName = param->type->toString();
30 if (P4::PsaProgramStructure::isCounterMetadata(ptName)) { // check if its counter metadata
31 auto jsn = new Util::JsonObject();
32 jsn->emplace("name", param->toString());
33 jsn->emplace("type", "hexstr");
34 // FIXME -- how big is a PSA_CounterType_t? Being an enum type, we can't
35 // sensibly call param->width_bits() here.
36 auto bitwidth = 0;
37
38 // encode the counter type from enum -> int
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);
48 } else {
49 modelError("%1%: Exptected a PSA_CounterType_t", fieldName);
50 return nullptr;
51 }
52 return jsn;
54 ptName)) { // check if its psa metadata
55 auto jsn = new Util::JsonObject();
56
57 // encode the metadata type and field in json
58 jsn->emplace("type", "field");
59 auto a = mkArrayField(jsn, "value"_cs);
60 a->append(ptName.exceptLast(2));
61 a->append(fieldName);
62 return jsn;
63 } else {
64 // not a special type
65 return nullptr;
66 }
67 return nullptr;
68 }
69};
70
72 public:
73 // PsaCodeGenerator(P4::ReferenceMap *refMap, P4::TypeMap *typeMap)
74 // : PortableCodeGenerator(refMap, typeMap) {}
75
76 void create(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
77 void createParsers(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
78 void createControls(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
79 void createDeparsers(ConversionContext *ctxt, P4::PortableProgramStructure *structure);
80};
81
82class ConvertPsaToJson : public Inspector {
83 public:
84 P4::ReferenceMap *refMap;
85 P4::TypeMap *typeMap;
86 const IR::ToplevelBlock *toplevel;
88 P4::PsaProgramStructure *structure;
89 PsaCodeGenerator *codeGenerator;
90
91 ConvertPsaToJson(P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
92 const IR::ToplevelBlock *toplevel, JsonObjects *json,
93 P4::PsaProgramStructure *structure)
94 : refMap(refMap), typeMap(typeMap), toplevel(toplevel), json(json), structure(structure) {
95 CHECK_NULL(refMap);
96 CHECK_NULL(typeMap);
97 CHECK_NULL(toplevel);
98 CHECK_NULL(json);
99 CHECK_NULL(structure);
100 codeGenerator = new PsaCodeGenerator();
101 CHECK_NULL(codeGenerator);
102 }
103
104 void postorder(UNUSED const IR::P4Program *program) override {
105 cstring scalarsName = "scalars"_cs;
106 // This visitor is used in multiple passes to convert expression to json
107 auto conv = new PsaSwitchExpressionConverter(refMap, typeMap, structure, scalarsName);
108 auto ctxt = new ConversionContext(refMap, typeMap, toplevel, structure, conv, json);
109 codeGenerator->create(ctxt, structure);
110 }
111};
112
113class PsaSwitchBackend : public Backend {
114 BMV2Options &options;
115
116 public:
117 void convert(const IR::ToplevelBlock *tlb) override;
118 PsaSwitchBackend(BMV2Options &options, P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
119 P4::ConvertEnums::EnumMapping *enumMap)
120 : Backend(options, refMap, typeMap, enumMap), options(options) {}
121};
122
123EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Hash)
124EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(InternetChecksum)
125EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Register)
126
127} // namespace P4::BMV2
128
129#endif /* BACKENDS_BMV2_PSA_SWITCH_PSASWITCH_H_ */
Definition backends/bmv2/common/options.h:18
Definition expression.h:42
Definition JsonObjects.h:17
Definition portable.h:37
Definition psaSwitch.h:71
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 typeMap.h:32
Definition lib/json.h:41
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
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:58
Definition bson.cpp:69
Definition bmv2/common/helpers.h:288