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
223 cstring name, const IR::Vector<IR::Annotation> &annos);
224
225 virtual const IR::ParserState *convertParser(const IR::V1Parser *,
227 virtual const IR::Statement *convertParserStatement(const IR::Expression *expr);
228 virtual const IR::P4Control *convertControl(const IR::V1Control *control, cstring newName);
229 virtual const IR::Declaration_Instance *convertDirectMeter(const IR::Meter *m, cstring newName);
230 virtual const IR::Declaration_Instance *convertDirectCounter(const IR::Counter *c,
231 cstring newName);
232 virtual const IR::Declaration_Instance *convert(const IR::CounterOrMeter *cm, cstring newName);
233 virtual const IR::Declaration_Instance *convertActionProfile(const IR::ActionProfile *,
234 cstring newName);
235 virtual const IR::P4Table *convertTable(const IR::V1Table *table, cstring newName,
237 std::map<cstring, cstring> &);
238 virtual const IR::P4Action *convertAction(const IR::ActionFunction *action, cstring newName,
239 const IR::Meter *meterToAccess,
240 cstring counterToAccess);
241 virtual const IR::Statement *convertMeterCall(const IR::Meter *meterToAccess);
242 virtual const IR::Statement *convertCounterCall(cstring counterToAccess);
243 virtual const IR::Type_Control *controlType(IR::ID name);
244 const IR::PathExpression *getState(IR::ID dest);
245 virtual const IR::Expression *counterType(const IR::CounterOrMeter *cm);
246 virtual void createChecksumVerifications();
247 virtual void createChecksumUpdates();
248 virtual void createStructures();
249 virtual cstring createType(const IR::Type_StructLike *type, bool header,
250 std::unordered_set<const IR::Type *> *converted);
251 virtual void createParser();
252 virtual void createControls();
253 void createDeparserInternal(IR::ID deparserId, IR::Parameter *packetOut, IR::Parameter *headers,
254 std::vector<IR::Parameter *>,
256 std::function<IR::BlockStatement *(IR::BlockStatement *)>);
257 virtual void createDeparser();
258 virtual void createMain();
259
260 public:
261 void include(cstring filename, cstring ppoptions = cstring());
264 void populateOutputNames();
265 const IR::AssignmentStatement *assign(Util::SourceInfo srcInfo, const IR::Expression *left,
266 const IR::Expression *right, const IR::Type *type);
267 virtual const IR::Expression *convertFieldList(const IR::Expression *expression);
268 virtual const IR::Expression *convertHashAlgorithm(Util::SourceInfo srcInfo, IR::ID algorithm);
269 virtual const IR::Expression *convertHashAlgorithms(const IR::NameList *algorithm);
270 virtual const IR::Declaration_Instance *convert(const IR::Register *reg, cstring newName,
271 const IR::Type *regElementType = nullptr);
272 virtual const IR::Type_Struct *createFieldListType(const IR::Expression *expression);
273 virtual const IR::FieldListCalculation *getFieldListCalculation(const IR::Expression *);
274 virtual const IR::FieldList *getFieldLists(const IR::FieldListCalculation *flc);
275 virtual const IR::Expression *paramReference(const IR::Parameter *param);
276 const IR::Statement *sliceAssign(const IR::Primitive *prim, const IR::Expression *left,
277 const IR::Expression *right, const IR::Expression *mask);
278 void tablesReferred(const IR::V1Control *control, std::vector<const IR::V1Table *> &out);
279 bool isHeader(const IR::ConcreteHeaderRef *nhr) const;
280 cstring makeUniqueName(cstring base);
281 bool isFieldInList(cstring type, cstring field, const IR::FieldList *fl) const;
284 virtual const IR::Vector<IR::Expression> *listIndexes(cstring type, cstring field) const;
287 const IR::Expression *listIndex(const IR::Expression *fl) const;
288
289 const IR::Type *explodeType(const std::vector<const IR::Type::Bits *> &fieldTypes);
290 const IR::Expression *explodeLabel(const IR::Constant *value, const IR::Constant *mask,
291 const std::vector<const IR::Type::Bits *> &fieldTypes);
292
293 virtual IR::Vector<IR::Argument> *createApplyArguments(cstring n);
294
295 const IR::V1Control *ingress;
296 IR::ID ingressReference;
297
298 const IR::P4Control *verifyChecksums;
299 const IR::P4Control *updateChecksums;
300 const IR::P4Control *deparser;
302 const IR::Expression *latest;
303 const int defaultRegisterWidth = 32;
304
305 virtual void loadModel();
306 void createExterns();
307 void createTypes();
308 virtual const IR::P4Program *create(Util::SourceInfo info);
309};
310
311} // namespace P4::P4V1
312
313#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:149
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:509
std::vector< const IR::Declaration * > localInstances
extra local instances to control created by primitive translation
Definition frontends/p4-14/fromv1.0/programStructure.h:202
const IR::Expression * latest
Represents 'latest' P4-14 construct.
Definition frontends/p4-14/fromv1.0/programStructure.h:302
std::map< cstring, const IR::Type * > finalHeaderType
Definition frontends/p4-14/fromv1.0/programStructure.h:173
static IR::Vector< IR::Annotation > addGlobalNameAnnotation(cstring name, const IR::Vector< IR::Annotation > &annos)
Definition frontends/p4-14/fromv1.0/programStructure.cpp:85
virtual void createControls()
Definition frontends/p4-14/fromv1.0/programStructure.cpp:2325
const IR::Expression * listIndex(const IR::Expression *fl) const
Definition frontends/p4-14/fromv1.0/programStructure.cpp:160
void populateOutputNames()
Definition frontends/p4-14/fromv1.0/programStructure.cpp:2627
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:131
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