diff --git a/elab_expr.cc b/elab_expr.cc index 2f92afa27..c1923b83c 100644 --- a/elab_expr.cc +++ b/elab_expr.cc @@ -675,10 +675,7 @@ NetExpr* PEBComp::elaborate_expr(Design*des, NetScope*scope, NetExpr*tmp = new NetEBComp(op_, lp, rp); tmp->set_line(*this); - tmp = pad_to_width(tmp, expr_wid, *this); - tmp->cast_signed(signed_flag_); - - return tmp; + return pad_to_width(tmp, expr_wid, signed_flag_, *this); } unsigned PEBLogic::test_width(Design*, NetScope*, width_mode_t&) @@ -716,10 +713,7 @@ NetExpr*PEBLogic::elaborate_expr(Design*des, NetScope*scope, NetExpr*tmp = new NetEBLogic(op_, lp, rp); tmp->set_line(*this); - tmp = pad_to_width(tmp, expr_wid, *this); - tmp->cast_signed(signed_flag_); - - return tmp; + return pad_to_width(tmp, expr_wid, signed_flag_, *this); } unsigned PEBLeftWidth::test_width(Design*des, NetScope*scope, width_mode_t&mode) @@ -1035,8 +1029,7 @@ NetExpr*PEBShift::elaborate_expr_leaf(Design*des, NetExpr*lp, NetExpr*rp, tmp->set_line(*this); tmp = new NetESelect(lp, tmp, 1); tmp->set_line(*this); - tmp = pad_to_width(tmp, expr_wid, *this); - tmp->cast_signed(true); + tmp = pad_to_width(tmp, expr_wid, true, *this); delete rp; return tmp; @@ -1414,23 +1407,7 @@ NetExpr*PECallFunction::cast_to_width_(NetExpr*expr, unsigned wid) const << " from expr_width()=" << expr->expr_width() << endl; } - /* If the expression is a const, then replace it with a new - const. This is a more efficient result. */ - if (NetEConst*tmp = dynamic_cast(expr)) { - tmp->cast_signed(signed_flag_); - if (wid != tmp->expr_width()) { - tmp = new NetEConst(verinum(tmp->value(), wid)); - tmp->set_line(*this); - delete expr; - } - return tmp; - } - - NetESelect*tmp = new NetESelect(expr, 0, wid); - tmp->cast_signed(signed_flag_); - tmp->set_line(*this); - - return tmp; + return cast_to_width(expr, wid, signed_flag_, *this); } /* @@ -1606,10 +1583,7 @@ NetExpr* PECallFunction::elaborate_sfunc_(Design*des, NetScope*scope, if (missing_parms || parm_errors) return 0; - NetExpr*tmp = pad_to_width(fun, expr_wid, *this); - tmp->cast_signed(signed_flag_); - - return tmp; + return pad_to_width(fun, expr_wid, signed_flag_, *this); } NetExpr* PECallFunction::elaborate_access_func_(Design*des, NetScope*scope, @@ -1666,10 +1640,7 @@ NetExpr* PECallFunction::elaborate_access_func_(Design*des, NetScope*scope, NetExpr*tmp = new NetEAccess(branch, nature); tmp->set_line(*this); - tmp = pad_to_width(tmp, expr_wid, *this); - tmp->cast_signed(signed_flag_); - - return tmp; + return pad_to_width(tmp, expr_wid, signed_flag_, *this); } /* @@ -2281,10 +2252,7 @@ NetExpr* PECallFunction::elaborate_base_(Design*des, NetScope*scope, NetScope*ds if(res->darray_type()) return func; - NetExpr*tmp = pad_to_width(func, expr_wid, *this); - tmp->cast_signed(signed_flag_); - - return tmp; + return pad_to_width(func, expr_wid, signed_flag_, *this); } cerr << get_fileline() << ": internal error: Unable to locate " @@ -2883,8 +2851,7 @@ NetExpr* PEConcat::elaborate_expr(Design*des, NetScope*scope, return 0; } - NetExpr*tmp = pad_to_width(concat, expr_wid, *this); - tmp->cast_signed(signed_flag_); + NetExpr*tmp = pad_to_width(concat, expr_wid, signed_flag_, *this); concat_depth -= 1; return tmp; @@ -3801,10 +3768,7 @@ NetExpr* PEIdent::elaborate_expr(Design*des, NetScope*scope, if (!tmp) return 0; - tmp = pad_to_width(tmp, expr_wid, *this); - tmp->cast_signed(signed_flag_); - - return tmp; + return pad_to_width(tmp, expr_wid, signed_flag_, *this); } // If the identifier names a signal (a register or wire) @@ -3840,10 +3804,7 @@ NetExpr* PEIdent::elaborate_expr(Design*des, NetScope*scope, << ", tmp=" << *tmp << endl; } - tmp = pad_to_width(tmp, expr_wid, *this); - tmp->cast_signed(signed_flag_); - - return tmp; + return pad_to_width(tmp, expr_wid, signed_flag_, *this); } // If the identifier is a named event @@ -3943,9 +3904,7 @@ NetExpr* PEIdent::elaborate_expr(Design*des, NetScope*scope, member_comp); if (!tmp) return 0; - tmp = pad_to_width(tmp, expr_wid, *this); - tmp->cast_signed(signed_flag_); - return tmp; + return pad_to_width(tmp, expr_wid, signed_flag_, *this); } if (net->class_type() != 0) { @@ -4524,7 +4483,7 @@ NetExpr* PEIdent::elaborate_expr_param_(Design*des, << "Elaborate parameter <" << path_ << "> as enumeration constant." << *etmp << endl; tmp = etmp->dup_expr(); - tmp = pad_to_width(tmp, expr_wid, *this); + tmp = pad_to_width(tmp, expr_wid, signed_flag_, *this); } else { perm_string name = peek_tail_name(path_); @@ -6114,7 +6073,7 @@ NetExpr* PEUnary::elaborate_expr(Design*des, NetScope*scope, } tmp->set_line(*this); } - tmp = pad_to_width(tmp, expr_wid, *this); + tmp = pad_to_width(tmp, expr_wid, signed_flag_, *this); break; case '&': // Reduction AND @@ -6132,7 +6091,7 @@ NetExpr* PEUnary::elaborate_expr(Design*des, NetScope*scope, } tmp = new NetEUReduce(op_, ip); tmp->set_line(*this); - tmp = pad_to_width(tmp, expr_wid, *this); + tmp = pad_to_width(tmp, expr_wid, signed_flag_, *this); break; case '~': diff --git a/netmisc.h b/netmisc.h index 2e2297b15..e0569bce0 100644 --- a/netmisc.h +++ b/netmisc.h @@ -59,14 +59,6 @@ inline NetScope* symbol_search(const LineInfo*li, return symbol_search(li, des, start, path, net, par, eve, ex1, ex2); } -/* - * This function transforms an expression by padding the high bits - * with V0 until the expression has the desired width. This may mean - * not transforming the expression at all, if it is already wide - * enough. - */ -extern NetExpr*pad_to_width(NetExpr*expr, unsigned wid, const LineInfo&info); - /* * This function transforms an expression by either zero or sign extending * the high bits until the expression has the desired width. This may mean @@ -76,6 +68,13 @@ extern NetExpr*pad_to_width(NetExpr*expr, unsigned wid, const LineInfo&info); */ extern NetExpr*pad_to_width(NetExpr*expr, unsigned wid, bool signed_flag, const LineInfo&info); +/* + * This version determines the extension method from the base expression type. + */ +inline NetExpr*pad_to_width(NetExpr*expr, unsigned wid, const LineInfo&info) +{ + return pad_to_width(expr, wid, expr->has_sign(), info); +} /* * This function transforms an expression by either zero or sign extending diff --git a/pad_to_width.cc b/pad_to_width.cc index 85f8d74e4..32db9e6c4 100644 --- a/pad_to_width.cc +++ b/pad_to_width.cc @@ -24,33 +24,6 @@ # include "netmisc.h" -/* - * This function transforms an expression by padding the high bits - * with V0 until the expression has the desired width. This may mean - * not transforming the expression at all, if it is already wide - * enough. - */ -NetExpr*pad_to_width(NetExpr*expr, unsigned wid, const LineInfo&info) -{ - if (wid <= expr->expr_width()) - return expr; - - /* If the expression is a const, then replace it with a wider - const. This is a more efficient result. */ - if (NetEConst*tmp = dynamic_cast(expr)) { - verinum oval = pad_to_width(tmp->value(), wid); - tmp = new NetEConst(oval); - tmp->set_line(info); - delete expr; - return tmp; - } - - NetESelect*tmp = new NetESelect(expr, 0, wid); - tmp->set_line(info); - tmp->cast_signed(expr->has_sign()); - return tmp; -} - NetExpr*pad_to_width(NetExpr*expr, unsigned wid, bool signed_flag, const LineInfo&info) {