P4C
The P4 Compiler
Loading...
Searching...
No Matches
tables_to_ids.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_PHV_UTILS_TABLES_TO_IDS_H_
20#define BACKENDS_TOFINO_BF_P4C_PHV_UTILS_TABLES_TO_IDS_H_
21
22class MapTablesToIDs : public Inspector {
23 private:
24 ordered_map<const IR::MAU::Table *, unsigned> tableToIDs;
25 ordered_map<unsigned, const IR::MAU::Table *> idToTables;
26 unsigned lastID = 0;
27
28 profile_t init_apply(const IR::Node *root) override {
29 tableToIDs.clear();
30 idToTables.clear();
31 lastID = 0;
32 return Inspector::init_apply(root);
33 }
34
35 bool preorder(const IR::MAU::Table *tbl) override {
36 BUG_CHECK(tableToIDs.count(tbl) == 0, "Table %1% already assigned ID %2%", tbl->name,
37 tableToIDs.at(tbl));
38 BUG_CHECK(idToTables.count(lastID) == 0, "ID %1% already assigned to table %2%", lastID,
39 idToTables.at(lastID));
40 tableToIDs[tbl] = lastID;
41 idToTables[lastID] = tbl;
42 ++lastID;
43 return true;
44 }
45
46 public:
47 const ordered_map<const IR::MAU::Table *, unsigned> &getTableToIDs() const {
48 return tableToIDs;
49 }
50
51 const ordered_map<unsigned, const IR::MAU::Table *> &getIDsToTables() const {
52 return idToTables;
53 }
54
55 unsigned getIDForTable(const IR::MAU::Table *tbl) const {
56 BUG_CHECK(tableToIDs.count(tbl), "No ID assigned to table %1%", tbl->name);
57 return tableToIDs.at(tbl);
58 }
59
60 const IR::MAU::Table *getTableForID(unsigned id) const {
61 BUG_CHECK(idToTables.count(id), "ID %1% not assigned to any table", id);
62 return idToTables.at(id);
63 }
64};
65
66#endif /* BACKENDS_TOFINO_BF_P4C_PHV_UTILS_TABLES_TO_IDS_H_ */
Definition tables_to_ids.h:22