P4C
The P4 Compiler
Loading...
Searching...
No Matches
expression.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef BACKENDS_BMV2_COMMON_EXPRESSION_H_
9#define BACKENDS_BMV2_COMMON_EXPRESSION_H_
10
11#include "backends/common/programStructure.h"
12#include "frontends/common/resolveReferences/referenceMap.h"
13#include "frontends/p4/coreLibrary.h"
14#include "frontends/p4/enumInstance.h"
15#include "frontends/p4/methodInstance.h"
16#include "frontends/p4/typeMap.h"
17#include "ir/ir.h"
18#include "lib/big_int_util.h"
19#include "lib/json.h"
20#include "lower.h"
21
22namespace P4::BMV2 {
23
28class ArithmeticFixup : public Transform {
29 P4::TypeMap *typeMap;
30
31 public:
32 const IR::Expression *fix(const IR::Expression *expr, const IR::Type_Bits *type);
33 const IR::Node *updateType(const IR::Expression *expression);
34 const IR::Node *postorder(IR::Expression *expression) override;
35 const IR::Node *postorder(IR::Operation_Binary *expression) override;
36 const IR::Node *postorder(IR::Neg *expression) override;
37 const IR::Node *postorder(IR::Cmpl *expression) override;
38 const IR::Node *postorder(IR::Cast *expression) override;
39 explicit ArithmeticFixup(P4::TypeMap *typeMap) : typeMap(typeMap) { CHECK_NULL(typeMap); }
40};
41
42class ExpressionConverter : public Inspector {
43 P4::ReferenceMap *refMap;
44 P4::TypeMap *typeMap;
45 P4::ProgramStructure *structure;
46 P4::P4CoreLibrary &corelib;
47 cstring scalarsName;
48
54 std::map<const IR::Expression *, Util::IJson *> map;
55 bool leftValue; // true if converting a left value
56 // in some cases the bmv2 JSON requires a 'bitwidth' attribute for hex
57 // strings (e.g. for constants in calculation inputs). When this flag is set
58 // to true, we add this attribute.
59 bool withConstantWidths{false};
60
61 public:
62 ExpressionConverter(P4::ReferenceMap *refMap, P4::TypeMap *typeMap,
63 P4::ProgramStructure *structure, cstring scalarsName)
64 : refMap(refMap),
65 typeMap(typeMap),
66 structure(structure),
67 corelib(P4::P4CoreLibrary::instance()),
68 scalarsName(scalarsName),
69 leftValue(false),
70 simpleExpressionsOnly(false) {}
74
76 const IR::Parameter *enclosingParamReference(const IR::Expression *expression);
77
83 virtual Util::IJson *convertParam(const IR::Parameter *param, cstring fieldName) = 0;
84
85 Util::IJson *get(const IR::Expression *expression) const;
86 Util::IJson *fixLocal(Util::IJson *json);
87
96 Util::IJson *convert(const IR::Expression *e, bool doFixup = true, bool wrap = true,
97 bool convertBool = false);
98 Util::IJson *convertLeftValue(const IR::Expression *e);
99 Util::IJson *convertWithConstantWidths(const IR::Expression *e);
100 bool isArrayIndexRuntime(const IR::Expression *e);
101
102 void postorder(const IR::BoolLiteral *expression) override;
103 void postorder(const IR::MethodCallExpression *expression) override;
104 void postorder(const IR::Cast *expression) override;
105 void postorder(const IR::Slice *expression) override;
106 void postorder(const IR::AddSat *expression) override { saturated_binary(expression); }
107 void postorder(const IR::SubSat *expression) override { saturated_binary(expression); }
108 void postorder(const IR::Constant *expression) override;
109 void postorder(const IR::ArrayIndex *expression) override;
110 void postorder(const IR::Member *expression) override;
111 void postorder(const IR::Mux *expression) override;
112 void postorder(const IR::IntMod *expression) override;
113 void postorder(const IR::Operation_Binary *expression) override;
114 void postorder(const IR::ListExpression *expression) override;
115 void postorder(const IR::StructExpression *expression) override;
116 void postorder(const IR::Operation_Unary *expression) override;
117 void postorder(const IR::PathExpression *expression) override;
118 void postorder(const IR::StringLiteral *expression) override;
119 void postorder(const IR::TypeNameExpression *expression) override;
120 void postorder(const IR::Expression *expression) override;
121 void mapExpression(const IR::Expression *expression, Util::IJson *json);
122
123 private:
124 void binary(const IR::Operation_Binary *expression);
125 void saturated_binary(const IR::Operation_Binary *expression);
126};
127
128} // namespace P4::BMV2
129
130#endif /* BACKENDS_BMV2_COMMON_EXPRESSION_H_ */
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:227
bool simpleExpressionsOnly
Definition expression.h:73
Util::IJson * convert(const IR::Expression *e, bool doFixup=true, bool wrap=true, bool convertBool=false)
Definition backends/bmv2/common/expression.cpp:758
Definition node.h:53
Definition coreLibrary.h:103
Definition backends/common/programStructure.h:23
Class used to encode maps from paths to declarations.
Definition referenceMap.h:67
Definition typeMap.h:32
Definition lib/json.h:41
Definition cstring.h:85
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition action.cpp:9
Definition bson.cpp:69