Permalink
Browse files

Add support for power in constant expressions.

  • Loading branch information...
steve
steve committed Jul 31, 2006
1 parent 06d6ac4 commit 49b65e86fee99fb1dcf0b4d947b6bd3bc9a0edfc
Showing with 248 additions and 21 deletions.
  1. +7 −1 design_dump.cc
  2. +9 −1 elab_expr.cc
  3. +78 −1 eval_tree.cc
  4. +12 −1 expr_synth.cc
  5. +2 −1 lexor.lex
  6. +18 −7 net_design.cc
  7. +38 −1 net_expr.cc
  8. +27 −1 netlist.h
  9. +9 −2 parse.y
  10. +10 −1 set_width.cc
  11. +15 −1 verinum.cc
  12. +6 −1 verinum.h
  13. +11 −1 verireal.cc
  14. +6 −1 verireal.h
View
@@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
-#ident "$Id: design_dump.cc,v 1.166 2006/06/18 04:15:50 steve Exp $"
+#ident "$Id: design_dump.cc,v 1.167 2006/07/31 03:50:17 steve Exp $"
#endif
# include "config.h"
@@ -991,6 +991,9 @@ void NetEBinary::dump(ostream&o) const
case 'O':
o << "~|";
break;
+ case 'p':
+ o << "**";
+ break;
case 'r':
o << ">>";
break;
@@ -1180,6 +1183,9 @@ void Design::dump(ostream&o) const
/*
* $Log: design_dump.cc,v $
+ * Revision 1.167 2006/07/31 03:50:17 steve
+ * Add support for power in constant expressions.
+ *
* Revision 1.166 2006/06/18 04:15:50 steve
* Add support for system functions in continuous assignments.
*
View
@@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
-#ident "$Id: elab_expr.cc,v 1.106 2006/07/07 04:06:37 steve Exp $"
+#ident "$Id: elab_expr.cc,v 1.107 2006/07/31 03:50:17 steve Exp $"
#endif
# include "config.h"
@@ -101,6 +101,11 @@ NetEBinary* PEBinary::elaborate_expr_base_(Design*des,
tmp->set_line(*this);
break;
+ case 'p':
+ tmp = new NetEBPow(op_, lp, rp);
+ tmp->set_line(*this);
+ break;
+
case '*':
tmp = new NetEBMult(op_, lp, rp);
tmp->set_line(*this);
@@ -1382,6 +1387,9 @@ NetExpr* PEUnary::elaborate_expr(Design*des, NetScope*scope,
/*
* $Log: elab_expr.cc,v $
+ * Revision 1.107 2006/07/31 03:50:17 steve
+ * Add support for power in constant expressions.
+ *
* Revision 1.106 2006/07/07 04:06:37 steve
* Fix context determined with of constants.
*
View
@@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
-#ident "$Id: eval_tree.cc,v 1.68 2006/03/18 22:52:27 steve Exp $"
+#ident "$Id: eval_tree.cc,v 1.69 2006/07/31 03:50:17 steve Exp $"
#endif
# include "config.h"
@@ -937,6 +937,80 @@ NetExpr* NetEBMult::eval_tree()
return new NetEConst(lval * rval);
}
+NetExpr* NetEBPow::eval_tree_real_()
+{
+ verireal lval;
+ verireal rval;
+
+ switch (left_->expr_type()) {
+ case IVL_VT_REAL: {
+ NetECReal*lc = dynamic_cast<NetECReal*> (left_);
+ if (lc == 0) return 0;
+ lval = lc->value();
+ break;
+ }
+
+ case IVL_VT_BOOL:
+ case IVL_VT_LOGIC: {
+ NetEConst*lc = dynamic_cast<NetEConst*>(left_);
+ if (lc == 0) return 0;
+ verinum tmp = lc->value();
+ lval = verireal(tmp.as_long());
+ break;
+ }
+
+ default:
+ assert(0);
+ }
+
+ switch (right_->expr_type()) {
+ case IVL_VT_REAL: {
+ NetECReal*rc = dynamic_cast<NetECReal*> (right_);
+ if (rc == 0) return 0;
+ rval = rc->value();
+ break;
+ }
+
+ case IVL_VT_BOOL:
+ case IVL_VT_LOGIC: {
+ NetEConst*rc = dynamic_cast<NetEConst*>(right_);
+ if (rc == 0) return 0;
+ verinum tmp = rc->value();
+ rval = verireal(tmp.as_long());
+ break;
+ }
+
+ default:
+ assert(0);
+ }
+
+
+ NetECReal*res = new NetECReal( pow(lval,rval) );
+ res->set_line(*this);
+ return res;
+}
+
+NetExpr* NetEBPow::eval_tree()
+{
+ eval_sub_tree_();
+
+ if (expr_type() == IVL_VT_REAL)
+ return eval_tree_real_();
+
+ assert(expr_type() == IVL_VT_LOGIC);
+
+ NetEConst*lc = dynamic_cast<NetEConst*>(left_);
+ if (lc == 0) return 0;
+ NetEConst*rc = dynamic_cast<NetEConst*>(right_);
+ if (rc == 0) return 0;
+
+ verinum lval = lc->value();
+ verinum rval = rc->value();
+
+ return new NetEConst( pow(lval,rval) );
+
+}
+
/*
* Evaluate the shift operator if possible. For this to work, both
* operands must be constant.
@@ -1589,6 +1663,9 @@ NetEConst* NetEUReduce::eval_tree()
/*
* $Log: eval_tree.cc,v $
+ * Revision 1.69 2006/07/31 03:50:17 steve
+ * Add support for power in constant expressions.
+ *
* Revision 1.68 2006/03/18 22:52:27 steve
* Properly handle signedness in compare.
*
View
@@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
-#ident "$Id: expr_synth.cc,v 1.78 2006/07/08 21:48:46 steve Exp $"
+#ident "$Id: expr_synth.cc,v 1.79 2006/07/31 03:50:17 steve Exp $"
#endif
# include "config.h"
@@ -234,6 +234,14 @@ NetNet* NetEBComp::synthesize(Design*des)
return osig;
}
+NetNet* NetEBPow::synthesize(Design*des)
+{
+ cerr << get_line() << ": internal error: Do not yet know how to handle"
+ << " power operator in this context." << endl;
+ des->errors += 1;
+ return 0;
+}
+
NetNet* NetEBMult::synthesize(Design*des)
{
NetNet*lsig = left_->synthesize(des);
@@ -882,6 +890,9 @@ NetNet* NetESignal::synthesize(Design*des)
/*
* $Log: expr_synth.cc,v $
+ * Revision 1.79 2006/07/31 03:50:17 steve
+ * Add support for power in constant expressions.
+ *
* Revision 1.78 2006/07/08 21:48:46 steve
* Handle real valued literals in net contexts.
*
View
@@ -21,7 +21,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
-#ident "$Id: lexor.lex,v 1.91 2006/05/17 04:15:12 steve Exp $"
+#ident "$Id: lexor.lex,v 1.92 2006/07/31 03:50:17 steve Exp $"
#endif
# include "config.h"
@@ -134,6 +134,7 @@ W [ \t\b\f\r]+
"<<<" { return K_LS; /* Note: Functionally, <<< is the same as <<. */}
">>" { return K_RS; }
">>>" { return K_RSS; }
+"**" { return K_POW; }
"<=" { return K_LE; }
">=" { return K_GE; }
"=>" { return K_EG; }
View
@@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
-#ident "$Id: net_design.cc,v 1.48 2005/11/27 05:56:20 steve Exp $"
+#ident "$Id: net_design.cc,v 1.49 2006/07/31 03:50:17 steve Exp $"
#endif
# include "config.h"
@@ -327,12 +327,20 @@ void NetScope::evaluate_parameters(Design*des)
switch (expr->expr_type()) {
case IVL_VT_REAL:
if (! dynamic_cast<const NetECReal*>(expr)) {
- cerr << (*cur).second.expr->get_line()
- << ": internal error: "
- "unable to evaluate real parameter values: " <<
- *expr << endl;
- des->errors += 1;
- continue;
+
+ NetExpr*nexpr = expr->eval_tree();
+ if (nexpr == 0) {
+ cerr << (*cur).second.expr->get_line()
+ << ": internal error: "
+ << "unable to evaluate real parameter value: "
+ << *expr << endl;
+ des->errors += 1;
+ continue;
+ }
+
+ assert(nexpr);
+ delete expr;
+ (*cur).second.expr = nexpr;
}
break;
@@ -554,6 +562,9 @@ void Design::delete_process(NetProcTop*top)
/*
* $Log: net_design.cc,v $
+ * Revision 1.49 2006/07/31 03:50:17 steve
+ * Add support for power in constant expressions.
+ *
* Revision 1.48 2005/11/27 05:56:20 steve
* Handle bit select of parameter with ranges.
*
View
@@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
-#ident "$Id: net_expr.cc,v 1.26 2005/11/26 00:35:43 steve Exp $"
+#ident "$Id: net_expr.cc,v 1.27 2006/07/31 03:50:17 steve Exp $"
#endif
# include "config.h"
@@ -248,6 +248,40 @@ ivl_variable_type_t NetEBMult::expr_type() const
return IVL_VT_LOGIC;
}
+NetEBPow::NetEBPow(char op, NetExpr*l, NetExpr*r)
+: NetEBinary(op, l, r)
+{
+ assert(op == 'p');
+ expr_width(l->expr_width());
+ cast_signed(l->has_sign() || r->has_sign());
+}
+
+NetEBPow::~NetEBPow()
+{
+}
+
+NetEBPow* NetEBPow::dup_expr() const
+{
+ NetEBPow*result = new NetEBPow(op_, left_->dup_expr(),
+ right_->dup_expr());
+ result->set_line(*this);
+ return result;
+}
+
+ivl_variable_type_t NetEBPow::expr_type() const
+{
+ if (right_->expr_type() == IVL_VT_REAL)
+ return IVL_VT_REAL;
+ if (left_->expr_type() == IVL_VT_REAL)
+ return IVL_VT_REAL;
+ if (left_->has_sign())
+ return IVL_VT_REAL;
+ if (right_->has_sign())
+ return IVL_VT_REAL;
+
+ return IVL_VT_LOGIC;
+}
+
NetEBShift::NetEBShift(char op, NetExpr*l, NetExpr*r)
: NetEBinary(op, l, r)
{
@@ -526,6 +560,9 @@ ivl_variable_type_t NetESFunc::expr_type() const
/*
* $Log: net_expr.cc,v $
+ * Revision 1.27 2006/07/31 03:50:17 steve
+ * Add support for power in constant expressions.
+ *
* Revision 1.26 2005/11/26 00:35:43 steve
* More precise about r-value width of constants.
*
View
@@ -19,7 +19,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
-#ident "$Id: netlist.h,v 1.358 2006/06/18 04:15:50 steve Exp $"
+#ident "$Id: netlist.h,v 1.359 2006/07/31 03:50:17 steve Exp $"
#endif
/*
@@ -2559,6 +2559,7 @@ class NetProcTop : public LineInfo, public Attrib {
* * -- Arithmetic multiply
* / -- Arithmetic divide
* % -- Arithmetic modulus
+ * p -- Arithmetic power (**)
* & -- Bit-wise AND
* | -- Bit-wise OR
* < -- Less then
@@ -2762,6 +2763,28 @@ class NetEBMult : public NetEBinary {
};
+/*
+ * Support the binary multiplication (*) operator.
+ */
+class NetEBPow : public NetEBinary {
+
+ public:
+ NetEBPow(char op, NetExpr*l, NetExpr*r);
+ ~NetEBPow();
+
+ virtual ivl_variable_type_t expr_type() const;
+
+ virtual bool set_width(unsigned w, bool last_chance);
+ virtual NetEBPow* dup_expr() const;
+ virtual NetExpr* eval_tree();
+ virtual NetNet* synthesize(Design*);
+
+ private:
+
+ NetExpr* eval_tree_real_();
+
+};
+
/*
* The binary logical operators are those that return boolean
@@ -3486,6 +3509,9 @@ extern ostream& operator << (ostream&, NetNet::Type);
/*
* $Log: netlist.h,v $
+ * Revision 1.359 2006/07/31 03:50:17 steve
+ * Add support for power in constant expressions.
+ *
* Revision 1.358 2006/06/18 04:15:50 steve
* Add support for system functions in continuous assignments.
*
Oops, something went wrong.

0 comments on commit 49b65e8

Please sign in to comment.