@@ -2826,40 +2826,145 @@ keywords are allowed.
28262826
28272827=head2 The XSUB Code Part
28282828
2829- XXX TBC
2830-
2831- XXX NB: the keywords described in L<XSUB Generic Keywords> and L<Sharing
2832- XSUB bodies> may also appear in this part.
2829+ Following an XSUB's optional init part, an optional code part follows. This
2830+ consists mainly of the C<CODE> or C<PPCODE> keywords, which provide the
2831+ code block for the main body of the XSUB. These two keywords are similar,
2832+ except that C<PPCODE> can be thought of as acting at a lower level; it
2833+ resets the stack pointer to the base of the stack frame and then relies on
2834+ the programmer to push any return values; whereas C<CODE> will (with
2835+ prompting) automatically generate code to return the value of C<RETVAL>.
2836+
2837+ There is also a rarely-used C<NOT_IMPLEMENTED_YET> keyword which generates
2838+ a body which croaks.
2839+
2840+ Only one of these keywords may appear in this part, and at most once; and
2841+ no other keywords are recognised in this part (although such keywords
2842+ could be instead be processed in the tail or head of the preceding and
2843+ following init and output parts).
2844+
2845+ In the absence of any of those three keywords, the XS compiler will
2846+ generate an autocall: a call to the C function of the same name as the
2847+ XSUB.
28332848
28342849=head3 Auto-calling a C function
28352850
2836- XXX TBC
2851+ In the absence of any explicit main body code via C<CODE> or C<PPCODE>,
2852+ the XS parser will generate a body for you automatically (this is referred
2853+ to as C<autocall> in this document). In its most basic form, the parser
2854+ assumes that the XSUB will be a simple wrapper for a C function of the
2855+ same name, with the same parameters and return type as the XSUB. So for
2856+ example, these two XSUB definitions are equivalent, but the first is an
2857+ autocall with less boilerplate needed:
28372858
2838- =head4 The C_ARGS: Keyword
2859+ int
2860+ foo(char *s, short flags)
28392861
2840- The C_ARGS: keyword allows creating of XSUBS which have different
2841- calling sequence from Perl than from C, without a need to write
2842- CODE: or PPCODE: section. The contents of the C_ARGS: paragraph is
2843- put as the argument to the called C function without any change.
2862+ int
2863+ foo(char *s, short flags)
2864+ CODE:
2865+ RETVAL = foo(s, flags);
2866+ OUTPUT:
2867+ RETVAL
28442868
2845- For example, suppose that a C function is declared as
2869+ Note that the XSUB C function and the wrapped C function are two
2870+ different entities; the first will have a name like C<XS_Foo__Bar_foo>;
2871+ when Perl code calls the 'Perl' function C<Foo::Bar::foo()>, behind the
2872+ scenes the Perl interpreter calls C<XS_Foo__Bar_foo()>, which extracts the
2873+ string and short int values from the two passed argument SVs, calls
2874+ C<foo()>, then stuffs its return value into an SV and returns that to the
2875+ Perl caller.
28462876
2847- symbolic nth_derivative(int n, symbolic function, int flags);
2877+ The two basic types of generated autocall code are:
28482878
2849- and that the default flags are kept in a global C variable
2850- C<default_flags>. Suppose that you want to create an interface which
2851- is called as
2879+ foo(a, b, c);
28522880
2853- $second_deriv = $function->nth_derivative(2 );
2881+ RETVAL = foo(a, b, c );
28542882
2855- To do this, declare the XSUB as
2883+ depending on whether the XSUB is declared C<void> or not. The variables
2884+ passed to the function are usually just the names of the XSUB's
2885+ parameters, in the same order. Parameters with default values are
2886+ included, while ellipses are ignored. So for example
28562887
2857- symbolic
2858- nth_derivative(function, n)
2859- symbolic function
2860- int n
2861- C_ARGS:
2862- n, function, default_flags
2888+ int
2889+ foo(int a, int b = 0, ...)
2890+
2891+ generates this autocall code:
2892+
2893+ RETVAL = foo(a, b);
2894+
2895+ There are various keywords which can be used to modify the basic behaviour
2896+ of an autocall.
2897+
2898+ =over
2899+
2900+ =item *
2901+
2902+ The L<PREFIX|/The MODULE Declaration> keyword, which allows wrapped C functions
2903+ which share a common prefix in their names to be mapped to perl functions
2904+ whose names don't have that prefix.
2905+
2906+ =item *
2907+
2908+ The
2909+ L<OUT|/"Updating and returning parameter values: the IN_OUT etc keywords">
2910+ etc parameter modifiers, which cause that parameter to be passed to the
2911+ autocalled function with a C<&> prefix, on the assumption that the
2912+ wrapped function expects a pointer and will update the location pointed
2913+ to.
2914+
2915+ =item *
2916+
2917+ The L<length(param_name)|/"The C<length(param_name)> pseudo-parameter">
2918+ pseudo-parameter, which allows the length of another parameter to be
2919+ passed as a separate argument to the wrapped function, even though it
2920+ isn't a parameter of the Perl function.
2921+
2922+ =item *
2923+
2924+ The L<C_ARGS|/"The C_ARGS: Keyword"> keyword, which allows the arguments
2925+ passed to the wrapped function to be completely overridden: handy when
2926+ arguments need to be skipped or reordered compared with the perl
2927+ function.
2928+
2929+ =item *
2930+
2931+ The L<INIT|/"The INIT: Keyword"> keyword, which allows code to be added
2932+ directly before the autocall.
2933+
2934+ =item *
2935+
2936+ The L<POSTCALL|/"The POSTCALL: Keyword"> keyword, which allows code to be
2937+ added directly after the autocall.
2938+
2939+ =item *
2940+
2941+ Support for L<C++|/"Using XS With C++"> XSUBs, which can (among other
2942+ things) modify the autocall into a C++ method call, e.g.
2943+ C<< THIS->foo(s,flags) >>.
2944+
2945+ =back
2946+
2947+
2948+ =head4 The C_ARGS: Keyword
2949+
2950+ void foo1(int a, int b, int c)
2951+ C_ARGS: b, a
2952+
2953+ void foo2(int a, int b)
2954+ C_ARGS: a < 0 ? 0 : a,
2955+ b,
2956+ 0
2957+
2958+ Normally the arguments for an autocall are generated automatically, based
2959+ on the XSUB's parameter declarations. The C<C_ARGS> keyword allows you to
2960+ override this and manually specify the text that will be placed between
2961+ the parentheses in the autocall. This is useful when the ordering and
2962+ nature of parameters varies between Perl and C, without a need to write a
2963+ C<CODE> or C<PPCODE> section.
2964+
2965+ The C<C_ARGS> section consists of all lines of text until the next keyword
2966+ or to the end of the XSUB, and is used without modification (except that
2967+ any POD or XS comments will be stripped).
28632968
28642969=head3 The CODE: Keyword
28652970
0 commit comments