Re: Can someone spot the error here?
- From: frank@xxxxxxxx
- Date: 21 May 2006 22:01:12 GMT
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/
.
- Follow-Ups:
- Re: Can someone spot the error here?
- From: scott moore
- Re: Can someone spot the error here?
- References:
- Can someone spot the error here?
- From: ed_davis2
- Re: Can someone spot the error here?
- From: Marco van de Voort
- Re: Can someone spot the error here?
- From: frank
- Re: Can someone spot the error here?
- From: Marco van de Voort
- Re: Can someone spot the error here?
- From: frank
- Re: Can someone spot the error here?
- From: Marco van de Voort
- Re: Can someone spot the error here?
- From: frank
- Re: Can someone spot the error here?
- From: scott moore
- Re: Can someone spot the error here?
- From: frank
- Re: Can someone spot the error here?
- From: scott moore
- Can someone spot the error here?
- Prev by Date: Re: Can someone spot the error here?
- Next by Date: Re: Can someone spot the error here?
- Previous by thread: Re: Can someone spot the error here?
- Next by thread: Re: Can someone spot the error here?
- Index(es):
Relevant Pages
|