5 xsubpp - compiler to convert Perl XS code into C code
9 B<xsubpp> [B<-v>] [B<-C++>] [B<-except>] [B<-s pattern>] [B<-prototypes>] [B<-noversioncheck>] [B<-nolinenumbers>] [B<-nooptimize>] [B<-typemap typemap>] ... file.xs
13 This compiler is typically run by the makefiles created by L<ExtUtils::MakeMaker>.
15 I<xsubpp> will compile XS code into C code by embedding the constructs
16 necessary to let C functions manipulate Perl values and creates the glue
17 necessary to let Perl access those functions. The compiler uses typemaps to
18 determine how to map C function parameters and variables to Perl values.
20 The compiler will search for typemap files called I<typemap>. It will use
21 the following search path to find default typemaps, with the rightmost
22 typemap taking precedence.
24 ../../../typemap:../../typemap:../typemap:typemap
28 Note that the C<XSOPT> MakeMaker option may be used to add these options to
29 any makefiles generated by MakeMaker.
35 Adds ``extern "C"'' to the C code.
39 Adds exception handling stubs to the C code.
41 =item B<-typemap typemap>
43 Indicates that a user-supplied typemap should take precedence over the
44 default typemaps. This option may be used multiple times, with the last
45 typemap having the highest precedence.
49 Prints the I<xsubpp> version number to standard output, then exits.
53 By default I<xsubpp> will not automatically generate prototype code for
54 all xsubs. This flag will enable prototypes.
56 =item B<-noversioncheck>
58 Disables the run time test that determines if the object file (derived
59 from the C<.xs> file) and the C<.pm> files have the same version
62 =item B<-nolinenumbers>
64 Prevents the inclusion of `#line' directives in the output.
68 Disables certain optimizations. The only optimization that is currently
69 affected is the use of I<target>s by the output C code (see L<perlguts>).
70 This may significantly slow down the generated code, but this is the way
71 B<xsubpp> of 5.005 and earlier operated.
75 Disable recognition of C<IN>, C<OUT_LIST> and C<INOUT_LIST> declarations.
79 Disable recognition of ANSI-like descriptions of function signature.
85 No environment variables are used.
91 =head1 MODIFICATION HISTORY
93 See the file F<changes.pod>.
97 perl(1), perlxs(1), perlxstut(1)
103 use vars '$cplusplus';
112 $XSUBPP_version = "1.9508";
114 my ($Is_VMS, $SymSet);
117 # Establish set of global symbols with max length 28, since xsubpp
118 # will later add the 'XS_' prefix.
119 require ExtUtils::XSSymSet;
120 $SymSet = new ExtUtils::XSSymSet 28;
125 $usage = "Usage: xsubpp [-v] [-C++] [-except] [-prototypes] [-noversioncheck] [-nolinenumbers] [-nooptimize] [-noinout] [-noargtypes] [-s pattern] [-typemap typemap]... file.xs\n";
127 $proto_re = "[" . quotemeta('\$%&*@;') . "]" ;
129 $OBJ = 1 if $Config{'ccflags'} =~ /PERL_OBJECT/i;
132 $WantPrototypes = -1 ;
133 $WantVersionChk = 1 ;
135 $WantLineNumbers = 1 ;
138 my $process_inout = 1;
139 my $process_argtypes = 1;
141 SWITCH: while (@ARGV and $ARGV[0] =~ /^-./) {
144 $spat = quotemeta shift, next SWITCH if $flag eq 's';
145 $cplusplus = 1, next SWITCH if $flag eq 'C++';
146 $WantPrototypes = 0, next SWITCH if $flag eq 'noprototypes';
147 $WantPrototypes = 1, next SWITCH if $flag eq 'prototypes';
148 $WantVersionChk = 0, next SWITCH if $flag eq 'noversioncheck';
149 $WantVersionChk = 1, next SWITCH if $flag eq 'versioncheck';
150 # XXX left this in for compat
151 $WantCAPI = 1, next SWITCH if $flag eq 'object_capi';
152 $except = " TRY", next SWITCH if $flag eq 'except';
153 push(@tm,shift), next SWITCH if $flag eq 'typemap';
154 $WantLineNumbers = 0, next SWITCH if $flag eq 'nolinenumbers';
155 $WantLineNumbers = 1, next SWITCH if $flag eq 'linenumbers';
156 $WantOptimize = 0, next SWITCH if $flag eq 'nooptimize';
157 $WantOptimize = 1, next SWITCH if $flag eq 'optimize';
158 $process_inout = 0, next SWITCH if $flag eq 'noinout';
159 $process_inout = 1, next SWITCH if $flag eq 'inout';
160 $process_argtypes = 0, next SWITCH if $flag eq 'noargtypes';
161 $process_argtypes = 1, next SWITCH if $flag eq 'argtypes';
162 (print "xsubpp version $XSUBPP_version\n"), exit
166 if ($WantPrototypes == -1)
167 { $WantPrototypes = 0}
172 @ARGV == 1 or die $usage;
173 ($dir, $filename) = $ARGV[0] =~ m#(.*)/(.*)#
174 or ($dir, $filename) = $ARGV[0] =~ m#(.*)\\(.*)#
175 or ($dir, $filename) = $ARGV[0] =~ m#(.*[>\]])(.*)#
176 or ($dir, $filename) = ('.', $ARGV[0]);
180 ++ $IncludedFiles{$ARGV[0]} ;
182 my(@XSStack) = ({type => 'none'}); # Stack of conditionals and INCLUDEs
183 my($XSS_work_idx, $cpp_next_tmp) = (0, "XSubPPtmpAAAA");
188 $_[0] =~ s/^\s+|\s+$//go ;
195 # rationalise any '*' by joining them into bunches and removing whitespace
199 # change multiple whitespace into a single space
202 # trim leading & trailing whitespace
208 $typemap = shift @ARGV;
209 foreach $typemap (@tm) {
210 die "Can't find $typemap in $pwd\n" unless -r $typemap;
212 unshift @tm, qw(../../../../lib/ExtUtils/typemap ../../../lib/ExtUtils/typemap
213 ../../lib/ExtUtils/typemap ../../../typemap ../../typemap
215 foreach $typemap (@tm) {
216 next unless -e $typemap ;
217 # skip directories, binary files etc.
218 warn("Warning: ignoring non-text typemap file '$typemap'\n"), next
220 open(TYPEMAP, $typemap)
221 or warn ("Warning: could not open typemap file '$typemap': $!\n"), next;
227 my $line_no = $. + 1;
228 if (/^INPUT\s*$/) { $mode = 'Input'; $current = \$junk; next; }
229 if (/^OUTPUT\s*$/) { $mode = 'Output'; $current = \$junk; next; }
230 if (/^TYPEMAP\s*$/) { $mode = 'Typemap'; $current = \$junk; next; }
231 if ($mode eq 'Typemap') {
235 # skip blank lines and comment lines
236 next if /^$/ or /^#/ ;
237 my($type,$kind, $proto) = /^\s*(.*?\S)\s+(\S+)\s*($proto_re*)\s*$/ or
238 warn("Warning: File '$typemap' Line $. '$line' TYPEMAP entry needs 2 or 3 columns\n"), next;
239 $type = TidyType($type) ;
240 $type_kind{$type} = $kind ;
241 # prototype defaults to '$'
242 $proto = "\$" unless $proto ;
243 warn("Warning: File '$typemap' Line $. '$line' Invalid prototype '$proto'\n")
244 unless ValidProtoString($proto) ;
245 $proto_letter{$type} = C_string($proto) ;
250 elsif ($mode eq 'Input') {
252 $input_expr{$_} = '';
253 $current = \$input_expr{$_};
257 $output_expr{$_} = '';
258 $current = \$output_expr{$_};
264 foreach $key (keys %input_expr) {
265 $input_expr{$key} =~ s/\n+$//;
268 $bal = qr[(?:(?>[^()]+)|\((??{ $bal })\))*]; # ()-balanced
269 $cast = qr[(?:\(\s*SV\s*\*\s*\)\s*)?]; # Optional (SV*) cast
270 $size = qr[,\s* (??{ $bal }) ]x; # Third arg (to setpvn)
272 foreach $key (keys %output_expr) {
275 my ($t, $with_size, $arg, $sarg) =
276 ($output_expr{$key} =~
277 m[^ \s+ sv_set ( [iunp] ) v (n)? # Type, is_setpvn
278 \s* \( \s* $cast \$arg \s* ,
279 \s* ( (??{ $bal }) ) # Set from
280 ( (??{ $size }) )? # Possible sizeof set-from
283 $targetable{$key} = [$t, $with_size, $arg, $sarg] if $t;
286 $END = "!End!\n\n"; # "impossible" keyword (multiple newline)
288 # Match an XS keyword
289 $BLOCK_re= '\s*(' . join('|', qw(
290 REQUIRE BOOT CASE PREINIT INPUT INIT CODE PPCODE OUTPUT
291 CLEANUP ALIAS ATTRS PROTOTYPES PROTOTYPE VERSIONCHECK INCLUDE
292 SCOPE INTERFACE INTERFACE_MACRO C_ARGS POSTCALL
295 # Input: ($_, @line) == unparsed input.
296 # Output: ($_, @line) == (rest of line, following lines).
297 # Return: the matched keyword if found, otherwise 0
299 $_ = shift(@line) while !/\S/ && @line;
300 s/^(\s*)($_[0])\s*:\s*(?:#.*)?/$1/s && $2;
303 my ($C_group_rex, $C_arg);
304 # Group in C (no support for comments or literals)
305 $C_group_rex = qr/ [({\[]
306 (?: (?> [^()\[\]{}]+ ) | (??{ $C_group_rex }) )*
308 # Chunk in C without comma at toplevel (no comments):
309 $C_arg = qr/ (?: (?> [^()\[\]{},"']+ )
310 | (??{ $C_group_rex })
311 | " (?: (?> [^\\"]+ )
313 )* " # String literal
314 | ' (?: (?> [^\\']+ ) | \\. )* ' # Char literal
317 if ($WantLineNumbers) {
319 package xsubpp::counter;
321 my ($class, $cfile) = @_;
323 $SECTION_END_MARKER = "#line --- \"$cfile\"";
332 while ($$self =~ s/^([^\n]*\n)//) {
335 $line =~ s|^\#line\s+---(?=\s)|#line $line_no|;
344 $self->PRINT(sprintf($fmt, @_));
348 # Not necessary if we're careful to end with a "\n"
354 my $cfile = $filename;
355 $cfile =~ s/\.xs$/.c/i or $cfile .= ".c";
356 tie(*PSEUDO_STDOUT, 'xsubpp::counter', $cfile);
357 select PSEUDO_STDOUT;
361 # the "do" is required for right semantics
362 do { $_ = shift(@line) } while !/\S/ && @line;
364 print("#line ", $line_no[@line_no - @line -1], " \"$filename\"\n")
365 if $WantLineNumbers && !/^\s*#\s*line\b/ && !/^#if XSubPPtmp/;
366 for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) {
369 print "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers;
375 while (!/\S/ && @line) {
379 for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) {
386 sub process_keyword($)
391 &{"${kwd}_handler"}()
392 while $kwd = check_keyword($pattern) ;
396 blurt ("Error: `CASE:' after unconditional `CASE:'")
397 if $condnum && $cond eq '';
399 TrimWhitespace($cond);
400 print " ", ($condnum++ ? " else" : ""), ($cond ? " if ($cond)\n" : "\n");
405 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
406 last if /^\s*NOT_IMPLEMENTED_YET/;
407 next unless /\S/; # skip blank lines
412 # remove trailing semicolon if no initialisation
413 s/\s*;$//g unless /[=;+].*\S/ ;
415 # check for optional initialisation code
417 $var_init = $1 if s/\s*([=;+].*)$//s ;
418 $var_init =~ s/"/\\"/g;
421 my ($var_type, $var_addr, $var_name) = /^(.*?[^&\s])\s*(\&?)\s*\b(\w+)$/s
422 or blurt("Error: invalid argument declaration '$line'"), next;
424 # Check for duplicate definitions
425 blurt ("Error: duplicate definition of argument '$var_name' ignored"), next
426 if $arg_list{$var_name}++
427 or defined $arg_types{$var_name} and not $processing_arg_with_types;
429 $thisdone |= $var_name eq "THIS";
430 $retvaldone |= $var_name eq "RETVAL";
431 $var_types{$var_name} = $var_type;
432 # XXXX This check is a safeguard against the unfinished conversion of
433 # generate_init(). When generate_init() is fixed,
434 # one can use 2-args map_type() unconditionally.
435 if ($var_type =~ / \( \s* \* \s* \) /x) {
436 # Function pointers are not yet supported with &output_init!
437 print "\t" . &map_type($var_type, $var_name);
440 print "\t" . &map_type($var_type);
443 $var_num = $args_match{$var_name};
445 $proto_arg[$var_num] = ProtoString($var_type)
447 $func_args =~ s/\b($var_name)\b/&$1/ if $var_addr;
448 if ($var_init =~ /^[=;]\s*NO_INIT\s*;?\s*$/
449 or $in_out{$var_name} and $in_out{$var_name} =~ /^OUT/
450 and $var_init !~ /\S/) {
454 print "\t$var_name;\n";
456 } elsif ($var_init =~ /\S/) {
457 &output_init($var_type, $var_num, $var_name, $var_init, $name_printed);
459 # generate initialization code
460 &generate_init($var_type, $var_num, $var_name, $name_printed);
468 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
470 if (/^\s*SETMAGIC\s*:\s*(ENABLE|DISABLE)\s*/) {
471 $DoSetMagic = ($1 eq "ENABLE" ? 1 : 0);
474 my ($outarg, $outcode) = /^\s*(\S+)\s*(.*?)\s*$/s ;
475 blurt ("Error: duplicate OUTPUT argument '$outarg' ignored"), next
476 if $outargs{$outarg} ++ ;
477 if (!$gotRETVAL and $outarg eq 'RETVAL') {
478 # deal with RETVAL last
479 $RETVAL_code = $outcode ;
483 blurt ("Error: OUTPUT $outarg not an argument"), next
484 unless defined($args_match{$outarg});
485 blurt("Error: No input definition for OUTPUT argument '$outarg' - ignored"), next
486 unless defined $var_types{$outarg} ;
487 $var_num = $args_match{$outarg};
489 print "\t$outcode\n";
490 print "\tSvSETMAGIC(ST(" , $var_num-1 , "));\n" if $DoSetMagic;
492 &generate_output($var_types{$outarg}, $var_num, $outarg, $DoSetMagic);
494 delete $in_out{$outarg} # No need to auto-OUTPUT
495 if exists $in_out{$outarg} and $in_out{$outarg} =~ /OUT$/;
499 sub C_ARGS_handler() {
500 my $in = merge_section();
506 sub INTERFACE_MACRO_handler() {
507 my $in = merge_section();
510 if ($in =~ /\s/) { # two
511 ($interface_macro, $interface_macro_set) = split ' ', $in;
513 $interface_macro = $in;
514 $interface_macro_set = 'UNKNOWN_CVT'; # catch later
516 $interface = 1; # local
517 $Interfaces = 1; # global
520 sub INTERFACE_handler() {
521 my $in = merge_section();
525 foreach (split /[\s,]+/, $in) {
526 $Interfaces{$_} = $_;
529 # XSFUNCTION = $interface_macro($ret_type,cv,XSANY.any_dptr);
531 $interface = 1; # local
532 $Interfaces = 1; # global
535 sub CLEANUP_handler() { print_section() }
536 sub PREINIT_handler() { print_section() }
537 sub POSTCALL_handler() { print_section() }
538 sub INIT_handler() { print_section() }
547 # Parse alias definitions
549 # alias = value alias = value ...
551 while ($line =~ s/^\s*([\w:]+)\s*=\s*(\w+)\s*//) {
553 $orig_alias = $alias ;
556 # check for optional package definition in the alias
557 $alias = $Packprefix . $alias if $alias !~ /::/ ;
559 # check for duplicate alias name & duplicate value
560 Warn("Warning: Ignoring duplicate alias '$orig_alias'")
561 if defined $XsubAliases{$alias} ;
563 Warn("Warning: Aliases '$orig_alias' and '$XsubAliasValues{$value}' have identical values")
564 if $XsubAliasValues{$value} ;
567 $XsubAliases{$alias} = $value ;
568 $XsubAliasValues{$value} = $orig_alias ;
571 blurt("Error: Cannot parse ALIAS definitions from '$orig'")
577 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
580 push @Attributes, $_;
586 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
589 GetAliases($_) if $_ ;
593 sub REQUIRE_handler ()
595 # the rest of the current line should contain a version number
598 TrimWhitespace($Ver) ;
600 death ("Error: REQUIRE expects a version number")
603 # check that the version number is of the form n.n
604 death ("Error: REQUIRE: expected a number, got '$Ver'")
605 unless $Ver =~ /^\d+(\.\d*)?/ ;
607 death ("Error: xsubpp $Ver (or better) required--this is only $XSUBPP_version.")
608 unless $XSUBPP_version >= $Ver ;
611 sub VERSIONCHECK_handler ()
613 # the rest of the current line should contain either ENABLE or
618 # check for ENABLE/DISABLE
619 death ("Error: VERSIONCHECK: ENABLE/DISABLE")
620 unless /^(ENABLE|DISABLE)/i ;
622 $WantVersionChk = 1 if $1 eq 'ENABLE' ;
623 $WantVersionChk = 0 if $1 eq 'DISABLE' ;
627 sub PROTOTYPE_handler ()
631 death("Error: Only 1 PROTOTYPE definition allowed per xsub")
632 if $proto_in_this_xsub ++ ;
634 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
638 if ($_ eq 'DISABLE') {
641 elsif ($_ eq 'ENABLE') {
645 # remove any whitespace
647 death("Error: Invalid prototype '$_'")
648 unless ValidProtoString($_) ;
649 $ProtoThisXSUB = C_string($_) ;
653 # If no prototype specified, then assume empty prototype ""
654 $ProtoThisXSUB = 2 unless $specified ;
662 death("Error: Only 1 SCOPE declaration allowed per xsub")
663 if $scope_in_this_xsub ++ ;
665 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
668 if ($_ =~ /^DISABLE/i) {
671 elsif ($_ =~ /^ENABLE/i) {
678 sub PROTOTYPES_handler ()
680 # the rest of the current line should contain either ENABLE or
685 # check for ENABLE/DISABLE
686 death ("Error: PROTOTYPES: ENABLE/DISABLE")
687 unless /^(ENABLE|DISABLE)/i ;
689 $WantPrototypes = 1 if $1 eq 'ENABLE' ;
690 $WantPrototypes = 0 if $1 eq 'DISABLE' ;
695 sub INCLUDE_handler ()
697 # the rest of the current line should contain a valid filename
701 death("INCLUDE: filename missing")
704 death("INCLUDE: output pipe is illegal")
707 # simple minded recursion detector
708 death("INCLUDE loop detected")
709 if $IncludedFiles{$_} ;
711 ++ $IncludedFiles{$_} unless /\|\s*$/ ;
713 # Save the current file context.
716 LastLine => $lastline,
717 LastLineNo => $lastline_no,
720 Filename => $filename,
727 open ($FH, "$_") or death("Cannot open '$_': $!") ;
731 #/* INCLUDE: Including '$_' from '$filename' */
737 # Prime the pump by reading the first
740 # skip leading blank lines
742 last unless /^\s*$/ ;
752 return 0 unless $XSStack[-1]{type} eq 'file' ;
754 my $data = pop @XSStack ;
755 my $ThisFile = $filename ;
756 my $isPipe = ($filename =~ /\|\s*$/) ;
758 -- $IncludedFiles{$filename}
763 $FH = $data->{Handle} ;
764 $filename = $data->{Filename} ;
765 $lastline = $data->{LastLine} ;
766 $lastline_no = $data->{LastLineNo} ;
767 @line = @{ $data->{Line} } ;
768 @line_no = @{ $data->{LineNo} } ;
770 if ($isPipe and $? ) {
772 print STDERR "Error reading from pipe '$ThisFile': $! in $filename, line $lastline_no\n" ;
778 #/* INCLUDE: Returning to '$filename' from '$ThisFile' */
785 sub ValidProtoString ($)
789 if ( $string =~ /^$proto_re+$/ ) {
800 $string =~ s[\\][\\\\]g ;
808 $proto_letter{$type} or "\$" ;
812 my @cpp = grep(/^\#\s*(?:if|e\w+)/, @line);
814 my ($cpp, $cpplevel);
816 if ($cpp =~ /^\#\s*if/) {
818 } elsif (!$cpplevel) {
819 Warn("Warning: #else/elif/endif without #if in this function");
820 print STDERR " (precede it with a blank line if the matching #if is outside the function)\n"
821 if $XSStack[-1]{type} eq 'if';
823 } elsif ($cpp =~ /^\#\s*endif/) {
827 Warn("Warning: #if without #endif in this function") if $cpplevel;
840 open($FH, $filename) or die "cannot open $filename: $!\n";
842 # Identify the version of xsubpp used
845 * This file was generated automatically by xsubpp version $XSUBPP_version from the
846 * contents of $filename. Do not edit this file, edit $filename instead.
848 * ANY CHANGES MADE HERE WILL BE LOST!
855 print("#line 1 \"$filename\"\n")
861 my $podstartline = $.;
864 print("/* Skipped embedded POD. */\n");
865 printf("#line %d \"$filename\"\n", $. + 1)
871 # At this point $. is at end of file so die won't state the start
872 # of the problem, and as we haven't yet read any lines &death won't
873 # show the correct line in the message either.
874 die ("Error: Unterminated pod in $filename, line $podstartline\n")
877 last if ($Module, $Package, $Prefix) =
878 /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/;
881 s/#if(?:def\s|\s+defined)\s*(\(__cplusplus\)|__cplusplus)/#if defined(__cplusplus) && !defined(PERL_OBJECT)/;
885 &Exit unless defined $_;
887 print "$xsubpp::counter::SECTION_END_MARKER\n" if $WantLineNumbers;
892 # Read next xsub into @line from ($lastline, <$FH>).
895 death ("Error: Unterminated `#if/#ifdef/#ifndef'")
896 if !defined $lastline && $XSStack[-1]{type} eq 'if';
899 return PopFile() if !defined $lastline;
902 /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) {
904 $Package = defined($2) ? $2 : ''; # keep -w happy
905 $Prefix = defined($3) ? $3 : ''; # keep -w happy
906 $Prefix = quotemeta $Prefix ;
907 ($Module_cname = $Module) =~ s/\W/_/g;
908 ($Packid = $Package) =~ tr/:/_/;
909 $Packprefix = $Package;
910 $Packprefix .= "::" if $Packprefix ne "";
916 while ($lastline =~ /^=/) {
917 while ($lastline = <$FH>) {
918 last if ($lastline =~ /^=cut\s*$/);
920 death ("Error: Unterminated pod") unless $lastline;
923 $lastline =~ s/^\s+$//;
925 if ($lastline !~ /^\s*#/ ||
927 # ANSI: if ifdef ifndef elif else endif define undef
929 # gcc: warning include_next
931 # others: ident (gcc notes that some cpps have this one)
932 $lastline =~ /^#[ \t]*(?:(?:if|ifn?def|elif|else|endif|define|undef|pragma|error|warning|line\s+\d+|ident)\b|(?:include(?:_next)?|import)\s*["<].*[>"])/) {
933 last if $lastline =~ /^\S/ && @line && $line[-1] eq "";
934 push(@line, $lastline);
935 push(@line_no, $lastline_no) ;
938 # Read next line and continuation lines
939 last unless defined($lastline = <$FH>);
942 $lastline .= $tmp_line
943 while ($lastline =~ /\\$/ && defined($tmp_line = <$FH>));
946 $lastline =~ s/^\s+$//;
948 pop(@line), pop(@line_no) while @line && $line[-1] eq "";
953 while (fetch_para()) {
954 # Print initial preprocessor statements and blank lines
955 while (@line && $line[0] !~ /^[^\#]/) {
956 my $line = shift(@line);
958 next unless $line =~ /^\#\s*((if)(?:n?def)?|elsif|else|endif)\b/;
960 if ($statement eq 'if') {
961 $XSS_work_idx = @XSStack;
962 push(@XSStack, {type => 'if'});
964 death ("Error: `$statement' with no matching `if'")
965 if $XSStack[-1]{type} ne 'if';
966 if ($XSStack[-1]{varname}) {
967 push(@InitFileCode, "#endif\n");
968 push(@BootCode, "#endif");
971 my(@fns) = keys %{$XSStack[-1]{functions}};
972 if ($statement ne 'endif') {
973 # Hide the functions defined in other #if branches, and reset.
974 @{$XSStack[-1]{other_functions}}{@fns} = (1) x @fns;
975 @{$XSStack[-1]}{qw(varname functions)} = ('', {});
977 my($tmp) = pop(@XSStack);
978 0 while (--$XSS_work_idx
979 && $XSStack[$XSS_work_idx]{type} ne 'if');
980 # Keep all new defined functions
981 push(@fns, keys %{$tmp->{other_functions}});
982 @{$XSStack[$XSS_work_idx]{functions}}{@fns} = (1) x @fns;
987 next PARAGRAPH unless @line;
989 if ($XSS_work_idx && !$XSStack[$XSS_work_idx]{varname}) {
990 # We are inside an #if, but have not yet #defined its xsubpp variable.
991 print "#define $cpp_next_tmp 1\n\n";
992 push(@InitFileCode, "#if $cpp_next_tmp\n");
993 push(@BootCode, "#if $cpp_next_tmp");
994 $XSStack[$XSS_work_idx]{varname} = $cpp_next_tmp++;
997 death ("Code is not inside a function"
998 ." (maybe last function was ended by a blank line "
999 ." followed by a a statement on column one?)")
1000 if $line[0] =~ /^\s/;
1002 # initialize info arrays
1009 undef($wantRETVAL) ;
1010 undef($RETVAL_no_return) ;
1013 undef(@arg_with_types) ;
1014 undef($processing_arg_with_types) ;
1018 undef($proto_in_this_xsub) ;
1019 undef($scope_in_this_xsub) ;
1021 undef($prepush_done);
1022 $interface_macro = 'XSINTERFACE_FUNC' ;
1023 $interface_macro_set = 'XSINTERFACE_FUNC_SET' ;
1024 $ProtoThisXSUB = $WantPrototypes ;
1029 while ($kwd = check_keyword("REQUIRE|PROTOTYPES|VERSIONCHECK|INCLUDE")) {
1030 &{"${kwd}_handler"}() ;
1031 next PARAGRAPH unless @line ;
1035 if (check_keyword("BOOT")) {
1037 push (@BootCode, "#line $line_no[@line_no - @line] \"$filename\"")
1038 if $WantLineNumbers && $line[0] !~ /^\s*#\s*line\b/;
1039 push (@BootCode, @line, "") ;
1044 # extract return type, function name and arguments
1045 ($ret_type) = TidyType($_);
1046 $RETVAL_no_return = 1 if $ret_type =~ s/^NO_OUTPUT\s+//;
1048 # Allow one-line ANSI-like declaration
1050 if $process_argtypes
1051 and $ret_type =~ s/^(.*?\w.*?)\s*\b(\w+\s*\(.*)/$1/s;
1053 # a function definition needs at least 2 lines
1054 blurt ("Error: Function definition too short '$ret_type'"), next PARAGRAPH
1057 $static = 1 if $ret_type =~ s/^static\s+//;
1059 $func_header = shift(@line);
1060 blurt ("Error: Cannot parse function definition from '$func_header'"), next PARAGRAPH
1061 unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*(const)?\s*(;\s*)?$/s;
1063 ($class, $func_name, $orig_args) = ($1, $2, $3) ;
1064 $class = "$4 $class" if $4;
1065 ($pname = $func_name) =~ s/^($Prefix)?/$Packprefix/;
1066 ($clean_func_name = $func_name) =~ s/^$Prefix//;
1067 $Full_func_name = "${Packid}_$clean_func_name";
1068 if ($Is_VMS) { $Full_func_name = $SymSet->addsym($Full_func_name); }
1070 # Check for duplicate function definition
1071 for $tmp (@XSStack) {
1072 next unless defined $tmp->{functions}{$Full_func_name};
1073 Warn("Warning: duplicate function definition '$clean_func_name' detected");
1076 $XSStack[$XSS_work_idx]{functions}{$Full_func_name} ++ ;
1077 %XsubAliases = %XsubAliasValues = %Interfaces = @Attributes = ();
1080 $orig_args =~ s/\\\s*/ /g; # process line continuations
1083 if ($process_argtypes and $orig_args =~ /\S/) {
1084 my $args = "$orig_args ,";
1085 if ($args =~ /^( (??{ $C_arg }) , )* $ /x) {
1086 @args = ($args =~ /\G ( (??{ $C_arg }) ) , /xg);
1092 ($arg, $default) = / ( [^=]* ) ( (?: = .* )? ) /x;
1093 my ($pre, $name) = ($arg =~ /(.*?) \s* \b(\w+) \s* $ /x);
1094 next unless length $pre;
1097 if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//) {
1099 $out_type = $type if $type ne 'IN';
1100 $arg =~ s/^(IN|IN_OUTLIST|OUTLIST|OUT|IN_OUT)\s+//;
1102 if (/\W/) { # Has a type
1103 push @arg_with_types, $arg;
1104 # warn "pushing '$arg'\n";
1105 $arg_types{$name} = $arg;
1106 $_ = "$name$default";
1108 $only_outlist{$_} = 1 if $out_type eq "OUTLIST";
1109 push @outlist, $name if $out_type =~ /OUTLIST$/;
1110 $in_out{$name} = $out_type if $out_type;
1113 @args = split(/\s*,\s*/, $orig_args);
1114 Warn("Warning: cannot parse argument list '$orig_args', fallback to split");
1117 @args = split(/\s*,\s*/, $orig_args);
1119 if ($process_inout and s/^(IN|IN_OUTLIST|OUTLIST|IN_OUT|OUT)\s+//) {
1121 next if $out_type eq 'IN';
1122 $only_outlist{$_} = 1 if $out_type eq "OUTLIST";
1123 push @outlist, $name if $out_type =~ /OUTLIST$/;
1124 $in_out{$_} = $out_type;
1128 if (defined($class)) {
1129 my $arg0 = ((defined($static) or $func_name eq 'new')
1130 ? "CLASS" : "THIS");
1131 unshift(@args, $arg0);
1132 ($report_args = "$arg0, $report_args") =~ s/^\w+, $/$arg0/;
1137 my $report_args = '';
1138 foreach $i (0 .. $#args) {
1139 if ($args[$i] =~ s/\.\.\.//) {
1141 if ($args[$i] eq '' && $i == $#args) {
1142 $report_args .= ", ...";
1147 if ($only_outlist{$args[$i]}) {
1148 push @args_num, undef;
1150 push @args_num, ++$num_args;
1151 $report_args .= ", $args[$i]";
1153 if ($args[$i] =~ /^([^=]*[^\s=])\s*=\s*(.*)/s) {
1156 $defaults{$args[$i]} = $2;
1157 $defaults{$args[$i]} =~ s/"/\\"/g;
1159 $proto_arg[$i+1] = "\$" ;
1161 $min_args = $num_args - $extra_args;
1162 $report_args =~ s/"/\\"/g;
1163 $report_args =~ s/^,\s+//;
1164 my @func_args = @args;
1165 shift @func_args if defined($class);
1168 s/^/&/ if $in_out{$_};
1170 $func_args = join(", ", @func_args);
1171 @args_match{@args} = @args_num;
1173 $PPCODE = grep(/^\s*PPCODE\s*:/, @line);
1174 $CODE = grep(/^\s*CODE\s*:/, @line);
1175 # Detect CODE: blocks which use ST(n)= or XST_m*(n,v)
1176 # to set explicit return values.
1177 $EXPLICIT_RETURN = ($CODE &&
1178 ("@line" =~ /(\bST\s*\([^;]*=) | (\bXST_m\w+\s*\()/x ));
1179 $ALIAS = grep(/^\s*ALIAS\s*:/, @line);
1180 $INTERFACE = grep(/^\s*INTERFACE\s*:/, @line);
1182 $xsreturn = 1 if $EXPLICIT_RETURN;
1184 # print function header
1186 #XS(XS_${Full_func_name})
1190 print Q<<"EOF" if $ALIAS ;
1193 print Q<<"EOF" if $INTERFACE ;
1194 # dXSFUNCTION($ret_type);
1197 $cond = ($min_args ? qq(items < $min_args) : 0);
1199 elsif ($min_args == $num_args) {
1200 $cond = qq(items != $min_args);
1203 $cond = qq(items < $min_args || items > $num_args);
1206 print Q<<"EOF" if $except;
1207 # char errbuf[1024];
1212 { print Q<<"EOF" if $cond }
1214 # Perl_croak(aTHX_ "Usage: %s($report_args)", GvNAME(CvGV(cv)));
1217 { print Q<<"EOF" if $cond }
1219 # Perl_croak(aTHX_ "Usage: $pname($report_args)");
1222 print Q<<"EOF" if $PPCODE;
1226 # Now do a block of some sort.
1229 $cond = ''; # last CASE: condidional
1230 push(@line, "$END:");
1231 push(@line_no, $line_no[-1]);
1235 &CASE_handler if check_keyword("CASE");
1240 # do initialization of input variables
1248 process_keyword("INPUT|PREINIT|INTERFACE_MACRO|C_ARGS|ALIAS|ATTRS|PROTOTYPE|SCOPE") ;
1250 print Q<<"EOF" if $ScopeThisXSUB;
1255 if (!$thisdone && defined($class)) {
1256 if (defined($static) or $func_name eq 'new') {
1258 $var_types{"CLASS"} = "char *";
1259 &generate_init("char *", 1, "CLASS");
1263 $var_types{"THIS"} = "$class *";
1264 &generate_init("$class *", 1, "THIS");
1269 if (/^\s*NOT_IMPLEMENTED_YET/) {
1270 print "\n\tPerl_croak(aTHX_ \"$pname: not implemented yet\");\n";
1273 if ($ret_type ne "void") {
1274 print "\t" . &map_type($ret_type, 'RETVAL') . ";\n"
1276 $args_match{"RETVAL"} = 0;
1277 $var_types{"RETVAL"} = $ret_type;
1278 print "\tdXSTARG;\n"
1279 if $WantOptimize and $targetable{$type_kind{$ret_type}};
1282 if (@arg_with_types) {
1283 unshift @line, @arg_with_types, $_;
1285 $processing_arg_with_types = 1;
1290 process_keyword("INIT|ALIAS|ATTRS|PROTOTYPE|INTERFACE_MACRO|INTERFACE|C_ARGS") ;
1292 if (check_keyword("PPCODE")) {
1294 death ("PPCODE must be last thing") if @line;
1295 print "\tLEAVE;\n" if $ScopeThisXSUB;
1296 print "\tPUTBACK;\n\treturn;\n";
1297 } elsif (check_keyword("CODE")) {
1299 } elsif (defined($class) and $func_name eq "DESTROY") {
1301 print "delete THIS;\n";
1304 if ($ret_type ne "void") {
1308 if (defined($static)) {
1309 if ($func_name eq 'new') {
1310 $func_name = "$class";
1314 } elsif (defined($class)) {
1315 if ($func_name eq 'new') {
1316 $func_name .= " $class";
1321 $func_name =~ s/^($spat)//
1323 $func_name = 'XSFUNCTION' if $interface;
1324 print "$func_name($func_args);\n";
1328 # do output variables
1329 $gotRETVAL = 0; # 1 if RETVAL seen in OUTPUT section;
1330 undef $RETVAL_code ; # code to set RETVAL (from OUTPUT section);
1331 # $wantRETVAL set if 'RETVAL =' autogenerated
1332 ($wantRETVAL, $ret_type) = (0, 'void') if $RETVAL_no_return;
1334 process_keyword("POSTCALL|OUTPUT|ALIAS|ATTRS|PROTOTYPE");
1336 &generate_output($var_types{$_}, $args_match{$_}, $_, $DoSetMagic)
1337 for grep $in_out{$_} =~ /OUT$/, keys %in_out;
1339 # all OUTPUT done, so now push the return value on the stack
1340 if ($gotRETVAL && $RETVAL_code) {
1341 print "\t$RETVAL_code\n";
1342 } elsif ($gotRETVAL || $wantRETVAL) {
1343 my $t = $WantOptimize && $targetable{$type_kind{$ret_type}};
1345 my $type = $ret_type;
1347 # 0: type, 1: with_size, 2: how, 3: how_size
1348 if ($t and not $t->[1] and $t->[0] eq 'p') {
1349 # PUSHp corresponds to setpvn. Treate setpv directly
1350 my $what = eval qq("$t->[2]");
1353 print "\tsv_setpv(TARG, $what); XSprePUSH; PUSHTARG;\n";
1357 my $what = eval qq("$t->[2]");
1361 $size = '' unless defined $size;
1362 $size = eval qq("$size");
1364 print "\tXSprePUSH; PUSH$t->[0]($what$size);\n";
1368 # RETVAL almost never needs SvSETMAGIC()
1369 &generate_output($ret_type, 0, 'RETVAL', 0);
1373 $xsreturn = 1 if $ret_type ne "void";
1374 my $num = $xsreturn;
1376 print "\tXSprePUSH;" if $c and not $prepush_done;
1377 print "\tEXTEND(SP,$c);\n" if $c;
1379 generate_output($var_types{$_}, $num++, $_, 0, 1) for @outlist;
1382 process_keyword("CLEANUP|ALIAS|ATTRS|PROTOTYPE") ;
1384 print Q<<"EOF" if $ScopeThisXSUB;
1387 print Q<<"EOF" if $ScopeThisXSUB and not $PPCODE;
1391 # print function trailer
1395 print Q<<EOF if $except;
1398 # sprintf(errbuf, "%s: %s\\tpropagated", Xname, Xreason);
1401 if (check_keyword("CASE")) {
1402 blurt ("Error: No `CASE:' at top of function")
1404 $_ = "CASE: $_"; # Restore CASE: label
1407 last if $_ eq "$END:";
1408 death(/^$BLOCK_re/o ? "Misplaced `$1:'" : "Junk at end of function");
1411 print Q<<EOF if $except;
1413 # Perl_croak(aTHX_ errbuf);
1417 print Q<<EOF unless $PPCODE;
1418 # XSRETURN($xsreturn);
1421 print Q<<EOF unless $PPCODE;
1431 my $newXS = "newXS" ;
1434 # Build the prototype string for the xsub
1435 if ($ProtoThisXSUB) {
1436 $newXS = "newXSproto";
1438 if ($ProtoThisXSUB eq 2) {
1439 # User has specified empty prototype
1442 elsif ($ProtoThisXSUB ne 1) {
1443 # User has specified a prototype
1444 $proto = ', "' . $ProtoThisXSUB . '"';
1448 if ($min_args < $num_args) {
1450 $proto_arg[$min_args] .= ";" ;
1452 push @proto_arg, "$s\@"
1455 $proto = ', "' . join ("", @proto_arg) . '"';
1460 $XsubAliases{$pname} = 0
1461 unless defined $XsubAliases{$pname} ;
1462 while ( ($name, $value) = each %XsubAliases) {
1463 push(@InitFileCode, Q<<"EOF");
1464 # cv = newXS(\"$name\", XS_$Full_func_name, file);
1465 # XSANY.any_i32 = $value ;
1467 push(@InitFileCode, Q<<"EOF") if $proto;
1468 # sv_setpv((SV*)cv$proto) ;
1472 elsif (@Attributes) {
1473 push(@InitFileCode, Q<<"EOF");
1474 # cv = newXS(\"$pname\", XS_$Full_func_name, file);
1475 # apply_attrs_string("$Package", cv, "@Attributes", 0);
1478 elsif ($interface) {
1479 while ( ($name, $value) = each %Interfaces) {
1480 $name = "$Package\::$name" unless $name =~ /::/;
1481 push(@InitFileCode, Q<<"EOF");
1482 # cv = newXS(\"$name\", XS_$Full_func_name, file);
1483 # $interface_macro_set(cv,$value) ;
1485 push(@InitFileCode, Q<<"EOF") if $proto;
1486 # sv_setpv((SV*)cv$proto) ;
1492 " ${newXS}(\"$pname\", XS_$Full_func_name, file$proto);\n");
1496 # print initialization routine
1505 #XS(boot_$Module_cname)
1511 # char* file = __FILE__;
1515 print Q<<"EOF" if $WantVersionChk ;
1516 # XS_VERSION_BOOTCHECK ;
1520 print Q<<"EOF" if defined $XsubAliases or defined $Interfaces ;
1526 print @InitFileCode;
1528 print Q<<"EOF" if defined $XsubAliases or defined $Interfaces ;
1534 print "\n /* Initialisation Section */\n\n" ;
1537 print "\n /* End of Initialisation Section */\n\n" ;
1546 warn("Please specify prototyping behavior for $filename (see perlxs manual)\n")
1551 local($type, $num, $var, $init, $name_printed) = @_;
1552 local($arg) = "ST(" . ($num - 1) . ")";
1554 if( $init =~ /^=/ ) {
1555 if ($name_printed) {
1556 eval qq/print " $init\\n"/;
1558 eval qq/print "\\t$var $init\\n"/;
1562 if( $init =~ s/^\+// && $num ) {
1563 &generate_init($type, $num, $var, $name_printed);
1564 } elsif ($name_printed) {
1568 eval qq/print "\\t$var;\\n"/;
1572 $deferred .= eval qq/"\\n\\t$init\\n"/;
1579 # work out the line number
1580 my $line_no = $line_no[@line_no - @line -1] ;
1582 print STDERR "@_ in $filename, line $line_no\n" ;
1598 local($type, $num, $var) = @_;
1599 local($arg) = "ST(" . ($num - 1) . ")";
1600 local($argoff) = $num - 1;
1604 $type = TidyType($type) ;
1605 blurt("Error: '$type' not in typemap"), return
1606 unless defined($type_kind{$type});
1608 ($ntype = $type) =~ s/\s*\*/Ptr/g;
1609 ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
1610 $tk = $type_kind{$type};
1611 $tk =~ s/OBJ$/REF/ if $func_name =~ /DESTROY$/;
1613 blurt("Error: No INPUT definition for type '$type', typekind '$type_kind{$type}' found"), return
1614 unless defined $input_expr{$tk} ;
1615 $expr = $input_expr{$tk};
1616 if ($expr =~ /DO_ARRAY_ELEM/) {
1617 blurt("Error: '$subtype' not in typemap"), return
1618 unless defined($type_kind{$subtype});
1619 blurt("Error: No INPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return
1620 unless defined $input_expr{$type_kind{$subtype}} ;
1621 $subexpr = $input_expr{$type_kind{$subtype}};
1622 $subexpr =~ s/ntype/subtype/g;
1623 $subexpr =~ s/\$arg/ST(ix_$var)/g;
1624 $subexpr =~ s/\n\t/\n\t\t/g;
1625 $subexpr =~ s/is not of (.*\")/[arg %d] is not of $1, ix_$var + 1/g;
1626 $subexpr =~ s/\$var/${var}[ix_$var - $argoff]/;
1627 $expr =~ s/DO_ARRAY_ELEM/$subexpr/;
1629 if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments
1632 if (defined($defaults{$var})) {
1633 $expr =~ s/(\t+)/$1 /g;
1635 if ($name_printed) {
1638 eval qq/print "\\t$var;\\n"/;
1641 if ($defaults{$var} eq 'NO_INIT') {
1642 $deferred .= eval qq/"\\n\\tif (items >= $num) {\\n$expr;\\n\\t}\\n"/;
1644 $deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/;
1647 } elsif ($ScopeThisXSUB or $expr !~ /^\t\$var =/) {
1648 if ($name_printed) {
1651 eval qq/print "\\t$var;\\n"/;
1654 $deferred .= eval qq/"\\n$expr;\\n"/;
1657 die "panic: do not know how to handle this branch for function pointers"
1659 eval qq/print "$expr;\\n"/;
1664 sub generate_output {
1665 local($type, $num, $var, $do_setmagic, $do_push) = @_;
1666 local($arg) = "ST(" . ($num - ($num != 0)) . ")";
1667 local($argoff) = $num - 1;
1670 $type = TidyType($type) ;
1671 if ($type =~ /^array\(([^,]*),(.*)\)/) {
1672 print "\tsv_setpvn($arg, (char *)$var, $2 * sizeof($1));\n";
1673 print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
1675 blurt("Error: '$type' not in typemap"), return
1676 unless defined($type_kind{$type});
1677 blurt("Error: No OUTPUT definition for type '$type', typekind '$type_kind{$type}' found"), return
1678 unless defined $output_expr{$type_kind{$type}} ;
1679 ($ntype = $type) =~ s/\s*\*/Ptr/g;
1680 $ntype =~ s/\(\)//g;
1681 ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
1682 $expr = $output_expr{$type_kind{$type}};
1683 if ($expr =~ /DO_ARRAY_ELEM/) {
1684 blurt("Error: '$subtype' not in typemap"), return
1685 unless defined($type_kind{$subtype});
1686 blurt("Error: No OUTPUT definition for type '$subtype', typekind '$type_kind{$subtype}' found"), return
1687 unless defined $output_expr{$type_kind{$subtype}} ;
1688 $subexpr = $output_expr{$type_kind{$subtype}};
1689 $subexpr =~ s/ntype/subtype/g;
1690 $subexpr =~ s/\$arg/ST(ix_$var)/g;
1691 $subexpr =~ s/\$var/${var}[ix_$var]/g;
1692 $subexpr =~ s/\n\t/\n\t\t/g;
1693 $expr =~ s/DO_ARRAY_ELEM\n/$subexpr/;
1694 eval "print qq\a$expr\a";
1696 print "\t\tSvSETMAGIC(ST(ix_$var));\n" if $do_setmagic;
1698 elsif ($var eq 'RETVAL') {
1699 if ($expr =~ /^\t\$arg = new/) {
1700 # We expect that $arg has refcnt 1, so we need to
1702 eval "print qq\a$expr\a";
1704 print "\tsv_2mortal(ST($num));\n";
1705 print "\tSvSETMAGIC(ST($num));\n" if $do_setmagic;
1707 elsif ($expr =~ /^\s*\$arg\s*=/) {
1708 # We expect that $arg has refcnt >=1, so we need
1710 eval "print qq\a$expr\a";
1712 print "\tsv_2mortal(ST(0));\n";
1713 print "\tSvSETMAGIC(ST(0));\n" if $do_setmagic;
1716 # Just hope that the entry would safely write it
1717 # over an already mortalized value. By
1718 # coincidence, something like $arg = &sv_undef
1720 print "\tST(0) = sv_newmortal();\n";
1721 eval "print qq\a$expr\a";
1723 # new mortals don't have set magic
1727 print "\tPUSHs(sv_newmortal());\n";
1729 eval "print qq\a$expr\a";
1731 print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
1733 elsif ($arg =~ /^ST\(\d+\)$/) {
1734 eval "print qq\a$expr\a";
1736 print "\tSvSETMAGIC($arg);\n" if $do_setmagic;
1742 my($type, $varname) = @_;
1745 $type =~ s/^array\(([^,]*),(.*)\).*/$1 */s;
1747 if ($varname && $type =~ / \( \s* \* (?= \s* \) ) /xg) {
1748 (substr $type, pos $type, 0) = " $varname ";
1750 $type .= "\t$varname";
1758 # If this is VMS, the exit status has meaning to the shell, so we
1759 # use a predictable value (SS$_Normal or SS$_Abort) rather than an
1761 # exit ($Is_VMS ? ($errors ? 44 : 1) : $errors) ;
1762 exit ($errors ? 1 : 0);