[m-dev.] pragma c code joy!

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Jun 24 18:02:04 AEST 1998


On 24-Jun-1998, Thomas Charles CONWAY <conway at cs.mu.OZ.AU> wrote:
> Hi
> 
> I'm just fiddling with code generation for pragma c code, and I've
> noticed if "may_call_mercury" is set then it generates code like
> the following:
> 
> /* code for predicate 'bar'/2 in mode 0 */
> Define_static(mercury__tryit__bar_2_0);
> 	incr_sp_push_msg(1, "tryit:bar/2");
> 	detstackvar(1) = (Word) succip;
> 	{
> 	Word	A;
> 	Word	B;
> 	A = r1;
> 	save_registers();
> {
> #line 36 "tryit.m"
> 
> 	B = A;
> ;}
> #line 158 "tryit.c"
> #ifndef CONSERVATIVE_GC
> 	restore_registers();
> #endif
> 	r1 = B;
> 
> 	}
> 	succip = (Code *) detstackvar(1);
> 	decr_sp_pop_msg(1);
> 	proceed();
> END_MODULE
> 
> The point that is worrying me is that the call to save_registers()
> is unconditional, but the call to restore them again is conditional.
> I'm pretty sure this is a bug. Anyone know otherwise?

See the comments in pragma_c_gen.m:

% (2)   The call to save_registers() is needed so that if the
%       C code calls Mercury code, we can call restore_registers()
%       on entry to the Mercury code (see export.m) to get the
%       right values of `sp', `hp', `curfr' and `maxfr' for the
%       recursive invocation of Mercury.
%
% (3)   The call to restore_registers() is needed in case the
%       C code calls Mercury code which allocates some data
%       on the heap, and this data is returned from Mercury
%       through C back to Mercury.  In that case, we need to
%       keep the value of `hp' that was set by the recursive
%       invocation of Mercury.  The Mercury calling convention
%       guarantees that when calling det or semidet code, the values
%       of `sp', `curfr', and `maxfr' will be preserved, so if we're
%       using conservative gc, there is nothing that needs restoring.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.



More information about the developers mailing list