P4C
The P4 Compiler
Loading...
Searching...
No Matches
frontends/p4-14/fromv1.0/programStructure.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 FRONTENDS_P4_14_FROMV1_0_PROGRAMSTRUCTURE_H_
18#define FRONTENDS_P4_14_FROMV1_0_PROGRAMSTRUCTURE_H_
19
20#include <set>
21#include <vector>
22
23#include "frontends/p4/callGraph.h"
24#include "frontends/p4/coreLibrary.h"
25#include "ir/ir.h"
26#include "lib/cstring.h"
27#include "lib/map.h"
28#include "v1model.h"
29
30namespace P4::P4V1 {
31
33 public:
34 const IR::Expression *header = nullptr;
35 const IR::Expression *userMetadata = nullptr;
36 const IR::Expression *standardMetadata = nullptr;
37 virtual void clear() {
38 header = nullptr;
39 userMetadata = nullptr;
40 standardMetadata = nullptr;
41 }
42};
43
46 // In P4-14 one can have multiple objects with different types with the same name
47 // In P4-16 this is not possible, so we may need to rename some objects.
48 // We will preserve the original name using an @name("") annotation.
49 template <typename T>
50 class NamedObjectInfo {
51 // If allNames is nullptr we don't check for duplicate names.
52 // Key is a name, value represents how many times this name was used as a base
53 // for newly generated unique names.
54 std::unordered_map<cstring, int> *allNames;
55 std::map<cstring, T> nameToObject;
56 std::map<T, cstring> objectToNewName;
57
58 // Iterate in order of name, but return pair<T, newname>
59 class iterator {
60 friend class NamedObjectInfo;
61
62 private:
63 typename std::map<cstring, T>::iterator it;
64 typename std::map<T, cstring> &objToName;
65 iterator(typename std::map<cstring, T>::iterator it,
66 typename std::map<T, cstring> &objToName)
67 : it(it), objToName(objToName) {}
68
69 public:
70 const iterator &operator++() {
71 ++it;
72 return *this;
73 }
74 bool operator!=(const iterator &other) const { return it != other.it; }
75 std::pair<T, cstring> operator*() const {
76 return std::pair<T, cstring>(it->second, objToName[it->second]);
77 }
78 };
79
80 public:
81 explicit NamedObjectInfo(std::unordered_map<cstring, int> *allNames) : allNames(allNames) {}
82 void emplace(T obj) {
83 if (objectToNewName.find(obj) != objectToNewName.end()) {
84 // Already done
85 LOG3(" already emplaced obj " << obj);
86 return;
87 }
88
89 nameToObject.emplace(obj->name, obj);
90 cstring newName;
91
92 if (allNames == nullptr || (allNames->find(obj->name) == allNames->end())) {
93 newName = obj->name;
94 } else {
95 newName = cstring::make_unique(*allNames, obj->name, allNames->at(obj->name), '_');
96 }
97 if (allNames != nullptr) allNames->insert({newName, 0});
98 LOG3("Discovered " << obj << " named " << newName);
99 objectToNewName.emplace(obj, newName);
100 }
102 T get(cstring name) const { return ::P4::get(nameToObject, name); }
104 cstring get(T object) const {
105 return ::P4::get(objectToNewName, object, object->name.name);
106 }
108 cstring newname(cstring name) const { return get(get(name)); }
109 bool contains(cstring name) const { return nameToObject.find(name) != nameToObject.end(); }
110 iterator begin() { return iterator(nameToObject.begin(), objectToNewName); }
111 iterator end() { return iterator(nameToObject.end(), objectToNewName); }
112 void erase(cstring name) {
113 allNames->erase(name);
114 auto obj = get(name);
115 objectToNewName.erase(obj);
116 nameToObject.erase(name);
117 }
118 };
119
120 std::set<cstring> included_files;
121
122 public:
124
125 P4V1::V1Model &v1model;
126 P4::P4CoreLibrary &p4lib;
127
128 std::unordered_map<cstring, int> allNames;
129 NamedObjectInfo<const IR::Type_StructLike *> types;
130 NamedObjectInfo<const IR::HeaderOrMetadata *> metadata;
131 NamedObjectInfo<const IR::Header *> headers;
132 NamedObjectInfo<const IR::HeaderStack *> stacks;
133 NamedObjectInfo<const IR::V1Control *> controls;
134 NamedObjectInfo<const IR::V1Parser *> parserStates;
135 NamedObjectInfo<const IR::V1Table *> tables;
136 NamedObjectInfo<const IR::ActionFunction *> actions;
137 NamedObjectInfo<const IR::Counter *> counters;
138 NamedObjectInfo<const IR::Register *> registers;
139 NamedObjectInfo<const IR::Meter *> meters;
140 NamedObjectInfo<const IR::ActionProfile *> action_profiles;
141 NamedObjectInfo<const IR::FieldList *> field_lists;
142 NamedObjectInfo<const IR::FieldListCalculation *> field_list_calculations;
143 NamedObjectInfo<const IR::ActionSelector *> action_selectors;
144 NamedObjectInfo<const IR::Type_Extern *> extern_types;
145 std::map<const IR::Type_Extern *, const IR::Type_Extern *> extern_remap;
146 NamedObjectInfo<const IR::Declaration_Instance *> externs;
147 NamedObjectInfo<const IR::ParserValueSet *> value_sets;
148 std::set<cstring> value_sets_implemented;
149 std::vector<const IR::CalculatedField *> calculated_fields;
150 std::map<const IR::Node *, const IR::Declaration_Instance *> globalInstances;
151 P4::CallGraph<cstring> calledActions;
152 P4::CallGraph<cstring> calledControls;
153 P4::CallGraph<cstring> calledCounters;
154 P4::CallGraph<cstring> calledMeters;
155 P4::CallGraph<cstring> calledRegisters;
156 P4::CallGraph<cstring> calledExterns;
158 std::map<cstring, IR::Vector<IR::Expression>> extracts; // for each parser
159 std::map<cstring, cstring> directCounters;
161 std::map<cstring, const IR::Meter *> directMeters;
162 std::map<const IR::Meter *, const IR::Declaration_Instance *> meterMap;
163 std::map<cstring, const IR::Declaration_Instance *> counterMap;
166
167 std::map<const IR::V1Table *, const IR::V1Control *> tableMapping;
168 std::map<const IR::V1Table *, const IR::Apply *> tableInvocation;
173 std::map<cstring, const IR::Type *> finalHeaderType;
176 std::map<cstring, cstring> registerLayoutType;
177
181 std::map<const IR::MethodCallExpression *, const IR::Type_Header *> extractsSynthesized;
182
183 std::map<cstring, const IR::ParserState *> parserEntryPoints;
186
187 // P4-14 struct/header type can be converted to three types
188 // of struct/header in P4-16.
189 // 1) as part of the 'hdr' struct
190 // 2) as part of the 'meta' struct
191 // 3) as the parameters of a parser/control block
192 // In case 1 and 2, the converter needs to fix the path
193 // by prepending 'hdr.' or 'meta.' to the ConcreteHeaderRef.
194 // In case 3. the converter only needs to convert headerRef to PathExpression
195 std::set<cstring> headerTypes;
196 std::set<cstring> metadataTypes;
197 std::set<cstring> parameterTypes;
198 std::set<cstring> metadataInstances;
199 std::set<cstring> headerInstances;
200
202 std::vector<const IR::Declaration *> localInstances;
203
204 ConversionContext *conversionContext = nullptr;
205
206 IR::Vector<IR::Type> *emptyTypeArguments = nullptr;
207 const IR::Parameter *parserPacketIn = nullptr;
208 const IR::Parameter *parserHeadersOut = nullptr;
209
210 // output is constructed here
211 IR::Vector<IR::Node> *declarations;
212
213 protected:
214 virtual const IR::Statement *convertPrimitive(const IR::Primitive *primitive);
215 virtual void checkHeaderType(const IR::Type_StructLike *hrd, bool toStruct);
216
234 static const IR::Annotations *addNameAnnotation(cstring name,
235 const IR::Annotations *annos = nullptr);
236
242 static const IR::Annotations *addGlobalNameAnnotation(cstring name,
243 const IR::Annotations *annos = nullptr);
244
245 virtual const IR::ParserState *convertParser(const IR::V1Parser *,
247 virtual const IR::Statement *convertParserStatement(const IR::Expression *expr);
248 virtual const IR::P4Control *convertControl(const IR::V1Control *control, cstring newName);
249 virtual const IR::Declaration_Instance *convertDirectMeter(const IR::Meter *m, cstring newName);
250 virtual const IR::Declaration_Instance *convertDirectCounter(const IR::Counter *c,
251 cstring newName);
252 virtual const IR::Declaration_Instance *convert(const IR::CounterOrMeter *cm, cstring newName);
253 virtual const IR::Declaration_Instance *convertActionProfile(const IR::ActionProfile *,
254 cstring newName);
255 virtual const IR::P4Table *convertTable(const IR::V1Table *table, cstring newName,
257 std::map<cstring, cstring> &);
258 virtual const IR::P4Action *convertAction(const IR::ActionFunction *action, cstring newName,
259 const IR::Meter *meterToAccess,
260 cstring counterToAccess);
261 virtual const IR::Statement *convertMeterCall(const IR::Meter *meterToAccess);
262 virtual const IR::Statement *convertCounterCall(cstring counterToAccess);
263 virtual const IR::Type_Control *controlType(IR::ID name);
264 const IR::PathExpression *getState(IR::ID dest);
265 virtual const IR::Expression *counterType(const IR::CounterOrMeter *cm);
266 virtual void createChecksumVerifications();
267 virtual void createChecksumUpdates();
268 virtual void createStructures();
269 virtual cstring createType(const IR::Type_StructLike *type, bool header,
270 std::unordered_set<const IR::Type *> *converted);
271 virtual void createParser();
272 virtual void createControls();
273 void createDeparserInternal(IR::ID deparserId, IR::Parameter *packetOut, IR::Parameter *headers,
274 std::vector<IR::Parameter *>,
276 std::function<IR::BlockStatement *(IR::BlockStatement *)>);
277 virtual void createDeparser();
278 virtual void createMain();
279
280 public:
281 void include(cstring filename, cstring ppoptions = cstring());
284 void populateOutputNames();
285 const IR::AssignmentStatement *assign(Util::SourceInfo srcInfo, const IR::Expression *left,
286 const IR::Expression *right, const IR::Type *type);
287 virtual const IR::Expression *convertFieldList(const IR::Expression *expression);
288 virtual const IR::Expression *convertHashAlgorithm(Util::SourceInfo srcInfo, IR::ID algorithm);
289 virtual const IR::Expression *convertHashAlgorithms(const IR::NameList *algorithm);
290 virtual const IR::Declaration_Instance *convert(const IR::Register *reg, cstring newName,
291 const IR::Type *regElementType = nullptr);
292 virtual const IR::Type_Struct *createFieldListType(const IR::Expression *expression);
293 virtual const IR::FieldListCalculation *getFieldListCalculation(const IR::Expression *);
294 virtual const IR::FieldList *getFieldLists(const IR::FieldListCalculation *flc);
295 virtual const IR::Expression *paramReference(const IR::Parameter *param);
296 const IR::Statement *sliceAssign(const IR::Primitive *prim, const IR::Expression *left,
297 const IR::Expression *right, const IR::Expression *mask);
298 void tablesReferred(const IR::V1Control *control, std::vector<const IR::V1Table *> &out);
299 bool isHeader(const IR::ConcreteHeaderRef *nhr) const;
300 cstring makeUniqueName(cstring base);
301 bool isFieldInList(cstring type, cstring field, const IR::FieldList *fl) const;
304 virtual const IR::Vector<IR::Expression> *listIndexes(cstring type, cstring field) const;
307 const IR::Expression *listIndex(const IR::Expression *fl) const;
308
309 const IR::Type *explodeType(const std::vector<const IR::Type::Bits *> &fieldTypes);
310 const IR::Expression *explodeLabel(const IR::Constant *value, const IR::Constant *mask,
311 const std::vector<const IR::Type::Bits *> &fieldTypes);
312
313 virtual IR::Vector<IR::Argument> *createApplyArguments(cstring n);
314
315 const IR::V1Control *ingress;
316 IR::ID ingressReference;
317
318 const IR::P4Control *verifyChecksums;
319 const IR::P4Control *updateChecksums;
320 const IR::P4Control *deparser;
322 const IR::Expression *latest;
323 const int defaultRegisterWidth = 32;
324
325 virtual void loadModel();
326 void createExterns();
327 void createTypes();
328 virtual const IR::P4Program *create(Util::SourceInfo info);
329};
330
331} // namespace P4::P4V1
332
333#endif /* FRONTENDS_P4_14_FROMV1_0_PROGRAMSTRUCTURE_H_ */
Definition callGraph.h:41
Definition node.h:52
Definition vector.h:59
Definition coreLibrary.h:103
Definition frontends/p4-14/fromv1.0/programStructure.h:32
Information about the structure of a P4-14 program, used to convert it to a P4-16 program.
Definition frontends/p4-14/fromv1.0/programStructure.h:45
std::map< const IR::MethodCallExpression *, const IR::Type_Header * > extractsSynthesized
Definition frontends/p4-14/fromv1.0/programStructure.h:181
cstring fieldListsEnum
Name of the serializable enum that holds one id for each field list.
Definition frontends/p4-14/fromv1.0/programStructure.h:185
virtual const IR::Vector< IR::Expression > * listIndexes(cstring type, cstring field) const
Definition frontends/p4-14/fromv1.0/programStructure.cpp:154
std::map< cstring, const IR::Meter * > directMeters
Maps table name to direct meter.
Definition frontends/p4-14/fromv1.0/programStructure.h:161
virtual const IR::ParserState * convertParser(const IR::V1Parser *, IR::IndexedVector< IR::Declaration > *)
Definition frontends/p4-14/fromv1.0/programStructure.cpp:508
std::vector< const IR::Declaration * > localInstances
extra local instances to control created by primitive translation
Definition frontends/p4-14/fromv1.0/programStructure.h:202
static const IR::Annotations * addGlobalNameAnnotation(cstring name, const IR::Annotations *annos=nullptr)
Definition frontends/p4-14/fromv1.0/programStructure.cpp:90
const IR::Expression * latest
Represents 'latest' P4-14 construct.
Definition frontends/p4-14/fromv1.0/programStructure.h:322
std::map< cstring, const IR::Type * > finalHeaderType
Definition frontends/p4-14/fromv1.0/programStructure.h:173
virtual void createControls()
Definition frontends/p4-14/fromv1.0/programStructure.cpp:2329
const IR::Expression * listIndex(const IR::Expression *fl) const
Definition frontends/p4-14/fromv1.0/programStructure.cpp:165
void populateOutputNames()
Definition frontends/p4-14/fromv1.0/programStructure.cpp:2639
std::map< cstring, cstring > registerLayoutType
Definition frontends/p4-14/fromv1.0/programStructure.h:176
ordered_set< const IR::FieldList * > allFieldLists
Field lists that appear in the program.
Definition frontends/p4-14/fromv1.0/programStructure.h:165
Definition frontends/p4-14/fromv1.0/v1model.h:262
Definition source_file.h:125
Definition cstring.h:85
Definition ordered_set.h:32
Definition converters.cpp:28
void info(const int kind, const char *format, const T *node, Args &&...args)
Report info messages of type kind. Requires that the node argument have source info.
Definition lib/error.h:148
Definition id.h:28