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/common/programStructure.h"
29#include "backends/common/psaProgramStructure.h"
30#include "frontends/common/constantFolding.h"
31#include "frontends/common/resolveReferences/referenceMap.h"
32#include "frontends/p4/coreLibrary.h"
33#include "frontends/p4/enumInstance.h"
34#include "frontends/p4/evaluator/evaluator.h"
35#include "frontends/p4/methodInstance.h"
36#include "frontends/p4/simplify.h"
37#include "frontends/p4/strengthReduction.h"
38#include "frontends/p4/typeMap.h"
39#include "frontends/p4/unusedDeclarations.h"
40#include "ir/ir.h"
41#include "lib/big_int_util.h"
42#include "lib/json.h"
43
44namespace BMV2 {
45
47 public:
49 P4::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 (P4::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;
81 ptName)) { // check if its psa metadata
82 auto jsn = new Util::JsonObject();
83
84 // encode the metadata type and field in json
85 jsn->emplace("type", "field");
86 auto a = mkArrayField(jsn, "value"_cs);
87 a->append(ptName.exceptLast(2));
88 a->append(fieldName);
89 return jsn;
90 } else {
91 // not a special type
92 return nullptr;
93 }
94 return nullptr;
95 }
96};
97
99 public:
101 : P4::PsaProgramStructure(refMap, typeMap) {}
102
103 void create(ConversionContext *ctxt);
104 void createStructLike(ConversionContext *ctxt, const IR::Type_StructLike *st);
105 void createTypes(ConversionContext *ctxt);
106 void createHeaders(ConversionContext *ctxt);
107 void createScalars(ConversionContext *ctxt);
108 void createParsers(ConversionContext *ctxt);
109 void createExterns();
110 void createActions(ConversionContext *ctxt);
111 void createControls(ConversionContext *ctxt);
112 void createDeparsers(ConversionContext *ctxt);
113 void createGlobals();
114 cstring convertHashAlgorithm(cstring algo);
115};
116
118 public:
119 P4::ReferenceMap *refMap;
120 P4::TypeMap *typeMap;
121 const IR::ToplevelBlock *toplevel;
122 JsonObjects *json;
123 PsaCodeGenerator *structure;
124
126 const IR::ToplevelBlock *toplevel, JsonObjects *json,
127 PsaCodeGenerator *structure)
128 : refMap(refMap), typeMap(typeMap), toplevel(toplevel), json(json), structure(structure) {
129 CHECK_NULL(refMap);
130 CHECK_NULL(typeMap);
131 CHECK_NULL(toplevel);
132 CHECK_NULL(json);
133 CHECK_NULL(structure);
134 }
135
136 void postorder(UNUSED const IR::P4Program *program) override {
137 cstring scalarsName = "scalars"_cs;
138 // This visitor is used in multiple passes to convert expression to json
139 auto conv = new PsaSwitchExpressionConverter(refMap, typeMap, structure, scalarsName);
140 auto ctxt = new ConversionContext(refMap, typeMap, toplevel, structure, conv, json);
141 structure->create(ctxt);
142 }
143};
144
145class PsaSwitchBackend : public Backend {
146 BMV2Options &options;
147
148 public:
149 void convert(const IR::ToplevelBlock *tlb) override;
150 PsaSwitchBackend(BMV2Options &options, P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
151 P4::ConvertEnums::EnumMapping *enumMap)
152 : Backend(options, refMap, typeMap, enumMap), options(options) {}
153};
154
155EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Hash)
156EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Checksum)
157EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(InternetChecksum)
158EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Counter)
159EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(DirectCounter)
160EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Meter)
161EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(DirectMeter)
162EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Register)
163EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Random)
164EXTERN_CONVERTER_W_INSTANCE(ActionProfile)
165EXTERN_CONVERTER_W_INSTANCE(ActionSelector)
166EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(Digest)
167
168} // namespace BMV2
169
170#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:117
Definition expression.h:51
Definition JsonObjects.h:27
Definition psaSwitch.h:98
Definition psaSwitch.h:145
Definition psaSwitch.h:46
Definition visitor.h:396
Definition backends/common/programStructure.h:32
Definition common/psaProgramStructure.h:36
static bool isCounterMetadata(cstring ptName)
Definition common/psaProgramStructure.h:92
static bool isStandardMetadata(cstring ptName)
Definition common/psaProgramStructure.h:96
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:80
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:21
Definition helpers.h:274