1998-11-04 00:28:49 +01:00
|
|
|
/*
|
2010-12-14 02:42:48 +01:00
|
|
|
* Copyright (c) 1998-2010 Stephen Williams (steve@icarus.com)
|
1998-11-04 00:28:49 +01: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
|
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
|
*/
|
|
|
|
|
|
2001-07-25 05:10:48 +02:00
|
|
|
# include "config.h"
|
|
|
|
|
|
|
|
|
|
# include <iostream>
|
|
|
|
|
|
1998-11-04 00:28:49 +01:00
|
|
|
# include "PExpr.h"
|
1999-05-10 02:16:57 +02:00
|
|
|
# include "netlist.h"
|
2001-01-15 00:04:55 +01:00
|
|
|
# include "compiler.h"
|
1998-11-04 00:28:49 +01:00
|
|
|
|
2001-11-07 05:01:59 +01:00
|
|
|
verinum* PExpr::eval_const(const Design*, const NetScope*) const
|
1998-11-04 00:28:49 +01:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 05:01:59 +01:00
|
|
|
verinum* PEBinary::eval_const(const Design*des, const NetScope*scope) const
|
1999-05-10 02:16:57 +02:00
|
|
|
{
|
2001-11-07 05:01:59 +01:00
|
|
|
verinum*l = left_->eval_const(des, scope);
|
1999-05-10 02:16:57 +02:00
|
|
|
if (l == 0) return 0;
|
2001-11-07 05:01:59 +01:00
|
|
|
verinum*r = right_->eval_const(des, scope);
|
1999-05-10 02:16:57 +02:00
|
|
|
if (r == 0) {
|
|
|
|
|
delete l;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
verinum*res;
|
|
|
|
|
|
|
|
|
|
switch (op_) {
|
1999-10-08 19:48:08 +02:00
|
|
|
case '+': {
|
2001-12-29 23:10:10 +01:00
|
|
|
if (l->is_defined() && r->is_defined()) {
|
2003-04-14 05:40:21 +02:00
|
|
|
res = new verinum(*l + *r);
|
2001-12-29 23:10:10 +01:00
|
|
|
} else {
|
|
|
|
|
res = new verinum(verinum::Vx, l->len());
|
|
|
|
|
}
|
1999-10-08 19:48:08 +02:00
|
|
|
break;
|
|
|
|
|
}
|
1999-05-10 02:16:57 +02:00
|
|
|
case '-': {
|
2001-12-29 23:10:10 +01:00
|
|
|
if (l->is_defined() && r->is_defined()) {
|
2003-04-14 05:40:21 +02:00
|
|
|
res = new verinum(*l - *r);
|
2001-12-29 23:10:10 +01:00
|
|
|
} else {
|
|
|
|
|
res = new verinum(verinum::Vx, l->len());
|
|
|
|
|
}
|
1999-05-10 02:16:57 +02:00
|
|
|
break;
|
|
|
|
|
}
|
1999-11-21 21:03:24 +01:00
|
|
|
case '*': {
|
2001-12-29 23:10:10 +01:00
|
|
|
if (l->is_defined() && r->is_defined()) {
|
2003-04-14 05:40:21 +02:00
|
|
|
res = new verinum(*l * *r);
|
2001-12-29 23:10:10 +01:00
|
|
|
} else {
|
|
|
|
|
res = new verinum(verinum::Vx, l->len());
|
|
|
|
|
}
|
1999-11-21 21:03:24 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case '/': {
|
2001-12-29 23:10:10 +01:00
|
|
|
if (l->is_defined() && r->is_defined()) {
|
|
|
|
|
long lv = l->as_long();
|
|
|
|
|
long rv = r->as_long();
|
|
|
|
|
res = new verinum(lv / rv, l->len());
|
|
|
|
|
} else {
|
|
|
|
|
res = new verinum(verinum::Vx, l->len());
|
|
|
|
|
}
|
1999-11-21 21:03:24 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case '%': {
|
2001-12-29 23:10:10 +01:00
|
|
|
if (l->is_defined() && r->is_defined()) {
|
|
|
|
|
long lv = l->as_long();
|
|
|
|
|
long rv = r->as_long();
|
|
|
|
|
res = new verinum(lv % rv, l->len());
|
|
|
|
|
} else {
|
|
|
|
|
res = new verinum(verinum::Vx, l->len());
|
|
|
|
|
}
|
1999-11-21 21:03:24 +01:00
|
|
|
break;
|
|
|
|
|
}
|
2003-03-26 07:16:18 +01:00
|
|
|
case '>': {
|
|
|
|
|
if (l->is_defined() && r->is_defined()) {
|
|
|
|
|
long lv = l->as_long();
|
|
|
|
|
long rv = r->as_long();
|
|
|
|
|
res = new verinum(lv > rv, l->len());
|
|
|
|
|
} else {
|
|
|
|
|
res = new verinum(verinum::Vx, l->len());
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case '<': {
|
|
|
|
|
if (l->is_defined() && r->is_defined()) {
|
|
|
|
|
long lv = l->as_long();
|
|
|
|
|
long rv = r->as_long();
|
|
|
|
|
res = new verinum(lv < rv, l->len());
|
|
|
|
|
} else {
|
|
|
|
|
res = new verinum(verinum::Vx, l->len());
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2001-12-29 01:43:55 +01:00
|
|
|
case 'l': { // left shift (<<)
|
2001-01-04 05:47:51 +01:00
|
|
|
assert(r->is_defined());
|
|
|
|
|
unsigned long rv = r->as_ulong();
|
2006-09-20 22:27:02 +02:00
|
|
|
unsigned use_wid = l->len();
|
|
|
|
|
if (! l->has_len())
|
|
|
|
|
use_wid += rv;
|
|
|
|
|
res = new verinum(verinum::V0, use_wid);
|
2001-01-04 05:47:51 +01:00
|
|
|
if (rv < res->len()) {
|
|
|
|
|
unsigned cnt = res->len() - rv;
|
|
|
|
|
for (unsigned idx = 0 ; idx < cnt ; idx += 1)
|
|
|
|
|
res->set(idx+rv, l->get(idx));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2001-12-29 01:43:55 +01:00
|
|
|
case 'r': { // right shift (>>)
|
|
|
|
|
assert(r->is_defined());
|
|
|
|
|
unsigned long rv = r->as_ulong();
|
|
|
|
|
res = new verinum(verinum::V0, l->len());
|
|
|
|
|
if (rv < res->len()) {
|
|
|
|
|
unsigned cnt = res->len() - rv;
|
|
|
|
|
for (unsigned idx = 0 ; idx < cnt ; idx += 1)
|
|
|
|
|
res->set(idx, l->get(idx+rv));
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2001-01-04 05:47:51 +01:00
|
|
|
|
1999-05-10 02:16:57 +02:00
|
|
|
default:
|
|
|
|
|
delete l;
|
|
|
|
|
delete r;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-04 05:47:51 +01:00
|
|
|
delete l;
|
|
|
|
|
delete r;
|
1999-05-10 02:16:57 +02:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2005-12-07 04:28:44 +01:00
|
|
|
verinum* PEConcat::eval_const(const Design*des, const NetScope*scope) const
|
|
|
|
|
{
|
|
|
|
|
verinum*accum = parms_[0]->eval_const(des, scope);
|
|
|
|
|
if (accum == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
for (unsigned idx = 1 ; idx < parms_.count() ; idx += 1) {
|
|
|
|
|
verinum*tmp = parms_[idx]->eval_const(des, scope);
|
2005-12-18 22:06:01 +01:00
|
|
|
/* Oops, found an argument that is not constant. Give up. */
|
|
|
|
|
if (tmp == 0) {
|
|
|
|
|
delete accum;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2005-12-07 04:28:44 +01:00
|
|
|
assert(tmp);
|
|
|
|
|
|
|
|
|
|
*accum = concat(*accum, *tmp);
|
|
|
|
|
delete tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return accum;
|
|
|
|
|
}
|
2001-01-15 00:04:55 +01:00
|
|
|
|
1999-09-18 03:52:48 +02:00
|
|
|
/*
|
|
|
|
|
* Evaluate an identifier as a constant expression. This is only
|
|
|
|
|
* possible if the identifier is that of a parameter.
|
|
|
|
|
*/
|
2001-11-07 05:01:59 +01:00
|
|
|
verinum* PEIdent::eval_const(const Design*des, const NetScope*scope) const
|
1999-05-10 02:16:57 +02:00
|
|
|
{
|
2000-03-08 05:36:53 +01:00
|
|
|
assert(scope);
|
2001-12-03 05:47:14 +01:00
|
|
|
const NetExpr*expr = des->find_parameter(scope, path_);
|
1999-09-18 03:52:48 +02:00
|
|
|
|
|
|
|
|
if (expr == 0)
|
1999-09-16 06:18:15 +02:00
|
|
|
return 0;
|
1999-09-18 03:52:48 +02:00
|
|
|
|
2002-06-07 04:57:54 +02:00
|
|
|
const NetEConst*eval = dynamic_cast<const NetEConst*>(expr);
|
2002-10-13 07:01:07 +02:00
|
|
|
if (eval == 0) {
|
|
|
|
|
cerr << get_line() << ": internal error: Unable to evaluate "
|
2002-10-20 00:59:49 +02:00
|
|
|
<< "constant expression (parameter=" << path_
|
|
|
|
|
<< "): " << *expr << endl;
|
2002-10-13 07:01:07 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2002-06-07 04:57:54 +02:00
|
|
|
assert(eval);
|
1999-11-29 00:42:02 +01:00
|
|
|
|
2002-06-07 04:57:54 +02:00
|
|
|
if (msb_ || lsb_)
|
1999-09-20 04:21:10 +02:00
|
|
|
return 0;
|
|
|
|
|
|
2002-06-07 04:57:54 +02:00
|
|
|
|
1999-05-10 02:16:57 +02:00
|
|
|
return new verinum(eval->value());
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 05:01:59 +01:00
|
|
|
verinum* PEFNumber::eval_const(const Design*, const NetScope*) const
|
2000-12-10 23:01:35 +01:00
|
|
|
{
|
|
|
|
|
long val = value_->as_long();
|
|
|
|
|
return new verinum(val);
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 05:01:59 +01:00
|
|
|
verinum* PENumber::eval_const(const Design*, const NetScope*) const
|
1998-11-04 00:28:49 +01:00
|
|
|
{
|
|
|
|
|
return new verinum(value());
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-23 05:08:50 +02:00
|
|
|
verinum* PEString::eval_const(const Design*, const NetScope*) const
|
|
|
|
|
{
|
|
|
|
|
return new verinum(string(text_));
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 05:01:59 +01:00
|
|
|
verinum* PETernary::eval_const(const Design*des, const NetScope*scope) const
|
1999-07-17 21:50:59 +02:00
|
|
|
{
|
2001-11-07 05:01:59 +01:00
|
|
|
verinum*test = expr_->eval_const(des, scope);
|
1999-11-30 05:48:17 +01:00
|
|
|
if (test == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
verinum::V bit = test->get(0);
|
|
|
|
|
delete test;
|
|
|
|
|
switch (bit) {
|
|
|
|
|
case verinum::V0:
|
2001-11-07 05:01:59 +01:00
|
|
|
return fal_->eval_const(des, scope);
|
1999-11-30 05:48:17 +01:00
|
|
|
case verinum::V1:
|
2001-11-07 05:01:59 +01:00
|
|
|
return tru_->eval_const(des, scope);
|
1999-11-30 05:48:17 +01:00
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
// XXXX It is possible to handle this case if both fal_
|
|
|
|
|
// and tru_ are constant. Someday...
|
|
|
|
|
}
|
1999-07-17 21:50:59 +02:00
|
|
|
}
|
|
|
|
|
|
2001-11-07 05:01:59 +01:00
|
|
|
verinum* PEUnary::eval_const(const Design*des, const NetScope*scope) const
|
2000-09-08 00:38:13 +02:00
|
|
|
{
|
2001-11-07 05:01:59 +01:00
|
|
|
verinum*val = expr_->eval_const(des, scope);
|
2000-09-08 00:38:13 +02:00
|
|
|
if (val == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
switch (op_) {
|
|
|
|
|
case '+':
|
|
|
|
|
return val;
|
|
|
|
|
|
2001-01-27 06:41:48 +01:00
|
|
|
case '-': {
|
|
|
|
|
/* We need to expand the value a bit if we are
|
|
|
|
|
taking the 2's complement so that we are
|
|
|
|
|
guaranteed to not overflow. */
|
|
|
|
|
verinum tmp (0UL, val->len()+1);
|
|
|
|
|
for (unsigned idx = 0 ; idx < val->len() ; idx += 1)
|
|
|
|
|
tmp.set(idx, val->get(idx));
|
|
|
|
|
|
|
|
|
|
*val = v_not(tmp) + verinum(verinum::V1, 1);
|
|
|
|
|
val->has_sign(true);
|
|
|
|
|
return val;
|
|
|
|
|
}
|
2000-09-08 00:38:13 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
delete val;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|