Support for wide modulus operations.
[iverilog.git] / elab_expr.cc
blobcf4470b9345b189103f922838f9a7d3dc9ae5bb5
1 /*
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)
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
20 # include "config.h"
21 # include "compiler.h"
23 # include "pform.h"
24 # include "netlist.h"
25 # include "netmisc.h"
26 # include "util.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;
41 return min;
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
49 << endl;
50 des->errors += 1;
51 return 0;
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)
63 unsized_flag = true;
65 switch (op_) {
66 case '+':
67 case '-':
68 if (unsized_flag) {
69 wid_left += 1;
70 wid_right += 1;
72 if (wid_left > min)
73 min = wid_left;
74 if (wid_right > min)
75 min = wid_right;
76 if (lval > 0 && min > lval)
77 min = lval;
78 break;
80 default:
81 if (wid_left > min)
82 min = wid_left;
83 if (wid_right > min)
84 min = wid_right;
85 break;
88 return min;
92 * Elaborate binary expressions. This involves elaborating the left
93 * and right sides, and creating one of a variety of different NetExpr
94 * types.
96 NetEBinary* PEBinary::elaborate_expr(Design*des, NetScope*scope,
97 int expr_wid, bool) const
99 assert(left_);
100 assert(right_);
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)) {
105 delete lp;
106 delete rp;
107 return 0;
110 NetEBinary*tmp = elaborate_eval_expr_base_(des, lp, rp, expr_wid);
111 return tmp;
114 NetEBinary* PEBinary::elaborate_eval_expr_base_(Design*des,
115 NetExpr*lp,
116 NetExpr*rp,
117 int expr_wid) const
119 /* If either expression can be evaluated ahead of time, then
120 do so. This can prove helpful later. */
121 { NetExpr*tmp;
122 tmp = lp->eval_tree();
123 if (tmp) {
124 delete lp;
125 lp = tmp;
128 tmp = rp->eval_tree();
129 if (tmp) {
130 delete rp;
131 rp = tmp;
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,
145 int expr_wid) const
147 bool flag;
149 if (debug_elaborate) {
150 cerr << get_line() << ": debug: elaborate expression "
151 << *this << " expr_wid=" << expr_wid << endl;
154 NetEBinary*tmp;
156 switch (op_) {
157 default:
158 tmp = new NetEBinary(op_, lp, rp);
159 tmp->set_line(*this);
160 break;
162 case 'a':
163 case 'o':
164 tmp = new NetEBLogic(op_, lp, rp);
165 tmp->set_line(*this);
166 break;
168 case 'p':
169 tmp = new NetEBPow(op_, lp, rp);
170 tmp->set_line(*this);
171 break;
173 case '*':
174 tmp = new NetEBMult(op_, lp, rp);
175 tmp->set_line(*this);
176 break;
178 case '%':
179 /* The % operator does not support real arguments in
180 baseline Verilog. But we allow it in our extended
181 form of verilog. */
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;
186 des->errors += 1;
189 /* Fall through to handle the % with the / operator. */
190 case '/':
191 tmp = new NetEBDiv(op_, lp, rp);
192 tmp->set_line(*this);
193 break;
195 case 'l': // <<
196 case 'r': // >>
197 case 'R': // >>>
198 tmp = new NetEBShift(op_, lp, rp);
199 tmp->set_line(*this);
200 break;
202 case '^':
203 case '&':
204 case '|':
205 case 'O': // NOR (~|)
206 case 'A': // NAND (~&)
207 case 'X':
208 tmp = new NetEBBits(op_, lp, rp);
209 tmp->set_line(*this);
210 break;
212 case '+':
213 case '-':
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);
219 break;
221 case 'E': /* === */
222 case 'N': /* !== */
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;
227 return 0;
229 /* Fall through... */
230 case 'e': /* == */
231 case 'n': /* != */
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. */
241 case 'L': /* <= */
242 case 'G': /* >= */
243 case '<':
244 case '>':
245 tmp = new NetEBComp(op_, lp, rp);
246 tmp->set_line(*this);
247 flag = tmp->set_width(1);
248 if (flag == false) {
249 cerr << get_line() << ": internal error: "
250 "expression bit width of comparison != 1." << endl;
251 des->errors += 1;
253 break;
256 return tmp;
259 unsigned PEBComp::test_width(Design*, NetScope*,unsigned, unsigned, bool&) const
261 return 1;
264 NetEBinary* PEBComp::elaborate_expr(Design*des, NetScope*scope,
265 int expr_width, bool sys_task_arg) const
267 assert(left_);
268 assert(right_);
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)) {
294 delete lp;
295 delete rp;
296 return 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.
310 return wid_left;
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];
321 if (expr == 0)
322 return 0;
323 unsigned wid = expr->test_width(des, scope, min, lval, unsized_flag);
324 if (debug_elaborate)
325 cerr << get_line() << ": debug: test_width"
326 << " of $signed/$unsigned returns test_width"
327 << " of subexpression." << endl;
328 return wid;
331 if (debug_elaborate)
332 cerr << get_line() << ": debug: test_width "
333 << "of system function " << name
334 << " returns 32 always?" << endl;
335 return 32;
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_);
346 if (def == 0) {
347 if (debug_elaborate)
348 cerr << get_line() << ": debug: test_width "
349 << "cannot find definition of " << path_
350 << " in " << scope_path(scope) << "." << endl;
351 return 0;
354 NetScope*dscope = def->scope();
355 assert(dscope);
357 if (NetNet*res = dscope->find_signal(dscope->basename())) {
358 if (debug_elaborate)
359 cerr << get_line() << ": debug: test_width "
360 << "of function returns width " << res->vector_width()
361 << "." << endl;
362 return res->vector_width();
365 ivl_assert(*this, 0);
366 return 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;
387 des->errors += 1;
388 return 0;
391 PExpr*expr = parms_[0];
392 NetExpr*sub = expr->elaborate_expr(des, scope, -1, true);
393 sub->cast_signed(true);
394 return sub;
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;
401 des->errors += 1;
402 return 0;
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);
412 return sub;
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
418 deleted. */
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;
424 des->errors += 1;
425 return 0;
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();
442 delete tmp;
445 verinum val (sub_expr_width, 8*sizeof(unsigned));
446 NetEConst*sub = new NetEConst(val);
447 sub->set_line(*this);
449 return sub;
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
455 evaluated. */
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;
460 des->errors += 1;
461 return 0;
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);
468 delete sub;
470 sub = new NetEConst(val);
471 sub->set_line(*this);
473 return sub;
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))
495 nparms = 0;
497 NetESFunc*fun = new NetESFunc(peek_tail_name(path_), sfunc_type,
498 wid, nparms);
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];
512 if (expr) {
513 NetExpr*tmp1 = expr->elaborate_expr(des, scope, -1, true);
514 if (NetExpr*tmp2 = tmp1->eval_tree()) {
515 delete tmp1;
516 fun->parm(idx, tmp2);
517 } else {
518 fun->parm(idx, tmp1);
521 } else {
522 missing_parms += 1;
523 fun->parm(idx, 0);
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;
533 des->errors += 1;
536 return fun;
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_);
546 if (def == 0) {
547 cerr << get_line() << ": error: No function " << path_ <<
548 " in this context (" << scope_path(scope) << ")." << endl;
549 des->errors += 1;
550 return 0;
552 assert(def);
554 NetScope*dscope = def->scope();
555 assert(dscope);
557 if (! check_call_matches_definition_(des, dscope))
558 return 0;
560 unsigned parms_count = parms_.count();
561 if ((parms_count == 1) && (parms_[0] == 0))
562 parms_count = 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];
576 if (tmp) {
577 int argwid = def->port(idx)->vector_width();
578 parms[idx] = elab_and_eval(des, scope, tmp, argwid);
579 if (debug_elaborate)
580 cerr << get_line() << ": debug:"
581 << " function " << path_
582 << " arg " << (idx+1)
583 << " argwid=" << argwid
584 << ": " << *parms[idx] << endl;
586 } else {
587 missing_parms += 1;
588 parms[idx] = 0;
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;
597 des->errors += 1;
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());
614 return func;
617 cerr << get_line() << ": internal error: Unable to locate "
618 "function return value for " << path_
619 << " in " << dscope->basename() << "." << endl;
620 des->errors += 1;
621 return 0;
625 NetExpr* PEConcat::elaborate_expr(Design*des, NetScope*scope,
626 int expr_wid, bool) const
628 NetExpr* repeat = 0;
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. */
637 if (repeat_) {
638 NetExpr*tmp = elab_and_eval(des, scope, repeat_, -1);
639 assert(tmp);
640 NetEConst*rep = dynamic_cast<NetEConst*>(tmp);
642 if (rep == 0) {
643 cerr << get_line() << ": error: "
644 "concatenation repeat expression cannot be evaluated."
645 << endl;
646 cerr << get_line() << ": : The expression is: "
647 << *tmp << endl;
648 des->errors += 1;
651 repeat = rep;
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;
665 des->errors += 1;
666 continue;
669 assert(parms_[idx]);
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: "
678 << *ex << endl;
679 des->errors += 1;
682 wid_sum += ex->expr_width();
683 tmp->set(idx, ex);
686 tmp->set_width(wid_sum * tmp->repeat());
688 return tmp;
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);
696 return tmp;
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
717 the caller. */
718 NetExpr*lsb_ex = elab_and_eval(des, scope, index_tail.lsb, -1);
719 NetEConst*lsb_c = dynamic_cast<NetEConst*>(lsb_ex);
720 if (lsb_c == 0) {
721 cerr << index_tail.lsb->get_line() << ": error: "
722 "Part select expressions must be constant."
723 << endl;
724 cerr << index_tail.lsb->get_line() << ": : "
725 "This lsb expression violates the rule: "
726 << *index_tail.lsb << endl;
727 des->errors += 1;
728 return false;
731 NetExpr*msb_ex = elab_and_eval(des, scope, index_tail.msb, -1);
732 NetEConst*msb_c = dynamic_cast<NetEConst*>(msb_ex);
733 if (msb_c == 0) {
734 cerr << index_tail.msb->get_line() << ": error: "
735 "Part select expressions must be constant."
736 << endl;
737 cerr << index_tail.msb->get_line() << ": : This msb expression "
738 "violates the rule: " << *index_tail.msb << endl;
739 des->errors += 1;
740 return false;
743 msb = msb_c->value().as_long();
744 lsb = lsb_c->value().as_long();
746 delete msb_ex;
747 delete lsb_ex;
748 return true;
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);
760 bool flag = true;
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);
768 if (wid_c == 0) {
769 cerr << get_line() << ": error: Indexed part width must be "
770 << "constant. Expression in question is..." << endl;
771 cerr << get_line() << ": : " << *wid_ex << endl;
772 des->errors += 1;
773 flag = false;
776 wid = wid_c? wid_c->value().as_ulong() : 1;
777 delete wid_ex;
779 return flag;
782 unsigned PEIdent::test_width(Design*des, NetScope*scope,
783 unsigned min, unsigned lval,
784 bool&unsized_flag) const
786 NetNet* net = 0;
787 const NetExpr*par = 0;
788 NetEvent* eve = 0;
790 const NetExpr*ex1, *ex2;
792 symbol_search(des, scope, path_, net, par, eve, ex1, ex2);
794 if (net != 0) {
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();
801 switch (use_sel) {
802 case index_component_t::SEL_NONE:
803 break;
804 case index_component_t::SEL_PART:
805 { long msb, lsb;
806 calculate_parts_(des, scope, msb, lsb);
807 use_width = 1 + ((msb>lsb)? (msb-lsb) : (lsb-msb));
808 break;
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);
814 use_width = tmp;
815 break;
817 case index_component_t::SEL_BIT:
818 use_width = 1;
819 break;
820 default:
821 ivl_assert(*this, 0);
823 return use_width;
826 return min;
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
843 assert(scope);
845 NetNet* net = 0;
846 const NetExpr*par = 0;
847 NetEvent* eve = 0;
849 const NetExpr*ex1, *ex2;
851 NetScope*found_in = symbol_search(des, scope, path_,
852 net, par, eve,
853 ex1, ex2);
855 // If the identifier name is a parameter name, then return
856 // a reference to the parameter expression.
857 if (par != 0)
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.
863 if (net != 0)
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.
868 if (eve != 0) {
869 NetEEvent*tmp = new NetEEvent(eve);
870 tmp->set_line(*this);
871 return tmp;
874 // Hmm... maybe this is a genvar? This is only possible while
875 // processing generate blocks, but then the genvar_tmp will be
876 // set in the scope.
877 if (path_.size() == 1
878 && scope->genvar_tmp.str()
879 && strcmp(peek_tail_name(path_), scope->genvar_tmp) == 0) {
880 if (debug_elaborate)
881 cerr << get_line() << ": debug: " << path_
882 << " is genvar with value " << scope->genvar_tmp_val
883 << "." << endl;
884 verinum val (scope->genvar_tmp_val);
885 NetEConst*tmp = new NetEConst(val);
886 tmp->set_line(*this);
887 return tmp;
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;
901 NetExpr*tmp = 0;
902 switch (value.type) {
903 case IVL_VT_BOOL:
904 tmp = new NetEConst(verinum(value.integer));
905 break;
906 case IVL_VT_REAL:
907 tmp = new NetECReal(verireal(value.real_val));
908 break;
909 default:
910 break;
912 assert(tmp);
913 tmp->set_line(*this);
915 if (debug_elaborate)
916 cerr << get_line() << ": debug: " << path_
917 << " is a specparam" << endl;
918 return tmp;
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;
930 des->errors += 1;
931 return 0;
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);
943 if (debug_elaborate)
944 cerr << get_line() << ": debug: Found scope "
945 << use_name << " in scope " << scope->basename()
946 << endl;
948 return tmp;
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);
961 if (debug_elaborate)
962 cerr << get_line() << ": debug: Found scope "
963 << nsc->basename()
964 << " path=" << path_ << endl;
966 if (! sys_task_arg) {
967 cerr << get_line() << ": error: Scope name "
968 << nsc->basename() << " not allowed here." << endl;
969 des->errors += 1;
972 return tmp;
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);
980 if (debug_elaborate)
981 cerr << get_line() << ": debug: Found scope "
982 << nsc->basename() << " in " << scope_path(scope) << endl;
984 return tmp;
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;
990 des->errors += 1;
991 return 0;
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,
1000 NetScope*scope,
1001 const NetExpr*par,
1002 NetScope*found_in,
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;
1031 des->errors += 1;
1032 return 0;
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;
1041 des->errors += 1;
1042 return 0;
1044 unsigned long ulsb=lsb;
1045 unsigned long umsb=msb;
1047 NetEConst*le = dynamic_cast<NetEConst*>(tmp);
1048 assert(le);
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));
1068 else
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. */
1075 if (exl.is_string()
1076 && (lsb%8 == 0)
1077 && (result.len()%8 == 0))
1078 result = verinum(result.as_string());
1080 delete tmp;
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);
1094 if (wid_ec == 0) {
1095 cerr << index_tail.lsb->get_line() << ": error: "
1096 << "Second expression of indexed part select "
1097 << "most be constant." << endl;
1098 des->errors += 1;
1099 return 0;
1102 unsigned wid = wid_ec->value().as_ulong();
1104 NetExpr*idx_ex = elab_and_eval(des, scope, index_tail.msb, -1);
1105 if (idx_ex == 0) {
1106 return 0;
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();
1130 if (re) {
1131 delete mtmp;
1132 mtmp = re;
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);
1143 if (le && re) {
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())) {
1153 rb = lv[ridx];
1155 } else if ((ridx >= 0) && (!lv.has_len())) {
1156 if (lv.has_sign())
1157 rb = lv[lv.len()-1];
1158 else
1159 rb = verinum::V0;
1162 NetEConst*re = new NetEConst(verinum(rb, 1));
1163 delete tmp;
1164 delete mtmp;
1165 tmp = re;
1167 } else {
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);
1176 if (par_me) {
1177 long par_mv = par_me->value().as_long();
1178 long par_lv = par_le->value().as_long();
1179 if (par_mv >= par_lv) {
1180 mtmp = par_lv
1181 ? make_add_expr(mtmp, 0-par_lv)
1182 : mtmp;
1183 } else {
1184 if (par_lv != 0)
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);
1196 tmp = stmp;
1199 } else {
1200 /* No bit or part select. Make the constant into a
1201 NetEConstParam if possible. */
1202 NetEConst*ctmp = dynamic_cast<NetEConst*>(tmp);
1203 if (ctmp != 0) {
1204 perm_string name = peek_tail_name(path_);
1205 NetEConstParam*ptmp
1206 = new NetEConstParam(found_in, name, ctmp->value());
1207 delete tmp;
1208 tmp = ptmp;
1212 tmp->set_line(*this);
1213 return tmp;
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;
1228 des->errors += 1;
1229 return 0;
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;
1239 des->errors += 1;
1240 return 0;
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)
1250 return 0;
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);
1261 delete word_index;
1262 return resx;
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);
1271 delete word_index;
1272 word_index = tmp;
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()) {
1282 word_index = tmp;
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);
1309 return res;
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
1318 long msv, lsv;
1319 bool flag = calculate_parts_(des, scope, msv, lsv);
1320 if (!flag)
1321 return 0;
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;
1331 des->errors += 1;
1332 //delete lsn;
1333 //delete msn;
1334 return net;
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;
1341 des->errors += 1;
1342 //delete lsn;
1343 //delete msn;
1344 return net;
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;
1351 des->errors += 1;
1352 //delete lsn;
1353 //delete msn;
1354 return net;
1357 // If the part select convers exactly the entire
1358 // vector, then do not bother with it. Return the
1359 // signal itself.
1360 if (net->sig()->sb_to_idx(lsv) == 0 && wid == net->vector_width())
1361 return net;
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);
1367 return ss;
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
1391 // part select.
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
1397 // signal itself.
1398 if (net->sig()->sb_to_idx(lsv) == 0 && wid == net->vector_width())
1399 return net;
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);
1406 delete base;
1407 return ss;
1410 if (net->msi() > net->lsi()) {
1411 if (long offset = net->lsi())
1412 base = make_add_expr(base, 0-offset);
1413 } else {
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;
1427 return ss;
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
1450 // part select.
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
1456 // signal itself.
1457 if (net->sig()->sb_to_idx(lsv) == (wid-1) && wid == net->vector_width())
1458 return net;
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);
1465 delete base;
1466 return ss;
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;
1481 return ss;
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;
1517 delete ex;
1518 return tmp;
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)
1524 return net;
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);
1534 return res;
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);
1545 } else {
1546 ex = make_add_expr(ex, - net->sig()->lsb());
1549 NetESelect*ss = new NetESelect(net, ex, 1);
1550 ss->set_line(*this);
1551 return ss;
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);
1589 return node;
1592 unsigned PENumber::test_width(Design*, NetScope*,
1593 unsigned min, unsigned lval, bool&unsized_flag) const
1595 unsigned use_wid = value_->len();
1596 if (min > use_wid)
1597 use_wid = min;
1599 if (! value_->has_len())
1600 unsized_flag = true;
1602 return use_wid;
1605 NetEConst* PENumber::elaborate_expr(Design*des, NetScope*,
1606 int expr_width, bool) const
1608 assert(value_);
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);
1621 return tmp;
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;
1629 if (min > use_wid)
1630 use_wid = min;
1632 return use_wid;
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);
1640 return tmp;
1643 unsigned PETernary::test_width(Design*des, NetScope*scope,
1644 unsigned min, unsigned lval,
1645 bool&flag) const
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)
1656 return true;
1657 if (l == IVL_VT_BOOL && r == IVL_VT_LOGIC)
1658 return true;
1659 if (l == r)
1660 return true;
1662 return false;
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
1673 assert(expr_);
1674 assert(tru_);
1675 assert(fal_);
1677 if (expr_wid < 0) {
1678 bool flag = false;
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);
1690 if (con == 0)
1691 return 0;
1693 NetExpr*tru = tru_->elaborate_expr(des, scope, expr_wid, false);
1694 if (tru == 0) {
1695 delete con;
1696 return 0;
1699 NetExpr*fal = fal_->elaborate_expr(des, scope, expr_wid, false);
1700 if (fal == 0) {
1701 delete con;
1702 delete tru;
1703 return 0;
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;
1711 des->errors += 1;
1712 return 0;
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);
1722 return res;
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() ?
1735 NetExpr*tmp;
1736 switch (op_) {
1737 default:
1738 tmp = new NetEUnary(op_, ip);
1739 tmp->set_line(*this);
1740 break;
1742 case '-':
1743 if (NetEConst*ipc = dynamic_cast<NetEConst*>(ip)) {
1745 verinum val = ipc->value();
1746 if (expr_wid > 0)
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;
1755 if (val.has_len())
1756 nval = verinum(nval, val.len());
1757 nval.has_sign(true);
1758 tmp = new NetEConst(nval);
1759 tmp->set_line(*this);
1760 delete ip;
1762 } else if (NetECReal*ipc = dynamic_cast<NetECReal*>(ip)) {
1764 /* When taking the - of a real, fold this into the
1765 constant value. */
1766 verireal val = - ipc->value();
1767 tmp = new NetECReal(val);
1768 tmp->set_line( *ip );
1769 delete ip;
1771 } else {
1772 tmp = new NetEUnary(op_, ip);
1773 tmp->set_line(*this);
1775 break;
1777 case '+':
1778 tmp = ip;
1779 break;
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();
1787 unsigned v1 = 0;
1788 unsigned vx = 0;
1789 for (unsigned idx = 0 ; idx < val.len() ; idx += 1)
1790 switch (val[idx]) {
1791 case verinum::V0:
1792 break;
1793 case verinum::V1:
1794 v1 += 1;
1795 break;
1796 default:
1797 vx += 1;
1798 break;
1801 verinum::V res;
1802 if (v1 > 0)
1803 res = verinum::V0;
1804 else if (vx > 0)
1805 res = verinum::Vx;
1806 else
1807 res = verinum::V1;
1809 verinum vres (res, 1, true);
1810 tmp = new NetEConst(vres);
1811 tmp->set_line(*this);
1812 delete ip;
1813 } else {
1814 tmp = new NetEUReduce(op_, ip);
1815 tmp->set_line(*this);
1817 break;
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);
1827 break;
1829 case '~':
1830 tmp = new NetEUBits(op_, ip);
1831 tmp->set_line(*this);
1832 break;
1835 return tmp;