[mercury-users] Stack overflow, because I don't use unique modes?

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Feb 11 21:10:56 AEDT 2002


On 11-Feb-2002, Ondrej Bojar <oboj7042 at ss1000.ms.mff.cuni.cz> wrote:
> On Mon, 11 Feb 2002, Fergus Henderson wrote:
> > The stack overflow problem is likely to be that you have a main loop
> > with a recursive procedure that is not tail recursive.
> 
> I guess mmc must *know* which predicate is tail recursive and which is
> not. Is there a way to get this information from mmc?

Actually, it's not quite that simple.  The compiler knows which
*calls* are tail calls.  But a single predicate can contain both tail
recursive calls and non-tail recursive calls.  And recursion can be
either direct or indirect.  The compiler can't easily tell which
procedures contain indirectly recursive non-tail calls.

If you just want to know which predicates contain directly tail recursive
calls, the simplest way that I know of is to compile with
GRADEFLAGS=--high-level-code and MCFLAGS=--no-line-numbers,
and examine the generated C code.  If the code for a
function starts with a comment "/* tailcall optimized into loop */",
then that function contains at least one tail recursive call.

> > So, probably the best way to debug the problem is get a C stack trace,
> > which you can do by building with
> >
> > 	GRADEFLAGS = --high-level-code
> > 	MGNUCFLAGS = --c-debug --no-c-optimize
> > 	MLFLAGS = --c-debug
> >
> > in your Mmakefile, and then running your program with gdb,
> > and using gdb's `backtrace' command.
> 
> I tried this and got:
> (gdb) run < clip.in
> Program received signal SIGSEGV, Segmentation fault.
> 0x082194f5 in _entry_mercury__io__read_char_code_4_0 () at io.c:48157

You forgot `GRADEFLAGS = --high-level-code'.

(If you do that, then Mercury will allocate its stack frames on the
Mercury stack, not the C stack, and gdb won't know how to trace them.)

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list