14#include "sparta/statistics/ExpressionNode.hpp"
15#include "sparta/statistics/ExpressionNodeTypes.hpp"
19class StatisticInstance;
32typedef std::pair<std::string, std::unique_ptr<StatisticInstance>>
stat_pair_t;
74 std::unique_ptr<ExpressionNode> content_;
90 if(rhp.content_ !=
nullptr){
91 content_.reset(rhp.content_->clone());
99 content_(std::move(rhp.content_))
133 std::vector<const TreeNode*>& already_used);
143 content_.reset(item);
167 std::vector<const TreeNode*>& used);
175 template<
typename RetT,
typename ArgT>
186 template<
typename RetT,
typename ArgT>
188 std::function<RetT (ArgT)>&,
197 template<
typename RetT,
typename ArgT>
199 RetT(*fxn)(ArgT, ArgT),
209 template<
typename RetT>
221 template<
typename RetT,
typename ArgT>
223 RetT(*fxn)(ArgT, ArgT, ArgT),
242 if(rhp.content_ !=
nullptr){
243 content_.reset(rhp.content_->clone());
258 content_ = std::move(rhp.content_);
267 if(content_ ==
nullptr){
268 throw SpartaException(
"Cannot clone content of an expression with null content");
270 return content_->clone();
280 uint32_t
getStats(std::vector<const StatisticInstance*>& results)
const {
281 return getStats_(results);
290 return content_ !=
nullptr;
303 template<
typename RetT,
typename ArgT>
318 template<
typename RetT,
typename ArgT>
320 std::function<RetT (ArgT)>&,
334 template<
typename RetT,
typename ArgT>
336 RetT(*fxn)(ArgT, ArgT),
351 template<
typename RetT>
369 template<
typename RetT,
typename ArgT>
371 RetT(*fxn)(ArgT, ArgT, ArgT),
438 if(content_ ==
nullptr){
439 throw SpartaException(
"Cannot evaluate expression because it has no content. Test with "
440 "hasContent before blindly evaluating foreign expressions");
443 return content_->evaluate();
483 bool show_range=
true,
484 bool resolve_subexprs=
true)
const {
488 content_->dump(o, show_range, resolve_subexprs);
492 bool supportsCompression()
const {
493 if (content_ ==
nullptr) {
496 return content_->supportsCompression();
509 bool resolve_subexprs=
true)
const {
510 std::stringstream ss;
511 dump(ss, show_range, resolve_subexprs);
524 std::vector<const Clock*> clocks;
526 if(clocks.size() == 0){
529 const Clock* result = *(clocks.begin());
530 for(
auto itr = clocks.begin()+1; itr != clocks.end(); ++itr){
532 throw SpartaException(
"Multiple TreeNodes found with different clocks when "
533 "attempting to determine the clock associated with the "
545 void getClocks(std::vector<const Clock*>& clocks)
const{
547 content_->getClocks(clocks);
556 uint32_t getStats_(std::vector<const StatisticInstance*>& results)
const {
557 return content_ ? content_->getStats(results) : 0;
566 void parse_(
const std::string& expression,
568 std::vector<const TreeNode*>& already_used,
584 content_.reset(
new Operation(type, op1, op2, op3));
587template <
typename RetT,
typename ArgT>
592 "function pointer of unary function \"" << fxn <<
"\" must not be nullptr");
596template <
typename RetT,
typename ArgT>
598 std::function<RetT (ArgT)>& fxn,
601 "function pointer of unary function \"" <<
"\" must not be nullptr");
605template <
typename RetT,
typename ArgT>
607 RetT(*fxn)(ArgT, ArgT),
611 "function pointer of binary function \"" << fxn <<
"\" must not be nullptr");
615template <
typename RetT>
623template <
typename RetT,
typename ArgT>
625 RetT(*fxn)(ArgT, ArgT, ArgT),
630 "function pointer of ternary function \"" << fxn <<
"\" must not be nullptr");
634template <
typename RetT,
typename ArgT>
639 "function pointer of unary function \"" << fxn <<
"\" must not be nullptr");
644template <
typename RetT,
typename ArgT>
646 std::function<RetT (ArgT)>& fxn,
649 "function pointer of unary function \"" <<
"\" must not be nullptr");
653template <
typename RetT,
typename ArgT>
655 RetT(*fxn)(ArgT, ArgT),
659 "function pointer of binary function \"" << fxn <<
"\" must not be nullptr");
663template <
typename RetT>
671template <
typename RetT,
typename ArgT>
673 RetT(*fxn)(ArgT, ArgT, ArgT),
678 "function pointer of ternary function \"" << fxn <<
"\" must not be nullptr");
684 sparta_assert(content_ ==
nullptr,
"Cannot call operator= on an expression which already has an item. Item would ""be discarded");
691 sparta_assert(content_ !=
nullptr,
"Cannot call operator+() on an expression which has no item. A lhp is ""required in order to promote");
699 sparta_assert(content_ !=
nullptr,
"Cannot call operator-() on an expression which has no item. A lhp is ""required in order to negate");
707 sparta_assert(content_ !=
nullptr,
"Cannot call operator+(rhp) on an expression which has no item. A lhp is ""required in order to add");
717 sparta_assert(content_ !=
nullptr,
"Cannot call operator-(rhp) on an expression which has no item. A lhp is ""required in order to subtract");
727 sparta_assert(content_ !=
nullptr,
"Cannot call operator*(rhp) on an expression which has no item. A lhp is ""required in order to multiply");
737 sparta_assert(content_ !=
nullptr,
"Cannot call operator/(rhp) on an expression which has no item. A lhp is ""required in order to divide");
747 sparta_assert(content_ !=
nullptr,
"Cannot call operator+=(rhp) on an expression which has no item. A lhp is ""required in order to add");
758 sparta_assert(content_ !=
nullptr,
"Cannot call operator-=(rhp) on an expression which has no item. A lhp is ""required in order to subtract");
770 sparta_assert(content_ !=
nullptr,
"Cannot call operator*=(rhp) on an expression which has no item. A lhp is ""required in order to divide");
781 sparta_assert(content_ !=
nullptr,
"Cannot call operator/=(rhp) on an expression which has no item. A lhp is ""required in order to divide");
Set of macros for Sparta assertions. Caught by the framework.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Exception class for all of Sparta.
Basic Node framework in sparta device tree composite pattern.
A representation of simulated time.
Used to construct and throw a standard C++ exception. Inherits from std::exception.
Node in a composite tree representing a sparta Tree item.
Abstract interface class for an item in an expression. Subclasses can contain other ExpressionNodes.
Expression container/builder. Contains a single ExpressionNode representing the root of an expression...
double evaluate() const
Compute value of this operate in simulation for the current computation window.
Expression(const std::string &expression, TreeNode *context, const StatisticPairs &report_si)
Construct with string expression.
void start()
Notify every item in this expression to start a new computation window.
Expression & operator=(Expression &&rhp)
Assignment operator with move. Moves content of rhp and discards current expression content.
Expression(Expression &&rhp)
Move Constructor.
ExpressionNode * cloneContent() const
Makes a clone of the content of this expression.
void getClocks(std::vector< const Clock * > &clocks) const
Gets all clocks associated with this Expression.
Expression & operator*=(const Expression &rhp)
Return this expression with a node multiplying by rhp.
Expression tfunc(const std::string &name, RetT(*fxn)(ArgT, ArgT, ArgT), const Expression &a, const Expression &b, const Expression &c) const
Construct a ternary function having the given name and function.
bool hasContent() const
Does this expression have content.
std::string stringize(bool show_range=true, bool resolve_subexprs=true) const
Return a string representing this expression including any TreeNode dependencies.
Expression & operator+=(const Expression &rhp)
Return this expression with a node adding rhp.
Expression & operator=(const Expression &rhp)
Assignment operator. Clones the content of rhp and discards current expression content.
Expression ufunc(const std::string &name, RetT(*fxn)(ArgT), const Expression &a) const
Construct a unary function having the given name and function.
Expression(const std::string &expression, TreeNode *context, std::vector< const TreeNode * > &already_used)
Construct with string expression.
const Clock * getClock()
Gets the clock associated with the content of this expression. This is done by finding all TreeNodes ...
Expression & operator-=(const Expression &rhp)
Return this expression with a node subtracting rhp.
Expression operator-() const
Return this expression with a negation inserted.
Expression(ExpressionNode *item)
Blind content constructor.
Expression operator/(const Expression &rhp) const
Return a new expression of: *this / rhp.
uint32_t getStats(std::vector< const StatisticInstance * > &results) const
Gets the statistics present in this expression.
Expression bfunc(const std::string &name, RetT(*fxn)(ArgT, ArgT), const Expression &a, const Expression &b) const
Construct a binary function having the given name and function.
Expression(const TreeNode *n, std::vector< const TreeNode * > &used)
Stat/Counter/Parameter construction.
Expression operator+() const
Return this expression with no effect.
Expression operator*(const Expression &rhp) const
Return a new expression of: *this * rhp.
Expression & operator/=(const Expression &rhp)
Return this expression with a node dividing by rhp.
virtual ~Expression()
Virtual destructor.
void end()
Notify every item in this expression to end the current computation window.
void dump(std::ostream &o, bool show_range=true, bool resolve_subexprs=true) const
Write the content of this entire expression to an ostream.
Expression()=default
Constructs an expression containing no content.
Expression(const Expression &rhp)
Copy Constructor.
Expression(const std::string &expression, TreeNode *context)
Construct with string expression.
operation_t
Types of operations supported.
@ OP_PROMOTE
Promotion: +x.
std::ostream & operator<<(std::ostream &o, const sparta::statistics::expression::Expression &ex)
Ostream printing function for Expressions.
std::vector< stat_pair_t > StatisticPairs
Type for storing a list of stat_pair_t.
std::pair< std::string, std::unique_ptr< StatisticInstance > > stat_pair_t
Type for storing each stat added.
Macros for handling exponential backoff.
Represents a Binary function node.
Operation Node (e.g. +,-,*,/)
Represents a Ternary function node.
Represents a Unary function node.