Merge branch 'master' into verilog-ams
[sverilog.git] / elab_scope.cc
blob04cce66260fbb66487a1b262f2143b29c6c7e0d9
1 /*
2 * Copyright (c) 2000-2008 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"
22 # include "netmisc.h"
23 # include <cstring>
24 # include <iostream>
25 # include <cstdlib>
26 # include <stdio.h>
29 * Elaboration happens in two passes, generally. The first scans the
30 * pform to generate the NetScope tree and attach it to the Design
31 * object. The methods in this source file implement the elaboration
32 * of the scopes.
35 # include "Module.h"
36 # include "PEvent.h"
37 # include "PExpr.h"
38 # include "PGate.h"
39 # include "PGenerate.h"
40 # include "PTask.h"
41 # include "PWire.h"
42 # include "Statement.h"
43 # include "netlist.h"
44 # include "util.h"
45 # include <typeinfo>
46 # include <assert.h>
47 # include "ivl_assert.h"
49 void Module::elaborate_parm_item_(perm_string name, const param_expr_t&cur,
50 Design*des, NetScope*scope)
52 PExpr*ex = cur.expr;
53 assert(ex);
55 NetExpr*val = ex->elaborate_pexpr(des, scope);
56 if (val == 0) return;
57 NetExpr*msb = 0;
58 NetExpr*lsb = 0;
59 bool signed_flag = cur.signed_flag;
61 /* If the parameter declaration includes msb and lsb,
62 then use them to calculate a width for the
63 result. Then make sure the constant expression of the
64 parameter value is coerced to have the correct
65 and defined width. */
66 if (cur.msb) {
67 assert(cur.lsb);
68 msb = cur.msb ->elaborate_pexpr(des, scope);
69 assert(msb);
70 lsb = cur.lsb ->elaborate_pexpr(des, scope);
71 assert(lsb);
74 if (signed_flag) {
75 /* If explicitly signed, then say so. */
76 val->cast_signed(true);
77 } else if (cur.msb) {
78 /* If there is a range, then the signedness comes
79 from the type and not the expression. */
80 val->cast_signed(signed_flag);
81 } else {
82 /* otherwise, let the expression describe
83 itself. */
84 signed_flag = val->has_sign();
87 NetScope::range_t*range_list = 0;
88 for (Module::range_t*range = cur.range ; range ; range = range->next) {
89 NetScope::range_t*tmp = new NetScope::range_t;
90 tmp->exclude_flag = range->exclude_flag;
91 tmp->low_open_flag = range->low_open_flag;
92 tmp->high_open_flag = range->high_open_flag;
94 if (range->low_expr) {
95 tmp->low_expr = elab_and_eval(des, scope, range->low_expr, -1);
96 ivl_assert(*range->low_expr, tmp->low_expr);
97 } else {
98 tmp->low_expr = 0;
101 if (range->high_expr) {
102 tmp->high_expr = elab_and_eval(des, scope, range->high_expr, -1);
103 ivl_assert(*range->high_expr, tmp->high_expr);
104 } else {
105 tmp->high_expr = 0;
108 tmp->next = range_list;
109 range_list = tmp;
112 val = scope->set_parameter(name, val, cur.type, msb, lsb, signed_flag, range_list, cur);
113 assert(val);
114 delete val;
117 bool Module::elaborate_scope(Design*des, NetScope*scope,
118 const replace_t&replacements) const
120 if (debug_scopes) {
121 cerr << get_fileline() << ": debug: Elaborate scope "
122 << scope_path(scope) << "." << endl;
125 // Generate all the parameters that this instance of this
126 // module introduces to the design. This loop elaborates the
127 // parameters, but doesn't evaluate references to
128 // parameters. This scan practically locates all the
129 // parameters and puts them in the parameter table in the
130 // design.
132 // No expressions are evaluated, yet. For now, leave them in
133 // the pform and just place a NetEParam placeholder in the
134 // place of the elaborated expression.
136 typedef map<perm_string,param_expr_t>::const_iterator mparm_it_t;
137 typedef map<pform_name_t,PExpr*>::const_iterator pform_parm_it_t;
140 // This loop scans the parameters in the module, and creates
141 // stub parameter entries in the scope for the parameter name.
143 for (mparm_it_t cur = parameters.begin()
144 ; cur != parameters.end() ; cur ++) {
146 NetEParam*tmp = new NetEParam;
147 tmp->set_line(*((*cur).second.expr));
148 tmp->cast_signed( (*cur).second.signed_flag );
150 scope->set_parameter((*cur).first, tmp, (*cur).second.type,
151 0, 0, false, 0, (*cur).second);
154 for (mparm_it_t cur = localparams.begin()
155 ; cur != localparams.end() ; cur ++) {
157 NetEParam*tmp = new NetEParam;
158 tmp->set_line(*((*cur).second.expr));
159 if ((*cur).second.msb)
160 tmp->cast_signed( (*cur).second.signed_flag );
162 scope->set_parameter((*cur).first, tmp, (*cur).second.type,
163 0, 0, false, 0, (*cur).second);
167 // Now scan the parameters again, this time elaborating them
168 // for use as parameter values. This is after the previous
169 // scan so that local parameter names can be used in the
170 // r-value expressions.
172 for (mparm_it_t cur = parameters.begin()
173 ; cur != parameters.end() ; cur ++) {
175 elaborate_parm_item_((*cur).first, (*cur).second, des, scope);
178 /* run parameter replacements that were collected from the
179 containing scope and meant for me. */
180 for (replace_t::const_iterator cur = replacements.begin()
181 ; cur != replacements.end() ; cur ++) {
183 NetExpr*val = (*cur).second;
184 if (val == 0) {
185 cerr << get_fileline() << ": internal error: "
186 << "Missing expression in parameter replacement for "
187 << (*cur).first;
189 assert(val);
190 if (debug_scopes) {
191 cerr << get_fileline() << ": debug: "
192 << "Replace " << (*cur).first
193 << " with expression " << *val
194 << " from " << val->get_fileline() << "." << endl;
195 cerr << get_fileline() << ": : "
196 << "Type=" << val->expr_type() << endl;
198 bool flag = scope->replace_parameter((*cur).first, val);
199 if (! flag) {
200 cerr << val->get_fileline() << ": warning: parameter "
201 << (*cur).first << " not found in "
202 << scope_path(scope) << "." << endl;
206 for (mparm_it_t cur = localparams.begin()
207 ; cur != localparams.end() ; cur ++) {
209 elaborate_parm_item_((*cur).first, (*cur).second, des, scope);
212 // Run through the defparams for this module, elaborate the
213 // expressions in this context and save the result is a table
214 // for later final override.
216 // It is OK to elaborate the expressions of the defparam here
217 // because Verilog requires that the expressions only use
218 // local parameter names. It is *not* OK to do the override
219 // here because the parameter receiving the assignment may be
220 // in a scope not discovered by this pass.
222 for (pform_parm_it_t cur = defparms.begin()
223 ; cur != defparms.end() ; cur ++ ) {
225 PExpr*ex = (*cur).second;
226 assert(ex);
228 NetExpr*val = ex->elaborate_pexpr(des, scope);
229 if (val == 0) continue;
230 scope->defparams[(*cur).first] = val;
233 // Evaluate the attributes. Evaluate them in the scope of the
234 // module that the attribute is attached to. Is this correct?
235 unsigned nattr;
236 attrib_list_t*attr = evaluate_attributes(attributes, nattr, des, scope);
238 for (unsigned idx = 0 ; idx < nattr ; idx += 1)
239 scope->attribute(attr[idx].key, attr[idx].val);
241 delete[]attr;
243 // Generate schemes can create new scopes in the form of
244 // generated code. Scan the generate schemes, and *generate*
245 // new scopes, which is slightly different from simple
246 // elaboration.
248 typedef list<PGenerate*>::const_iterator generate_it_t;
249 for (generate_it_t cur = generate_schemes.begin()
250 ; cur != generate_schemes.end() ; cur ++ ) {
251 (*cur) -> generate_scope(des, scope);
255 // Tasks introduce new scopes, so scan the tasks in this
256 // module. Create a scope for the task and pass that to the
257 // elaborate_scope method of the PTask for detailed
258 // processing.
260 typedef map<perm_string,PTask*>::const_iterator tasks_it_t;
262 for (tasks_it_t cur = tasks_.begin()
263 ; cur != tasks_.end() ; cur ++ ) {
265 hname_t use_name( (*cur).first );
266 if (scope->child(use_name)) {
267 cerr << get_fileline() << ": error: task/scope name "
268 << use_name << " already used in this context."
269 << endl;
270 des->errors += 1;
271 continue;
273 NetScope*task_scope = new NetScope(scope, use_name,
274 NetScope::TASK);
275 task_scope->set_line((*cur).second);
276 (*cur).second->elaborate_scope(des, task_scope);
280 // Functions are very similar to tasks, at least from the
281 // perspective of scopes. So handle them exactly the same
282 // way.
284 typedef map<perm_string,PFunction*>::const_iterator funcs_it_t;
286 for (funcs_it_t cur = funcs_.begin()
287 ; cur != funcs_.end() ; cur ++ ) {
289 hname_t use_name( (*cur).first );
290 if (scope->child(use_name)) {
291 cerr << get_fileline() << ": error: function/scope name "
292 << use_name << " already used in this context."
293 << endl;
294 des->errors += 1;
295 continue;
297 NetScope*func_scope = new NetScope(scope, use_name,
298 NetScope::FUNC);
299 func_scope->set_line((*cur).second);
300 (*cur).second->elaborate_scope(des, func_scope);
304 // Gates include modules, which might introduce new scopes, so
305 // scan all of them to create those scopes.
307 typedef list<PGate*>::const_iterator gates_it_t;
308 for (gates_it_t cur = gates_.begin()
309 ; cur != gates_.end() ; cur ++ ) {
311 (*cur) -> elaborate_scope(des, scope);
315 // initial and always blocks may contain begin-end and
316 // fork-join blocks that can introduce scopes. Therefore, I
317 // get to scan processes here.
319 typedef list<PProcess*>::const_iterator proc_it_t;
321 for (proc_it_t cur = behaviors.begin()
322 ; cur != behaviors.end() ; cur ++ ) {
324 (*cur) -> statement() -> elaborate_scope(des, scope);
327 // Scan through all the named events in this scope. We do not
328 // need anything more than the current scope to do this
329 // elaboration, so do it now. This allows for normal
330 // elaboration to reference these events.
332 for (map<perm_string,PEvent*>::const_iterator et = events.begin()
333 ; et != events.end() ; et ++ ) {
335 (*et).second->elaborate_scope(des, scope);
338 return des->errors == 0;
341 bool PGenerate::generate_scope(Design*des, NetScope*container)
343 switch (scheme_type) {
344 case GS_LOOP:
345 return generate_scope_loop_(des, container);
347 case GS_CONDIT:
348 return generate_scope_condit_(des, container, false);
350 case GS_ELSE:
351 return generate_scope_condit_(des, container, true);
353 case GS_CASE:
354 return generate_scope_case_(des, container);
355 return true;
357 case GS_CASE_ITEM:
358 cerr << get_fileline() << ": internal error: "
359 << "Case item outside of a case generate scheme?" << endl;
360 return false;
362 default:
363 cerr << get_fileline() << ": sorry: Generate of this sort"
364 << " is not supported yet!" << endl;
365 return false;
370 * This is the elaborate scope method for a generate loop.
372 bool PGenerate::generate_scope_loop_(Design*des, NetScope*container)
374 // Check that the loop_index variable was declared in a
375 // genvar statement.
377 // MISSING CODE!
379 // We're going to need a genvar...
380 int genvar;
382 // The initial value for the genvar does not need (nor can it
383 // use) the genvar itself, so we can evaluate this expression
384 // the same way any other parameter value is evaluated.
385 NetExpr*init_ex = elab_and_eval(des, container, loop_init, -1);
386 NetEConst*init = dynamic_cast<NetEConst*> (init_ex);
387 if (init == 0) {
388 cerr << get_fileline() << ": error: Cannot evaluate genvar"
389 << " init expression: " << *loop_init << endl;
390 des->errors += 1;
391 return false;
394 // Since we will be adding the genvar value as a local parameter
395 // to each instances scope. We need to make sure a parameter does
396 // not already exist.
397 const NetExpr*tmsb;
398 const NetExpr*tlsb;
399 const NetExpr*texpr = container->get_parameter(loop_index, tmsb, tlsb);
400 if (texpr != 0) {
401 cerr << get_fileline() << ": error: Cannot have a genvar "
402 << "and parameter with the same name: " << loop_index << endl;
403 des->errors += 1;
404 return false;
407 genvar = init->value().as_long();
408 delete init_ex;
410 if (debug_scopes)
411 cerr << get_fileline() << ": debug: genvar init = " << genvar << endl;
412 container->genvar_tmp = loop_index;
413 container->genvar_tmp_val = genvar;
414 NetExpr*test_ex = elab_and_eval(des, container, loop_test, -1);
415 NetEConst*test = dynamic_cast<NetEConst*>(test_ex);
416 assert(test);
417 while (test->value().as_long()) {
419 // The actual name of the scope includes the genvar so
420 // that each instance has a unique name in the
421 // container. The format of using [] is part of the
422 // Verilog standard.
423 hname_t use_name (scope_name, genvar);
424 if (container->child(use_name)) {
425 cerr << get_fileline() << ": error: block/scope name "
426 << use_name << " already used in this context."
427 << endl;
428 des->errors += 1;
429 return false;
431 if (debug_scopes)
432 cerr << get_fileline() << ": debug: "
433 << "Create generated scope " << use_name << endl;
435 NetScope*scope = new NetScope(container, use_name,
436 NetScope::GENBLOCK);
437 scope->set_line(get_file(), get_lineno());
439 // Set in the scope a localparam for the value of the
440 // genvar within this instance of the generate
441 // block. Code within this scope thus has access to the
442 // genvar as a constant.
444 verinum genvar_verinum(genvar);
445 genvar_verinum.has_sign(true);
446 NetEConstParam*gp = new NetEConstParam(scope,
447 loop_index,
448 genvar_verinum);
449 // The file and line information should really come
450 // from the genvar statement, not the for loop.
451 scope->set_localparam(loop_index, gp, *this);
452 if (debug_scopes)
453 cerr << get_fileline() << ": debug: "
454 << "Create implicit localparam "
455 << loop_index << " = " << genvar_verinum << endl;
458 elaborate_subscope_(des, scope);
460 // Calculate the step for the loop variable.
461 NetExpr*step_ex = elab_and_eval(des, container, loop_step, -1);
462 NetEConst*step = dynamic_cast<NetEConst*>(step_ex);
463 assert(step);
464 if (debug_scopes)
465 cerr << get_fileline() << ": debug: genvar step from "
466 << genvar << " to " << step->value().as_long() << endl;
468 genvar = step->value().as_long();
469 container->genvar_tmp_val = genvar;
470 delete step;
471 delete test_ex;
472 test_ex = elab_and_eval(des, container, loop_test, -1);
473 test = dynamic_cast<NetEConst*>(test_ex);
474 assert(test);
477 // Clear the genvar_tmp field in the scope to reflect that the
478 // genvar is no longer value for evaluating expressions.
479 container->genvar_tmp = perm_string();
481 return true;
484 bool PGenerate::generate_scope_condit_(Design*des, NetScope*container, bool else_flag)
486 NetExpr*test_ex = elab_and_eval(des, container, loop_test, -1);
487 NetEConst*test = dynamic_cast<NetEConst*> (test_ex);
488 assert(test);
490 // If the condition evaluates as false, then do not create the
491 // scope.
492 if ( (test->value().as_long() == 0 && !else_flag)
493 || (test->value().as_long() != 0 && else_flag) ) {
494 if (debug_scopes)
495 cerr << get_fileline() << ": debug: Generate condition "
496 << (else_flag? "(else)" : "(if)")
497 << " value=" << test->value() << ": skip generation"
498 << endl;
499 delete test_ex;
500 return true;
503 hname_t use_name (scope_name);
504 if (container->child(use_name)) {
505 cerr << get_fileline() << ": error: block/scope name "
506 << scope_name << " already used in this context."
507 << endl;
508 des->errors += 1;
509 return false;
511 if (debug_scopes)
512 cerr << get_fileline() << ": debug: Generate condition "
513 << (else_flag? "(else)" : "(if)")
514 << " value=" << test->value() << ": Generate scope="
515 << use_name << endl;
517 NetScope*scope = new NetScope(container, use_name,
518 NetScope::GENBLOCK);
519 scope->set_line(get_file(), get_lineno());
521 elaborate_subscope_(des, scope);
523 return true;
526 bool PGenerate::generate_scope_case_(Design*des, NetScope*container)
528 NetExpr*case_value_ex = elab_and_eval(des, container, loop_test, -1);
529 NetEConst*case_value_co = dynamic_cast<NetEConst*>(case_value_ex);
530 assert(case_value_co);
532 // The name of the scope to generate, whatever that item is.
533 hname_t use_name (scope_name);
535 if (debug_scopes)
536 cerr << get_fileline() << ": debug: Generate case "
537 << "switch value=" << case_value_co->value() << endl;
539 PGenerate*default_item = 0;
541 typedef list<PGenerate*>::const_iterator generator_it_t;
542 generator_it_t cur = generates.begin();
543 while (cur != generates.end()) {
544 PGenerate*item = *cur;
545 assert( item->scheme_type == PGenerate::GS_CASE_ITEM );
547 // Detect that the item is a default.
548 if (item->loop_test == 0) {
549 default_item = item;
550 cur ++;
551 continue;
554 NetExpr*item_value_ex = elab_and_eval(des, container, item->loop_test, -1);
555 NetEConst*item_value_co = dynamic_cast<NetEConst*>(item_value_ex);
556 assert(item_value_co);
558 // If we stumble on the item that matches, then break
559 // out now.
560 if (case_value_co->value() == item_value_co->value()) {
561 delete item_value_co;
562 break;
565 delete item_value_co;
566 cur ++;
569 delete case_value_co;
570 case_value_co = 0;
572 PGenerate*item = (cur == generates.end())? default_item : *cur;
573 if (item == 0) {
574 cerr << get_fileline() << ": debug: "
575 << "No generate items found" << endl;
576 return true;
579 if (debug_scopes)
580 cerr << get_fileline() << ": debug: "
581 << "Generate case matches item at "
582 << item->get_fileline() << endl;
584 NetScope*scope = new NetScope(container, use_name,
585 NetScope::GENBLOCK);
586 scope->set_line(get_file(), get_lineno());
587 item->elaborate_subscope_(des, scope);
589 return true;
592 void PGenerate::elaborate_subscope_(Design*des, NetScope*scope)
594 // Scan the generated scope for nested generate schemes,
595 // and *generate* new scopes, which is slightly different
596 // from simple elaboration.
598 typedef list<PGenerate*>::const_iterator generate_it_t;
599 for (generate_it_t cur = generates.begin()
600 ; cur != generates.end() ; cur ++ ) {
601 (*cur) -> generate_scope(des, scope);
604 // Scan the generated scope for gates that may create
605 // their own scopes.
606 typedef list<PGate*>::const_iterator pgate_list_it_t;
607 for (pgate_list_it_t cur = gates.begin()
608 ; cur != gates.end() ; cur ++) {
609 (*cur) ->elaborate_scope(des, scope);
612 // Save the scope that we created, for future use.
613 scope_list_.push_back(scope);
616 void PGModule::elaborate_scope_mod_(Design*des, Module*mod, NetScope*sc) const
618 if (get_name() == "") {
619 cerr << get_fileline() << ": error: Instantiation of module "
620 << mod->mod_name() << " requires an instance name." << endl;
621 des->errors += 1;
622 return;
625 // Missing module instance names have already been rejected.
626 assert(get_name() != "");
628 // Check for duplicate scopes. Simply look up the scope I'm
629 // about to create, and if I find it then somebody beat me to
630 // it.
632 if (sc->child(hname_t(get_name()))) {
633 cerr << get_fileline() << ": error: Instance/Scope name " <<
634 get_name() << " already used in this context." <<
635 endl;
636 des->errors += 1;
637 return;
640 // check for recursive instantiation by scanning the current
641 // scope and its parents. Look for a module instantiation of
642 // the same module, but farther up in the scope.
644 for (NetScope*scn = sc ; scn ; scn = scn->parent()) {
645 if (scn->type() != NetScope::MODULE)
646 continue;
648 if (strcmp(mod->mod_name(), scn->module_name()) != 0)
649 continue;
651 cerr << get_fileline() << ": error: You cannot instantiate "
652 << "module " << mod->mod_name() << " within itself." << endl;
654 cerr << get_fileline() << ": : The offending instance is "
655 << scope_path(sc) << "." << get_name() << " within "
656 << scope_path(scn) << "." << endl;
658 des->errors += 1;
659 return;
662 NetExpr*mse = msb_ ? elab_and_eval(des, sc, msb_, -1) : 0;
663 NetExpr*lse = lsb_ ? elab_and_eval(des, sc, lsb_, -1) : 0;
664 NetEConst*msb = dynamic_cast<NetEConst*> (mse);
665 NetEConst*lsb = dynamic_cast<NetEConst*> (lse);
667 assert( (msb == 0) || (lsb != 0) );
669 long instance_low = 0;
670 long instance_high = 0;
671 long instance_count = 1;
672 bool instance_array = false;
674 if (msb) {
675 instance_array = true;
676 instance_high = msb->value().as_long();
677 instance_low = lsb->value().as_long();
678 if (instance_high > instance_low)
679 instance_count = instance_high - instance_low + 1;
680 else
681 instance_count = instance_low - instance_high + 1;
683 delete mse;
684 delete lse;
687 NetScope::scope_vec_t instances (instance_count);
688 if (debug_scopes) {
689 cerr << get_fileline() << ": debug: Create " << instance_count
690 << " instances of " << get_name()
691 << "." << endl;
694 // Run through the module instances, and make scopes out of
695 // them. Also do parameter overrides that are done on the
696 // instantiation line.
697 for (int idx = 0 ; idx < instance_count ; idx += 1) {
699 hname_t use_name (get_name());
701 if (instance_array) {
702 int instance_idx = idx;
703 if (instance_low < instance_high)
704 instance_idx = instance_low + idx;
705 else
706 instance_idx = instance_low - idx;
708 use_name = hname_t(get_name(), instance_idx);
711 if (debug_scopes) {
712 cerr << get_fileline() << ": debug: Module instance " << use_name
713 << " becomes child of " << scope_path(sc)
714 << "." << endl;
717 // Create the new scope as a MODULE with my name.
718 NetScope*my_scope = new NetScope(sc, use_name, NetScope::MODULE);
719 my_scope->set_line(get_file(), mod->get_file(),
720 get_lineno(), mod->get_lineno());
721 my_scope->set_module_name(mod->mod_name());
722 my_scope->default_nettype(mod->default_nettype);
724 instances[idx] = my_scope;
726 // Set time units and precision.
727 my_scope->time_unit(mod->time_unit);
728 my_scope->time_precision(mod->time_precision);
729 des->set_precision(mod->time_precision);
731 // Look for module parameter replacements. The "replace" map
732 // maps parameter name to replacement expression that is
733 // passed. It is built up by the ordered overrides or named
734 // overrides.
736 typedef map<perm_string,PExpr*>::const_iterator mparm_it_t;
737 map<perm_string,PExpr*> replace;
740 // Positional parameter overrides are matched to parameter
741 // names by using the param_names list of parameter
742 // names. This is an ordered list of names so the first name
743 // is parameter 0, the second parameter 1, and so on.
745 if (overrides_) {
746 assert(parms_ == 0);
747 list<perm_string>::const_iterator cur
748 = mod->param_names.begin();
749 unsigned idx = 0;
750 for (;;) {
751 if (idx >= overrides_->count())
752 break;
753 if (cur == mod->param_names.end())
754 break;
756 replace[*cur] = (*overrides_)[idx];
758 idx += 1;
759 cur ++;
763 // Named parameter overrides carry a name with each override
764 // so the mapping into the replace list is much easier.
765 if (parms_) {
766 assert(overrides_ == 0);
767 for (unsigned idx = 0 ; idx < nparms_ ; idx += 1)
768 replace[parms_[idx].name] = parms_[idx].parm;
773 Module::replace_t replace_net;
775 // And here we scan the replacements we collected. Elaborate
776 // the expression in my context, then replace the sub-scope
777 // parameter value with the new expression.
779 for (mparm_it_t cur = replace.begin()
780 ; cur != replace.end() ; cur ++ ) {
782 PExpr*tmp = (*cur).second;
783 // No expression means that the parameter is not
784 // replaced at all.
785 if (tmp == 0)
786 continue;
787 NetExpr*val = tmp->elaborate_pexpr(des, sc);
788 replace_net[(*cur).first] = val;
791 // This call actually arranges for the description of the
792 // module type to process this instance and handle parameters
793 // and sub-scopes that might occur. Parameters are also
794 // created in that scope, as they exist. (I'll override them
795 // later.)
796 mod->elaborate_scope(des, my_scope, replace_net);
800 /* Stash the instance array of scopes into the parent
801 scope. Later elaboration passes will use this vector to
802 further elaborate the array.
804 Note that the array is ordered from LSB to MSB. We will use
805 that fact in the main elaborate to connect things in the
806 correct order. */
807 sc->instance_arrays[get_name()] = instances;
811 * The isn't really able to create new scopes, but it does create the
812 * event name in the current scope, so can be done during the
813 * elaborate_scope scan. Note that the name_ of the PEvent object has
814 * no hierarchy, but neither does the NetEvent, until it is stored in
815 * the NetScope object.
817 void PEvent::elaborate_scope(Design*des, NetScope*scope) const
819 NetEvent*ev = new NetEvent(name_);
820 ev->set_line(*this);
821 scope->add_event(ev);
824 void PFunction::elaborate_scope(Design*des, NetScope*scope) const
826 assert(scope->type() == NetScope::FUNC);
828 if (statement_)
829 statement_->elaborate_scope(des, scope);
832 void PTask::elaborate_scope(Design*des, NetScope*scope) const
834 assert(scope->type() == NetScope::TASK);
836 if (statement_)
837 statement_->elaborate_scope(des, scope);
842 * The base statement does not have sub-statements and does not
843 * introduce any scope, so this is a no-op.
845 void Statement::elaborate_scope(Design*, NetScope*) const
850 * When I get a behavioral block, check to see if it has a name. If it
851 * does, then create a new scope for the statements within it,
852 * otherwise use the current scope. Use the selected scope to scan the
853 * statements that I contain.
855 void PBlock::elaborate_scope(Design*des, NetScope*scope) const
857 NetScope*my_scope = scope;
859 if (pscope_name() != 0) {
860 hname_t use_name(pscope_name());
861 if (scope->child(use_name)) {
862 cerr << get_fileline() << ": error: block/scope name "
863 << use_name << " already used in this context."
864 << endl;
865 des->errors += 1;
866 return;
868 my_scope = new NetScope(scope, use_name, bl_type_==BL_PAR
869 ? NetScope::FORK_JOIN
870 : NetScope::BEGIN_END);
871 my_scope->set_line(get_file(), get_lineno());
874 for (unsigned idx = 0 ; idx < list_.count() ; idx += 1)
875 list_[idx] -> elaborate_scope(des, my_scope);
880 * The case statement itself does not introduce scope, but contains
881 * other statements that may be named blocks. So scan the case items
882 * with the elaborate_scope method.
884 void PCase::elaborate_scope(Design*des, NetScope*scope) const
886 assert(items_);
887 for (unsigned idx = 0 ; idx < (*items_).count() ; idx += 1) {
888 assert( (*items_)[idx] );
890 if (Statement*sp = (*items_)[idx]->stat)
891 sp -> elaborate_scope(des, scope);
896 * The conditional statement (if-else) does not introduce scope, but
897 * the statements of the clauses may, so elaborate_scope the contained
898 * statements.
900 void PCondit::elaborate_scope(Design*des, NetScope*scope) const
902 if (if_)
903 if_ -> elaborate_scope(des, scope);
905 if (else_)
906 else_ -> elaborate_scope(des, scope);
910 * Statements that contain a further statement but do not
911 * intrinsically add a scope need to elaborate_scope the contained
912 * statement.
914 void PDelayStatement::elaborate_scope(Design*des, NetScope*scope) const
916 if (statement_)
917 statement_ -> elaborate_scope(des, scope);
921 * Statements that contain a further statement but do not
922 * intrinsically add a scope need to elaborate_scope the contained
923 * statement.
925 void PEventStatement::elaborate_scope(Design*des, NetScope*scope) const
927 if (statement_)
928 statement_ -> elaborate_scope(des, scope);
932 * Statements that contain a further statement but do not
933 * intrinsically add a scope need to elaborate_scope the contained
934 * statement.
936 void PForever::elaborate_scope(Design*des, NetScope*scope) const
938 if (statement_)
939 statement_ -> elaborate_scope(des, scope);
943 * Statements that contain a further statement but do not
944 * intrinsically add a scope need to elaborate_scope the contained
945 * statement.
947 void PForStatement::elaborate_scope(Design*des, NetScope*scope) const
949 if (statement_)
950 statement_ -> elaborate_scope(des, scope);
954 * Statements that contain a further statement but do not
955 * intrinsically add a scope need to elaborate_scope the contained
956 * statement.
958 void PRepeat::elaborate_scope(Design*des, NetScope*scope) const
960 if (statement_)
961 statement_ -> elaborate_scope(des, scope);
965 * Statements that contain a further statement but do not
966 * intrinsically add a scope need to elaborate_scope the contained
967 * statement.
969 void PWhile::elaborate_scope(Design*des, NetScope*scope) const
971 if (statement_)
972 statement_ -> elaborate_scope(des, scope);