unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: ludo@gnu.org (Ludovic Courtès)
To: guile-devel@gnu.org
Subject: Re: [VM] Tail recursion and multiple values
Date: Sat, 28 Feb 2009 15:45:19 +0100	[thread overview]
Message-ID: <8763iuhb7k.fsf@gnu.org> (raw)
In-Reply-To: 871vtiiqma.fsf@gnu.org

ludo@gnu.org (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	other threads:[~2009-02-28 14:45 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-28 14:27 [VM] Tail recursion and multiple values Ludovic Courtès
2009-02-28 14:45 ` Ludovic Courtès [this message]
2009-03-01 20:31   ` Andy Wingo
2009-03-01 23:48     ` Ludovic Courtès
2009-03-02 18:03       ` Andy Wingo
2009-03-02 21:55         ` Ludovic Courtès
2009-03-02 23:15           ` Andy Wingo
2009-03-02 23:33             ` Andreas Rottmann
2009-03-02 23:43             ` Eduardo Cavazos
2009-03-03 23:45             ` Ludovic Courtès
2009-03-04 22:11               ` Clinton Ebadi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8763iuhb7k.fsf@gnu.org \
    --to=ludo@gnu.org \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).