Logic gates can take part select inputs
[iverilog.git] / elab_expr.cc
blobb2e50bfc4e3415aaf167c010c3b53269fb738481
1 /*
2 * Copyright (c) 1999-2006 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)
8 * any later version.
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
19 #ifdef HAVE_CVS_IDENT
20 #ident "$Id: elab_expr.cc,v 1.126 2007/06/02 03:42:12 steve Exp $"
21 #endif
23 # include "config.h"
24 # include "compiler.h"
26 # include "pform.h"
27 # include "netlist.h"
28 # include "netmisc.h"
29 # include "util.h"
30 # include "ivl_assert.h"
33 * The default behavor for the test_width method is to just return the
34 * minimum width that is passed in.
36 unsigned PExpr::test_width(Design*des, NetScope*scope,
37 unsigned min, unsigned lval, bool&) const
39 if (debug_elaborate) {
40 cerr << get_line() << ": debug: test_width defaults to "
41 << min << ", ignoring unsized_flag" << endl;
43 return min;
46 NetExpr* PExpr::elaborate_expr(Design*des, NetScope*, int, bool) const
48 cerr << get_line() << ": internal error: I do not know how to elaborate"
49 << " expression. " << endl;
50 cerr << get_line() << ": : Expression is: " << *this
51 << endl;
52 des->errors += 1;
53 return 0;
56 unsigned PEBinary::test_width(Design*des, NetScope*scope,
57 unsigned min, unsigned lval, bool&unsized_flag) const
59 bool flag_left = false;
60 bool flag_right = false;
61 unsigned wid_left = left_->test_width(des,scope, min, lval, flag_left);
62 unsigned wid_right = right_->test_width(des,scope, min, lval, flag_right);
64 if (flag_left || flag_right)
65 unsized_flag = true;
67 switch (op_) {
68 case '+':
69 case '-':
70 if (unsized_flag) {
71 wid_left += 1;
72 wid_right += 1;
74 if (wid_left > min)
75 min = wid_left;
76 if (wid_right > min)
77 min = wid_right;
78 if (lval > 0 && min > lval)
79 min = lval;
80 break;
82 default:
83 if (wid_left > min)
84 min = wid_left;
85 if (wid_right > min)
86 min = wid_right;
87 break;
90 return min;
94 * Elaborate binary expressions. This involves elaborating the left
95 * and right sides, and creating one of a variety of different NetExpr
96 * types.
98 NetEBinary* PEBinary::elaborate_expr(Design*des, NetScope*scope,
99 int expr_wid, bool) const
101 assert(left_);
102 assert(right_);
104 NetExpr*lp = left_->elaborate_expr(des, scope, expr_wid, false);
105 NetExpr*rp = right_->elaborate_expr(des, scope, expr_wid, false);
106 if ((lp == 0) || (rp == 0)) {
107 delete lp;
108 delete rp;
109 return 0;
112 NetEBinary*tmp = elaborate_eval_expr_base_(des, lp, rp, expr_wid);
113 return tmp;
116 NetEBinary* PEBinary::elaborate_eval_expr_base_(Design*des,
117 NetExpr*lp,
118 NetExpr*rp,
119 int expr_wid) const
121 /* If either expression can be evaluated ahead of time, then
122 do so. This can prove helpful later. */
123 { NetExpr*tmp;
124 tmp = lp->eval_tree();
125 if (tmp) {
126 delete lp;
127 lp = tmp;
130 tmp = rp->eval_tree();
131 if (tmp) {
132 delete rp;
133 rp = tmp;
137 return elaborate_expr_base_(des, lp, rp, expr_wid);
141 * This is common elaboration of the operator. It presumes that the
142 * operands are elaborated as necessary, and all I need to do is make
143 * the correct NetEBinary object and connect the parameters.
145 NetEBinary* PEBinary::elaborate_expr_base_(Design*des,
146 NetExpr*lp, NetExpr*rp,
147 int expr_wid) const
149 bool flag;
151 NetEBinary*tmp;
153 switch (op_) {
154 default:
155 tmp = new NetEBinary(op_, lp, rp);
156 tmp->set_line(*this);
157 break;
159 case 'a':
160 case 'o':
161 tmp = new NetEBLogic(op_, lp, rp);
162 tmp->set_line(*this);
163 break;
165 case 'p':
166 tmp = new NetEBPow(op_, lp, rp);
167 tmp->set_line(*this);
168 break;
170 case '*':
171 tmp = new NetEBMult(op_, lp, rp);
172 tmp->set_line(*this);
173 break;
175 case '%':
176 /* The % operator does not support real arguments in
177 baseline Verilog. But we allow it in our extended
178 form of verilog. */
179 if (generation_flag < GN_VER2001X) {
180 if (lp->expr_type()==IVL_VT_REAL || rp->expr_type()==IVL_VT_REAL) {
181 cerr << get_line() << ": error: Modulus operator may not "
182 "have REAL operands." << endl;
183 des->errors += 1;
186 /* Fall through to handle the % with the / operator. */
187 case '/':
188 tmp = new NetEBDiv(op_, lp, rp);
189 tmp->set_line(*this);
190 break;
192 case 'l': // <<
193 case 'r': // >>
194 case 'R': // >>>
195 tmp = new NetEBShift(op_, lp, rp);
196 tmp->set_line(*this);
197 break;
199 case '^':
200 case '&':
201 case '|':
202 case 'O': // NOR (~|)
203 case 'A': // NAND (~&)
204 case 'X':
205 tmp = new NetEBBits(op_, lp, rp);
206 tmp->set_line(*this);
207 break;
209 case '+':
210 case '-':
211 tmp = new NetEBAdd(op_, lp, rp, expr_wid==-2? true : false);
212 if (expr_wid > 0 && (tmp->expr_type() == IVL_VT_BOOL
213 || tmp->expr_type() == IVL_VT_LOGIC))
214 tmp->set_width(expr_wid);
215 tmp->set_line(*this);
216 break;
218 case 'E': /* === */
219 case 'N': /* !== */
220 if (lp->expr_type() == IVL_VT_REAL
221 || rp->expr_type() == IVL_VT_REAL) {
222 cerr << get_line() << ": error: Case equality may not "
223 << "have real operands." << endl;
224 return 0;
226 /* Fall through... */
227 case 'e': /* == */
228 case 'n': /* != */
229 if (dynamic_cast<NetEConst*>(rp)
230 && (lp->expr_width() > rp->expr_width()))
231 rp->set_width(lp->expr_width());
233 if (dynamic_cast<NetEConst*>(lp)
234 && (lp->expr_width() < rp->expr_width()))
235 lp->set_width(rp->expr_width());
237 /* from here, handle this like other compares. */
238 case 'L': /* <= */
239 case 'G': /* >= */
240 case '<':
241 case '>':
242 tmp = new NetEBComp(op_, lp, rp);
243 tmp->set_line(*this);
244 flag = tmp->set_width(1);
245 if (flag == false) {
246 cerr << get_line() << ": internal error: "
247 "expression bit width of comparison != 1." << endl;
248 des->errors += 1;
250 break;
253 return tmp;
256 unsigned PEBComp::test_width(Design*, NetScope*,unsigned, unsigned, bool&) const
258 return 1;
261 NetEBinary* PEBComp::elaborate_expr(Design*des, NetScope*scope,
262 int expr_width, bool sys_task_arg) const
264 assert(left_);
265 assert(right_);
267 bool unsized_flag = false;
268 unsigned left_width = left_->test_width(des, scope, 0, 0, unsized_flag);
269 bool save_flag = unsized_flag;
270 unsigned right_width = right_->test_width(des, scope, 0, 0, unsized_flag);
272 if (save_flag != unsized_flag)
273 left_width = left_->test_width(des, scope, 0, 0, unsized_flag);
275 /* Width of operands is self-determined. */
276 int use_wid = left_width;
277 if (right_width > left_width)
278 use_wid = right_width;
280 if (debug_elaborate) {
281 cerr << get_line() << ": debug: "
282 << "Comparison expression operands are "
283 << left_width << " bits and "
284 << right_width << " bits. Resorting to "
285 << use_wid << " bits." << endl;
288 NetExpr*lp = left_->elaborate_expr(des, scope, use_wid, false);
289 NetExpr*rp = right_->elaborate_expr(des, scope, use_wid, false);
290 if ((lp == 0) || (rp == 0)) {
291 delete lp;
292 delete rp;
293 return 0;
296 return elaborate_eval_expr_base_(des, lp, rp, use_wid);
299 unsigned PEBShift::test_width(Design*des, NetScope*scope,
300 unsigned min, unsigned lval, bool&unsized_flag) const
302 unsigned wid_left = left_->test_width(des,scope,min, 0, unsized_flag);
304 // The right expression is self-determined and has no impact
305 // on the expression size that is generated.
307 return wid_left;
312 * Given a call to a system function, generate the proper expression
313 * nodes to represent the call in the netlist. Since we don't support
314 * size_tf functions, make assumptions about widths based on some
315 * known function names.
317 NetExpr* PECallFunction::elaborate_sfunc_(Design*des, NetScope*scope) const
320 /* Catch the special case that the system function is the
321 $signed function. This function is special, in that it does
322 not lead to executable code but takes the single parameter
323 and makes it into a signed expression. No bits are changed,
324 it just changes the interpretation. */
325 if (strcmp(peek_tail_name(path_), "$signed") == 0) {
326 if ((parms_.count() != 1) || (parms_[0] == 0)) {
327 cerr << get_line() << ": error: The $signed() function "
328 << "takes exactly one(1) argument." << endl;
329 des->errors += 1;
330 return 0;
333 PExpr*expr = parms_[0];
334 NetExpr*sub = expr->elaborate_expr(des, scope, -1, true);
335 sub->cast_signed(true);
336 return sub;
338 /* add $unsigned to match $signed */
339 if (strcmp(peek_tail_name(path_), "$unsigned") == 0) {
340 if ((parms_.count() != 1) || (parms_[0] == 0)) {
341 cerr << get_line() << ": error: The $unsigned() function "
342 << "takes exactly one(1) argument." << endl;
343 des->errors += 1;
344 return 0;
347 PExpr*expr = parms_[0];
348 NetExpr*sub = expr->elaborate_expr(des, scope, -1, true);
349 sub->cast_signed(false);
350 return sub;
353 /* Interpret the internal $sizeof system function to return
354 the bit width of the sub-expression. The value of the
355 sub-expression is not used, so the expression itself can be
356 deleted. */
357 if ((strcmp(peek_tail_name(path_), "$sizeof") == 0)
358 || (strcmp(peek_tail_name(path_), "$bits") == 0)) {
359 if ((parms_.count() != 1) || (parms_[0] == 0)) {
360 cerr << get_line() << ": error: The $bits() function "
361 << "takes exactly one(1) argument." << endl;
362 des->errors += 1;
363 return 0;
366 if (strcmp(peek_tail_name(path_), "$sizeof") == 0)
367 cerr << get_line() << ": warning: $sizeof is deprecated."
368 << " Use $bits() instead." << endl;
370 PExpr*expr = parms_[0];
371 ivl_assert(*this, expr);
373 /* Elaborate the sub-expression to get its
374 self-determined width, and save that width. Then
375 delete the expression because we don't really want
376 the expression itself. */
377 long sub_expr_width = 0;
378 if (NetExpr*tmp = expr->elaborate_expr(des, scope, -1, true)) {
379 sub_expr_width = tmp->expr_width();
380 delete tmp;
383 verinum val (sub_expr_width, 8*sizeof(unsigned));
384 NetEConst*sub = new NetEConst(val);
385 sub->set_line(*this);
387 return sub;
390 /* Interpret the internal $is_signed system function to return
391 a single bit flag -- 1 if the expression is signed, 0
392 otherwise. The subexpression is elaborated but not
393 evaluated. */
394 if (strcmp(peek_tail_name(path_), "$is_signed") == 0) {
395 if ((parms_.count() != 1) || (parms_[0] == 0)) {
396 cerr << get_line() << ": error: The $is_signed() function "
397 << "takes exactly one(1) argument." << endl;
398 des->errors += 1;
399 return 0;
402 PExpr*expr = parms_[0];
403 NetExpr*sub = expr->elaborate_expr(des, scope, -1, true);
405 verinum val (sub->has_sign()? verinum::V1 : verinum::V0, 1);
406 delete sub;
408 sub = new NetEConst(val);
409 sub->set_line(*this);
411 return sub;
414 /* Get the return type of the system function by looking it up
415 in the sfunc_table. */
416 const struct sfunc_return_type*sfunc_info
417 = lookup_sys_func(peek_tail_name(path_));
419 ivl_variable_type_t sfunc_type = sfunc_info->type;
420 unsigned wid = sfunc_info->wid;
423 /* How many parameters are there? The Verilog language allows
424 empty parameters in certain contexts, so the parser will
425 allow things like func(1,,3). It will also cause func() to
426 be interpreted as a single empty parameter.
428 Functions cannot really take empty parameters, but the
429 case ``func()'' is the same as no parameters at all. So
430 catch that special case here. */
431 unsigned nparms = parms_.count();
432 if ((nparms == 1) && (parms_[0] == 0))
433 nparms = 0;
435 NetESFunc*fun = new NetESFunc(peek_tail_name(path_), sfunc_type,
436 wid, nparms);
437 if (sfunc_info->signed_flag)
438 fun->cast_signed(true);
440 /* Now run through the expected parameters. If we find that
441 there are missing parameters, print an error message.
443 While we're at it, try to evaluate the function parameter
444 expression as much as possible, and use the reduced
445 expression if one is created. */
447 unsigned missing_parms = 0;
448 for (unsigned idx = 0 ; idx < nparms ; idx += 1) {
449 PExpr*expr = parms_[idx];
450 if (expr) {
451 NetExpr*tmp1 = expr->elaborate_expr(des, scope, -1, true);
452 if (NetExpr*tmp2 = tmp1->eval_tree()) {
453 delete tmp1;
454 fun->parm(idx, tmp2);
455 } else {
456 fun->parm(idx, tmp1);
459 } else {
460 missing_parms += 1;
461 fun->parm(idx, 0);
465 if (missing_parms > 0) {
466 cerr << get_line() << ": error: The function "
467 << peek_tail_name(path_)
468 << " has been called with empty parameters." << endl;
469 cerr << get_line() << ": : Verilog doesn't allow "
470 << "passing empty parameters to functions." << endl;
471 des->errors += 1;
474 return fun;
477 NetExpr* PECallFunction::elaborate_expr(Design*des, NetScope*scope,
478 int expr_wid, bool) const
480 if (peek_tail_name(path_)[0] == '$')
481 return elaborate_sfunc_(des, scope);
483 NetFuncDef*def = des->find_function(scope, path_);
484 if (def == 0) {
485 cerr << get_line() << ": error: No function " << path_ <<
486 " in this context (" << scope_path(scope) << ")." << endl;
487 des->errors += 1;
488 return 0;
490 assert(def);
492 NetScope*dscope = def->scope();
493 assert(dscope);
495 if (! check_call_matches_definition_(des, dscope))
496 return 0;
498 unsigned parms_count = parms_.count();
499 if ((parms_count == 1) && (parms_[0] == 0))
500 parms_count = 0;
504 svector<NetExpr*> parms (parms_count);
506 /* Elaborate the input expressions for the function. This is
507 done in the scope of the function call, and not the scope
508 of the function being called. The scope of the called
509 function is elaborated when the definition is elaborated. */
511 unsigned missing_parms = 0;
512 for (unsigned idx = 0 ; idx < parms.count() ; idx += 1) {
513 PExpr*tmp = parms_[idx];
514 if (tmp) {
515 parms[idx] = elab_and_eval(des, scope, tmp, -1);
517 } else {
518 missing_parms += 1;
519 parms[idx] = 0;
523 if (missing_parms > 0) {
524 cerr << get_line() << ": error: The function " << path_
525 << " has been called with empty parameters." << endl;
526 cerr << get_line() << ": : Verilog doesn't allow "
527 << "passing empty parameters to functions." << endl;
528 des->errors += 1;
532 /* Look for the return value signal for the called
533 function. This return value is a magic signal in the scope
534 of the function, that has the name of the function. The
535 function code assigns to this signal to return a value.
537 dscope, in this case, is the scope of the function, so the
538 return value is the name within that scope. */
540 if (NetNet*res = dscope->find_signal(dscope->basename())) {
541 NetESignal*eres = new NetESignal(res);
542 NetEUFunc*func = new NetEUFunc(dscope, eres, parms);
543 func->set_line(*this);
544 func->cast_signed(res->get_signed());
545 return func;
548 cerr << get_line() << ": internal error: Unable to locate "
549 "function return value for " << path_
550 << " in " << dscope->basename() << "." << endl;
551 des->errors += 1;
552 return 0;
556 NetExpr* PEConcat::elaborate_expr(Design*des, NetScope*scope,
557 int expr_wid, bool) const
559 NetExpr* repeat = 0;
561 if (debug_elaborate) {
562 cerr << get_line() << ": debug: Elaborate expr=" << *this
563 << ", expr_wid=" << expr_wid << endl;
566 /* If there is a repeat expression, then evaluate the constant
567 value and set the repeat count. */
568 if (repeat_) {
569 NetExpr*tmp = elab_and_eval(des, scope, repeat_, -1);
570 assert(tmp);
571 NetEConst*rep = dynamic_cast<NetEConst*>(tmp);
573 if (rep == 0) {
574 cerr << get_line() << ": error: "
575 "concatenation repeat expression cannot be evaluated."
576 << endl;
577 cerr << get_line() << ": : The expression is: "
578 << *tmp << endl;
579 des->errors += 1;
582 repeat = rep;
585 /* Make the empty concat expression. */
586 NetEConcat*tmp = new NetEConcat(parms_.count(), repeat);
587 tmp->set_line(*this);
589 unsigned wid_sum = 0;
591 /* Elaborate all the parameters and attach them to the concat node. */
592 for (unsigned idx = 0 ; idx < parms_.count() ; idx += 1) {
593 if (parms_[idx] == 0) {
594 cerr << get_line() << ": error: Missing expression "
595 << (idx+1) << " of concatenation list." << endl;
596 des->errors += 1;
597 continue;
600 assert(parms_[idx]);
601 NetExpr*ex = elab_and_eval(des, scope, parms_[idx], 0, 0);
602 if (ex == 0) continue;
604 ex->set_line(*parms_[idx]);
606 if (! ex->has_width()) {
607 cerr << ex->get_line() << ": error: operand of "
608 << "concatenation has indefinite width: "
609 << *ex << endl;
610 des->errors += 1;
613 wid_sum += ex->expr_width();
614 tmp->set(idx, ex);
617 tmp->set_width(wid_sum * tmp->repeat());
619 return tmp;
622 NetExpr* PEFNumber::elaborate_expr(Design*des, NetScope*scope, int, bool) const
624 NetECReal*tmp = new NetECReal(*value_);
625 tmp->set_line(*this);
626 tmp->set_width(1U, false);
627 return tmp;
631 * Given that the msb_ and lsb_ are part select expressions, this
632 * function calculates their values. Note that this method does *not*
633 * convert the values to canonical form.
635 bool PEIdent::calculate_parts_(Design*des, NetScope*scope,
636 long&msb, long&lsb) const
638 const name_component_t&name_tail = path_.back();
639 ivl_assert(*this, !name_tail.index.empty());
641 const index_component_t&index_tail = name_tail.index.back();
642 ivl_assert(*this, index_tail.sel == index_component_t::SEL_PART);
643 ivl_assert(*this, index_tail.msb && index_tail.lsb);
645 /* This handles part selects. In this case, there are
646 two bit select expressions, and both must be
647 constant. Evaluate them and pass the results back to
648 the caller. */
649 NetExpr*lsb_ex = elab_and_eval(des, scope, index_tail.lsb, -1);
650 NetEConst*lsb_c = dynamic_cast<NetEConst*>(lsb_ex);
651 if (lsb_c == 0) {
652 cerr << index_tail.lsb->get_line() << ": error: "
653 "Part select expressions must be constant."
654 << endl;
655 cerr << index_tail.lsb->get_line() << ": : "
656 "This lsb expression violates the rule: "
657 << *index_tail.lsb << endl;
658 des->errors += 1;
659 return false;
662 NetExpr*msb_ex = elab_and_eval(des, scope, index_tail.msb, -1);
663 NetEConst*msb_c = dynamic_cast<NetEConst*>(msb_ex);
664 if (msb_c == 0) {
665 cerr << index_tail.msb->get_line() << ": error: "
666 "Part select expressions must be constant."
667 << endl;
668 cerr << index_tail.msb->get_line() << ": : This msb expression "
669 "violates the rule: " << *index_tail.msb << endl;
670 des->errors += 1;
671 return false;
674 msb = msb_c->value().as_long();
675 lsb = lsb_c->value().as_long();
677 delete msb_ex;
678 delete lsb_ex;
679 return true;
682 bool PEIdent::calculate_up_do_width_(Design*des, NetScope*scope,
683 unsigned long&wid) const
685 const name_component_t&name_tail = path_.back();
686 ivl_assert(*this, !name_tail.index.empty());
688 const index_component_t&index_tail = name_tail.index.back();
689 ivl_assert(*this, index_tail.lsb && index_tail.msb);
691 bool flag = true;
693 /* Calculate the width expression (in the lsb_ position)
694 first. If the expression is not constant, error but guess 1
695 so we can keep going and find more errors. */
696 NetExpr*wid_ex = elab_and_eval(des, scope, index_tail.lsb, -1);
697 NetEConst*wid_c = dynamic_cast<NetEConst*>(wid_ex);
699 if (wid_c == 0) {
700 cerr << get_line() << ": error: Indexed part width must be "
701 << "constant. Expression in question is..." << endl;
702 cerr << get_line() << ": : " << *wid_ex << endl;
703 des->errors += 1;
704 flag = false;
707 wid = wid_c? wid_c->value().as_ulong() : 1;
708 delete wid_ex;
710 return flag;
713 unsigned PEIdent::test_width(Design*des, NetScope*scope,
714 unsigned min, unsigned lval,
715 bool&unsized_flag) const
717 NetNet* net = 0;
718 const NetExpr*par = 0;
719 NetEvent* eve = 0;
721 const NetExpr*ex1, *ex2;
723 symbol_search(des, scope, path_, net, par, eve, ex1, ex2);
725 if (net != 0) {
726 const name_component_t&name_tail = path_.back();
727 index_component_t::ctype_t use_sel = index_component_t::SEL_NONE;
728 if (!name_tail.index.empty())
729 use_sel = name_tail.index.back().sel;
731 unsigned use_width = net->vector_width();
732 switch (use_sel) {
733 case index_component_t::SEL_NONE:
734 break;
735 case index_component_t::SEL_PART:
736 { long msb, lsb;
737 calculate_parts_(des, scope, msb, lsb);
738 use_width = 1 + ((msb>lsb)? (msb-lsb) : (lsb-msb));
739 break;
741 case index_component_t::SEL_IDX_UP:
742 case index_component_t::SEL_IDX_DO:
743 { unsigned long tmp = 0;
744 calculate_up_do_width_(des, scope, tmp);
745 use_width = tmp;
746 break;
748 case index_component_t::SEL_BIT:
749 use_width = 1;
750 break;
751 default:
752 ivl_assert(*this, 0);
754 return use_width;
757 return min;
761 * Elaborate an identifier in an expression. The identifier can be a
762 * parameter name, a signal name or a memory name. It can also be a
763 * scope name (Return a NetEScope) but only certain callers can use
764 * scope names. However, we still support it here.
766 * Function names are not handled here, they are detected by the
767 * parser and are elaborated by PECallFunction.
769 * The signal name may be escaped, but that affects nothing here.
771 NetExpr* PEIdent::elaborate_expr(Design*des, NetScope*scope,
772 int expr_wid, bool sys_task_arg) const
774 assert(scope);
776 NetNet* net = 0;
777 const NetExpr*par = 0;
778 NetEvent* eve = 0;
780 const NetExpr*ex1, *ex2;
782 NetScope*found_in = symbol_search(des, scope, path_,
783 net, par, eve,
784 ex1, ex2);
786 // If the identifier name is a parameter name, then return
787 // a reference to the parameter expression.
788 if (par != 0)
789 return elaborate_expr_param(des, scope, par, found_in, ex1, ex2);
792 // If the identifier names a signal (a register or wire)
793 // then create a NetESignal node to handle it.
794 if (net != 0)
795 return elaborate_expr_net(des, scope, net, found_in, sys_task_arg);
797 // If the identifier is a named event.
798 // is a variable reference.
799 if (eve != 0) {
800 NetEEvent*tmp = new NetEEvent(eve);
801 tmp->set_line(*this);
802 return tmp;
805 // Hmm... maybe this is a genvar? This is only possible while
806 // processing generate blocks, but then the genvar_tmp will be
807 // set in the scope.
808 if (path_.size() == 1
809 && scope->genvar_tmp.str()
810 && strcmp(peek_tail_name(path_), scope->genvar_tmp) == 0) {
811 if (debug_elaborate)
812 cerr << get_line() << ": debug: " << path_
813 << " is genvar with value " << scope->genvar_tmp_val
814 << "." << endl;
815 verinum val (scope->genvar_tmp_val);
816 NetEConst*tmp = new NetEConst(val);
817 tmp->set_line(*this);
818 return tmp;
821 // A specparam? Look up the name to see if it is a
822 // specparam. If we find it, then turn it into a NetEConst
823 // value and return that. Of course, this does not apply if
824 // specify blocks are disabled.
826 if (gn_specify_blocks_flag) {
827 map<perm_string,NetScope::spec_val_t>::const_iterator specp;
828 perm_string key = peek_tail_name(path_);
829 if (path_.size() == 1
830 && ((specp = scope->specparams.find(key)) != scope->specparams.end())) {
831 NetScope::spec_val_t value = (*specp).second;
832 NetExpr*tmp = 0;
833 switch (value.type) {
834 case IVL_VT_BOOL:
835 tmp = new NetEConst(verinum(value.integer));
836 break;
837 case IVL_VT_REAL:
838 tmp = new NetECReal(verireal(value.real_val));
839 break;
840 default:
841 break;
843 assert(tmp);
844 tmp->set_line(*this);
846 if (debug_elaborate)
847 cerr << get_line() << ": debug: " << path_
848 << " is a specparam" << endl;
849 return tmp;
853 // At this point we've exhausted all the possibilities that
854 // are not scopes. If this is not a system task argument, then
855 // it cannot be a scope name, so give up.
857 if (! sys_task_arg) {
858 // I cannot interpret this identifier. Error message.
859 cerr << get_line() << ": error: Unable to bind wire/reg/memory "
860 "`" << path_ << "' in `" << scope_path(scope) << "'" << endl;
861 des->errors += 1;
862 return 0;
865 // Finally, if this is a scope name, then return that. Look
866 // first to see if this is a name of a local scope. Failing
867 // that, search globally for a hierarchical name.
868 if ((path_.size() == 1)) {
869 hname_t use_name ( peek_tail_name(path_) );
870 if (NetScope*nsc = scope->child(use_name)) {
871 NetEScope*tmp = new NetEScope(nsc);
872 tmp->set_line(*this);
874 if (debug_elaborate)
875 cerr << get_line() << ": debug: Found scope "
876 << use_name << " in scope " << scope->basename()
877 << endl;
879 return tmp;
883 list<hname_t> spath = eval_scope_path(des, scope, path_);
885 ivl_assert(*this, spath.size() == path_.size());
887 // Try full hierarchical scope name.
888 if (NetScope*nsc = des->find_scope(spath)) {
889 NetEScope*tmp = new NetEScope(nsc);
890 tmp->set_line(*this);
892 if (debug_elaborate)
893 cerr << get_line() << ": debug: Found scope "
894 << nsc->basename()
895 << " path=" << path_ << endl;
897 if (! sys_task_arg) {
898 cerr << get_line() << ": error: Scope name "
899 << nsc->basename() << " not allowed here." << endl;
900 des->errors += 1;
903 return tmp;
906 // Try relative scope name.
907 if (NetScope*nsc = des->find_scope(scope, spath)) {
908 NetEScope*tmp = new NetEScope(nsc);
909 tmp->set_line(*this);
911 if (debug_elaborate)
912 cerr << get_line() << ": debug: Found scope "
913 << nsc->basename() << " in " << scope_path(scope) << endl;
915 return tmp;
918 // I cannot interpret this identifier. Error message.
919 cerr << get_line() << ": error: Unable to bind wire/reg/memory "
920 "`" << path_ << "' in `" << scope_path(scope) << "'" << endl;
921 des->errors += 1;
922 return 0;
926 * Handle the case that the identifier is a parameter reference. The
927 * parameter expression has already been located for us (as the par
928 * argument) so we just need to process the sub-expression.
930 NetExpr* PEIdent::elaborate_expr_param(Design*des,
931 NetScope*scope,
932 const NetExpr*par,
933 NetScope*found_in,
934 const NetExpr*par_msb,
935 const NetExpr*par_lsb) const
937 NetExpr*tmp = par->dup_expr();
939 const name_component_t&name_tail = path_.back();
940 index_component_t::ctype_t use_sel = index_component_t::SEL_NONE;
941 if (!name_tail.index.empty())
942 use_sel = name_tail.index.back().sel;
944 if (use_sel == index_component_t::SEL_PART) {
945 ivl_assert(*this, !name_tail.index.empty());
946 const index_component_t&index_tail = name_tail.index.back();
947 ivl_assert(*this, index_tail.msb);
948 ivl_assert(*this, index_tail.lsb);
950 /* If the identifier has a part select, we support
951 it by pulling the right bits out and making a
952 sized unsigned constant. This code assumes the
953 lsb of a parameter is 0 and the msb is the
954 width of the parameter. */
956 verinum*lsn = index_tail.lsb->eval_const(des, scope);
957 verinum*msn = index_tail.msb->eval_const(des, scope);
958 if ((lsn == 0) || (msn == 0)) {
959 cerr << get_line() << ": error: "
960 "Part select expressions must be "
961 "constant expressions." << endl;
962 des->errors += 1;
963 return 0;
966 long lsb = lsn->as_long();
967 long msb = msn->as_long();
968 if ((lsb < 0) || (msb < lsb)) {
969 cerr << get_line() << ": error: invalid part "
970 << "select: " << path_
971 << "["<<msb<<":"<<lsb<<"]" << endl;
972 des->errors += 1;
973 return 0;
975 unsigned long ulsb=lsb;
976 unsigned long umsb=msb;
978 NetEConst*le = dynamic_cast<NetEConst*>(tmp);
979 assert(le);
981 verinum result (verinum::V0, msb-lsb+1, true);
982 verinum exl = le->value();
984 /* Pull the bits from the parameter, one at a
985 time. If the bit is within the range, simply
986 copy it to the result. If the bit is outside
987 the range, we sign extend signed unsized
988 numbers, zero extend unsigned unsigned numbers,
989 and X extend sized numbers. */
990 for (unsigned long idx = ulsb ; idx <= umsb ; idx += 1) {
991 if (idx < exl.len())
992 result.set(idx-lsb, exl.get(idx));
993 else if (exl.is_string())
994 result.set(idx-lsb, verinum::V0);
995 else if (exl.has_len())
996 result.set(idx-lsb, verinum::Vx);
997 else if (exl.has_sign())
998 result.set(idx-lsb, exl.get(exl.len()-1));
999 else
1000 result.set(idx-lsb, verinum::V0);
1003 /* If the input is a string, and the part select
1004 is working on byte boundaries, then the result
1005 can be made into a string. */
1006 if (exl.is_string()
1007 && (lsb%8 == 0)
1008 && (result.len()%8 == 0))
1009 result = verinum(result.as_string());
1011 delete tmp;
1012 tmp = new NetEConst(result);
1014 } else if (use_sel == index_component_t::SEL_IDX_UP || use_sel == index_component_t::SEL_IDX_DO) {
1016 ivl_assert(*this, !name_tail.index.empty());
1017 const index_component_t&index_tail = name_tail.index.back();
1018 ivl_assert(*this, index_tail.msb);
1019 ivl_assert(*this, index_tail.lsb);
1021 /* Get and evaluate the width of the index
1022 select. This must be constant. */
1023 NetExpr*wid_ex = elab_and_eval(des, scope, index_tail.lsb, -1);
1024 NetEConst*wid_ec = dynamic_cast<NetEConst*> (wid_ex);
1025 if (wid_ec == 0) {
1026 cerr << index_tail.lsb->get_line() << ": error: "
1027 << "Second expression of indexed part select "
1028 << "most be constant." << endl;
1029 des->errors += 1;
1030 return 0;
1033 unsigned wid = wid_ec->value().as_ulong();
1035 NetExpr*idx_ex = elab_and_eval(des, scope, index_tail.msb, -1);
1036 if (idx_ex == 0) {
1037 return 0;
1040 if (use_sel == index_component_t::SEL_IDX_DO && wid > 1) {
1041 idx_ex = make_add_expr(idx_ex, 1-(long)wid);
1045 /* Wrap the param expression with a part select. */
1046 tmp = new NetESelect(tmp, idx_ex, wid);
1049 } else if (use_sel == index_component_t::SEL_BIT) {
1050 ivl_assert(*this, !name_tail.index.empty());
1051 const index_component_t&index_tail = name_tail.index.back();
1052 ivl_assert(*this, index_tail.msb);
1053 ivl_assert(*this, !index_tail.lsb);
1055 /* Handle the case where a parameter has a bit
1056 select attached to it. Generate a NetESelect
1057 object to select the bit as desired. */
1058 NetExpr*mtmp = index_tail.msb->elaborate_expr(des, scope, -1,false);
1059 if (! dynamic_cast<NetEConst*>(mtmp)) {
1060 NetExpr*re = mtmp->eval_tree();
1061 if (re) {
1062 delete mtmp;
1063 mtmp = re;
1067 /* Let's first try to get constant values for both
1068 the parameter and the bit select. If they are
1069 both constant, then evaluate the bit select and
1070 return instead a single-bit constant. */
1072 NetEConst*le = dynamic_cast<NetEConst*>(tmp);
1073 NetEConst*re = dynamic_cast<NetEConst*>(mtmp);
1074 if (le && re) {
1076 /* Argument and bit select are constant. Calculate
1077 the final result. */
1078 verinum lv = le->value();
1079 verinum rv = re->value();
1080 verinum::V rb = verinum::Vx;
1082 long ridx = rv.as_long();
1083 if ((ridx >= 0) && ((unsigned long) ridx < lv.len())) {
1084 rb = lv[ridx];
1086 } else if ((ridx >= 0) && (!lv.has_len())) {
1087 if (lv.has_sign())
1088 rb = lv[lv.len()-1];
1089 else
1090 rb = verinum::V0;
1093 NetEConst*re = new NetEConst(verinum(rb, 1));
1094 delete tmp;
1095 delete mtmp;
1096 tmp = re;
1098 } else {
1100 const NetEConst*par_me =dynamic_cast<const NetEConst*>(par_msb);
1101 const NetEConst*par_le =dynamic_cast<const NetEConst*>(par_lsb);
1103 assert(par_me || !par_msb);
1104 assert(par_le || !par_lsb);
1105 assert(par_me || !par_le);
1107 if (par_me) {
1108 long par_mv = par_me->value().as_long();
1109 long par_lv = par_le->value().as_long();
1110 if (par_mv >= par_lv) {
1111 mtmp = par_lv
1112 ? make_add_expr(mtmp, 0-par_lv)
1113 : mtmp;
1114 } else {
1115 if (par_lv != 0)
1116 mtmp = make_add_expr(mtmp, 0-par_mv);
1117 mtmp = make_sub_expr(par_lv-par_mv, mtmp);
1121 /* The value is constant, but the bit select
1122 expression is not. Elaborate a NetESelect to
1123 evaluate the select at run-time. */
1125 NetESelect*stmp = new NetESelect(tmp, mtmp, 1);
1126 tmp->set_line(*this);
1127 tmp = stmp;
1130 } else {
1131 /* No bit or part select. Make the constant into a
1132 NetEConstParam if possible. */
1133 NetEConst*ctmp = dynamic_cast<NetEConst*>(tmp);
1134 if (ctmp != 0) {
1135 perm_string name = peek_tail_name(path_);
1136 NetEConstParam*ptmp
1137 = new NetEConstParam(found_in, name, ctmp->value());
1138 delete tmp;
1139 tmp = ptmp;
1143 tmp->set_line(*this);
1144 return tmp;
1148 * Handle word selects of vector arrays.
1150 NetExpr* PEIdent::elaborate_expr_net_word_(Design*des, NetScope*scope,
1151 NetNet*net, NetScope*found_in,
1152 bool sys_task_arg) const
1154 const name_component_t&name_tail = path_.back();
1156 if (name_tail.index.empty() && !sys_task_arg) {
1157 cerr << get_line() << ": error: Array " << path()
1158 << " Needs an array index here." << endl;
1159 des->errors += 1;
1160 return 0;
1163 index_component_t index_front;
1164 if (! name_tail.index.empty()) {
1165 index_front = name_tail.index.front();
1166 ivl_assert(*this, index_front.sel != index_component_t::SEL_NONE);
1167 if (index_front.sel != index_component_t::SEL_BIT) {
1168 cerr << get_line() << ": error: Array " << path_
1169 << " cannot be indexed by a range." << endl;
1170 des->errors += 1;
1171 return 0;
1173 ivl_assert(*this, index_front.msb);
1174 ivl_assert(*this, !index_front.lsb);
1177 NetExpr*word_index = index_front.sel == index_component_t::SEL_NONE
1179 : elab_and_eval(des, scope, index_front.msb, -1);
1180 if (word_index == 0 && !sys_task_arg)
1181 return 0;
1183 if (NetEConst*word_addr = dynamic_cast<NetEConst*>(word_index)) {
1184 long addr = word_addr->value().as_long();
1186 // Special case: The index is out of range, so the value
1187 // of this expression is a 'bx vector the width of a word.
1188 if (!net->array_index_is_valid(addr)) {
1189 verinum xxx (verinum::Vx, net->vector_width());
1190 NetEConst*resx = new NetEConst(xxx);
1191 resx->set_line(*this);
1192 delete word_index;
1193 return resx;
1196 // Recalculate the constant address with the adjusted base.
1197 unsigned use_addr = net->array_index_to_address(addr);
1198 if (use_addr != addr) {
1199 verinum val (use_addr, 8*sizeof(use_addr));
1200 NetEConst*tmp = new NetEConst(val);
1201 tmp->set_line(*this);
1202 delete word_index;
1203 word_index = tmp;
1206 } else if (word_index) {
1207 // If there is a non-zero base to the memory, then build an
1208 // expression to calculate the canonical address.
1209 if (long base = net->array_first()) {
1211 word_index = make_add_expr(word_index, 0-base);
1212 if (NetExpr*tmp = word_index->eval_tree()) {
1213 word_index = tmp;
1218 NetESignal*res = new NetESignal(net, word_index);
1219 res->set_line(*this);
1221 // Detect that the word has a bit/part select as well.
1223 index_component_t::ctype_t word_sel = index_component_t::SEL_NONE;
1224 if (name_tail.index.size() > 1)
1225 word_sel = name_tail.index.back().sel;
1227 if (word_sel == index_component_t::SEL_PART)
1228 return elaborate_expr_net_part_(des, scope, res, found_in);
1230 if (word_sel == index_component_t::SEL_IDX_UP)
1231 return elaborate_expr_net_idx_up_(des, scope, res, found_in);
1233 if (word_sel == index_component_t::SEL_IDX_DO)
1234 return elaborate_expr_net_idx_do_(des, scope, res, found_in);
1236 if (word_sel == index_component_t::SEL_BIT)
1237 return elaborate_expr_net_bit_(des, scope, res, found_in);
1239 ivl_assert(*this, word_sel == index_component_t::SEL_NONE);
1240 return res;
1244 * Handle part selects of NetNet identifiers.
1246 NetExpr* PEIdent::elaborate_expr_net_part_(Design*des, NetScope*scope,
1247 NetESignal*net, NetScope*found_in) const
1249 long msv, lsv;
1250 bool flag = calculate_parts_(des, scope, msv, lsv);
1251 if (!flag)
1252 return 0;
1254 /* The indices of part selects are signed integers, so allow
1255 negative values. However, the width that they represent is
1256 unsigned. Remember that any order is possible,
1257 i.e., [1:0], [-4:6], etc. */
1258 unsigned long wid = 1 + ((msv>lsv)? (msv-lsv) : (lsv-msv));
1259 if (wid > net->vector_width()) {
1260 cerr << get_line() << ": error: part select ["
1261 << msv << ":" << lsv << "] out of range." << endl;
1262 des->errors += 1;
1263 //delete lsn;
1264 //delete msn;
1265 return net;
1267 ivl_assert(*this, wid <= net->vector_width());
1269 if (net->sig()->sb_to_idx(msv) < net->sig()->sb_to_idx(lsv)) {
1270 cerr << get_line() << ": error: part select ["
1271 << msv << ":" << lsv << "] out of order." << endl;
1272 des->errors += 1;
1273 //delete lsn;
1274 //delete msn;
1275 return net;
1279 if (net->sig()->sb_to_idx(msv) >= net->vector_width()) {
1280 cerr << get_line() << ": error: part select ["
1281 << msv << ":" << lsv << "] out of range." << endl;
1282 des->errors += 1;
1283 //delete lsn;
1284 //delete msn;
1285 return net;
1288 // If the part select convers exactly the entire
1289 // vector, then do not bother with it. Return the
1290 // signal itself.
1291 if (net->sig()->sb_to_idx(lsv) == 0 && wid == net->vector_width())
1292 return net;
1294 NetExpr*ex = new NetEConst(verinum(net->sig()->sb_to_idx(lsv)));
1295 NetESelect*ss = new NetESelect(net, ex, wid);
1296 ss->set_line(*this);
1298 return ss;
1302 * Part select indexed up, i.e. net[<m> +: <l>]
1304 NetExpr* PEIdent::elaborate_expr_net_idx_up_(Design*des, NetScope*scope,
1305 NetESignal*net, NetScope*found_in) const
1307 const name_component_t&name_tail = path_.back();
1308 ivl_assert(*this, !name_tail.index.empty());
1310 const index_component_t&index_tail = name_tail.index.back();
1311 ivl_assert(*this, index_tail.lsb != 0);
1312 ivl_assert(*this, index_tail.msb != 0);
1314 NetExpr*base = elab_and_eval(des, scope, index_tail.msb, -1);
1316 unsigned long wid = 0;
1317 calculate_up_do_width_(des, scope, wid);
1320 // Handle the special case that the base is constant as
1321 // well. In this case it can be converted to a conventional
1322 // part select.
1323 if (NetEConst*base_c = dynamic_cast<NetEConst*> (base)) {
1324 long lsv = base_c->value().as_long();
1326 // If the part select convers exactly the entire
1327 // vector, then do not bother with it. Return the
1328 // signal itself.
1329 if (net->sig()->sb_to_idx(lsv) == 0 && wid == net->vector_width())
1330 return net;
1332 // Otherwise, make a part select that covers the right range.
1333 NetExpr*ex = new NetEConst(verinum(net->sig()->sb_to_idx(lsv)));
1334 NetESelect*ss = new NetESelect(net, ex, wid);
1335 ss->set_line(*this);
1337 delete base;
1338 return ss;
1341 if (net->msi() > net->lsi()) {
1342 if (long offset = net->lsi())
1343 base = make_add_expr(base, 0-offset);
1344 } else {
1345 long vwid = net->lsi() - net->msi() + 1;
1346 long offset = net->msi();
1347 base = make_sub_expr(vwid-offset-wid, base);
1350 NetESelect*ss = new NetESelect(net, base, wid);
1351 ss->set_line(*this);
1353 if (debug_elaborate) {
1354 cerr << get_line() << ": debug: Elaborate part "
1355 << "select base="<< *base << ", wid="<< wid << endl;
1358 return ss;
1362 * Part select indexed down, i.e. net[<m> -: <l>]
1364 NetExpr* PEIdent::elaborate_expr_net_idx_do_(Design*des, NetScope*scope,
1365 NetESignal*net, NetScope*found_in)const
1367 const name_component_t&name_tail = path_.back();
1368 ivl_assert(*this, ! name_tail.index.empty());
1370 const index_component_t&index_tail = name_tail.index.back();
1371 ivl_assert(*this, index_tail.lsb != 0);
1372 ivl_assert(*this, index_tail.msb != 0);
1374 NetExpr*base = elab_and_eval(des, scope, index_tail.msb, -1);
1376 unsigned long wid = 0;
1377 calculate_up_do_width_(des, scope, wid);
1379 // Handle the special case that the base is constant as
1380 // well. In this case it can be converted to a conventional
1381 // part select.
1382 if (NetEConst*base_c = dynamic_cast<NetEConst*> (base)) {
1383 long lsv = base_c->value().as_long();
1385 // If the part select convers exactly the entire
1386 // vector, then do not bother with it. Return the
1387 // signal itself.
1388 if (net->sig()->sb_to_idx(lsv) == (wid-1) && wid == net->vector_width())
1389 return net;
1391 // Otherwise, make a part select that covers the right range.
1392 NetExpr*ex = new NetEConst(verinum(net->sig()->sb_to_idx(lsv)-wid+1));
1393 NetESelect*ss = new NetESelect(net, ex, wid);
1394 ss->set_line(*this);
1396 delete base;
1397 return ss;
1400 long offset = net->lsi();
1401 NetExpr*base_adjusted = wid > 1
1402 ? make_add_expr(base,1-(long)wid-offset)
1403 : (offset == 0? base : make_add_expr(base, 0-offset));
1404 NetESelect*ss = new NetESelect(net, base_adjusted, wid);
1405 ss->set_line(*this);
1407 if (debug_elaborate) {
1408 cerr << get_line() << ": debug: Elaborate part "
1409 << "select base="<< *base_adjusted << ", wid="<< wid << endl;
1412 return ss;
1415 NetExpr* PEIdent::elaborate_expr_net_bit_(Design*des, NetScope*scope,
1416 NetESignal*net, NetScope*found_in) const
1418 const name_component_t&name_tail = path_.back();
1419 ivl_assert(*this, !name_tail.index.empty());
1421 const index_component_t&index_tail = name_tail.index.back();
1422 ivl_assert(*this, index_tail.msb != 0);
1423 ivl_assert(*this, index_tail.lsb == 0);
1425 NetExpr*ex = elab_and_eval(des, scope, index_tail.msb, -1);
1427 // If the bit select is constant, then treat it similar
1428 // to the part select, so that I save the effort of
1429 // making a mux part in the netlist.
1430 if (NetEConst*msc = dynamic_cast<NetEConst*> (ex)) {
1431 long msv = msc->value().as_long();
1432 unsigned idx = net->sig()->sb_to_idx(msv);
1434 if (idx >= net->vector_width()) {
1435 /* The bit select is out of range of the
1436 vector. This is legal, but returns a
1437 constant 1'bx value. */
1438 verinum x (verinum::Vx);
1439 NetEConst*tmp = new NetEConst(x);
1440 tmp->set_line(*this);
1442 cerr << get_line() << ": warning: Bit select ["
1443 << msv << "] out of range of vector "
1444 << net->name() << "[" << net->sig()->msb()
1445 << ":" << net->sig()->lsb() << "]." << endl;
1446 cerr << get_line() << ": : Replacing "
1447 << "expression with a constant 1'bx." << endl;
1448 delete ex;
1449 return tmp;
1452 // If the vector is only one bit, we are done. The
1453 // bit select will return the scaler itself.
1454 if (net->vector_width() == 1)
1455 return net;
1457 // Make an expression out of the index
1458 NetEConst*idx_c = new NetEConst(verinum(idx));
1459 idx_c->set_line(*net);
1461 // Make a bit select with the canonical index
1462 NetESelect*res = new NetESelect(net, idx_c, 1);
1463 res->set_line(*net);
1465 return res;
1468 // Non-constant bit select? punt and make a subsignal
1469 // device to mux the bit in the net. This is a fairly
1470 // complicated task because we need to generate
1471 // expressions to convert calculated bit select
1472 // values to canonical values that are used internally.
1474 if (net->sig()->msb() < net->sig()->lsb()) {
1475 ex = make_sub_expr(net->sig()->lsb(), ex);
1476 } else {
1477 ex = make_add_expr(ex, - net->sig()->lsb());
1480 NetESelect*ss = new NetESelect(net, ex, 1);
1481 ss->set_line(*this);
1482 return ss;
1485 NetExpr* PEIdent::elaborate_expr_net(Design*des, NetScope*scope,
1486 NetNet*net, NetScope*found_in,
1487 bool sys_task_arg) const
1489 if (net->array_dimensions() > 0)
1490 return elaborate_expr_net_word_(des, scope, net, found_in, sys_task_arg);
1492 NetESignal*node = new NetESignal(net);
1493 node->set_line(*this);
1495 index_component_t::ctype_t use_sel = index_component_t::SEL_NONE;
1496 if (! path_.back().index.empty())
1497 use_sel = path_.back().index.back().sel;
1499 // If this is a part select of a signal, then make a new
1500 // temporary signal that is connected to just the
1501 // selected bits. The lsb_ and msb_ expressions are from
1502 // the foo[msb:lsb] expression in the original.
1503 if (use_sel == index_component_t::SEL_PART)
1504 return elaborate_expr_net_part_(des, scope, node, found_in);
1506 if (use_sel == index_component_t::SEL_IDX_UP)
1507 return elaborate_expr_net_idx_up_(des, scope, node, found_in);
1509 if (use_sel == index_component_t::SEL_IDX_DO)
1510 return elaborate_expr_net_idx_do_(des, scope, node, found_in);
1512 if (use_sel == index_component_t::SEL_BIT)
1513 return elaborate_expr_net_bit_(des, scope, node, found_in);
1515 // It's not anything else, so this must be a simple identifier
1516 // expression with no part or bit select. Return the signal
1517 // itself as the expression.
1518 assert(use_sel == index_component_t::SEL_NONE);
1520 return node;
1523 unsigned PENumber::test_width(Design*, NetScope*,
1524 unsigned min, unsigned lval, bool&unsized_flag) const
1526 unsigned use_wid = value_->len();
1527 if (min > use_wid)
1528 use_wid = min;
1530 if (! value_->has_len())
1531 unsized_flag = true;
1533 return use_wid;
1536 NetEConst* PENumber::elaborate_expr(Design*des, NetScope*,
1537 int expr_width, bool) const
1539 assert(value_);
1540 verinum tvalue = *value_;
1542 // If the expr_width is >0, then the context is requesting a
1543 // specific size (for example this is part of the r-values of
1544 // an assignment) so we pad to the desired width and ignore
1545 // the self-determined size.
1546 if (expr_width > 0) {
1547 tvalue = pad_to_width(tvalue, expr_width);
1550 NetEConst*tmp = new NetEConst(tvalue);
1551 tmp->set_line(*this);
1552 return tmp;
1555 unsigned PEString::test_width(Design*des, NetScope*scope,
1556 unsigned min, unsigned lval,
1557 bool&unsized_flag) const
1559 unsigned use_wid = text_? 8*strlen(text_) : 0;
1560 if (min > use_wid)
1561 use_wid = min;
1563 return use_wid;
1566 NetEConst* PEString::elaborate_expr(Design*des, NetScope*,
1567 int expr_width, bool) const
1569 NetEConst*tmp = new NetEConst(value());
1570 tmp->set_line(*this);
1571 return tmp;
1574 unsigned PETernary::test_width(Design*des, NetScope*scope,
1575 unsigned min, unsigned lval,
1576 bool&flag) const
1578 unsigned tru_wid = tru_->test_width(des, scope, min, lval, flag);
1579 unsigned fal_wid = fal_->test_width(des, scope, min, lval, flag);
1580 return max(tru_wid,fal_wid);
1583 static bool test_ternary_operand_compat(ivl_variable_type_t l,
1584 ivl_variable_type_t r)
1586 if (l == IVL_VT_LOGIC && r == IVL_VT_BOOL)
1587 return true;
1588 if (l == IVL_VT_BOOL && r == IVL_VT_LOGIC)
1589 return true;
1590 if (l == r)
1591 return true;
1593 return false;
1597 * Elaborate the Ternary operator. I know that the expressions were
1598 * parsed so I can presume that they exist, and call elaboration
1599 * methods. If any elaboration fails, then give up and return 0.
1601 NetETernary*PETernary::elaborate_expr(Design*des, NetScope*scope,
1602 int expr_wid, bool) const
1604 assert(expr_);
1605 assert(tru_);
1606 assert(fal_);
1608 if (expr_wid < 0) {
1609 bool flag = false;
1610 unsigned tru_wid = tru_->test_width(des, scope, 0, 0, flag);
1611 unsigned fal_wid = fal_->test_width(des, scope, 0, 0, flag);
1612 expr_wid = max(tru_wid, fal_wid);
1613 if (debug_elaborate)
1614 cerr << get_line() << ": debug: "
1615 << "Self-sized ternary chooses wid="<< expr_wid
1616 << " from " <<tru_wid
1617 << " and " << fal_wid << endl;
1620 NetExpr*con = expr_->elaborate_expr(des, scope, -1, false);
1621 if (con == 0)
1622 return 0;
1624 NetExpr*tru = tru_->elaborate_expr(des, scope, expr_wid, false);
1625 if (tru == 0) {
1626 delete con;
1627 return 0;
1630 NetExpr*fal = fal_->elaborate_expr(des, scope, expr_wid, false);
1631 if (fal == 0) {
1632 delete con;
1633 delete tru;
1634 return 0;
1637 if (! test_ternary_operand_compat(tru->expr_type(), fal->expr_type())) {
1638 cerr << get_line() << ": error: Data types "
1639 << tru->expr_type() << " and "
1640 << fal->expr_type() << " of ternary"
1641 << " do not match." << endl;
1642 des->errors += 1;
1643 return 0;
1646 /* Whatever the width we choose for the ternary operator, we
1647 need to make sure the operands match. */
1648 tru = pad_to_width(tru, expr_wid);
1649 fal = pad_to_width(fal, expr_wid);
1651 NetETernary*res = new NetETernary(con, tru, fal);
1652 res->set_line(*this);
1653 return res;
1656 NetExpr* PEUnary::elaborate_expr(Design*des, NetScope*scope,
1657 int expr_wid, bool) const
1659 NetExpr*ip = expr_->elaborate_expr(des, scope, expr_wid, false);
1660 if (ip == 0) return 0;
1662 /* Should we evaluate expressions ahead of time,
1663 * just like in PEBinary::elaborate_expr() ?
1666 NetExpr*tmp;
1667 switch (op_) {
1668 default:
1669 tmp = new NetEUnary(op_, ip);
1670 tmp->set_line(*this);
1671 break;
1673 case '-':
1674 if (NetEConst*ipc = dynamic_cast<NetEConst*>(ip)) {
1676 verinum val = ipc->value();
1677 if (expr_wid > 0)
1678 val = pad_to_width(val, expr_wid);
1680 /* When taking the - of a number, turn it into a
1681 signed expression and extend it one bit to
1682 accommodate a possible sign bit. */
1683 verinum zero (verinum::V0, val.len()+1, val.has_len());
1684 verinum nval = zero - val;
1686 if (val.has_len())
1687 nval = verinum(nval, val.len());
1688 nval.has_sign(true);
1689 tmp = new NetEConst(nval);
1690 tmp->set_line(*this);
1691 delete ip;
1693 } else if (NetECReal*ipc = dynamic_cast<NetECReal*>(ip)) {
1695 /* When taking the - of a real, fold this into the
1696 constant value. */
1697 verireal val = - ipc->value();
1698 tmp = new NetECReal(val);
1699 tmp->set_line( *ip );
1700 delete ip;
1702 } else {
1703 tmp = new NetEUnary(op_, ip);
1704 tmp->set_line(*this);
1706 break;
1708 case '+':
1709 tmp = ip;
1710 break;
1712 case '!': // Logical NOT
1713 /* If the operand to unary ! is a constant, then I can
1714 evaluate this expression here and return a logical
1715 constant in its place. */
1716 if (NetEConst*ipc = dynamic_cast<NetEConst*>(ip)) {
1717 verinum val = ipc->value();
1718 unsigned v1 = 0;
1719 unsigned vx = 0;
1720 for (unsigned idx = 0 ; idx < val.len() ; idx += 1)
1721 switch (val[idx]) {
1722 case verinum::V0:
1723 break;
1724 case verinum::V1:
1725 v1 += 1;
1726 break;
1727 default:
1728 vx += 1;
1729 break;
1732 verinum::V res;
1733 if (v1 > 0)
1734 res = verinum::V0;
1735 else if (vx > 0)
1736 res = verinum::Vx;
1737 else
1738 res = verinum::V1;
1740 verinum vres (res, 1, true);
1741 tmp = new NetEConst(vres);
1742 tmp->set_line(*this);
1743 delete ip;
1744 } else {
1745 tmp = new NetEUReduce(op_, ip);
1746 tmp->set_line(*this);
1748 break;
1750 case '&': // Reduction AND
1751 case '|': // Reduction OR
1752 case '^': // Reduction XOR
1753 case 'A': // Reduction NAND (~&)
1754 case 'N': // Reduction NOR (~|)
1755 case 'X': // Reduction NXOR (~^)
1756 tmp = new NetEUReduce(op_, ip);
1757 tmp->set_line(*this);
1758 break;
1760 case '~':
1761 tmp = new NetEUBits(op_, ip);
1762 tmp->set_line(*this);
1763 break;
1766 return tmp;