P4C
The P4 Compiler
Loading...
Searching...
No Matches
continuation.h
1#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_CONTINUATION_H_
2#define BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_CONTINUATION_H_
3
4#include <cstdint>
5#include <deque>
6#include <initializer_list>
7#include <iosfwd>
8#include <map>
9#include <optional>
10#include <string>
11#include <utility>
12#include <variant>
13#include <vector>
14
15#include "backends/p4tools/common/lib/namespace_context.h"
16#include "backends/p4tools/common/lib/trace_event.h"
17#include "ir/ir.h"
18#include "ir/node.h"
19#include "lib/cstring.h"
20
21namespace P4::P4Tools::Test {
22class SmallStepTest;
23} // namespace P4::P4Tools::Test
24
25namespace P4::P4Tools::P4Testgen {
26
30 public:
32 //
33 // TODO: Add more as needed. At least, we'd probably want to use this to model parser
34 // exceptions.
35 enum class Exception {
37 Exit,
39 NoMatch,
41 Reject,
43 Drop,
47 Abort,
48 };
49
53 friend std::ostream &operator<<(std::ostream &out, const Exception value) {
54 static std::map<Exception, std::string> strings;
55 if (strings.empty()) {
56#define INSERT_ELEMENT(p) strings[p] = #p
57 INSERT_ELEMENT(Exception::Exit);
58 INSERT_ELEMENT(Exception::NoMatch);
59 INSERT_ELEMENT(Exception::Reject);
60 INSERT_ELEMENT(Exception::Drop);
61 INSERT_ELEMENT(Exception::Abort);
62#undef INSERT_ELEMENT
63 }
64
65 return out << strings[value];
66 }
67
73 struct Return {
74 std::optional<const IR::Node *> expr;
75
77 bool operator==(const Return &other) const;
78
79 Return() : expr(std::nullopt) {}
80 explicit Return(const IR::Node *expr);
81 };
82
85 using PropertyValue = std::variant<cstring, uint64_t, int64_t, bool, const IR::Expression *>;
86
98
104 struct Guard {
105 const IR::Expression *cond;
106
108 bool operator==(const Guard &other) const;
109
110 explicit Guard(const IR::Expression *cond);
111 };
112
113 using Command = std::variant<
115 const IR::Node *,
117 const TraceEvent *,
120 Return,
122 Exception,
126 Guard>;
127
129 class Body {
130 friend class Continuation;
131 friend class Test::SmallStepTest;
132
133 std::deque<Command> cmds;
134
135 public:
137 bool empty() const;
138
141 const Command next() const;
142
144 void push(Command cmd);
145
148 void pop();
149
151 void clear();
152
153 bool operator==(const Body &) const;
154
156 Body(std::initializer_list<Command> cmds);
157
160 explicit Body(const std::vector<Command> &cmds);
161
162 private:
163 Body() = default;
164 };
165
168 class Parameter {
169 friend Continuation;
170
171 public:
172 const IR::PathExpression *param;
173
174 private:
175 explicit Parameter(const IR::PathExpression *param) : param(param) {}
176 };
177
179 //
180 // Invariant: this parameter is uniquely named.
181 std::optional<const IR::PathExpression *> parameterOpt;
182 Body body;
183
190 Body apply(std::optional<const IR::Node *> value_opt) const;
191
194 static const Parameter *genParameter(const IR::Type *type, cstring name,
195 const NamespaceContext *ctx);
196
198 explicit Continuation(Body body) : Continuation(std::nullopt, std::move(body)) {}
199
202 Continuation(std::optional<const Parameter *> parameterOpt, Body body)
203 : parameterOpt(parameterOpt ? std::make_optional((*parameterOpt)->param) : std::nullopt),
204 body(std::move(body)) {}
205};
206
207} // namespace P4::P4Tools::P4Testgen
208
209#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_LIB_CONTINUATION_H_ */
Definition node.h:94
Represents a stack of namespaces.
Definition namespace_context.h:14
A continuation body is a list of commands.
Definition continuation.h:129
void clear()
Removes all commands in this body.
Definition continuation.cpp:50
void push(Command cmd)
Pushes the given command onto the command stack.
Definition continuation.cpp:43
const Command next() const
Definition continuation.cpp:35
bool empty() const
Determines whether this body is empty.
Definition continuation.cpp:33
void pop()
Definition continuation.cpp:45
Definition continuation.h:29
Body apply(std::optional< const IR::Node * > value_opt) const
Definition continuation.cpp:91
friend std::ostream & operator<<(std::ostream &out, const Exception value)
Definition continuation.h:53
Exception
Enumerates the exceptions that can be thrown during symbolic execution.
Definition continuation.h:35
@ PacketTooShort
Thrown on premature packet end. Models P4's error.PacketTooShort.
@ Drop
This is an internal interpreter exception to express drop semantics.
@ Abort
Thrown when the target terminates.
@ NoMatch
Thrown when a select expression fails to match. This models P4's error.NoMatch.
@ Reject
Thrown when the parser reaches the reject state.
@ Exit
Thrown when an exit statement is encountered.
std::optional< const IR::PathExpression * > parameterOpt
Represents the continuation's parameter.
Definition continuation.h:181
Continuation(std::optional< const Parameter * > parameterOpt, Body body)
Definition continuation.h:202
static const Parameter * genParameter(const IR::Type *type, cstring name, const NamespaceContext *ctx)
Definition continuation.cpp:166
Continuation(Body body)
Creates a parameterless continuation.
Definition continuation.h:198
std::variant< cstring, uint64_t, int64_t, bool, const IR::Expression * > PropertyValue
Definition continuation.h:85
Definition modules/testgen/test/small-step/util.h:40
An event in a trace of the execution of a P4 program.
Definition trace_event.h:14
Definition cstring.h:85
STL namespace.
Definition continuation.h:104
bool operator==(const Guard &other) const
Delegates to IR equality.
Definition continuation.cpp:29
cstring propertyName
The name of the property that is being set.
Definition continuation.h:89
bool operator==(const PropertyUpdate &other) const
Delegates to the equality of the property.
Definition continuation.cpp:23
PropertyValue property
The value of the property.
Definition continuation.h:91
bool operator==(const Return &other) const
Delegates to IR equality.
Definition continuation.cpp:16