1998-11-04 00:28:49 +01:00
|
|
|
/*
|
2014-02-25 21:39:21 +01:00
|
|
|
* Copyright (c) 1998-2014 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
|
2012-08-29 03:41:23 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
1998-11-04 00:28:49 +01:00
|
|
|
*/
|
|
|
|
|
|
2001-07-25 05:10:48 +02:00
|
|
|
# include "config.h"
|
|
|
|
|
|
2008-01-05 00:23:47 +01:00
|
|
|
# include <cstring>
|
2001-07-25 05:10:48 +02:00
|
|
|
# include <iostream>
|
|
|
|
|
|
1998-11-04 00:28:49 +01:00
|
|
|
# include "PExpr.h"
|
1999-05-10 02:16:57 +02:00
|
|
|
# include "netlist.h"
|
2005-11-27 06:56:20 +01:00
|
|
|
# include "netmisc.h"
|
2001-01-15 00:04:55 +01:00
|
|
|
# include "compiler.h"
|
1998-11-04 00:28:49 +01:00
|
|
|
|
2007-06-04 21:14:06 +02:00
|
|
|
verinum* PExpr::eval_const(Design*, NetScope*) const
|
1998-11-04 00:28:49 +01:00
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-04 21:14:06 +02:00
|
|
|
verinum* PEBinary::eval_const(Design*des, 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();
|
|
|
|
|
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+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;
|
|
|
|
|
}
|
2007-06-04 21:14:06 +02:00
|
|
|
verinum* PEConcat::eval_const(Design*des, NetScope*scope) const
|
2005-12-07 05:04:23 +01:00
|
|
|
{
|
|
|
|
|
verinum*accum = parms_[0]->eval_const(des, scope);
|
|
|
|
|
if (accum == 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2010-10-26 04:36:44 +02:00
|
|
|
for (unsigned idx = 1 ; idx < parms_.size() ; idx += 1) {
|
2006-05-17 18:49:30 +02:00
|
|
|
|
2005-12-07 05:04:23 +01:00
|
|
|
verinum*tmp = parms_[idx]->eval_const(des, scope);
|
2006-05-17 18:49:30 +02:00
|
|
|
if (tmp == 0) {
|
|
|
|
|
delete accum;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2005-12-07 05:04:23 +01:00
|
|
|
assert(tmp);
|
|
|
|
|
|
|
|
|
|
*accum = concat(*accum, *tmp);
|
|
|
|
|
delete tmp;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return accum;
|
|
|
|
|
}
|
1999-05-10 02:16:57 +02:00
|
|
|
|
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.
|
|
|
|
|
*/
|
2007-06-04 21:14:06 +02:00
|
|
|
verinum* PEIdent::eval_const(Design*des, NetScope*scope) const
|
1999-05-10 02:16:57 +02:00
|
|
|
{
|
2000-03-08 05:36:53 +01:00
|
|
|
assert(scope);
|
2005-11-27 06:56:20 +01:00
|
|
|
NetNet*net;
|
|
|
|
|
NetEvent*eve;
|
|
|
|
|
const NetExpr*expr;
|
2006-04-10 02:37:42 +02:00
|
|
|
|
2007-05-24 06:07:11 +02:00
|
|
|
const name_component_t&name_tail = path_.back();
|
|
|
|
|
|
2006-04-10 02:37:42 +02:00
|
|
|
// Handle the special case that this ident is a genvar
|
|
|
|
|
// variable name. In that case, the genvar meaning preempts
|
|
|
|
|
// everything and we just return that value immediately.
|
|
|
|
|
if (scope->genvar_tmp
|
2007-05-24 06:07:11 +02:00
|
|
|
&& strcmp(name_tail.name,scope->genvar_tmp) == 0) {
|
2006-04-10 02:37:42 +02:00
|
|
|
return new verinum(scope->genvar_tmp_val);
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-28 18:52:39 +01:00
|
|
|
symbol_search(this, des, scope, path_, net, expr, eve);
|
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) {
|
2007-12-20 18:31:01 +01:00
|
|
|
cerr << get_fileline() << ": 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
|
|
|
|
2007-05-24 06:07:11 +02:00
|
|
|
if (!name_tail.index.empty())
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-04 21:14:06 +02:00
|
|
|
verinum* PEFNumber::eval_const(Design*, NetScope*) const
|
2000-12-10 23:01:35 +01:00
|
|
|
{
|
|
|
|
|
long val = value_->as_long();
|
|
|
|
|
return new verinum(val);
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-04 21:14:06 +02:00
|
|
|
verinum* PENumber::eval_const(Design*, NetScope*) const
|
1998-11-04 00:28:49 +01:00
|
|
|
{
|
|
|
|
|
return new verinum(value());
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-04 21:14:06 +02:00
|
|
|
verinum* PEString::eval_const(Design*, NetScope*) const
|
2002-05-23 05:08:50 +02:00
|
|
|
{
|
|
|
|
|
return new verinum(string(text_));
|
|
|
|
|
}
|
|
|
|
|
|
2007-06-04 21:14:06 +02:00
|
|
|
verinum* PETernary::eval_const(Design*des, 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
|
|
|
}
|
|
|
|
|
|
2007-06-04 21:14:06 +02:00
|
|
|
verinum* PEUnary::eval_const(Design*des, 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. */
|
2006-08-08 07:11:37 +02:00
|
|
|
verinum tmp ((uint64_t)0, val->len()+1);
|
2001-01-27 06:41:48 +01:00
|
|
|
for (unsigned idx = 0 ; idx < val->len() ; idx += 1)
|
|
|
|
|
tmp.set(idx, val->get(idx));
|
|
|
|
|
|
2014-02-25 21:39:21 +01:00
|
|
|
*val = -tmp;
|
2001-01-27 06:41:48 +01:00
|
|
|
val->has_sign(true);
|
|
|
|
|
return val;
|
|
|
|
|
}
|
2000-09-08 00:38:13 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
delete val;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|