Skip to content

Commit a0042e8

Browse files
committed
perlxs.pod: update: CODE, PPCODE
Rewrite these sections: =head3 The CODE: Keyword =head3 The PPCODE: Keyword
1 parent 1744e61 commit a0042e8

File tree

1 file changed

+189
-83
lines changed

1 file changed

+189
-83
lines changed

dist/ExtUtils-ParseXS/lib/perlxs.pod

Lines changed: 189 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2929,97 +2929,203 @@ any POD or XS comments will be stripped).
29292929

29302930
=head3 The CODE: Keyword
29312931

2932-
This keyword is used in more complicated XSUBs which require
2933-
special handling for the C function. The RETVAL variable is
2934-
still declared, but it will not be returned unless it is specified
2935-
in the OUTPUT: section.
2936-
2937-
The following XSUB is for a C function which requires special handling of
2938-
its parameters. The Perl usage is given first.
2939-
2940-
$status = rpcb_gettime("localhost", $timep);
2941-
2942-
The XSUB follows.
2943-
2944-
bool_t
2945-
rpcb_gettime(host, timep)
2946-
char *host
2947-
time_t timep
2932+
int
2933+
abs_double(int i)
29482934
CODE:
2949-
RETVAL = rpcb_gettime(host, &timep);
2935+
if (i < 0)
2936+
i = -i;
2937+
RETVAL = i * 2;
29502938
OUTPUT:
2951-
timep
2952-
RETVAL
2939+
RETVAL
2940+
2941+
The C<CODE> keyword is the usual mechanism for providing your own code as
2942+
the main body of the XSUB. It is typically used when the XSUB, rather than
2943+
wrapping a library function, is providing general functionality which can
2944+
be more easily or efficiently implemented in C than in Perl.
2945+
Alternatively, it can still be used to wrap a library function for cases
2946+
which are too complex for autocall to handle.
2947+
2948+
Note that on entry to the C<CODE> block of code, the values of any passed
2949+
arguments will have been assigned to auto variables, but the original SVs
2950+
will still be on the stack and accessible via C<ST(i)> if necessary.
2951+
2952+
Similarly to autocall XSUBs, a C<RETVAL> variable is declared if the
2953+
return value of the XSUB is not C<void>. Unlike autocall, you have to
2954+
explicitly tell the XS compiler to generate code to return the value of
2955+
C<RETVAL>, by using the The L<OUTPUT|/"The OUTPUT: Keyword"> keyword.
2956+
(Requiring this was probably a bad design decision, but we're stuck with
2957+
it now.) Newer XS parsers will warn if C<RETVAL> is seen in the C<CODE>
2958+
section without a corresponding C<OUTPUT> section.
2959+
2960+
A C<CODE> XSUB will typically return just the C<RETVAL> value (or possibly
2961+
more items with the C<OUTLIST> parameter modifiers). To take complete
2962+
control over returning values, you can use the C<PPCODE> keyword instead.
2963+
Note that it is possible for a C<CODE> section to do this too, by doing its
2964+
own stack manipulation and then doing an C<XSRETURN(n)> to return directly
2965+
while indicating that there are C<n> items on the stack. This bypasses the
2966+
normal C<XSRETURN(1)> etc that the XS parser will have planted after the
2967+
C<CODE> lines. But it is usually cleaner to use C<PPCODE> instead.
2968+
2969+
Any lines following C<CODE> until the next keyword (except POD and XS
2970+
comments) are copied out as-is to the C code file. Multiple C<CODE>
2971+
keywords are not allowed.
29532972

29542973
=head3 The PPCODE: Keyword
29552974

