50 class NamedObjectInfo {
54 std::unordered_map<cstring, int> *allNames;
55 std::map<cstring, T> nameToObject;
56 std::map<T, cstring> objectToNewName;
60 friend class NamedObjectInfo;
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) {}
70 const iterator &operator++() {
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]);
81 explicit NamedObjectInfo(std::unordered_map<cstring, int> *allNames) : allNames(allNames) {}
83 if (objectToNewName.find(obj) != objectToNewName.end()) {
85 LOG3(
" already emplaced obj " << obj);
89 nameToObject.emplace(obj->name, obj);
92 if (allNames ==
nullptr || (allNames->find(obj->name) == allNames->end())) {
95 newName = cstring::make_unique(*allNames, obj->name, allNames->at(obj->name),
'_');
97 if (allNames !=
nullptr) allNames->insert({newName, 0});
98 LOG3(
"Discovered " << obj <<
" named " << newName);
99 objectToNewName.emplace(obj, newName);
102 T get(
cstring name)
const { return ::P4::get(nameToObject, name); }
105 return ::P4::get(objectToNewName,
object, object->name.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); }
113 allNames->erase(name);
114 auto obj = get(name);
115 objectToNewName.erase(obj);
116 nameToObject.erase(name);
120 std::set<cstring> included_files;
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;
158 std::map<cstring, IR::Vector<IR::Expression>> extracts;
159 std::map<cstring, cstring> directCounters;
162 std::map<const IR::Meter *, const IR::Declaration_Instance *> meterMap;
163 std::map<cstring, const IR::Declaration_Instance *> counterMap;
167 std::map<const IR::V1Table *, const IR::V1Control *> tableMapping;
168 std::map<const IR::V1Table *, const IR::Apply *> tableInvocation;
183 std::map<cstring, const IR::ParserState *> parserEntryPoints;
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;
207 const IR::Parameter *parserPacketIn =
nullptr;
208 const IR::Parameter *parserHeadersOut =
nullptr;
214 virtual const IR::Statement *convertPrimitive(
const IR::Primitive *primitive);
215 virtual void checkHeaderType(
const IR::Type_StructLike *hrd,
bool toStruct);
234 static const IR::Annotations *addNameAnnotation(
cstring name,
235 const IR::Annotations *annos =
nullptr);
243 const IR::Annotations *annos =
nullptr);
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,
252 virtual const IR::Declaration_Instance *convert(
const IR::CounterOrMeter *cm,
cstring newName);
253 virtual const IR::Declaration_Instance *convertActionProfile(
const IR::ActionProfile *,
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,
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();
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();
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);
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;
301 bool isFieldInList(
cstring type,
cstring field,
const IR::FieldList *fl)
const;
307 const IR::Expression *
listIndex(
const IR::Expression *fl)
const;
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);
315 const IR::V1Control *ingress;
318 const IR::P4Control *verifyChecksums;
319 const IR::P4Control *updateChecksums;
320 const IR::P4Control *deparser;
323 const int defaultRegisterWidth = 32;
325 virtual void loadModel();
326 void createExterns();