Re: multiple return values



chairam wrote:
> Hi, I'm studying lisp
>
> Some function like: truncate, floor, round, return 2 values
>
> if you want to catch all values you have to use multiple-value-bind
> but:
> why 2 values and not a list of 2 values?

The storage locations that hold arguments and return values are not
first-class objects. They are data communication pathways. It's
important to be able to implement those pathways efficiently on a given
processor.

If the aggregate that holds return values were represented as a first
class list, then the program could retain a reference to that list. The
list would have to be allocated in the usual way and subject to garbage
collection.

The way it is right now, the program cannot get a handle on the place,
or places, which hold return values. It can only retrieve copies of the
values held in those inaccessible storage locations.

This is similar to how it works in other languages. For example in C, a
function can return a struct object. However, that returned struct
isn't a first class object. You cannot take the address of a member,
and if it contains an array, you therefore cannot use that array.

struct foo { int x; int y[3]; };

struct foo func(void);

...

z = func().x; /* okay to access x */

func().x = 3; /* error, returned struct isn't an lvalue */

func().y; /* error, cannot take address of any part */


These restrictions give the implementation a lot of freedom with
respect to implementing procedure linkage.

For instance, return values can be placed into registers, or pushed
onto the stack, or placed into a memory area that is reserved by the
caller, which could be on the stack.

None of these locations are programmer-visible first class objects in
the high level language; they are managed by the compiler. If registers
are used, the compiler takes care that the generate code knows what is
clobbered and what is saved. If return values are pushed on the stack,
the compiler ensures that everything is popped off the stack even if
the high level program doesn't use some or all of the return values.

Keeping the memory locations and the entire mechanism shrouded from the
programmer allows for optimizations. If a memory area is used for
return values, in principle it would be possible to allow the
programmer to see that area as an object in the high level language.
But that would just invite bugs, probably because that memory is quite
likely quickly disposed of and then reused again for another purpose.
For instance, suppose that the stack top is used for returning values.
As soon as the compiler-generated code which called the function cleans
those values off the stack, any subsequent evaluation can re-use the
same space by pushing values there. The programmer must use the
appropriate high level mechanism to ``rescue'' some or all of the
return values from that memory before it is gone.

> I have my own opinion but appreciate others'

That's wonderful! Now all you need is a time machine. :)

.



Relevant Pages

  • Re: Why not auto?
    ... >>> on a system where stack space is limited declaring all local variables ... The compiler is in a perfect position to know which parameters would ... The compiler could also apply such optimisations to automatic ... likely to promote memory reuse and improved caching. ...
    (comp.lang.c)
  • Re: IAR compiler & MSP430 problem
    ... the compiler STILL compiler with no warning ... the linker isn't telling you when you have run our of RAM. ... which produced a prediction of stack use ... chips have different memory resources. ...
    (comp.arch.embedded)
  • Re: next mystery: 32 vs 64 bits...
    ... etc. Stack and register reorganization are always ... (this is not too much of a problem in my compiler, I just resort back to the ... given the newer way I am handling memory references (better ... 'on stack' typically, is not 'in memory'. ...
    (alt.lang.asm)
  • Re: Doubts in shellcode !?
    ... The compiler actually generates a "call" instruction ... transfer by manipulating the data on the stack, ... > |> and clobber memory that you have no business clobbering.... ... just to make sure no local pointers got out of control and ...
    (comp.security.unix)
  • Re: made it to page 4 of gforth tutorial
    ... the stack space anyway. ... And there are clearly going to be some platform ... came with an embedded C compiler once. ... like I've written before, if one *really* needs recursion (as I have, ...
    (comp.lang.forth)