P4C
The P4 Compiler
Loading...
Searching...
No Matches
bmv2/common/backend.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_COMMON_BACKEND_H_
18#define BACKENDS_BMV2_COMMON_BACKEND_H_
19
20#include "JsonObjects.h"
21#include "controlFlowGraph.h"
22#include "expression.h"
23#include "frontends/common/model.h"
24#include "frontends/p4/coreLibrary.h"
25#include "helpers.h"
26#include "ir/ir.h"
27#include "lib/cstring.h"
28#include "lib/error.h"
29#include "lib/exceptions.h"
30#include "lib/gc.h"
31#include "lib/json.h"
32#include "lib/log.h"
33#include "lib/nullstream.h"
34#include "midend/actionSynthesis.h"
35#include "midend/convertEnums.h"
36#include "midend/removeComplexExpressions.h"
37#include "midend/removeLeftSlices.h"
38#include "options.h"
39#include "sharedActionSelectorCheck.h"
40
41namespace P4::BMV2 {
42
43enum gress_t { INGRESS, EGRESS };
44enum block_t {
45 PARSER,
46 PIPELINE,
47 DEPARSER,
48 V1_PARSER,
49 V1_DEPARSER,
50 V1_INGRESS,
51 V1_EGRESS,
52 V1_VERIFY,
53 V1_COMPUTE
54};
55
56class ExpressionConverter;
57
59class Backend {
60 public:
61 BMV2Options &options;
62 P4::ReferenceMap *refMap;
63 P4::TypeMap *typeMap;
64 P4::ConvertEnums::EnumMapping *enumMap;
65 P4::P4CoreLibrary &corelib;
67 const IR::ToplevelBlock *toplevel = nullptr;
68
69 public:
70 Backend(BMV2Options &options, P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
71 P4::ConvertEnums::EnumMapping *enumMap)
72 : options(options),
73 refMap(refMap),
74 typeMap(typeMap),
75 enumMap(enumMap),
76 corelib(P4::P4CoreLibrary::instance()),
77 json(new BMV2::JsonObjects()) {
78 refMap->setIsV1(options.isv1());
79 }
80 void serialize(std::ostream &out) const { json->toplevel->serialize(out); }
81 virtual void convert(const IR::ToplevelBlock *block) = 0;
82};
83
85// The policy is: do not synthesize actions for the controls whose names
91 const std::set<cstring> *skip;
92
93 public:
94 explicit SkipControls(const std::set<cstring> *skip) : skip(skip) { CHECK_NULL(skip); }
95 bool convert(const Visitor::Context *, const IR::P4Control *control) override {
96 if (skip->find(control->name) != skip->end()) return false;
97 return true;
98 }
99};
100
107 const std::set<cstring> *process;
108
109 public:
110 explicit ProcessControls(const std::set<cstring> *process) : process(process) {
111 CHECK_NULL(process);
112 }
113 bool convert(const IR::P4Control *control) const {
114 if (process->find(control->name) != process->end()) return true;
115 return false;
116 }
117};
118
124 P4::ReferenceMap *refMap;
125 const IR::Type_Struct *userMetaType;
128 cstring namePrefix;
129 bool renamed = false;
130
131 public:
132 RenameUserMetadata(P4::ReferenceMap *refMap, const IR::Type_Struct *userMetaType,
133 cstring namePrefix)
134 : refMap(refMap), userMetaType(userMetaType), namePrefix(namePrefix) {
135 setName("RenameUserMetadata");
136 CHECK_NULL(refMap);
137 visitDagOnce = false;
138 }
139
140 const IR::Node *postorder(IR::Type_Struct *type) override {
141 // Clone the user metadata type
142 auto orig = getOriginal<IR::Type_Struct>();
143 if (userMetaType->name != orig->name) return type;
144
145 auto vec = new IR::IndexedVector<IR::Node>();
146 LOG2("Creating clone of " << orig);
147 renamed = true;
148 auto clone = type->clone();
149 clone->name = namePrefix;
150 vec->push_back(clone);
151
152 // Rename all fields
154 for (auto f : type->fields) {
155 auto anno = f->getAnnotation(IR::Annotation::nameAnnotation);
156 cstring suffix = cstring::empty;
157 if (anno != nullptr) suffix = anno->getName();
158 if (suffix.startsWith(".")) {
159 // We can't change the name of this field.
160 // Hopefully the user knows what they are doing.
161 fields.push_back(f->clone());
162 continue;
163 }
164
165 if (!suffix.isNullOrEmpty())
166 suffix = "."_cs + suffix;
167 else
168 suffix = "."_cs + f->name;
169 cstring newName = namePrefix + suffix;
170 auto stringLit = new IR::StringLiteral(newName);
171 LOG2("Renaming " << f << " to " << newName);
172 auto annos = f->annotations->addOrReplace(IR::Annotation::nameAnnotation, stringLit);
173 auto field = new IR::StructField(f->srcInfo, f->name, annos, f->type);
174 fields.push_back(field);
175 }
176
177 auto annotated =
178 new IR::Type_Struct(type->srcInfo, type->name, type->annotations, std::move(fields));
179 vec->push_back(annotated);
180 return vec;
181 }
182
183 const IR::Node *preorder(IR::Type_Name *type) override {
184 // Find any reference to the user metadata type that is used and replace them
185 auto decl = refMap->getDeclaration(type->path);
186 if (decl == userMetaType)
187 type->path = new IR::Path(type->path->srcInfo, IR::ID(type->path->srcInfo, namePrefix));
188 LOG2("Replacing reference with " << type);
189 return type;
190 }
191
192 void end_apply(const IR::Node *) override {
193 BUG_CHECK(renamed, "Could not identify user metadata type declaration %1%", userMetaType);
194 }
195};
196
197} // namespace P4::BMV2
198
199#endif /* BACKENDS_BMV2_COMMON_BACKEND_H_ */
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:59
Definition JsonObjects.h:27
Definition bmv2/common/backend.h:106
bool convert(const IR::P4Control *control) const
Definition bmv2/common/backend.h:113
Definition bmv2/common/backend.h:123
This class implements a policy suitable for the SynthesizeActions pass.
Definition bmv2/common/backend.h:89
bool convert(const Visitor::Context *, const IR::P4Control *control) override
Definition bmv2/common/backend.h:95
Definition node.h:52
Definition node.h:95
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
Definition visitor.h:424
Definition typeMap.h:41
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:21
Definition id.h:28
Definition visitor.h:47