P4C
The P4 Compiler
Loading...
Searching...
No Matches
program_structure.h
1
19#ifndef BACKENDS_TOFINO_BF_P4C_ARCH_PROGRAM_STRUCTURE_H_
20#define BACKENDS_TOFINO_BF_P4C_ARCH_PROGRAM_STRUCTURE_H_
21
22#include "bf-p4c/ir/gress.h"
23#include "frontends/common/resolveReferences/resolveReferences.h"
24#include "frontends/p4/evaluator/evaluator.h"
25#include "ir/ir.h"
26#include "ir/namemap.h"
27#include "lib/ordered_set.h"
28
29namespace BFN {
30
32using SimpleNameMap = std::map<cstring, cstring>;
35
36// used by checksum translation
38
41 cstring structName;
42 cstring fieldName;
43 int width;
44 int offset;
45
46 // indicate the field is in a header or struct that is a member of
47 // the compiler_generated_meta.
48 bool isCG;
49
50 MetadataField(cstring sn, cstring fn, int w)
51 : structName(sn), fieldName(fn), width(w), offset(0), isCG(false) {}
52
53 MetadataField(cstring sn, cstring fn, int w, int o)
54 : structName(sn), fieldName(fn), width(w), offset(o), isCG(false) {}
55
56 MetadataField(cstring sn, cstring fn, int w, bool isCG)
57 : structName(sn), fieldName(fn), width(w), offset(0), isCG(isCG) {}
58
59 MetadataField(cstring sn, cstring fn, int w, int o, bool isCG)
60 : structName(sn), fieldName(fn), width(w), offset(o), isCG(isCG) {}
61
62 cstring name() { return structName + "." + fieldName; }
63
64 bool operator<(const MetadataField &other) const {
65 if (structName != other.structName) return structName < other.structName;
66 return fieldName < other.fieldName;
67 }
68
69 bool operator==(const MetadataField &other) const {
70 return structName == other.structName && fieldName == other.fieldName;
71 }
72
73 bool operator!=(const MetadataField &a) const { return !(*this == a); }
74 friend std::ostream &operator<<(std::ostream &out, const BFN::MetadataField &m);
75};
76
80
81 static const cstring INGRESS_PARSER;
82 static const cstring INGRESS;
83 static const cstring INGRESS_DEPARSER;
84 static const cstring EGRESS_PARSER;
85 static const cstring EGRESS;
86 static const cstring EGRESS_DEPARSER;
87
90 void include(cstring filename, IR::Vector<IR::Node> *decls);
91
92 IR::IndexedVector<IR::Node> declarations;
93 IR::Vector<IR::Node> targetTypes;
98
99 NodeNameMap nameMap;
100
107 std::vector<const IR::Type_Action *> action_types;
108
110 const IR::ToplevelBlock *toplevel;
111
113 std::set<cstring> unique_names = {"checksum"_cs, "hash"_cs, "random"_cs};
114
118
119 cstring getBlockName(cstring name);
120 bool isIngressParser(const IR::P4Parser *parser);
121 bool isIngress(const IR::P4Control *control);
122 bool isIngressDeparser(const IR::P4Control *control);
123 bool isEgressParser(const IR::P4Parser *parser);
124 bool isEgress(const IR::P4Control *control);
125 bool isEgressDeparser(const IR::P4Control *control);
126
127 // additional declarations created by the translation pass and to be
128 // prepended to each block.
129 std::vector<const IR::Declaration *> ingressParserDeclarations;
130 std::vector<const IR::Declaration *> egressParserDeclarations;
131 std::vector<const IR::Declaration *> ingressDeclarations;
132 std::vector<const IR::Declaration *> egressDeclarations;
133 std::vector<const IR::Declaration *> ingressDeparserDeclarations;
134 std::vector<const IR::Declaration *> egressDeparserDeclarations;
135
136 // additional statement created by the translation pass and to be
137 // appended to each block.
138 std::vector<const IR::StatOrDecl *> ingressStatements;
139 std::vector<const IR::StatOrDecl *> egressStatements;
140 std::vector<const IR::StatOrDecl *> ingressDeparserStatements;
141 std::vector<const IR::StatOrDecl *> egressDeparserStatements;
142
143 // key is parser state name.
146
152
155 std::map<MetadataField, MetadataField> ingressMetadataNameMap;
156 std::map<MetadataField, MetadataField> egressMetadataNameMap;
157 std::set<MetadataField> targetMetadataSet;
158
159 void addMetadata(gress_t gress, MetadataField src, MetadataField dst) {
160 auto &nameMap =
161 (gress == gress_t::INGRESS) ? ingressMetadataNameMap : egressMetadataNameMap;
162 auto itr = nameMap.emplace(src, dst);
163 if (!itr.second) {
164 BUG_CHECK(
165 itr.first->second == dst,
166 "Cannot add metadata mapping %1% - %2% as mapping already exists to %1% - %3%",
167 src.name(), dst.name(), itr.first->second.name());
168 }
169 targetMetadataSet.insert(dst);
170 LOG3("Adding Metadata map on thread '" << gress);
171 LOG3(" src : " << src << ", dst : " << dst);
172 }
173
174 void addMetadata(MetadataField src, MetadataField dst) {
175 addMetadata(gress_t::INGRESS, src, dst);
176 addMetadata(gress_t::EGRESS, src, dst);
177 }
178
180 void createErrors();
181 void createTofinoArch();
182 void createTypes();
183 void createActions();
184 virtual void createParsers() = 0;
185 virtual void createControls() = 0;
186 virtual void createMain() = 0;
187 virtual const IR::P4Program *create(const IR::P4Program *program) = 0;
188};
189
190} // namespace BFN
191
192#endif /* BACKENDS_TOFINO_BF_P4C_ARCH_PROGRAM_STRUCTURE_H_ */
Definition node.h:52
Definition vector.h:59
Definition backends/common/programStructure.h:32
Definition cstring.h:85
Definition ordered_map.h:32
Definition ordered_set.h:32
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
A helper struct used to construct the metadata remapping tables.
Definition program_structure.h:40
ordered_map< cstring, const IR::P4Control * > controls
Definition program_structure.h:103
ordered_map< const IR::Member *, const IR::Member * > pathsToDo
maintain the paths to translate and their thread info
Definition program_structure.h:149
ordered_map< const IR::Node *, const IR::Node * > _map
the translation map.
Definition program_structure.h:79
const IR::ToplevelBlock * toplevel
program control block names from P14
Definition program_structure.h:110
void include(cstring filename, IR::Vector< IR::Node > *decls)
ordered_map< cstring, cstring > blockNames
Definition program_structure.h:117
std::map< MetadataField, MetadataField > ingressMetadataNameMap
Definition program_structure.h:155
ordered_set< cstring > errors
target architecture types
Definition program_structure.h:95
std::set< cstring > unique_names
all unique names in the program
Definition program_structure.h:113