P4C
The P4 Compiler
 
Loading...
Searching...
No Matches
expression.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_EXPRESSION_H_
18#define BACKENDS_BMV2_COMMON_EXPRESSION_H_
19
20#include "frontends/common/resolveReferences/referenceMap.h"
21#include "frontends/p4/coreLibrary.h"
22#include "frontends/p4/enumInstance.h"
23#include "frontends/p4/methodInstance.h"
24#include "frontends/p4/typeMap.h"
25#include "ir/ir.h"
26#include "lib/big_int_util.h"
27#include "lib/json.h"
28#include "lower.h"
29#include "programStructure.h"
30
31namespace BMV2 {
32
39class ArithmeticFixup : public Transform {
40 P4::TypeMap *typeMap;
41
42 public:
43 const IR::Expression *fix(const IR::Expression *expr, const IR::Type_Bits *type);
44 const IR::Node *updateType(const IR::Expression *expression);
45 const IR::Node *postorder(IR::Expression *expression) override;
46 const IR::Node *postorder(IR::Operation_Binary *expression) override;
47 const IR::Node *postorder(IR::Neg *expression) override;
48 const IR::Node *postorder(IR::Cmpl *expression) override;
49 const IR::Node *postorder(IR::Cast *expression) override;
50 explicit ArithmeticFixup(P4::TypeMap *typeMap) : typeMap(typeMap) { CHECK_NULL(typeMap); }
51};
52
53class ExpressionConverter : public Inspector {
54 P4::ReferenceMap *refMap;
55 P4::TypeMap *typeMap;
56 ProgramStructure *structure;
57 P4::P4CoreLibrary &corelib;
58 cstring scalarsName;
59
65 std::map<const IR::Expression *, Util::IJson *> map;
66 bool leftValue; // true if converting a left value
67 // in some cases the bmv2 JSON requires a 'bitwidth' attribute for hex
68 // strings (e.g. for constants in calculation inputs). When this flag is set
69 // to true, we add this attribute.
70 bool withConstantWidths{false};
71
72 public:
74 cstring scalarsName)
75 : refMap(refMap),
76 typeMap(typeMap),
77 structure(structure),
78 corelib(P4::P4CoreLibrary::instance()),
79 scalarsName(scalarsName),
80 leftValue(false),
81 simpleExpressionsOnly(false) {}
85
87 const IR::Parameter *enclosingParamReference(const IR::Expression *expression);
88
89 // Each architecture typically has some special parameters that requires
90 // special handling. The examples are standard_metadata in the v1model and
91 // packet path related metadata in PSA. Each target should subclass the
92 // ExpressionConverter and implement this function with target-specific
93 // handling code to deal with the special parameters.
94 virtual Util::IJson *convertParam(const IR::Parameter *param, cstring fieldName) = 0;
95
96 Util::IJson *get(const IR::Expression *expression) const;
97 Util::IJson *fixLocal(Util::IJson *json);
98
109 Util::IJson *convert(const IR::Expression *e, bool doFixup = true, bool wrap = true,
110 bool convertBool = false);
111 Util::IJson *convertLeftValue(const IR::Expression *e);
112 Util::IJson *convertWithConstantWidths(const IR::Expression *e);
113 bool isArrayIndexRuntime(const IR::Expression *e);
114
115 void postorder(const IR::BoolLiteral *expression) override;
116 void postorder(const IR::MethodCallExpression *expression) override;
117 void postorder(const IR::Cast *expression) override;
118 void postorder(const IR::Slice *expression) override;
119 void postorder(const IR::AddSat *expression) override { saturated_binary(expression); }
120 void postorder(const IR::SubSat *expression) override { saturated_binary(expression); }
121 void postorder(const IR::Constant *expression) override;
122 void postorder(const IR::ArrayIndex *expression) override;
123 void postorder(const IR::Member *expression) override;
124 void postorder(const IR::Mux *expression) override;
125 void postorder(const IR::IntMod *expression) override;
126 void postorder(const IR::Operation_Binary *expression) override;
127 void postorder(const IR::ListExpression *expression) override;
128 void postorder(const IR::StructExpression *expression) override;
129 void postorder(const IR::Operation_Unary *expression) override;
130 void postorder(const IR::PathExpression *expression) override;
131 void postorder(const IR::StringLiteral *expression) override;
132 void postorder(const IR::TypeNameExpression *expression) override;
133 void postorder(const IR::Expression *expression) override;
134 void mapExpression(const IR::Expression *expression, Util::IJson *json);
135
136 private:
137 void binary(const IR::Operation_Binary *expression);
138 void saturated_binary(const IR::Operation_Binary *expression);
139};
140
141} // namespace BMV2
142
143#endif /* BACKENDS_BMV2_COMMON_EXPRESSION_H_ */
Definition expression.h:39
Definition expression.h:53
Util::IJson * convert(const IR::Expression *e, bool doFixup=true, bool wrap=true, bool convertBool=false)
Definition expression.cpp:767
const IR::Parameter * enclosingParamReference(const IR::Expression *expression)
Non-null if the expression refers to a parameter from the enclosing control.
Definition expression.cpp:236
bool simpleExpressionsOnly
Definition expression.h:84
Definition programStructure.h:40
Definition sharedActionSelectorCheck.h:40
Definition coreLibrary.h:100
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition typeMap.h:42
Definition json.h:40
Definition cstring.h:72