2 * Copyright (c) 1999-2007 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 # include "compiler.h"
27 # include "ivl_assert.h"
30 * The default behavor for the test_width method is to just return the
31 * minimum width that is passed in.
33 unsigned PExpr::test_width(Design
*des
, NetScope
*scope
,
34 unsigned min
, unsigned lval
, bool&) const
36 if (debug_elaborate
) {
37 cerr
<< get_line() << ": debug: test_width defaults to "
38 << min
<< ", ignoring unsized_flag. typeid="
39 << typeid(*this).name() << endl
;
44 NetExpr
* PExpr::elaborate_expr(Design
*des
, NetScope
*, int, bool) const
46 cerr
<< get_line() << ": internal error: I do not know how to elaborate"
47 << " expression. " << endl
;
48 cerr
<< get_line() << ": : Expression is: " << *this
54 unsigned PEBinary::test_width(Design
*des
, NetScope
*scope
,
55 unsigned min
, unsigned lval
, bool&unsized_flag
) const
57 bool flag_left
= false;
58 bool flag_right
= false;
59 unsigned wid_left
= left_
->test_width(des
,scope
, min
, lval
, flag_left
);
60 unsigned wid_right
= right_
->test_width(des
,scope
, min
, lval
, flag_right
);
62 if (flag_left
|| flag_right
)
76 if (lval
> 0 && min
> lval
)
92 * Elaborate binary expressions. This involves elaborating the left
93 * and right sides, and creating one of a variety of different NetExpr
96 NetEBinary
* PEBinary::elaborate_expr(Design
*des
, NetScope
*scope
,
97 int expr_wid
, bool) const
102 NetExpr
*lp
= left_
->elaborate_expr(des
, scope
, expr_wid
, false);
103 NetExpr
*rp
= right_
->elaborate_expr(des
, scope
, expr_wid
, false);
104 if ((lp
== 0) || (rp
== 0)) {
110 NetEBinary
*tmp
= elaborate_eval_expr_base_(des
, lp
, rp
, expr_wid
);
114 NetEBinary
* PEBinary::elaborate_eval_expr_base_(Design
*des
,
119 /* If either expression can be evaluated ahead of time, then
120 do so. This can prove helpful later. */
122 tmp
= lp
->eval_tree();
128 tmp
= rp
->eval_tree();
135 return elaborate_expr_base_(des
, lp
, rp
, expr_wid
);
139 * This is common elaboration of the operator. It presumes that the
140 * operands are elaborated as necessary, and all I need to do is make
141 * the correct NetEBinary object and connect the parameters.
143 NetEBinary
* PEBinary::elaborate_expr_base_(Design
*des
,
144 NetExpr
*lp
, NetExpr
*rp
,
149 if (debug_elaborate
) {
150 cerr
<< get_line() << ": debug: elaborate expression "
151 << *this << " expr_wid=" << expr_wid
<< endl
;
158 tmp
= new NetEBinary(op_
, lp
, rp
);
159 tmp
->set_line(*this);
164 tmp
= new NetEBLogic(op_
, lp
, rp
);
165 tmp
->set_line(*this);
169 tmp
= new NetEBPow(op_
, lp
, rp
);
170 tmp
->set_line(*this);
174 tmp
= new NetEBMult(op_
, lp
, rp
);
175 tmp
->set_line(*this);
179 /* The % operator does not support real arguments in
180 baseline Verilog. But we allow it in our extended
182 if (generation_flag
< GN_VER2001X
) {
183 if (lp
->expr_type()==IVL_VT_REAL
|| rp
->expr_type()==IVL_VT_REAL
) {
184 cerr
<< get_line() << ": error: Modulus operator may not "
185 "have REAL operands." << endl
;
189 /* Fall through to handle the % with the / operator. */
191 tmp
= new NetEBDiv(op_
, lp
, rp
);
192 tmp
->set_line(*this);
198 tmp
= new NetEBShift(op_
, lp
, rp
);
199 tmp
->set_line(*this);
205 case 'O': // NOR (~|)
206 case 'A': // NAND (~&)
208 tmp
= new NetEBBits(op_
, lp
, rp
);
209 tmp
->set_line(*this);
214 tmp
= new NetEBAdd(op_
, lp
, rp
, expr_wid
==-2? true : false);
215 if (expr_wid
> 0 && (tmp
->expr_type() == IVL_VT_BOOL
216 || tmp
->expr_type() == IVL_VT_LOGIC
))
217 tmp
->set_width(expr_wid
);
218 tmp
->set_line(*this);
223 if (lp
->expr_type() == IVL_VT_REAL
224 || rp
->expr_type() == IVL_VT_REAL
) {
225 cerr
<< get_line() << ": error: Case equality may not "
226 << "have real operands." << endl
;
229 /* Fall through... */
232 if (dynamic_cast<NetEConst
*>(rp
)
233 && (lp
->expr_width() > rp
->expr_width()))
234 rp
->set_width(lp
->expr_width());
236 if (dynamic_cast<NetEConst
*>(lp
)
237 && (lp
->expr_width() < rp
->expr_width()))
238 lp
->set_width(rp
->expr_width());
240 /* from here, handle this like other compares. */
245 tmp
= new NetEBComp(op_
, lp
, rp
);
246 tmp
->set_line(*this);
247 flag
= tmp
->set_width(1);
249 cerr
<< get_line() << ": internal error: "
250 "expression bit width of comparison != 1." << endl
;
259 unsigned PEBComp::test_width(Design
*, NetScope
*,unsigned, unsigned, bool&) const
264 NetEBinary
* PEBComp::elaborate_expr(Design
*des
, NetScope
*scope
,
265 int expr_width
, bool sys_task_arg
) const
270 bool unsized_flag
= false;
271 unsigned left_width
= left_
->test_width(des
, scope
, 0, 0, unsized_flag
);
272 bool save_flag
= unsized_flag
;
273 unsigned right_width
= right_
->test_width(des
, scope
, 0, 0, unsized_flag
);
275 if (save_flag
!= unsized_flag
)
276 left_width
= left_
->test_width(des
, scope
, 0, 0, unsized_flag
);
278 /* Width of operands is self-determined. */
279 int use_wid
= left_width
;
280 if (right_width
> left_width
)
281 use_wid
= right_width
;
283 if (debug_elaborate
) {
284 cerr
<< get_line() << ": debug: "
285 << "Comparison expression operands are "
286 << left_width
<< " bits and "
287 << right_width
<< " bits. Resorting to "
288 << use_wid
<< " bits." << endl
;
291 NetExpr
*lp
= left_
->elaborate_expr(des
, scope
, use_wid
, false);
292 NetExpr
*rp
= right_
->elaborate_expr(des
, scope
, use_wid
, false);
293 if ((lp
== 0) || (rp
== 0)) {
299 return elaborate_eval_expr_base_(des
, lp
, rp
, use_wid
);
302 unsigned PEBShift::test_width(Design
*des
, NetScope
*scope
,
303 unsigned min
, unsigned lval
, bool&unsized_flag
) const
305 unsigned wid_left
= left_
->test_width(des
,scope
,min
, 0, unsized_flag
);
307 // The right expression is self-determined and has no impact
308 // on the expression size that is generated.
313 unsigned PECallFunction::test_width_sfunc_(Design
*des
, NetScope
*scope
,
314 unsigned min
, unsigned lval
,
315 bool&unsized_flag
) const
317 perm_string name
= peek_tail_name(path_
);
319 if (name
=="$signed"|| name
=="$unsigned") {
320 PExpr
*expr
= parms_
[0];
323 unsigned wid
= expr
->test_width(des
, scope
, min
, lval
, unsized_flag
);
325 cerr
<< get_line() << ": debug: test_width"
326 << " of $signed/$unsigned returns test_width"
327 << " of subexpression." << endl
;
332 cerr
<< get_line() << ": debug: test_width "
333 << "of system function " << name
334 << " returns 32 always?" << endl
;
338 unsigned PECallFunction::test_width(Design
*des
, NetScope
*scope
,
339 unsigned min
, unsigned lval
,
340 bool&unsized_flag
) const
342 if (peek_tail_name(path_
)[0] == '$')
343 return test_width_sfunc_(des
, scope
, min
, lval
, unsized_flag
);
345 NetFuncDef
*def
= des
->find_function(scope
, path_
);
348 cerr
<< get_line() << ": debug: test_width "
349 << "cannot find definition of " << path_
350 << " in " << scope_path(scope
) << "." << endl
;
354 NetScope
*dscope
= def
->scope();
357 if (NetNet
*res
= dscope
->find_signal(dscope
->basename())) {
359 cerr
<< get_line() << ": debug: test_width "
360 << "of function returns width " << res
->vector_width()
362 return res
->vector_width();
365 ivl_assert(*this, 0);
370 * Given a call to a system function, generate the proper expression
371 * nodes to represent the call in the netlist. Since we don't support
372 * size_tf functions, make assumptions about widths based on some
373 * known function names.
375 NetExpr
* PECallFunction::elaborate_sfunc_(Design
*des
, NetScope
*scope
, int expr_wid
) const
378 /* Catch the special case that the system function is the
379 $signed function. This function is special, in that it does
380 not lead to executable code but takes the single parameter
381 and makes it into a signed expression. No bits are changed,
382 it just changes the interpretation. */
383 if (strcmp(peek_tail_name(path_
), "$signed") == 0) {
384 if ((parms_
.count() != 1) || (parms_
[0] == 0)) {
385 cerr
<< get_line() << ": error: The $signed() function "
386 << "takes exactly one(1) argument." << endl
;
391 PExpr
*expr
= parms_
[0];
392 NetExpr
*sub
= expr
->elaborate_expr(des
, scope
, -1, true);
393 sub
->cast_signed(true);
396 /* add $unsigned to match $signed */
397 if (strcmp(peek_tail_name(path_
), "$unsigned") == 0) {
398 if ((parms_
.count() != 1) || (parms_
[0] == 0)) {
399 cerr
<< get_line() << ": error: The $unsigned() function "
400 << "takes exactly one(1) argument." << endl
;
405 PExpr
*expr
= parms_
[0];
406 NetExpr
*sub
= expr
->elaborate_expr(des
, scope
, -1, true);
407 sub
->cast_signed(false);
409 if (expr_wid
> 0 && (unsigned)expr_wid
> sub
->expr_width())
410 sub
= pad_to_width(sub
, expr_wid
);
415 /* Interpret the internal $sizeof system function to return
416 the bit width of the sub-expression. The value of the
417 sub-expression is not used, so the expression itself can be
419 if ((strcmp(peek_tail_name(path_
), "$sizeof") == 0)
420 || (strcmp(peek_tail_name(path_
), "$bits") == 0)) {
421 if ((parms_
.count() != 1) || (parms_
[0] == 0)) {
422 cerr
<< get_line() << ": error: The $bits() function "
423 << "takes exactly one(1) argument." << endl
;
428 if (strcmp(peek_tail_name(path_
), "$sizeof") == 0)
429 cerr
<< get_line() << ": warning: $sizeof is deprecated."
430 << " Use $bits() instead." << endl
;
432 PExpr
*expr
= parms_
[0];
433 ivl_assert(*this, expr
);
435 /* Elaborate the sub-expression to get its
436 self-determined width, and save that width. Then
437 delete the expression because we don't really want
438 the expression itself. */
439 long sub_expr_width
= 0;
440 if (NetExpr
*tmp
= expr
->elaborate_expr(des
, scope
, -1, true)) {
441 sub_expr_width
= tmp
->expr_width();
445 verinum
val (sub_expr_width
, 8*sizeof(unsigned));
446 NetEConst
*sub
= new NetEConst(val
);
447 sub
->set_line(*this);
452 /* Interpret the internal $is_signed system function to return
453 a single bit flag -- 1 if the expression is signed, 0
454 otherwise. The subexpression is elaborated but not
456 if (strcmp(peek_tail_name(path_
), "$is_signed") == 0) {
457 if ((parms_
.count() != 1) || (parms_
[0] == 0)) {
458 cerr
<< get_line() << ": error: The $is_signed() function "
459 << "takes exactly one(1) argument." << endl
;
464 PExpr
*expr
= parms_
[0];
465 NetExpr
*sub
= expr
->elaborate_expr(des
, scope
, -1, true);
467 verinum
val (sub
->has_sign()? verinum::V1
: verinum::V0
, 1);
470 sub
= new NetEConst(val
);
471 sub
->set_line(*this);
476 /* Get the return type of the system function by looking it up
477 in the sfunc_table. */
478 const struct sfunc_return_type
*sfunc_info
479 = lookup_sys_func(peek_tail_name(path_
));
481 ivl_variable_type_t sfunc_type
= sfunc_info
->type
;
482 unsigned wid
= sfunc_info
->wid
;
485 /* How many parameters are there? The Verilog language allows
486 empty parameters in certain contexts, so the parser will
487 allow things like func(1,,3). It will also cause func() to
488 be interpreted as a single empty parameter.
490 Functions cannot really take empty parameters, but the
491 case ``func()'' is the same as no parameters at all. So
492 catch that special case here. */
493 unsigned nparms
= parms_
.count();
494 if ((nparms
== 1) && (parms_
[0] == 0))
497 NetESFunc
*fun
= new NetESFunc(peek_tail_name(path_
), sfunc_type
,
499 if (sfunc_info
->signed_flag
)
500 fun
->cast_signed(true);
502 /* Now run through the expected parameters. If we find that
503 there are missing parameters, print an error message.
505 While we're at it, try to evaluate the function parameter
506 expression as much as possible, and use the reduced
507 expression if one is created. */
509 unsigned missing_parms
= 0;
510 for (unsigned idx
= 0 ; idx
< nparms
; idx
+= 1) {
511 PExpr
*expr
= parms_
[idx
];
513 NetExpr
*tmp1
= expr
->elaborate_expr(des
, scope
, -1, true);
514 if (NetExpr
*tmp2
= tmp1
->eval_tree()) {
516 fun
->parm(idx
, tmp2
);
518 fun
->parm(idx
, tmp1
);
527 if (missing_parms
> 0) {
528 cerr
<< get_line() << ": error: The function "
529 << peek_tail_name(path_
)
530 << " has been called with empty parameters." << endl
;
531 cerr
<< get_line() << ": : Verilog doesn't allow "
532 << "passing empty parameters to functions." << endl
;
539 NetExpr
* PECallFunction::elaborate_expr(Design
*des
, NetScope
*scope
,
540 int expr_wid
, bool) const
542 if (peek_tail_name(path_
)[0] == '$')
543 return elaborate_sfunc_(des
, scope
, expr_wid
);
545 NetFuncDef
*def
= des
->find_function(scope
, path_
);
547 cerr
<< get_line() << ": error: No function " << path_
<<
548 " in this context (" << scope_path(scope
) << ")." << endl
;
554 NetScope
*dscope
= def
->scope();
557 if (! check_call_matches_definition_(des
, dscope
))
560 unsigned parms_count
= parms_
.count();
561 if ((parms_count
== 1) && (parms_
[0] == 0))
566 svector
<NetExpr
*> parms (parms_count
);
568 /* Elaborate the input expressions for the function. This is
569 done in the scope of the function call, and not the scope
570 of the function being called. The scope of the called
571 function is elaborated when the definition is elaborated. */
573 unsigned missing_parms
= 0;
574 for (unsigned idx
= 0 ; idx
< parms
.count() ; idx
+= 1) {
575 PExpr
*tmp
= parms_
[idx
];
577 int argwid
= def
->port(idx
)->vector_width();
578 parms
[idx
] = elab_and_eval(des
, scope
, tmp
, argwid
);
580 cerr
<< get_line() << ": debug:"
581 << " function " << path_
582 << " arg " << (idx
+1)
583 << " argwid=" << argwid
584 << ": " << *parms
[idx
] << endl
;
592 if (missing_parms
> 0) {
593 cerr
<< get_line() << ": error: The function " << path_
594 << " has been called with empty parameters." << endl
;
595 cerr
<< get_line() << ": : Verilog doesn't allow "
596 << "passing empty parameters to functions." << endl
;
601 /* Look for the return value signal for the called
602 function. This return value is a magic signal in the scope
603 of the function, that has the name of the function. The
604 function code assigns to this signal to return a value.
606 dscope, in this case, is the scope of the function, so the
607 return value is the name within that scope. */
609 if (NetNet
*res
= dscope
->find_signal(dscope
->basename())) {
610 NetESignal
*eres
= new NetESignal(res
);
611 NetEUFunc
*func
= new NetEUFunc(dscope
, eres
, parms
);
612 func
->set_line(*this);
613 func
->cast_signed(res
->get_signed());
617 cerr
<< get_line() << ": internal error: Unable to locate "
618 "function return value for " << path_
619 << " in " << dscope
->basename() << "." << endl
;
625 NetExpr
* PEConcat::elaborate_expr(Design
*des
, NetScope
*scope
,
626 int expr_wid
, bool) const
630 if (debug_elaborate
) {
631 cerr
<< get_line() << ": debug: Elaborate expr=" << *this
632 << ", expr_wid=" << expr_wid
<< endl
;
635 /* If there is a repeat expression, then evaluate the constant
636 value and set the repeat count. */
638 NetExpr
*tmp
= elab_and_eval(des
, scope
, repeat_
, -1);
640 NetEConst
*rep
= dynamic_cast<NetEConst
*>(tmp
);
643 cerr
<< get_line() << ": error: "
644 "concatenation repeat expression cannot be evaluated."
646 cerr
<< get_line() << ": : The expression is: "
654 /* Make the empty concat expression. */
655 NetEConcat
*tmp
= new NetEConcat(parms_
.count(), repeat
);
656 tmp
->set_line(*this);
658 unsigned wid_sum
= 0;
660 /* Elaborate all the parameters and attach them to the concat node. */
661 for (unsigned idx
= 0 ; idx
< parms_
.count() ; idx
+= 1) {
662 if (parms_
[idx
] == 0) {
663 cerr
<< get_line() << ": error: Missing expression "
664 << (idx
+1) << " of concatenation list." << endl
;
670 NetExpr
*ex
= elab_and_eval(des
, scope
, parms_
[idx
], 0, 0);
671 if (ex
== 0) continue;
673 ex
->set_line(*parms_
[idx
]);
675 if (! ex
->has_width()) {
676 cerr
<< ex
->get_line() << ": error: operand of "
677 << "concatenation has indefinite width: "
682 wid_sum
+= ex
->expr_width();
686 tmp
->set_width(wid_sum
* tmp
->repeat());
691 NetExpr
* PEFNumber::elaborate_expr(Design
*des
, NetScope
*scope
, int, bool) const
693 NetECReal
*tmp
= new NetECReal(*value_
);
694 tmp
->set_line(*this);
695 tmp
->set_width(1U, false);
700 * Given that the msb_ and lsb_ are part select expressions, this
701 * function calculates their values. Note that this method does *not*
702 * convert the values to canonical form.
704 bool PEIdent::calculate_parts_(Design
*des
, NetScope
*scope
,
705 long&msb
, long&lsb
) const
707 const name_component_t
&name_tail
= path_
.back();
708 ivl_assert(*this, !name_tail
.index
.empty());
710 const index_component_t
&index_tail
= name_tail
.index
.back();
711 ivl_assert(*this, index_tail
.sel
== index_component_t::SEL_PART
);
712 ivl_assert(*this, index_tail
.msb
&& index_tail
.lsb
);
714 /* This handles part selects. In this case, there are
715 two bit select expressions, and both must be
716 constant. Evaluate them and pass the results back to
718 NetExpr
*lsb_ex
= elab_and_eval(des
, scope
, index_tail
.lsb
, -1);
719 NetEConst
*lsb_c
= dynamic_cast<NetEConst
*>(lsb_ex
);
721 cerr
<< index_tail
.lsb
->get_line() << ": error: "
722 "Part select expressions must be constant."
724 cerr
<< index_tail
.lsb
->get_line() << ": : "
725 "This lsb expression violates the rule: "
726 << *index_tail
.lsb
<< endl
;
731 NetExpr
*msb_ex
= elab_and_eval(des
, scope
, index_tail
.msb
, -1);
732 NetEConst
*msb_c
= dynamic_cast<NetEConst
*>(msb_ex
);
734 cerr
<< index_tail
.msb
->get_line() << ": error: "
735 "Part select expressions must be constant."
737 cerr
<< index_tail
.msb
->get_line() << ": : This msb expression "
738 "violates the rule: " << *index_tail
.msb
<< endl
;
743 msb
= msb_c
->value().as_long();
744 lsb
= lsb_c
->value().as_long();
751 bool PEIdent::calculate_up_do_width_(Design
*des
, NetScope
*scope
,
752 unsigned long&wid
) const
754 const name_component_t
&name_tail
= path_
.back();
755 ivl_assert(*this, !name_tail
.index
.empty());
757 const index_component_t
&index_tail
= name_tail
.index
.back();
758 ivl_assert(*this, index_tail
.lsb
&& index_tail
.msb
);
762 /* Calculate the width expression (in the lsb_ position)
763 first. If the expression is not constant, error but guess 1
764 so we can keep going and find more errors. */
765 NetExpr
*wid_ex
= elab_and_eval(des
, scope
, index_tail
.lsb
, -1);
766 NetEConst
*wid_c
= dynamic_cast<NetEConst
*>(wid_ex
);
769 cerr
<< get_line() << ": error: Indexed part width must be "
770 << "constant. Expression in question is..." << endl
;
771 cerr
<< get_line() << ": : " << *wid_ex
<< endl
;
776 wid
= wid_c
? wid_c
->value().as_ulong() : 1;
782 unsigned PEIdent::test_width(Design
*des
, NetScope
*scope
,
783 unsigned min
, unsigned lval
,
784 bool&unsized_flag
) const
787 const NetExpr
*par
= 0;
790 const NetExpr
*ex1
, *ex2
;
792 symbol_search(des
, scope
, path_
, net
, par
, eve
, ex1
, ex2
);
795 const name_component_t
&name_tail
= path_
.back();
796 index_component_t::ctype_t use_sel
= index_component_t::SEL_NONE
;
797 if (!name_tail
.index
.empty())
798 use_sel
= name_tail
.index
.back().sel
;
800 unsigned use_width
= net
->vector_width();
802 case index_component_t::SEL_NONE
:
804 case index_component_t::SEL_PART
:
806 calculate_parts_(des
, scope
, msb
, lsb
);
807 use_width
= 1 + ((msb
>lsb
)? (msb
-lsb
) : (lsb
-msb
));
810 case index_component_t::SEL_IDX_UP
:
811 case index_component_t::SEL_IDX_DO
:
812 { unsigned long tmp
= 0;
813 calculate_up_do_width_(des
, scope
, tmp
);
817 case index_component_t::SEL_BIT
:
821 ivl_assert(*this, 0);
830 * Elaborate an identifier in an expression. The identifier can be a
831 * parameter name, a signal name or a memory name. It can also be a
832 * scope name (Return a NetEScope) but only certain callers can use
833 * scope names. However, we still support it here.
835 * Function names are not handled here, they are detected by the
836 * parser and are elaborated by PECallFunction.
838 * The signal name may be escaped, but that affects nothing here.
840 NetExpr
* PEIdent::elaborate_expr(Design
*des
, NetScope
*scope
,
841 int expr_wid
, bool sys_task_arg
) const
846 const NetExpr
*par
= 0;
849 const NetExpr
*ex1
, *ex2
;
851 NetScope
*found_in
= symbol_search(des
, scope
, path_
,
855 // If the identifier name is a parameter name, then return
856 // a reference to the parameter expression.
858 return elaborate_expr_param(des
, scope
, par
, found_in
, ex1
, ex2
);
861 // If the identifier names a signal (a register or wire)
862 // then create a NetESignal node to handle it.
864 return elaborate_expr_net(des
, scope
, net
, found_in
, sys_task_arg
);
866 // If the identifier is a named event.
867 // is a variable reference.
869 NetEEvent
*tmp
= new NetEEvent(eve
);
870 tmp
->set_line(*this);
874 // Hmm... maybe this is a genvar? This is only possible while
875 // processing generate blocks, but then the genvar_tmp will be
877 if (path_
.size() == 1
878 && scope
->genvar_tmp
.str()
879 && strcmp(peek_tail_name(path_
), scope
->genvar_tmp
) == 0) {
881 cerr
<< get_line() << ": debug: " << path_
882 << " is genvar with value " << scope
->genvar_tmp_val
884 verinum
val (scope
->genvar_tmp_val
);
885 NetEConst
*tmp
= new NetEConst(val
);
886 tmp
->set_line(*this);
890 // A specparam? Look up the name to see if it is a
891 // specparam. If we find it, then turn it into a NetEConst
892 // value and return that. Of course, this does not apply if
893 // specify blocks are disabled.
895 if (gn_specify_blocks_flag
) {
896 map
<perm_string
,NetScope::spec_val_t
>::const_iterator specp
;
897 perm_string key
= peek_tail_name(path_
);
898 if (path_
.size() == 1
899 && ((specp
= scope
->specparams
.find(key
)) != scope
->specparams
.end())) {
900 NetScope::spec_val_t value
= (*specp
).second
;
902 switch (value
.type
) {
904 tmp
= new NetEConst(verinum(value
.integer
));
907 tmp
= new NetECReal(verireal(value
.real_val
));
913 tmp
->set_line(*this);
916 cerr
<< get_line() << ": debug: " << path_
917 << " is a specparam" << endl
;
922 // At this point we've exhausted all the possibilities that
923 // are not scopes. If this is not a system task argument, then
924 // it cannot be a scope name, so give up.
926 if (! sys_task_arg
) {
927 // I cannot interpret this identifier. Error message.
928 cerr
<< get_line() << ": error: Unable to bind wire/reg/memory "
929 "`" << path_
<< "' in `" << scope_path(scope
) << "'" << endl
;
934 // Finally, if this is a scope name, then return that. Look
935 // first to see if this is a name of a local scope. Failing
936 // that, search globally for a hierarchical name.
937 if ((path_
.size() == 1)) {
938 hname_t
use_name ( peek_tail_name(path_
) );
939 if (NetScope
*nsc
= scope
->child(use_name
)) {
940 NetEScope
*tmp
= new NetEScope(nsc
);
941 tmp
->set_line(*this);
944 cerr
<< get_line() << ": debug: Found scope "
945 << use_name
<< " in scope " << scope
->basename()
952 list
<hname_t
> spath
= eval_scope_path(des
, scope
, path_
);
954 ivl_assert(*this, spath
.size() == path_
.size());
956 // Try full hierarchical scope name.
957 if (NetScope
*nsc
= des
->find_scope(spath
)) {
958 NetEScope
*tmp
= new NetEScope(nsc
);
959 tmp
->set_line(*this);
962 cerr
<< get_line() << ": debug: Found scope "
964 << " path=" << path_
<< endl
;
966 if (! sys_task_arg
) {
967 cerr
<< get_line() << ": error: Scope name "
968 << nsc
->basename() << " not allowed here." << endl
;
975 // Try relative scope name.
976 if (NetScope
*nsc
= des
->find_scope(scope
, spath
)) {
977 NetEScope
*tmp
= new NetEScope(nsc
);
978 tmp
->set_line(*this);
981 cerr
<< get_line() << ": debug: Found scope "
982 << nsc
->basename() << " in " << scope_path(scope
) << endl
;
987 // I cannot interpret this identifier. Error message.
988 cerr
<< get_line() << ": error: Unable to bind wire/reg/memory "
989 "`" << path_
<< "' in `" << scope_path(scope
) << "'" << endl
;
995 * Handle the case that the identifier is a parameter reference. The
996 * parameter expression has already been located for us (as the par
997 * argument) so we just need to process the sub-expression.
999 NetExpr
* PEIdent::elaborate_expr_param(Design
*des
,
1003 const NetExpr
*par_msb
,
1004 const NetExpr
*par_lsb
) const
1006 NetExpr
*tmp
= par
->dup_expr();
1008 const name_component_t
&name_tail
= path_
.back();
1009 index_component_t::ctype_t use_sel
= index_component_t::SEL_NONE
;
1010 if (!name_tail
.index
.empty())
1011 use_sel
= name_tail
.index
.back().sel
;
1013 if (use_sel
== index_component_t::SEL_PART
) {
1014 ivl_assert(*this, !name_tail
.index
.empty());
1015 const index_component_t
&index_tail
= name_tail
.index
.back();
1016 ivl_assert(*this, index_tail
.msb
);
1017 ivl_assert(*this, index_tail
.lsb
);
1019 /* If the identifier has a part select, we support
1020 it by pulling the right bits out and making a
1021 sized unsigned constant. This code assumes the
1022 lsb of a parameter is 0 and the msb is the
1023 width of the parameter. */
1025 verinum
*lsn
= index_tail
.lsb
->eval_const(des
, scope
);
1026 verinum
*msn
= index_tail
.msb
->eval_const(des
, scope
);
1027 if ((lsn
== 0) || (msn
== 0)) {
1028 cerr
<< get_line() << ": error: "
1029 "Part select expressions must be "
1030 "constant expressions." << endl
;
1035 long lsb
= lsn
->as_long();
1036 long msb
= msn
->as_long();
1037 if ((lsb
< 0) || (msb
< lsb
)) {
1038 cerr
<< get_line() << ": error: invalid part "
1039 << "select: " << path_
1040 << "["<<msb
<<":"<<lsb
<<"]" << endl
;
1044 unsigned long ulsb
=lsb
;
1045 unsigned long umsb
=msb
;
1047 NetEConst
*le
= dynamic_cast<NetEConst
*>(tmp
);
1050 verinum
result (verinum::V0
, msb
-lsb
+1, true);
1051 verinum exl
= le
->value();
1053 /* Pull the bits from the parameter, one at a
1054 time. If the bit is within the range, simply
1055 copy it to the result. If the bit is outside
1056 the range, we sign extend signed unsized
1057 numbers, zero extend unsigned unsigned numbers,
1058 and X extend sized numbers. */
1059 for (unsigned long idx
= ulsb
; idx
<= umsb
; idx
+= 1) {
1060 if (idx
< exl
.len())
1061 result
.set(idx
-lsb
, exl
.get(idx
));
1062 else if (exl
.is_string())
1063 result
.set(idx
-lsb
, verinum::V0
);
1064 else if (exl
.has_len())
1065 result
.set(idx
-lsb
, verinum::Vx
);
1066 else if (exl
.has_sign())
1067 result
.set(idx
-lsb
, exl
.get(exl
.len()-1));
1069 result
.set(idx
-lsb
, verinum::V0
);
1072 /* If the input is a string, and the part select
1073 is working on byte boundaries, then the result
1074 can be made into a string. */
1077 && (result
.len()%8 == 0))
1078 result
= verinum(result
.as_string());
1081 tmp
= new NetEConst(result
);
1083 } else if (use_sel
== index_component_t::SEL_IDX_UP
|| use_sel
== index_component_t::SEL_IDX_DO
) {
1085 ivl_assert(*this, !name_tail
.index
.empty());
1086 const index_component_t
&index_tail
= name_tail
.index
.back();
1087 ivl_assert(*this, index_tail
.msb
);
1088 ivl_assert(*this, index_tail
.lsb
);
1090 /* Get and evaluate the width of the index
1091 select. This must be constant. */
1092 NetExpr
*wid_ex
= elab_and_eval(des
, scope
, index_tail
.lsb
, -1);
1093 NetEConst
*wid_ec
= dynamic_cast<NetEConst
*> (wid_ex
);
1095 cerr
<< index_tail
.lsb
->get_line() << ": error: "
1096 << "Second expression of indexed part select "
1097 << "most be constant." << endl
;
1102 unsigned wid
= wid_ec
->value().as_ulong();
1104 NetExpr
*idx_ex
= elab_and_eval(des
, scope
, index_tail
.msb
, -1);
1109 if (use_sel
== index_component_t::SEL_IDX_DO
&& wid
> 1) {
1110 idx_ex
= make_add_expr(idx_ex
, 1-(long)wid
);
1114 /* Wrap the param expression with a part select. */
1115 tmp
= new NetESelect(tmp
, idx_ex
, wid
);
1118 } else if (use_sel
== index_component_t::SEL_BIT
) {
1119 ivl_assert(*this, !name_tail
.index
.empty());
1120 const index_component_t
&index_tail
= name_tail
.index
.back();
1121 ivl_assert(*this, index_tail
.msb
);
1122 ivl_assert(*this, !index_tail
.lsb
);
1124 /* Handle the case where a parameter has a bit
1125 select attached to it. Generate a NetESelect
1126 object to select the bit as desired. */
1127 NetExpr
*mtmp
= index_tail
.msb
->elaborate_expr(des
, scope
, -1,false);
1128 if (! dynamic_cast<NetEConst
*>(mtmp
)) {
1129 NetExpr
*re
= mtmp
->eval_tree();
1136 /* Let's first try to get constant values for both
1137 the parameter and the bit select. If they are
1138 both constant, then evaluate the bit select and
1139 return instead a single-bit constant. */
1141 NetEConst
*le
= dynamic_cast<NetEConst
*>(tmp
);
1142 NetEConst
*re
= dynamic_cast<NetEConst
*>(mtmp
);
1145 /* Argument and bit select are constant. Calculate
1146 the final result. */
1147 verinum lv
= le
->value();
1148 verinum rv
= re
->value();
1149 verinum::V rb
= verinum::Vx
;
1151 long ridx
= rv
.as_long();
1152 if ((ridx
>= 0) && ((unsigned long) ridx
< lv
.len())) {
1155 } else if ((ridx
>= 0) && (!lv
.has_len())) {
1157 rb
= lv
[lv
.len()-1];
1162 NetEConst
*re
= new NetEConst(verinum(rb
, 1));
1169 const NetEConst
*par_me
=dynamic_cast<const NetEConst
*>(par_msb
);
1170 const NetEConst
*par_le
=dynamic_cast<const NetEConst
*>(par_lsb
);
1172 assert(par_me
|| !par_msb
);
1173 assert(par_le
|| !par_lsb
);
1174 assert(par_me
|| !par_le
);
1177 long par_mv
= par_me
->value().as_long();
1178 long par_lv
= par_le
->value().as_long();
1179 if (par_mv
>= par_lv
) {
1181 ? make_add_expr(mtmp
, 0-par_lv
)
1185 mtmp
= make_add_expr(mtmp
, 0-par_mv
);
1186 mtmp
= make_sub_expr(par_lv
-par_mv
, mtmp
);
1190 /* The value is constant, but the bit select
1191 expression is not. Elaborate a NetESelect to
1192 evaluate the select at run-time. */
1194 NetESelect
*stmp
= new NetESelect(tmp
, mtmp
, 1);
1195 tmp
->set_line(*this);
1200 /* No bit or part select. Make the constant into a
1201 NetEConstParam if possible. */
1202 NetEConst
*ctmp
= dynamic_cast<NetEConst
*>(tmp
);
1204 perm_string name
= peek_tail_name(path_
);
1206 = new NetEConstParam(found_in
, name
, ctmp
->value());
1212 tmp
->set_line(*this);
1217 * Handle word selects of vector arrays.
1219 NetExpr
* PEIdent::elaborate_expr_net_word_(Design
*des
, NetScope
*scope
,
1220 NetNet
*net
, NetScope
*found_in
,
1221 bool sys_task_arg
) const
1223 const name_component_t
&name_tail
= path_
.back();
1225 if (name_tail
.index
.empty() && !sys_task_arg
) {
1226 cerr
<< get_line() << ": error: Array " << path()
1227 << " Needs an array index here." << endl
;
1232 index_component_t index_front
;
1233 if (! name_tail
.index
.empty()) {
1234 index_front
= name_tail
.index
.front();
1235 ivl_assert(*this, index_front
.sel
!= index_component_t::SEL_NONE
);
1236 if (index_front
.sel
!= index_component_t::SEL_BIT
) {
1237 cerr
<< get_line() << ": error: Array " << path_
1238 << " cannot be indexed by a range." << endl
;
1242 ivl_assert(*this, index_front
.msb
);
1243 ivl_assert(*this, !index_front
.lsb
);
1246 NetExpr
*word_index
= index_front
.sel
== index_component_t::SEL_NONE
1248 : elab_and_eval(des
, scope
, index_front
.msb
, -1);
1249 if (word_index
== 0 && !sys_task_arg
)
1252 if (NetEConst
*word_addr
= dynamic_cast<NetEConst
*>(word_index
)) {
1253 long addr
= word_addr
->value().as_long();
1255 // Special case: The index is out of range, so the value
1256 // of this expression is a 'bx vector the width of a word.
1257 if (!net
->array_index_is_valid(addr
)) {
1258 verinum
xxx (verinum::Vx
, net
->vector_width());
1259 NetEConst
*resx
= new NetEConst(xxx
);
1260 resx
->set_line(*this);
1265 // Recalculate the constant address with the adjusted base.
1266 unsigned use_addr
= net
->array_index_to_address(addr
);
1267 if (addr
< 0 || use_addr
!= (unsigned long)addr
) {
1268 verinum
val (use_addr
, 8*sizeof(use_addr
));
1269 NetEConst
*tmp
= new NetEConst(val
);
1270 tmp
->set_line(*this);
1275 } else if (word_index
) {
1276 // If there is a non-zero base to the memory, then build an
1277 // expression to calculate the canonical address.
1278 if (long base
= net
->array_first()) {
1280 word_index
= make_add_expr(word_index
, 0-base
);
1281 if (NetExpr
*tmp
= word_index
->eval_tree()) {
1287 NetESignal
*res
= new NetESignal(net
, word_index
);
1288 res
->set_line(*this);
1290 // Detect that the word has a bit/part select as well.
1292 index_component_t::ctype_t word_sel
= index_component_t::SEL_NONE
;
1293 if (name_tail
.index
.size() > 1)
1294 word_sel
= name_tail
.index
.back().sel
;
1296 if (word_sel
== index_component_t::SEL_PART
)
1297 return elaborate_expr_net_part_(des
, scope
, res
, found_in
);
1299 if (word_sel
== index_component_t::SEL_IDX_UP
)
1300 return elaborate_expr_net_idx_up_(des
, scope
, res
, found_in
);
1302 if (word_sel
== index_component_t::SEL_IDX_DO
)
1303 return elaborate_expr_net_idx_do_(des
, scope
, res
, found_in
);
1305 if (word_sel
== index_component_t::SEL_BIT
)
1306 return elaborate_expr_net_bit_(des
, scope
, res
, found_in
);
1308 ivl_assert(*this, word_sel
== index_component_t::SEL_NONE
);
1313 * Handle part selects of NetNet identifiers.
1315 NetExpr
* PEIdent::elaborate_expr_net_part_(Design
*des
, NetScope
*scope
,
1316 NetESignal
*net
, NetScope
*found_in
) const
1319 bool flag
= calculate_parts_(des
, scope
, msv
, lsv
);
1323 /* The indices of part selects are signed integers, so allow
1324 negative values. However, the width that they represent is
1325 unsigned. Remember that any order is possible,
1326 i.e., [1:0], [-4:6], etc. */
1327 unsigned long wid
= 1 + ((msv
>lsv
)? (msv
-lsv
) : (lsv
-msv
));
1328 if (wid
> net
->vector_width()) {
1329 cerr
<< get_line() << ": error: part select ["
1330 << msv
<< ":" << lsv
<< "] out of range." << endl
;
1336 ivl_assert(*this, wid
<= net
->vector_width());
1338 if (net
->sig()->sb_to_idx(msv
) < net
->sig()->sb_to_idx(lsv
)) {
1339 cerr
<< get_line() << ": error: part select ["
1340 << msv
<< ":" << lsv
<< "] out of order." << endl
;
1348 if (net
->sig()->sb_to_idx(msv
) >= net
->vector_width()) {
1349 cerr
<< get_line() << ": error: part select ["
1350 << msv
<< ":" << lsv
<< "] out of range." << endl
;
1357 // If the part select convers exactly the entire
1358 // vector, then do not bother with it. Return the
1360 if (net
->sig()->sb_to_idx(lsv
) == 0 && wid
== net
->vector_width())
1363 NetExpr
*ex
= new NetEConst(verinum(net
->sig()->sb_to_idx(lsv
)));
1364 NetESelect
*ss
= new NetESelect(net
, ex
, wid
);
1365 ss
->set_line(*this);
1371 * Part select indexed up, i.e. net[<m> +: <l>]
1373 NetExpr
* PEIdent::elaborate_expr_net_idx_up_(Design
*des
, NetScope
*scope
,
1374 NetESignal
*net
, NetScope
*found_in
) const
1376 const name_component_t
&name_tail
= path_
.back();
1377 ivl_assert(*this, !name_tail
.index
.empty());
1379 const index_component_t
&index_tail
= name_tail
.index
.back();
1380 ivl_assert(*this, index_tail
.lsb
!= 0);
1381 ivl_assert(*this, index_tail
.msb
!= 0);
1383 NetExpr
*base
= elab_and_eval(des
, scope
, index_tail
.msb
, -1);
1385 unsigned long wid
= 0;
1386 calculate_up_do_width_(des
, scope
, wid
);
1389 // Handle the special case that the base is constant as
1390 // well. In this case it can be converted to a conventional
1392 if (NetEConst
*base_c
= dynamic_cast<NetEConst
*> (base
)) {
1393 long lsv
= base_c
->value().as_long();
1395 // If the part select convers exactly the entire
1396 // vector, then do not bother with it. Return the
1398 if (net
->sig()->sb_to_idx(lsv
) == 0 && wid
== net
->vector_width())
1401 // Otherwise, make a part select that covers the right range.
1402 NetExpr
*ex
= new NetEConst(verinum(net
->sig()->sb_to_idx(lsv
)));
1403 NetESelect
*ss
= new NetESelect(net
, ex
, wid
);
1404 ss
->set_line(*this);
1410 if (net
->msi() > net
->lsi()) {
1411 if (long offset
= net
->lsi())
1412 base
= make_add_expr(base
, 0-offset
);
1414 long vwid
= net
->lsi() - net
->msi() + 1;
1415 long offset
= net
->msi();
1416 base
= make_sub_expr(vwid
-offset
-wid
, base
);
1419 NetESelect
*ss
= new NetESelect(net
, base
, wid
);
1420 ss
->set_line(*this);
1422 if (debug_elaborate
) {
1423 cerr
<< get_line() << ": debug: Elaborate part "
1424 << "select base="<< *base
<< ", wid="<< wid
<< endl
;
1431 * Part select indexed down, i.e. net[<m> -: <l>]
1433 NetExpr
* PEIdent::elaborate_expr_net_idx_do_(Design
*des
, NetScope
*scope
,
1434 NetESignal
*net
, NetScope
*found_in
)const
1436 const name_component_t
&name_tail
= path_
.back();
1437 ivl_assert(*this, ! name_tail
.index
.empty());
1439 const index_component_t
&index_tail
= name_tail
.index
.back();
1440 ivl_assert(*this, index_tail
.lsb
!= 0);
1441 ivl_assert(*this, index_tail
.msb
!= 0);
1443 NetExpr
*base
= elab_and_eval(des
, scope
, index_tail
.msb
, -1);
1445 unsigned long wid
= 0;
1446 calculate_up_do_width_(des
, scope
, wid
);
1448 // Handle the special case that the base is constant as
1449 // well. In this case it can be converted to a conventional
1451 if (NetEConst
*base_c
= dynamic_cast<NetEConst
*> (base
)) {
1452 long lsv
= base_c
->value().as_long();
1454 // If the part select convers exactly the entire
1455 // vector, then do not bother with it. Return the
1457 if (net
->sig()->sb_to_idx(lsv
) == (wid
-1) && wid
== net
->vector_width())
1460 // Otherwise, make a part select that covers the right range.
1461 NetExpr
*ex
= new NetEConst(verinum(net
->sig()->sb_to_idx(lsv
)-wid
+1));
1462 NetESelect
*ss
= new NetESelect(net
, ex
, wid
);
1463 ss
->set_line(*this);
1469 long offset
= net
->lsi();
1470 NetExpr
*base_adjusted
= wid
> 1
1471 ? make_add_expr(base
,1-(long)wid
-offset
)
1472 : (offset
== 0? base
: make_add_expr(base
, 0-offset
));
1473 NetESelect
*ss
= new NetESelect(net
, base_adjusted
, wid
);
1474 ss
->set_line(*this);
1476 if (debug_elaborate
) {
1477 cerr
<< get_line() << ": debug: Elaborate part "
1478 << "select base="<< *base_adjusted
<< ", wid="<< wid
<< endl
;
1484 NetExpr
* PEIdent::elaborate_expr_net_bit_(Design
*des
, NetScope
*scope
,
1485 NetESignal
*net
, NetScope
*found_in
) const
1487 const name_component_t
&name_tail
= path_
.back();
1488 ivl_assert(*this, !name_tail
.index
.empty());
1490 const index_component_t
&index_tail
= name_tail
.index
.back();
1491 ivl_assert(*this, index_tail
.msb
!= 0);
1492 ivl_assert(*this, index_tail
.lsb
== 0);
1494 NetExpr
*ex
= elab_and_eval(des
, scope
, index_tail
.msb
, -1);
1496 // If the bit select is constant, then treat it similar
1497 // to the part select, so that I save the effort of
1498 // making a mux part in the netlist.
1499 if (NetEConst
*msc
= dynamic_cast<NetEConst
*> (ex
)) {
1500 long msv
= msc
->value().as_long();
1501 unsigned idx
= net
->sig()->sb_to_idx(msv
);
1503 if (idx
>= net
->vector_width()) {
1504 /* The bit select is out of range of the
1505 vector. This is legal, but returns a
1506 constant 1'bx value. */
1507 verinum
x (verinum::Vx
);
1508 NetEConst
*tmp
= new NetEConst(x
);
1509 tmp
->set_line(*this);
1511 cerr
<< get_line() << ": warning: Bit select ["
1512 << msv
<< "] out of range of vector "
1513 << net
->name() << "[" << net
->sig()->msb()
1514 << ":" << net
->sig()->lsb() << "]." << endl
;
1515 cerr
<< get_line() << ": : Replacing "
1516 << "expression with a constant 1'bx." << endl
;
1521 // If the vector is only one bit, we are done. The
1522 // bit select will return the scaler itself.
1523 if (net
->vector_width() == 1)
1526 // Make an expression out of the index
1527 NetEConst
*idx_c
= new NetEConst(verinum(idx
));
1528 idx_c
->set_line(*net
);
1530 // Make a bit select with the canonical index
1531 NetESelect
*res
= new NetESelect(net
, idx_c
, 1);
1532 res
->set_line(*net
);
1537 // Non-constant bit select? punt and make a subsignal
1538 // device to mux the bit in the net. This is a fairly
1539 // complicated task because we need to generate
1540 // expressions to convert calculated bit select
1541 // values to canonical values that are used internally.
1543 if (net
->sig()->msb() < net
->sig()->lsb()) {
1544 ex
= make_sub_expr(net
->sig()->lsb(), ex
);
1546 ex
= make_add_expr(ex
, - net
->sig()->lsb());
1549 NetESelect
*ss
= new NetESelect(net
, ex
, 1);
1550 ss
->set_line(*this);
1554 NetExpr
* PEIdent::elaborate_expr_net(Design
*des
, NetScope
*scope
,
1555 NetNet
*net
, NetScope
*found_in
,
1556 bool sys_task_arg
) const
1558 if (net
->array_dimensions() > 0)
1559 return elaborate_expr_net_word_(des
, scope
, net
, found_in
, sys_task_arg
);
1561 NetESignal
*node
= new NetESignal(net
);
1562 node
->set_line(*this);
1564 index_component_t::ctype_t use_sel
= index_component_t::SEL_NONE
;
1565 if (! path_
.back().index
.empty())
1566 use_sel
= path_
.back().index
.back().sel
;
1568 // If this is a part select of a signal, then make a new
1569 // temporary signal that is connected to just the
1570 // selected bits. The lsb_ and msb_ expressions are from
1571 // the foo[msb:lsb] expression in the original.
1572 if (use_sel
== index_component_t::SEL_PART
)
1573 return elaborate_expr_net_part_(des
, scope
, node
, found_in
);
1575 if (use_sel
== index_component_t::SEL_IDX_UP
)
1576 return elaborate_expr_net_idx_up_(des
, scope
, node
, found_in
);
1578 if (use_sel
== index_component_t::SEL_IDX_DO
)
1579 return elaborate_expr_net_idx_do_(des
, scope
, node
, found_in
);
1581 if (use_sel
== index_component_t::SEL_BIT
)
1582 return elaborate_expr_net_bit_(des
, scope
, node
, found_in
);
1584 // It's not anything else, so this must be a simple identifier
1585 // expression with no part or bit select. Return the signal
1586 // itself as the expression.
1587 assert(use_sel
== index_component_t::SEL_NONE
);
1592 unsigned PENumber::test_width(Design
*, NetScope
*,
1593 unsigned min
, unsigned lval
, bool&unsized_flag
) const
1595 unsigned use_wid
= value_
->len();
1599 if (! value_
->has_len())
1600 unsized_flag
= true;
1605 NetEConst
* PENumber::elaborate_expr(Design
*des
, NetScope
*,
1606 int expr_width
, bool) const
1609 verinum tvalue
= *value_
;
1611 // If the expr_width is >0, then the context is requesting a
1612 // specific size (for example this is part of the r-values of
1613 // an assignment) so we pad to the desired width and ignore
1614 // the self-determined size.
1615 if (expr_width
> 0) {
1616 tvalue
= pad_to_width(tvalue
, expr_width
);
1619 NetEConst
*tmp
= new NetEConst(tvalue
);
1620 tmp
->set_line(*this);
1624 unsigned PEString::test_width(Design
*des
, NetScope
*scope
,
1625 unsigned min
, unsigned lval
,
1626 bool&unsized_flag
) const
1628 unsigned use_wid
= text_
? 8*strlen(text_
) : 0;
1635 NetEConst
* PEString::elaborate_expr(Design
*des
, NetScope
*,
1636 int expr_width
, bool) const
1638 NetEConst
*tmp
= new NetEConst(value());
1639 tmp
->set_line(*this);
1643 unsigned PETernary::test_width(Design
*des
, NetScope
*scope
,
1644 unsigned min
, unsigned lval
,
1647 unsigned tru_wid
= tru_
->test_width(des
, scope
, min
, lval
, flag
);
1648 unsigned fal_wid
= fal_
->test_width(des
, scope
, min
, lval
, flag
);
1649 return max(tru_wid
,fal_wid
);
1652 static bool test_ternary_operand_compat(ivl_variable_type_t l
,
1653 ivl_variable_type_t r
)
1655 if (l
== IVL_VT_LOGIC
&& r
== IVL_VT_BOOL
)
1657 if (l
== IVL_VT_BOOL
&& r
== IVL_VT_LOGIC
)
1666 * Elaborate the Ternary operator. I know that the expressions were
1667 * parsed so I can presume that they exist, and call elaboration
1668 * methods. If any elaboration fails, then give up and return 0.
1670 NetETernary
*PETernary::elaborate_expr(Design
*des
, NetScope
*scope
,
1671 int expr_wid
, bool) const
1679 unsigned tru_wid
= tru_
->test_width(des
, scope
, 0, 0, flag
);
1680 unsigned fal_wid
= fal_
->test_width(des
, scope
, 0, 0, flag
);
1681 expr_wid
= max(tru_wid
, fal_wid
);
1682 if (debug_elaborate
)
1683 cerr
<< get_line() << ": debug: "
1684 << "Self-sized ternary chooses wid="<< expr_wid
1685 << " from " <<tru_wid
1686 << " and " << fal_wid
<< endl
;
1689 NetExpr
*con
= expr_
->elaborate_expr(des
, scope
, -1, false);
1693 NetExpr
*tru
= tru_
->elaborate_expr(des
, scope
, expr_wid
, false);
1699 NetExpr
*fal
= fal_
->elaborate_expr(des
, scope
, expr_wid
, false);
1706 if (! test_ternary_operand_compat(tru
->expr_type(), fal
->expr_type())) {
1707 cerr
<< get_line() << ": error: Data types "
1708 << tru
->expr_type() << " and "
1709 << fal
->expr_type() << " of ternary"
1710 << " do not match." << endl
;
1715 /* Whatever the width we choose for the ternary operator, we
1716 need to make sure the operands match. */
1717 tru
= pad_to_width(tru
, expr_wid
);
1718 fal
= pad_to_width(fal
, expr_wid
);
1720 NetETernary
*res
= new NetETernary(con
, tru
, fal
);
1721 res
->set_line(*this);
1725 NetExpr
* PEUnary::elaborate_expr(Design
*des
, NetScope
*scope
,
1726 int expr_wid
, bool) const
1728 NetExpr
*ip
= expr_
->elaborate_expr(des
, scope
, expr_wid
, false);
1729 if (ip
== 0) return 0;
1731 /* Should we evaluate expressions ahead of time,
1732 * just like in PEBinary::elaborate_expr() ?
1738 tmp
= new NetEUnary(op_
, ip
);
1739 tmp
->set_line(*this);
1743 if (NetEConst
*ipc
= dynamic_cast<NetEConst
*>(ip
)) {
1745 verinum val
= ipc
->value();
1747 val
= pad_to_width(val
, expr_wid
);
1749 /* When taking the - of a number, turn it into a
1750 signed expression and extend it one bit to
1751 accommodate a possible sign bit. */
1752 verinum
zero (verinum::V0
, val
.len()+1, val
.has_len());
1753 verinum nval
= zero
- val
;
1756 nval
= verinum(nval
, val
.len());
1757 nval
.has_sign(true);
1758 tmp
= new NetEConst(nval
);
1759 tmp
->set_line(*this);
1762 } else if (NetECReal
*ipc
= dynamic_cast<NetECReal
*>(ip
)) {
1764 /* When taking the - of a real, fold this into the
1766 verireal val
= - ipc
->value();
1767 tmp
= new NetECReal(val
);
1768 tmp
->set_line( *ip
);
1772 tmp
= new NetEUnary(op_
, ip
);
1773 tmp
->set_line(*this);
1781 case '!': // Logical NOT
1782 /* If the operand to unary ! is a constant, then I can
1783 evaluate this expression here and return a logical
1784 constant in its place. */
1785 if (NetEConst
*ipc
= dynamic_cast<NetEConst
*>(ip
)) {
1786 verinum val
= ipc
->value();
1789 for (unsigned idx
= 0 ; idx
< val
.len() ; idx
+= 1)
1809 verinum
vres (res
, 1, true);
1810 tmp
= new NetEConst(vres
);
1811 tmp
->set_line(*this);
1814 tmp
= new NetEUReduce(op_
, ip
);
1815 tmp
->set_line(*this);
1819 case '&': // Reduction AND
1820 case '|': // Reduction OR
1821 case '^': // Reduction XOR
1822 case 'A': // Reduction NAND (~&)
1823 case 'N': // Reduction NOR (~|)
1824 case 'X': // Reduction NXOR (~^)
1825 tmp
= new NetEUReduce(op_
, ip
);
1826 tmp
->set_line(*this);
1830 tmp
= new NetEUBits(op_
, ip
);
1831 tmp
->set_line(*this);