[gambit-list] non-tail recursion segfaults

Marc Feeley feeley at iro.umontreal.ca
Mon Apr 2 14:44:39 EDT 2007

Hash: SHA1

On 2-Apr-07, at 10:54 AM, Phil Dawes wrote:

> Hi Gambit list,
> I've started getting segmentation faults whenever I compile
> srfi-1.scm[1] and use it within my program and was wondering if it was
> to do with non-tail recursion.
> I did a quick experiment and found that on my machine the following  
> code
> compiled with gsc segfaults when i is just 20000 (it works fine
> interpreted):
> --------------
> (define (loop i)
>    (if (zero? i)
>        '()
>        (cons i (loop (- i 1)))))
> (loop 20000)
> --------------
> This seems quite limiting - is there flag I can tweak to improve this?
> Many thanks,
> Phil

It always amazes me when a bug that causes such "simple" code to fail  
goes unnoticed for so long.  This bug is related to "context merging"  
at join points, which was changed in beta 21.  The call (loop (- i  
1)) is transformed to something like

    (loop (if ... (##fx- i 1) (- i 1)))

Note that (##fx- i 1) is inlined and does not perform a function  
call, whereas (- i 1) performs a function call.  The Gambit compiler  
generates a stack overflow for the call to (- i 1).  For the call to  
loop the compiler thinks that it is unnecessary to check for stack  
overflows because it was checked on the call (- i 1).  Of course this  
is incorrect when the "true" branch of the if is taken.  Apply this  
patch to gsc/_front.scm to fix the problem:

<                                     (context-poll context1)
- ---
 >                                     (poll-merge
 >                                      (context-poll context1)
 >                                      (context-poll context2))

Thanks for the bug report!


Version: GnuPG v1.4.3 (Darwin)


More information about the Gambit-list mailing list