rehsack has asked for the wisdom of the Perl Monks concerning the following question:

I try to speed up Params::Util using XS. One method the author wrote, allows a method to override the isa() method, so sv_derived_from() doesn't work in any case. I used following code now:
void _INSTANCE(ref,type) SV *ref; char *type; PROTOTYPE: $$ CODE: { STRLEN len; if( SvROK(ref) && type && ( ( len = strlen(type) ) > 0 ) ) { if( SvMAGICAL(ref) ) mg_get(ref); if( sv_isobject(ref) ) { if( sv_derived_from(ref,type) ) { ST(0) = ref; XSRETURN(1); } else { I32 isa_type = 0; int count; ENTER; SAVETMPS; PUSHMARK(SP); XPUSHs( sv_2mortal( newSVsv( ref ) ) ); XPUSHs( sv_2mortal( newSVpv( type, len ) ) ); PUTBACK; if( ( count = call_method("isa", G_SCALAR) ) ) { I32 oldax = ax; SPAGAIN; SP -= count; ax = (SP - PL_stack_base) + 1; isa_type = SvIV(ST(0)); ax = oldax; } PUTBACK; FREETMPS; LEAVE; if( isa_type ) { ST(0) = ref; XSRETURN(1); } } } } XSRETURN_UNDEF; }
Why is it necessary to save ax and why isn't it documented in perlcall (http://search.cpan.org/~rgarcia/perl-5.10.0/pod/perlcall.pod#Using_Perl_to_dispose_of_temporaries)? Did I understand sth. wrong and made another mistake?

Replies are listed 'Best First'.
Re: Why do I need storing ax over ENTER/SAVETMPS -- FREETMPS/LEAVE?
by almut (Canon) on Oct 22, 2008 at 23:35 UTC
    Why is it necessary to save ax

    As you're in an XSUB, the ax variable has automatically been setup (by xsubpp) to indicate the stack base offset, which is used by macros like ST and XSRETURN. It's usually a good idea to restore the variable to its original value after you've been messing with it, or else an ST/XSRETURN call further down might do bad things...

    Also see section Variables created by "xsubpp"... in perlapi.

Re: Why do I need storing ax over ENTER/SAVETMPS -- FREETMPS/LEAVE?
by Anonymous Monk on Oct 23, 2008 at 00:49 UTC
Re: Why do I need storing ax over ENTER/SAVETMPS -- FREETMPS/LEAVE?
by gone2015 (Deacon) on Oct 22, 2008 at 23:29 UTC

    I dunno, really. But in your position I'd try looking at the C preprocessor output to see what all those macros are up to. On gcc the -E option.