P4C
The P4 Compiler
Loading...
Searching...
No Matches
extern.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef BACKENDS_BMV2_COMMON_EXTERN_H_
18#define BACKENDS_BMV2_COMMON_EXTERN_H_
19
20#include "backends/common/programStructure.h"
21#include "frontends/p4/methodInstance.h"
22#include "helpers.h"
23#include "ir/ir.h"
24
25namespace P4::BMV2 {
26
28 static std::map<cstring, ExternConverter *> *cvtForType;
29
30 public:
31 static void registerExternConverter(cstring, ExternConverter *);
32
33 virtual Util::IJson *convertExternObject(ConversionContext *ctxt, const P4::ExternMethod *em,
34 const IR::MethodCallExpression *mc,
35 const IR::StatOrDecl *s, const bool &emitExterns);
36 virtual void convertExternInstance(ConversionContext *ctxt, const IR::Declaration *c,
37 const IR::ExternBlock *eb, const bool &emitExterns);
38 virtual Util::IJson *convertExternFunction(ConversionContext *ctxt,
39 const P4::ExternFunction *ef,
40 const IR::MethodCallExpression *mc,
41 const IR::StatOrDecl *s, const bool emitExterns);
42
43 static ExternConverter *get(cstring type);
44 static ExternConverter *get(const IR::Type_Extern *type) { return get(type->name); }
45 static ExternConverter *get(const IR::ExternBlock *eb) { return get(eb->type); }
46 static ExternConverter *get(const P4::ExternFunction *ef) { return get(ef->method->name); }
47 static ExternConverter *get(const P4::ExternMethod *em) {
48 return get(em->originalExternType->name);
49 }
50
51 static Util::IJson *cvtExternObject(ConversionContext *ctxt, const P4::ExternMethod *em,
52 const IR::MethodCallExpression *mc, const IR::StatOrDecl *s,
53 const bool &emitExterns);
54 static void cvtExternInstance(ConversionContext *ctxt, const IR::Declaration *c,
55 const IR::ExternBlock *eb, const bool &emitExterns);
56 static Util::IJson *cvtExternFunction(ConversionContext *ctxt, const P4::ExternFunction *ef,
57 const IR::MethodCallExpression *mc,
58 const IR::StatOrDecl *s, const bool emitExterns);
59
60 // helper function for simple switch
61 void modelError(const char *format, const IR::Node *place) const;
62 void addToFieldList(ConversionContext *ctxt, const IR::Expression *expr, Util::JsonArray *fl);
63 int createFieldList(ConversionContext *ctxt, const IR::Expression *expr, cstring group,
64 cstring listName, Util::JsonArray *field_lists);
65 cstring createCalculation(ConversionContext *ctxt, cstring algo, const IR::Expression *fields,
66 Util::JsonArray *calculations, bool usePayload, const IR::Node *node);
67 static cstring convertHashAlgorithm(cstring algorithm);
68 Util::IJson *convertAssertAssume(ConversionContext *ctxt,
69 const IR::MethodCallExpression *methodCall,
70 const P4::ExternFunction *ef);
71};
72
73#define EXTERN_CONVERTER_W_FUNCTION_AND_MODEL(extern_name, model_type, model_name) \
74 class ExternConverter_##extern_name : public ExternConverter { \
75 model_type &model_name; \
76 ExternConverter_##extern_name() : model_name(model_type::instance) { \
77 registerExternConverter(cstring(#extern_name), this); \
78 } \
79 static ExternConverter_##extern_name singleton; \
80 Util::IJson *convertExternFunction(ConversionContext *ctxt, const P4::ExternFunction *ef, \
81 const IR::MethodCallExpression *mc, \
82 const IR::StatOrDecl *s, \
83 const bool emitExterns) override; \
84 };
85
86#define EXTERN_CONVERTER_W_FUNCTION(extern_name) \
87 class ExternConverter_##extern_name : public ExternConverter { \
88 ExternConverter_##extern_name() { registerExternConverter(cstring(#extern_name), this); } \
89 static ExternConverter_##extern_name singleton; \
90 Util::IJson *convertExternFunction(ConversionContext *ctxt, const P4::ExternFunction *ef, \
91 const IR::MethodCallExpression *mc, \
92 const IR::StatOrDecl *s, \
93 const bool emitExterns) override; \
94 };
95
96#define EXTERN_CONVERTER_W_INSTANCE_AND_MODEL(extern_name, model_type, model_name) \
97 class ExternConverter_##extern_name : public ExternConverter { \
98 model_type &model_name; \
99 ExternConverter_##extern_name() : model_name(model_type::instance) { \
100 registerExternConverter(cstring(#extern_name), this); \
101 } \
102 static ExternConverter_##extern_name singleton; \
103 void convertExternInstance(ConversionContext *ctxt, const IR::Declaration *c, \
104 const IR::ExternBlock *eb, const bool &emitExterns) override; \
105 };
106
107#define EXTERN_CONVERTER_W_INSTANCE(extern_name) \
108 class ExternConverter_##extern_name : public ExternConverter { \
109 ExternConverter_##extern_name() { registerExternConverter(cstring(#extern_name), this); } \
110 static ExternConverter_##extern_name singleton; \
111 void convertExternInstance(ConversionContext *ctxt, const IR::Declaration *c, \
112 const IR::ExternBlock *eb, const bool &emitExterns) override; \
113 };
114
115#define EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE_AND_MODEL(extern_name, type, name) \
116 class ExternConverter_##extern_name : public ExternConverter { \
117 type &name; \
118 ExternConverter_##extern_name() : name(type::instance) { \
119 registerExternConverter(cstring(#extern_name), this); \
120 } \
121 static ExternConverter_##extern_name singleton; \
122 void convertExternInstance(ConversionContext *ctxt, const IR::Declaration *c, \
123 const IR::ExternBlock *eb, const bool &emitExternes) override; \
124 Util::IJson *convertExternObject(ConversionContext *ctxt, const P4::ExternMethod *em, \
125 const IR::MethodCallExpression *mc, \
126 const IR::StatOrDecl *s, \
127 const bool &emitExterns) override; \
128 };
129
130#define EXTERN_CONVERTER_W_OBJECT_AND_INSTANCE(extern_name) \
131 class ExternConverter_##extern_name : public ExternConverter { \
132 ExternConverter_##extern_name() { registerExternConverter(cstring(#extern_name), this); } \
133 static ExternConverter_##extern_name singleton; \
134 void convertExternInstance(ConversionContext *ctxt, const IR::Declaration *c, \
135 const IR::ExternBlock *eb, const bool &emitExterns) override; \
136 Util::IJson *convertExternObject(ConversionContext *ctxt, const P4::ExternMethod *em, \
137 const IR::MethodCallExpression *mc, \
138 const IR::StatOrDecl *s, \
139 const bool &emitExterns) override; \
140 };
141
142EXTERN_CONVERTER_W_FUNCTION(assert)
143EXTERN_CONVERTER_W_FUNCTION(assume)
144
145} // namespace P4::BMV2
146
147#endif /* BACKENDS_BMV2_COMMON_EXTERN_H_ */
Definition extern.h:27
virtual void convertExternInstance(ConversionContext *ctxt, const IR::Declaration *c, const IR::ExternBlock *eb, const bool &emitExterns)
Definition extern.cpp:83
Definition methodInstance.h:194
Definition methodInstance.h:168
Definition node.h:94
Definition json.h:40
Definition json.h:115
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:21
Definition bmv2/common/helpers.h:297