1999-09-20 04:21:10 +02:00
|
|
|
/*
|
2011-02-26 23:59:52 +01:00
|
|
|
* Copyright (c) 1999-2011 Stephen Williams (steve@icarus.com)
|
1999-09-20 04:21:10 +02:00
|
|
|
*
|
|
|
|
|
* This source code is free software; you can redistribute it
|
|
|
|
|
* and/or modify it in source code form under the terms of the GNU
|
|
|
|
|
* General Public License as published by the Free Software
|
|
|
|
|
* Foundation; either version 2 of the License, or (at your option)
|
|
|
|
|
* any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-08-29 03:41:23 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
1999-09-20 04:21:10 +02:00
|
|
|
*/
|
|
|
|
|
|
2001-07-25 05:10:48 +02:00
|
|
|
# include "config.h"
|
2003-03-11 00:40:53 +01:00
|
|
|
# include "compiler.h"
|
2001-07-25 05:10:48 +02:00
|
|
|
|
|
|
|
|
# include <iostream>
|
2008-01-05 00:23:47 +01:00
|
|
|
# include <cstdlib>
|
2008-08-20 22:42:51 +02:00
|
|
|
# include <cstring>
|
2010-05-31 22:12:06 +02:00
|
|
|
# include <cmath>
|
2001-07-25 05:10:48 +02:00
|
|
|
|
1999-09-20 04:21:10 +02:00
|
|
|
# include "netlist.h"
|
2007-04-07 06:46:18 +02:00
|
|
|
# include "ivl_assert.h"
|
2008-03-08 03:51:50 +01:00
|
|
|
# include "netmisc.h"
|
1999-09-20 04:21:10 +02:00
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
NetExpr* NetExpr::eval_tree()
|
1999-09-20 04:21:10 +02:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
static bool get_real_arg_(const NetExpr*expr, verireal&val)
|
2007-04-07 06:46:18 +02:00
|
|
|
{
|
2008-03-08 03:51:50 +01:00
|
|
|
switch (expr->expr_type()) {
|
2007-04-07 06:46:18 +02:00
|
|
|
case IVL_VT_REAL: {
|
2012-05-29 22:56:16 +02:00
|
|
|
const NetECReal*c = dynamic_cast<const NetECReal*> (expr);
|
2008-03-08 03:51:50 +01:00
|
|
|
if (c == 0) return false;
|
|
|
|
|
val = c->value();
|
2007-04-07 06:46:18 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case IVL_VT_BOOL:
|
|
|
|
|
case IVL_VT_LOGIC: {
|
2012-05-29 22:56:16 +02:00
|
|
|
const NetEConst*c = dynamic_cast<const NetEConst*>(expr);
|
2008-03-08 03:51:50 +01:00
|
|
|
if (c == 0) return false;
|
|
|
|
|
verinum tmp = c->value();
|
|
|
|
|
val = verireal(tmp.as_double());
|
2007-04-07 06:46:18 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 05:29:24 +02:00
|
|
|
case IVL_VT_DARRAY:
|
|
|
|
|
return false;
|
|
|
|
|
|
2007-04-07 06:46:18 +02:00
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
2007-04-07 06:46:18 +02:00
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
static bool get_real_arguments(const NetExpr*le, const NetExpr*re,
|
2010-11-01 22:37:06 +01:00
|
|
|
double&lval, double&rval)
|
|
|
|
|
{
|
|
|
|
|
verireal val;
|
|
|
|
|
|
|
|
|
|
if (!get_real_arg_(le, val)) return false;
|
|
|
|
|
lval = val.as_double();
|
|
|
|
|
|
|
|
|
|
if (!get_real_arg_(re, val)) return false;
|
|
|
|
|
rval = val.as_double();
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
bool NetEBinary::get_real_arguments_(verireal&lval, verireal&rval)
|
|
|
|
|
{
|
|
|
|
|
if (!get_real_arg_(left_, lval)) return false;
|
|
|
|
|
if (!get_real_arg_(right_, rval)) return false;
|
2007-04-07 06:46:18 +02:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
NetECReal* NetEBAdd::eval_tree_real_(const NetExpr*l, const NetExpr*r) const
|
2010-11-01 22:37:06 +01:00
|
|
|
{
|
2012-05-29 22:56:16 +02:00
|
|
|
double lval;
|
|
|
|
|
double rval;
|
2010-11-01 22:37:06 +01:00
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
bool flag = get_real_arguments(l, r, lval, rval);
|
2010-11-01 22:37:06 +01:00
|
|
|
if (!flag) return 0;
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
double res_val;
|
2010-11-01 22:37:06 +01:00
|
|
|
|
|
|
|
|
switch (op()) {
|
|
|
|
|
case '+':
|
|
|
|
|
res_val = lval + rval;
|
|
|
|
|
break;
|
|
|
|
|
case '-':
|
|
|
|
|
res_val = lval - rval;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
ivl_assert(*this, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
NetECReal*res = new NetECReal( verireal(res_val) );
|
2010-11-01 22:37:06 +01:00
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
res->set_line(*this);
|
|
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated (real): " << *this
|
|
|
|
|
<< " --> " << *res << endl;
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
NetExpr* NetEBAdd::eval_tree()
|
1999-10-11 01:29:37 +02:00
|
|
|
{
|
2011-02-26 23:59:52 +01:00
|
|
|
eval_expr(left_);
|
|
|
|
|
eval_expr(right_);
|
2007-04-07 06:46:18 +02:00
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
// First try to elaborate the expression completely.
|
|
|
|
|
NetExpr*res = eval_arguments_(left_,right_);
|
|
|
|
|
if (res != 0)
|
2010-11-01 22:37:06 +01:00
|
|
|
return res;
|
2005-11-10 14:28:11 +01:00
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
// If the expression type is real, then do not attempt the
|
|
|
|
|
// following alternative processing.
|
|
|
|
|
if (expr_type() == IVL_VT_REAL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
// The expression has not evaluated to a constant. Let's still
|
|
|
|
|
// try to optimize by trying to combine a right constant value
|
|
|
|
|
// with the right constant value of a sub-expression add. For
|
|
|
|
|
// example, the expression (a + 2) - 1 can be rewritten as a + 1.
|
2005-11-10 14:28:11 +01:00
|
|
|
|
|
|
|
|
NetEBAdd*se = dynamic_cast<NetEBAdd*>(left_);
|
2012-05-29 22:56:16 +02:00
|
|
|
NetEConst*lc = se? dynamic_cast<NetEConst*>(se->right_) : 0;
|
|
|
|
|
NetEConst*rc = dynamic_cast<NetEConst*>(right_);
|
2005-11-10 14:28:11 +01:00
|
|
|
|
|
|
|
|
if (lc != 0 && rc != 0) {
|
2008-12-19 06:33:31 +01:00
|
|
|
ivl_assert(*this, se != 0);
|
|
|
|
|
|
|
|
|
|
if (debug_eval_tree) {
|
|
|
|
|
cerr << get_fileline() << ": debug: "
|
2009-03-08 02:16:13 +01:00
|
|
|
<< "Partially evaluate " << *this
|
2008-12-19 06:33:31 +01:00
|
|
|
<< " using (a+2)-1 --> (a+1) transform." << endl;
|
|
|
|
|
}
|
|
|
|
|
|
2005-11-10 14:28:11 +01:00
|
|
|
verinum lval = lc->value();
|
|
|
|
|
verinum rval = rc->value();
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
unsigned wid = expr_width();
|
|
|
|
|
ivl_assert(*this, wid > 0);
|
|
|
|
|
ivl_assert(*this, lval.len() == wid);
|
|
|
|
|
ivl_assert(*this, rval.len() == wid);
|
2008-10-04 04:52:26 +02:00
|
|
|
|
2005-11-10 14:28:11 +01:00
|
|
|
verinum val;
|
|
|
|
|
if (op_ == se->op_) {
|
|
|
|
|
/* (a + lval) + rval --> a + (rval+lval) */
|
|
|
|
|
/* (a - lval) - rval --> a - (rval+lval) */
|
2011-02-26 23:59:52 +01:00
|
|
|
val = verinum(rval + lval, wid);
|
2005-11-10 14:28:11 +01:00
|
|
|
} else {
|
|
|
|
|
/* (a - lval) + rval --> a + (rval-lval) */
|
|
|
|
|
/* (a + lval) - rval --> a - (rval-lval) */
|
2011-02-26 23:59:52 +01:00
|
|
|
val = verinum(rval - lval, wid);
|
2008-10-04 04:52:26 +02:00
|
|
|
}
|
|
|
|
|
|
2005-11-10 14:28:11 +01:00
|
|
|
NetEConst*tmp = new NetEConst(val);
|
|
|
|
|
left_ = se->left_->dup_expr();
|
|
|
|
|
delete se;
|
2008-03-08 03:51:50 +01:00
|
|
|
tmp->set_line(*right_);
|
2005-11-10 14:28:11 +01:00
|
|
|
delete right_;
|
|
|
|
|
right_ = tmp;
|
1999-10-11 01:29:37 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
// We may have changed the subexpression, but the result is
|
|
|
|
|
// still not constant, so return nil here anyhow.
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetExpr* NetEBAdd::eval_arguments_(const NetExpr*l, const NetExpr*r) const
|
|
|
|
|
{
|
|
|
|
|
if (expr_type() == IVL_VT_REAL)
|
|
|
|
|
return eval_tree_real_(l,r);
|
|
|
|
|
|
|
|
|
|
const NetEConst*lc = dynamic_cast<const NetEConst*>(l);
|
|
|
|
|
const NetEConst*rc = dynamic_cast<const NetEConst*>(r);
|
|
|
|
|
|
|
|
|
|
/* If both operands are constant, then replace the entire
|
|
|
|
|
expression with a constant value. */
|
|
|
|
|
if (lc != 0 && rc != 0) {
|
|
|
|
|
verinum lval = lc->value();
|
|
|
|
|
verinum rval = rc->value();
|
|
|
|
|
|
|
|
|
|
unsigned wid = expr_width();
|
|
|
|
|
ivl_assert(*this, wid > 0);
|
|
|
|
|
ivl_assert(*this, lval.len() == wid);
|
|
|
|
|
ivl_assert(*this, rval.len() == wid);
|
|
|
|
|
|
|
|
|
|
verinum val;
|
|
|
|
|
switch (op_) {
|
|
|
|
|
case '+':
|
|
|
|
|
val = verinum(lval + rval, wid);
|
|
|
|
|
break;
|
|
|
|
|
case '-':
|
|
|
|
|
val = verinum(lval - rval, wid);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetEConst *res = new NetEConst(val);
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
res->set_line(*this);
|
|
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated: " << *this
|
|
|
|
|
<< " --> " << *res << endl;
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-11-10 14:28:11 +01:00
|
|
|
/* Nothing more to be done, the value is not constant. */
|
|
|
|
|
return 0;
|
1999-10-11 01:29:37 +02:00
|
|
|
}
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
NetEConst* NetEBBits::eval_tree()
|
2007-04-07 06:46:18 +02:00
|
|
|
{
|
2010-11-01 22:37:06 +01:00
|
|
|
if (debug_eval_tree) {
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluating expression:"
|
2011-02-26 23:59:52 +01:00
|
|
|
<< *this << endl;
|
2007-04-07 06:46:18 +02:00
|
|
|
}
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
eval_expr(left_);
|
|
|
|
|
eval_expr(right_);
|
2001-01-02 04:23:40 +01:00
|
|
|
|
|
|
|
|
NetEConst*lc = dynamic_cast<NetEConst*>(left_);
|
|
|
|
|
NetEConst*rc = dynamic_cast<NetEConst*>(right_);
|
2008-03-08 03:51:50 +01:00
|
|
|
if (lc == 0 || rc == 0) return 0;
|
2002-05-25 18:43:47 +02:00
|
|
|
|
|
|
|
|
/* Notice the special case where one of the operands is 0 and
|
|
|
|
|
this is a bitwise &. If this happens, then the result is
|
|
|
|
|
known to be 0. */
|
2008-03-08 03:51:50 +01:00
|
|
|
if ((op() == '&') && (lc->value() == verinum(0))) {
|
2002-05-25 18:43:47 +02:00
|
|
|
verinum res (verinum::V0, expr_width());
|
|
|
|
|
return new NetEConst(res);
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
if ((op() == '&') && (rc->value() == verinum(0))) {
|
2002-05-25 18:43:47 +02:00
|
|
|
verinum res (verinum::V0, expr_width());
|
|
|
|
|
return new NetEConst(res);
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-02 04:23:40 +01:00
|
|
|
verinum lval = lc->value();
|
|
|
|
|
verinum rval = rc->value();
|
|
|
|
|
|
|
|
|
|
unsigned wid = expr_width();
|
2011-02-26 23:59:52 +01:00
|
|
|
ivl_assert(*this, wid > 0);
|
|
|
|
|
ivl_assert(*this, lval.len() == wid);
|
|
|
|
|
ivl_assert(*this, rval.len() == wid);
|
2001-01-02 04:23:40 +01:00
|
|
|
|
|
|
|
|
verinum res (verinum::V0, wid);
|
|
|
|
|
|
|
|
|
|
switch (op()) {
|
|
|
|
|
|
|
|
|
|
case '|': {
|
2011-02-26 23:59:52 +01:00
|
|
|
for (unsigned idx = 0 ; idx < wid ; idx += 1)
|
2001-01-02 04:23:40 +01:00
|
|
|
res.set(idx, lval.get(idx) | rval.get(idx));
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case '&': {
|
2011-02-26 23:59:52 +01:00
|
|
|
for (unsigned idx = 0 ; idx < wid ; idx += 1)
|
2001-01-02 04:23:40 +01:00
|
|
|
res.set(idx, lval.get(idx) & rval.get(idx));
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2003-10-26 05:54:56 +01:00
|
|
|
case '^': {
|
2011-02-26 23:59:52 +01:00
|
|
|
for (unsigned idx = 0 ; idx < wid ; idx += 1)
|
2003-10-26 05:54:56 +01:00
|
|
|
res.set(idx, lval.get(idx) ^ rval.get(idx));
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-02 04:23:40 +01:00
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new NetEConst(res);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
NetEConst* NetEBComp::eval_less_(const NetExpr*le, const NetExpr*re) const
|
2001-02-09 06:44:23 +01:00
|
|
|
{
|
2012-05-29 22:56:16 +02:00
|
|
|
if (le->expr_type() == IVL_VT_REAL || re->expr_type() == IVL_VT_REAL)
|
|
|
|
|
return eval_leeq_real_(le, re, false);
|
2008-01-24 05:43:13 +01:00
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
const NetEConst*rc = dynamic_cast<const NetEConst*>(re);
|
2010-11-01 22:37:06 +01:00
|
|
|
if (rc == 0) return 0;
|
2001-02-09 06:44:23 +01:00
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
verinum rv = rc->value();
|
2001-02-09 06:44:23 +01:00
|
|
|
if (! rv.is_defined()) {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::Vx, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2001-02-09 06:44:23 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
if (NetEConst*tmp = must_be_leeq_(le, rv, false)) {
|
2008-01-24 05:43:13 +01:00
|
|
|
return tmp;
|
2001-02-09 06:44:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Now go on to the normal test of the values. */
|
2012-05-29 22:56:16 +02:00
|
|
|
const NetEConst*lc = dynamic_cast<const NetEConst*>(le);
|
2010-11-01 22:37:06 +01:00
|
|
|
if (lc == 0) return 0;
|
|
|
|
|
|
|
|
|
|
verinum lv = lc->value();
|
2001-02-09 06:44:23 +01:00
|
|
|
if (! lv.is_defined()) {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::Vx, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2001-02-09 06:44:23 +01:00
|
|
|
}
|
|
|
|
|
|
2008-01-24 05:43:13 +01:00
|
|
|
if (lv < rv) {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::V1, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2002-04-27 05:17:15 +02:00
|
|
|
} else {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::V0, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2003-06-05 06:28:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
NetEConst* NetEBComp::must_be_leeq_(const NetExpr*le, const verinum&rv, bool eq_flag) const
|
2008-01-24 05:43:13 +01:00
|
|
|
{
|
|
|
|
|
assert(le->expr_width() > 0);
|
|
|
|
|
verinum lv (verinum::V1, le->expr_width());
|
|
|
|
|
if (le->has_sign() && rv.has_sign()) {
|
|
|
|
|
// If the expression is signed, then the largest
|
|
|
|
|
// possible value for the left_ needs to have a 0 in the
|
|
|
|
|
// sign position.
|
|
|
|
|
lv.set(lv.len()-1, verinum::V0);
|
|
|
|
|
lv.has_sign(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lv < rv || (eq_flag && (lv == rv))) {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::V1, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2008-01-24 05:43:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
NetEConst* NetEBComp::eval_leeq_real_(const NetExpr*le, const NetExpr*re, bool eq_flag) const
|
2010-11-01 22:37:06 +01:00
|
|
|
{
|
|
|
|
|
double lval;
|
|
|
|
|
double rval;
|
|
|
|
|
|
|
|
|
|
bool flag = get_real_arguments(le, re, lval, rval);
|
|
|
|
|
if (! flag) return 0;
|
|
|
|
|
|
|
|
|
|
bool tmp = false;
|
|
|
|
|
if (lval < rval) tmp = true;
|
|
|
|
|
if (tmp == false && eq_flag && lval == rval) tmp = true;
|
|
|
|
|
|
|
|
|
|
verinum result(tmp ? verinum::V1 : verinum::V0, 1);
|
|
|
|
|
NetEConst*res = new NetEConst(result);
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
NetEConst* NetEBComp::eval_leeq_(const NetExpr*le, const NetExpr*re) const
|
1999-09-20 04:21:10 +02:00
|
|
|
{
|
2012-05-29 22:56:16 +02:00
|
|
|
if (le->expr_type() == IVL_VT_REAL || re->expr_type() == IVL_VT_REAL)
|
|
|
|
|
return eval_leeq_real_(le, re, true);
|
2010-11-01 22:37:06 +01:00
|
|
|
// assert(expr_type() == IVL_VT_LOGIC);
|
2003-06-05 06:28:24 +02:00
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
const NetEConst*r = dynamic_cast<const NetEConst*>(re);
|
1999-09-20 04:21:10 +02:00
|
|
|
if (r == 0) return 0;
|
|
|
|
|
|
|
|
|
|
verinum rv = r->value();
|
2000-12-16 20:03:30 +01:00
|
|
|
if (! rv.is_defined()) {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::Vx, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2000-12-16 20:03:30 +01:00
|
|
|
}
|
1999-09-20 04:21:10 +02:00
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
if (le->expr_width() == 0) {
|
2007-12-20 18:31:01 +01:00
|
|
|
cerr << get_fileline() << ": internal error: Something wrong "
|
2003-06-04 03:26:17 +02:00
|
|
|
<< "with the left side width of <= ?" << endl;
|
2007-12-20 18:31:01 +01:00
|
|
|
cerr << get_fileline() << ": : " << *this << endl;
|
2003-06-04 03:26:17 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
if (NetEConst*tmp = must_be_leeq_(le, rv, true)) {
|
2008-01-24 05:43:13 +01:00
|
|
|
return tmp;
|
1999-09-20 04:21:10 +02:00
|
|
|
}
|
|
|
|
|
|
2000-12-16 20:03:30 +01:00
|
|
|
/* Now go on to the normal test of the values. */
|
2012-05-29 22:56:16 +02:00
|
|
|
const NetEConst*l = dynamic_cast<const NetEConst*>(le);
|
2000-12-16 21:00:17 +01:00
|
|
|
if (l == 0) return 0;
|
2010-11-01 22:37:06 +01:00
|
|
|
|
2008-01-24 05:43:13 +01:00
|
|
|
verinum lv = l->value();
|
2000-12-16 20:03:30 +01:00
|
|
|
if (! lv.is_defined()) {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::Vx, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2000-12-16 20:03:30 +01:00
|
|
|
}
|
|
|
|
|
|
2008-01-24 05:43:13 +01:00
|
|
|
if (lv <= rv) {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::V1, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2002-04-24 04:47:02 +02:00
|
|
|
} else {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::V0, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2000-12-16 20:03:30 +01:00
|
|
|
}
|
1999-09-20 04:21:10 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
NetEConst* NetEBComp::eval_gt_(const NetExpr*le, const NetExpr*re) const
|
2001-02-10 21:29:39 +01:00
|
|
|
{
|
2012-05-29 22:56:16 +02:00
|
|
|
if (le->expr_type() == IVL_VT_REAL || re->expr_type() == IVL_VT_REAL)
|
|
|
|
|
return eval_leeq_real_(re, le, false);
|
2003-04-15 07:06:56 +02:00
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
const NetEConst*l = dynamic_cast<const NetEConst*>(le);
|
2001-02-10 21:29:39 +01:00
|
|
|
if (l == 0) return 0;
|
|
|
|
|
|
|
|
|
|
verinum lv = l->value();
|
|
|
|
|
if (! lv.is_defined()) {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::Vx, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2001-02-10 21:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
if (NetEConst*tmp = must_be_leeq_(re, lv, false)) {
|
2008-01-24 05:43:13 +01:00
|
|
|
return tmp;
|
2001-02-10 21:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Now go on to the normal test of the values. */
|
2012-05-29 22:56:16 +02:00
|
|
|
const NetEConst*r = dynamic_cast<const NetEConst*>(re);
|
2001-02-10 21:29:39 +01:00
|
|
|
if (r == 0) return 0;
|
2010-11-01 22:37:06 +01:00
|
|
|
|
2003-03-15 19:07:58 +01:00
|
|
|
verinum rv = r->value();
|
2001-02-10 21:29:39 +01:00
|
|
|
if (! rv.is_defined()) {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::Vx, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2001-02-10 21:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
2008-01-24 05:43:13 +01:00
|
|
|
if (lv > rv) {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::V1, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2008-01-24 05:43:13 +01:00
|
|
|
} else {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::V0, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2001-02-10 21:29:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
NetEConst* NetEBComp::eval_gteq_(const NetExpr*le, const NetExpr*re) const
|
2001-02-10 21:29:39 +01:00
|
|
|
{
|
2012-05-29 22:56:16 +02:00
|
|
|
if (le->expr_type() == IVL_VT_REAL || re->expr_type() == IVL_VT_REAL)
|
|
|
|
|
return eval_leeq_real_(re, le, true);
|
2003-04-15 07:06:56 +02:00
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
const NetEConst*l = dynamic_cast<const NetEConst*>(left_);
|
2001-02-10 21:29:39 +01:00
|
|
|
if (l == 0) return 0;
|
|
|
|
|
|
|
|
|
|
verinum lv = l->value();
|
|
|
|
|
if (! lv.is_defined()) {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::Vx, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2001-02-10 21:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
if (NetEConst*tmp = must_be_leeq_(re, lv, true)) {
|
2008-01-24 05:43:13 +01:00
|
|
|
return tmp;
|
2003-04-15 07:06:56 +02:00
|
|
|
}
|
|
|
|
|
|
2001-02-10 21:29:39 +01:00
|
|
|
/* Now go on to the normal test of the values. */
|
2012-05-29 22:56:16 +02:00
|
|
|
const NetEConst*r = dynamic_cast<const NetEConst*>(re);
|
2001-02-10 21:29:39 +01:00
|
|
|
if (r == 0) return 0;
|
2010-11-01 22:37:06 +01:00
|
|
|
|
2003-04-15 07:06:56 +02:00
|
|
|
verinum rv = r->value();
|
2001-02-10 21:29:39 +01:00
|
|
|
if (! rv.is_defined()) {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::Vx, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2001-02-10 21:29:39 +01:00
|
|
|
}
|
|
|
|
|
|
2008-01-24 05:43:13 +01:00
|
|
|
if (lv >= rv) {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::V1, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2006-03-18 23:52:27 +01:00
|
|
|
} else {
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = new NetEConst(verinum(verinum::V0, 1));
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
return res;
|
2001-02-10 21:29:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-14 07:22:59 +02:00
|
|
|
/*
|
|
|
|
|
* Evaluate <A>==<B> or <A>!=<B>. The equality operator checks all the
|
|
|
|
|
* bits and returns true(false) if there are any bits in the vector
|
|
|
|
|
* that are defined (0 or 1) and different. If all the defined bits
|
|
|
|
|
* are equal, but there are are x/z bits, then the situation is
|
|
|
|
|
* ambiguous so the result is x.
|
|
|
|
|
*/
|
2012-05-29 22:56:16 +02:00
|
|
|
NetEConst* NetEBComp::eval_eqeq_real_(bool ne_flag, const NetExpr*le, const NetExpr*re) const
|
2008-08-29 19:52:09 +02:00
|
|
|
{
|
2012-05-29 22:56:16 +02:00
|
|
|
double lval;
|
|
|
|
|
double rval;
|
2008-08-29 19:52:09 +02:00
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
bool flag = get_real_arguments(le, re, lval, rval);
|
2010-11-01 22:37:06 +01:00
|
|
|
if (! flag) return 0;
|
2008-08-29 19:52:09 +02:00
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
verinum result(((lval == rval) ^ ne_flag) ?
|
2010-11-01 22:37:06 +01:00
|
|
|
verinum::V1 : verinum::V0, 1);
|
|
|
|
|
NetEConst*res = new NetEConst(result);
|
|
|
|
|
ivl_assert(*this, res);
|
2008-08-29 19:52:09 +02:00
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
return res;
|
2008-08-29 19:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
NetEConst* NetEBComp::eval_eqeq_(bool ne_flag, const NetExpr*le, const NetExpr*re) const
|
2000-09-29 06:42:56 +02:00
|
|
|
{
|
2012-05-29 22:56:16 +02:00
|
|
|
if (le->expr_type() == IVL_VT_REAL ||
|
|
|
|
|
re->expr_type() == IVL_VT_REAL)
|
|
|
|
|
return eval_eqeq_real_(ne_flag, le, re);
|
2008-08-29 19:52:09 +02:00
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
const NetEConst*lc = dynamic_cast<const NetEConst*>(le);
|
|
|
|
|
const NetEConst*rc = dynamic_cast<const NetEConst*>(re);
|
2010-11-01 22:37:06 +01:00
|
|
|
if (lc == 0 || rc == 0) return 0;
|
2000-09-29 06:42:56 +02:00
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
const verinum&lv = lc->value();
|
|
|
|
|
const verinum&rv = rc->value();
|
2000-09-29 06:42:56 +02:00
|
|
|
|
2006-03-18 23:52:27 +01:00
|
|
|
const verinum::V eq_res = ne_flag? verinum::V0 : verinum::V1;
|
|
|
|
|
const verinum::V ne_res = ne_flag? verinum::V1 : verinum::V0;
|
|
|
|
|
|
|
|
|
|
verinum::V res = eq_res;
|
2000-09-29 06:42:56 +02:00
|
|
|
unsigned top = lv.len();
|
|
|
|
|
if (rv.len() < top)
|
|
|
|
|
top = rv.len();
|
|
|
|
|
|
|
|
|
|
for (unsigned idx = 0 ; idx < top ; idx += 1) {
|
|
|
|
|
|
2008-08-14 07:22:59 +02:00
|
|
|
bool x_bit_present = false;
|
|
|
|
|
|
2000-09-29 06:42:56 +02:00
|
|
|
switch (lv.get(idx)) {
|
|
|
|
|
|
|
|
|
|
case verinum::Vx:
|
|
|
|
|
case verinum::Vz:
|
|
|
|
|
res = verinum::Vx;
|
2008-08-14 07:22:59 +02:00
|
|
|
x_bit_present = true;
|
2000-09-29 06:42:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (rv.get(idx)) {
|
|
|
|
|
|
|
|
|
|
case verinum::Vx:
|
|
|
|
|
case verinum::Vz:
|
|
|
|
|
res = verinum::Vx;
|
2008-08-14 07:22:59 +02:00
|
|
|
x_bit_present = true;
|
2000-09-29 06:42:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2008-08-14 07:22:59 +02:00
|
|
|
if (x_bit_present)
|
|
|
|
|
continue;
|
2000-09-29 06:42:56 +02:00
|
|
|
|
2008-08-14 07:22:59 +02:00
|
|
|
if (rv.get(idx) != lv.get(idx)) {
|
2006-03-18 23:52:27 +01:00
|
|
|
res = ne_res;
|
2008-08-14 07:22:59 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2000-09-29 06:42:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (res != verinum::Vx) {
|
2006-03-18 23:52:27 +01:00
|
|
|
verinum::V lpad = verinum::V0;
|
|
|
|
|
verinum::V rpad = verinum::V0;
|
|
|
|
|
|
|
|
|
|
if (lv.has_sign() && lv.get(lv.len()-1) == verinum::V1)
|
|
|
|
|
lpad = verinum::V1;
|
|
|
|
|
if (rv.has_sign() && rv.get(rv.len()-1) == verinum::V1)
|
|
|
|
|
rpad = verinum::V1;
|
|
|
|
|
|
2000-09-29 06:42:56 +02:00
|
|
|
for (unsigned idx = top ; idx < lv.len() ; idx += 1)
|
|
|
|
|
switch (lv.get(idx)) {
|
|
|
|
|
|
|
|
|
|
case verinum::Vx:
|
|
|
|
|
case verinum::Vz:
|
|
|
|
|
res = verinum::Vx;
|
|
|
|
|
break;
|
|
|
|
|
|
2006-03-18 23:52:27 +01:00
|
|
|
case verinum::V0:
|
|
|
|
|
if (res != verinum::Vx && rpad != verinum::V0)
|
|
|
|
|
res = ne_res;
|
|
|
|
|
break;
|
|
|
|
|
|
2000-09-29 06:42:56 +02:00
|
|
|
case verinum::V1:
|
2006-03-18 23:52:27 +01:00
|
|
|
if (res != verinum::Vx && rpad != verinum::V1)
|
|
|
|
|
res = ne_res;
|
2000-09-29 06:42:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (unsigned idx = top ; idx < rv.len() ; idx += 1)
|
|
|
|
|
switch (rv.get(idx)) {
|
|
|
|
|
|
|
|
|
|
case verinum::Vx:
|
|
|
|
|
case verinum::Vz:
|
|
|
|
|
res = verinum::Vx;
|
|
|
|
|
break;
|
|
|
|
|
|
2006-03-18 23:52:27 +01:00
|
|
|
case verinum::V0:
|
|
|
|
|
if (res != verinum::Vx && lpad != verinum::V0)
|
|
|
|
|
res = ne_res;
|
|
|
|
|
break;
|
|
|
|
|
|
2000-09-29 06:42:56 +02:00
|
|
|
case verinum::V1:
|
2006-03-18 23:52:27 +01:00
|
|
|
if (res != verinum::Vx && lpad != verinum::V1)
|
|
|
|
|
res = ne_res;
|
2000-09-29 06:42:56 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*result = new NetEConst(verinum(res, 1));
|
|
|
|
|
ivl_assert(*this, result);
|
|
|
|
|
return result;
|
2000-09-29 06:42:56 +02:00
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
NetEConst* NetEBComp::eval_eqeqeq_(bool ne_flag, const NetExpr*le, const NetExpr*re) const
|
2001-01-04 17:49:50 +01:00
|
|
|
{
|
2012-05-29 22:56:16 +02:00
|
|
|
const NetEConst*lc = dynamic_cast<const NetEConst*>(le);
|
|
|
|
|
const NetEConst*rc = dynamic_cast<const NetEConst*>(re);
|
2010-11-01 22:37:06 +01:00
|
|
|
if (lc == 0 || rc == 0) return 0;
|
2001-01-04 17:49:50 +01:00
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
const verinum&lv = lc->value();
|
|
|
|
|
const verinum&rv = rc->value();
|
2001-01-04 17:49:50 +01:00
|
|
|
|
|
|
|
|
verinum::V res = verinum::V1;
|
|
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
// Find the smallest argument length.
|
2001-01-04 17:49:50 +01:00
|
|
|
unsigned cnt = lv.len();
|
2010-11-01 22:37:06 +01:00
|
|
|
if (cnt > rv.len()) cnt = rv.len();
|
2001-01-04 17:49:50 +01:00
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
// Check the common bits.
|
2001-01-04 17:49:50 +01:00
|
|
|
for (unsigned idx = 0 ; idx < cnt ; idx += 1)
|
2010-11-01 22:37:06 +01:00
|
|
|
if (lv.get(idx) != rv.get(idx)) {
|
2001-01-04 17:49:50 +01:00
|
|
|
res = verinum::V0;
|
2010-11-01 22:37:06 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2001-01-04 17:49:50 +01:00
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
bool is_signed = lv.has_sign() && rv.has_sign();
|
2001-01-04 17:49:50 +01:00
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
// If the left value is longer check it against the pad bit.
|
|
|
|
|
if (res == verinum::V1) {
|
|
|
|
|
verinum::V pad = verinum::V0;
|
|
|
|
|
if (is_signed) pad = rv.get(rv.len()-1);
|
|
|
|
|
for (unsigned idx = cnt ; idx < lv.len() ; idx += 1)
|
|
|
|
|
if (lv.get(idx) != pad) {
|
|
|
|
|
res = verinum::V0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-01-04 17:49:50 +01:00
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
// If the right value is longer check it against the pad bit.
|
|
|
|
|
if (res == verinum::V1) {
|
|
|
|
|
verinum::V pad = verinum::V0;
|
|
|
|
|
if (is_signed) pad = lv.get(lv.len()-1);
|
|
|
|
|
for (unsigned idx = cnt ; idx < rv.len() ; idx += 1) {
|
|
|
|
|
if (rv.get(idx) != pad)
|
|
|
|
|
res = verinum::V0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-01-04 17:49:50 +01:00
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
if (ne_flag) {
|
|
|
|
|
if (res == verinum::V0) res = verinum::V1;
|
|
|
|
|
else res = verinum::V0;
|
|
|
|
|
}
|
2001-01-04 17:49:50 +01:00
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*result = new NetEConst(verinum(res, 1));
|
|
|
|
|
ivl_assert(*this, result);
|
|
|
|
|
return result;
|
2001-01-04 17:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
NetEConst* NetEBComp::eval_arguments_(const NetExpr*l, const NetExpr*r) const
|
1999-09-20 04:21:10 +02:00
|
|
|
{
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*res = 0;
|
|
|
|
|
|
1999-09-20 04:21:10 +02:00
|
|
|
switch (op_) {
|
2001-01-04 17:49:50 +01:00
|
|
|
case 'E': // Case equality (===)
|
2012-05-29 22:56:16 +02:00
|
|
|
res = eval_eqeqeq_(false, l, r);
|
2010-11-01 22:37:06 +01:00
|
|
|
break;
|
2001-01-04 17:49:50 +01:00
|
|
|
|
|
|
|
|
case 'e': // Equality (==)
|
2012-05-29 22:56:16 +02:00
|
|
|
res = eval_eqeq_(false, l, r);
|
2010-11-01 22:37:06 +01:00
|
|
|
break;
|
1999-09-20 04:21:10 +02:00
|
|
|
|
2001-02-10 21:29:39 +01:00
|
|
|
case 'G': // >=
|
2012-05-29 22:56:16 +02:00
|
|
|
res = eval_gteq_(l, r);
|
2010-11-01 22:37:06 +01:00
|
|
|
break;
|
2001-02-10 21:29:39 +01:00
|
|
|
|
2001-01-04 17:49:50 +01:00
|
|
|
case 'L': // <=
|
2012-05-29 22:56:16 +02:00
|
|
|
res = eval_leeq_(l, r);
|
2010-11-01 22:37:06 +01:00
|
|
|
break;
|
1999-09-20 04:21:10 +02:00
|
|
|
|
2003-01-30 17:23:07 +01:00
|
|
|
case 'N': // Case inequality (!==)
|
2012-05-29 22:56:16 +02:00
|
|
|
res = eval_eqeqeq_(true, l, r);
|
2010-11-01 22:37:06 +01:00
|
|
|
break;
|
2001-01-04 17:49:50 +01:00
|
|
|
|
|
|
|
|
case 'n': // not-equal (!=)
|
2012-05-29 22:56:16 +02:00
|
|
|
res = eval_eqeq_(true, l, r);
|
2010-11-01 22:37:06 +01:00
|
|
|
break;
|
2000-09-29 06:42:56 +02:00
|
|
|
|
2001-02-09 06:44:23 +01:00
|
|
|
case '<': // Less than
|
2012-05-29 22:56:16 +02:00
|
|
|
res = eval_less_(l, r);
|
2010-11-01 22:37:06 +01:00
|
|
|
break;
|
2001-02-09 06:44:23 +01:00
|
|
|
|
2008-01-29 21:19:59 +01:00
|
|
|
case '>': // Greater than
|
2012-05-29 22:56:16 +02:00
|
|
|
res = eval_gt_(l, r);
|
2010-11-01 22:37:06 +01:00
|
|
|
break;
|
2001-02-10 21:29:39 +01:00
|
|
|
|
1999-09-20 04:21:10 +02:00
|
|
|
}
|
2012-05-29 22:56:16 +02:00
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetEConst* NetEBComp::eval_tree()
|
|
|
|
|
{
|
|
|
|
|
eval_expr(left_);
|
|
|
|
|
eval_expr(right_);
|
|
|
|
|
|
|
|
|
|
NetEConst*res = eval_arguments_(left_, right_);
|
2010-11-01 22:37:06 +01:00
|
|
|
if (res == 0) return 0;
|
|
|
|
|
res->set_line(*this);
|
|
|
|
|
|
|
|
|
|
if (debug_eval_tree) {
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated";
|
|
|
|
|
if (left_->expr_type() == IVL_VT_REAL ||
|
|
|
|
|
right_->expr_type() == IVL_VT_REAL)
|
|
|
|
|
cerr << " (real)";
|
|
|
|
|
cerr << ": " << *this << " --> " << *res << endl;
|
|
|
|
|
}
|
|
|
|
|
return res;
|
1999-09-20 04:21:10 +02:00
|
|
|
}
|
|
|
|
|
|
2009-01-09 02:43:40 +01:00
|
|
|
NetExpr* NetEBDiv::eval_tree_real_()
|
|
|
|
|
{
|
|
|
|
|
verireal lval;
|
|
|
|
|
verireal rval;
|
|
|
|
|
|
|
|
|
|
bool flag = get_real_arguments_(lval, rval);
|
|
|
|
|
if (! flag) return 0;
|
|
|
|
|
|
|
|
|
|
NetECReal*res = 0;
|
|
|
|
|
switch (op_) {
|
|
|
|
|
case '/':
|
|
|
|
|
res = new NetECReal(lval / rval);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case '%':
|
2009-01-09 02:59:39 +01:00
|
|
|
// Since this could/may be called early we don't want to
|
|
|
|
|
// leak functionality.
|
2009-01-09 02:43:40 +01:00
|
|
|
if (!gn_icarus_misc_flag) return 0;
|
2009-01-09 18:43:02 +01:00
|
|
|
res = new NetECReal(lval % rval);
|
2009-01-09 02:43:40 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
res->set_line(*this);
|
2010-11-01 22:37:06 +01:00
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated (real): " << *this
|
|
|
|
|
<< " --> " << *res << endl;
|
|
|
|
|
|
2009-01-09 02:43:40 +01:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2001-02-07 03:46:31 +01:00
|
|
|
/*
|
2003-01-27 06:09:17 +01:00
|
|
|
* The NetEBDiv operator includes the / and % operators. First evaluate
|
2001-02-07 03:46:31 +01:00
|
|
|
* the sub-expressions, then perform the required operation.
|
|
|
|
|
*/
|
2011-02-26 23:59:52 +01:00
|
|
|
NetExpr* NetEBDiv::eval_tree()
|
2000-04-28 20:43:23 +02:00
|
|
|
{
|
2008-03-08 03:51:50 +01:00
|
|
|
eval_expr(left_);
|
|
|
|
|
eval_expr(right_);
|
2001-02-07 03:46:31 +01:00
|
|
|
|
2009-01-09 02:43:40 +01:00
|
|
|
if (expr_type() == IVL_VT_REAL) return eval_tree_real_();
|
|
|
|
|
assert(expr_type() == IVL_VT_LOGIC);
|
2003-05-30 04:55:32 +02:00
|
|
|
|
2009-01-09 02:43:40 +01:00
|
|
|
NetEConst*lc = dynamic_cast<NetEConst*>(left_);
|
|
|
|
|
NetEConst*rc = dynamic_cast<NetEConst*>(right_);
|
|
|
|
|
if (lc == 0 || rc == 0) return 0;
|
2003-02-07 03:47:57 +01:00
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
verinum lval = lc->value();
|
|
|
|
|
verinum rval = rc->value();
|
2003-02-07 03:47:57 +01:00
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
unsigned wid = expr_width();
|
|
|
|
|
ivl_assert(*this, wid > 0);
|
|
|
|
|
ivl_assert(*this, lval.len() == wid);
|
|
|
|
|
ivl_assert(*this, rval.len() == wid);
|
|
|
|
|
|
|
|
|
|
verinum val;
|
2009-01-09 02:43:40 +01:00
|
|
|
switch (op_) {
|
|
|
|
|
case '/':
|
2011-02-26 23:59:52 +01:00
|
|
|
val = verinum(lval / rval, wid);
|
2009-01-09 02:43:40 +01:00
|
|
|
break;
|
|
|
|
|
case '%':
|
2011-02-26 23:59:52 +01:00
|
|
|
val = verinum(lval % rval, wid);
|
2009-01-09 02:43:40 +01:00
|
|
|
break;
|
2011-02-26 23:59:52 +01:00
|
|
|
default:
|
|
|
|
|
return 0;
|
2001-02-07 03:46:31 +01:00
|
|
|
}
|
2011-02-26 23:59:52 +01:00
|
|
|
NetExpr*tmp = new NetEConst(val);
|
2009-01-09 02:43:40 +01:00
|
|
|
ivl_assert(*this, tmp);
|
|
|
|
|
tmp->set_line(*this);
|
2010-11-01 22:37:06 +01:00
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated: " << *this
|
|
|
|
|
<< " --> " << *tmp << endl;
|
|
|
|
|
|
2009-01-09 02:43:40 +01:00
|
|
|
return tmp;
|
2000-04-28 20:43:23 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst* NetEBLogic::eval_tree_real_()
|
|
|
|
|
{
|
|
|
|
|
verireal lval;
|
|
|
|
|
verireal rval;
|
|
|
|
|
|
|
|
|
|
bool flag = get_real_arguments_(lval, rval);
|
|
|
|
|
if (! flag) return 0;
|
|
|
|
|
|
|
|
|
|
verinum::V res;
|
|
|
|
|
switch (op_) {
|
2010-11-04 18:45:01 +01:00
|
|
|
case 'a': // Logical AND (&&)
|
|
|
|
|
if ((lval.as_double() != 0.0) && (rval.as_double() != 0.0))
|
|
|
|
|
res = verinum::V1;
|
|
|
|
|
else
|
|
|
|
|
res = verinum::V0;
|
|
|
|
|
break;
|
2010-11-01 22:37:06 +01:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
case 'o': // Logical OR (||)
|
|
|
|
|
if ((lval.as_double() != 0.0) || (rval.as_double() != 0.0))
|
|
|
|
|
res = verinum::V1;
|
|
|
|
|
else
|
|
|
|
|
res = verinum::V0;
|
|
|
|
|
break;
|
2010-11-01 22:37:06 +01:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetEConst*tmp = new NetEConst(verinum(res, 1));
|
|
|
|
|
ivl_assert(*this, tmp);
|
|
|
|
|
tmp->set_line(*this);
|
|
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated (real): " << *this
|
|
|
|
|
<< " --> " << *tmp << endl;
|
|
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
NetEConst* NetEBLogic::eval_tree()
|
1999-09-23 02:21:54 +02:00
|
|
|
{
|
2008-03-08 03:51:50 +01:00
|
|
|
eval_expr(left_);
|
|
|
|
|
eval_expr(right_);
|
|
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
if (left_->expr_type() == IVL_VT_REAL ||
|
|
|
|
|
right_->expr_type() == IVL_VT_REAL)
|
|
|
|
|
return eval_tree_real_();
|
|
|
|
|
assert(expr_type() == IVL_VT_LOGIC);
|
|
|
|
|
|
2001-01-04 05:28:42 +01:00
|
|
|
NetEConst*lc = dynamic_cast<NetEConst*>(left_);
|
|
|
|
|
NetEConst*rc = dynamic_cast<NetEConst*>(right_);
|
2008-03-08 03:51:50 +01:00
|
|
|
if (lc == 0 || rc == 0) return 0;
|
2001-01-04 05:28:42 +01:00
|
|
|
|
|
|
|
|
verinum::V lv = verinum::V0;
|
|
|
|
|
verinum::V rv = verinum::V0;
|
|
|
|
|
|
|
|
|
|
verinum v = lc->value();
|
|
|
|
|
for (unsigned idx = 0 ; idx < v.len() ; idx += 1)
|
2010-11-01 22:37:06 +01:00
|
|
|
if (v.get(idx) == verinum::V1) {
|
2001-01-04 05:28:42 +01:00
|
|
|
lv = verinum::V1;
|
2010-11-01 22:37:06 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2001-01-04 05:28:42 +01:00
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
if (lv == verinum::V0 && ! v.is_defined()) lv = verinum::Vx;
|
2001-01-04 05:28:42 +01:00
|
|
|
|
|
|
|
|
v = rc->value();
|
|
|
|
|
for (unsigned idx = 0 ; idx < v.len() ; idx += 1)
|
2010-11-01 22:37:06 +01:00
|
|
|
if (v.get(idx) == verinum::V1) {
|
2001-01-04 05:28:42 +01:00
|
|
|
rv = verinum::V1;
|
2010-11-01 22:37:06 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2001-01-04 05:28:42 +01:00
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
if (rv == verinum::V0 && ! v.is_defined()) rv = verinum::Vx;
|
2001-01-04 05:28:42 +01:00
|
|
|
|
|
|
|
|
verinum::V res;
|
|
|
|
|
switch (op_) {
|
2010-11-04 18:45:01 +01:00
|
|
|
case 'a': // Logical AND (&&)
|
|
|
|
|
if ((lv == verinum::V0) || (rv == verinum::V0))
|
|
|
|
|
res = verinum::V0;
|
2001-01-04 05:28:42 +01:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
else if ((lv == verinum::V1) && (rv == verinum::V1))
|
|
|
|
|
res = verinum::V1;
|
2001-01-04 05:28:42 +01:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
else
|
|
|
|
|
res = verinum::Vx;
|
2001-01-04 05:28:42 +01:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
break;
|
2001-01-04 05:28:42 +01:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
case 'o': // Logical OR (||)
|
|
|
|
|
if ((lv == verinum::V1) || (rv == verinum::V1))
|
|
|
|
|
res = verinum::V1;
|
2001-01-04 05:28:42 +01:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
else if ((lv == verinum::V0) && (rv == verinum::V0))
|
|
|
|
|
res = verinum::V0;
|
2001-01-04 05:28:42 +01:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
else
|
|
|
|
|
res = verinum::Vx;
|
2001-01-04 05:28:42 +01:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
break;
|
2001-01-04 05:28:42 +01:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
NetEConst*tmp = new NetEConst(verinum(res, 1));
|
|
|
|
|
ivl_assert(*this, tmp);
|
|
|
|
|
tmp->set_line(*this);
|
|
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated: " << *this
|
|
|
|
|
<< " --> " << *tmp << endl;
|
|
|
|
|
|
|
|
|
|
return tmp;
|
1999-09-23 02:21:54 +02:00
|
|
|
}
|
|
|
|
|
|
2007-04-07 06:46:18 +02:00
|
|
|
|
2012-05-30 02:59:29 +02:00
|
|
|
NetExpr* NetEBMult::eval_tree_real_(const NetExpr*l, const NetExpr*r) const
|
2003-05-30 04:55:32 +02:00
|
|
|
{
|
2012-05-30 02:59:29 +02:00
|
|
|
double lval;
|
|
|
|
|
double rval;
|
2003-05-30 04:55:32 +02:00
|
|
|
|
2012-05-30 02:59:29 +02:00
|
|
|
bool flag = get_real_arguments(l, r, lval, rval);
|
2007-04-07 06:46:18 +02:00
|
|
|
if (! flag) return 0;
|
2003-05-30 04:55:32 +02:00
|
|
|
|
2012-05-30 02:59:29 +02:00
|
|
|
NetECReal*res = new NetECReal( verireal(lval * rval) );
|
2010-11-01 22:37:06 +01:00
|
|
|
ivl_assert(*this, res);
|
2003-05-30 04:55:32 +02:00
|
|
|
res->set_line(*this);
|
2010-11-01 22:37:06 +01:00
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated (real): " << *this
|
|
|
|
|
<< " --> " << *res << endl;
|
|
|
|
|
|
2003-05-30 04:55:32 +02:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
NetExpr* NetEBMult::eval_tree()
|
2000-01-13 04:35:35 +01:00
|
|
|
{
|
2008-03-08 03:51:50 +01:00
|
|
|
eval_expr(left_);
|
|
|
|
|
eval_expr(right_);
|
2000-09-27 20:28:37 +02:00
|
|
|
|
2012-05-30 02:59:29 +02:00
|
|
|
return eval_arguments_(left_, right_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetExpr* NetEBMult::eval_arguments_(const NetExpr*l, const NetExpr*r) const
|
|
|
|
|
{
|
|
|
|
|
if (expr_type() == IVL_VT_REAL) return eval_tree_real_(l,r);
|
2005-07-11 18:56:50 +02:00
|
|
|
assert(expr_type() == IVL_VT_LOGIC);
|
2003-05-30 04:55:32 +02:00
|
|
|
|
2012-05-30 02:59:29 +02:00
|
|
|
const NetEConst*lc = dynamic_cast<const NetEConst*>(l);
|
|
|
|
|
const NetEConst*rc = dynamic_cast<const NetEConst*>(r);
|
2008-03-08 03:51:50 +01:00
|
|
|
if (lc == 0 || rc == 0) return 0;
|
2000-09-27 20:28:37 +02:00
|
|
|
|
|
|
|
|
verinum lval = lc->value();
|
|
|
|
|
verinum rval = rc->value();
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
unsigned wid = expr_width();
|
|
|
|
|
ivl_assert(*this, wid > 0);
|
|
|
|
|
ivl_assert(*this, lval.len() == wid);
|
|
|
|
|
ivl_assert(*this, rval.len() == wid);
|
|
|
|
|
|
|
|
|
|
verinum val(lval * rval, wid);
|
|
|
|
|
NetEConst*tmp = new NetEConst(val);
|
2010-11-01 22:37:06 +01:00
|
|
|
ivl_assert(*this, tmp);
|
|
|
|
|
tmp->set_line(*this);
|
2008-03-26 05:35:08 +01:00
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
2010-11-01 22:37:06 +01:00
|
|
|
cerr << get_fileline() << ": debug: Evaluated: " << *this
|
|
|
|
|
<< " --> " << *tmp << endl;
|
2008-03-26 05:35:08 +01:00
|
|
|
|
|
|
|
|
return tmp;
|
2000-01-13 04:35:35 +01:00
|
|
|
}
|
|
|
|
|
|
2006-07-31 05:50:17 +02:00
|
|
|
NetExpr* NetEBPow::eval_tree_real_()
|
|
|
|
|
{
|
|
|
|
|
verireal lval;
|
|
|
|
|
verireal rval;
|
|
|
|
|
|
2007-04-07 06:46:18 +02:00
|
|
|
bool flag = get_real_arguments_(lval, rval);
|
|
|
|
|
if (! flag) return 0;
|
2006-07-31 05:50:17 +02:00
|
|
|
|
|
|
|
|
NetECReal*res = new NetECReal( pow(lval,rval) );
|
2010-11-01 22:37:06 +01:00
|
|
|
ivl_assert(*this, res);
|
2006-07-31 05:50:17 +02:00
|
|
|
res->set_line(*this);
|
2009-12-07 05:06:13 +01:00
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
2010-11-01 22:37:06 +01:00
|
|
|
cerr << get_fileline() << ": debug: Evaluated (real): " << *this
|
|
|
|
|
<< " --> " << *res << endl;
|
2009-12-07 05:06:13 +01:00
|
|
|
|
2006-07-31 05:50:17 +02:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
NetExpr* NetEBPow::eval_tree()
|
2006-07-31 05:50:17 +02:00
|
|
|
{
|
2008-03-08 03:51:50 +01:00
|
|
|
eval_expr(left_);
|
|
|
|
|
eval_expr(right_);
|
2006-07-31 05:50:17 +02:00
|
|
|
|
2010-11-01 22:37:06 +01:00
|
|
|
if (expr_type() == IVL_VT_REAL) return eval_tree_real_();
|
2006-07-31 05:50:17 +02:00
|
|
|
assert(expr_type() == IVL_VT_LOGIC);
|
|
|
|
|
|
|
|
|
|
NetEConst*lc = dynamic_cast<NetEConst*>(left_);
|
|
|
|
|
NetEConst*rc = dynamic_cast<NetEConst*>(right_);
|
2008-03-08 03:51:50 +01:00
|
|
|
if (lc == 0 || rc == 0) return 0;
|
2006-07-31 05:50:17 +02:00
|
|
|
|
|
|
|
|
verinum lval = lc->value();
|
|
|
|
|
verinum rval = rc->value();
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
unsigned wid = expr_width();
|
|
|
|
|
ivl_assert(*this, wid > 0);
|
|
|
|
|
ivl_assert(*this, lval.len() == wid);
|
|
|
|
|
|
|
|
|
|
verinum val(pow(lval, rval), wid);
|
|
|
|
|
NetEConst*res = new NetEConst(val);
|
2010-11-01 22:37:06 +01:00
|
|
|
ivl_assert(*this, res);
|
2009-12-07 05:06:13 +01:00
|
|
|
res->set_line(*this);
|
|
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
2010-11-01 22:37:06 +01:00
|
|
|
cerr << get_fileline() << ": debug: Evaluated: " << *this
|
|
|
|
|
<< " --> " << *res << endl;
|
2009-12-07 05:06:13 +01:00
|
|
|
|
|
|
|
|
return res;
|
2006-07-31 05:50:17 +02:00
|
|
|
}
|
|
|
|
|
|
1999-09-23 05:56:57 +02:00
|
|
|
/*
|
|
|
|
|
* Evaluate the shift operator if possible. For this to work, both
|
|
|
|
|
* operands must be constant.
|
|
|
|
|
*/
|
2011-02-26 23:59:52 +01:00
|
|
|
NetEConst* NetEBShift::eval_tree()
|
1999-09-23 05:56:57 +02:00
|
|
|
{
|
2008-03-08 03:51:50 +01:00
|
|
|
eval_expr(left_);
|
|
|
|
|
eval_expr(right_);
|
2012-05-29 22:56:16 +02:00
|
|
|
return eval_arguments_(left_,right_);
|
|
|
|
|
}
|
1999-09-23 05:56:57 +02:00
|
|
|
|
2012-05-29 22:56:16 +02:00
|
|
|
NetEConst* NetEBShift::eval_arguments_(const NetExpr*l, const NetExpr*r) const
|
|
|
|
|
{
|
|
|
|
|
const NetEConst*le = dynamic_cast<const NetEConst*>(l);
|
|
|
|
|
const NetEConst*re = dynamic_cast<const NetEConst*>(r);
|
2008-03-08 03:51:50 +01:00
|
|
|
if (le == 0 || re == 0) return 0;
|
1999-09-23 05:56:57 +02:00
|
|
|
|
|
|
|
|
NetEConst*res;
|
|
|
|
|
|
|
|
|
|
verinum lv = le->value();
|
2011-02-26 23:59:52 +01:00
|
|
|
verinum rv = re->value();
|
2001-01-01 22:49:33 +01:00
|
|
|
|
|
|
|
|
unsigned wid = expr_width();
|
2011-02-26 23:59:52 +01:00
|
|
|
ivl_assert(*this, wid > 0);
|
|
|
|
|
ivl_assert(*this, lv.len() == wid);
|
2001-01-01 22:49:33 +01:00
|
|
|
|
2011-03-12 21:26:03 +01:00
|
|
|
verinum val;
|
1999-09-23 05:56:57 +02:00
|
|
|
if (rv.is_defined()) {
|
|
|
|
|
unsigned shift = rv.as_ulong();
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
switch (op_) {
|
|
|
|
|
case 'l':
|
|
|
|
|
val = verinum(lv << shift, wid);
|
|
|
|
|
break;
|
|
|
|
|
case 'r':
|
|
|
|
|
lv.has_sign(false);
|
|
|
|
|
case 'R':
|
|
|
|
|
val = verinum(lv >> shift, wid);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
2001-01-01 22:49:33 +01:00
|
|
|
}
|
1999-09-23 05:56:57 +02:00
|
|
|
} else {
|
2011-03-12 21:26:03 +01:00
|
|
|
val = verinum(verinum::Vx, wid);
|
1999-09-23 05:56:57 +02:00
|
|
|
}
|
2011-03-12 21:26:03 +01:00
|
|
|
val.has_sign(has_sign());
|
|
|
|
|
res = new NetEConst(val);
|
2010-11-01 22:37:06 +01:00
|
|
|
res->set_line(*this);
|
|
|
|
|
|
2011-03-12 21:26:03 +01:00
|
|
|
if (debug_eval_tree)
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated: " << *this
|
|
|
|
|
<< " --> " << *res << endl;
|
|
|
|
|
|
1999-09-23 05:56:57 +02:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
NetEConst* NetEConcat::eval_tree()
|
1999-09-20 04:21:10 +02:00
|
|
|
{
|
2002-05-05 23:11:49 +02:00
|
|
|
unsigned repeat_val = repeat();
|
2002-05-06 04:30:27 +02:00
|
|
|
unsigned local_errors = 0;
|
2002-05-05 23:11:49 +02:00
|
|
|
|
2007-08-10 07:08:54 +02:00
|
|
|
if (debug_eval_tree) {
|
2010-11-01 22:37:06 +01:00
|
|
|
cerr << get_fileline() << ": debug: Evaluating expression:"
|
2011-02-26 23:59:52 +01:00
|
|
|
<< *this << endl;
|
2007-08-10 07:08:54 +02:00
|
|
|
}
|
|
|
|
|
|
2002-05-05 23:11:49 +02:00
|
|
|
unsigned gap = 0;
|
2012-06-25 00:33:40 +02:00
|
|
|
for (unsigned idx = 0 ; idx < parms_.size() ; idx += 1) {
|
1999-09-21 02:13:40 +02:00
|
|
|
|
|
|
|
|
// Parameter not here? This is an error, but presumably
|
|
|
|
|
// already caught and we are here just to catch more.
|
2008-03-08 03:51:50 +01:00
|
|
|
if (parms_[idx] == 0) continue;
|
2002-05-05 23:11:49 +02:00
|
|
|
|
1999-09-21 02:13:40 +02:00
|
|
|
// If this parameter is already a constant, all is well
|
|
|
|
|
// so go on.
|
2002-05-05 23:11:49 +02:00
|
|
|
if (dynamic_cast<NetEConst*>(parms_[idx])) {
|
|
|
|
|
gap += parms_[idx]->expr_width();
|
1999-09-20 04:21:10 +02:00
|
|
|
continue;
|
2002-05-05 23:11:49 +02:00
|
|
|
}
|
1999-09-20 04:21:10 +02:00
|
|
|
|
1999-09-21 02:13:40 +02:00
|
|
|
// Finally, try to evaluate the parameter expression
|
|
|
|
|
// that is here. If I succeed, reset the parameter to
|
|
|
|
|
// the evaluated value.
|
1999-09-20 04:21:10 +02:00
|
|
|
assert(parms_[idx]);
|
2011-02-26 23:59:52 +01:00
|
|
|
NetExpr*expr = parms_[idx]->eval_tree();
|
1999-09-20 04:21:10 +02:00
|
|
|
if (expr) {
|
2008-03-08 03:51:50 +01:00
|
|
|
expr->set_line(*parms_[idx]);
|
1999-09-20 04:21:10 +02:00
|
|
|
delete parms_[idx];
|
|
|
|
|
parms_[idx] = expr;
|
2002-05-06 04:30:27 +02:00
|
|
|
|
|
|
|
|
if (! expr->has_width()) {
|
2007-12-20 18:31:01 +01:00
|
|
|
cerr << get_fileline() << ": error: concatenation "
|
2002-05-06 04:30:27 +02:00
|
|
|
<< "operand has indefinite width: "
|
|
|
|
|
<< *parms_[idx] << endl;
|
|
|
|
|
local_errors += 1;
|
2002-11-09 02:40:19 +01:00
|
|
|
} else if (expr->expr_width() == 0) {
|
2007-12-20 18:31:01 +01:00
|
|
|
cerr << expr->get_fileline() << ": internal error: "
|
2002-11-09 02:40:19 +01:00
|
|
|
<< "Operand of concatenation has no width: "
|
|
|
|
|
<< *expr << endl;
|
|
|
|
|
local_errors += 1;
|
2002-05-06 04:30:27 +02:00
|
|
|
}
|
2002-11-09 02:40:19 +01:00
|
|
|
|
2002-05-05 23:11:49 +02:00
|
|
|
gap += expr->expr_width();
|
1999-09-20 04:21:10 +02:00
|
|
|
}
|
2002-05-06 04:30:27 +02:00
|
|
|
|
1999-09-20 04:21:10 +02:00
|
|
|
}
|
|
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
if (local_errors > 0) return 0;
|
2002-05-06 04:30:27 +02:00
|
|
|
|
2002-05-05 23:11:49 +02:00
|
|
|
// At this point, the "gap" is the width of a single repeat of
|
|
|
|
|
// the concatenation. The total width of the result is the gap
|
|
|
|
|
// times the repeat count.
|
|
|
|
|
verinum val (verinum::Vx, repeat_val * gap);
|
1999-09-20 04:21:10 +02:00
|
|
|
|
|
|
|
|
// build up the result from least significant to most.
|
|
|
|
|
|
|
|
|
|
unsigned cur = 0;
|
2002-04-27 07:03:46 +02:00
|
|
|
bool is_string_flag = true;
|
2012-06-25 00:33:40 +02:00
|
|
|
for (unsigned idx = parms_.size() ; idx > 0 ; idx -= 1) {
|
1999-09-20 04:21:10 +02:00
|
|
|
NetEConst*expr = dynamic_cast<NetEConst*>(parms_[idx-1]);
|
|
|
|
|
if (expr == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
verinum tmp = expr->value();
|
2002-04-27 07:03:46 +02:00
|
|
|
for (unsigned bit = 0; bit < tmp.len(); bit += 1, cur += 1)
|
2002-05-05 23:11:49 +02:00
|
|
|
for (unsigned rep = 0 ; rep < repeat_val ; rep += 1)
|
1999-09-20 04:21:10 +02:00
|
|
|
val.set(rep*gap+cur, tmp[bit]);
|
2002-04-27 07:03:46 +02:00
|
|
|
|
|
|
|
|
is_string_flag = is_string_flag && tmp.is_string();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If all the values were strings, then re-stringify this
|
|
|
|
|
constant. This might be useful information in the code
|
|
|
|
|
generator or other optimizer steps. */
|
|
|
|
|
if (is_string_flag) {
|
|
|
|
|
val = verinum(val.as_string());
|
1999-09-20 04:21:10 +02:00
|
|
|
}
|
|
|
|
|
|
2008-10-14 05:50:56 +02:00
|
|
|
// Normally, concatenations are unsigned. However, the
|
|
|
|
|
// $signed() function works by marking the expression as
|
|
|
|
|
// signed, so we really have to check.
|
|
|
|
|
val.has_sign( this->has_sign() );
|
|
|
|
|
|
1999-09-20 04:21:10 +02:00
|
|
|
NetEConst*res = new NetEConst(val);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
NetEConst* NetESelect::eval_tree()
|
2002-12-05 03:14:33 +01:00
|
|
|
{
|
2010-11-01 22:37:06 +01:00
|
|
|
if (debug_eval_tree) {
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluating expression:"
|
2011-02-26 23:59:52 +01:00
|
|
|
<< *this << endl;
|
2010-11-01 22:37:06 +01:00
|
|
|
}
|
|
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
eval_expr(expr_);
|
2002-12-05 03:14:33 +01:00
|
|
|
NetEConst*expr = dynamic_cast<NetEConst*>(expr_);
|
|
|
|
|
|
2006-11-04 07:16:27 +01:00
|
|
|
long bval = 0;
|
|
|
|
|
if (base_) {
|
2008-03-08 03:51:50 +01:00
|
|
|
eval_expr(base_);
|
2006-11-04 07:16:27 +01:00
|
|
|
NetEConst*base = dynamic_cast<NetEConst*>(base_);
|
2002-12-05 03:14:33 +01:00
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
if (base == 0) return 0;
|
2006-11-04 07:16:27 +01:00
|
|
|
|
|
|
|
|
bval = base->value().as_long();
|
2002-12-05 03:14:33 +01:00
|
|
|
}
|
|
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
if (expr == 0) return 0;
|
2002-12-05 03:14:33 +01:00
|
|
|
|
|
|
|
|
verinum eval = expr->value();
|
|
|
|
|
verinum oval (verinum::V0, expr_width(), true);
|
2006-11-04 07:16:27 +01:00
|
|
|
|
|
|
|
|
verinum::V pad_bit = verinum::Vx;
|
|
|
|
|
if (base_ == 0) {
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
/* If the base is NULL (different from 0) then this
|
|
|
|
|
select is here for zero or sign extension. So
|
|
|
|
|
calculate a proper pad bit. */
|
|
|
|
|
if (has_sign())
|
|
|
|
|
pad_bit = eval.get(expr->expr_width()-1);
|
|
|
|
|
else
|
2006-11-04 07:16:27 +01:00
|
|
|
pad_bit = verinum::V0;
|
|
|
|
|
}
|
2002-12-05 03:14:33 +01:00
|
|
|
|
2003-06-24 03:38:02 +02:00
|
|
|
for (unsigned long idx = 0 ; idx < expr_width() ; idx += 1) {
|
|
|
|
|
if ((bval >= 0) && ((unsigned long) bval < eval.len()))
|
2002-12-05 03:14:33 +01:00
|
|
|
oval.set(idx, eval.get(bval));
|
2004-10-04 03:10:51 +02:00
|
|
|
else
|
2006-11-04 07:16:27 +01:00
|
|
|
oval.set(idx, pad_bit);
|
2002-12-05 03:14:33 +01:00
|
|
|
|
|
|
|
|
bval += 1;
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-23 05:16:43 +01:00
|
|
|
oval.has_sign(has_sign());
|
|
|
|
|
|
2002-12-05 03:14:33 +01:00
|
|
|
NetEConst*res = new NetEConst(oval);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
static void print_ternary_cond(NetExpr*expr)
|
|
|
|
|
{
|
|
|
|
|
if (NetEConst*c = dynamic_cast<NetEConst*>(expr)) {
|
|
|
|
|
cerr << c->value() << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (NetECReal*c = dynamic_cast<NetECReal*>(expr)) {
|
|
|
|
|
cerr << c->value() << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
|
2000-12-16 20:03:30 +01:00
|
|
|
/*
|
|
|
|
|
* A ternary expression evaluation is controlled by the condition
|
|
|
|
|
* expression. If the condition evaluates to true or false, then
|
|
|
|
|
* return the evaluated true or false expression. If the condition
|
|
|
|
|
* evaluates to x or z, then merge the constant bits of the true and
|
|
|
|
|
* false expressions.
|
|
|
|
|
*/
|
2011-02-26 23:59:52 +01:00
|
|
|
NetExpr* NetETernary::eval_tree()
|
2000-12-16 20:03:30 +01:00
|
|
|
{
|
2008-03-08 03:51:50 +01:00
|
|
|
eval_expr(cond_);
|
|
|
|
|
switch (const_logical(cond_)) {
|
|
|
|
|
case C_0:
|
2011-02-26 23:59:52 +01:00
|
|
|
eval_expr(false_val_);
|
2008-03-08 03:51:50 +01:00
|
|
|
if (debug_eval_tree) {
|
2008-09-04 18:41:51 +02:00
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
cerr << get_fileline() << ": debug: Evaluate ternary with "
|
|
|
|
|
<< "constant condition value: ";
|
|
|
|
|
print_ternary_cond(cond_);
|
|
|
|
|
cerr << get_fileline() << ": : Selecting false case: "
|
|
|
|
|
<< *false_val_ << endl;
|
2001-01-01 22:49:33 +01:00
|
|
|
}
|
2000-12-16 20:03:30 +01:00
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
if (expr_type() == IVL_VT_REAL &&
|
|
|
|
|
false_val_->expr_type() != IVL_VT_REAL) {
|
|
|
|
|
verireal f;
|
|
|
|
|
if (get_real_arg_(false_val_, f)) {
|
|
|
|
|
NetECReal*rc = new NetECReal(f);
|
|
|
|
|
rc->set_line(*this);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
2004-09-11 01:51:42 +02:00
|
|
|
}
|
|
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
return false_val_->dup_expr();
|
2001-11-19 02:54:14 +01:00
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
case C_1:
|
2011-02-26 23:59:52 +01:00
|
|
|
eval_expr(true_val_);
|
2004-09-11 01:51:42 +02:00
|
|
|
if (debug_eval_tree) {
|
2007-12-20 18:31:01 +01:00
|
|
|
cerr << get_fileline() << ": debug: Evaluate ternary with "
|
2008-03-08 03:51:50 +01:00
|
|
|
<< "constant condition value: ";
|
|
|
|
|
print_ternary_cond(cond_);
|
2007-12-20 18:31:01 +01:00
|
|
|
cerr << get_fileline() << ": : Selecting true case: "
|
2004-09-11 01:51:42 +02:00
|
|
|
<< *true_val_ << endl;
|
|
|
|
|
}
|
2001-11-19 02:54:14 +01:00
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
if (expr_type() == IVL_VT_REAL &&
|
|
|
|
|
true_val_->expr_type() != IVL_VT_REAL) {
|
|
|
|
|
verireal t;
|
|
|
|
|
if (get_real_arg_(true_val_, t)) {
|
|
|
|
|
NetECReal*rc = new NetECReal(t);
|
|
|
|
|
rc->set_line(*this);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
2004-09-11 01:51:42 +02:00
|
|
|
}
|
2008-03-08 03:51:50 +01:00
|
|
|
|
|
|
|
|
return true_val_->dup_expr();
|
|
|
|
|
|
|
|
|
|
case C_X:
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
2004-09-11 01:51:42 +02:00
|
|
|
}
|
2001-11-19 02:54:14 +01:00
|
|
|
|
2000-12-16 20:03:30 +01:00
|
|
|
/* Here we have a more complex case. We need to evaluate both
|
|
|
|
|
expressions down to constants then compare the values to
|
|
|
|
|
build up a constant result. */
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
eval_expr(true_val_);
|
|
|
|
|
eval_expr(false_val_);
|
2008-03-08 03:51:50 +01:00
|
|
|
|
2012-05-30 02:59:29 +02:00
|
|
|
return blended_arguments_(true_val_, false_val_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetExpr*NetETernary::blended_arguments_(const NetExpr*te, const NetExpr*fe) const
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
const NetEConst*t = dynamic_cast<const NetEConst*>(te);
|
|
|
|
|
const NetEConst*f = dynamic_cast<const NetEConst*>(fe);
|
2008-03-08 03:51:50 +01:00
|
|
|
if (t == 0 || f == 0) {
|
|
|
|
|
verireal tv, fv;
|
2012-05-30 02:59:29 +02:00
|
|
|
if (!get_real_arg_(te, tv)) return 0;
|
|
|
|
|
if (!get_real_arg_(te, fv)) return 0;
|
2000-12-16 20:03:30 +01:00
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
verireal val = verireal(0.0);
|
|
|
|
|
if (tv.as_double() == fv.as_double()) val = tv;
|
2000-12-16 20:03:30 +01:00
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
if (debug_eval_tree) {
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluate ternary with "
|
|
|
|
|
<< "constant condition value: ";
|
|
|
|
|
print_ternary_cond(cond_);
|
|
|
|
|
cerr << get_fileline() << ": : Blending real cases "
|
2009-01-06 04:44:52 +01:00
|
|
|
<< "true=" << tv.as_double()
|
|
|
|
|
<< ", false=" << fv.as_double()
|
|
|
|
|
<< ", to get " << val << endl;
|
2008-03-08 03:51:50 +01:00
|
|
|
}
|
2000-12-16 20:03:30 +01:00
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
NetECReal*rc = new NetECReal(val);
|
|
|
|
|
rc->set_line(*this);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
2000-12-16 20:03:30 +01:00
|
|
|
|
2004-09-11 01:51:42 +02:00
|
|
|
unsigned tsize = t->expr_width();
|
|
|
|
|
unsigned fsize = f->expr_width();
|
|
|
|
|
/* Size of the result is the size of the widest operand. */
|
|
|
|
|
unsigned rsize = tsize > fsize? tsize : fsize;
|
2000-12-16 20:03:30 +01:00
|
|
|
|
2004-09-11 01:51:42 +02:00
|
|
|
verinum val (verinum::V0, rsize);
|
|
|
|
|
for (unsigned idx = 0 ; idx < rsize ; idx += 1) {
|
|
|
|
|
verinum::V tv = idx < tsize? t->value().get(idx) : verinum::V0;
|
2008-03-08 03:51:50 +01:00
|
|
|
verinum::V fv = idx < fsize? f->value().get(idx) : verinum::V0;
|
2000-12-16 20:03:30 +01:00
|
|
|
|
2008-03-08 03:51:50 +01:00
|
|
|
if (tv == fv) val.set(idx, tv);
|
|
|
|
|
else val.set(idx, verinum::Vx);
|
2000-12-16 20:03:30 +01:00
|
|
|
}
|
|
|
|
|
|
2004-09-11 01:51:42 +02:00
|
|
|
if (debug_eval_tree) {
|
2007-12-20 18:31:01 +01:00
|
|
|
cerr << get_fileline() << ": debug: Evaluate ternary with "
|
2008-03-08 03:51:50 +01:00
|
|
|
<< "constant condition value: ";
|
|
|
|
|
print_ternary_cond(cond_);
|
2007-12-20 18:31:01 +01:00
|
|
|
cerr << get_fileline() << ": : Blending cases to get "
|
2004-09-11 01:51:42 +02:00
|
|
|
<< val << endl;
|
|
|
|
|
}
|
|
|
|
|
|
2000-12-16 20:03:30 +01:00
|
|
|
NetEConst*rc = new NetEConst(val);
|
|
|
|
|
rc->set_line(*this);
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2008-02-20 02:52:01 +01:00
|
|
|
NetExpr* NetEUnary::eval_tree_real_()
|
|
|
|
|
{
|
|
|
|
|
NetECReal*val= dynamic_cast<NetECReal*> (expr_), *res;
|
|
|
|
|
if (val == 0) return 0;
|
|
|
|
|
|
|
|
|
|
switch (op_) {
|
|
|
|
|
case '+':
|
|
|
|
|
res = new NetECReal(val->value());
|
2010-11-04 18:45:01 +01:00
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
break;
|
2008-03-08 03:51:50 +01:00
|
|
|
|
2008-02-20 02:52:01 +01:00
|
|
|
case '-':
|
|
|
|
|
res = new NetECReal(-(val->value()));
|
2010-11-04 18:45:01 +01:00
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
break;
|
2008-03-08 03:51:50 +01:00
|
|
|
|
2008-02-20 02:52:01 +01:00
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2010-11-04 18:45:01 +01:00
|
|
|
|
|
|
|
|
res->set_line(*this);
|
|
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated (real): " << *this
|
|
|
|
|
<< " --> " << *res << endl;
|
|
|
|
|
|
|
|
|
|
return res;
|
2008-02-20 02:52:01 +01:00
|
|
|
}
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
NetExpr* NetEUnary::eval_tree()
|
2000-07-07 06:53:53 +02:00
|
|
|
{
|
2008-03-08 03:51:50 +01:00
|
|
|
eval_expr(expr_);
|
2008-02-20 02:52:01 +01:00
|
|
|
if (expr_type() == IVL_VT_REAL) return eval_tree_real_();
|
|
|
|
|
|
2001-01-02 04:23:40 +01:00
|
|
|
NetEConst*rval = dynamic_cast<NetEConst*>(expr_);
|
2008-03-08 03:51:50 +01:00
|
|
|
if (rval == 0) return 0;
|
2001-01-02 05:21:13 +01:00
|
|
|
|
|
|
|
|
verinum val = rval->value();
|
2001-01-02 04:23:40 +01:00
|
|
|
|
2001-01-02 05:21:13 +01:00
|
|
|
switch (op_) {
|
|
|
|
|
|
2001-12-30 01:39:25 +01:00
|
|
|
case '+':
|
|
|
|
|
/* Unary + is a no-op. */
|
2010-11-04 18:45:01 +01:00
|
|
|
break;
|
2001-12-30 01:39:25 +01:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
case '-':
|
|
|
|
|
if (val.is_defined()) {
|
|
|
|
|
verinum tmp (verinum::V0, val.len());
|
|
|
|
|
tmp.has_sign(val.has_sign());
|
2011-02-26 23:59:52 +01:00
|
|
|
val = verinum(tmp - val, val.len());
|
2010-11-04 18:45:01 +01:00
|
|
|
} else {
|
|
|
|
|
for (unsigned idx = 0 ; idx < val.len() ; idx += 1)
|
|
|
|
|
val.set(idx, verinum::Vx);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2001-01-02 05:21:13 +01:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
case '~':
|
2011-03-11 20:27:54 +01:00
|
|
|
/* Bitwise not is even simpler than logical
|
2010-11-04 18:45:01 +01:00
|
|
|
not. Just invert all the bits of the operand and
|
|
|
|
|
make the new value with the same dimensions. */
|
|
|
|
|
for (unsigned idx = 0 ; idx < val.len() ; idx += 1)
|
|
|
|
|
switch (val.get(idx)) {
|
|
|
|
|
case verinum::V0:
|
|
|
|
|
val.set(idx, verinum::V1);
|
|
|
|
|
break;
|
|
|
|
|
case verinum::V1:
|
|
|
|
|
val.set(idx, verinum::V0);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
val.set(idx, verinum::Vx);
|
|
|
|
|
}
|
2001-01-02 05:21:13 +01:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
break;
|
2001-01-02 05:21:13 +01:00
|
|
|
|
2003-10-31 03:47:11 +01:00
|
|
|
case '!':
|
|
|
|
|
assert(0);
|
2001-01-02 05:21:13 +01:00
|
|
|
default:
|
|
|
|
|
return 0;
|
2001-01-02 04:23:40 +01:00
|
|
|
}
|
2010-11-04 18:45:01 +01:00
|
|
|
|
|
|
|
|
NetEConst *res = new NetEConst(val);
|
|
|
|
|
ivl_assert(*this, res);
|
|
|
|
|
res->set_line(*this);
|
|
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated: " << *this
|
|
|
|
|
<< " --> " << *res << endl;
|
|
|
|
|
|
|
|
|
|
return res;
|
2001-01-02 05:21:13 +01:00
|
|
|
}
|
|
|
|
|
|
2000-07-07 06:53:53 +02:00
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
NetExpr* NetEUBits::eval_tree()
|
2001-01-02 05:21:13 +01:00
|
|
|
{
|
2011-02-26 23:59:52 +01:00
|
|
|
return NetEUnary::eval_tree();
|
2001-01-02 05:21:13 +01:00
|
|
|
}
|
|
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
NetEConst* NetEUReduce::eval_tree_real_()
|
2001-01-02 05:21:13 +01:00
|
|
|
{
|
2010-11-04 18:45:01 +01:00
|
|
|
ivl_assert(*this, op_ == '!');
|
|
|
|
|
|
|
|
|
|
NetECReal*val= dynamic_cast<NetECReal*> (expr_);
|
|
|
|
|
if (val == 0) return 0;
|
2010-11-01 22:37:06 +01:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
verinum::V res = val->value().as_double() == 0.0 ? verinum::V1 :
|
|
|
|
|
verinum::V0;
|
|
|
|
|
|
|
|
|
|
NetEConst*tmp = new NetEConst(verinum(res, 1));
|
|
|
|
|
ivl_assert(*this, tmp);
|
|
|
|
|
tmp->set_line(*this);
|
|
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated (real): " << *this
|
|
|
|
|
<< " --> " << *tmp << endl;
|
|
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
NetEConst* NetEUReduce::eval_tree()
|
2010-11-04 18:45:01 +01:00
|
|
|
{
|
2008-03-08 03:51:50 +01:00
|
|
|
eval_expr(expr_);
|
2010-11-04 18:45:01 +01:00
|
|
|
if (expr_type() == IVL_VT_REAL) return eval_tree_real_();
|
|
|
|
|
|
2001-01-02 05:21:13 +01:00
|
|
|
NetEConst*rval = dynamic_cast<NetEConst*>(expr_);
|
2008-03-08 03:51:50 +01:00
|
|
|
if (rval == 0) return 0;
|
2000-07-07 06:53:53 +02:00
|
|
|
|
|
|
|
|
verinum val = rval->value();
|
2010-11-04 18:45:01 +01:00
|
|
|
|
2001-01-02 05:21:13 +01:00
|
|
|
verinum::V res;
|
2008-11-18 00:39:53 +01:00
|
|
|
bool invert = false;
|
2000-07-07 06:53:53 +02:00
|
|
|
|
|
|
|
|
switch (op_) {
|
|
|
|
|
|
|
|
|
|
case '!': {
|
|
|
|
|
/* Evaluate the unary logical not by first scanning
|
|
|
|
|
the operand value for V1 and Vx bits. If we find
|
|
|
|
|
any V1 bits we know that the value is TRUE, so
|
|
|
|
|
the result of ! is V0. If there are no V1 bits
|
|
|
|
|
but there are some Vx/Vz bits, the result is
|
|
|
|
|
unknown. Otherwise, the result is V1. */
|
2010-11-04 18:45:01 +01:00
|
|
|
bool v1 = false, vx = false;
|
|
|
|
|
for (unsigned idx = 0 ; idx < val.len() && !v1 ; idx += 1) {
|
2000-07-07 06:53:53 +02:00
|
|
|
switch (val.get(idx)) {
|
|
|
|
|
case verinum::V0:
|
|
|
|
|
break;
|
|
|
|
|
case verinum::V1:
|
2010-11-04 18:45:01 +01:00
|
|
|
v1 = true;
|
2000-07-07 06:53:53 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
2010-11-04 18:45:01 +01:00
|
|
|
vx = true;
|
2000-07-07 06:53:53 +02:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-01-02 05:21:13 +01:00
|
|
|
|
|
|
|
|
res = v1? verinum::V0 : (vx? verinum::Vx : verinum::V1);
|
2002-04-14 21:28:47 +02:00
|
|
|
break;
|
2000-07-07 06:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
2008-11-18 00:39:53 +01:00
|
|
|
case 'A':
|
|
|
|
|
invert = true;
|
2001-01-02 05:21:13 +01:00
|
|
|
case '&': {
|
|
|
|
|
res = verinum::V1;
|
2001-01-02 04:23:40 +01:00
|
|
|
for (unsigned idx = 0 ; idx < val.len() ; idx += 1)
|
2001-01-02 05:21:13 +01:00
|
|
|
res = res & val.get(idx);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2001-01-02 04:23:40 +01:00
|
|
|
|
2008-11-18 00:39:53 +01:00
|
|
|
case 'N':
|
|
|
|
|
invert = true;
|
2001-01-02 05:21:13 +01:00
|
|
|
case '|': {
|
|
|
|
|
res = verinum::V0;
|
|
|
|
|
for (unsigned idx = 0 ; idx < val.len() ; idx += 1)
|
|
|
|
|
res = res | val.get(idx);
|
|
|
|
|
break;
|
2001-01-02 04:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
2008-11-18 00:39:53 +01:00
|
|
|
case 'X':
|
|
|
|
|
invert = true;
|
2002-05-25 18:43:47 +02:00
|
|
|
case '^': {
|
|
|
|
|
/* Reduction XOR. */
|
|
|
|
|
unsigned ones = 0, unknown = 0;
|
|
|
|
|
for (unsigned idx = 0 ; idx < val.len() ; idx += 1)
|
|
|
|
|
switch (val.get(idx)) {
|
|
|
|
|
case verinum::V0:
|
|
|
|
|
break;
|
|
|
|
|
case verinum::V1:
|
|
|
|
|
ones += 1;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
unknown += 1;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-18 00:39:53 +01:00
|
|
|
if (unknown) res = verinum::Vx;
|
|
|
|
|
else if (ones%2) res = verinum::V1;
|
|
|
|
|
else res = verinum::V0;
|
|
|
|
|
break;
|
2002-05-25 18:43:47 +02:00
|
|
|
}
|
|
|
|
|
|
2000-07-07 06:53:53 +02:00
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2001-01-02 04:23:40 +01:00
|
|
|
|
2008-11-18 00:39:53 +01:00
|
|
|
if (invert) res = ~res;
|
2010-11-04 18:45:01 +01:00
|
|
|
|
|
|
|
|
NetEConst*tmp = new NetEConst(verinum(res, 1));
|
|
|
|
|
ivl_assert(*this, tmp);
|
|
|
|
|
tmp->set_line(*this);
|
|
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated: " << *this
|
|
|
|
|
<< " --> " << *tmp << endl;
|
|
|
|
|
|
|
|
|
|
return tmp;
|
2001-01-02 04:23:40 +01:00
|
|
|
}
|
2008-07-31 23:05:27 +02:00
|
|
|
|
2010-11-05 19:32:30 +01:00
|
|
|
static NetEConst* evaluate_clog2(NetExpr*&arg_)
|
2008-07-31 23:05:27 +02:00
|
|
|
{
|
2008-10-13 18:51:05 +02:00
|
|
|
eval_expr(arg_);
|
2010-11-04 18:45:01 +01:00
|
|
|
|
2008-10-13 18:51:05 +02:00
|
|
|
NetEConst*tmpi = dynamic_cast<NetEConst *>(arg_);
|
|
|
|
|
NetECReal*tmpr = dynamic_cast<NetECReal *>(arg_);
|
2008-08-16 04:28:11 +02:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
if (tmpi == 0 && tmpr == 0) return 0;
|
|
|
|
|
|
|
|
|
|
verinum arg;
|
|
|
|
|
if (tmpi) {
|
|
|
|
|
arg = tmpi->value();
|
|
|
|
|
} else {
|
|
|
|
|
arg = verinum(tmpr->value().as_double(), true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetEConst*rtn;
|
|
|
|
|
|
|
|
|
|
/* If we have an x in the verinum we return 'bx. */
|
|
|
|
|
if (!arg.is_defined()) {
|
|
|
|
|
verinum tmp (verinum::Vx, integer_width);
|
|
|
|
|
tmp.has_sign(true);
|
2008-08-16 04:28:11 +02:00
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
rtn = new NetEConst(tmp);
|
|
|
|
|
ivl_assert(*arg_, rtn);
|
|
|
|
|
} else {
|
2008-08-29 06:15:43 +02:00
|
|
|
bool is_neg = false;
|
2008-07-31 23:05:27 +02:00
|
|
|
uint64_t res = 0;
|
2010-11-04 18:45:01 +01:00
|
|
|
|
2008-08-29 06:15:43 +02:00
|
|
|
if (arg.is_negative()) {
|
|
|
|
|
is_neg = true;
|
|
|
|
|
// If the length is not defined, then work with
|
|
|
|
|
// the trimmed version of the number.
|
|
|
|
|
if (! arg.has_len())
|
|
|
|
|
arg = trim_vnum(arg);
|
|
|
|
|
}
|
2008-07-31 23:05:27 +02:00
|
|
|
arg.has_sign(false); // $unsigned()
|
2008-08-29 06:15:43 +02:00
|
|
|
|
2008-07-31 23:05:27 +02:00
|
|
|
if (!arg.is_zero()) {
|
|
|
|
|
arg = arg - verinum((uint64_t)1, 1);
|
|
|
|
|
while (!arg.is_zero()) {
|
|
|
|
|
res += 1;
|
|
|
|
|
arg = arg >> 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-08-29 06:15:43 +02:00
|
|
|
|
|
|
|
|
if (is_neg && res < integer_width)
|
|
|
|
|
res = integer_width;
|
|
|
|
|
|
2008-10-02 05:35:26 +02:00
|
|
|
verinum tmp (res, integer_width);
|
2009-10-14 01:38:53 +02:00
|
|
|
tmp.has_sign(true);
|
2010-11-04 18:45:01 +01:00
|
|
|
|
|
|
|
|
rtn = new NetEConst(tmp);
|
|
|
|
|
ivl_assert(*arg_, rtn);
|
2008-07-31 23:05:27 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-04 18:45:01 +01:00
|
|
|
return rtn;
|
2008-07-31 23:05:27 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-05 19:32:30 +01:00
|
|
|
static NetECReal* evaluate_math_one_arg(NetExpr*&arg_, const char*name)
|
2008-08-30 06:11:44 +02:00
|
|
|
{
|
2008-10-13 18:51:05 +02:00
|
|
|
eval_expr(arg_);
|
2010-11-05 19:32:30 +01:00
|
|
|
|
2008-10-13 18:51:05 +02:00
|
|
|
NetEConst*tmpi = dynamic_cast<NetEConst *>(arg_);
|
|
|
|
|
NetECReal*tmpr = dynamic_cast<NetECReal *>(arg_);
|
2010-11-05 19:32:30 +01:00
|
|
|
|
|
|
|
|
NetECReal*res = 0;
|
|
|
|
|
|
2008-08-30 06:11:44 +02:00
|
|
|
if (tmpi || tmpr) {
|
|
|
|
|
double arg;
|
|
|
|
|
if (tmpi) {
|
|
|
|
|
arg = tmpi->value().as_double();
|
|
|
|
|
} else {
|
|
|
|
|
arg = tmpr->value().as_double();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp(name, "$ln") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(log(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$log10") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(log10(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$exp") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(exp(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$sqrt") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(sqrt(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$floor") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(floor(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$ceil") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(ceil(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$sin") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(sin(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$cos") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(cos(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$tan") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(tan(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$asin") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(asin(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$acos") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(acos(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$atan") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(atan(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$sinh") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(sinh(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$cosh") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(cosh(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$tanh") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(tanh(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$asinh") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(asinh(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$acosh") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(acosh(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$atanh") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(atanh(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
|
|
|
|
} else {
|
|
|
|
|
cerr << arg_->get_fileline() << ": warning: Unhandled"
|
|
|
|
|
"constant system function " << name << "." << endl;
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-05 19:32:30 +01:00
|
|
|
return res;
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-05 19:32:30 +01:00
|
|
|
static NetECReal* evaluate_math_two_args(NetExpr*&arg0_, NetExpr*&arg1_,
|
|
|
|
|
const char*name)
|
2008-08-30 06:11:44 +02:00
|
|
|
{
|
2008-10-13 18:51:05 +02:00
|
|
|
eval_expr(arg0_);
|
|
|
|
|
eval_expr(arg1_);
|
2010-11-05 19:32:30 +01:00
|
|
|
|
2008-10-13 18:51:05 +02:00
|
|
|
NetEConst*tmpi0 = dynamic_cast<NetEConst *>(arg0_);
|
|
|
|
|
NetECReal*tmpr0 = dynamic_cast<NetECReal *>(arg0_);
|
|
|
|
|
NetEConst*tmpi1 = dynamic_cast<NetEConst *>(arg1_);
|
|
|
|
|
NetECReal*tmpr1 = dynamic_cast<NetECReal *>(arg1_);
|
2010-11-05 19:32:30 +01:00
|
|
|
|
|
|
|
|
NetECReal*res = 0;
|
|
|
|
|
|
2008-08-30 06:11:44 +02:00
|
|
|
if ((tmpi0 || tmpr0) && (tmpi1 || tmpr1)) {
|
|
|
|
|
double arg0, arg1;
|
|
|
|
|
if (tmpi0) {
|
|
|
|
|
arg0 = tmpi0->value().as_double();
|
|
|
|
|
} else {
|
|
|
|
|
arg0 = tmpr0->value().as_double();
|
|
|
|
|
}
|
|
|
|
|
if (tmpi1) {
|
|
|
|
|
arg1 = tmpi1->value().as_double();
|
|
|
|
|
} else {
|
|
|
|
|
arg1 = tmpr1->value().as_double();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (strcmp(name, "$pow") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(pow(arg0, arg1)));
|
|
|
|
|
ivl_assert(*arg0_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$atan2") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(atan2(arg0, arg1)));
|
|
|
|
|
ivl_assert(*arg0_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$hypot") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(hypot(arg0, arg1)));
|
|
|
|
|
ivl_assert(*arg0_, res);
|
|
|
|
|
} else {
|
|
|
|
|
cerr << arg0_->get_fileline() << ": warning: Unhandled"
|
|
|
|
|
"constant system function " << name << "." << endl;
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-05 19:32:30 +01:00
|
|
|
return res;
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-05 19:32:30 +01:00
|
|
|
static NetExpr* evaluate_abs(NetExpr*&arg_)
|
2008-08-30 06:11:44 +02:00
|
|
|
{
|
2008-10-13 18:51:05 +02:00
|
|
|
eval_expr(arg_);
|
2010-11-05 19:32:30 +01:00
|
|
|
|
|
|
|
|
NetExpr*res = 0;
|
|
|
|
|
|
2008-10-13 18:51:05 +02:00
|
|
|
NetEConst*tmpi = dynamic_cast<NetEConst *>(arg_);
|
2008-08-30 06:11:44 +02:00
|
|
|
if (tmpi) {
|
|
|
|
|
verinum arg = tmpi->value();
|
2008-08-31 02:35:57 +02:00
|
|
|
if (arg.is_negative()) {
|
2008-08-30 06:11:44 +02:00
|
|
|
arg = v_not(arg) + verinum(1);
|
|
|
|
|
}
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetEConst(arg);
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
2008-10-13 18:51:05 +02:00
|
|
|
NetECReal*tmpr = dynamic_cast<NetECReal *>(arg_);
|
2008-08-30 06:11:44 +02:00
|
|
|
if (tmpr) {
|
|
|
|
|
double arg = tmpr->value().as_double();
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(fabs(arg)));
|
|
|
|
|
ivl_assert(*arg_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-05 19:32:30 +01:00
|
|
|
return res;
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-05 19:32:30 +01:00
|
|
|
static NetExpr* evaluate_min_max(NetExpr*&arg0_, NetExpr*&arg1_,
|
|
|
|
|
const char*name)
|
2008-08-30 06:11:44 +02:00
|
|
|
{
|
2008-10-13 18:51:05 +02:00
|
|
|
eval_expr(arg0_);
|
|
|
|
|
eval_expr(arg1_);
|
2010-11-05 19:32:30 +01:00
|
|
|
|
2008-10-13 18:51:05 +02:00
|
|
|
NetEConst*tmpi0 = dynamic_cast<NetEConst *>(arg0_);
|
|
|
|
|
NetECReal*tmpr0 = dynamic_cast<NetECReal *>(arg0_);
|
|
|
|
|
NetEConst*tmpi1 = dynamic_cast<NetEConst *>(arg1_);
|
|
|
|
|
NetECReal*tmpr1 = dynamic_cast<NetECReal *>(arg1_);
|
2010-11-05 19:32:30 +01:00
|
|
|
|
|
|
|
|
NetExpr*res = 0;
|
|
|
|
|
|
2008-08-30 06:11:44 +02:00
|
|
|
if (tmpi0 && tmpi1) {
|
|
|
|
|
verinum arg0 = tmpi0->value();
|
|
|
|
|
verinum arg1 = tmpi1->value();
|
|
|
|
|
if (strcmp(name, "$min") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetEConst( arg0 < arg1 ? arg0 : arg1);
|
|
|
|
|
ivl_assert(*arg0_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$max") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetEConst( arg0 < arg1 ? arg1 : arg0);
|
|
|
|
|
ivl_assert(*arg0_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
2010-11-05 19:32:30 +01:00
|
|
|
} else if ((tmpi0 || tmpr0) && (tmpi1 || tmpr1)) {
|
2008-08-30 06:11:44 +02:00
|
|
|
double arg0, arg1;
|
|
|
|
|
if (tmpi0) {
|
|
|
|
|
arg0 = tmpi0->value().as_double();
|
|
|
|
|
} else {
|
|
|
|
|
arg0 = tmpr0->value().as_double();
|
|
|
|
|
}
|
|
|
|
|
if (tmpi1) {
|
|
|
|
|
arg1 = tmpi1->value().as_double();
|
|
|
|
|
} else {
|
|
|
|
|
arg1 = tmpr1->value().as_double();
|
|
|
|
|
}
|
|
|
|
|
if (strcmp(name, "$min") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(arg0 < arg1 ? arg0 : arg1));
|
|
|
|
|
ivl_assert(*arg0_, res);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else if (strcmp(name, "$max") == 0) {
|
2010-11-05 19:32:30 +01:00
|
|
|
res = new NetECReal(verireal(arg0 < arg1 ? arg1 : arg0));
|
|
|
|
|
ivl_assert(*arg0_, res);
|
|
|
|
|
} else {
|
|
|
|
|
cerr << arg0_->get_fileline() << ": warning: Unhandled"
|
|
|
|
|
"constant system function " << name << "." << endl;
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-05 19:32:30 +01:00
|
|
|
return res;
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
NetExpr* NetESFunc::eval_tree()
|
2008-07-31 23:05:27 +02:00
|
|
|
{
|
2008-08-30 06:11:44 +02:00
|
|
|
/* If we are not targeting at least Verilog-2005, Verilog-AMS
|
|
|
|
|
* or using the Icarus misc flag then we do not support these
|
|
|
|
|
* functions as constant. */
|
|
|
|
|
if (generation_flag < GN_VER2005 &&
|
|
|
|
|
!gn_icarus_misc_flag && !gn_verilog_ams_flag) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char*nm = name();
|
|
|
|
|
NetExpr*rtn = 0;
|
|
|
|
|
/* Only $clog2 and the builtin mathematical functions can
|
|
|
|
|
* be a constant system function. */
|
|
|
|
|
if (strcmp(nm, "$clog2") == 0 ||
|
|
|
|
|
strcmp(nm, "$ln") == 0 ||
|
|
|
|
|
strcmp(nm, "$log10") == 0 ||
|
|
|
|
|
strcmp(nm, "$exp") == 0 ||
|
|
|
|
|
strcmp(nm, "$sqrt") == 0 ||
|
|
|
|
|
strcmp(nm, "$floor") == 0 ||
|
|
|
|
|
strcmp(nm, "$ceil") == 0 ||
|
|
|
|
|
strcmp(nm, "$sin") == 0 ||
|
|
|
|
|
strcmp(nm, "$cos") == 0 ||
|
|
|
|
|
strcmp(nm, "$tan") == 0 ||
|
|
|
|
|
strcmp(nm, "$asin") == 0 ||
|
|
|
|
|
strcmp(nm, "$acos") == 0 ||
|
|
|
|
|
strcmp(nm, "$atan") == 0 ||
|
|
|
|
|
strcmp(nm, "$sinh") == 0 ||
|
|
|
|
|
strcmp(nm, "$cosh") == 0 ||
|
|
|
|
|
strcmp(nm, "$tanh") == 0 ||
|
|
|
|
|
strcmp(nm, "$asinh") == 0 ||
|
|
|
|
|
strcmp(nm, "$acosh") == 0 ||
|
|
|
|
|
strcmp(nm, "$atanh") == 0) {
|
2008-07-31 23:05:27 +02:00
|
|
|
if (nparms() != 1 || parm(0) == 0) {
|
2008-08-30 06:11:44 +02:00
|
|
|
cerr << get_fileline() << ": error: " << nm
|
|
|
|
|
<< " takes a single argument." << endl;
|
2008-07-31 23:05:27 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
2008-10-02 05:35:26 +02:00
|
|
|
NetExpr*arg = parm(0)->dup_expr();
|
2008-08-30 06:11:44 +02:00
|
|
|
if (strcmp(nm, "$clog2") == 0) {
|
2008-10-02 05:35:26 +02:00
|
|
|
rtn = evaluate_clog2(arg);
|
2008-08-30 06:11:44 +02:00
|
|
|
} else {
|
2008-10-02 05:35:26 +02:00
|
|
|
rtn = evaluate_math_one_arg(arg, nm);
|
2008-07-31 23:05:27 +02:00
|
|
|
}
|
2008-10-02 05:35:26 +02:00
|
|
|
delete arg;
|
2008-07-31 23:05:27 +02:00
|
|
|
}
|
|
|
|
|
|
2008-08-30 06:11:44 +02:00
|
|
|
if (strcmp(nm, "$pow") == 0 ||
|
|
|
|
|
strcmp(nm, "$atan2") == 0 ||
|
|
|
|
|
strcmp(nm, "$hypot") == 0) {
|
|
|
|
|
if (nparms() != 2 || parm(0) == 0 || parm(1) == 0) {
|
|
|
|
|
cerr << get_fileline() << ": error: " << nm
|
|
|
|
|
<< " takes two arguments." << endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-10-02 05:35:26 +02:00
|
|
|
NetExpr*arg0 = parm(0)->dup_expr();
|
|
|
|
|
NetExpr*arg1 = parm(1)->dup_expr();
|
|
|
|
|
rtn = evaluate_math_two_args(arg0, arg1, nm);
|
|
|
|
|
delete arg0;
|
|
|
|
|
delete arg1;
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((gn_icarus_misc_flag || gn_verilog_ams_flag) &&
|
2010-04-25 18:42:34 +02:00
|
|
|
(strcmp(nm, "$abs") == 0)) {
|
2008-08-30 06:11:44 +02:00
|
|
|
if (nparms() != 1 || parm(0) == 0) {
|
|
|
|
|
cerr << get_fileline() << ": error: " << nm
|
|
|
|
|
<< " takes a single argument." << endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-10-02 05:35:26 +02:00
|
|
|
NetExpr*arg = parm(0)->dup_expr();
|
2010-04-25 18:42:34 +02:00
|
|
|
rtn = evaluate_abs(arg);
|
2008-10-02 05:35:26 +02:00
|
|
|
delete arg;
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((gn_icarus_misc_flag || gn_verilog_ams_flag) &&
|
|
|
|
|
(strcmp(nm, "$min") == 0 || strcmp(nm, "$max") == 0)) {
|
|
|
|
|
if (nparms() != 2 || parm(0) == 0 || parm(1) == 0) {
|
|
|
|
|
cerr << get_fileline() << ": error: " << nm
|
|
|
|
|
<< " takes two arguments." << endl;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-10-02 05:35:26 +02:00
|
|
|
NetExpr*arg0 = parm(0)->dup_expr();
|
|
|
|
|
NetExpr*arg1 = parm(1)->dup_expr();
|
|
|
|
|
rtn = evaluate_min_max(arg0, arg1, nm);
|
|
|
|
|
delete arg0;
|
|
|
|
|
delete arg1;
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rtn != 0) {
|
|
|
|
|
rtn->set_line(*this);
|
2010-11-04 18:45:01 +01:00
|
|
|
|
|
|
|
|
if (debug_eval_tree)
|
|
|
|
|
cerr << get_fileline() << ": debug: Evaluated: " << *this
|
|
|
|
|
<< " --> " << *rtn << endl;
|
2008-08-30 06:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rtn;
|
2008-07-31 23:05:27 +02:00
|
|
|
}
|
2009-02-28 03:58:36 +01:00
|
|
|
|
2011-02-26 23:59:52 +01:00
|
|
|
NetExpr* NetEUFunc::eval_tree()
|
2009-02-28 03:58:36 +01:00
|
|
|
{
|
2011-04-05 21:43:54 +02:00
|
|
|
// If we know the function cannot be evaluated as a constant,
|
|
|
|
|
// give up now.
|
|
|
|
|
if (!func()->is_const_func())
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
// Variables inside static functions can be accessed from outside
|
|
|
|
|
// the function, so we can't be sure they are constant unless the
|
|
|
|
|
// function was called in a constant context.
|
|
|
|
|
if (!func()->is_auto() && !need_const_)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
// Run through the input parameters to check they are constants.
|
|
|
|
|
for (unsigned idx = 0; idx < parm_count(); idx += 1) {
|
|
|
|
|
if (dynamic_cast<const NetEConst*> (parm(idx)))
|
|
|
|
|
continue;
|
|
|
|
|
if (dynamic_cast<const NetECReal*> (parm(idx)))
|
|
|
|
|
continue;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (need_const_) {
|
2012-05-29 19:02:10 +02:00
|
|
|
NetFuncDef*def = func_->func_def();
|
|
|
|
|
ivl_assert(*this, def);
|
|
|
|
|
|
2012-06-05 10:13:06 +02:00
|
|
|
vector<NetExpr*>args(parms_.size());
|
|
|
|
|
for (unsigned idx = 0 ; idx < parms_.size() ; idx += 1)
|
|
|
|
|
args[idx] = parms_[idx]->dup_expr();
|
|
|
|
|
|
|
|
|
|
NetExpr*res = def->evaluate_function(*this, args);
|
2012-05-29 19:02:10 +02:00
|
|
|
return res;
|
2011-04-05 21:43:54 +02:00
|
|
|
}
|
2012-05-29 19:02:10 +02:00
|
|
|
|
2009-02-28 03:58:36 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|