P4C
The P4 Compiler
Loading...
Searching...
No Matches
core/small_step/expr_stepper.h
1/*
2 * SPDX-FileCopyrightText: 2022 The P4 Language Consortium
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_SMALL_STEP_EXPR_STEPPER_H_
8#define BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_SMALL_STEP_EXPR_STEPPER_H_
9
10#include <cstdint>
11#include <utility>
12#include <vector>
13
14#include "ir/ir.h"
15#include "ir/irutils.h"
16#include "ir/solver.h"
17#include "lib/cstring.h"
18
19#include "backends/p4tools/modules/testgen/core/extern_info.h"
20#include "backends/p4tools/modules/testgen/core/program_info.h"
21#include "backends/p4tools/modules/testgen/core/small_step/abstract_stepper.h"
22#include "backends/p4tools/modules/testgen/lib/execution_state.h"
23
24namespace P4::P4Tools::P4Testgen {
25
27class ExprStepper : public AbstractStepper {
28 /**************************************************************************************************
29 ExternMethodImpls
30 **************************************************************************************************/
31 public:
33 template <typename StepperType>
34 class ExternMethodImpls {
35 public:
41 using MethodImpl = std::function<void(const ExternInfo &externInfo, StepperType &stepper)>;
42
55 std::optional<MethodImpl> find(const IR::PathExpression &externObjectRef,
56 const IR::ID &methodName,
57 const IR::Vector<IR::Argument> &args) const {
58 // Extern functions and internal methods use a placeholder receiver, such as "*method".
59 // This placeholder also has an extern type.
60 const auto *externType = IR::getExternType(externObjectRef.type);
61 BUG_CHECK(externType != nullptr,
62 "Not a valid extern: %1% with member %2%. Type is %3%.", externObjectRef,
63 methodName, externObjectRef.type->node_type_name());
64
65 cstring qualifiedMethodName = externType->name + "." + methodName;
66 auto submapIt = impls.find(qualifiedMethodName);
67 if (submapIt == impls.end()) {
68 return std::nullopt;
69 }
70 if (submapIt->second.count(args.size()) == 0) {
71 return std::nullopt;
72 }
73
74 // Find matching methods: if any arguments are named, then the parameter name must
75 // match.
76 std::optional<MethodImpl> matchingImpl;
77 for (const auto &pair : submapIt->second.at(args.size())) {
78 const auto &paramNames = pair.first;
79 const auto &methodImpl = pair.second;
80
81 if (matches(paramNames, args)) {
82 BUG_CHECK(!matchingImpl, "Ambiguous extern method call: %1%",
83 qualifiedMethodName);
84 matchingImpl = methodImpl;
85 }
86 }
87
88 return matchingImpl;
89 }
90
91 private:
96 std::map<cstring,
97 std::map<size_t, std::vector<std::pair<std::vector<cstring>, MethodImpl>>>>
98 impls;
99
103 static bool matches(const std::vector<cstring> &paramNames,
104 const IR::Vector<IR::Argument> &args) {
105 // Number of parameters should match the number of arguments.
106 if (paramNames.size() != args.size()) {
107 return false;
108 }
109 // Any named argument should match the name of the corresponding parameter.
110 for (size_t idx = 0; idx < paramNames.size(); idx++) {
111 const auto &paramName = paramNames.at(idx);
112 const auto &arg = args.at(idx);
113
114 if (arg->name.name == nullptr) {
115 continue;
116 }
117 if (paramName != arg->name.name) {
118 return false;
119 }
120 }
121
122 return true;
123 }
124
125 public:
131 using ImplList = std::list<std::tuple<cstring, std::vector<cstring>, MethodImpl>>;
132
133 explicit ExternMethodImpls(const ImplList &implList) {
134 for (const auto &implSpec : implList) {
135 auto &[name, paramNames, impl] = implSpec;
136
137 auto &tmpImplList = impls[name][paramNames.size()];
138
139 // Make sure that we have at most one implementation for each set of parameter
140 // names. This is a quadratic-time algorithm, but should be fine, since we expect
141 // the number of overloads to be small in practice.
142 for (auto &pair : tmpImplList) {
143 BUG_CHECK(pair.first != paramNames, "Multiple implementations of %1%(%2%)",
144 name, paramNames);
145 }
146
147 tmpImplList.emplace_back(paramNames, impl);
148 }
149 }
150 };
151
154
157
158 /**************************************************************************************************
159 ExprStepper
160 **************************************************************************************************/
161
162 private:
164 friend class TableStepper;
165
167 friend class ExtractUtils;
168
169 protected:
175
177 const IR::Expression *advanceCond;
178
181
183 const IR::Expression *advanceFailCond;
184 };
185
190 int advanceSize) const;
196 const ExecutionState &state, const IR::Expression *advanceExpr,
197 const IR::Expression *restrictions) const;
198
202 static std::vector<std::pair<IR::StateVariable, const IR::Expression *>> setFields(
203 ExecutionState &nextState, const std::vector<IR::StateVariable> &flatFields,
204 int varBitFieldSize);
209 void handleHitMissActionRun(const IR::Member *member);
210
214 bool resolveMethodCallArguments(const IR::MethodCallExpression *call);
215
227 virtual void evalExternMethodCall(const ExternInfo &externInfo);
228
241 void evalInternalExternMethodCall(const ExternInfo &externInfo);
242
250 void evalActionCall(const IR::P4Action *action, const IR::MethodCallExpression *call);
251
256 // Otherwise, the target default value is chosen.
258 void generateCopyIn(ExecutionState &nextState, const IR::StateVariable &targetPath,
259 const IR::StateVariable &srcPath, cstring dir, bool forceTaint) const;
260
264 virtual void stepNoMatch(std::string traceLog, const IR::Expression *condition = nullptr);
265
266 public:
267 ExprStepper(const ExprStepper &) = default;
268
269 ExprStepper(ExprStepper &&) = default;
270
271 ExprStepper &operator=(const ExprStepper &) = delete;
272
273 ExprStepper &operator=(ExprStepper &&) = delete;
274
276
277 bool preorder(const IR::BoolLiteral *boolLiteral) override;
278 bool preorder(const IR::Constant *constant) override;
279 bool preorder(const IR::Member *member) override;
280 bool preorder(const IR::ArrayIndex *arr) override;
281 bool preorder(const IR::MethodCallExpression *call) override;
282 bool preorder(const IR::Mux *mux) override;
283 bool preorder(const IR::PathExpression *pathExpression) override;
284
288 bool preorder(const IR::P4ValueSet *valueSet) override;
289 bool preorder(const IR::Operation_Binary *binary) override;
290 bool preorder(const IR::Operation_Unary *unary) override;
291 bool preorder(const IR::SelectExpression *selectExpression) override;
292 bool preorder(const IR::BaseListExpression *listExpression) override;
293 bool preorder(const IR::StructExpression *structExpression) override;
294 bool preorder(const IR::AbstractSlice *slice) override;
295 bool preorder(const IR::P4Table *table) override;
296};
297
298} // namespace P4::P4Tools::P4Testgen
299
300#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_CORE_SMALL_STEP_EXPR_STEPPER_H_ */
Provides a higher-level interface for an SMT solver.
Definition solver.h:30
Definition ir/vector.h:50
ExecutionState & state
The state being evaluated.
Definition abstract_stepper.h:55
const ProgramInfo & programInfo
Target-specific information about the P4 program being evaluated.
Definition abstract_stepper.h:52
Represents state of execution after having reached a program point.
Definition execution_state.h:40
Encapsulates a set of extern method implementations.
Definition core/small_step/expr_stepper.h:34
std::optional< MethodImpl > find(const IR::PathExpression &externObjectRef, const IR::ID &methodName, const IR::Vector< IR::Argument > &args) const
Definition core/small_step/expr_stepper.h:55
std::list< std::tuple< cstring, std::vector< cstring >, MethodImpl > > ImplList
Definition core/small_step/expr_stepper.h:131
std::function< void(const ExternInfo &externInfo, StepperType &stepper)> MethodImpl
Definition core/small_step/expr_stepper.h:41
int advanceFailSize
Specifies at what point the parser cursor advancement will fail.
Definition core/small_step/expr_stepper.h:180
virtual PacketCursorAdvanceInfo calculateSuccessfulParserAdvance(const ExecutionState &state, int advanceSize) const
Definition extern_stepper.cpp:99
void evalInternalExternMethodCall(const ExternInfo &externInfo)
Definition extern_stepper.cpp:331
int advanceSize
How much the parser cursor will be advanced in a successful parsing case.
Definition core/small_step/expr_stepper.h:174
static std::vector< std::pair< IR::StateVariable, const IR::Expression * > > setFields(ExecutionState &nextState, const std::vector< IR::StateVariable > &flatFields, int varBitFieldSize)
Definition extern_stepper.cpp:48
friend class ExtractUtils
Extract utils may access some protected members of the expression stepper.
Definition core/small_step/expr_stepper.h:167
virtual void evalExternMethodCall(const ExternInfo &externInfo)
Definition extern_stepper.cpp:923
void evalActionCall(const IR::P4Action *action, const IR::MethodCallExpression *call)
Definition core/small_step/expr_stepper.cpp:102
virtual void stepNoMatch(std::string traceLog, const IR::Expression *condition=nullptr)
Definition core/small_step/expr_stepper.cpp:519
void handleHitMissActionRun(const IR::Member *member)
Definition core/small_step/expr_stepper.cpp:55
static const ExprStepper::ExternMethodImpls< ExprStepper > CORE_EXTERN_METHOD_IMPLS
Provides implementations of all known extern methods built into P4 core.
Definition core/small_step/expr_stepper.h:156
friend class TableStepper
We delegate evaluation to the TableStepper, which needs to access protected members.
Definition core/small_step/expr_stepper.h:164
void generateCopyIn(ExecutionState &nextState, const IR::StateVariable &targetPath, const IR::StateVariable &srcPath, cstring dir, bool forceTaint) const
TODO: Consolidate this into the copy_in_out extern.
const IR::Expression * advanceFailCond
The condition that needs to be satisfied for the advance/extract to be rejected.
Definition core/small_step/expr_stepper.h:183
virtual PacketCursorAdvanceInfo calculateAdvanceExpression(const ExecutionState &state, const IR::Expression *advanceExpr, const IR::Expression *restrictions) const
Definition extern_stepper.cpp:111
const IR::Expression * advanceCond
The condition that needs to be satisfied to successfully advance the parser cursor.
Definition core/small_step/expr_stepper.h:177
static const ExprStepper::ExternMethodImpls< ExprStepper > INTERNAL_EXTERN_METHOD_IMPLS
Definitions of internal helper functions.
Definition core/small_step/expr_stepper.h:153
bool resolveMethodCallArguments(const IR::MethodCallExpression *call)
Definition core/small_step/expr_stepper.cpp:129
Definition core/small_step/expr_stepper.h:172
Definition extern_info.h:20
Stores target-specific information about a P4 program.
Definition core/program_info.h:27
Definition cstring.h:76
const Type_Extern * getExternType(const Type *type)
Definition irutils.cpp:30
Definition phv/solver/action_constraint_solver.cpp:34
Definition id.h:19