Add support for text macros with arguments.
[iverilog.git] / elab_expr.cc
bloba040d1b7eb4c16f823b008ad529109c037b08b3e
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 void PEBinary::surpress_operand_sign_if_needed_(NetExpr*lp, NetExpr*rp)
116 // If either operand is unsigned, then treat the whole
117 // expression as unsigned. This test needs to be done hear
118 // instead of in *_expr_base_ because it needs to be done
119 // ahead of any subexpression evaluation (because they need to
120 // know their signedness to evaluate) and because there are
121 // exceptions to this rule.
122 if (! lp->has_sign())
123 rp->cast_signed(false);
124 if (! rp->has_sign())
125 lp->cast_signed(false);
128 NetEBinary* PEBinary::elaborate_eval_expr_base_(Design*des,
129 NetExpr*lp,
130 NetExpr*rp,
131 int expr_wid) const
133 /* If either expression can be evaluated ahead of time, then
134 do so. This can prove helpful later. */
135 { NetExpr*tmp;
136 tmp = lp->eval_tree();
137 if (tmp) {
138 delete lp;
139 lp = tmp;
142 tmp = rp->eval_tree();
143 if (tmp) {
144 delete rp;
145 rp = tmp;
149 return elaborate_expr_base_(des, lp, rp, expr_wid);
153 * This is common elaboration of the operator. It presumes that the
154 * operands are elaborated as necessary, and all I need to do is make
155 * the correct NetEBinary object and connect the parameters.
157 NetEBinary* PEBinary::elaborate_expr_base_(Design*des,
158 NetExpr*lp, NetExpr*rp,
159 int expr_wid) const
161 bool flag;
163 if (debug_elaborate) {
164 cerr << get_line() << ": debug: elaborate expression "
165 << *this << " expr_wid=" << expr_wid << endl;
168 NetEBinary*tmp;
170 switch (op_) {
171 default:
172 tmp = new NetEBinary(op_, lp, rp);
173 tmp->set_line(*this);
174 break;
176 case 'a':
177 case 'o':
178 tmp = new NetEBLogic(op_, lp, rp);
179 tmp->set_line(*this);
180 break;
182 case 'p':
183 tmp = new NetEBPow(op_, lp, rp);
184 tmp->set_line(*this);
185 break;
187 case '*':
188 tmp = new NetEBMult(op_, lp, rp);
189 tmp->set_line(*this);
190 break;
192 case '%':
193 /* The % operator does not support real arguments in
194 baseline Verilog. But we allow it in our extended
195 form of verilog. */
196 if (generation_flag < GN_VER2001X) {
197 if (lp->expr_type()==IVL_VT_REAL || rp->expr_type()==IVL_VT_REAL) {
198 cerr << get_line() << ": error: Modulus operator may not "
199 "have REAL operands." << endl;
200 des->errors += 1;
203 /* Fall through to handle the % with the / operator. */
204 case '/':
205 tmp = new NetEBDiv(op_, lp, rp);
206 tmp->set_line(*this);
207 break;
209 case 'l': // <<
210 case 'r': // >>
211 case 'R': // >>>
212 tmp = new NetEBShift(op_, lp, rp);
213 tmp->set_line(*this);
214 break;
216 case '^':
217 case '&':
218 case '|':
219 case 'O': // NOR (~|)
220 case 'A': // NAND (~&)
221 case 'X':
222 tmp = new NetEBBits(op_, lp, rp);
223 tmp->set_line(*this);
224 break;
226 case '+':
227 case '-':
228 tmp = new NetEBAdd(op_, lp, rp, expr_wid==-2? true : false);
229 if (expr_wid > 0 && (tmp->expr_type() == IVL_VT_BOOL
230 || tmp->expr_type() == IVL_VT_LOGIC))
231 tmp->set_width(expr_wid);
232 tmp->set_line(*this);
233 break;
235 case 'E': /* === */
236 case 'N': /* !== */
237 if (lp->expr_type() == IVL_VT_REAL
238 || rp->expr_type() == IVL_VT_REAL) {
239 cerr << get_line() << ": error: Case equality may not "
240 << "have real operands." << endl;
241 return 0;
243 /* Fall through... */
244 case 'e': /* == */
245 case 'n': /* != */
246 if (dynamic_cast<NetEConst*>(rp)
247 && (lp->expr_width() > rp->expr_width()))
248 rp->set_width(lp->expr_width());
250 if (dynamic_cast<NetEConst*>(lp)
251 && (lp->expr_width() < rp->expr_width()))
252 lp->set_width(rp->expr_width());
254 /* from here, handle this like other compares. */
255 case 'L': /* <= */
256 case 'G': /* >= */
257 case '<':
258 case '>':
259 tmp = new NetEBComp(op_, lp, rp);
260 tmp->set_line(*this);
261 flag = tmp->set_width(1);
262 if (flag == false) {
263 cerr << get_line() << ": internal error: "
264 "expression bit width of comparison != 1." << endl;
265 des->errors += 1;
267 break;
270 return tmp;
273 unsigned PEBComp::test_width(Design*, NetScope*,unsigned, unsigned, bool&) const
275 return 1;
278 NetEBinary* PEBComp::elaborate_expr(Design*des, NetScope*scope,
279 int expr_width, bool sys_task_arg) const
281 assert(left_);
282 assert(right_);
284 bool unsized_flag = false;
285 unsigned left_width = left_->test_width(des, scope, 0, 0, unsized_flag);
286 bool save_flag = unsized_flag;
287 unsigned right_width = right_->test_width(des, scope, 0, 0, unsized_flag);
289 if (save_flag != unsized_flag)
290 left_width = left_->test_width(des, scope, 0, 0, unsized_flag);
292 /* Width of operands is self-determined. */
293 int use_wid = left_width;
294 if (right_width > left_width)
295 use_wid = right_width;
297 if (debug_elaborate) {
298 cerr << get_line() << ": debug: "
299 << "Comparison expression operands are "
300 << left_width << " bits and "
301 << right_width << " bits. Resorting to "
302 << use_wid << " bits." << endl;
305 NetExpr*lp = left_->elaborate_expr(des, scope, use_wid, false);
306 NetExpr*rp = right_->elaborate_expr(des, scope, use_wid, false);
307 if ((lp == 0) || (rp == 0)) {
308 delete lp;
309 delete rp;
310 return 0;
313 surpress_operand_sign_if_needed_(lp, rp);
315 return elaborate_eval_expr_base_(des, lp, rp, use_wid);
318 unsigned PEBShift::test_width(Design*des, NetScope*scope,
319 unsigned min, unsigned lval, bool&unsized_flag) const
321 unsigned wid_left = left_->test_width(des,scope,min, 0, unsized_flag);
323 // The right expression is self-determined and has no impact
324 // on the expression size that is generated.
326 return wid_left;
329 unsigned PECallFunction::test_width_sfunc_(Design*des, NetScope*scope,
330 unsigned min, unsigned lval,
331 bool&unsized_flag) const
333 perm_string name = peek_tail_name(path_);
335 if (name=="$signed"|| name=="$unsigned") {
336 PExpr*expr = parms_[0];
337 if (expr == 0)
338 return 0;
339 unsigned wid = expr->test_width(des, scope, min, lval, unsized_flag);
340 if (debug_elaborate)
341 cerr << get_line() << ": debug: test_width"
342 << " of $signed/$unsigned returns test_width"
343 << " of subexpression." << endl;
344 return wid;
347 if (debug_elaborate)
348 cerr << get_line() << ": debug: test_width "
349 << "of system function " << name
350 << " returns 32 always?" << endl;
351 return 32;
354 unsigned PECallFunction::test_width(Design*des, NetScope*scope,
355 unsigned min, unsigned lval,
356 bool&unsized_flag) const
358 if (peek_tail_name(path_)[0] == '$')
359 return test_width_sfunc_(des, scope, min, lval, unsized_flag);
361 NetFuncDef*def = des->find_function(scope, path_);
362 if (def == 0) {
363 if (debug_elaborate)
364 cerr << get_line() << ": debug: test_width "
365 << "cannot find definition of " << path_
366 << " in " << scope_path(scope) << "." << endl;
367 return 0;
370 NetScope*dscope = def->scope();
371 assert(dscope);
373 if (NetNet*res = dscope->find_signal(dscope->basename())) {
374 if (debug_elaborate)
375 cerr << get_line() << ": debug: test_width "
376 << "of function returns width " << res->vector_width()
377 << "." << endl;
378 return res->vector_width();
381 ivl_assert(*this, 0);
382 return 0;
386 * Given a call to a system function, generate the proper expression
387 * nodes to represent the call in the netlist. Since we don't support
388 * size_tf functions, make assumptions about widths based on some
389 * known function names.
391 NetExpr* PECallFunction::elaborate_sfunc_(Design*des, NetScope*scope, int expr_wid) const
394 /* Catch the special case that the system function is the
395 $signed function. This function is special, in that it does
396 not lead to executable code but takes the single parameter
397 and makes it into a signed expression. No bits are changed,
398 it just changes the interpretation. */
399 if (strcmp(peek_tail_name(path_), "$signed") == 0) {
400 if ((parms_.count() != 1) || (parms_[0] == 0)) {
401 cerr << get_line() << ": error: The $signed() function "
402 << "takes exactly one(1) argument." << endl;
403 des->errors += 1;
404 return 0;
407 PExpr*expr = parms_[0];
408 NetExpr*sub = expr->elaborate_expr(des, scope, -1, true);
409 sub->cast_signed(true);
410 return sub;
412 /* add $unsigned to match $signed */
413 if (strcmp(peek_tail_name(path_), "$unsigned") == 0) {
414 if ((parms_.count() != 1) || (parms_[0] == 0)) {
415 cerr << get_line() << ": error: The $unsigned() function "
416 << "takes exactly one(1) argument." << endl;
417 des->errors += 1;
418 return 0;
421 PExpr*expr = parms_[0];
422 NetExpr*sub = expr->elaborate_expr(des, scope, -1, true);
423 sub->cast_signed(false);
425 if (expr_wid > 0 && (unsigned)expr_wid > sub->expr_width())
426 sub = pad_to_width(sub, expr_wid);
428 return sub;
431 /* Interpret the internal $sizeof system function to return
432 the bit width of the sub-expression. The value of the
433 sub-expression is not used, so the expression itself can be
434 deleted. */
435 if ((strcmp(peek_tail_name(path_), "$sizeof") == 0)
436 || (strcmp(peek_tail_name(path_), "$bits") == 0)) {
437 if ((parms_.count() != 1) || (parms_[0] == 0)) {
438 cerr << get_line() << ": error: The $bits() function "
439 << "takes exactly one(1) argument." << endl;
440 des->errors += 1;
441 return 0;
444 if (strcmp(peek_tail_name(path_), "$sizeof") == 0)
445 cerr << get_line() << ": warning: $sizeof is deprecated."
446 << " Use $bits() instead." << endl;
448 PExpr*expr = parms_[0];
449 ivl_assert(*this, expr);
451 /* Elaborate the sub-expression to get its
452 self-determined width, and save that width. Then
453 delete the expression because we don't really want
454 the expression itself. */
455 long sub_expr_width = 0;
456 if (NetExpr*tmp = expr->elaborate_expr(des, scope, -1, true)) {
457 sub_expr_width = tmp->expr_width();
458 delete tmp;
461 verinum val (sub_expr_width, 8*sizeof(unsigned));
462 NetEConst*sub = new NetEConst(val);
463 sub->set_line(*this);
465 return sub;
468 /* Interpret the internal $is_signed system function to return
469 a single bit flag -- 1 if the expression is signed, 0
470 otherwise. The subexpression is elaborated but not
471 evaluated. */
472 if (strcmp(peek_tail_name(path_), "$is_signed") == 0) {
473 if ((parms_.count() != 1) || (parms_[0] == 0)) {
474 cerr << get_line() << ": error: The $is_signed() function "
475 << "takes exactly one(1) argument." << endl;
476 des->errors += 1;
477 return 0;
480 PExpr*expr = parms_[0];
481 NetExpr*sub = expr->elaborate_expr(des, scope, -1, true);
483 verinum val (sub->has_sign()? verinum::V1 : verinum::V0, 1);
484 delete sub;
486 sub = new NetEConst(val);
487 sub->set_line(*this);
489 return sub;
492 /* Get the return type of the system function by looking it up
493 in the sfunc_table. */
494 const struct sfunc_return_type*sfunc_info
495 = lookup_sys_func(peek_tail_name(path_));
497 ivl_variable_type_t sfunc_type = sfunc_info->type;
498 unsigned wid = sfunc_info->wid;
501 /* How many parameters are there? The Verilog language allows
502 empty parameters in certain contexts, so the parser will
503 allow things like func(1,,3). It will also cause func() to
504 be interpreted as a single empty parameter.
506 Functions cannot really take empty parameters, but the
507 case ``func()'' is the same as no parameters at all. So
508 catch that special case here. */
509 unsigned nparms = parms_.count();
510 if ((nparms == 1) && (parms_[0] == 0))
511 nparms = 0;
513 NetESFunc*fun = new NetESFunc(peek_tail_name(path_), sfunc_type,
514 wid, nparms);
515 if (sfunc_info->signed_flag)
516 fun->cast_signed(true);
518 /* Now run through the expected parameters. If we find that
519 there are missing parameters, print an error message.
521 While we're at it, try to evaluate the function parameter
522 expression as much as possible, and use the reduced
523 expression if one is created. */
525 unsigned missing_parms = 0;
526 for (unsigned idx = 0 ; idx < nparms ; idx += 1) {
527 PExpr*expr = parms_[idx];
528 if (expr) {
529 NetExpr*tmp1 = expr->elaborate_expr(des, scope, -1, true);
530 if (NetExpr*tmp2 = tmp1->eval_tree()) {
531 delete tmp1;
532 fun->parm(idx, tmp2);
533 } else {
534 fun->parm(idx, tmp1);
537 } else {
538 missing_parms += 1;
539 fun->parm(idx, 0);
543 if (missing_parms > 0) {
544 cerr << get_line() << ": error: The function "
545 << peek_tail_name(path_)
546 << " has been called with empty parameters." << endl;
547 cerr << get_line() << ": : Verilog doesn't allow "
548 << "passing empty parameters to functions." << endl;
549 des->errors += 1;
552 return fun;
555 NetExpr* PECallFunction::elaborate_expr(Design*des, NetScope*scope,
556 int expr_wid, bool) const
558 if (peek_tail_name(path_)[0] == '$')
559 return elaborate_sfunc_(des, scope, expr_wid);
561 NetFuncDef*def = des->find_function(scope, path_);
562 if (def == 0) {
563 cerr << get_line() << ": error: No function " << path_ <<
564 " in this context (" << scope_path(scope) << ")." << endl;
565 des->errors += 1;
566 return 0;
568 assert(def);
570 NetScope*dscope = def->scope();
571 assert(dscope);
573 if (! check_call_matches_definition_(des, dscope))
574 return 0;
576 unsigned parms_count = parms_.count();
577 if ((parms_count == 1) && (parms_[0] == 0))
578 parms_count = 0;
582 svector<NetExpr*> parms (parms_count);
584 /* Elaborate the input expressions for the function. This is
585 done in the scope of the function call, and not the scope
586 of the function being called. The scope of the called
587 function is elaborated when the definition is elaborated. */
589 unsigned missing_parms = 0;
590 for (unsigned idx = 0 ; idx < parms.count() ; idx += 1) {
591 PExpr*tmp = parms_[idx];
592 if (tmp) {
593 int argwid = def->port(idx)->vector_width();
594 parms[idx] = elab_and_eval(des, scope, tmp, argwid);
595 if (debug_elaborate)
596 cerr << get_line() << ": debug:"
597 << " function " << path_
598 << " arg " << (idx+1)
599 << " argwid=" << argwid
600 << ": " << *parms[idx] << endl;
602 } else {
603 missing_parms += 1;
604 parms[idx] = 0;
608 if (missing_parms > 0) {
609 cerr << get_line() << ": error: The function " << path_
610 << " has been called with empty parameters." << endl;
611 cerr << get_line() << ": : Verilog doesn't allow "
612 << "passing empty parameters to functions." << endl;
613 des->errors += 1;
617 /* Look for the return value signal for the called
618 function. This return value is a magic signal in the scope
619 of the function, that has the name of the function. The
620 function code assigns to this signal to return a value.
622 dscope, in this case, is the scope of the function, so the
623 return value is the name within that scope. */
625 if (NetNet*res = dscope->find_signal(dscope->basename())) {
626 NetESignal*eres = new NetESignal(res);
627 NetEUFunc*func = new NetEUFunc(dscope, eres, parms);
628 func->set_line(*this);
629 func->cast_signed(res->get_signed());
630 return func;
633 cerr << get_line() << ": internal error: Unable to locate "
634 "function return value for " << path_
635 << " in " << dscope->basename() << "." << endl;
636 des->errors += 1;
637 return 0;
641 NetExpr* PEConcat::elaborate_expr(Design*des, NetScope*scope,
642 int expr_wid, bool) const
644 NetExpr* repeat = 0;
646 if (debug_elaborate) {
647 cerr << get_line() << ": debug: Elaborate expr=" << *this
648 << ", expr_wid=" << expr_wid << endl;
651 /* If there is a repeat expression, then evaluate the constant
652 value and set the repeat count. */
653 if (repeat_) {
654 NetExpr*tmp = elab_and_eval(des, scope, repeat_, -1);
655 assert(tmp);
656 NetEConst*rep = dynamic_cast<NetEConst*>(tmp);
658 if (rep == 0) {
659 cerr << get_line() << ": error: "
660 "concatenation repeat expression cannot be evaluated."
661 << endl;
662 cerr << get_line() << ": : The expression is: "
663 << *tmp << endl;
664 des->errors += 1;
667 repeat = rep;
670 /* Make the empty concat expression. */
671 NetEConcat*tmp = new NetEConcat(parms_.count(), repeat);
672 tmp->set_line(*this);
674 unsigned wid_sum = 0;
676 /* Elaborate all the parameters and attach them to the concat node. */
677 for (unsigned idx = 0 ; idx < parms_.count() ; idx += 1) {
678 if (parms_[idx] == 0) {
679 cerr << get_line() << ": error: Missing expression "
680 << (idx+1) << " of concatenation list." << endl;
681 des->errors += 1;
682 continue;
685 assert(parms_[idx]);
686 NetExpr*ex = elab_and_eval(des, scope, parms_[idx], 0, 0);
687 if (ex == 0) continue;
689 ex->set_line(*parms_[idx]);
691 if (! ex->has_width()) {
692 cerr << ex->get_line() << ": error: operand of "
693 << "concatenation has indefinite width: "
694 << *ex << endl;
695 des->errors += 1;
698 wid_sum += ex->expr_width();
699 tmp->set(idx, ex);
702 tmp->set_width(wid_sum * tmp->repeat());
704 return tmp;
707 NetExpr* PEFNumber::elaborate_expr(Design*des, NetScope*scope, int, bool) const
709 NetECReal*tmp = new NetECReal(*value_);
710 tmp->set_line(*this);
711 tmp->set_width(1U, false);
712 return tmp;
716 * Given that the msb_ and lsb_ are part select expressions, this
717 * function calculates their values. Note that this method does *not*
718 * convert the values to canonical form.
720 bool PEIdent::calculate_parts_(Design*des, NetScope*scope,
721 long&msb, long&lsb) const
723 const name_component_t&name_tail = path_.back();
724 ivl_assert(*this, !name_tail.index.empty());
726 const index_component_t&index_tail = name_tail.index.back();
727 ivl_assert(*this, index_tail.sel == index_component_t::SEL_PART);
728 ivl_assert(*this, index_tail.msb && index_tail.lsb);
730 /* This handles part selects. In this case, there are
731 two bit select expressions, and both must be
732 constant. Evaluate them and pass the results back to
733 the caller. */
734 NetExpr*lsb_ex = elab_and_eval(des, scope, index_tail.lsb, -1);
735 NetEConst*lsb_c = dynamic_cast<NetEConst*>(lsb_ex);
736 if (lsb_c == 0) {
737 cerr << index_tail.lsb->get_line() << ": error: "
738 "Part select expressions must be constant."
739 << endl;
740 cerr << index_tail.lsb->get_line() << ": : "
741 "This lsb expression violates the rule: "
742 << *index_tail.lsb << endl;
743 des->errors += 1;
744 return false;
747 NetExpr*msb_ex = elab_and_eval(des, scope, index_tail.msb, -1);
748 NetEConst*msb_c = dynamic_cast<NetEConst*>(msb_ex);
749 if (msb_c == 0) {
750 cerr << index_tail.msb->get_line() << ": error: "
751 "Part select expressions must be constant."
752 << endl;
753 cerr << index_tail.msb->get_line() << ": : This msb expression "
754 "violates the rule: " << *index_tail.msb << endl;
755 des->errors += 1;
756 return false;
759 msb = msb_c->value().as_long();
760 lsb = lsb_c->value().as_long();
762 delete msb_ex;
763 delete lsb_ex;
764 return true;
767 bool PEIdent::calculate_up_do_width_(Design*des, NetScope*scope,
768 unsigned long&wid) const
770 const name_component_t&name_tail = path_.back();
771 ivl_assert(*this, !name_tail.index.empty());
773 const index_component_t&index_tail = name_tail.index.back();
774 ivl_assert(*this, index_tail.lsb && index_tail.msb);
776 bool flag = true;
778 /* Calculate the width expression (in the lsb_ position)
779 first. If the expression is not constant, error but guess 1
780 so we can keep going and find more errors. */
781 NetExpr*wid_ex = elab_and_eval(des, scope, index_tail.lsb, -1);
782 NetEConst*wid_c = dynamic_cast<NetEConst*>(wid_ex);
784 if (wid_c == 0) {
785 cerr << get_line() << ": error: Indexed part width must be "
786 << "constant. Expression in question is..." << endl;
787 cerr << get_line() << ": : " << *wid_ex << endl;
788 des->errors += 1;
789 flag = false;
792 wid = wid_c? wid_c->value().as_ulong() : 1;
793 delete wid_ex;
795 return flag;
798 unsigned PEIdent::test_width(Design*des, NetScope*scope,
799 unsigned min, unsigned lval,
800 bool&unsized_flag) const
802 NetNet* net = 0;
803 const NetExpr*par = 0;
804 NetEvent* eve = 0;
806 const NetExpr*ex1, *ex2;
808 symbol_search(des, scope, path_, net, par, eve, ex1, ex2);
810 if (net != 0) {
811 const name_component_t&name_tail = path_.back();
812 index_component_t::ctype_t use_sel = index_component_t::SEL_NONE;
813 if (!name_tail.index.empty())
814 use_sel = name_tail.index.back().sel;
816 unsigned use_width = net->vector_width();
817 switch (use_sel) {
818 case index_component_t::SEL_NONE:
819 break;
820 case index_component_t::SEL_PART:
821 { long msb, lsb;
822 calculate_parts_(des, scope, msb, lsb);
823 use_width = 1 + ((msb>lsb)? (msb-lsb) : (lsb-msb));
824 break;
826 case index_component_t::SEL_IDX_UP:
827 case index_component_t::SEL_IDX_DO:
828 { unsigned long tmp = 0;
829 calculate_up_do_width_(des, scope, tmp);
830 use_width = tmp;
831 break;
833 case index_component_t::SEL_BIT:
834 use_width = 1;
835 break;
836 default:
837 ivl_assert(*this, 0);
839 return use_width;
842 return min;
846 * Elaborate an identifier in an expression. The identifier can be a
847 * parameter name, a signal name or a memory name. It can also be a
848 * scope name (Return a NetEScope) but only certain callers can use
849 * scope names. However, we still support it here.
851 * Function names are not handled here, they are detected by the
852 * parser and are elaborated by PECallFunction.
854 * The signal name may be escaped, but that affects nothing here.
856 NetExpr* PEIdent::elaborate_expr(Design*des, NetScope*scope,
857 int expr_wid, bool sys_task_arg) const
859 assert(scope);
861 NetNet* net = 0;
862 const NetExpr*par = 0;
863 NetEvent* eve = 0;
865 const NetExpr*ex1, *ex2;
867 NetScope*found_in = symbol_search(des, scope, path_,
868 net, par, eve,
869 ex1, ex2);
871 // If the identifier name is a parameter name, then return
872 // a reference to the parameter expression.
873 if (par != 0)
874 return elaborate_expr_param(des, scope, par, found_in, ex1, ex2);
877 // If the identifier names a signal (a register or wire)
878 // then create a NetESignal node to handle it.
879 if (net != 0)
880 return elaborate_expr_net(des, scope, net, found_in, sys_task_arg);
882 // If the identifier is a named event.
883 // is a variable reference.
884 if (eve != 0) {
885 NetEEvent*tmp = new NetEEvent(eve);
886 tmp->set_line(*this);
887 return tmp;
890 // Hmm... maybe this is a genvar? This is only possible while
891 // processing generate blocks, but then the genvar_tmp will be
892 // set in the scope.
893 if (path_.size() == 1
894 && scope->genvar_tmp.str()
895 && strcmp(peek_tail_name(path_), scope->genvar_tmp) == 0) {
896 if (debug_elaborate)
897 cerr << get_line() << ": debug: " << path_
898 << " is genvar with value " << scope->genvar_tmp_val
899 << "." << endl;
900 verinum val (scope->genvar_tmp_val);
901 NetEConst*tmp = new NetEConst(val);
902 tmp->set_line(*this);
903 return tmp;
906 // A specparam? Look up the name to see if it is a
907 // specparam. If we find it, then turn it into a NetEConst
908 // value and return that. Of course, this does not apply if
909 // specify blocks are disabled.
911 if (gn_specify_blocks_flag) {
912 map<perm_string,NetScope::spec_val_t>::const_iterator specp;
913 perm_string key = peek_tail_name(path_);
914 if (path_.size() == 1
915 && ((specp = scope->specparams.find(key)) != scope->specparams.end())) {
916 NetScope::spec_val_t value = (*specp).second;
917 NetExpr*tmp = 0;
918 switch (value.type) {
919 case IVL_VT_BOOL:
920 tmp = new NetEConst(verinum(value.integer));
921 break;
922 case IVL_VT_REAL:
923 tmp = new NetECReal(verireal(value.real_val));
924 break;
925 default:
926 break;
928 assert(tmp);
929 tmp->set_line(*this);
931 if (debug_elaborate)
932 cerr << get_line() << ": debug: " << path_
933 << " is a specparam" << endl;
934 return tmp;
938 // At this point we've exhausted all the possibilities that
939 // are not scopes. If this is not a system task argument, then
940 // it cannot be a scope name, so give up.
942 if (! sys_task_arg) {
943 // I cannot interpret this identifier. Error message.
944 cerr << get_line() << ": error: Unable to bind wire/reg/memory "
945 "`" << path_ << "' in `" << scope_path(scope) << "'" << endl;
946 des->errors += 1;
947 return 0;
950 // Finally, if this is a scope name, then return that. Look
951 // first to see if this is a name of a local scope. Failing
952 // that, search globally for a hierarchical name.
953 if ((path_.size() == 1)) {
954 hname_t use_name ( peek_tail_name(path_) );
955 if (NetScope*nsc = scope->child(use_name)) {
956 NetEScope*tmp = new NetEScope(nsc);
957 tmp->set_line(*this);
959 if (debug_elaborate)
960 cerr << get_line() << ": debug: Found scope "
961 << use_name << " in scope " << scope->basename()
962 << endl;
964 return tmp;
968 list<hname_t> spath = eval_scope_path(des, scope, path_);
970 ivl_assert(*this, spath.size() == path_.size());
972 // Try full hierarchical scope name.
973 if (NetScope*nsc = des->find_scope(spath)) {
974 NetEScope*tmp = new NetEScope(nsc);
975 tmp->set_line(*this);
977 if (debug_elaborate)
978 cerr << get_line() << ": debug: Found scope "
979 << nsc->basename()
980 << " path=" << path_ << endl;
982 if (! sys_task_arg) {
983 cerr << get_line() << ": error: Scope name "
984 << nsc->basename() << " not allowed here." << endl;
985 des->errors += 1;
988 return tmp;
991 // Try relative scope name.
992 if (NetScope*nsc = des->find_scope(scope, spath)) {
993 NetEScope*tmp = new NetEScope(nsc);
994 tmp->set_line(*this);
996 if (debug_elaborate)
997 cerr << get_line() << ": debug: Found scope "
998 << nsc->basename() << " in " << scope_path(scope) << endl;
1000 return tmp;
1003 // I cannot interpret this identifier. Error message.
1004 cerr << get_line() << ": error: Unable to bind wire/reg/memory "
1005 "`" << path_ << "' in `" << scope_path(scope) << "'" << endl;
1006 des->errors += 1;
1007 return 0;
1011 * Handle the case that the identifier is a parameter reference. The
1012 * parameter expression has already been located for us (as the par
1013 * argument) so we just need to process the sub-expression.
1015 NetExpr* PEIdent::elaborate_expr_param(Design*des,
1016 NetScope*scope,
1017 const NetExpr*par,
1018 NetScope*found_in,
1019 const NetExpr*par_msb,
1020 const NetExpr*par_lsb) const
1022 NetExpr*tmp = par->dup_expr();
1024 const name_component_t&name_tail = path_.back();
1025 index_component_t::ctype_t use_sel = index_component_t::SEL_NONE;
1026 if (!name_tail.index.empty())
1027 use_sel = name_tail.index.back().sel;
1029 if (use_sel == index_component_t::SEL_PART) {
1030 ivl_assert(*this, !name_tail.index.empty());
1031 const index_component_t&index_tail = name_tail.index.back();
1032 ivl_assert(*this, index_tail.msb);
1033 ivl_assert(*this, index_tail.lsb);
1035 /* If the identifier has a part select, we support
1036 it by pulling the right bits out and making a
1037 sized unsigned constant. This code assumes the
1038 lsb of a parameter is 0 and the msb is the
1039 width of the parameter. */
1041 verinum*lsn = index_tail.lsb->eval_const(des, scope);
1042 verinum*msn = index_tail.msb->eval_const(des, scope);
1043 if ((lsn == 0) || (msn == 0)) {
1044 cerr << get_line() << ": error: "
1045 "Part select expressions must be "
1046 "constant expressions." << endl;
1047 des->errors += 1;
1048 return 0;
1051 long lsb = lsn->as_long();
1052 long msb = msn->as_long();
1053 if ((lsb < 0) || (msb < lsb)) {
1054 cerr << get_line() << ": error: invalid part "
1055 << "select: " << path_
1056 << "["<<msb<<":"<<lsb<<"]" << endl;
1057 des->errors += 1;
1058 return 0;
1060 unsigned long ulsb=lsb;
1061 unsigned long umsb=msb;
1063 NetEConst*le = dynamic_cast<NetEConst*>(tmp);
1064 assert(le);
1066 verinum result (verinum::V0, msb-lsb+1, true);
1067 verinum exl = le->value();
1069 /* Pull the bits from the parameter, one at a
1070 time. If the bit is within the range, simply
1071 copy it to the result. If the bit is outside
1072 the range, we sign extend signed unsized
1073 numbers, zero extend unsigned unsigned numbers,
1074 and X extend sized numbers. */
1075 for (unsigned long idx = ulsb ; idx <= umsb ; idx += 1) {
1076 if (idx < exl.len())
1077 result.set(idx-lsb, exl.get(idx));
1078 else if (exl.is_string())
1079 result.set(idx-lsb, verinum::V0);
1080 else if (exl.has_len())
1081 result.set(idx-lsb, verinum::Vx);
1082 else if (exl.has_sign())
1083 result.set(idx-lsb, exl.get(exl.len()-1));
1084 else
1085 result.set(idx-lsb, verinum::V0);
1088 /* If the input is a string, and the part select
1089 is working on byte boundaries, then the result
1090 can be made into a string. */
1091 if (exl.is_string()
1092 && (lsb%8 == 0)
1093 && (result.len()%8 == 0))
1094 result = verinum(result.as_string());
1096 delete tmp;
1097 tmp = new NetEConst(result);
1099 } else if (use_sel == index_component_t::SEL_IDX_UP || use_sel == index_component_t::SEL_IDX_DO) {
1101 ivl_assert(*this, !name_tail.index.empty());
1102 const index_component_t&index_tail = name_tail.index.back();
1103 ivl_assert(*this, index_tail.msb);
1104 ivl_assert(*this, index_tail.lsb);
1106 /* Get and evaluate the width of the index
1107 select. This must be constant. */
1108 NetExpr*wid_ex = elab_and_eval(des, scope, index_tail.lsb, -1);
1109 NetEConst*wid_ec = dynamic_cast<NetEConst*> (wid_ex);
1110 if (wid_ec == 0) {
1111 cerr << index_tail.lsb->get_line() << ": error: "
1112 << "Second expression of indexed part select "
1113 << "most be constant." << endl;
1114 des->errors += 1;
1115 return 0;
1118 unsigned wid = wid_ec->value().as_ulong();
1120 NetExpr*idx_ex = elab_and_eval(des, scope, index_tail.msb, -1);
1121 if (idx_ex == 0) {
1122 return 0;
1125 if (use_sel == index_component_t::SEL_IDX_DO && wid > 1) {
1126 idx_ex = make_add_expr(idx_ex, 1-(long)wid);
1130 /* Wrap the param expression with a part select. */
1131 tmp = new NetESelect(tmp, idx_ex, wid);
1134 } else if (use_sel == index_component_t::SEL_BIT) {
1135 ivl_assert(*this, !name_tail.index.empty());
1136 const index_component_t&index_tail = name_tail.index.back();
1137 ivl_assert(*this, index_tail.msb);
1138 ivl_assert(*this, !index_tail.lsb);
1140 /* Handle the case where a parameter has a bit
1141 select attached to it. Generate a NetESelect
1142 object to select the bit as desired. */
1143 NetExpr*mtmp = index_tail.msb->elaborate_expr(des, scope, -1,false);
1144 if (! dynamic_cast<NetEConst*>(mtmp)) {
1145 NetExpr*re = mtmp->eval_tree();
1146 if (re) {
1147 delete mtmp;
1148 mtmp = re;
1152 /* Let's first try to get constant values for both
1153 the parameter and the bit select. If they are
1154 both constant, then evaluate the bit select and
1155 return instead a single-bit constant. */
1157 NetEConst*le = dynamic_cast<NetEConst*>(tmp);
1158 NetEConst*re = dynamic_cast<NetEConst*>(mtmp);
1159 if (le && re) {
1161 /* Argument and bit select are constant. Calculate
1162 the final result. */
1163 verinum lv = le->value();
1164 verinum rv = re->value();
1165 verinum::V rb = verinum::Vx;
1167 long ridx = rv.as_long();
1168 if ((ridx >= 0) && ((unsigned long) ridx < lv.len())) {
1169 rb = lv[ridx];
1171 } else if ((ridx >= 0) && (!lv.has_len())) {
1172 if (lv.has_sign())
1173 rb = lv[lv.len()-1];
1174 else
1175 rb = verinum::V0;
1178 NetEConst*re = new NetEConst(verinum(rb, 1));
1179 delete tmp;
1180 delete mtmp;
1181 tmp = re;
1183 } else {
1185 const NetEConst*par_me =dynamic_cast<const NetEConst*>(par_msb);
1186 const NetEConst*par_le =dynamic_cast<const NetEConst*>(par_lsb);
1188 assert(par_me || !par_msb);
1189 assert(par_le || !par_lsb);
1190 assert(par_me || !par_le);
1192 if (par_me) {
1193 long par_mv = par_me->value().as_long();
1194 long par_lv = par_le->value().as_long();
1195 if (par_mv >= par_lv) {
1196 mtmp = par_lv
1197 ? make_add_expr(mtmp, 0-par_lv)
1198 : mtmp;
1199 } else {
1200 if (par_lv != 0)
1201 mtmp = make_add_expr(mtmp, 0-par_mv);
1202 mtmp = make_sub_expr(par_lv-par_mv, mtmp);
1206 /* The value is constant, but the bit select
1207 expression is not. Elaborate a NetESelect to
1208 evaluate the select at run-time. */
1210 NetESelect*stmp = new NetESelect(tmp, mtmp, 1);
1211 tmp->set_line(*this);
1212 tmp = stmp;
1215 } else {
1216 /* No bit or part select. Make the constant into a
1217 NetEConstParam if possible. */
1218 NetEConst*ctmp = dynamic_cast<NetEConst*>(tmp);
1219 if (ctmp != 0) {
1220 perm_string name = peek_tail_name(path_);
1221 NetEConstParam*ptmp
1222 = new NetEConstParam(found_in, name, ctmp->value());
1223 delete tmp;
1224 tmp = ptmp;
1228 tmp->set_line(*this);
1229 return tmp;
1233 * Handle word selects of vector arrays.
1235 NetExpr* PEIdent::elaborate_expr_net_word_(Design*des, NetScope*scope,
1236 NetNet*net, NetScope*found_in,
1237 bool sys_task_arg) const
1239 const name_component_t&name_tail = path_.back();
1241 if (name_tail.index.empty() && !sys_task_arg) {
1242 cerr << get_line() << ": error: Array " << path()
1243 << " Needs an array index here." << endl;
1244 des->errors += 1;
1245 return 0;
1248 index_component_t index_front;
1249 if (! name_tail.index.empty()) {
1250 index_front = name_tail.index.front();
1251 ivl_assert(*this, index_front.sel != index_component_t::SEL_NONE);
1252 if (index_front.sel != index_component_t::SEL_BIT) {
1253 cerr << get_line() << ": error: Array " << path_
1254 << " cannot be indexed by a range." << endl;
1255 des->errors += 1;
1256 return 0;
1258 ivl_assert(*this, index_front.msb);
1259 ivl_assert(*this, !index_front.lsb);
1262 NetExpr*word_index = index_front.sel == index_component_t::SEL_NONE
1264 : elab_and_eval(des, scope, index_front.msb, -1);
1265 if (word_index == 0 && !sys_task_arg)
1266 return 0;
1268 if (NetEConst*word_addr = dynamic_cast<NetEConst*>(word_index)) {
1269 long addr = word_addr->value().as_long();
1271 // Special case: The index is out of range, so the value
1272 // of this expression is a 'bx vector the width of a word.
1273 if (!net->array_index_is_valid(addr)) {
1274 verinum xxx (verinum::Vx, net->vector_width());
1275 NetEConst*resx = new NetEConst(xxx);
1276 resx->set_line(*this);
1277 delete word_index;
1278 return resx;
1281 // Recalculate the constant address with the adjusted base.
1282 unsigned use_addr = net->array_index_to_address(addr);
1283 if (addr < 0 || use_addr != (unsigned long)addr) {
1284 verinum val (use_addr, 8*sizeof(use_addr));
1285 NetEConst*tmp = new NetEConst(val);
1286 tmp->set_line(*this);
1287 delete word_index;
1288 word_index = tmp;
1291 } else if (word_index) {
1292 // If there is a non-zero base to the memory, then build an
1293 // expression to calculate the canonical address.
1294 if (long base = net->array_first()) {
1296 word_index = make_add_expr(word_index, 0-base);
1297 if (NetExpr*tmp = word_index->eval_tree()) {
1298 word_index = tmp;
1303 NetESignal*res = new NetESignal(net, word_index);
1304 res->set_line(*this);
1306 // Detect that the word has a bit/part select as well.
1308 index_component_t::ctype_t word_sel = index_component_t::SEL_NONE;
1309 if (name_tail.index.size() > 1)
1310 word_sel = name_tail.index.back().sel;
1312 if (word_sel == index_component_t::SEL_PART)
1313 return elaborate_expr_net_part_(des, scope, res, found_in);
1315 if (word_sel == index_component_t::SEL_IDX_UP)
1316 return elaborate_expr_net_idx_up_(des, scope, res, found_in);
1318 if (word_sel == index_component_t::SEL_IDX_DO)
1319 return elaborate_expr_net_idx_do_(des, scope, res, found_in);
1321 if (word_sel == index_component_t::SEL_BIT)
1322 return elaborate_expr_net_bit_(des, scope, res, found_in);
1324 ivl_assert(*this, word_sel == index_component_t::SEL_NONE);
1325 return res;
1329 * Handle part selects of NetNet identifiers.
1331 NetExpr* PEIdent::elaborate_expr_net_part_(Design*des, NetScope*scope,
1332 NetESignal*net, NetScope*found_in) const
1334 long msv, lsv;
1335 bool flag = calculate_parts_(des, scope, msv, lsv);
1336 if (!flag)
1337 return 0;
1339 /* The indices of part selects are signed integers, so allow
1340 negative values. However, the width that they represent is
1341 unsigned. Remember that any order is possible,
1342 i.e., [1:0], [-4:6], etc. */
1343 unsigned long wid = 1 + ((msv>lsv)? (msv-lsv) : (lsv-msv));
1344 if (wid > net->vector_width()) {
1345 cerr << get_line() << ": error: part select ["
1346 << msv << ":" << lsv << "] out of range." << endl;
1347 des->errors += 1;
1348 //delete lsn;
1349 //delete msn;
1350 return net;
1352 ivl_assert(*this, wid <= net->vector_width());
1354 if (net->sig()->sb_to_idx(msv) < net->sig()->sb_to_idx(lsv)) {
1355 cerr << get_line() << ": error: part select ["
1356 << msv << ":" << lsv << "] out of order." << endl;
1357 des->errors += 1;
1358 //delete lsn;
1359 //delete msn;
1360 return net;
1364 if (net->sig()->sb_to_idx(msv) >= net->vector_width()) {
1365 cerr << get_line() << ": error: part select ["
1366 << msv << ":" << lsv << "] out of range." << endl;
1367 des->errors += 1;
1368 //delete lsn;
1369 //delete msn;
1370 return net;
1373 // If the part select convers exactly the entire
1374 // vector, then do not bother with it. Return the
1375 // signal itself.
1376 if (net->sig()->sb_to_idx(lsv) == 0 && wid == net->vector_width())
1377 return net;
1379 NetExpr*ex = new NetEConst(verinum(net->sig()->sb_to_idx(lsv)));
1380 NetESelect*ss = new NetESelect(net, ex, wid);
1381 ss->set_line(*this);
1383 return ss;
1387 * Part select indexed up, i.e. net[<m> +: <l>]
1389 NetExpr* PEIdent::elaborate_expr_net_idx_up_(Design*des, NetScope*scope,
1390 NetESignal*net, NetScope*found_in) const
1392 const name_component_t&name_tail = path_.back();
1393 ivl_assert(*this, !name_tail.index.empty());
1395 const index_component_t&index_tail = name_tail.index.back();
1396 ivl_assert(*this, index_tail.lsb != 0);
1397 ivl_assert(*this, index_tail.msb != 0);
1399 NetExpr*base = elab_and_eval(des, scope, index_tail.msb, -1);
1401 unsigned long wid = 0;
1402 calculate_up_do_width_(des, scope, wid);
1405 // Handle the special case that the base is constant as
1406 // well. In this case it can be converted to a conventional
1407 // part select.
1408 if (NetEConst*base_c = dynamic_cast<NetEConst*> (base)) {
1409 long lsv = base_c->value().as_long();
1411 // If the part select convers exactly the entire
1412 // vector, then do not bother with it. Return the
1413 // signal itself.
1414 if (net->sig()->sb_to_idx(lsv) == 0 && wid == net->vector_width())
1415 return net;
1417 // Otherwise, make a part select that covers the right range.
1418 NetExpr*ex = new NetEConst(verinum(net->sig()->sb_to_idx(lsv)));
1419 NetESelect*ss = new NetESelect(net, ex, wid);
1420 ss->set_line(*this);
1422 delete base;
1423 return ss;
1426 if (net->msi() > net->lsi()) {
1427 if (long offset = net->lsi())
1428 base = make_add_expr(base, 0-offset);
1429 } else {
1430 long vwid = net->lsi() - net->msi() + 1;
1431 long offset = net->msi();
1432 base = make_sub_expr(vwid-offset-wid, base);
1435 NetESelect*ss = new NetESelect(net, base, wid);
1436 ss->set_line(*this);
1438 if (debug_elaborate) {
1439 cerr << get_line() << ": debug: Elaborate part "
1440 << "select base="<< *base << ", wid="<< wid << endl;
1443 return ss;
1447 * Part select indexed down, i.e. net[<m> -: <l>]
1449 NetExpr* PEIdent::elaborate_expr_net_idx_do_(Design*des, NetScope*scope,
1450 NetESignal*net, NetScope*found_in)const
1452 const name_component_t&name_tail = path_.back();
1453 ivl_assert(*this, ! name_tail.index.empty());
1455 const index_component_t&index_tail = name_tail.index.back();
1456 ivl_assert(*this, index_tail.lsb != 0);
1457 ivl_assert(*this, index_tail.msb != 0);
1459 NetExpr*base = elab_and_eval(des, scope, index_tail.msb, -1);
1461 unsigned long wid = 0;
1462 calculate_up_do_width_(des, scope, wid);
1464 // Handle the special case that the base is constant as
1465 // well. In this case it can be converted to a conventional
1466 // part select.
1467 if (NetEConst*base_c = dynamic_cast<NetEConst*> (base)) {
1468 long lsv = base_c->value().as_long();
1470 // If the part select convers exactly the entire
1471 // vector, then do not bother with it. Return the
1472 // signal itself.
1473 if (net->sig()->sb_to_idx(lsv) == (wid-1) && wid == net->vector_width())
1474 return net;
1476 // Otherwise, make a part select that covers the right range.
1477 NetExpr*ex = new NetEConst(verinum(net->sig()->sb_to_idx(lsv)-wid+1));
1478 NetESelect*ss = new NetESelect(net, ex, wid);
1479 ss->set_line(*this);
1481 delete base;
1482 return ss;
1485 long offset = net->lsi();
1486 NetExpr*base_adjusted = wid > 1
1487 ? make_add_expr(base,1-(long)wid-offset)
1488 : (offset == 0? base : make_add_expr(base, 0-offset));
1489 NetESelect*ss = new NetESelect(net, base_adjusted, wid);
1490 ss->set_line(*this);
1492 if (debug_elaborate) {
1493 cerr << get_line() << ": debug: Elaborate part "
1494 << "select base="<< *base_adjusted << ", wid="<< wid << endl;
1497 return ss;
1500 NetExpr* PEIdent::elaborate_expr_net_bit_(Design*des, NetScope*scope,
1501 NetESignal*net, NetScope*found_in) const
1503 const name_component_t&name_tail = path_.back();
1504 ivl_assert(*this, !name_tail.index.empty());
1506 const index_component_t&index_tail = name_tail.index.back();
1507 ivl_assert(*this, index_tail.msb != 0);
1508 ivl_assert(*this, index_tail.lsb == 0);
1510 NetExpr*ex = elab_and_eval(des, scope, index_tail.msb, -1);
1512 // If the bit select is constant, then treat it similar
1513 // to the part select, so that I save the effort of
1514 // making a mux part in the netlist.
1515 if (NetEConst*msc = dynamic_cast<NetEConst*> (ex)) {
1516 long msv = msc->value().as_long();
1517 unsigned idx = net->sig()->sb_to_idx(msv);
1519 if (idx >= net->vector_width()) {
1520 /* The bit select is out of range of the
1521 vector. This is legal, but returns a
1522 constant 1'bx value. */
1523 verinum x (verinum::Vx);
1524 NetEConst*tmp = new NetEConst(x);
1525 tmp->set_line(*this);
1527 cerr << get_line() << ": warning: Bit select ["
1528 << msv << "] out of range of vector "
1529 << net->name() << "[" << net->sig()->msb()
1530 << ":" << net->sig()->lsb() << "]." << endl;
1531 cerr << get_line() << ": : Replacing "
1532 << "expression with a constant 1'bx." << endl;
1533 delete ex;
1534 return tmp;
1537 // If the vector is only one bit, we are done. The
1538 // bit select will return the scaler itself.
1539 if (net->vector_width() == 1)
1540 return net;
1542 // Make an expression out of the index
1543 NetEConst*idx_c = new NetEConst(verinum(idx));
1544 idx_c->set_line(*net);
1546 // Make a bit select with the canonical index
1547 NetESelect*res = new NetESelect(net, idx_c, 1);
1548 res->set_line(*net);
1550 return res;
1553 // Non-constant bit select? punt and make a subsignal
1554 // device to mux the bit in the net. This is a fairly
1555 // complicated task because we need to generate
1556 // expressions to convert calculated bit select
1557 // values to canonical values that are used internally.
1559 if (net->sig()->msb() < net->sig()->lsb()) {
1560 ex = make_sub_expr(net->sig()->lsb(), ex);
1561 } else {
1562 ex = make_add_expr(ex, - net->sig()->lsb());
1565 NetESelect*ss = new NetESelect(net, ex, 1);
1566 ss->set_line(*this);
1567 return ss;
1570 NetExpr* PEIdent::elaborate_expr_net(Design*des, NetScope*scope,
1571 NetNet*net, NetScope*found_in,
1572 bool sys_task_arg) const
1574 if (net->array_dimensions() > 0)
1575 return elaborate_expr_net_word_(des, scope, net, found_in, sys_task_arg);
1577 NetESignal*node = new NetESignal(net);
1578 node->set_line(*this);
1580 index_component_t::ctype_t use_sel = index_component_t::SEL_NONE;
1581 if (! path_.back().index.empty())
1582 use_sel = path_.back().index.back().sel;
1584 // If this is a part select of a signal, then make a new
1585 // temporary signal that is connected to just the
1586 // selected bits. The lsb_ and msb_ expressions are from
1587 // the foo[msb:lsb] expression in the original.
1588 if (use_sel == index_component_t::SEL_PART)
1589 return elaborate_expr_net_part_(des, scope, node, found_in);
1591 if (use_sel == index_component_t::SEL_IDX_UP)
1592 return elaborate_expr_net_idx_up_(des, scope, node, found_in);
1594 if (use_sel == index_component_t::SEL_IDX_DO)
1595 return elaborate_expr_net_idx_do_(des, scope, node, found_in);
1597 if (use_sel == index_component_t::SEL_BIT)
1598 return elaborate_expr_net_bit_(des, scope, node, found_in);
1600 // It's not anything else, so this must be a simple identifier
1601 // expression with no part or bit select. Return the signal
1602 // itself as the expression.
1603 assert(use_sel == index_component_t::SEL_NONE);
1605 return node;
1608 unsigned PENumber::test_width(Design*, NetScope*,
1609 unsigned min, unsigned lval, bool&unsized_flag) const
1611 unsigned use_wid = value_->len();
1612 if (min > use_wid)
1613 use_wid = min;
1615 if (! value_->has_len())
1616 unsized_flag = true;
1618 return use_wid;
1621 NetEConst* PENumber::elaborate_expr(Design*des, NetScope*,
1622 int expr_width, bool) const
1624 assert(value_);
1625 verinum tvalue = *value_;
1627 // If the expr_width is >0, then the context is requesting a
1628 // specific size (for example this is part of the r-values of
1629 // an assignment) so we pad to the desired width and ignore
1630 // the self-determined size.
1631 if (expr_width > 0) {
1632 tvalue = pad_to_width(tvalue, expr_width);
1635 NetEConst*tmp = new NetEConst(tvalue);
1636 tmp->set_line(*this);
1637 return tmp;
1640 unsigned PEString::test_width(Design*des, NetScope*scope,
1641 unsigned min, unsigned lval,
1642 bool&unsized_flag) const
1644 unsigned use_wid = text_? 8*strlen(text_) : 0;
1645 if (min > use_wid)
1646 use_wid = min;
1648 return use_wid;
1651 NetEConst* PEString::elaborate_expr(Design*des, NetScope*,
1652 int expr_width, bool) const
1654 NetEConst*tmp = new NetEConst(value());
1655 tmp->set_line(*this);
1656 return tmp;
1659 unsigned PETernary::test_width(Design*des, NetScope*scope,
1660 unsigned min, unsigned lval,
1661 bool&flag) const
1663 unsigned tru_wid = tru_->test_width(des, scope, min, lval, flag);
1664 unsigned fal_wid = fal_->test_width(des, scope, min, lval, flag);
1665 return max(tru_wid,fal_wid);
1668 static bool test_ternary_operand_compat(ivl_variable_type_t l,
1669 ivl_variable_type_t r)
1671 if (l == IVL_VT_LOGIC && r == IVL_VT_BOOL)
1672 return true;
1673 if (l == IVL_VT_BOOL && r == IVL_VT_LOGIC)
1674 return true;
1675 if (l == r)
1676 return true;
1678 return false;
1682 * Elaborate the Ternary operator. I know that the expressions were
1683 * parsed so I can presume that they exist, and call elaboration
1684 * methods. If any elaboration fails, then give up and return 0.
1686 NetETernary*PETernary::elaborate_expr(Design*des, NetScope*scope,
1687 int expr_wid, bool) const
1689 assert(expr_);
1690 assert(tru_);
1691 assert(fal_);
1693 if (expr_wid < 0) {
1694 bool flag = false;
1695 unsigned tru_wid = tru_->test_width(des, scope, 0, 0, flag);
1696 unsigned fal_wid = fal_->test_width(des, scope, 0, 0, flag);
1697 expr_wid = max(tru_wid, fal_wid);
1698 if (debug_elaborate)
1699 cerr << get_line() << ": debug: "
1700 << "Self-sized ternary chooses wid="<< expr_wid
1701 << " from " <<tru_wid
1702 << " and " << fal_wid << endl;
1705 NetExpr*con = expr_->elaborate_expr(des, scope, -1, false);
1706 if (con == 0)
1707 return 0;
1709 NetExpr*tru = tru_->elaborate_expr(des, scope, expr_wid, false);
1710 if (tru == 0) {
1711 delete con;
1712 return 0;
1715 NetExpr*fal = fal_->elaborate_expr(des, scope, expr_wid, false);
1716 if (fal == 0) {
1717 delete con;
1718 delete tru;
1719 return 0;
1722 if (! test_ternary_operand_compat(tru->expr_type(), fal->expr_type())) {
1723 cerr << get_line() << ": error: Data types "
1724 << tru->expr_type() << " and "
1725 << fal->expr_type() << " of ternary"
1726 << " do not match." << endl;
1727 des->errors += 1;
1728 return 0;
1731 /* Whatever the width we choose for the ternary operator, we
1732 need to make sure the operands match. */
1733 tru = pad_to_width(tru, expr_wid);
1734 fal = pad_to_width(fal, expr_wid);
1736 NetETernary*res = new NetETernary(con, tru, fal);
1737 res->set_line(*this);
1738 return res;
1741 NetExpr* PEUnary::elaborate_expr(Design*des, NetScope*scope,
1742 int expr_wid, bool) const
1744 NetExpr*ip = expr_->elaborate_expr(des, scope, expr_wid, false);
1745 if (ip == 0) return 0;
1747 /* Should we evaluate expressions ahead of time,
1748 * just like in PEBinary::elaborate_expr() ?
1751 NetExpr*tmp;
1752 switch (op_) {
1753 default:
1754 tmp = new NetEUnary(op_, ip);
1755 tmp->set_line(*this);
1756 break;
1758 case '-':
1759 if (NetEConst*ipc = dynamic_cast<NetEConst*>(ip)) {
1761 verinum val = ipc->value();
1762 if (expr_wid > 0)
1763 val = pad_to_width(val, expr_wid);
1765 /* When taking the - of a number, turn it into a
1766 signed expression and extend it one bit to
1767 accommodate a possible sign bit. */
1768 verinum zero (verinum::V0, val.len()+1, val.has_len());
1769 verinum nval = zero - val;
1771 if (val.has_len())
1772 nval = verinum(nval, val.len());
1773 nval.has_sign(true);
1774 tmp = new NetEConst(nval);
1775 tmp->set_line(*this);
1776 delete ip;
1778 } else if (NetECReal*ipc = dynamic_cast<NetECReal*>(ip)) {
1780 /* When taking the - of a real, fold this into the
1781 constant value. */
1782 verireal val = - ipc->value();
1783 tmp = new NetECReal(val);
1784 tmp->set_line( *ip );
1785 delete ip;
1787 } else {
1788 tmp = new NetEUnary(op_, ip);
1789 tmp->set_line(*this);
1791 break;
1793 case '+':
1794 tmp = ip;
1795 break;
1797 case '!': // Logical NOT
1798 /* If the operand to unary ! is a constant, then I can
1799 evaluate this expression here and return a logical
1800 constant in its place. */
1801 if (NetEConst*ipc = dynamic_cast<NetEConst*>(ip)) {
1802 verinum val = ipc->value();
1803 unsigned v1 = 0;
1804 unsigned vx = 0;
1805 for (unsigned idx = 0 ; idx < val.len() ; idx += 1)
1806 switch (val[idx]) {
1807 case verinum::V0:
1808 break;
1809 case verinum::V1:
1810 v1 += 1;
1811 break;
1812 default:
1813 vx += 1;
1814 break;
1817 verinum::V res;
1818 if (v1 > 0)
1819 res = verinum::V0;
1820 else if (vx > 0)
1821 res = verinum::Vx;
1822 else
1823 res = verinum::V1;
1825 verinum vres (res, 1, true);
1826 tmp = new NetEConst(vres);
1827 tmp->set_line(*this);
1828 delete ip;
1829 } else {
1830 tmp = new NetEUReduce(op_, ip);
1831 tmp->set_line(*this);
1833 break;
1835 case '&': // Reduction AND
1836 case '|': // Reduction OR
1837 case '^': // Reduction XOR
1838 case 'A': // Reduction NAND (~&)
1839 case 'N': // Reduction NOR (~|)
1840 case 'X': // Reduction NXOR (~^)
1841 tmp = new NetEUReduce(op_, ip);
1842 tmp->set_line(*this);
1843 break;
1845 case '~':
1846 tmp = new NetEUBits(op_, ip);
1847 tmp->set_line(*this);
1848 break;
1851 return tmp;