P4C
The P4 Compiler
Loading...
Searching...
No Matches
simpleSwitch.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_SIMPLE_SWITCH_SIMPLESWITCH_H_
18#define BACKENDS_BMV2_SIMPLE_SWITCH_SIMPLESWITCH_H_
19
20#include <algorithm>
21#include <cstring>
22
23#include "backends/bmv2/common/action.h"
24#include "backends/bmv2/common/backend.h"
25#include "backends/bmv2/common/control.h"
26#include "backends/bmv2/common/deparser.h"
27#include "backends/bmv2/common/extern.h"
28#include "backends/bmv2/common/globals.h"
29#include "backends/bmv2/common/header.h"
30#include "backends/bmv2/common/options.h"
31#include "backends/bmv2/common/parser.h"
32#include "backends/bmv2/common/sharedActionSelectorCheck.h"
33#include "backends/common/programStructure.h"
34#include "frontends/common/constantFolding.h"
35#include "frontends/p4-14/fromv1.0/v1model.h"
36#include "frontends/p4/evaluator/evaluator.h"
37#include "frontends/p4/simplify.h"
38#include "frontends/p4/unusedDeclarations.h"
39#include "midend/convertEnums.h"
40
41namespace P4::BMV2 {
42
44 public:
45 std::set<cstring> pipeline_controls;
46 std::set<cstring> non_pipeline_controls;
47
48 const IR::P4Parser *parser = nullptr;
49 const IR::P4Control *ingress = nullptr;
50 const IR::P4Control *egress = nullptr;
51 const IR::P4Control *compute_checksum = nullptr;
52 const IR::P4Control *verify_checksum = nullptr;
53 const IR::P4Control *deparser = nullptr;
54
56 BlockConverted blockKind(const IR::Node *node) const {
57 if (node == parser)
58 return BlockConverted::Parser;
59 else if (node == ingress)
60 return BlockConverted::Ingress;
61 else if (node == egress)
62 return BlockConverted::Egress;
63 else if (node == compute_checksum)
64 return BlockConverted::ChecksumCompute;
65 else if (node == verify_checksum)
66 return BlockConverted::ChecksumVerify;
67 else if (node == deparser)
68 return BlockConverted::Deparser;
69 return BlockConverted::None;
70 }
71};
72
74 V1ProgramStructure *structure;
75
76 public:
78 V1ProgramStructure *structure, cstring scalarsName)
79 : ExpressionConverter(refMap, typeMap, structure, scalarsName), structure(structure) {}
80
81 void modelError(const char *format, const IR::Node *node) {
82 ::P4::errorWithSuffix(ErrorType::ERR_MODEL, format,
83 "\nAre you using an up-to-date v1model.p4?", node);
84 }
85
86 bool isStandardMetadataParameter(const IR::Parameter *param) {
87 auto params = structure->parser->getApplyParameters();
88 if (params->size() != 4) {
89 modelError("%1%: Expected 4 parameter for parser", structure->parser);
90 return false;
91 }
92 if (params->parameters.at(3) == param) return true;
93
94 params = structure->ingress->getApplyParameters();
95 if (params->size() != 3) {
96 modelError("%1%: Expected 3 parameter for ingress", structure->ingress);
97 return false;
98 }
99 if (params->parameters.at(2) == param) return true;
100
101 params = structure->egress->getApplyParameters();
102 if (params->size() != 3) {
103 modelError("%1%: Expected 3 parameter for egress", structure->egress);
104 return false;
105 }
106 if (params->parameters.at(2) == param) return true;
107
108 return false;
109 }
110
111 Util::IJson *convertParam(const IR::Parameter *param, cstring fieldName) override {
112 if (isStandardMetadataParameter(param)) {
113 auto result = new Util::JsonObject();
114 if (fieldName != "") {
115 result->emplace("type", "field");
116 auto e = BMV2::mkArrayField(result, "value"_cs);
117 e->append("standard_metadata");
118 e->append(fieldName);
119 } else {
120 result->emplace("type", "header");
121 result->emplace("value", "standard_metadata");
122 }
123 return result;
124 }
125 return nullptr;
126 }
127};
128
130 V1ProgramStructure *structure;
131 P4V1::V1Model &v1model;
132
133 public:
134 explicit ParseV1Architecture(V1ProgramStructure *structure)
135 : structure(structure), v1model(P4V1::V1Model::instance) {}
136 void modelError(const char *format, const IR::Node *node);
137 bool preorder(const IR::PackageBlock *block) override;
138};
139
141 BMV2Options &options;
142 P4V1::V1Model &v1model;
143 V1ProgramStructure *structure = nullptr;
144 ExpressionConverter *conv = nullptr;
145
146 protected:
147 void createRecirculateFieldsList(ConversionContext *ctxt, const IR::ToplevelBlock *tlb,
148 cstring scalarName);
149 cstring createCalculation(cstring algo, const IR::Expression *fields,
150 Util::JsonArray *calculations, bool usePayload, const IR::Node *node);
151
152 public:
153 void modelError(const char *format, const IR::Node *place) const;
154 void convertChecksum(const IR::BlockStatement *body, Util::JsonArray *checksums,
155 Util::JsonArray *calculations, bool verify);
156 void createActions(ConversionContext *ctxt, V1ProgramStructure *structure);
157
158 void convert(const IR::ToplevelBlock *tlb) override;
160 P4::ConvertEnums::EnumMapping *enumMap)
161 : Backend(options, refMap, typeMap, enumMap),
162 options(options),
163 v1model(P4V1::V1Model::instance) {}
164};
165
166EXTERN_CONVERTER_W_FUNCTION(clone)
167EXTERN_CONVERTER_W_FUNCTION(clone_preserving_field_list)
168EXTERN_CONVERTER_W_FUNCTION_AND_MODEL(hash, P4V1::V1Model, v1model)
169EXTERN_CONVERTER_W_FUNCTION(digest)
170EXTERN_CONVERTER_W_FUNCTION(resubmit_preserving_field_list)
171EXTERN_CONVERTER_W_FUNCTION(recirculate_preserving_field_list)
172EXTERN_CONVERTER_W_FUNCTION(mark_to_drop)
173EXTERN_CONVERTER_W_FUNCTION(log_msg)
174EXTERN_CONVERTER_W_FUNCTION_AND_MODEL(random, P4V1::V1Model, v1model)
175EXTERN_CONVERTER_W_FUNCTION_AND_MODEL(truncate, P4V1::V1Model, v1model)
176EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE_AND_MODEL(register, P4V1::V1Model, v1model)
177EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE_AND_MODEL(counter, P4V1::V1Model, v1model)
178EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE_AND_MODEL(meter, P4V1::V1Model, v1model)
179EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(direct_counter)
180EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE_AND_MODEL(direct_meter, P4V1::V1Model, v1model)
181EXTERN_CONVERTER_W_INSTANCE_AND_MODEL(action_profile, P4V1::V1Model, v1model)
182EXTERN_CONVERTER_W_INSTANCE_AND_MODEL(action_selector, P4V1::V1Model, v1model)
183
184} // namespace P4::BMV2
185
186#endif /* BACKENDS_BMV2_SIMPLE_SWITCH_SIMPLESWITCH_H_ */
Definition backends/bmv2/common/options.h:26
Backend is a the base class for SimpleSwitchBackend and PortableSwitchBackend.
Definition bmv2/common/backend.h:60
Definition expression.h:51
Definition simpleSwitch.h:129
Definition simpleSwitch.h:140
void convert(const IR::ToplevelBlock *tlb) override
Definition simpleSwitch.cpp:1081
Definition simpleSwitch.h:73
Util::IJson * convertParam(const IR::Parameter *param, cstring fieldName) override
Definition simpleSwitch.h:111
Definition simpleSwitch.h:43
Definition node.h:94
Definition visitor.h:400
Definition frontends/p4-14/fromv1.0/v1model.h:262
Definition backends/common/programStructure.h:32
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition json.h:40
Definition json.h:115
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 errorWithSuffix(const int kind, const char *format, const char *suffix, const T *node, Args &&...args)
This is similar to the above method, but also has a suffix.
Definition lib/error.h:69
Definition bmv2/common/helpers.h:297