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