17#ifndef BACKENDS_BMV2_COMMON_BACKEND_H_
18#define BACKENDS_BMV2_COMMON_BACKEND_H_
20#include "JsonObjects.h"
21#include "controlFlowGraph.h"
22#include "expression.h"
23#include "frontends/common/model.h"
24#include "frontends/p4/coreLibrary.h"
26#include "ir/annotations.h"
28#include "lib/cstring.h"
30#include "lib/exceptions.h"
34#include "lib/nullstream.h"
35#include "midend/actionSynthesis.h"
36#include "midend/convertEnums.h"
37#include "midend/removeComplexExpressions.h"
38#include "midend/removeLeftSlices.h"
40#include "sharedActionSelectorCheck.h"
44enum gress_t { INGRESS, EGRESS };
57class ExpressionConverter;
65 P4::ConvertEnums::EnumMapping *enumMap;
68 const IR::ToplevelBlock *toplevel =
nullptr;
72 P4::ConvertEnums::EnumMapping *enumMap)
77 corelib(P4::P4CoreLibrary::instance()),
81 void serialize(std::ostream &out)
const { json->toplevel->serialize(out); }
82 virtual void convert(
const IR::ToplevelBlock *block) = 0;
92 const std::set<cstring> *skip;
95 explicit SkipControls(
const std::set<cstring> *skip) : skip(skip) { CHECK_NULL(skip); }
97 if (skip->find(control->name) != skip->end())
return false;
108 const std::set<cstring> *process;
111 explicit ProcessControls(
const std::set<cstring> *process) : process(process) {
114 bool convert(
const IR::P4Control *control)
const {
115 if (process->find(control->name) != process->end())
return true;
126 const IR::Type_Struct *userMetaType;
130 bool renamed =
false;
135 : refMap(refMap), userMetaType(userMetaType), namePrefix(namePrefix) {
136 setName(
"RenameUserMetadata");
138 visitDagOnce =
false;
141 const IR::Node *postorder(IR::Type_Struct *type)
override {
143 auto orig = getOriginal<IR::Type_Struct>();
144 if (userMetaType->name != orig->name)
return type;
147 LOG2(
"Creating clone of " << orig);
149 auto clone = type->clone();
150 clone->name = namePrefix;
151 vec->push_back(clone);
155 for (
auto f : type->fields) {
156 auto anno = f->getAnnotation(IR::Annotation::nameAnnotation);
157 cstring suffix = cstring::empty;
158 if (anno !=
nullptr) suffix = anno->getName();
159 if (suffix.startsWith(
".")) {
162 fields.push_back(f->clone());
166 if (!suffix.isNullOrEmpty())
167 suffix =
"."_cs + suffix;
169 suffix =
"."_cs + f->name;
170 cstring newName = namePrefix + suffix;
171 LOG2(
"Renaming " << f <<
" to " << newName);
172 auto field =
new IR::StructField(
173 f->srcInfo, f->name, IR::Annotations::setNameAnnotation(f->annotations, newName),
175 fields.push_back(field);
179 new IR::Type_Struct(type->srcInfo, type->name, type->annotations, std::move(fields));
180 vec->push_back(annotated);
184 const IR::Node *preorder(IR::Type_Name *type)
override {
187 if (decl == userMetaType)
188 type->path =
new IR::Path(type->path->srcInfo,
IR::ID(type->path->srcInfo, namePrefix));
189 LOG2(
"Replacing reference with " << type);
193 void end_apply(
const IR::Node *)
override {
194 BUG_CHECK(renamed,
"Could not identify user metadata type declaration %1%", userMetaType);
Definition actionSynthesis.h:30
Definition backends/bmv2/common/options.h:26
Backend is a the base class for SimpleSwitchBackend and PortableSwitchBackend.
Definition bmv2/common/backend.h:60
Definition JsonObjects.h:27
Definition bmv2/common/backend.h:107
bool convert(const IR::P4Control *control) const
Definition bmv2/common/backend.h:114
This class implements a policy suitable for the SynthesizeActions pass.
Definition bmv2/common/backend.h:90
bool convert(const Visitor::Context *, const IR::P4Control *control) override
Definition bmv2/common/backend.h:96
Definition coreLibrary.h:103
bool isv1() const
True if we are compiling a P4 v1.0 or v1.1 program.
Definition parser_options.cpp:473
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
const IR::IDeclaration * getDeclaration(const IR::Path *path, bool notNull=false) const override
Definition referenceMap.cpp:78
void setIsV1(bool isv1)
Set boolean indicating whether map is for a P4_14 program to isV1.
Definition referenceMap.h:105
Definition removeComplexExpressions.h:30
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:21