P4C
The P4 Compiler
Loading...
Searching...
No Matches
extract_deparser.h
1
19#ifndef BF_P4C_PARDE_EXTRACT_DEPARSER_H_
20#define BF_P4C_PARDE_EXTRACT_DEPARSER_H_
21
22#include "backends/tofino/bf-p4c/arch/bridge_metadata.h"
23#include "backends/tofino/bf-p4c/bf-p4c-options.h"
24#include "backends/tofino/bf-p4c/common/ir_utils.h"
25#include "backends/tofino/bf-p4c/common/pragma/all_pragmas.h"
26#include "backends/tofino/bf-p4c/common/pragma/collect_global_pragma.h"
27#include "backends/tofino/bf-p4c/device.h"
28#include "backends/tofino/bf-p4c/ir/gress.h"
29#include "backends/tofino/bf-p4c/parde/parde_visitor.h"
30#include "frontends/common/resolveReferences/referenceMap.h"
31#include "frontends/p4/typeMap.h"
32#include "ir/ir.h"
33#include "lib/cstring.h"
34#include "lib/exceptions.h"
35
36namespace P4 {
37namespace IR {
38
39namespace BFN {
40class Pipe;
41} // namespace BFN
42
43class P4Control;
44
45} // namespace IR
46} // namespace P4
47
48namespace BFN {
49
50using namespace P4;
59 P4::TypeMap *typeMap;
60 P4::ReferenceMap *refMap;
61 IR::BFN::Pipe *rv;
62 IR::BFN::Deparser *dprsr = nullptr;
64
66
67 std::set<ordered_set<cstring> *> userEnforcedHeaderOrdering;
68
69 void generateEmits(const IR::MethodCallExpression *mc);
70 void generateDigest(IR::BFN::Digest *&digest, cstring name, const IR::Expression *list,
71 const IR::Expression *select, int digest_index,
72 cstring controlPlaneName = nullptr);
73 void convertConcatToList(std::vector<const IR::Expression *> &slices, const IR::Concat *expr);
74 void processConcat(IR::Vector<IR::BFN::FieldLVal> &vec, const IR::Concat *expr);
75
76 std::tuple<int, const IR::Expression *> getDigestIndex(const IR::IfStatement *, cstring name,
77 bool singleEntry = false);
78 int getDigestIndex(const IR::Declaration_Instance *);
79 void processMirrorEmit(const IR::MethodCallExpression *, const IR::Expression *, int idx);
80 void processMirrorEmit(const IR::MethodCallExpression *, int idx);
81 void processResubmitEmit(const IR::MethodCallExpression *, const IR::Expression *, int idx);
82 void processResubmitEmit(const IR::MethodCallExpression *, int idx);
83 void processDigestPack(const IR::MethodCallExpression *, int, cstring);
84 void enforceHeaderOrdering();
85
87 IR::ID getTnaParamName(const IR::BFN::TnaDeparser *deparser, IR::ID orig_name);
88
89 bool preorder(const IR::Annotation *annot) override;
90 bool preorder(const IR::AssignmentStatement *stmt) override;
91 void postorder(const IR::MethodCallExpression *mc) override;
92 void end_apply() override;
93
94 public:
95 explicit ExtractDeparser(P4::ReferenceMap *refMap, P4::TypeMap *typeMap, IR::BFN::Pipe *rv)
96 : typeMap(typeMap), refMap(refMap), rv(rv) {
97 setName("ExtractDeparser");
98 }
99
100 bool preorder(const IR::BFN::TnaDeparser *deparser) override {
101 gress_t thread = deparser->thread;
102 dprsr = new IR::BFN::Deparser(thread);
103 digests.clear();
104 return true;
105 }
106
107 void postorder(const IR::BFN::TnaDeparser *deparser) override {
108 for (const auto &kv : digests) {
109 auto name = kv.first;
110 auto digest = kv.second;
111 if (!digest) continue;
112 for (auto fieldList : digest->fieldLists) {
113 if (fieldList->idx < 0 ||
114 fieldList->idx > static_cast<int>(Device::maxCloneId(deparser->thread))) {
115 error("Invalid %1% index %2% in %3%", name, fieldList->idx,
116 int(deparser->thread));
117 }
118 }
119 }
120 rv->thread[deparser->thread].deparser = dprsr;
121 }
122};
124// in mirror, digest or resubmit.
126 const IR::Type *left = nullptr;
127 bool stmtOk = false;
128 explicit AssignmentStmtErrorCheck(const IR::Type *left) : left(left) {}
129
130 void postorder(const IR::MethodCallExpression *methodCall) override {
131 auto member = methodCall->method->to<IR::Member>();
132 auto expr = member->expr->to<IR::PathExpression>();
133 if (!expr) return;
134 const IR::Type_Extern *type = nullptr;
135 if (auto spType = expr->type->to<IR::Type_SpecializedCanonical>()) {
136 type = spType->baseType->to<IR::Type_Extern>();
137 } else {
138 type = expr->type->to<IR::Type_Extern>();
139 }
140 if (!type) return;
141 if (type->name != "Mirror" && type->name != "Digest" && type->name != "Resubmit") {
142 return;
143 }
144 auto arguments = *methodCall->arguments;
145 for (auto argument : arguments) {
146 if (argument->expression->type->equiv(*left)) {
147 stmtOk = true;
148 return;
149 }
150 }
151 return;
152 }
153};
154
155} // namespace BFN
156
157#endif /* BF_P4C_PARDE_EXTRACT_DEPARSER_H_ */
Definition vector.h:59
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:41
Definition cstring.h:85
Definition ordered_map.h:32
Transforms midend deparser IR::BFN::TnaDeparser into backend deparser IR::BFN::Deparser.
Definition extract_deparser.h:58
Definition parde_visitor.h:117
The namespace encapsulating Barefoot/Intel-specific stuff.
Definition add_t2na_meta.cpp:21
The namespace encapsulating IR node classes.
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
void error(const char *format, Args &&...args)
Report an error with the given message.
Definition lib/error.h:51
Definition extract_deparser.h:125
Definition id.h:28