gem5  v22.0.0.2
mathexpr.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, 2020 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include "sim/mathexpr.hh"
39 
40 #include <algorithm>
41 #include <cmath>
42 #include <regex>
43 #include <string>
44 
45 #include "base/logging.hh"
46 
47 namespace gem5
48 {
49 
50 MathExpr::MathExpr(std::string expr)
51  : ops(
52  std::array<OpSearch, uNeg + 1> {{
53  OpSearch {true, bAdd, 0, '+', [](double a, double b) { return a + b; }},
54  OpSearch {true, bSub, 0, '-', [](double a, double b) { return a - b; }},
55  OpSearch {true, bMul, 1, '*', [](double a, double b) { return a * b; }},
56  OpSearch {true, bDiv, 1, '/', [](double a, double b) { return a / b; }},
57  OpSearch {false,uNeg, 2, '-', [](double a, double b) { return -b; }},
58  OpSearch {true, bPow, 3, '^', [](double a, double b) {
59  return std::pow(a,b); }
60  }},
61  })
62 {
63  // Cleanup
64  expr.erase(remove_if(expr.begin(), expr.end(), isspace), expr.end());
65 
66  root = MathExpr::parse(expr);
67  panic_if(!root, "Invalid expression\n");
68 }
69 
77 MathExpr::Node *
78 MathExpr::parse(std::string expr) {
79  if (expr.size() == 0)
80  return NULL;
81 
82  // From low to high priority
83  int par = 0;
84  for (unsigned p = 0; p < MAX_PRIO; p++) {
85  for (int i = expr.size() - 1; i >= 0; i--) {
86  if (expr[i] == ')')
87  par++;
88  if (expr[i] == '(')
89  par--;
90 
91  if (par < 0) return NULL;
92  if (par > 0) continue;
93 
94  for (unsigned opt = 0; opt < ops.size(); opt++) {
95  if (ops[opt].priority != p) continue;
96  if (ops[opt].c == expr[i]) {
97  // Try to parse each side
98  Node *l = NULL;
99  if (ops[opt].binary)
100  l = parse(expr.substr(0, i));
101  Node *r = parse(expr.substr(i + 1));
102  if ((l && r) || (!ops[opt].binary && r)) {
103  // Match!
104  Node *n = new Node();
105  n->op = ops[opt].op;
106  n->l = l;
107  n->r = r;
108  return n;
109  }
110  }
111  }
112  }
113  }
114 
115  // Remove trivial parenthesis
116  if (expr.size() >= 2 && expr[0] == '(' && expr[expr.size() - 1] == ')')
117  return parse(expr.substr(1, expr.size() - 2));
118 
119  // Match a number
120  {
121  char *sptr;
122  double v = strtod(expr.c_str(), &sptr);
123  if (sptr != expr.c_str()) {
124  Node *n = new Node();
125  n->op = sValue;
126  n->value = v;
127  return n;
128  }
129  }
130 
131  // Match a variable
132  {
133  bool contains_non_alpha = false;
134  for (auto & c: expr)
135  contains_non_alpha = contains_non_alpha or
136  !( (c >= 'a' && c <= 'z') ||
137  (c >= 'A' && c <= 'Z') ||
138  (c >= '0' && c <= '9') ||
139  c == '$' || c == '\\' || c == '.' || c == '_');
140 
141  if (!contains_non_alpha) {
142  Node * n = new Node();
143  n->op = sVariable;
144  n->variable = expr;
145  return n;
146  }
147  }
148 
149  return NULL;
150 }
151 
152 double
154  if (!n)
155  return 0;
156  else if (n->op == sValue)
157  return n->value;
158  else if (n->op == sVariable)
159  return fn(n->variable);
160 
161  for (auto & opt : ops)
162  if (opt.op == n->op)
163  return opt.fn( eval(n->l, fn), eval(n->r, fn) );
164 
165  panic("Invalid node!\n");
166  return 0;
167 }
168 
169 std::string
170 MathExpr::toStr(Node *n, std::string prefix) const {
171  std::string ret;
172  ret += prefix + "|-- " + n->toStr() + "\n";
173  if (n->r)
174  ret += toStr(n->r, prefix + "| ");
175  if (n->l)
176  ret += toStr(n->l, prefix + "| ");
177  return ret;
178 }
179 
180 void
182  std::vector<std::string> &variables) const
183 {
184  if (!n || n->op == sValue || n->op == nInvalid) {
185  return;
186  } else if (n->op == sVariable) {
187  variables.push_back(n->variable);
188  } else {
189  getVariables(n->l, variables);
190  getVariables(n->r, variables);
191  }
192 }
193 
194 } // namespace gem5
gem5::MathExpr::eval
double eval(EvalCallback fn) const
Evaluates the expression.
Definition: mathexpr.hh:72
gem5::MathExpr::getVariables
std::vector< std::string > getVariables() const
Return all variables in the this expression.
Definition: mathexpr.hh:83
mathexpr.hh
gem5::MathExpr::parse
Node * parse(std::string expr)
Parse and create nodes from string.
Definition: mathexpr.cc:78
gem5::MathExpr::sVariable
@ sVariable
Definition: mathexpr.hh:93
gem5::MathExpr::bAdd
@ bAdd
Definition: mathexpr.hh:93
gem5::MathExpr::nInvalid
@ nInvalid
Definition: mathexpr.hh:93
gem5::MathExpr::bMul
@ bMul
Definition: mathexpr.hh:93
gem5::ArmISA::a
Bitfield< 8 > a
Definition: misc_types.hh:66
std::vector< std::string >
gem5::VegaISA::r
Bitfield< 5 > r
Definition: pagetable.hh:60
gem5::MathExpr::MathExpr
MathExpr(std::string expr)
Definition: mathexpr.cc:50
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:67
gem5::MathExpr::ops
std::array< OpSearch, uNeg+1 > ops
Operator list.
Definition: mathexpr.hh:109
gem5::MathExpr::toStr
std::string toStr() const
Prints an ASCII representation of the expression tree.
Definition: mathexpr.hh:63
gem5::VegaISA::c
Bitfield< 2 > c
Definition: pagetable.hh:63
gem5::ArmISA::b
Bitfield< 7 > b
Definition: misc_types.hh:382
gem5::VegaISA::p
Bitfield< 54 > p
Definition: pagetable.hh:70
gem5::MathExpr::bDiv
@ bDiv
Definition: mathexpr.hh:93
gem5::MathExpr::EvalCallback
std::function< double(std::string)> EvalCallback
Definition: mathexpr.hh:56
gem5::MathExpr::uNeg
@ uNeg
Definition: mathexpr.hh:93
gem5::MathExpr::sValue
@ sValue
Definition: mathexpr.hh:93
gem5::MathExpr::bPow
@ bPow
Definition: mathexpr.hh:93
gem5::MathExpr::OpSearch
Definition: mathexpr.hh:99
gem5::VegaISA::v
Bitfield< 0 > v
Definition: pagetable.hh:65
panic_if
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Definition: logging.hh:204
gem5::MathExpr::Node
Definition: mathexpr.hh:111
std
Overload hash function for BasicBlockRange type.
Definition: misc.hh:2388
gem5::ArmISA::n
Bitfield< 31 > n
Definition: misc_types.hh:456
gem5::MathExpr::bSub
@ bSub
Definition: mathexpr.hh:93
logging.hh
gem5::ArmISA::fn
Bitfield< 18, 16 > fn
Definition: types.hh:149
gem5::MathExpr::MAX_PRIO
const int MAX_PRIO
Definition: mathexpr.hh:97
gem5::VegaISA::l
Bitfield< 55 > l
Definition: pagetable.hh:54
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:178

Generated on Thu Jul 28 2022 13:32:36 for gem5 by doxygen 1.8.17