gem5 v24.0.0.0
Loading...
Searching...
No Matches
gem5::MathExpr Class Reference

#include <mathexpr.hh>

Classes

class  Node
 
struct  OpSearch
 

Public Types

typedef std::function< double(std::string)> EvalCallback
 

Public Member Functions

 MathExpr (std::string expr)
 
std::string toStr () const
 Prints an ASCII representation of the expression tree.
 
double eval (EvalCallback fn) const
 Evaluates the expression.
 
std::vector< std::string > getVariables () const
 Return all variables in the this expression.
 

Private Types

enum  Operator {
  bAdd , bSub , bMul , bDiv ,
  bPow , uNeg , sValue , sVariable ,
  nInvalid
}
 
typedef double(* binOp) (double, double)
 

Private Member Functions

Nodeparse (std::string expr)
 Parse and create nodes from string.
 
std::string toStr (Node *n, std::string prefix) const
 Print tree as string.
 
double eval (const Node *n, EvalCallback fn) const
 Eval a node.
 
void getVariables (const Node *n, std::vector< std::string > &vars) const
 Return all variable reachable from a node to a vector of strings.
 

Private Attributes

const int MAX_PRIO = 4
 
std::array< OpSearch, uNeg+1 > ops
 Operator list.
 
Noderoot
 Root node.
 

Detailed Description

Definition at line 50 of file mathexpr.hh.

Member Typedef Documentation

◆ binOp

typedef double(* gem5::MathExpr::binOp) (double, double)
private

Definition at line 98 of file mathexpr.hh.

◆ EvalCallback

typedef std::function<double(std::string)> gem5::MathExpr::EvalCallback

Definition at line 56 of file mathexpr.hh.

Member Enumeration Documentation

◆ Operator

Enumerator
bAdd 
bSub 
bMul 
bDiv 
bPow 
uNeg 
sValue 
sVariable 
nInvalid 

Definition at line 91 of file mathexpr.hh.

Constructor & Destructor Documentation

◆ MathExpr()

gem5::MathExpr::MathExpr ( std::string expr)

Definition at line 50 of file mathexpr.cc.

References gem5::ArmISA::a, gem5::ArmISA::b, bAdd, bDiv, bMul, bPow, bSub, and uNeg.

Member Function Documentation

◆ eval() [1/2]

double gem5::MathExpr::eval ( const Node * n,
EvalCallback fn ) const
private

Eval a node.

Definition at line 153 of file mathexpr.cc.

References eval(), gem5::ArmISA::fn, gem5::ArmISA::n, ops, panic, sValue, and sVariable.

◆ eval() [2/2]

double gem5::MathExpr::eval ( EvalCallback fn) const
inline

Evaluates the expression.

Parameters
fnA callback funcion to evaluate variables
Returns
The value for this expression

Definition at line 72 of file mathexpr.hh.

References eval(), gem5::ArmISA::fn, and root.

Referenced by eval(), eval(), and gem5::MathExprPowerModel::eval().

◆ getVariables() [1/2]

std::vector< std::string > gem5::MathExpr::getVariables ( ) const
inline

Return all variables in the this expression.

This function starts from the root node and traverses all nodes while adding the variables it finds to a vector. Returns the found variables in a vector of strings

Returns
A Vector with the names of all variables

Definition at line 83 of file mathexpr.hh.

References getVariables(), and root.

Referenced by getVariables(), and getVariables().

◆ getVariables() [2/2]

void gem5::MathExpr::getVariables ( const Node * n,
std::vector< std::string > & vars ) const
private

Return all variable reachable from a node to a vector of strings.

Definition at line 181 of file mathexpr.cc.

References getVariables(), gem5::ArmISA::n, nInvalid, sValue, and sVariable.

◆ parse()

MathExpr::Node * gem5::MathExpr::parse ( std::string expr)
private

Parse and create nodes from string.

This function parses a string expression into an expression tree.

It will look for operators in priority order to recursively build the tree, respecting parenthesization. Constants can be expressed in any format accepted by std::stod, whereas variables are essentially [A-Za-z0-9.$\]+

Definition at line 78 of file mathexpr.cc.

References gem5::ArmISA::c, gem5::ArmISA::i, gem5::MipsISA::l, MAX_PRIO, gem5::ArmISA::n, ops, gem5::MipsISA::p, parse(), gem5::ArmISA::priority, gem5::MipsISA::r, sValue, sVariable, and gem5::ArmISA::v.

Referenced by parse().

◆ toStr() [1/2]

std::string gem5::MathExpr::toStr ( ) const
inline

Prints an ASCII representation of the expression tree.

Returns
A string containing the ASCII representation of the expression

Definition at line 63 of file mathexpr.hh.

References root, and toStr().

Referenced by toStr(), and toStr().

◆ toStr() [2/2]

std::string gem5::MathExpr::toStr ( Node * n,
std::string prefix ) const
private

Print tree as string.

Definition at line 170 of file mathexpr.cc.

References gem5::ArmISA::n, and toStr().

Member Data Documentation

◆ MAX_PRIO

const int gem5::MathExpr::MAX_PRIO = 4
private

Definition at line 97 of file mathexpr.hh.

Referenced by parse().

◆ ops

std::array<OpSearch, uNeg + 1> gem5::MathExpr::ops
private

Operator list.

Definition at line 109 of file mathexpr.hh.

Referenced by eval(), and parse().

◆ root

Node* gem5::MathExpr::root
private

Root node.

Definition at line 136 of file mathexpr.hh.

Referenced by eval(), getVariables(), and toStr().


The documentation for this class was generated from the following files:

Generated on Tue Jun 18 2024 16:24:12 for gem5 by doxygen 1.11.0