P4C
The P4 Compiler
Loading...
Searching...
No Matches
common/expressions.h
1#ifndef BACKENDS_P4TOOLS_MODULES_SMITH_COMMON_EXPRESSIONS_H_
2#define BACKENDS_P4TOOLS_MODULES_SMITH_COMMON_EXPRESSIONS_H_
3
4#include <cstddef>
5#include <cstdint>
6#include <vector>
7
8#include "backends/p4tools/modules/smith/common/generator.h"
9#include "backends/p4tools/modules/smith/util/util.h"
10#include "ir/indexed_vector.h"
11#include "ir/ir.h"
12#include "ir/vector.h"
13#include "lib/cstring.h"
14
15namespace P4::P4Tools::P4Smith {
16
17using TyperefProbs = struct TyperefProbs {
18 int64_t p4_bit;
19 int64_t p4_signed_bit;
20 int64_t p4_varbit;
21 int64_t p4_int;
22 int64_t p4_error;
23 int64_t p4_bool;
24 int64_t p4_string;
25 // derived types
26 int64_t p4_enum;
27 int64_t p4_header;
28 int64_t p4_header_stack;
29 int64_t p4_struct;
30 int64_t p4_header_union;
31 int64_t p4_tuple;
32 int64_t p4_void;
33 int64_t p4_match_kind;
34
35 [[nodiscard]] std::string toString() const {
36 std::stringstream ss;
37 ss << "p4_bit=" << p4_bit << " p4_signed_bit=" << p4_signed_bit
38 << " p4_varbit=" << p4_varbit << " p4_int=" << p4_int << " p4_error=" << p4_error
39 << " p4_bool=" << p4_bool << " p4_string=" << p4_string << " p4_enum=" << p4_enum
40 << " p4_header=" << p4_header << " p4_header_stack=" << p4_header_stack
41 << " p4_struct=" << p4_struct << " p4_header_union=" << p4_header_union
42 << " p4_tuple=" << p4_tuple << " p4_void=" << p4_void
43 << " p4_match_kind=" << p4_match_kind;
44 return ss.str();
45 }
46};
47
49 public:
50 virtual ~ExpressionGenerator() = default;
51 explicit ExpressionGenerator(const SmithTarget &target) : Generator(target) {}
52
53 static constexpr size_t MAX_DEPTH = 3;
54
55 static const IR::Type_Boolean *genBoolType();
56
57 static const IR::Type_InfInt *genIntType();
58
59 // isSigned, true -> int<>, false -> bit<>
60 [[nodiscard]] const IR::Type_Bits *genBitType(bool isSigned) const;
61 [[nodiscard]] const IR::Type *pickRndBaseType(const std::vector<int64_t> &type_probs) const;
62
63 [[nodiscard]] virtual const IR::Type *pickRndType(TyperefProbs type_probs);
64
65 static IR::BoolLiteral *genBoolLiteral();
66
67 static IR::Constant *genIntLiteral(size_t bit_width = INTEGER_WIDTH);
68
69 static IR::Constant *genBitLiteral(const IR::Type *tb);
70
71 [[nodiscard]] virtual std::vector<int> availableBitWidths() const {
72 return {4, 8, 16, 32, 64, 128};
73 }
74
75 private:
76 IR::Expression *constructUnaryExpr(const IR::Type_Bits *tb);
77
78 IR::Expression *createSaturationOperand(const IR::Type_Bits *tb);
79
80 IR::Expression *constructBinaryBitExpr(const IR::Type_Bits *tb);
81
82 IR::Expression *constructTernaryBitExpr(const IR::Type_Bits *tb);
83
84 public:
85 virtual IR::Expression *pickBitVar(const IR::Type_Bits *tb);
86
87 virtual IR::Expression *constructBitExpr(const IR::Type_Bits *tb);
88
89 private:
90 IR::Expression *constructCmpExpr();
91
92 public:
93 virtual IR::Expression *constructBooleanExpr();
94
95 private:
96 IR::Expression *constructUnaryIntExpr();
97
98 IR::Expression *constructBinaryIntExpr();
99
100 static IR::Expression *pickIntVar();
101
102 public:
103 IR::Expression *constructIntExpr();
104
105 private:
106 IR::ListExpression *genStructListExpr(const IR::Type_Name *tn);
107
108 IR::Expression *editHdrStack(cstring lval);
109
110 public:
111 virtual IR::Expression *constructStructExpr(const IR::Type_Name *tn);
112
113 virtual IR::MethodCallExpression *genFunctionCall(cstring method_name,
114 IR::ParameterList params);
115
116 virtual IR::MethodCallExpression *pickFunction(
117 IR::IndexedVector<IR::Declaration> viable_functions, const IR::Type **ret_type);
118
119 virtual IR::Expression *genExpression(const IR::Type *tp);
120
121 virtual IR::ListExpression *genExpressionList(IR::Vector<IR::Type> types, bool only_lval);
122
123 virtual IR::Expression *genInputArg(const IR::Parameter *param);
124
125 virtual IR::Expression *pickLvalOrSlice(const IR::Type *tp);
126
127 virtual bool checkInputArg(const IR::Parameter *param);
128};
129
130} // namespace P4::P4Tools::P4Smith
131
132#endif /* BACKENDS_P4TOOLS_MODULES_SMITH_COMMON_EXPRESSIONS_H_ */
Definition node.h:52
Definition vector.h:58
Definition common/expressions.h:48
Definition generator.h:10
Definition p4tools/modules/smith/core/target.h:17
Definition cstring.h:85
const char * toString(const z3::expr &e)
Converts a Z3 expression to a string.
Definition z3_solver.cpp:32