P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
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/error.h"
28#include "lib/exceptions.h"
29#include "lib/gc.h"
30#include "lib/json.h"
31#include "lib/log.h"
32#include "lib/nullstream.h"
33#include "metermap.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 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
58// Backend is a the base class for SimpleSwitchBackend and PortableSwitchBackend.
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
92 // set of controls where actions are not synthesized
93 const std::set<cstring> *skip;
94
95 public:
96 explicit SkipControls(const std::set<cstring> *skip) : skip(skip) { CHECK_NULL(skip); }
97 bool convert(const Visitor::Context *, const IR::P4Control *control) override {
98 if (skip->find(control->name) != skip->end()) return false;
99 return true;
100 }
101};
102
111 const std::set<cstring> *process;
112
113 public:
114 explicit ProcessControls(const std::set<cstring> *process) : process(process) {
115 CHECK_NULL(process);
116 }
117 bool convert(const IR::P4Control *control) const {
118 if (process->find(control->name) != process->end()) return true;
119 return false;
120 }
121};
122
129class RenameUserMetadata : public Transform {
130 P4::ReferenceMap *refMap;
131 const IR::Type_Struct *userMetaType;
132 // Used as a prefix for the fields of the userMetadata structure
133 // and also as a name for the userMetadata type clone.
134 cstring namePrefix;
135 bool renamed = false;
136
137 public:
138 RenameUserMetadata(P4::ReferenceMap *refMap, const IR::Type_Struct *userMetaType,
139 cstring namePrefix)
140 : refMap(refMap), userMetaType(userMetaType), namePrefix(namePrefix) {
141 setName("RenameUserMetadata");
142 CHECK_NULL(refMap);
143 visitDagOnce = false;
144 }
145
146 const IR::Node *postorder(IR::Type_Struct *type) override {
147 // Clone the user metadata type
148 auto orig = getOriginal<IR::Type_Struct>();
149 if (userMetaType->name != orig->name) return type;
150
151 auto vec = new IR::IndexedVector<IR::Node>();
152 LOG2("Creating clone of " << orig);
153 renamed = true;
154 auto clone = type->clone();
155 clone->name = namePrefix;
156 vec->push_back(clone);
157
158 // Rename all fields
159 IR::IndexedVector<IR::StructField> fields;
160 for (auto f : type->fields) {
161 auto anno = f->getAnnotation(IR::Annotation::nameAnnotation);
162 cstring suffix = "";
163 if (anno != nullptr) suffix = anno->getName();
164 if (suffix.startsWith(".")) {
165 // We can't change the name of this field.
166 // Hopefully the user knows what they are doing.
167 fields.push_back(f->clone());
168 continue;
169 }
170
171 if (!suffix.isNullOrEmpty())
172 suffix = cstring(".") + suffix;
173 else
174 suffix = cstring(".") + f->name;
175 cstring newName = namePrefix + suffix;
176 auto stringLit = new IR::StringLiteral(newName);
177 LOG2("Renaming " << f << " to " << newName);
178 auto annos = f->annotations->addOrReplace(IR::Annotation::nameAnnotation, stringLit);
179 auto field = new IR::StructField(f->srcInfo, f->name, annos, f->type);
180 fields.push_back(field);
181 }
182
183 auto annotated =
184 new IR::Type_Struct(type->srcInfo, type->name, type->annotations, std::move(fields));
185 vec->push_back(annotated);
186 return vec;
187 }
188
189 const IR::Node *preorder(IR::Type_Name *type) override {
190 // Find any reference to the user metadata type that is used and replace them
191 auto decl = refMap->getDeclaration(type->path);
192 if (decl == userMetaType)
193 type->path = new IR::Path(type->path->srcInfo, IR::ID(type->path->srcInfo, namePrefix));
194 LOG2("Replacing reference with " << type);
195 return type;
196 }
197
198 void end_apply(const IR::Node *) override {
199 BUG_CHECK(renamed, "Could not identify user metadata type declaration %1%", userMetaType);
200 }
201};
202
203} // namespace BMV2
204
205#endif /* BACKENDS_BMV2_COMMON_BACKEND_H_ */
Definition options.h:26
Definition backend.h:59
Definition JsonObjects.h:27
Definition backend.h:110
bool convert(const IR::P4Control *control) const
Definition backend.h:117
Definition backend.h:129
Definition backend.h:91
bool convert(const Visitor::Context *, const IR::P4Control *control) override
Definition backend.h:97
Definition actionSynthesis.h:30
Definition coreLibrary.h:100
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 typeMap.h:42
Definition cstring.h:72