🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #875

Re: FOR loops

#875
From: Stephen Russell
Date: Wed, 04 Jul 1990 14:26
61 lines
2795 bytes
In article <1990Jul4.074634.645@diku.dk> jensting@skinfaxe.diku.dk (Jens Tingleff) writes:
>ROSS@UCF1VM.BITNET (Bri) writes:
>>Anyone else have any answers as to why Wirth designed such a restriction?
>
>could be a continuation of the restriction from Pascal ...

This is the likely history.

>The reason for demanding a local variable is (or could be ?) that a
>local variable can be detected to be non-aliased. I.E. that no context
>switch ...

Context switches are not an issue, as they didn't exist in Pascal. Rather,
the restriction allows efficient implementation of For loops. Many loop
optimisations, such as induction variables for array access, rely on a
simple sequence of index variable values. The Pascal restriction (and the
intention in Modula2?) allows the compiler to easily check that no
assignment statements, or procedures called, in the body of the loop can
change the index variable.

Detecting changes to the index variable is made more difficult by
allowing arbitrary designators. For example,

	FOR a[i][k][j] := 1 TO 10 DO ...

requires that the compiler checks for changes to i, j, and k in the
loop. Aliasing makes things even tougher.  For example, if the body
of the above loop contained

	a[x][y][z] := 100

then the index variable is changed iff x=i, y=k and z=j. Similar
problems occur with pointer variables:

	FOR p^ := 1 TO 10 DO ... q^ := 100; ... END;

If p = q, then the index variable is changed, but proving that p # q
is impossible for the compiler in the general case.

So, by making designators illegal, the compiler can safely perform these
worthwhile optimisations.

As well, some machines provide efficient loop instructions (such as
decrement and loop, etc).  Code generation is again improved if the
index variable is simple.

Some historical comments: recall that FORTRAN imposed some tight
restrictions on DO loops, and for the same reasons: to generate tight
loop code. As well, in designing Pascal, Wirth was trying to avoid the
inefficiency of Algol's baroque loop construct.

>ANYWAY. A slightly more levelheaded reason for requiring the FOR control
>var to be simple and local is that it's easier to decide to make 
>the loop-cntr. var a register var...... .

Quite right.


Cheers,

Steve.
Technical Details
Message-ID: <1081@cluster.cs.su.oz>
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!samsung!munnari.oz.au!metro!cluster!steve
References: <INFO-M2%90070316593556@UCF1VM.BITNET> <1990Jul4.074634.645@diku.dk>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!samsung!munnari.oz.au!metro!cluster!steve
From: steve@cs.su.oz (Stephen Russell)
Newsgroups: comp.lang.modula2
Subject: Re: FOR loops
Message-ID: <1081@cluster.cs.su.oz>
Date: 4 Jul 90 14:26:01 GMT
References: <INFO-M2%90070316593556@UCF1VM.BITNET> <1990Jul4.074634.645@diku.dk>
Sender: news@cluster.cs.su.oz
Reply-To: steve@cluster.cs.su.oz (Stephen Russell)
Organization: Basser Dept of Computer Science, University of Sydney, Australia
Lines: 61