RE: [sv-bc] Ambiguous declaration / initialization in for-loop

From: Michael Mac McNamara <mcnamara_at_.....>
Date: Fri Oct 28 2005 - 12:03:23 PDT
You recommend this, which seems like good advice.

However, the text Chris quotes seems to require this, implying a
compliant tool must reject code where some variable not declared in the
for loop is read or written in the calculation of the control of the
loop.

Is this indeed the requirement?  Specifically, is the following illegal:
    integer sz = 2;
    ...

    for ( int count = 0, done = 0, j = 0; j * count*sz < 125; j++,
count++)
        $display("Value j = %d\n", j );

Secondly, the declaration of a control variable called done, hints that
the body of the loop uses 'done' for some controlling function.  Must
'done' be used for this (and only for this)? (specifically, in the given
example, 'done' is declared and never used; is this an error that must
be flagged by a compliant tool, as 'done' is clearly NOT a control
variable?).  



Would it be legal use 'done' as a flag to single early exit, and test
that inside the body of the loop, and then break out of the loop? (e.g.:

    for ( int count = 0, done = 0, j = 0; j * count < 125; j++, count++)
begin
	 if ($today() = `o31)
	    done = 1;
       ...
        $display("Value j = %d\n", j );
       if ( done ) break;
    end

Thirdly, the given example implies that it is legal to read the control
variables inside the loop (printing of 'j');

Is it also legal to write to these control variables, other than within
the for production?  (Again, sane coding styles caution against
complicating control structures by manipulating the control variables
other than in the for production; but this is a normative phrase in the
standard, which appears to be stating a requirement rather than a
suggestion.

Specifically, is the following legal:

for (int count = 0; count < 100; ) begin
   ...
   count++;
end



-----Original Message-----
From: owner-sv-bc@eda.org [mailto:owner-sv-bc@eda.org] On Behalf Of Brad
Pierce
Sent: Friday, October 28, 2005 10:59 AM
To: sv-bc@eda.org
Subject: Re: [sv-bc] Ambiguous declaration / initialization in for-loop

Chris,

Think of the enhanced for-loop

    for ( int count = 0, done = 0, j = 0; j * count < 125; j++, count++)
        $display("Value j = %d\n", j );
	"In a for loop initialization, either all or none of the control
variables 
	are locally declared. In the second loop of the last example,
count, done 
	and j are all locally declared."
as

    begin
      int automatic count, done, j;
      for ( count = 0, done = 0, j = 0; j * count < 125; j++, count++)
        $display("Value j = %d\n", j );
    end

The enhanced for-loop can be recoginized by the data_type after the
opening paren.  If there is no data_type there, it is an old-style
for-loop.  If there is a data_type, then it's an enhanced for-loop.  In
an enhanced for-loop all the control variables are locally declared. 

I recommend only using the enhanced for-loop.

-- Brad
Received on Fri Oct 28 12:03:44 2005

This archive was generated by hypermail 2.1.8 : Fri Oct 28 2005 - 12:04:36 PDT