Nikos Nikoleris has submitted this change. ( https://gem5-review.googlesource.com/c/public/gem5/+/27889 )

Change subject: sim: Add function that returns all variables in a MathExpr
......................................................................

sim: Add function that returns all variables in a MathExpr

This changes adds support for retrieving all variables in a math
expression. The added function can be called in all valid expressions
and will return the variables in a vector of strings.

Change-Id: I086ba04aa1f798400c97a0b6bf982018a2457c64
Signed-off-by: Nikos Nikoleris <nikos.nikole...@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/27889
Reviewed-by: Bobby R. Bruce <bbr...@ucdavis.edu>
Maintainer: Jason Lowe-Power <power...@gmail.com>
Tested-by: kokoro <noreply+kok...@google.com>
---
M src/sim/mathexpr.cc
M src/sim/mathexpr.hh
2 files changed, 37 insertions(+), 4 deletions(-)

Approvals:
  Bobby R. Bruce: Looks good to me, approved
  Jason Lowe-Power: Looks good to me, approved
  kokoro: Regressions pass



diff --git a/src/sim/mathexpr.cc b/src/sim/mathexpr.cc
index f80c535..0cbcd90 100644
--- a/src/sim/mathexpr.cc
+++ b/src/sim/mathexpr.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 ARM Limited
+ * Copyright (c) 2016, 2020 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
@@ -174,3 +174,17 @@
     return ret;
 }

+void
+MathExpr::getVariables(const Node *n,
+                       std::vector<std::string> &variables) const
+{
+    if (!n || n->op == sValue || n->op == nInvalid) {
+        return;
+    } else if (n->op == sVariable) {
+        variables.push_back(n->variable);
+    } else {
+        getVariables(n->l, variables);
+        getVariables(n->r, variables);
+    }
+}
+
diff --git a/src/sim/mathexpr.hh b/src/sim/mathexpr.hh
index b8db739..3dfe2b8 100644
--- a/src/sim/mathexpr.hh
+++ b/src/sim/mathexpr.hh
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016 ARM Limited
+ * Copyright (c) 2016, 2020 ARM Limited
  * All rights reserved
  *
  * The license below extends only to copyright in the software and shall
@@ -42,6 +42,7 @@
 #include <array>
 #include <functional>
 #include <string>
+#include <vector>

 class MathExpr {
   public:
@@ -66,6 +67,22 @@
      */
     double eval(EvalCallback fn) const { return eval(root, fn); }

+    /**
+     * 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
+     *
+     * @return A Vector with the names of all variables
+    */
+    std::vector<std::string> getVariables() const
+    {
+        std::vector<std::string> vars;
+        getVariables(root, vars);
+        return vars;
+    }
+
   private:
     enum Operator {
         bAdd, bSub, bMul, bDiv, bPow, uNeg, sValue, sVariable, nInvalid
@@ -119,8 +136,10 @@

     /** Eval a node */
     double eval(const Node *n, EvalCallback fn) const;
+
+    /** Return all variable reachable from a node to a vector of
+     * strings */
+    void getVariables(const Node *n, std::vector<std::string> &vars) const;
 };

 #endif
-
-

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/27889
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I086ba04aa1f798400c97a0b6bf982018a2457c64
Gerrit-Change-Number: 27889
Gerrit-PatchSet: 2
Gerrit-Owner: Nikos Nikoleris <nikos.nikole...@arm.com>
Gerrit-Reviewer: Bobby R. Bruce <bbr...@ucdavis.edu>
Gerrit-Reviewer: Giacomo Travaglini <giacomo.travagl...@arm.com>
Gerrit-Reviewer: Jason Lowe-Power <power...@gmail.com>
Gerrit-Reviewer: Nikos Nikoleris <nikos.nikole...@arm.com>
Gerrit-Reviewer: kokoro <noreply+kok...@google.com>
Gerrit-MessageType: merged
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to