P4C
The P4 Compiler
Loading...
Searching...
No Matches
P4::P4Tools::Utils Class Reference

General utility functions that are not present in the compiler framework. More...

#include <util.h>

Static Public Member Functions

template<typename ContainerType >
static std::string containerToString (const ContainerType &container)
 
static const IR::MethodCallExpression * generateInternalMethodCall (std::string_view methodName, const std::vector< const IR::Expression * > &argVector, const IR::Type *returnType=IR::Type_Void::get(), const IR::ParameterList *paramList=new IR::ParameterList())
 
static std::optional< uint32_t > getCurrentSeed ()
 
static big_int getRandBigInt (const big_int &max)
 
static big_int getRandBigInt (const big_int &min, const big_int &max)
 This is a big_int version of getRndInt.
 
static const IR::Constant * getRandConstantForType (const IR::Type_Bits *type)
 
static const IR::Constant * getRandConstantForWidth (int bitWidth)
 
static int64_t getRandInt (const std::vector< int64_t > &percent)
 
static int64_t getRandInt (int64_t min, int64_t max)
 
static uint64_t getRandInt (uint64_t max)
 
static std::string getTimeStamp ()
 
template<typename Iter >
static Iter pickRandom (Iter start, Iter end)
 
static void setRandomSeed (int seed)
 
template<typename T >
static void shuffle (T *inp)
 Shuffles the given iterable.
 

Detailed Description

General utility functions that are not present in the compiler framework.

Member Function Documentation

◆ containerToString()

template<typename ContainerType >
static std::string P4::P4Tools::Utils::containerToString ( const ContainerType & container)
inlinestatic

Convert a container type (array, set, vector, etc.) into a well-formed [val1, val2, ...] representation. This function is used for debugging output.

◆ generateInternalMethodCall()

const IR::MethodCallExpression * P4::P4Tools::Utils::generateInternalMethodCall ( std::string_view methodName,
const std::vector< const IR::Expression * > & argVector,
const IR::Type * returnType = IR::Type_Void::get(),
const IR::ParameterList * paramList = new IR::ParameterList() )
static
Returns
a method call to an internal extern consumed by the interpreter. The return type is typically Type_Void.

◆ getCurrentSeed()

std::optional< uint32_t > P4::P4Tools::Utils::getCurrentSeed ( )
static
Returns
currentSeed.

◆ getRandBigInt()

big_int P4::P4Tools::Utils::getRandBigInt ( const big_int & max)
static
Returns
a random big integer in the range [0,
Parameters
max].Always return 0 if no seed is set.

◆ getRandConstantForType()

const IR::Constant * P4::P4Tools::Utils::getRandConstantForType ( const IR::Type_Bits * type)
static
Returns
a IR::Constant with a random big integer that fits the specified
Parameters
type.

◆ getRandConstantForWidth()

const IR::Constant * P4::P4Tools::Utils::getRandConstantForWidth ( int bitWidth)
static
Returns
a IR::Constant with a random big integer that fits the specified bit width. The type will be an unsigned Type_Bits with
Parameters
bitWidth.

◆ getRandInt() [1/3]

int64_t P4::P4Tools::Utils::getRandInt ( const std::vector< int64_t > & percent)
static
Returns
a random integer based on the percent vector.

◆ getRandInt() [2/3]

int64_t P4::P4Tools::Utils::getRandInt ( int64_t min,
int64_t max )
static
Returns
a random integer between min and max.

◆ getRandInt() [3/3]

uint64_t P4::P4Tools::Utils::getRandInt ( uint64_t max)
static
Returns
a random integer in the range [0,
Parameters
max].Always return 0 if no seed is set.

◆ getTimeStamp()

std::string P4::P4Tools::Utils::getTimeStamp ( )
static

Return the current timestamp with millisecond accuracy. Format: year-month-day-hour:minute:second.millisecond Borrowed from https://stackoverflow.com/a/35157784

◆ pickRandom()

template<typename Iter >
static Iter P4::P4Tools::Utils::pickRandom ( Iter start,
Iter end )
inlinestatic
Returns
a random element from the given range between
Parameters
startand
end.

◆ shuffle()

template<typename T >
static void P4::P4Tools::Utils::shuffle ( T * inp)
inlinestatic

Shuffles the given iterable.

Parameters
inp