2 * Copyright (c) 2000-2003 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20 #ident "$Id: elab_scope.cc,v 1.46 2007/06/02 03:42:12 steve Exp $"
24 # include "compiler.h"
30 * Elaboration happens in two passes, generally. The first scans the
31 * pform to generate the NetScope tree and attach it to the Design
32 * object. The methods in this source file implement the elaboration
40 # include "PGenerate.h"
43 # include "Statement.h"
49 bool Module::elaborate_scope(Design
*des
, NetScope
*scope
,
50 const replace_t
&replacements
) const
53 cerr
<< get_line() << ": debug: Elaborate scope "
54 << scope_path(scope
) << "." << endl
;
57 // Generate all the parameters that this instance of this
58 // module introduces to the design. This loop elaborates the
59 // parameters, but doesn't evaluate references to
60 // parameters. This scan practically locates all the
61 // parameters and puts them in the parameter table in the
64 // No expressions are evaluated, yet. For now, leave them in
65 // the pform and just place a NetEParam placeholder in the
66 // place of the elaborated expression.
68 typedef map
<perm_string
,param_expr_t
>::const_iterator mparm_it_t
;
69 typedef map
<pform_name_t
,PExpr
*>::const_iterator pform_parm_it_t
;
72 // This loop scans the parameters in the module, and creates
73 // stub parameter entries in the scope for the parameter name.
75 for (mparm_it_t cur
= parameters
.begin()
76 ; cur
!= parameters
.end() ; cur
++) {
78 NetEParam
*tmp
= new NetEParam
;
79 tmp
->set_line(*((*cur
).second
.expr
));
80 tmp
->cast_signed( (*cur
).second
.signed_flag
);
82 scope
->set_parameter((*cur
).first
, tmp
, 0, 0, false);
85 for (mparm_it_t cur
= localparams
.begin()
86 ; cur
!= localparams
.end() ; cur
++) {
88 NetEParam
*tmp
= new NetEParam
;
89 tmp
->set_line(*((*cur
).second
.expr
));
90 if ((*cur
).second
.msb
)
91 tmp
->cast_signed( (*cur
).second
.signed_flag
);
93 scope
->set_parameter((*cur
).first
, tmp
, 0, 0, false);
97 // Now scan the parameters again, this time elaborating them
98 // for use as parameter values. This is after the previous
99 // scan so that local parameter names can be used in the
100 // r-value expressions.
102 for (mparm_it_t cur
= parameters
.begin()
103 ; cur
!= parameters
.end() ; cur
++) {
105 PExpr
*ex
= (*cur
).second
.expr
;
108 NetExpr
*val
= ex
->elaborate_pexpr(des
, scope
);
111 bool signed_flag
= (*cur
).second
.signed_flag
;
113 /* If the parameter declaration includes msb and lsb,
114 then use them to calculate a width for the
115 result. Then make sure the constant expression of the
116 parameter value is coerced to have the correct
117 and defined width. */
118 if ((*cur
).second
.msb
) {
119 msb
= (*cur
).second
.msb
->elaborate_pexpr(des
, scope
);
121 lsb
= (*cur
).second
.lsb
->elaborate_pexpr(des
, scope
);
125 /* If explicitly signed, then say so. */
126 val
->cast_signed(true);
127 } else if ((*cur
).second
.msb
) {
128 /* If there is a range, then the signedness comes
129 from the type and not the expression. */
130 val
->cast_signed(signed_flag
);
132 /* otherwise, let the expression describe
134 signed_flag
= val
->has_sign();
137 val
= scope
->set_parameter((*cur
).first
, val
,
138 msb
, lsb
, signed_flag
);
143 /* run parameter replacements that were collected from the
144 containing scope and meant for me. */
145 for (replace_t::const_iterator cur
= replacements
.begin()
146 ; cur
!= replacements
.end() ; cur
++) {
148 NetExpr
*val
= (*cur
).second
;
150 cerr
<< get_line() << ": internal error: "
151 << "Missing expression in parameter replacement for "
156 cerr
<< get_line() << ": debug: "
157 << "Replace " << (*cur
).first
158 << " with expression " << *val
159 << " from " << val
->get_line() << "." << endl
;
161 bool flag
= scope
->replace_parameter((*cur
).first
, val
);
163 cerr
<< val
->get_line() << ": warning: parameter "
164 << (*cur
).first
<< " not found in "
165 << scope_path(scope
) << "." << endl
;
169 for (mparm_it_t cur
= localparams
.begin()
170 ; cur
!= localparams
.end() ; cur
++) {
172 PExpr
*ex
= (*cur
).second
.expr
;
175 NetExpr
*val
= ex
->elaborate_pexpr(des
, scope
);
178 bool signed_flag
= false;
180 /* If the parameter declaration includes msb and lsb,
181 then use them to calculate a width for the
182 result. Then make sure the constant expression of the
183 parameter value is coerced to have the correct
184 and defined width. */
185 if ((*cur
).second
.msb
) {
186 msb
= (*cur
).second
.msb
->elaborate_pexpr(des
, scope
);
188 lsb
= (*cur
).second
.lsb
->elaborate_pexpr(des
, scope
);
189 signed_flag
= (*cur
).second
.signed_flag
;
192 val
->cast_signed(signed_flag
);
193 val
= scope
->set_parameter((*cur
).first
, val
,
194 msb
, lsb
, signed_flag
);
199 // Run through the defparams for this module, elaborate the
200 // expressions in this context and save the result is a table
201 // for later final override.
203 // It is OK to elaborate the expressions of the defparam here
204 // because Verilog requires that the expressions only use
205 // local parameter names. It is *not* OK to do the override
206 // here because the parameter receiving the assignment may be
207 // in a scope not discovered by this pass.
209 for (pform_parm_it_t cur
= defparms
.begin()
210 ; cur
!= defparms
.end() ; cur
++ ) {
212 PExpr
*ex
= (*cur
).second
;
215 NetExpr
*val
= ex
->elaborate_pexpr(des
, scope
);
216 if (val
== 0) continue;
217 scope
->defparams
[(*cur
).first
] = val
;
220 // Evaluate the attributes. Evaluate them in the scope of the
221 // module that the attribute is attached to. Is this correct?
223 attrib_list_t
*attr
= evaluate_attributes(attributes
, nattr
, des
, scope
);
225 for (unsigned idx
= 0 ; idx
< nattr
; idx
+= 1)
226 scope
->attribute(attr
[idx
].key
, attr
[idx
].val
);
230 // Generate schemes can create new scopes in the form of
231 // generated code. Scan the generate schemes, and *generate*
232 // new scopes, which is slightly different from simple
235 typedef list
<PGenerate
*>::const_iterator generate_it_t
;
236 for (generate_it_t cur
= generate_schemes
.begin()
237 ; cur
!= generate_schemes
.end() ; cur
++ ) {
238 (*cur
) -> generate_scope(des
, scope
);
242 // Tasks introduce new scopes, so scan the tasks in this
243 // module. Create a scope for the task and pass that to the
244 // elaborate_scope method of the PTask for detailed
247 typedef map
<perm_string
,PTask
*>::const_iterator tasks_it_t
;
249 for (tasks_it_t cur
= tasks_
.begin()
250 ; cur
!= tasks_
.end() ; cur
++ ) {
252 hname_t
use_name( (*cur
).first
);
253 NetScope
*task_scope
= new NetScope(scope
, use_name
,
255 (*cur
).second
->elaborate_scope(des
, task_scope
);
259 // Functions are very similar to tasks, at least from the
260 // perspective of scopes. So handle them exactly the same
263 typedef map
<perm_string
,PFunction
*>::const_iterator funcs_it_t
;
265 for (funcs_it_t cur
= funcs_
.begin()
266 ; cur
!= funcs_
.end() ; cur
++ ) {
268 hname_t
use_name( (*cur
).first
);
269 NetScope
*func_scope
= new NetScope(scope
, use_name
,
271 (*cur
).second
->elaborate_scope(des
, func_scope
);
275 // Gates include modules, which might introduce new scopes, so
276 // scan all of them to create those scopes.
278 typedef list
<PGate
*>::const_iterator gates_it_t
;
279 for (gates_it_t cur
= gates_
.begin()
280 ; cur
!= gates_
.end() ; cur
++ ) {
282 (*cur
) -> elaborate_scope(des
, scope
);
286 // initial and always blocks may contain begin-end and
287 // fork-join blocks that can introduce scopes. Therefore, I
288 // get to scan processes here.
290 typedef list
<PProcess
*>::const_iterator proc_it_t
;
292 for (proc_it_t cur
= behaviors_
.begin()
293 ; cur
!= behaviors_
.end() ; cur
++ ) {
295 (*cur
) -> statement() -> elaborate_scope(des
, scope
);
298 // Scan through all the named events in this scope. We do not
299 // need anything more than the current scope to do this
300 // elaboration, so do it now. This allows for normal
301 // elaboration to reference these events.
303 for (map
<perm_string
,PEvent
*>::const_iterator et
= events
.begin()
304 ; et
!= events
.end() ; et
++ ) {
306 (*et
).second
->elaborate_scope(des
, scope
);
309 return des
->errors
== 0;
312 bool PGenerate::generate_scope(Design
*des
, NetScope
*container
)
314 switch (scheme_type
) {
316 return generate_scope_loop_(des
, container
);
319 return generate_scope_condit_(des
, container
, false);
322 return generate_scope_condit_(des
, container
, true);
325 cerr
<< get_line() << ": sorry: Generate of this sort"
326 << " is not supported yet!" << endl
;
332 * This is the elaborate scope method for a generate loop.
334 bool PGenerate::generate_scope_loop_(Design
*des
, NetScope
*container
)
336 // We're going to need a genvar...
339 // The initial value for the genvar does not need (nor can it
340 // use) the genvar itself, so we can evaluate this expression
341 // the same way any other paramter value is evaluated.
342 NetExpr
*init_ex
= elab_and_eval(des
, container
, loop_init
, -1);
343 NetEConst
*init
= dynamic_cast<NetEConst
*> (init_ex
);
345 cerr
<< get_line() << ": error: Cannot evaluate genvar"
346 << " init expression: " << *loop_init
<< endl
;
351 genvar
= init
->value().as_long();
355 cerr
<< get_line() << ": debug: genvar init = " << genvar
<< endl
;
357 container
->genvar_tmp
= loop_index
;
358 container
->genvar_tmp_val
= 0;
359 NetExpr
*test_ex
= elab_and_eval(des
, container
, loop_test
, -1);
360 NetEConst
*test
= dynamic_cast<NetEConst
*>(test_ex
);
362 while (test
->value().as_long()) {
364 // The actual name of the scope includes the genvar so
365 // that each instance has a unique name in the
366 // container. The format of using [] is part of the
368 hname_t
use_name (scope_name
, genvar
);
370 cerr
<< get_line() << ": debug: "
371 << "Create generated scope " << use_name
<< endl
;
373 NetScope
*scope
= new NetScope(container
, use_name
,
376 // Set in the scope a localparam for the value of the
377 // genvar within this instance of the generate
378 // block. Code within this scope thus has access to the
379 // genvar as a constant.
381 verinum
genvar_verinum(genvar
);
382 genvar_verinum
.has_sign(true);
383 NetEConstParam
*gp
= new NetEConstParam(scope
,
386 scope
->set_localparam(loop_index
, gp
);
389 cerr
<< get_line() << ": debug: "
390 << "Create implicit localparam "
391 << loop_index
<< " = " << genvar_verinum
<< endl
;
394 elaborate_subscope_(des
, scope
);
396 // Calculate the step for the loop variable.
397 NetExpr
*step_ex
= elab_and_eval(des
, container
, loop_step
, -1);
398 NetEConst
*step
= dynamic_cast<NetEConst
*>(step_ex
);
401 cerr
<< get_line() << ": debug: genvar step from "
402 << genvar
<< " to " << step
->value().as_long() << endl
;
404 genvar
= step
->value().as_long();
405 container
->genvar_tmp_val
= genvar
;
408 test_ex
= elab_and_eval(des
, container
, loop_test
, -1);
409 test
= dynamic_cast<NetEConst
*>(test_ex
);
413 // Clear the genvar_tmp field in the scope to reflect that the
414 // genvar is no longer value for evaluating expressions.
415 container
->genvar_tmp
= perm_string();
420 bool PGenerate::generate_scope_condit_(Design
*des
, NetScope
*container
, bool else_flag
)
422 NetExpr
*test_ex
= elab_and_eval(des
, container
, loop_test
, -1);
423 NetEConst
*test
= dynamic_cast<NetEConst
*> (test_ex
);
426 // If the condition evaluates as false, then do not create the
428 if (test
->value().as_long() == 0 && !else_flag
429 || test
->value().as_long() != 0 && else_flag
) {
431 cerr
<< get_line() << ": debug: Generate condition "
432 << (else_flag
? "(else)" : "(if)")
433 << " value=" << test
->value() << ": skip generation"
439 hname_t
use_name (scope_name
);
441 cerr
<< get_line() << ": debug: Generate condition "
442 << (else_flag
? "(else)" : "(if)")
443 << " value=" << test
->value() << ": Generate scope="
446 NetScope
*scope
= new NetScope(container
, use_name
,
449 elaborate_subscope_(des
, scope
);
454 void PGenerate::elaborate_subscope_(Design
*des
, NetScope
*scope
)
456 // Scan the generated scope for nested generate schemes,
457 // and *generate* new scopes, which is slightly different
458 // from simple elaboration.
460 typedef list
<PGenerate
*>::const_iterator generate_it_t
;
461 for (generate_it_t cur
= generates
.begin()
462 ; cur
!= generates
.end() ; cur
++ ) {
463 (*cur
) -> generate_scope(des
, scope
);
466 // Scan the generated scope for gates that may create
468 typedef list
<PGate
*>::const_iterator pgate_list_it_t
;
469 for (pgate_list_it_t cur
= gates
.begin()
470 ; cur
!= gates
.end() ; cur
++) {
471 (*cur
) ->elaborate_scope(des
, scope
);
474 // Save the scope that we created, for future use.
475 scope_list_
.push_back(scope
);
478 void PGModule::elaborate_scope_mod_(Design
*des
, Module
*mod
, NetScope
*sc
) const
480 if (get_name() == "") {
481 cerr
<< get_line() << ": error: Instantiation of module "
482 << mod
->mod_name() << " requires an instance name." << endl
;
487 // Missing module instance names have already been rejected.
488 assert(get_name() != "");
490 // Check for duplicate scopes. Simply look up the scope I'm
491 // about to create, and if I find it then somebody beat me to
494 if (sc
->child(hname_t(get_name()))) {
495 cerr
<< get_line() << ": error: Instance/Scope name " <<
496 get_name() << " already used in this context." <<
502 // check for recursive instantiation by scanning the current
503 // scope and its parents. Look for a module instantiation of
504 // the same module, but farther up in the scope.
506 for (NetScope
*scn
= sc
; scn
; scn
= scn
->parent()) {
507 if (scn
->type() != NetScope::MODULE
)
510 if (strcmp(mod
->mod_name(), scn
->module_name()) != 0)
513 cerr
<< get_line() << ": error: You cannot instantiate "
514 << "module " << mod
->mod_name() << " within itself." << endl
;
516 cerr
<< get_line() << ": : The offending instance is "
517 << scope_path(sc
) << "." << get_name() << " within "
518 << scope_path(scn
) << "." << endl
;
524 NetExpr
*mse
= msb_
? elab_and_eval(des
, sc
, msb_
, -1) : 0;
525 NetExpr
*lse
= lsb_
? elab_and_eval(des
, sc
, lsb_
, -1) : 0;
526 NetEConst
*msb
= dynamic_cast<NetEConst
*> (mse
);
527 NetEConst
*lsb
= dynamic_cast<NetEConst
*> (lse
);
529 assert( (msb
== 0) || (lsb
!= 0) );
531 long instance_low
= 0;
532 long instance_high
= 0;
533 long instance_count
= 1;
534 bool instance_array
= false;
537 instance_array
= true;
538 instance_high
= msb
->value().as_long();
539 instance_low
= lsb
->value().as_long();
540 if (instance_high
> instance_low
)
541 instance_count
= instance_high
- instance_low
+ 1;
543 instance_count
= instance_low
- instance_high
+ 1;
549 NetScope::scope_vec_t
instances (instance_count
);
551 cerr
<< get_line() << ": debug: Create " << instance_count
552 << " instances of " << get_name()
556 // Run through the module instances, and make scopes out of
557 // them. Also do parameter overrides that are done on the
558 // instantiation line.
559 for (int idx
= 0 ; idx
< instance_count
; idx
+= 1) {
561 hname_t
use_name (get_name());
563 if (instance_array
) {
564 int instance_idx
= idx
;
565 if (instance_low
< instance_high
)
566 instance_idx
= instance_low
+ idx
;
568 instance_idx
= instance_low
- idx
;
570 use_name
= hname_t(get_name(), instance_idx
);
574 cerr
<< get_line() << ": debug: Module instance " << use_name
575 << " becomes child of " << scope_path(sc
)
579 // Create the new scope as a MODULE with my name.
580 NetScope
*my_scope
= new NetScope(sc
, use_name
, NetScope::MODULE
);
581 my_scope
->set_module_name(mod
->mod_name());
582 my_scope
->default_nettype(mod
->default_nettype
);
584 instances
[idx
] = my_scope
;
586 // Set time units and precision.
587 my_scope
->time_unit(mod
->time_unit
);
588 my_scope
->time_precision(mod
->time_precision
);
589 des
->set_precision(mod
->time_precision
);
591 // Look for module parameter replacements. The "replace" map
592 // maps parameter name to replacement expression that is
593 // passed. It is built up by the ordered overrides or named
596 typedef map
<perm_string
,PExpr
*>::const_iterator mparm_it_t
;
597 map
<perm_string
,PExpr
*> replace
;
600 // Positional parameter overrides are matched to parameter
601 // names by using the param_names list of parameter
602 // names. This is an ordered list of names so the first name
603 // is parameter 0, the second parameter 1, and so on.
607 list
<perm_string
>::const_iterator cur
608 = mod
->param_names
.begin();
611 if (idx
>= overrides_
->count())
613 if (cur
== mod
->param_names
.end())
616 replace
[*cur
] = (*overrides_
)[idx
];
623 // Named parameter overrides carry a name with each override
624 // so the mapping into the replace list is much easier.
626 assert(overrides_
== 0);
627 for (unsigned idx
= 0 ; idx
< nparms_
; idx
+= 1)
628 replace
[parms_
[idx
].name
] = parms_
[idx
].parm
;
633 Module::replace_t replace_net
;
635 // And here we scan the replacements we collected. Elaborate
636 // the expression in my context, then replace the sub-scope
637 // parameter value with the new expression.
639 for (mparm_it_t cur
= replace
.begin()
640 ; cur
!= replace
.end() ; cur
++ ) {
642 PExpr
*tmp
= (*cur
).second
;
643 // No expression means that the parameter is not
647 NetExpr
*val
= tmp
->elaborate_pexpr(des
, sc
);
648 replace_net
[(*cur
).first
] = val
;
651 // This call actually arranges for the description of the
652 // module type to process this instance and handle parameters
653 // and sub-scopes that might occur. Parameters are also
654 // created in that scope, as they exist. (I'll override them
656 mod
->elaborate_scope(des
, my_scope
, replace_net
);
660 /* Stash the instance array of scopes into the parent
661 scope. Later elaboration passes will use this vector to
662 further elaborate the array. */
663 sc
->instance_arrays
[get_name()] = instances
;
667 * The isn't really able to create new scopes, but it does create the
668 * event name in the current scope, so can be done during the
669 * elaborate_scope scan. Note that the name_ of the PEvent object has
670 * no hierarchy, but neither does the NetEvent, until it is stored in
671 * the NetScope object.
673 void PEvent::elaborate_scope(Design
*des
, NetScope
*scope
) const
675 NetEvent
*ev
= new NetEvent(name_
);
677 scope
->add_event(ev
);
680 void PFunction::elaborate_scope(Design
*des
, NetScope
*scope
) const
682 assert(scope
->type() == NetScope::FUNC
);
685 statement_
->elaborate_scope(des
, scope
);
688 void PTask::elaborate_scope(Design
*des
, NetScope
*scope
) const
690 assert(scope
->type() == NetScope::TASK
);
693 statement_
->elaborate_scope(des
, scope
);
698 * The base statement does not have sub-statements and does not
699 * introduce any scope, so this is a no-op.
701 void Statement::elaborate_scope(Design
*, NetScope
*) const
706 * When I get a behavioral block, check to see if it has a name. If it
707 * does, then create a new scope for the statements within it,
708 * otherwise use the current scope. Use the selected scope to scan the
709 * statements that I contain.
711 void PBlock::elaborate_scope(Design
*des
, NetScope
*scope
) const
713 NetScope
*my_scope
= scope
;
716 my_scope
= new NetScope(scope
, hname_t(name_
), bl_type_
==BL_PAR
717 ? NetScope::FORK_JOIN
718 : NetScope::BEGIN_END
);
721 for (unsigned idx
= 0 ; idx
< list_
.count() ; idx
+= 1)
722 list_
[idx
] -> elaborate_scope(des
, my_scope
);
727 * The case statement itself does not introduce scope, but contains
728 * other statements that may be named blocks. So scan the case items
729 * with the elaborate_scope method.
731 void PCase::elaborate_scope(Design
*des
, NetScope
*scope
) const
734 for (unsigned idx
= 0 ; idx
< (*items_
).count() ; idx
+= 1) {
735 assert( (*items_
)[idx
] );
737 if (Statement
*sp
= (*items_
)[idx
]->stat
)
738 sp
-> elaborate_scope(des
, scope
);
743 * The conditional statement (if-else) does not introduce scope, but
744 * the statements of the clauses may, so elaborate_scope the contained
747 void PCondit::elaborate_scope(Design
*des
, NetScope
*scope
) const
750 if_
-> elaborate_scope(des
, scope
);
753 else_
-> elaborate_scope(des
, scope
);
757 * Statements that contain a further statement but do not
758 * intrinsically add a scope need to elaborate_scope the contained
761 void PDelayStatement::elaborate_scope(Design
*des
, NetScope
*scope
) const
764 statement_
-> elaborate_scope(des
, scope
);
768 * Statements that contain a further statement but do not
769 * intrinsically add a scope need to elaborate_scope the contained
772 void PEventStatement::elaborate_scope(Design
*des
, NetScope
*scope
) const
775 statement_
-> elaborate_scope(des
, scope
);
779 * Statements that contain a further statement but do not
780 * intrinsically add a scope need to elaborate_scope the contained
783 void PForever::elaborate_scope(Design
*des
, NetScope
*scope
) const
786 statement_
-> elaborate_scope(des
, scope
);
790 * Statements that contain a further statement but do not
791 * intrinsically add a scope need to elaborate_scope the contained
794 void PForStatement::elaborate_scope(Design
*des
, NetScope
*scope
) const
797 statement_
-> elaborate_scope(des
, scope
);
801 * Statements that contain a further statement but do not
802 * intrinsically add a scope need to elaborate_scope the contained
805 void PRepeat::elaborate_scope(Design
*des
, NetScope
*scope
) const
808 statement_
-> elaborate_scope(des
, scope
);
812 * Statements that contain a further statement but do not
813 * intrinsically add a scope need to elaborate_scope the contained
816 void PWhile::elaborate_scope(Design
*des
, NetScope
*scope
) const
819 statement_
-> elaborate_scope(des
, scope
);