From: Glenn Kasten
Subject: How is values usually implemented?
Date: 
Message-ID: <3275244C.76F4@auco.com>
How are the Common LISP "values" and related functions typically
implemented (to support the return of multiple values from a function)?

--
Glenn Kasten
AUCO, Inc.
·······@auco.com

From: Thomas A. Russ
Subject: Re: How is values usually implemented?
Date: 
Message-ID: <ymi4tjdeo6g.fsf@hobbes.isi.edu>
In article <·············@auco.com> Glenn Kasten <·······@auco.com> writes:
 > 
 > How are the Common LISP "values" and related functions typically
 > implemented (to support the return of multiple values from a function)?

One common technique is to return the first few (roughly 2 to 4) values
in registers.  If there are more, then they are returned on the stack.

This helps since most functions don't return many values and a fair
number of the built-in CommonLisp functions return 2 values.  Adding to
this is the observation that often only the first value is used and you
want to have a fairly efficient way of returning the values that doesn't
require special handling for the common cases.

-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu    
From: Kelly Murray
Subject: Re: How is values usually implemented?
Date: 
Message-ID: <555mja$pjb@sparky.franz.com>
> > How are the Common LISP "values" and related functions typically
> > implemented (to support the return of multiple values from a function)?

> One common technique is to return the first few (roughly 2 to 4) values
> in registers.  If there are more, then they are returned on the stack.

I'll just add that the technique depends a lot on how function calls
in general are implemented.  I can say that in my TopCL implementation,
which was oriented towards a 386 which has few registers.
It worked as described below,
which is roughly how I remember, and wasn't 100% efficient:

The compiler knows if a function is called for a single value or not,
and marks the call-frame indicating if it wants multiple values.
The (values) expression compiles into pushing all the values,
and then checking the flag for how many the caller wants.
If it wants only a single value,
it puts it in the return register and pops the rest. 
If called for multiple-values, it copies the values down to
the callers stack (overwriting it's call frame usually)
and stores the number in a register, 
and puts a "multiple-values" tag in the return register,
so the caller knows it got back multiple.  

If a caller can accept multiple, it must check upon return
from the called function if it got multiple values
by checking the return register for this special tag.
If it isn't multiple, set all the needed values to NIL
except the first one, which gets the single return value.
If it got multiple, pop them off the stack into where they go.

In the common case, the return value just comes back in a register
and is used directly. 
Checking is only done when the called function has multiple
values to return and/or the caller wants multiple values.
Note that "tail position" calls must "inherit" the accept-multiple
status from their callers, which can be a source of compiler bugs,
so be warned if your doing an implementation...

-Kelly Murray  ···@franz.com  http://www.franz.com