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 "backends/common/programStructure.h"
21#include "frontends/common/resolveReferences/referenceMap.h"
22#include "frontends/p4/coreLibrary.h"
23#include "frontends/p4/enumInstance.h"
24#include "frontends/p4/methodInstance.h"
25#include "frontends/p4/typeMap.h"
26#include "ir/ir.h"
27#include "lib/big_int_util.h"
28#include "lib/json.h"
29#include "lower.h"
30
31namespace P4::BMV2 {
32
37class ArithmeticFixup : public Transform {
38 P4::TypeMap *typeMap;
39
40 public:
41 const IR::Expression *fix(const IR::Expression *expr, const IR::Type_Bits *type);
42 const IR::Node *updateType(const IR::Expression *expression);
43 const IR::Node *postorder(IR::Expression *expression) override;
44 const IR::Node *postorder(IR::Operation_Binary *expression) override;
45 const IR::Node *postorder(IR::Neg *expression) override;
46 const IR::Node *postorder(IR::Cmpl *expression) override;
47 const IR::Node *postorder(IR::Cast *expression) override;
48 explicit ArithmeticFixup(P4::TypeMap *typeMap) : typeMap(typeMap) { CHECK_NULL(typeMap); }
49};
50
52 P4::ReferenceMap *refMap;
53 P4::TypeMap *typeMap;
54 P4::ProgramStructure *structure;
55 P4::P4CoreLibrary &corelib;
56 cstring scalarsName;
57
63 std::map<const IR::Expression *, Util::IJson *> map;
64 bool leftValue; // true if converting a left value
65 // in some cases the bmv2 JSON requires a 'bitwidth' attribute for hex
66 // strings (e.g. for constants in calculation inputs). When this flag is set
67 // to true, we add this attribute.
68 bool withConstantWidths{false};
69
70 public:
72 P4::ProgramStructure *structure, cstring scalarsName)
73 : refMap(refMap),
74 typeMap(typeMap),
75 structure(structure),
76 corelib(P4::P4CoreLibrary::instance()),
77 scalarsName(scalarsName),
78 leftValue(false),
79 simpleExpressionsOnly(false) {}
83
85 const IR::Parameter *enclosingParamReference(const IR::Expression *expression);
86
92 virtual Util::IJson *convertParam(const IR::Parameter *param, cstring fieldName) = 0;
93
94 Util::IJson *get(const IR::Expression *expression) const;
95 Util::IJson *fixLocal(Util::IJson *json);
96
105 Util::IJson *convert(const IR::Expression *e, bool doFixup = true, bool wrap = true,
106 bool convertBool = false);
107 Util::IJson *convertLeftValue(const IR::Expression *e);
108 Util::IJson *convertWithConstantWidths(const IR::Expression *e);
109 bool isArrayIndexRuntime(const IR::Expression *e);
110
111 void postorder(const IR::BoolLiteral *expression) override;
112 void postorder(const IR::MethodCallExpression *expression) override;
113 void postorder(const IR::Cast *expression) override;
114 void postorder(const IR::Slice *expression) override;
115 void postorder(const IR::AddSat *expression) override { saturated_binary(expression); }
116 void postorder(const IR::SubSat *expression) override { saturated_binary(expression); }
117 void postorder(const IR::Constant *expression) override;
118 void postorder(const IR::ArrayIndex *expression) override;
119 void postorder(const IR::Member *expression) override;
120 void postorder(const IR::Mux *expression) override;
121 void postorder(const IR::IntMod *expression) override;
122 void postorder(const IR::Operation_Binary *expression) override;
123 void postorder(const IR::ListExpression *expression) override;
124 void postorder(const IR::StructExpression *expression) override;
125 void postorder(const IR::Operation_Unary *expression) override;
126 void postorder(const IR::PathExpression *expression) override;
127 void postorder(const IR::StringLiteral *expression) override;
128 void postorder(const IR::TypeNameExpression *expression) override;
129 void postorder(const IR::Expression *expression) override;
130 void mapExpression(const IR::Expression *expression, Util::IJson *json);
131
132 private:
133 void binary(const IR::Operation_Binary *expression);
134 void saturated_binary(const IR::Operation_Binary *expression);
135};
136
137} // namespace P4::BMV2
138
139#endif /* BACKENDS_BMV2_COMMON_EXPRESSION_H_ */
Definition expression.h:37
Definition expression.h:51
virtual Util::IJson * convertParam(const IR::Parameter *param, cstring fieldName)=0
const IR::Parameter * enclosingParamReference(const IR::Expression *expression)
Non-null if the expression refers to a parameter from the enclosing control.
Definition backends/bmv2/common/expression.cpp:237
bool simpleExpressionsOnly
Definition expression.h:82
Util::IJson * convert(const IR::Expression *e, bool doFixup=true, bool wrap=true, bool convertBool=false)
Definition backends/bmv2/common/expression.cpp:768
Definition node.h:95
Definition visitor.h:400
Definition coreLibrary.h:103
Definition backends/common/programStructure.h:32
Class used to encode maps from paths to declarations.
Definition referenceMap.h:66
Definition visitor.h:424
Definition typeMap.h:41
Definition json.h:40
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:21