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