P4C
The P4 Compiler
Loading...
Searching...
No Matches
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
36class ExpressionGenerator : public Generator {
37 public:
38 virtual ~ExpressionGenerator() = default;
39 explicit ExpressionGenerator(const SmithTarget &target) : Generator(target) {}
40
41 static constexpr size_t MAX_DEPTH = 3;
42
43 static constexpr int BIT_WIDTHS[6] = {4, 8, 16, 32, 64, 128};
44
45 static const IR::Type_Boolean *genBoolType();
46
47 static const IR::Type_InfInt *genIntType();
48
49 // isSigned, true -> int<>, false -> bit<>
50 static const IR::Type_Bits *genBitType(bool isSigned);
51
52 static const IR::Type *pickRndBaseType(const std::vector<int64_t> &type_probs);
53
54 virtual const IR::Type *pickRndType(TyperefProbs type_probs);
55
56 static IR::BoolLiteral *genBoolLiteral();
57
58 static IR::Constant *genIntLiteral(size_t bit_width = INTEGER_WIDTH);
59
60 static IR::Constant *genBitLiteral(const IR::Type *tb);
61
62 private:
63 IR::Expression *constructUnaryExpr(const IR::Type_Bits *tb);
64
65 IR::Expression *createSaturationOperand(const IR::Type_Bits *tb);
66
67 IR::Expression *constructBinaryBitExpr(const IR::Type_Bits *tb);
68
69 IR::Expression *constructTernaryBitExpr(const IR::Type_Bits *tb);
70
71 public:
72 virtual IR::Expression *pickBitVar(const IR::Type_Bits *tb);
73
74 virtual IR::Expression *constructBitExpr(const IR::Type_Bits *tb);
75
76 private:
77 IR::Expression *constructCmpExpr();
78
79 public:
80 virtual IR::Expression *constructBooleanExpr();
81
82 private:
83 IR::Expression *constructUnaryIntExpr();
84
85 IR::Expression *constructBinaryIntExpr();
86
87 static IR::Expression *pickIntVar();
88
89 public:
90 IR::Expression *constructIntExpr();
91
92 private:
93 IR::ListExpression *genStructListExpr(const IR::Type_Name *tn);
94
95 IR::Expression *editHdrStack(cstring lval);
96
97 public:
98 virtual IR::Expression *constructStructExpr(const IR::Type_Name *tn);
99
100 virtual IR::MethodCallExpression *genFunctionCall(cstring method_name,
101 IR::ParameterList params);
102
103 virtual IR::MethodCallExpression *pickFunction(
104 IR::IndexedVector<IR::Declaration> viable_functions, const IR::Type **ret_type);
105
106 virtual IR::Expression *genExpression(const IR::Type *tp);
107
108 virtual IR::ListExpression *genExpressionList(IR::Vector<IR::Type> types, bool only_lval);
109
110 virtual IR::Expression *genInputArg(const IR::Parameter *param);
111
112 virtual IR::Expression *pickLvalOrSlice(const IR::Type *tp);
113
114 virtual bool checkInputArg(const IR::Parameter *param);
115};
116
117} // namespace P4::P4Tools::P4Smith
118
119#endif /* BACKENDS_P4TOOLS_MODULES_SMITH_COMMON_EXPRESSIONS_H_ */
Definition indexed_vector.h:40
Definition vector.h:58
Definition p4tools/modules/smith/core/target.h:17
Definition cstring.h:85