P4C
The P4 Compiler
|
Public Types | |
enum class | CodeBlock { P4Code , PhvAsm , MauAsm , HdrAsm , ParserIAsm , DeparserIAsm , ParserEAsm , DeparserEAsm } |
enum class | Hdr { None , CoreP4 , TofinoMin , Tofino1arch , Tofino2arch , V1model_2018 , V1model_2020 } |
enum class | Pass { FullFrontend , FullMidend , ConverterToBackend , ThreadLocalInstances , FullBackend , PhvLogging } |
Runs a preconstructed pass over the pipe or program. More... | |
Public Member Functions | |
TestCode (Hdr header, std::string code, const std::initializer_list< std::string > &insertion={}, const std::string &blockMarker="", const std::initializer_list< std::string > &options={}) | |
bool | apply_pass (Pass pass) |
bool | apply_pass (Visitor &pass, const Visitor_Context *context=nullptr) |
Runs the pass over either the pipe (if created) else the program. | |
bool | apply_pass (Visitor *pass, const Visitor_Context *context=nullptr) |
bool | CreateBackend () |
Runs all the necessary passes to create a backend ready for testing. | |
bool | CreateBlockThreadLocalInstances () |
std::string | extract_code (CodeBlock blk_type, size_t pos=0) const |
std::string | extract_code (size_t pos=0) const |
void | flags (Match::Flag f) |
Sets the flags to be used by other member functions. | |
std::string | get_field_container (const std::string &field, const std::string &str, int idx=0) const |
const IR::P4Program * | get_program () |
Match::Result | match (CodeBlock blk_type, const Match::CheckList &exprs) const |
Calls Match::match() on the code block specified by CodeBlock . | |
Match::Result | match (const Match::CheckList &exprs) const |
Calls Match::match() on the P4 code block specified by blockMarker . | |
void | set_phv_log_file (std::string path) |
Static Public Member Functions | |
static std::string | any_to_brace () |
Useful strings. | |
static std::string | empty_appy () |
static std::string | empty_state () |
static std::string | min_control_shell () |
static std::string | min_control_shell_marker () |
static TestCode | TestControlBlock (const std::string &defines, const std::string &block, Hdr header=Hdr::None) |
static std::string | tofino_shell () |
static std::string | tofino_shell_control_marker () |
static std::string | tofino_shell_deparser_marker () |
static std::string | tofino_shell_parser_marker () |
Friends | |
std::ostream & | operator<< (std::ostream &out, const TestCode &tc) |
|
strong |
CodeBlock flag for passing into match()
and extract_code()
. P4Code is always available, xxxAsm code is only availble post Pass::FullBackend
.
|
strong |
The header file to prefix the 'code' with. N.B. 98% of the test time can be taken up processing boiler-plate for Front-end tests. But Mid-end & Back-end test also run faster by minimising unnecessary code. e.g. preferably use 'Hdr::None', or 'Hdr::TofinoMin', rather than 'Hdr::Tofino1arch'.
|
strong |
Runs a preconstructed pass over the pipe or program.
P4::Test::TestCode::TestCode | ( | Hdr | header, |
std::string | code, | ||
const std::initializer_list< std::string > & | insertion = {}, | ||
const std::string & | blockMarker = "", | ||
const std::initializer_list< std::string > & | options = {} ) |
See test_bf_gtest_helpers.cpp for example usage of 'TestCode'. State the 'header' needed and the 'code' (with optional N% insertion points). The N% entires are repalced by the 'insertion' elements e.g. TestCode(Hdr::None, // No header required. R" control TestIngress<H, M>(inout H hdr, inout M meta); package TestPackage<H, M>(TestIngress<H, M> ig); %0% // Definitions. control ti(inout Hd headers, inout Md meta){ %1% // Apply block. } TestPackage(ti()) main;)", // Our program. {"struct Hd{}; struct Md{};", "apply{}"}, // replaces %0% & %1%. "control ti" + any_to_brace(), // The start of the block. {"-S"}); // Commandline options.
header | Required header. |
code | Program, with insertions points. |
insertion | Default: none. |
blockMarker | Default: all code. |
options | Default: P4_16, tofino, tna (or tofinoX, tXna when using TofinoXarch header file) |
std::string P4::Test::TestCode::extract_code | ( | CodeBlock | blk_type, |
size_t | pos = 0 ) const |
Extract the code block specified by CodeBlock
stripping of the initial pos
characters e.g. Match::Result.pos
.
|
inline |
Extract the P4 code block specified by blockMarker
. stripping of the initial pos
characters e.g. Match::Result.pos
.
std::string P4::Test::TestCode::get_field_container | ( | const std::string & | field, |
const std::string & | str, | ||
int | idx = 0 ) const |
Get the container for given field
field | The field to search for |
str | The string in which to search for the field (most likely returned by extract_code(CodeBlock::PhvAsm) |
idx | The index of the (stage X, container) pair to return the conntainer for. This is not the stage number. |
|
static |
'min_control_shell' is a minimal program, requiring insertions for: %0% Defines for 'struct headers_t' and 'struct local_metadata_t'. %1% A control ingress_control block e.g. 'empty_appy()'. The 'min_control_shell_marker identifies the block within the 'min_control_shell' string.
|
inlinestatic |
See test_bf_gtest_helpers.cpp for example usage of 'TestControlBlock'. TestControlBlock() is a handy wrapper for testing minimal control block P4 programs. N.B. 98% of the test time can be taken up processing boiler-plate. If this wrapper is not quite right, create your own minimal code block!
defines | Must contain a definition of 'struct Headers'. |
block | The code inserted into 'control testingress(inout Headers headers){%1%}'. |
header | Required header. |
|
static |
'tofino_shell' is a tofino program, requiring insertions for: %0% Defines for 'struct headers_t' and 'struct local_metadata_t'. %1% A parser ingress_parser block e.g. 'empty_state()'. %2% A control ingress_control block e.g. 'empty_appy()'. %3% A control ingress_deparser block e.g. 'empty_appy()'. The 'tofino_shell_*_marker's identify those blocks within the 'tofino_shell' string.