2956-
The PPCODE: keyword is an alternate form of the CODE: keyword and is used
2957-
to tell the B<xsubpp> compiler that the programmer is supplying the code to
2958-
control the argument stack for the XSUBs return values. Occasionally one
2959-
will want an XSUB to return a list of values rather than a single value.
2960-
In these cases one must use PPCODE: and then explicitly push the list of
2961-
values on the stack. The PPCODE: and CODE: keywords should not be used
2962-
together within the same XSUB.
2963-
2964-
The actual difference between PPCODE: and CODE: sections is in the
2965-
initialization of C<SP> macro (which stands for the I<current> Perl
2966-
stack pointer), and in the handling of data on the stack when returning
2967-
from an XSUB. In CODE: sections SP preserves the value which was on
2968-
entry to the XSUB: SP is on the function pointer (which follows the
2969-
last parameter). In PPCODE: sections SP is moved backward to the
2970-
beginning of the parameter list, which allows C<PUSH*()> macros
2971-
to place output values in the place Perl expects them to be when
2972-
the XSUB returns back to Perl.
2973-
2974-
The generated trailer for a CODE: section ensures that the number of return
2975-
values Perl will see is either 0 or 1 (depending on the C<void>ness of the
2976-
return value of the C function, and heuristics to work around CODE
2977-
setting C<ST(0)> on a C<void> XSUB. The trailer generated for a PPCODE: section
2978-
is based on the number of return values and on the number of times
2979-
C<SP> was updated by C<[X]PUSH*()> macros.
2980-
2981-
Note that macros C<ST(i)>, C<XST_m*()> and C<XSRETURN*()> work equally
2982-
well in CODE: sections and PPCODE: sections.
2983-
2984-
The following XSUB will call the C rpcb_gettime() function
2985-
and will return its two output values, timep and status, to
2986-
Perl as a single list.
2975+
# XS equivalent of: sub one_to_n { my $n = $_[0]; 1..$n }
29872976

29882977
void
2989-
rpcb_gettime(host)
2990-
char *host
2991-
PREINIT:
2992-
time_t timep;
2993-
bool_t status;
2978+
one_to_n(int n)
29942979
PPCODE:
2995-
status = rpcb_gettime(host, &timep);
2996-
EXTEND(SP, 2);
2997-
PUSHs(sv_2mortal(newSViv(status)));
2998-
PUSHs(sv_2mortal(newSViv(timep)));
2999-
3000-
Notice that the programmer must supply the C code necessary
3001-
to have the real rpcb_gettime() function called and to have
3002-
the return values properly placed on the argument stack.
3003-
3004-
The C<void> return type for this function tells the B<xsubpp> compiler that
3005-
the RETVAL variable is not needed or used and that it should not be created.
3006-
In most scenarios the void return type should be used with the PPCODE:
3007-
directive.
3008-
3009-
The EXTEND() macro is used to make room on the argument
3010-
stack for 2 return values. The PPCODE: directive causes the
3011-
B<xsubpp> compiler to create a stack pointer available as C<SP>, and it
3012-
is this pointer which is being used in the EXTEND() macro.
3013-
The values are then pushed onto the stack with the PUSHs()
3014-
macro.
3015-
3016-
Now the rpcb_gettime() function can be used from Perl with
3017-
the following statement.
3018-
3019-
($status, $timep) = rpcb_gettime("localhost");
3020-
3021-
When handling output parameters with a PPCODE section, be sure to handle
3022-
'set' magic properly. See L<perlguts> for details about 'set' magic.
2980+
{
2981+
int i;
2982+
if (n < 1)
2983+
Perl_croak_nocontext(
2984+
"one_to_n(): argument %d must be >= 1", n);
2985+
EXTEND(SP, n);
2986+
for (i = 1; i <= n; i++)
2987+
mPUSHi(i);
2988+
}
2989+
2990+
The C<PPCODE> keyword is similar to the C<CODE> keyword, except that on
2991+
entry it resets the stack pointer to the base of the current stack frame,
2992+
and it doesn't generate any code to return C<RETVAL> or similar: pushing
2993+
return values onto the stack is left to the programmer. In this way it can
2994+
be viewed as a lower-level alternative to C<CODE>, when you want to take
2995+
full control of manipulating the argument stack. The "PP" in its name
2996+
stands for "PUSH/PULL", reflecting the low-level stack manipulation.
2997+
C<PPCODE> is typically used when you want to return several values or even
2998+
an arbitrary list, compared with C<CODE>, which normally returns just the
2999+
value of C<RETVAL>.
3000+
3001+
The C<PPCODE> keyword must be the last keyword in the XSUB. Any lines
3002+
following C<PPCODE> until the end of the XSUB (except POD and XS comments)
3003+
are copied out as-is to the C code file. Multiple C<PPCODE> keywords are
3004+
not allowed.
3005+
3006+
Typically you declare a C<PPCODE> XSUB with a return type of C<void>; any
3007+
other return type will cause a C<RETVAL> auto variable of that type to be
3008+
declared, which will be otherwise unused.
3009+
3010+
On entry to the C<PPCODE> block of code, the values of any declared
3011+
parameters arguments will have already been assigned to auto variables,
3012+
but the original SVs will still be on the stack and initially accessible
3013+
via C<ST(i)> if necessary. But the default assumption for a C<PPCODE>
3014+
block is that you have already finished processing any supplied arguments,
3015+
and that you want to push a number of return values onto the stack. The
3016+
simple C<one_to_n()> example shown above is based on that assumption. But
3017+
more complex strategies are possible.
3018+
3019+
There are basically two ways to access and manipulate the stack in a
3020+
C<PPCODE> block. First, by using the C<ST(i)> macro, to get, modify, or
3021+
replace the I<i>th item in the current stack frame, and secondly to push
3022+
(usually temporary) return values onto the stack. The first uses the
3023+
hidden C<ax> variable, which is set on entry to the XSUB, and is the index
3024+
of the base of the current stack frame. This remains unchanged throughout
3025+
execution of the XSUB. The second approach uses the local stack pointer,
3026+
C<SP> (more on that below), which on entry to the C<PPCODE> block points
3027+
to the base of the stack frame. Macros like C<mPUSHi()> store a temporary
3028+
SV at that location, then increment C<SP>. On return from a C<PPCODE>
3029+
XSUB, the current value of C<SP> is used to indicate to the caller how
3030+
many values are being returned.
3031+
3032+
In general these two ways of accessing the stack should not be mixed, or
3033+
confusion is likely to arise. The PUSH strategy is most useful when you
3034+
have no further use for the passed arguments, and just want to generate
3035+
and return a list of values, as in the C<one_to_n()> example above. The
3036+
C<ST(i)> strategy is better when you still need to access the passed
3037+
arguments. In the example below,
3038+
3039+
# XS equivalent of: sub triple { map { $_ * 3} @_ }
3040+
3041+
void
3042+
triple(...)
3043+
PPCODE:
3044+
SP += items;
3045+
{
3046+
int i;
3047+
for (i = 0; i < items; i++) {
3048+
int val = (int)SvIV(ST(i));
3049+
ST(i) = sv_2mortal(newSViv(val*3));
3050+
}
3051+
}
3052+
3053+
C<SP> is first incremented to reclaim the passed arguments which are still
3054+
on the stack; then one by one, each passed argument is retrieved, and then
3055+
each stack slot is replaced with a new mortal value. When the loop is
3056+
finished, the current stack frame contains a list of mortals, which is
3057+
then returned to the caller, with C<SP> indicating how many items are
3058+
returned.
3059+
3060+
Before pushing return values onto the stack (or storing values at C<ST(i)>
3061+
locations higher than the number of passed arguments), it is necessary to
3062+
ensure there is sufficient space on the stack. This can be achieved either
3063+
through the C<EXTEND(SP, n)> macro as shown in the C<one_to_n()> example
3064+
above, or by using the 'X' variants of the push macros, such as
3065+
C<mXPUSHi()>, which can be used to check and extend the stack by one each
3066+
time. Doing a single C<EXTEND> in advance is more efficient. C<EXTEND>
3067+
will ensure that there is at least enough space on the stack for n further
3068+
items to be pushed.
3069+
3070+
If using the PUSH strategy, it is useful to understand in more detail how
3071+
pushing and the local stack pointer, C<SP> are implemented. The generated
3072+
C file will have access to (among others) the following macro definitions
3073+
or similar:
3074+
3075+
#define dSP SV **sp = PL_stack_sp
3076+
#define SP sp
3077+
#define PUSHs(s) *++sp = (s)
3078+
#define mPUSHi(i) sv_setiv(PUSHs(sv_newmortal()), (IV)(i))
3079+
#define PUTBACK PL_stack_sp = sp
3080+
#define SPAGAIN sp = PL_stack_sp
3081+
#define dXSARGS dSP; ....
3082+
3083+
The global (or per-interpreter) variable C<PL_stack_sp> is a pointer to
3084+
the current top-most entry on the stack, equal initially to
3085+
C<&ST(items-1)>. On entry to the XSUB, the C<dXSARGS> at its top will
3086+
cause the C<sp> variable to be declared and initialised. This becomes a
3087+
I<local> copy of the argument stack pointer. The standard stack
3088+
manipulation macros such as C<PUSHs> all use this local copy.
3089+
3090+
The XS parser will usually emit two lines of C code similar to these
3091+
around the PP code block lines:
3092+
3093+
SP -= items;
3094+
... PP lines ...
3095+
PUTBACK; return;
3096+
3097+
This has the effect of resetting the local copy of the stack pointer (but
3098+
I<not> the stack pointer itself) back to the base of the current stack
3099+
frame, discarding any passed arguments. The original arguments are still
3100+
on the stack. C<PUSHs()> etc will, starting at the base of the stack
3101+
frame, progressively overwrite any original arguments. Finally, the
3102+
C<PUTBACK> sets the real stack pointer to the copy, making the changes
3103+
permanent, and also allowing the caller to determine how many arguments
3104+
were returned.
3105+
3106+
Any functions called from the XSUB will only see the value of
3107+
C<PL_stack_sp> and not C<SP>. So when calling out to a function which
3108+
manipulates the stack, you may need to resynchronise the two; for example:
3109+
3110+
PUTBACK;
3111+
push_contents_of_array(av);
3112+
SPAGAIN;
3113+
3114+
The C<EXTEND(SP,n)> and C<mXPUSHfoo()> macros will update both
3115+
C<PL_stack_sp> and C<SP> if the extending causes the stack to be
3116+
reallocated.
3117+
3118+
Note that there are several C<mPUSHfoo()> macros, which generally create a
3119+
temporary SV, set its value to the argument, and push it onto the stack.
3120+
These are:
3121+
3122+
mPUSHs(sv) mortalise and push an SV
3123+
mPUSHi(iv) create+push mortal and set to the integer val
3124+
mPUSHu(uv) create+push mortal and set to the unsigned val
3125+
mPUSHn(n) create+push mortal and set to the num (float) val
3126+
mPUSHp(str, len) create+push mortal and set to the string+length
3127+
mPUSHpvs("string") create+push mortal and set to the literal string
3128+
(perl 5.38.0 onwards)
30233129

30243130
=head3 NOT_IMPLEMENTED_YET
30253131

0 commit comments

Comments
 (0)