Re: Can someone spot the error here?



scott moore <nospam@xxxxxxxxxxx> wrote:

From a compiler writer's standpoint, the result needs to be created
at the bottom of a function, or as nearly at the bottom as possible.
The reason why is that a function result is optimally passed back
in a register, or even in a flag (for boolean results).

[...]

All of this leads to the idea that the function result is best
prepared towards the end of the function.

No disagreement here, though this is actually no different with or
without result variables. Even in ISO 7185, you can assign results
at the beginning of the function, and keeping it in a register the
whole time may or may not be good optimization (depending on the
function's other variables, number of available registers, etc.).

We're also in agreement that write-only result plus temp variable is
equivalent (given an optimizing compiler) to result-variables, so
the latter are syntactic sugar indeed.

Of course, it's a matter of taste how much sugar one prefers.
Purists may complain that Pascal has three kinds of loops (every
"for" loop can be transformed into a "while" loop, and "while" into
"repeat" and back, sometimes with the need of an additional
variable), two conditional statements, etc. On the other extreme
I've seen the opinion that whenever you use code patterns (and the
"basic 7185 skeleton" is a pattern, of course), you're not using
enough abstraction.

I think everyone will prefer a different balance here, so I'm not
going to preach on whether or not to use result variables at all. My
point was that *if* one does, of the three forms I know, I think
some are better and some are worse (which I think I made clear, no
need to repeat myself).

The result is preference towards efficiently compilable code, without
the need for the programmer to be studied in compiler construction.

I'm not sure I follow you here. Even if the result is kept in a
register or flag, the compiler could easily allow read access to it.

Do you mean that the register wouldn't have to be allocated until
the result is (first) stored, usually late in the function, so it
can be used for other purposes until then? OK, but AFAICS this
wouldn't even matter, since before it's stored it's undefined, so
whatever you read-back will be just as (in)valid. And let's face it
-- a compiler that doesn't do variable lifetime analysis, especially
on a register-challenged machine such as IA32, isn't going to
produce very optimal code anyway.

Which means to me that, at least with modern compilers, treating the
result, whether write-only or read-write, as just another variable
with the constraint that it must be in this or that location on
function exit, and leaving the rest to the generic optimizer, should
do the job. If it's short-lived, the optimizer will recognize this
fact, even if it's named (irrelevant at that point) and also read
from (only valid after it's first assignment; otherwise, the
optimizer can warn about an undefined value, like it does for other
variables).

I think I agree that Pascal's function result methodology is not
the correct answer for an idealized language such as Pascal is.
I would have preferred:

function x: y;

begin

end returning <expr>

Which would have made it clear that the result is to be prepared only
once, and very much at the termination of the function.

I see your point. OTOH, this would require a temp variable whenever
there's a branch, even in the canonical factorial function example.
With Pascal as it is, this function can be written much like the
mathematical definition, which I suppose was also his goal.

Frank

--
Frank Heckenbach, frank@xxxxxxxx, http://fjf.gnu.de/
GnuPG and PGP keys: http://fjf.gnu.de/plan (7977168E)
Pascal code, BP CRT bugfix: http://fjf.gnu.de/programs.html
Free GNU Pascal Compiler: http://www.gnu-pascal.de/
.



Relevant Pages

  • Re: subroutine stack and C machine model
    ... Use a compiler optimizer ... higher determinism means that optimizer has more information. ... C programmers like to brag that their "language" is more ... prospective computer science majors at Princeton. ...
    (comp.lang.c)
  • Re: [EGN] Numerical Accuracy
    ... The advice worked because in fact the Microsoft compiler had a bug. ... The capabilities of the optimizer are not so ... The language standard itself says ... of "programming" as a nonprofession. ...
    (comp.programming)
  • Re: switch statement, was compiler, status...
    ... Primary Register, Secondary Register, ... Stack, and abit of storage does it. ... This version of Small-C is copyrighted as a revision to Ron Cain's ... Croatia) is "Calculator Compiler" by Senko Rasik. ...
    (alt.lang.asm)
  • Re: [EGN] Numerical Accuracy
    ... which has been pointed out to you before in this forum. ... The capabilities of the optimizer are not so ... I suspect your own compiler can't do it either, ... first year programming courses teach students to not place ...
    (comp.programming)
  • Re: Bad performance on alpha? (make buildworld)
    ... It's been a few years since I'd written a compiler, ... it is 4-way superscalar but the different execution ... instruction decode stages rather than just the integer pipeline), ... > architecture and relying on the L1 cache to handle the register spills ...
    (freebsd-performance)