P4C
The P4 Compiler
Loading...
Searching...
No Matches
continuation.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_LIB_CONTINUATION_H_
8#define BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_CONTINUATION_H_
9
10#include <cstdint>
11#include <deque>
12#include <initializer_list>
13#include <iosfwd>
14#include <map>
15#include <optional>
16#include <string>
17#include <utility>
18#include <variant>
19#include <vector>
20
21#include "backends/p4tools/common/lib/namespace_context.h"
22#include "backends/p4tools/common/lib/trace_event.h"
23#include "ir/ir.h"
24#include "ir/node.h"
25#include "lib/cstring.h"
26
27namespace P4::P4Tools::Test {
28class SmallStepTest;
29} // namespace P4::P4Tools::Test
30
31namespace P4::P4Tools::P4Testgen {
32
36 public:
38 //
39 // TODO: Add more as needed. At least, we'd probably want to use this to model parser
40 // exceptions.
55
59 friend std::ostream &operator<<(std::ostream &out, const Exception value) {
60 static std::map<Exception, std::string> strings;
61 if (strings.empty()) {
62#define INSERT_ELEMENT(p) strings[p] = #p
63 INSERT_ELEMENT(Exception::Exit);
64 INSERT_ELEMENT(Exception::NoMatch);
65 INSERT_ELEMENT(Exception::Reject);
66 INSERT_ELEMENT(Exception::Drop);
67 INSERT_ELEMENT(Exception::Abort);
68#undef INSERT_ELEMENT
69 }
70
71 return out << strings[value];
72 }
73
79 struct Return {
80 std::optional<const IR::Node *> expr;
81
83 bool operator==(const Return &other) const;
84
85 Return() : expr(std::nullopt) {}
86 explicit Return(const IR::Node *expr);
87 };
88
91 using PropertyValue = std::variant<cstring, uint64_t, int64_t, bool, const IR::Expression *>;
92
93 struct PropertyUpdate {
98
100 bool operator==(const PropertyUpdate &other) const;
101
102 explicit PropertyUpdate(cstring propertyName, PropertyValue property);
103 };
104
110 struct Guard {
111 const IR::Expression *cond;
112
114 bool operator==(const Guard &other) const;
115
116 explicit Guard(const IR::Expression *cond);
117 };
118
119 using Command = std::variant<
121 const IR::Node *,
123 const TraceEvent *,
126 Return,
128 Exception,
132 Guard>;
133
135 class Body {
136 friend class Continuation;
137 friend class Test::SmallStepTest;
138
139 std::deque<Command> cmds;
140
141 public:
143 bool empty() const;
144
147 const Command next() const;
148
150 void push(Command cmd);
151
154 void pop();
155
157 void clear();
158
159 bool operator==(const Body &) const;
160
162 Body(std::initializer_list<Command> cmds);
163
166 explicit Body(const std::vector<Command> &cmds);
167
168 private:
169 Body() = default;
170 };
171
174 class Parameter {
175 friend Continuation;
176
177 public:
178 const IR::PathExpression *param;
179
180 private:
181 explicit Parameter(const IR::PathExpression *param) : param(param) {}
182 };
183
185 //
186 // Invariant: this parameter is uniquely named.
187 std::optional<const IR::PathExpression *> parameterOpt;
188 Body body;
189
196 Body apply(std::optional<const IR::Node *> value_opt) const;
197
200 static const Parameter *genParameter(const IR::Type *type, cstring name,
201 const NamespaceContext *ctx);
202
204 explicit Continuation(Body body) : Continuation(std::nullopt, std::move(body)) {}
205
208 Continuation(std::optional<const Parameter *> parameterOpt, Body body)
209 : parameterOpt(parameterOpt ? std::make_optional((*parameterOpt)->param) : std::nullopt),
210 body(std::move(body)) {}
211};
212
213} // namespace P4::P4Tools::P4Testgen
214
215#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_CONTINUATION_H_ */
Definition node.h:53
Represents a stack of namespaces.
Definition namespace_context.h:20
A continuation body is a list of commands.
Definition continuation.h:135
void clear()
Removes all commands in this body.
Definition continuation.cpp:54
void push(Command cmd)
Pushes the given command onto the command stack.
Definition continuation.cpp:47
Body(std::initializer_list< Command > cmds)
Allows the command stack to be initialized with a list initializer.
Definition continuation.cpp:60
const Command next() const
Definition continuation.cpp:39
bool empty() const
Determines whether this body is empty.
Definition continuation.cpp:37
void pop()
Definition continuation.cpp:49
Body apply(std::optional< const IR::Node * > value_opt) const
Definition continuation.cpp:95
friend std::ostream & operator<<(std::ostream &out, const Exception value)
Definition continuation.h:59
Exception
Enumerates the exceptions that can be thrown during symbolic execution.
Definition continuation.h:41
@ PacketTooShort
Thrown on premature packet end. Models P4's error.PacketTooShort.
Definition continuation.h:51
@ Drop
This is an internal interpreter exception to express drop semantics.
Definition continuation.h:49
@ Abort
Thrown when the target terminates.
Definition continuation.h:53
@ NoMatch
Thrown when a select expression fails to match. This models P4's error.NoMatch.
Definition continuation.h:45
@ Reject
Thrown when the parser reaches the reject state.
Definition continuation.h:47
@ Exit
Thrown when an exit statement is encountered.
Definition continuation.h:43
std::optional< const IR::PathExpression * > parameterOpt
Represents the continuation's parameter.
Definition continuation.h:187
Continuation(std::optional< const Parameter * > parameterOpt, Body body)
Definition continuation.h:208
static const Parameter * genParameter(const IR::Type *type, cstring name, const NamespaceContext *ctx)
Definition continuation.cpp:171
Continuation(Body body)
Creates a parameterless continuation.
Definition continuation.h:204
std::variant< cstring, uint64_t, int64_t, bool, const IR::Expression * > PropertyValue
Definition continuation.h:91
Definition modules/testgen/test/small-step/util.h:46
An event in a trace of the execution of a P4 program.
Definition trace_event.h:20
Definition cstring.h:85
STL namespace.
Definition continuation.h:110
bool operator==(const Guard &other) const
Delegates to IR equality.
Definition continuation.cpp:33
cstring propertyName
The name of the property that is being set.
Definition continuation.h:95
bool operator==(const PropertyUpdate &other) const
Delegates to the equality of the property.
Definition continuation.cpp:27
PropertyValue property
The value of the property.
Definition continuation.h:97
bool operator==(const Return &other) const
Delegates to IR equality.
Definition continuation.cpp:20