P4C
The P4 Compiler
Loading...
Searching...
No Matches
targets/bmv2/test_spec.h
1#ifndef BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_TEST_SPEC_H_
2#define BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_TEST_SPEC_H_
3
4#include <cstddef>
5#include <functional>
6#include <map>
7#include <optional>
8#include <utility>
9#include <vector>
10
11#include "backends/p4tools/common/lib/model.h"
12#include "ir/declaration.h"
13#include "ir/ir.h"
14#include "lib/cstring.h"
15#include "lib/rtti.h"
16
17#include "backends/p4tools/modules/testgen/lib/execution_state.h"
18#include "backends/p4tools/modules/testgen/lib/test_object.h"
19#include "backends/p4tools/modules/testgen/lib/test_spec.h"
20#include "backends/p4tools/modules/testgen/targets/bmv2/constants.h"
21
23
24/* =========================================================================================
25 * IndexExpression
26 * ========================================================================================= */
31 private:
33 const IR::Expression *index;
34
36 const IR::Expression *value;
37
38 public:
39 explicit IndexExpression(const IR::Expression *index, const IR::Expression *value);
40
43 [[nodiscard]] const IR::Constant *getEvaluatedIndex() const;
44
47 [[nodiscard]] const IR::Constant *getEvaluatedValue() const;
48
50 [[nodiscard]] const IR::Expression *getIndex() const;
51
53 [[nodiscard]] const IR::Expression *getValue() const;
54
55 [[nodiscard]] const IndexExpression *evaluate(const Model &model,
56 bool doComplete) const override;
57
58 [[nodiscard]] cstring getObjectName() const override;
59
60 DECLARE_TYPEINFO(IndexExpression, TestObject);
61};
62
63/* =========================================================================================
64 * IndexMap
65 * ========================================================================================= */
67class IndexMap : public TestObject {
68 protected:
70 const IR::Expression *initialValue;
71
73 std::vector<IndexExpression> indexConditions;
74
75 public:
76 explicit IndexMap(const IR::Expression *initialValue);
77
79 void writeToIndex(const IR::Expression *index, const IR::Expression *value);
80
82 [[nodiscard]] const IR::Expression *getInitialValue() const;
83
86 [[nodiscard]] const IR::Expression *getValueAtIndex(const IR::Expression *index) const;
87
90 [[nodiscard]] const IR::Constant *getEvaluatedInitialValue() const;
91
93 [[nodiscard]] std::map<big_int, std::pair<int, const IR::Constant *>> unravelMap() const;
94
95 DECLARE_TYPEINFO(IndexMap, TestObject);
96};
97
98/* =========================================================================================
99 * Bmv2V1ModelRegisterValue
100 * ========================================================================================= */
110 public:
111 explicit Bmv2V1ModelRegisterValue(const IR::Expression *initialValue);
112
113 [[nodiscard]] cstring getObjectName() const override;
114
115 [[nodiscard]] const Bmv2V1ModelRegisterValue *evaluate(const Model &model,
116 bool doComplete) const override;
117
118 DECLARE_TYPEINFO(Bmv2V1ModelRegisterValue, IndexMap);
119};
120
121/* =========================================================================================
122 * Bmv2V1ModelMeterValue
123 * ========================================================================================= */
124
126 private:
128 bool isDirect;
129
130 public:
131 explicit Bmv2V1ModelMeterValue(const IR::Expression *initialValue, bool isDirect);
132
133 [[nodiscard]] cstring getObjectName() const override;
134
135 [[nodiscard]] const Bmv2V1ModelMeterValue *evaluate(const Model &model,
136 bool doComplete) const override;
137
139 [[nodiscard]] bool isDirectMeter() const;
140
141 DECLARE_TYPEINFO(Bmv2V1ModelMeterValue, IndexMap);
142};
143
144/* =========================================================================================
145 * Bmv2V1ModelActionProfile
146 * ========================================================================================= */
148 private:
150 std::vector<std::pair<cstring, std::vector<ActionArg>>> actions;
151
153 const IR::IDeclaration *profileDecl;
154
155 public:
156 explicit Bmv2V1ModelActionProfile(const IR::IDeclaration *profileDecl);
157
158 [[nodiscard]] cstring getObjectName() const override;
159
161 [[nodiscard]] const std::vector<std::pair<cstring, std::vector<ActionArg>>> *getActions() const;
162
164 [[nodiscard]] const IR::IDeclaration *getProfileDecl() const;
165
167 [[nodiscard]] size_t getActionMapSize() const;
168
170 void addToActionMap(cstring actionName, std::vector<ActionArg> actionArgs);
171
172 [[nodiscard]] const Bmv2V1ModelActionProfile *evaluate(const Model &model,
173 bool doComplete) const override;
174
175 DECLARE_TYPEINFO(Bmv2V1ModelActionProfile, TestObject);
176};
177
178/* =========================================================================================
179 * ActionSelector
180 * ========================================================================================= */
182 private:
184 const IR::IDeclaration *selectorDecl;
185
187 const Bmv2V1ModelActionProfile *actionProfile;
188
189 public:
190 explicit Bmv2V1ModelActionSelector(const IR::IDeclaration *selectorDecl,
191 const Bmv2V1ModelActionProfile *actionProfile);
192
193 [[nodiscard]] cstring getObjectName() const override;
194
196 [[nodiscard]] const IR::IDeclaration *getSelectorDecl() const;
197
199 [[nodiscard]] const Bmv2V1ModelActionProfile *getActionProfile() const;
200
201 [[nodiscard]] const Bmv2V1ModelActionSelector *evaluate(const Model &model,
202 bool doComplete) const override;
203
204 DECLARE_TYPEINFO(Bmv2V1ModelActionSelector, TestObject);
205};
206
207/* =========================================================================================
208 * Bmv2V1ModelCloneInfo
209 * ========================================================================================= */
211 private:
213 const IR::Expression *sessionId;
214
216 BMv2Constants::CloneType cloneType;
217
219 std::reference_wrapper<const ExecutionState> clonedState;
220
222 std::optional<int> preserveIndex;
223
224 public:
225 explicit Bmv2V1ModelCloneInfo(const IR::Expression *sessionId,
226 BMv2Constants::CloneType cloneType,
227 const ExecutionState &clonedState,
228 std::optional<int> preserveIndex);
229
230 [[nodiscard]] cstring getObjectName() const override;
231
232 [[nodiscard]] const Bmv2V1ModelCloneInfo *evaluate(const Model &model,
233 bool doComplete) const override;
234
236 [[nodiscard]] const IR::Expression *getSessionId() const;
237
239 [[nodiscard]] BMv2Constants::CloneType getCloneType() const;
240
242 [[nodiscard]] std::optional<int> getPreserveIndex() const;
243
245 [[nodiscard]] const ExecutionState &getClonedState() const;
246
247 DECLARE_TYPEINFO(Bmv2V1ModelCloneInfo, TestObject);
248};
249
250/* =========================================================================================
251 * Bmv2V1ModelCloneSpec
252 * ========================================================================================= */
254 private:
256 const IR::Expression *sessionId;
257
259 const IR::Expression *clonePort;
260
263 bool isClone;
264
265 public:
266 explicit Bmv2V1ModelCloneSpec(const IR::Expression *sessionId, const IR::Expression *clonePort,
267 bool isClone);
268
269 [[nodiscard]] cstring getObjectName() const override;
270
271 [[nodiscard]] const Bmv2V1ModelCloneSpec *evaluate(const Model &model,
272 bool doComplete) const override;
273
275 [[nodiscard]] const IR::Expression *getSessionId() const;
276
278 [[nodiscard]] const IR::Expression *getClonePort() const;
279
282 [[nodiscard]] bool isClonedPacket() const;
283
286 [[nodiscard]] const IR::Constant *getEvaluatedClonePort() const;
287
290 [[nodiscard]] const IR::Constant *getEvaluatedSessionId() const;
291
292 DECLARE_TYPEINFO(Bmv2V1ModelCloneSpec, TestObject);
293};
294
295/* =========================================================================================
296 * MetadataCollection
297 * ========================================================================================= */
299 private:
301 std::map<cstring, const IR::Literal *> metadataFields;
302
303 public:
305
306 [[nodiscard]] cstring getObjectName() const override;
307
308 [[nodiscard]] const MetadataCollection *evaluate(const Model & /*model*/,
309 bool doComplete) const override;
310
312 [[nodiscard]] const std::map<cstring, const IR::Literal *> &getMetadataFields() const;
313
315 void addMetaDataField(cstring name, const IR::Literal *metadataField);
316
318 const IR::Literal *getMetadataField(cstring name);
319
320 DECLARE_TYPEINFO(MetadataCollection, TestObject);
321};
322
323/* =========================================================================================
324 * Table Key Match Types
325 * ========================================================================================= */
326
327class Optional : public TableMatch {
328 private:
330 const IR::Expression *value;
331
333 bool addMatch;
334
335 public:
336 explicit Optional(const IR::KeyElement *key, const IR::Expression *value, bool addMatch);
337
338 [[nodiscard]] const Optional *evaluate(const Model &model, bool doComplete) const override;
339
340 [[nodiscard]] cstring getObjectName() const override;
341
344 [[nodiscard]] const IR::Constant *getEvaluatedValue() const;
345
347 [[nodiscard]] bool addAsExactMatch() const;
348
349 DECLARE_TYPEINFO(Optional, TableMatch);
350};
351
352class Range : public TableMatch {
353 private:
355 const IR::Expression *low;
356
358 const IR::Expression *high;
359
360 public:
361 explicit Range(const IR::KeyElement *key, const IR::Expression *low,
362 const IR::Expression *high);
363
364 [[nodiscard]] const Range *evaluate(const Model &model, bool doComplete) const override;
365
366 [[nodiscard]] cstring getObjectName() const override;
367
370 [[nodiscard]] const IR::Constant *getEvaluatedLow() const;
371
374 [[nodiscard]] const IR::Constant *getEvaluatedHigh() const;
375
376 DECLARE_TYPEINFO(Range, TableMatch);
377};
378
379} // namespace P4::P4Tools::P4Testgen::Bmv2
380
381#endif /* BACKENDS_P4TOOLS_MODULES_TESTGEN_TARGETS_BMV2_TEST_SPEC_H_ */
The Declaration interface, representing objects with names.
Definition declaration.h:26
Definition backends/p4tools/common/lib/model.h:19
CloneType
Clone type is derived from v1model.p4.
Definition p4tools/modules/testgen/targets/bmv2/constants.h:34
Definition targets/bmv2/test_spec.h:147
size_t getActionMapSize() const
Definition targets/bmv2/test_spec.cpp:151
const IR::IDeclaration * getProfileDecl() const
Definition targets/bmv2/test_spec.cpp:145
cstring getObjectName() const override
Definition targets/bmv2/test_spec.cpp:143
void addToActionMap(cstring actionName, std::vector< ActionArg > actionArgs)
Add an action (its name) and the arguments to the action map of this profile.
Definition targets/bmv2/test_spec.cpp:147
const std::vector< std::pair< cstring, std::vector< ActionArg > > > * getActions() const
Definition targets/bmv2/test_spec.cpp:136
const Bmv2V1ModelActionProfile * evaluate(const Model &model, bool doComplete) const override
Definition targets/bmv2/test_spec.cpp:153
Definition targets/bmv2/test_spec.h:181
const Bmv2V1ModelActionSelector * evaluate(const Model &model, bool doComplete) const override
Definition targets/bmv2/test_spec.cpp:184
const Bmv2V1ModelActionProfile * getActionProfile() const
Definition targets/bmv2/test_spec.cpp:180
cstring getObjectName() const override
Definition targets/bmv2/test_spec.cpp:176
const IR::IDeclaration * getSelectorDecl() const
Definition targets/bmv2/test_spec.cpp:178
Definition targets/bmv2/test_spec.h:210
const Bmv2V1ModelCloneInfo * evaluate(const Model &model, bool doComplete) const override
Definition targets/bmv2/test_spec.cpp:213
cstring getObjectName() const override
Definition targets/bmv2/test_spec.cpp:203
const ExecutionState & getClonedState() const
Definition targets/bmv2/test_spec.cpp:209
const IR::Expression * getSessionId() const
Definition targets/bmv2/test_spec.cpp:207
std::optional< int > getPreserveIndex() const
Definition targets/bmv2/test_spec.cpp:211
BMv2Constants::CloneType getCloneType() const
Definition targets/bmv2/test_spec.cpp:205
Definition targets/bmv2/test_spec.h:253
const Bmv2V1ModelCloneSpec * evaluate(const Model &model, bool doComplete) const override
Definition targets/bmv2/test_spec.cpp:246
const IR::Constant * getEvaluatedSessionId() const
Definition targets/bmv2/test_spec.cpp:239
bool isClonedPacket() const
Definition targets/bmv2/test_spec.cpp:252
cstring getObjectName() const override
Definition targets/bmv2/test_spec.cpp:226
const IR::Constant * getEvaluatedClonePort() const
Definition targets/bmv2/test_spec.cpp:232
const IR::Expression * getClonePort() const
Definition targets/bmv2/test_spec.cpp:228
const IR::Expression * getSessionId() const
Definition targets/bmv2/test_spec.cpp:230
Definition targets/bmv2/test_spec.h:125
cstring getObjectName() const override
Definition targets/bmv2/test_spec.cpp:115
const Bmv2V1ModelMeterValue * evaluate(const Model &model, bool doComplete) const override
Definition targets/bmv2/test_spec.cpp:117
bool isDirectMeter() const
Definition targets/bmv2/test_spec.cpp:129
Definition targets/bmv2/test_spec.h:109
cstring getObjectName() const override
Definition targets/bmv2/test_spec.cpp:94
const Bmv2V1ModelRegisterValue * evaluate(const Model &model, bool doComplete) const override
Definition targets/bmv2/test_spec.cpp:96
Definition targets/bmv2/test_spec.h:30
const IR::Expression * getIndex() const
Definition targets/bmv2/test_spec.cpp:33
const IR::Constant * getEvaluatedValue() const
Definition targets/bmv2/test_spec.cpp:19
const IR::Constant * getEvaluatedIndex() const
Definition targets/bmv2/test_spec.cpp:26
const IndexExpression * evaluate(const Model &model, bool doComplete) const override
Definition targets/bmv2/test_spec.cpp:39
cstring getObjectName() const override
Definition targets/bmv2/test_spec.cpp:37
const IR::Expression * getValue() const
Definition targets/bmv2/test_spec.cpp:35
Readable and writable symbolic map, which maps indices to particular values.
Definition targets/bmv2/test_spec.h:67
std::map< big_int, std::pair< int, const IR::Constant * > > unravelMap() const
Return the "writes" to this index map as a.
Definition targets/bmv2/test_spec.cpp:45
const IR::Constant * getEvaluatedInitialValue() const
Definition targets/bmv2/test_spec.cpp:80
const IR::Expression * initialValue
A new IndexMap always requires an initial value. This can be a constant or taint.
Definition targets/bmv2/test_spec.h:70
const IR::Expression * getValueAtIndex(const IR::Expression *index) const
Definition targets/bmv2/test_spec.cpp:69
const IR::Expression * getInitialValue() const
Definition targets/bmv2/test_spec.cpp:67
void writeToIndex(const IR::Expression *index, const IR::Expression *value)
Write.
Definition targets/bmv2/test_spec.cpp:63
std::vector< IndexExpression > indexConditions
Each element is an API name paired with a match rule.
Definition targets/bmv2/test_spec.h:73
Definition targets/bmv2/test_spec.h:298
void addMetaDataField(cstring name, const IR::Literal *metadataField)
Add a metadata field to the collection.
Definition targets/bmv2/test_spec.cpp:327
const MetadataCollection * evaluate(const Model &, bool doComplete) const override
Definition targets/bmv2/test_spec.cpp:314
const IR::Literal * getMetadataField(cstring name)
Definition targets/bmv2/test_spec.cpp:323
cstring getObjectName() const override
Definition targets/bmv2/test_spec.cpp:312
const std::map< cstring, const IR::Literal * > & getMetadataFields() const
Definition targets/bmv2/test_spec.cpp:319
Definition targets/bmv2/test_spec.h:327
cstring getObjectName() const override
Definition targets/bmv2/test_spec.cpp:275
const IR::Constant * getEvaluatedValue() const
Definition targets/bmv2/test_spec.cpp:261
bool addAsExactMatch() const
Definition targets/bmv2/test_spec.cpp:277
const Optional * evaluate(const Model &model, bool doComplete) const override
Definition targets/bmv2/test_spec.cpp:270
Definition targets/bmv2/test_spec.h:352
cstring getObjectName() const override
Definition targets/bmv2/test_spec.cpp:331
const IR::Constant * getEvaluatedHigh() const
Definition targets/bmv2/test_spec.cpp:291
const IR::Constant * getEvaluatedLow() const
Definition targets/bmv2/test_spec.cpp:282
const Range * evaluate(const Model &model, bool doComplete) const override
Definition targets/bmv2/test_spec.cpp:300
Represents state of execution after having reached a program point.
Definition execution_state.h:34
Definition lib/test_spec.h:126
Definition test_object.h:15
Definition cstring.h:85
Inja.
Definition targets/bmv2/cmd_stepper.cpp:33