P4C
The P4 Compiler
Loading...
Searching...
No Matches
modules/testgen/test/small-step/util.h
1#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TEST_SMALL_STEP_UTIL_H_
2#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TEST_SMALL_STEP_UTIL_H_
3
4#include <gtest/gtest.h>
5
6#include <functional>
7#include <optional>
8#include <stack>
9#include <string>
10#include <utility>
11#include <vector>
12
13#include "backends/p4tools/common/compiler/compiler_target.h"
14#include "backends/p4tools/common/core/z3_solver.h"
15#include "backends/p4tools/common/lib/namespace_context.h"
16#include "backends/p4tools/common/lib/symbolic_env.h"
17#include "ir/declaration.h"
18#include "ir/indexed_vector.h"
19#include "ir/ir.h"
20#include "ir/vector.h"
21#include "lib/cstring.h"
22#include "lib/enumerator.h"
23
24#include "backends/p4tools/modules/testgen/core/small_step/small_step.h"
25#include "backends/p4tools/modules/testgen/core/target.h"
26#include "backends/p4tools/modules/testgen/lib/continuation.h"
27#include "backends/p4tools/modules/testgen/lib/execution_state.h"
28#include "backends/p4tools/modules/testgen/test/gtest_utils.h"
29
30namespace P4::Test {
31
32using Body = P4Tools::P4Testgen::Continuation::Body;
33using Continuation = P4Tools::P4Testgen::Continuation;
34using ExecutionState = P4Tools::P4Testgen::ExecutionState;
35using NamespaceContext = P4Tools::NamespaceContext;
36using Return = P4Tools::P4Testgen::Continuation::Return;
37using SmallStepEvaluator = P4Tools::P4Testgen::SmallStepEvaluator;
38using TestgenTarget = P4Tools::P4Testgen::TestgenTarget;
39using Z3Solver = P4Tools::Z3Solver;
40
41class SmallStepTest : public P4ToolsTest {
42 public:
44 static ExecutionState mkState(Body body) { return ExecutionState(std::move(body)); }
45};
46
47namespace SmallStepUtil {
48
49using namespace P4::literals;
50
53std::optional<const P4ToolsTestCase> createSmallStepExprTest(const std::string &,
54 const std::string &);
55
57template <class T>
58const T *extractExpr(const IR::P4Program &program) {
59 // Get the mau declarations in the P4Program.
60 auto *decl = program.getDeclsByName("mau"_cs)->single();
61
62 // Convert the mau declaration to a control and ensure that
63 // there is a single statement in the body.
64 const auto *control = decl->checkedTo<IR::P4Control>();
65 if (control->body->components.size() != 1) {
66 return nullptr;
67 }
68
69 // Ensure that the control body statement is a method call statement.
70 const auto *mcStmt = control->body->components[0]->to<IR::MethodCallStatement>();
71 if (!mcStmt) {
72 return nullptr;
73 }
74
75 // Ensure that there is only one argument to the method call and return it.
76 const auto *mcArgs = mcStmt->methodCall->arguments;
77 if (mcArgs->size() != 1) {
78 return nullptr;
79 }
80 return (*mcArgs)[0]->expression->to<T>();
81}
82
84template <class T>
85void stepAndExamineValue(const T *value, const P4Tools::CompilerResult &compilerResult) {
86 // Produce a ProgramInfo, which is needed to create a SmallStepEvaluator.
87 const auto *progInfo = TestgenTarget::produceProgramInfo(compilerResult);
88 ASSERT_TRUE(progInfo);
89
90 // Create a base state with a parameter continuation to apply the value on.
91 const auto *v = Continuation::genParameter(value->type, "v"_cs, NamespaceContext::Empty);
92 Body bodyBase({Return(v->param)});
93 Continuation continuationBase(v, bodyBase);
94 ExecutionState esBase = SmallStepTest::mkState(bodyBase);
95
96 // Step on the value.
97 Z3Solver solver;
98 auto &testState = esBase.clone();
99 Body body({Return(value)});
100 testState.replaceBody(body);
101 testState.pushContinuation(
102 *new ExecutionState::StackFrame(continuationBase, esBase.getNamespaceContext()));
103 SmallStepEvaluator eval(solver, *progInfo);
104 auto *successors = eval.step(testState);
105 ASSERT_EQ(successors->size(), 1u);
106
107 // Examine the resulting execution state.
108 const auto branch = (*successors)[0];
109 const auto *constraint = branch.constraint;
110 auto executionState = branch.nextState;
111 ASSERT_TRUE(constraint->checkedTo<IR::BoolLiteral>()->value);
112 ASSERT_EQ(executionState.get().getNamespaceContext(), NamespaceContext::Empty);
113 ASSERT_TRUE(executionState.get().getSymbolicEnv().getInternalMap().empty());
114
115 // Examine the resulting body.
116 Body finalBody = executionState.get().getBody();
117 ASSERT_EQ(finalBody, Body({Return(value)}));
118
119 // Examine the resulting stack.
120 ASSERT_EQ(executionState.get().getStack().empty(), true);
121}
122
127template <class T>
128void stepAndExamineOp(
129 const T *op, const IR::Expression *subexpr, const P4Tools::CompilerResult &compilerResult,
130 std::function<const IR::Expression *(const IR::PathExpression *)> rebuildNode) {
131 // Produce a ProgramInfo, which is needed to create a SmallStepEvaluator.
132 const auto *progInfo = TestgenTarget::produceProgramInfo(compilerResult);
133 ASSERT_TRUE(progInfo);
134
135 // Step on the operation.
136 Z3Solver solver;
137 Body body({Return(op)});
138 ExecutionState es = SmallStepTest::mkState(body);
139 SmallStepEvaluator eval(solver, *progInfo);
140 auto *successors = eval.step(es);
141 ASSERT_EQ(successors->size(), 1U);
142
143 // Examine the resulting execution state.
144 const auto branch = (*successors)[0];
145 const auto *constraint = branch.constraint;
146 auto executionState = branch.nextState;
147 ASSERT_TRUE(constraint->checkedTo<IR::BoolLiteral>()->value);
148 ASSERT_EQ(executionState.get().getNamespaceContext(), NamespaceContext::Empty);
149 ASSERT_TRUE(executionState.get().getSymbolicEnv().getInternalMap().empty());
150
151 // Examine the resulting body.
152 Body finalBody = executionState.get().getBody();
153 ASSERT_EQ(finalBody, Body({Return(subexpr)}));
154
155 // Examine the resulting stack.
156 ASSERT_EQ(executionState.get().getStack().size(), 1u);
157 const auto stackFrame = executionState.get().getStack().top();
158 ASSERT_TRUE(stackFrame.get().getExceptionHandlers().empty());
159 ASSERT_EQ(stackFrame.get().getNameSpaces(), NamespaceContext::Empty);
160
161 // Examine the pushed continuation.
162 Continuation pushedContinuation = stackFrame.get().getContinuation();
163 ASSERT_TRUE(pushedContinuation.parameterOpt);
164 Body pushedBody = pushedContinuation.body;
165 ASSERT_EQ(pushedBody, Body({Return(rebuildNode(*pushedContinuation.parameterOpt))}));
166}
167
168} // namespace SmallStepUtil
169
170} // namespace P4::Test
171
172#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TEST_SMALL_STEP_UTIL_H_ */
static const NamespaceContext * Empty
Represents the empty namespace context.
Definition namespace_context.h:33
static const Parameter * genParameter(const IR::Type *type, cstring name, const NamespaceContext *ctx)
Definition continuation.cpp:166
Definition p4tools/modules/testgen/core/target.h:19
static const ProgramInfo * produceProgramInfo(const CompilerResult &compilerResult)
Definition p4tools/modules/testgen/core/target.cpp:49
A Z3-based implementation of AbstractSolver. Encapsulates a z3::solver and a z3::context.
Definition z3_solver.h:28
GTest for P4 Tools tests.
Definition gtest_utils.h:52
Definition modules/testgen/test/small-step/util.h:41
static ExecutionState mkState(Body body)
Creates an execution state out of a continuation body.
Definition modules/testgen/test/small-step/util.h:44
Definition cstring.h:80