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);
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,
232 virtual const IR::Declaration_Instance *convert(
const IR::CounterOrMeter *cm,
cstring newName);
233 virtual const IR::Declaration_Instance *convertActionProfile(
const IR::ActionProfile *,
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,
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();
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();
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);
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;
281 bool isFieldInList(
cstring type,
cstring field,
const IR::FieldList *fl)
const;
287 const IR::Expression *
listIndex(
const IR::Expression *fl)
const;
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);
295 const IR::V1Control *ingress;
298 const IR::P4Control *verifyChecksums;
299 const IR::P4Control *updateChecksums;
300 const IR::P4Control *deparser;
303 const int defaultRegisterWidth = 32;
305 virtual void loadModel();
306 void createExterns();