17#ifndef BACKENDS_BMV2_SIMPLE_SWITCH_SIMPLESWITCH_H_
18#define BACKENDS_BMV2_SIMPLE_SWITCH_SIMPLESWITCH_H_
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"
45 std::set<cstring> pipeline_controls;
46 std::set<cstring> non_pipeline_controls;
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;
56 BlockConverted blockKind(
const IR::Node *node)
const {
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;
81 void modelError(
const char *format,
const IR::Node *node) {
83 "\nAre you using an up-to-date v1model.p4?", node);
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);
92 if (params->parameters.at(3) == param)
return true;
94 params = structure->ingress->getApplyParameters();
95 if (params->size() != 3) {
96 modelError(
"%1%: Expected 3 parameter for ingress", structure->ingress);
99 if (params->parameters.at(2) == param)
return true;
101 params = structure->egress->getApplyParameters();
102 if (params->size() != 3) {
103 modelError(
"%1%: Expected 3 parameter for egress", structure->egress);
106 if (params->parameters.at(2) == param)
return true;
112 if (isStandardMetadataParameter(param)) {
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);
120 result->emplace(
"type",
"header");
121 result->emplace(
"value",
"standard_metadata");
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;
147 void createRecirculateFieldsList(
ConversionContext *ctxt,
const IR::ToplevelBlock *tlb,
149 cstring createCalculation(
cstring algo,
const IR::Expression *fields,
153 void modelError(
const char *format,
const IR::Node *place)
const;
154 void convertChecksum(
const IR::BlockStatement *body,
Util::JsonArray *checksums,
158 void convert(
const IR::ToplevelBlock *tlb)
override;
160 P4::ConvertEnums::EnumMapping *enumMap)
161 :
Backend(options, refMap, typeMap, enumMap),
163 v1model(P4V1::V1Model::instance) {}
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)
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 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
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