A non-consing arbitrary number of multiple return values?

From: Adam Warner (usenet_at_consulting.net.nz)
Date: 01/26/05


Date: Wed, 26 Jan 2005 23:56:30 +1300

Hi all,

Have I correctly concluded that it is impossible for a /single stack/
function interface to return an arbitrary number of values without consing?

My reasoning is thus:

1. The function caller places n arguments on the stack and informs the
function callee of their location and number. No problem so far.

2. The callee then reserves space on the stack "above" the arguments for
local lexical variables. The amount of space to reserve can be accurately
determined at compile time. Still no problem.

3. The number of values the callee returns may only be known at run time,
and crucially may only be known by the callee once it is time to return to
the caller. This is too late to reserve space on the stack when the caller
may itself call a function before returning. At the time it calls a
subfunction the callee needs to allocate space for its arguments. But it
can't allocate the arguments when it doesn't know how much space to
reserve for multiple return values.

Maintaining two stacks--one for arguments and local variables and the
other for multiple return values--seems natural but costly.

Another potential solution would be to only support a low number of
multiple return values, such as the minimum of 20 as specified by ANSI
Common Lisp (multiple-values-limit). At steps 1, 2 or 3 one would always
reserve space for up to 20 return values.

Has anyone ever found it useful to return more than a few multiple return
values? If you knew your implementation never consed multiple return
values would you start returning hundreds or thousands of values this way?
Or would you still just pass them by reference via, e.g. an array or list?

Regards,
Adam



Relevant Pages

  • Re: C++ programmer stumbles over pass by value. Need advice.
    ... mutate or whether the callee builds a fresh object to hold the multiple ... Current the Sun JVM always builds the complex object and this ... Soon the Sun JVM will be able to deduce that it doesn't need to create the ...
    (comp.lang.java.programmer)
  • Re: C++ programmer stumbles over pass by value. Need advice.
    ... whether the callee builds a fresh object to hold the multiple return ... You may be inclined to pass a container object to the callee to mutate ... since the container can be reused by the caller. ... should build a new container for multiple values each time because the ...
    (comp.lang.java.programmer)
  • Re: C++ programmer stumbles over pass by value. Need advice.
    ... whether the callee builds a fresh object to hold the multiple return ... You may be inclined to pass a container object to the callee to mutate ... since the container can be reused by the caller. ... should build a new container for multiple values each time because the ...
    (comp.lang.java.programmer)
  • Re: A non-consing arbitrary number of multiple return values?
    ... > function interface to return an arbitrary number of multiple values ... > without consing? ... callee will fill these in when it's returning. ...
    (comp.lang.lisp)