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

As a way to relax, I decide to do some dig on perl internal during my holiday. The first I try to do is Macro dSP which I often use in Inline::C. what it actually does behind dSP? Here is some clues:
#define dSP SV **sp = PL_stack_sp #define PL_stack_sp (vTHX->Istack_sp)
It seems that vTHX is a perlinterpreter instance, but I can't find definition of perlinterpreter struct and definition of Istack_sp in perl source directory.

Please help me!





I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: what definition of Istack_sp
by Anonymous Monk on Jan 12, 2019 at 20:29 UTC
      Thanks! I've found statement in intrpvar.h: PERLVAR(I, stack_sp, SV **) /* top of the stack */ and in perl.h:
      # define PERLVAR(prefix,var,type) type prefix##var; /* 'var' is an array of length 'n' */ # define PERLVARA(prefix,var,n,type) type prefix##var[n]; /* initialize 'var' to init' */ # define PERLVARI(prefix,var,type,init) type prefix##var; /* like PERLVARI, but make 'var' a const */ # define PERLVARIC(prefix,var,type,init) type prefix##var; struct interpreter { # include "intrpvar.h" };
      So perl use PERLVAR to define perlinterpreter struct, it's strange to me, why is it not define perlinterpreter structure directly in perl.h? Please enlighten me.




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

        The perl binary has multiple ways in which it can be built. The simplest (and original) model just defines PL_stack_sp directly as a global variable. But when built with MULTIPLICITY (when for example building a threaded perl), then multiple interpreters are allowed, and (within the core), PL_stack_sp is defined as my_perl->Ispack_sp.

        However in XS code (rather than in the core), it's more complex. At one point PL_stack_sp was defined as a function call, which would shield the XS code from changes in the interpreter struct layout across differing perl versions - so XS modules wouldn't have be to recompiled for each new perl release. However, this slowed XS code down a lot, and there were other things which also broke binary compatibility, so that was ditched and we just changed the binary guarantee to apply only across minor releases.

        Dave.