P4C
The P4 Compiler
Loading...
Searching...
No Matches
P4::Test::TestCode Class Reference

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)
 

Member Enumeration Documentation

◆ CodeBlock

enum class P4::Test::TestCode::CodeBlock
strong

CodeBlock flag for passing into match() and extract_code(). P4Code is always available, xxxAsm code is only availble post Pass::FullBackend.

◆ Hdr

enum class P4::Test::TestCode::Hdr
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'.

  1. Get your tests running with 'Hdr::Tofino1arch' & 'tofino_shell()'.
  2. Prune the dead code before you finish.

◆ Pass

enum class P4::Test::TestCode::Pass
strong

Runs a preconstructed pass over the pipe or program.

Enumerator
FullFrontend 

Remove the pipe and run over the program.

FullMidend 

Remove the pipe and run over the program.

ConverterToBackend 

Run over the program and create the pipe.

ThreadLocalInstances 

Run over the pipe.

FullBackend 

Run over the pipe, creates asm CodeBlocks.

Constructor & Destructor Documentation

◆ TestCode()

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.

Parameters
headerRequired header.
codeProgram, with insertions points.
insertionDefault: none.
blockMarkerDefault: all code.
optionsDefault: P4_16, tofino, tna (or tofinoX, tXna when using TofinoXarch header file)

Member Function Documentation

◆ extract_code() [1/2]

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.

◆ extract_code() [2/2]

std::string P4::Test::TestCode::extract_code ( size_t pos = 0) const
inline

Extract the P4 code block specified by blockMarker. stripping of the initial pos characters e.g. Match::Result.pos.

◆ get_field_container()

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

Parameters
fieldThe field to search for
strThe string in which to search for the field (most likely returned by extract_code(CodeBlock::PhvAsm)
idxThe index of the (stage X, container) pair to return the conntainer for. This is not the stage number.

◆ min_control_shell()

std::string P4::Test::TestCode::min_control_shell ( )
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.

◆ TestControlBlock()

static TestCode P4::Test::TestCode::TestControlBlock ( const std::string & defines,
const std::string & block,
Hdr header = Hdr::None )
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!

Parameters
definesMust contain a definition of 'struct Headers'.
blockThe code inserted into 'control testingress(inout Headers headers){%1%}'.
headerRequired header.

◆ tofino_shell()

std::string P4::Test::TestCode::tofino_shell ( )
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.