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/common/action.h"
21#include "backends/bmv2/common/control.h"
22#include "backends/bmv2/common/deparser.h"
23#include "backends/bmv2/common/extern.h"
24#include "backends/bmv2/common/header.h"
25#include "backends/bmv2/common/helpers.h"
26#include "backends/bmv2/common/lower.h"
27#include "backends/bmv2/common/parser.h"
28#include "backends/bmv2/common/programStructure.h"
29#include "frontends/common/constantFolding.h"
30#include "frontends/common/resolveReferences/referenceMap.h"
31#include "frontends/p4/coreLibrary.h"
32#include "frontends/p4/enumInstance.h"
33#include "frontends/p4/evaluator/evaluator.h"
34#include "frontends/p4/methodInstance.h"
35#include "frontends/p4/simplify.h"
36#include "frontends/p4/strengthReduction.h"
37#include "frontends/p4/typeMap.h"
38#include "frontends/p4/unusedDeclarations.h"
39#include "ir/ir.h"
40#include "lib/big_int_util.h"
41#include "lib/json.h"
42#include "psaProgramStructure.h"
43
44namespace BMV2 {
45
47 public:
49 ProgramStructure *structure, cstring scalarsName)
50 : BMV2::ExpressionConverter(refMap, typeMap, structure, scalarsName) {}
51
52 void modelError(const char *format, const cstring field) {
53 ::error(ErrorType::ERR_MODEL,
54 (cstring(format) + "\nInvalid metadata parameter value for PSA").c_str(), field);
55 }
56
57 Util::IJson *convertParam(UNUSED const IR::Parameter *param, cstring fieldName) override {
58 cstring ptName = param->type->toString();
59 if (PsaProgramStructure::isCounterMetadata(ptName)) { // check if its counter metadata
60 auto jsn = new Util::JsonObject();
61 jsn->emplace("name", param->toString());
62 jsn->emplace("type", "hexstr");
63 auto bitwidth = param->type->width_bits();
64
65 // encode the counter type from enum -> int
66 if (fieldName == "BYTES") {
67 cstring repr = BMV2::stringRepr(0, ROUNDUP(bitwidth, 32));
68 jsn->emplace("value", repr);
69 } else if (fieldName == "PACKETS") {
70 cstring repr = BMV2::stringRepr(1, ROUNDUP(bitwidth, 32));
71 jsn->emplace("value", repr);
72 } else if (fieldName == "PACKETS_AND_BYTES") {
73 cstring repr = BMV2::stringRepr(2, ROUNDUP(bitwidth, 32));
74 jsn->emplace("value", repr);
75 } else {
76 modelError("%1%: Exptected a PSA_CounterType_t", fieldName);
77 return nullptr;
78 }
79 return jsn;
80 } else if (PsaProgramStructure::isStandardMetadata(ptName)) { // check if its psa metadata
81 auto jsn = new Util::JsonObject();
82
83 // encode the metadata type and field in json
84 jsn->emplace("type", "field");
85 auto a = mkArrayField(jsn, "value");
86 a->append(ptName.exceptLast(2));
87 a->append(fieldName);
88 return jsn;
89 } else {
90 // not a special type
91 return nullptr;
92 }
93 return nullptr;
94 }
95};
96
98 public:
100 : PsaProgramStructure(refMap, typeMap) {}
101
102 void create(ConversionContext *ctxt);
103 void createStructLike(ConversionContext *ctxt, const IR::Type_StructLike *st);
104 void createTypes(ConversionContext *ctxt);
105 void createHeaders(ConversionContext *ctxt);
106 void createScalars(ConversionContext *ctxt);
107 void createParsers(ConversionContext *ctxt);
108 void createExterns();
109 void createActions(ConversionContext *ctxt);
110 void createControls(ConversionContext *ctxt);
111 void createDeparsers(ConversionContext *ctxt);
112 void createGlobals();
113 cstring convertHashAlgorithm(cstring algo);
114};
115
116class ConvertPsaToJson : public Inspector {
117 public:
118 P4::ReferenceMap *refMap;
119 P4::TypeMap *typeMap;
120 const IR::ToplevelBlock *toplevel;
121 JsonObjects *json;
122 PsaCodeGenerator *structure;
123
125 const IR::ToplevelBlock *toplevel, JsonObjects *json,
126 PsaCodeGenerator *structure)
127 : refMap(refMap), typeMap(typeMap), toplevel(toplevel), json(json), structure(structure) {
128 CHECK_NULL(refMap);
129 CHECK_NULL(typeMap);
130 CHECK_NULL(toplevel);
131 CHECK_NULL(json);
132 CHECK_NULL(structure);
133 }
134
135 void postorder(UNUSED const IR::P4Program *program) override {
136 cstring scalarsName = "scalars";
137 // This visitor is used in multiple passes to convert expression to json
138 auto conv = new PsaSwitchExpressionConverter(refMap, typeMap, structure, scalarsName);
139 auto ctxt = new ConversionContext(refMap, typeMap, toplevel, structure, conv, json);
140 structure->create(ctxt);
141 }
142};
143
144class PsaSwitchBackend : public Backend {
145 BMV2Options &options;
146
147 public:
148 void convert(const IR::ToplevelBlock *tlb) override;
149 PsaSwitchBackend(BMV2Options &options, P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
150 P4::ConvertEnums::EnumMapping *enumMap)
151 : Backend(options, refMap, typeMap, enumMap), options(options) {}
152};
153
154EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Hash)
155EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Checksum)
156EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(InternetChecksum)
157EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Counter)
158EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(DirectCounter)
159EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Meter)
160EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(DirectMeter)
161EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Register)
162EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Random)
163EXTERN_CONVERTER_W_INSTANCE(ActionProfile)
164EXTERN_CONVERTER_W_INSTANCE(ActionSelector)
165EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Digest)
166
167} // namespace BMV2
168
169#endif /* BACKENDS_BMV2_PSA_SWITCH_PSASWITCH_H_ */
Definition options.h:26
Definition backend.h:59
Definition psaSwitch.h:116
Definition expression.h:53
Definition JsonObjects.h:27
Definition programStructure.h:40
Definition psaSwitch.h:97
Definition psaProgramStructure.h:29
static bool isStandardMetadata(cstring ptName)
Definition psaProgramStructure.h:93
static bool isCounterMetadata(cstring ptName)
Definition psaProgramStructure.h:87
Definition psaSwitch.h:144
Definition psaSwitch.h:46
Definition sharedActionSelectorCheck.h:40
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:42
Definition json.h:40
Definition json.h:158
Definition cstring.h:72
Definition helpers.h:261