@@ -2968,97 +2968,203 @@ any POD or XS comments will be stripped).
29682968
29692969=head3 The CODE: Keyword
29702970
2971- This keyword is used in more complicated XSUBs which require
2972- special handling for the C function. The RETVAL variable is
2973- still declared, but it will not be returned unless it is specified
2974- in the OUTPUT: section.
2975-
2976- The following XSUB is for a C function which requires special handling of
2977- its parameters. The Perl usage is given first.
2978-
2979- $status = rpcb_gettime("localhost", $timep);
2980-
2981- The XSUB follows.
2982-
2983- bool_t
2984- rpcb_gettime(host, timep)
2985- char *host
2986- time_t timep
2971+ int
2972+ abs_double(int i)
29872973 CODE:
2988- RETVAL = rpcb_gettime(host, &timep);
2974+ if (i < 0)
2975+ i = -i;
2976+ RETVAL = i * 2;
29892977 OUTPUT:
2990- timep
2991- RETVAL
2978+ RETVAL
2979+
2980+ The C<CODE> keyword is the usual mechanism for providing your own code as
2981+ the main body of the XSUB. It is typically used when the XSUB, rather than
2982+ wrapping a library function, is providing general functionality which can
2983+ be more easily or efficiently implemented in C than in Perl.
2984+ Alternatively, it can still be used to wrap a library function for cases
2985+ which are too complex for autocall to handle.
2986+
2987+ Note that on entry to the C<CODE> block of code, the values of any passed
2988+ arguments will have been assigned to auto variables, but the original SVs
2989+ will still be on the stack and accessible via C<ST(i)> if necessary.
2990+
2991+ Similarly to autocall XSUBs, a C<RETVAL> variable is declared if the
2992+ return value of the XSUB is not C<void>. Unlike autocall, you have to
2993+ explicitly tell the XS compiler to generate code to return the value of
2994+ C<RETVAL>, by using the The L<OUTPUT|/"The OUTPUT: Keyword"> keyword.
2995+ (Requiring this was probably a bad design decision, but we're stuck with
2996+ it now.) Newer XS parsers will warn if C<RETVAL> is seen in the C<CODE>
2997+ section without a corresponding C<OUTPUT> section.
2998+
2999+ A C<CODE> XSUB will typically return just the C<RETVAL> value (or possibly
3000+ more items with the C<OUTLIST> parameter modifiers). To take complete
3001+ control over returning values, you can use the C<PPCODE> keyword instead.
3002+ Note that it is possible for a C<CODE> section to do this too, by doing its
3003+ own stack manipulation and then doing an C<XSRETURN(n)> to return directly
3004+ while indicating that there are C<n> items on the stack. This bypasses the
3005+ normal C<XSRETURN(1)> etc that the XS parser will have planted after the
3006+ C<CODE> lines. But it is usually cleaner to use C<PPCODE> instead.
3007+
3008+ Any lines following C<CODE> until the next keyword (except POD and XS
3009+ comments) are copied out as-is to the C code file. Multiple C<CODE>
3010+ keywords are not allowed.
29923011
29933012=head3 The PPCODE: Keyword
29943013
2995- The PPCODE: keyword is an alternate form of the CODE: keyword and is used
2996- to tell the B<xsubpp> compiler that the programmer is supplying the code to
2997- control the argument stack for the XSUBs return values. Occasionally one
2998- will want an XSUB to return a list of values rather than a single value.
2999- In these cases one must use PPCODE: and then explicitly push the list of
3000- values on the stack. The PPCODE: and CODE: keywords should not be used
3001- together within the same XSUB.
3002-
3003- The actual difference between PPCODE: and CODE: sections is in the
3004- initialization of C<SP> macro (which stands for the I<current> Perl
3005- stack pointer), and in the handling of data on the stack when returning
3006- from an XSUB. In CODE: sections SP preserves the value which was on
3007- entry to the XSUB: SP is on the function pointer (which follows the
3008- last parameter). In PPCODE: sections SP is moved backward to the
3009- beginning of the parameter list, which allows C<PUSH*()> macros
3010- to place output values in the place Perl expects them to be when
3011- the XSUB returns back to Perl.
3012-
3013- The generated trailer for a CODE: section ensures that the number of return
3014- values Perl will see is either 0 or 1 (depending on the C<void>ness of the
3015- return value of the C function, and heuristics to work around CODE
3016- setting C<ST(0)> on a C<void> XSUB. The trailer generated for a PPCODE: section
3017- is based on the number of return values and on the number of times
3018- C<SP> was updated by C<[X]PUSH*()> macros.
3019-
3020- Note that macros C<ST(i)>, C<XST_m*()> and C<XSRETURN*()> work equally
3021- well in CODE: sections and PPCODE: sections.
3022-
3023- The following XSUB will call the C rpcb_gettime() function
3024- and will return its two output values, timep and status, to
3025- Perl as a single list.
3014+ # XS equivalent of: sub one_to_n { my $n = $_[0]; 1..$n }
30263015
30273016 void
3028- rpcb_gettime(host)
3029- char *host
3030- PREINIT:
3031- time_t timep;
3032- bool_t status;
3017+ one_to_n(int n)
30333018 PPCODE:
3034- status = rpcb_gettime(host, &timep);
3035- EXTEND(SP, 2);
3036- PUSHs(sv_2mortal(newSViv(status)));
3037- PUSHs(sv_2mortal(newSViv(timep)));
3038-
3039- Notice that the programmer must supply the C code necessary
3040- to have the real rpcb_gettime() function called and to have
3041- the return values properly placed on the argument stack.
3042-
3043- The C<void> return type for this function tells the B<xsubpp> compiler that
3044- the RETVAL variable is not needed or used and that it should not be created.
3045- In most scenarios the void return type should be used with the PPCODE:
3046- directive.
3047-
3048- The EXTEND() macro is used to make room on the argument
3049- stack for 2 return values. The PPCODE: directive causes the
3050- B<xsubpp> compiler to create a stack pointer available as C<SP>, and it
3051- is this pointer which is being used in the EXTEND() macro.
3052- The values are then pushed onto the stack with the PUSHs()
3053- macro.
3054-
3055- Now the rpcb_gettime() function can be used from Perl with
3056- the following statement.
3057-
3058- ($status, $timep) = rpcb_gettime("localhost");
3059-
3060- When handling output parameters with a PPCODE section, be sure to handle
3061- 'set' magic properly. See L<perlguts> for details about 'set' magic.
3019+ {
3020+ int i;
3021+ if (n < 1)
3022+ Perl_croak_nocontext(
3023+ "one_to_n(): argument %d must be >= 1", n);
3024+ EXTEND(SP, n);
3025+ for (i = 1; i <= n; i++)
3026+ mPUSHi(i);
3027+ }
3028+
3029+ The C<PPCODE> keyword is similar to the C<CODE> keyword, except that on
3030+ entry it resets the stack pointer to the base of the current stack frame,
3031+ and it doesn't generate any code to return C<RETVAL> or similar: pushing
3032+ return values onto the stack is left to the programmer. In this way it can
3033+ be viewed as a lower-level alternative to C<CODE>, when you want to take
3034+ full control of manipulating the argument stack. The "PP" in its name
3035+ stands for "PUSH/PULL", reflecting the low-level stack manipulation.
3036+ C<PPCODE> is typically used when you want to return several values or even
3037+ an arbitrary list, compared with C<CODE>, which normally returns just the
3038+ value of C<RETVAL>.
3039+
3040+ The C<PPCODE> keyword must be the last keyword in the XSUB. Any lines
3041+ following C<PPCODE> until the end of the XSUB (except POD and XS comments)
3042+ are copied out as-is to the C code file. Multiple C<PPCODE> keywords are
3043+ not allowed.
3044+
3045+ Typically you declare a C<PPCODE> XSUB with a return type of C<void>; any
3046+ other return type will cause a C<RETVAL> auto variable of that type to be
3047+ declared, which will be otherwise unused.
3048+
3049+ On entry to the C<PPCODE> block of code, the values of any declared
3050+ parameters arguments will have already been assigned to auto variables,
3051+ but the original SVs will still be on the stack and initially accessible
3052+ via C<ST(i)> if necessary. But the default assumption for a C<PPCODE>
3053+ block is that you have already finished processing any supplied arguments,
3054+ and that you want to push a number of return values onto the stack. The
3055+ simple C<one_to_n()> example shown above is based on that assumption. But
3056+ more complex strategies are possible.
3057+
3058+ There are basically two ways to access and manipulate the stack in a
3059+ C<PPCODE> block. First, by using the C<ST(i)> macro, to get, modify, or
3060+ replace the I<i>th item in the current stack frame, and secondly to push
3061+ (usually temporary) return values onto the stack. The first uses the
3062+ hidden C<ax> variable, which is set on entry to the XSUB, and is the index
3063+ of the base of the current stack frame. This remains unchanged throughout
3064+ execution of the XSUB. The second approach uses the local stack pointer,
3065+ C<SP> (more on that below), which on entry to the C<PPCODE> block points
3066+ to the base of the stack frame. Macros like C<mPUSHi()> store a temporary
3067+ SV at that location, then increment C<SP>. On return from a C<PPCODE>
3068+ XSUB, the current value of C<SP> is used to indicate to the caller how
3069+ many values are being returned.
3070+
3071+ In general these two ways of accessing the stack should not be mixed, or
3072+ confusion is likely to arise. The PUSH strategy is most useful when you
3073+ have no further use for the passed arguments, and just want to generate
3074+ and return a list of values, as in the C<one_to_n()> example above. The
3075+ C<ST(i)> strategy is better when you still need to access the passed
3076+ arguments. In the example below,
3077+
3078+ # XS equivalent of: sub triple { map { $_ * 3} @_ }
3079+
3080+ void
3081+ triple(...)
3082+ PPCODE:
3083+ SP += items;
3084+ {
3085+ int i;
3086+ for (i = 0; i < items; i++) {
3087+ int val = (int)SvIV(ST(i));
3088+ ST(i) = sv_2mortal(newSViv(val*3));
3089+ }
3090+ }
3091+
3092+ C<SP> is first incremented to reclaim the passed arguments which are still
3093+ on the stack; then one by one, each passed argument is retrieved, and then
3094+ each stack slot is replaced with a new mortal value. When the loop is
3095+ finished, the current stack frame contains a list of mortals, which is
3096+ then returned to the caller, with C<SP> indicating how many items are
3097+ returned.
3098+
3099+ Before pushing return values onto the stack (or storing values at C<ST(i)>
3100+ locations higher than the number of passed arguments), it is necessary to
3101+ ensure there is sufficient space on the stack. This can be achieved either
3102+ through the C<EXTEND(SP, n)> macro as shown in the C<one_to_n()> example
3103+ above, or by using the 'X' variants of the push macros, such as
3104+ C<mXPUSHi()>, which can be used to check and extend the stack by one each
3105+ time. Doing a single C<EXTEND> in advance is more efficient. C<EXTEND>
3106+ will ensure that there is at least enough space on the stack for n further
3107+ items to be pushed.
3108+
3109+ If using the PUSH strategy, it is useful to understand in more detail how
3110+ pushing and the local stack pointer, C<SP> are implemented. The generated
3111+ C file will have access to (among others) the following macro definitions
3112+ or similar:
3113+
3114+ #define dSP SV **sp = PL_stack_sp
3115+ #define SP sp
3116+ #define PUSHs(s) *++sp = (s)
3117+ #define mPUSHi(i) sv_setiv(PUSHs(sv_newmortal()), (IV)(i))
3118+ #define PUTBACK PL_stack_sp = sp
3119+ #define SPAGAIN sp = PL_stack_sp
3120+ #define dXSARGS dSP; ....
3121+
3122+ The global (or per-interpreter) variable C<PL_stack_sp> is a pointer to
3123+ the current top-most entry on the stack, equal initially to
3124+ C<&ST(items-1)>. On entry to the XSUB, the C<dXSARGS> at its top will
3125+ cause the C<sp> variable to be declared and initialised. This becomes a
3126+ I<local> copy of the argument stack pointer. The standard stack
3127+ manipulation macros such as C<PUSHs> all use this local copy.
3128+
3129+ The XS parser will usually emit two lines of C code similar to these
3130+ around the PP code block lines:
3131+
3132+ SP -= items;
3133+ ... PP lines ...
3134+ PUTBACK; return;
3135+
3136+ This has the effect of resetting the local copy of the stack pointer (but
3137+ I<not> the stack pointer itself) back to the base of the current stack
3138+ frame, discarding any passed arguments. The original arguments are still
3139+ on the stack. C<PUSHs()> etc will, starting at the base of the stack
3140+ frame, progressively overwrite any original arguments. Finally, the
3141+ C<PUTBACK> sets the real stack pointer to the copy, making the changes
3142+ permanent, and also allowing the caller to determine how many arguments
3143+ were returned.
3144+
3145+ Any functions called from the XSUB will only see the value of
3146+ C<PL_stack_sp> and not C<SP>. So when calling out to a function which
3147+ manipulates the stack, you may need to resynchronise the two; for example:
3148+
3149+ PUTBACK;
3150+ push_contents_of_array(av);
3151+ SPAGAIN;
3152+
3153+ The C<EXTEND(SP,n)> and C<mXPUSHfoo()> macros will update both
3154+ C<PL_stack_sp> and C<SP> if the extending causes the stack to be
3155+ reallocated.
3156+
3157+ Note that there are several C<mPUSHfoo()> macros, which generally create a
3158+ temporary SV, set its value to the argument, and push it onto the stack.
3159+ These are:
3160+
3161+ mPUSHs(sv) mortalise and push an SV
3162+ mPUSHi(iv) create+push mortal and set to the integer val
3163+ mPUSHu(uv) create+push mortal and set to the unsigned val
3164+ mPUSHn(n) create+push mortal and set to the num (float) val
3165+ mPUSHp(str, len) create+push mortal and set to the string+length
3166+ mPUSHpvs("string") create+push mortal and set to the literal string
3167+ (perl 5.38.0 onwards)
30623168
30633169=head3 NOT_IMPLEMENTED_YET
30643170
0 commit comments