guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [VM] Tail recursion and multiple values


From: Ludovic Courtès
Subject: Re: [VM] Tail recursion and multiple values
Date: Sat, 28 Feb 2009 15:45:19 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.90 (gnu/linux)

address@hidden (Ludovic Courtès) writes:

> Use of multiple values breaks tail recursion in VM-compiled code:
> 
>   (let loop ((x 1000000))
>     (and (> x 0)
>          (call-with-values
>              (lambda ()
>                (values (1+ x) (1- x)))
>            (lambda (next prev)
>              (loop prev)))))

Actually no: it works with VM-compiled code, but it breaks when using
Guile-VM with `,o interp #t' (which appears to be the default, except at
the REPL).

In this case, `loop' is an interpreter procedure while
`call-with-values' is a program.  It's the implementation of `goto/args'
in this particular case that seems to break tail recursion:

  if (!SCM_FALSEP (scm_procedure_p (x)))
    {
      POP_LIST (nargs);
      SYNC_REGISTER ();
      sp[-1] = scm_apply (x, sp[0], SCM_EOL);

The `scm_apply ()' call introduces a new stack frame instead of re-using
the current one.

At this point I'm not sure how to fix it.  We need better integration of
calls from the VM to subrs/procedures anyway (notably removing
`POP_LIST ()' and argument consing before subr/procedure calls!) so
probably we could postpone this issue until then.  Andy?

Thanks,
Ludo'.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]