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<-typemap typemap>]... file.xs
13 I<xsubpp> will compile XS code into C code by embedding the constructs
14 necessary to let C functions manipulate Perl values and creates the glue
15 necessary to let Perl access those functions. The compiler uses typemaps to
16 determine how to map C function parameters and variables to Perl values.
18 The compiler will search for typemap files called I<typemap>. It will use
19 the following search path to find default typemaps, with the rightmost
20 typemap taking precedence.
22 ../../../typemap:../../typemap:../typemap:typemap
30 Adds ``extern "C"'' to the C code.
35 Adds exception handling stubs to the C code.
37 =item B<-typemap typemap>
39 Indicates that a user-supplied typemap should take precedence over the
40 default typemaps. This option may be used multiple times, with the last
41 typemap having the highest precedence.
45 Prints the I<xsubpp> version number to standard output, then exits.
49 By default I<xsubpp> will not automatically generate prototype code for
50 all xsubs. This flag will enable prototypes.
52 =item B<-noversioncheck>
54 Disables the run time test that determines if the object file (derived
55 from the C<.xs> file) and the C<.pm> files have the same version
62 No environment variables are used.
68 =head1 MODIFICATION HISTORY
70 See the file F<changes.pod>.
74 perl(1), perlxs(1), perlxstut(1), perlapi(1)
79 $XSUBPP_version = "1.932";
86 $usage = "Usage: xsubpp [-v] [-C++] [-except] [-prototypes] [-noversioncheck] [-s pattern] [-typemap typemap]... file.xs\n";
88 $proto_re = "[" . quotemeta('\$%&*@;') . "]" ;
91 $WantPrototypes = -1 ;
94 SWITCH: while (@ARGV and $ARGV[0] =~ /^-./) {
97 $spat = shift, next SWITCH if $flag eq 's';
98 $cplusplus = 1, next SWITCH if $flag eq 'C++';
99 $WantPrototypes = 0, next SWITCH if $flag eq 'noprototypes';
100 $WantPrototypes = 1, next SWITCH if $flag eq 'prototypes';
101 $WantVersionChk = 0, next SWITCH if $flag eq 'noversioncheck';
102 $WantVersionChk = 1, next SWITCH if $flag eq 'versioncheck';
103 $except = " TRY", next SWITCH if $flag eq 'except';
104 push(@tm,shift), next SWITCH if $flag eq 'typemap';
105 (print "xsubpp version $XSUBPP_version\n"), exit
109 if ($WantPrototypes == -1)
110 { $WantPrototypes = 0}
115 @ARGV == 1 or die $usage;
116 ($dir, $filename) = $ARGV[0] =~ m#(.*)/(.*)#
117 or ($dir, $filename) = $ARGV[0] =~ m#(.*[>\]])(.*)#
118 or ($dir, $filename) = ('.', $ARGV[0]);
120 # Check for VMS; Config.pm may not be installed yet, but this routine
121 # is built into VMS perl
122 if (defined(&VMS::Filespec::vmsify)) { $Is_VMS = 1; $pwd = $ENV{DEFAULT}; }
123 else { $Is_VMS = 0; chomp($pwd = `pwd`); }
125 ++ $IncludedFiles{$ARGV[0]} ;
129 $_[0] =~ s/^\s+|\s+$//go ;
136 # rationalise any '*' by joining them into bunches and removing whitespace
140 # change multiple whitespace into a single space
143 # trim leading & trailing whitespace
149 $typemap = shift @ARGV;
150 foreach $typemap (@tm) {
151 die "Can't find $typemap in $pwd\n" unless -r $typemap;
153 unshift @tm, qw(../../../../lib/ExtUtils/typemap ../../../lib/ExtUtils/typemap
154 ../../lib/ExtUtils/typemap ../../../typemap ../../typemap
156 foreach $typemap (@tm) {
157 next unless -e $typemap ;
158 # skip directories, binary files etc.
159 warn("Warning: ignoring non-text typemap file '$typemap'\n"), next
161 open(TYPEMAP, $typemap)
162 or warn ("Warning: could not open typemap file '$typemap': $!\n"), next;
168 if (/^INPUT\s*$/) { $mode = 'Input'; $current = \$junk; next; }
169 if (/^OUTPUT\s*$/) { $mode = 'Output'; $current = \$junk; next; }
170 if (/^TYPEMAP\s*$/) { $mode = 'Typemap'; $current = \$junk; next; }
171 if ($mode eq 'Typemap') {
175 # skip blank lines and comment lines
176 next if /^$/ or /^#/ ;
177 my($type,$kind, $proto) = /^\s*(.*?\S)\s+(\S+)\s*($proto_re*)\s*$/ or
178 warn("Warning: File '$typemap' Line $. '$line' TYPEMAP entry needs 2 or 3 columns\n"), next;
179 $type = TidyType($type) ;
180 $type_kind{$type} = $kind ;
181 # prototype defaults to '$'
182 $proto = '$' unless $proto ;
183 warn("Warning: File '$typemap' Line $. '$line' Invalid prototype '$proto'\n")
184 unless ValidProtoString($proto) ;
185 $proto_letter{$type} = C_string($proto) ;
190 elsif ($mode eq 'Input') {
192 $input_expr{$_} = '';
193 $current = \$input_expr{$_};
197 $output_expr{$_} = '';
198 $current = \$output_expr{$_};
204 foreach $key (keys %input_expr) {
205 $input_expr{$key} =~ s/\n+$//;
208 $END = "!End!\n\n"; # "impossible" keyword (multiple newline)
210 # Match an XS keyword
211 $BLOCK_re= '\s*(' . join('|', qw(
212 REQUIRE BOOT CASE PREINIT INPUT INIT CODE PPCODE OUTPUT
213 CLEANUP ALIAS PROTOTYPES PROTOTYPE VERSIONCHECK INCLUDE
216 # Input: ($_, @line) == unparsed input.
217 # Output: ($_, @line) == (rest of line, following lines).
218 # Return: the matched keyword if found, otherwise 0
220 $_ = shift(@line) while !/\S/ && @line;
221 s/^(\s*)($_[0])\s*:\s*(?:#.*)?/$1/s && $2;
226 $_ = shift(@line) while !/\S/ && @line;
227 for (; defined($_) && !/^$BLOCK_re/o; $_ = shift(@line)) {
232 sub process_keyword($)
237 &{"${kwd}_handler"}()
238 while $kwd = check_keyword($pattern) ;
242 blurt ("Error: `CASE:' after unconditional `CASE:'")
243 if $condnum && $cond eq '';
245 TrimWhitespace($cond);
246 print " ", ($condnum++ ? " else" : ""), ($cond ? " if ($cond)\n" : "\n");
251 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
252 last if /^\s*NOT_IMPLEMENTED_YET/;
253 next unless /\S/; # skip blank lines
258 # remove trailing semicolon if no initialisation
259 s/\s*;$//g unless /=/ ;
261 # check for optional initialisation code
263 $var_init = $1 if s/\s*(=.*)$//s ;
264 $var_init =~ s/"/\\"/g;
267 my ($var_type, $var_addr, $var_name) = /^(.*?[^& ]) *(\&?) *\b(\w+)$/s
268 or blurt("Error: invalid argument declaration '$line'"), next;
270 # Check for duplicate definitions
271 blurt ("Error: duplicate definition of argument '$var_name' ignored"), next
272 if $arg_list{$var_name} ++ ;
274 $thisdone |= $var_name eq "THIS";
275 $retvaldone |= $var_name eq "RETVAL";
276 $var_types{$var_name} = $var_type;
277 print "\t" . &map_type($var_type);
278 $var_num = $args_match{$var_name};
280 $proto_arg[$var_num] = ProtoString($var_type)
283 $var_addr{$var_name} = 1;
284 $func_args =~ s/\b($var_name)\b/&$1/;
286 if ($var_init =~ /^=\s*NO_INIT\s*;?\s*$/) {
287 print "\t$var_name;\n";
288 } elsif ($var_init =~ /\S/) {
289 &output_init($var_type, $var_num, "$var_name $var_init");
291 # generate initialization code
292 &generate_init($var_type, $var_num, $var_name);
300 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
302 my ($outarg, $outcode) = /^\s*(\S+)\s*(.*?)\s*$/s ;
303 blurt ("Error: duplicate OUTPUT argument '$outarg' ignored"), next
304 if $outargs{$outarg} ++ ;
305 if (!$gotRETVAL and $outarg eq 'RETVAL') {
306 # deal with RETVAL last
307 $RETVAL_code = $outcode ;
311 blurt ("Error: OUTPUT $outarg not an argument"), next
312 unless defined($args_match{$outarg});
313 blurt("Error: No input definition for OUTPUT argument '$outarg' - ignored"), next
314 unless defined $var_types{$outarg} ;
316 print "\t$outcode\n";
318 $var_num = $args_match{$outarg};
319 &generate_output($var_types{$outarg}, $var_num, $outarg);
324 sub CLEANUP_handler() { print_section() }
325 sub PREINIT_handler() { print_section() }
326 sub INIT_handler() { print_section() }
335 # Parse alias definitions
337 # alias = value alias = value ...
339 while ($line =~ s/^\s*([\w:]+)\s*=\s*(\w+)\s*//) {
341 $orig_alias = $alias ;
344 # check for optional package definition in the alias
345 $alias = $Packprefix . $alias if $alias !~ /::/ ;
347 # check for duplicate alias name & duplicate value
348 Warn("Warning: Ignoring duplicate alias '$orig_alias'")
349 if defined $XsubAliases{$pname}{$alias} ;
351 Warn("Warning: Aliases '$orig_alias' and '$XsubAliasValues{$pname}{$value}' have identical values")
352 if $XsubAliasValues{$pname}{$value} ;
354 $XsubAliases{$pname}{$alias} = $value ;
355 $XsubAliasValues{$pname}{$value} = $orig_alias ;
358 blurt("Error: Cannot parse ALIAS definitions from '$orig'")
364 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
367 GetAliases($_) if $_ ;
371 sub REQUIRE_handler ()
373 # the rest of the current line should contain a version number
376 TrimWhitespace($Ver) ;
378 death ("Error: REQUIRE expects a version number")
381 # check that the version number is of the form n.n
382 death ("Error: REQUIRE: expected a number, got '$Ver'")
383 unless $Ver =~ /^\d+(\.\d*)?/ ;
385 death ("Error: xsubpp $Ver (or better) required--this is only $XSUBPP_version.")
386 unless $XSUBPP_version >= $Ver ;
389 sub VERSIONCHECK_handler ()
391 # the rest of the current line should contain either ENABLE or
396 # check for ENABLE/DISABLE
397 death ("Error: VERSIONCHECK: ENABLE/DISABLE")
398 unless /^(ENABLE|DISABLE)/i ;
400 $WantVersionChk = 1 if $1 eq 'ENABLE' ;
401 $WantVersionChk = 0 if $1 eq 'DISABLE' ;
405 sub PROTOTYPE_handler ()
407 death("Error: Only 1 PROTOTYPE definition allowed per xsub")
408 if $proto_in_this_xsub ++ ;
410 for (; !/^$BLOCK_re/o; $_ = shift(@line)) {
413 if ($_ eq 'DISABLE') {
416 elsif ($_ eq 'ENABLE') {
420 # remove any whitespace
422 death("Error: Invalid prototype '$_'")
423 unless ValidProtoString($_) ;
424 $ProtoThisXSUB = C_string($_) ;
432 sub PROTOTYPES_handler ()
434 # the rest of the current line should contain either ENABLE or
439 # check for ENABLE/DISABLE
440 death ("Error: PROTOTYPES: ENABLE/DISABLE")
441 unless /^(ENABLE|DISABLE)/i ;
443 $WantPrototypes = 1 if $1 eq 'ENABLE' ;
444 $WantPrototypes = 0 if $1 eq 'DISABLE' ;
449 sub INCLUDE_handler ()
451 # the rest of the current line should contain a valid filename
455 death("INCLUDE: filename missing")
458 death("INCLUDE: output pipe is illegal")
461 # simple minded recursion detector
462 death("INCLUDE loop detected")
463 if $IncludedFiles{$_} ;
465 ++ $IncludedFiles{$_} unless /\|\s*$/ ;
467 # Save the current file context.
469 LastLine => $lastline,
470 LastLineNo => $lastline_no,
473 Filename => $filename,
480 open ($FH, "$_") or death("Cannot open '$_': $!") ;
484 #/* INCLUDE: Including '$_' from '$filename' */
490 # Prime the pump by reading the first
493 # skip leading blank lines
495 last unless /^\s*$/ ;
505 return 0 unless @FileStack ;
507 my $data = pop @FileStack ;
508 my $ThisFile = $filename ;
509 my $isPipe = ($filename =~ /\|\s*$/) ;
511 -- $IncludedFiles{$filename}
516 $FH = $data->{Handle} ;
517 $filename = $data->{Filename} ;
518 $lastline = $data->{LastLine} ;
519 $lastline_no = $data->{LastLineNo} ;
520 @line = @{ $data->{Line} } ;
521 @line_no = @{ $data->{LineNo} } ;
523 if ($isPipe and $? ) {
525 print STDERR "Error reading from pipe '$ThisFile': $! in $filename, line $lastline_no\n" ;
531 #/* INCLUDE: Returning to '$filename' from '$ThisFile' */
538 sub ValidProtoString ($)
542 if ( $string =~ /^$proto_re+$/ ) {
553 $string =~ s[\\][\\\\]g ;
561 $proto_letter{$type} or '$' ;
565 my @cpp = grep(/^\#\s*(?:if|e\w+)/, @line);
567 my ($cpp, $cpplevel);
569 if ($cpp =~ /^\#\s*if/) {
571 } elsif (!$cpplevel) {
572 Warn("Warning: #else/elif/endif without #if in this function");
574 } elsif ($cpp =~ /^\#\s*endif/) {
578 Warn("Warning: #if without #endif in this function") if $cpplevel;
591 open($FH, $filename) or die "cannot open $filename: $!\n";
593 # Identify the version of xsubpp used
596 * This file was generated automatically by xsubpp version $XSUBPP_version from the
597 * contents of $filename. Don't edit this file, edit $filename instead.
599 * ANY CHANGES MADE HERE WILL BE LOST!
607 last if ($Module, $Package, $Prefix) =
608 /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/;
611 &Exit unless defined $_;
617 # Read next xsub into @line from ($lastline, <$FH>).
622 if (! defined $lastline) {
623 return 1 if PopFile() ;
628 /^MODULE\s*=\s*([\w:]+)(?:\s+PACKAGE\s*=\s*([\w:]+))?(?:\s+PREFIX\s*=\s*(\S+))?\s*$/) {
630 $Package = defined($2) ? $2 : ''; # keep -w happy
631 $Prefix = defined($3) ? $3 : ''; # keep -w happy
632 ($Module_cname = $Module) =~ s/\W/_/g;
633 ($Packid = $Package) =~ tr/:/_/;
634 $Packprefix = $Package;
635 $Packprefix .= "::" if $Packprefix ne "";
640 if ($lastline !~ /^\s*#/ ||
641 $lastline =~ /^#[ \t]*(?:(?:if|ifn?def|else|elif|endif|define|undef|pragma)\b|include\s*["<].*[>"])/) {
642 last if $lastline =~ /^\S/ && @line && $line[-1] eq "";
643 push(@line, $lastline);
644 push(@line_no, $lastline_no) ;
647 # Read next line and continuation lines
648 last unless defined($lastline = <$FH>);
651 $lastline .= $tmp_line
652 while ($lastline =~ /\\$/ && defined($tmp_line = <$FH>));
655 $lastline =~ s/^\s+$//;
657 pop(@line), pop(@line_no) while @line && $line[-1] eq "";
662 while (fetch_para()) {
663 # Print initial preprocessor statements and blank lines
664 print shift(@line), "\n"
665 while @line && $line[0] !~ /^[^\#]/;
667 next PARAGRAPH unless @line;
669 death ("Code is not inside a function")
670 if $line[0] =~ /^\s/;
672 # initialize info arrays
683 undef($proto_in_this_xsub) ;
684 $ProtoThisXSUB = $WantPrototypes ;
687 while ($kwd = check_keyword("REQUIRE|PROTOTYPES|VERSIONCHECK|INCLUDE")) {
688 &{"${kwd}_handler"}() ;
689 next PARAGRAPH unless @line ;
693 if (check_keyword("BOOT")) {
695 push (@BootCode, $_, @line, "") ;
700 # extract return type, function name and arguments
701 my($ret_type) = TidyType($_);
703 # a function definition needs at least 2 lines
704 blurt ("Error: Function definition too short '$ret_type'"), next PARAGRAPH
707 $static = 1 if $ret_type =~ s/^static\s+//;
709 $func_header = shift(@line);
710 blurt ("Error: Cannot parse function definition from '$func_header'"), next PARAGRAPH
711 unless $func_header =~ /^(?:([\w:]*)::)?(\w+)\s*\(\s*(.*?)\s*\)\s*$/s;
713 ($class, $func_name, $orig_args) = ($1, $2, $3) ;
714 ($pname = $func_name) =~ s/^($Prefix)?/$Packprefix/;
716 # Check for duplicate function definition
717 if (defined $Func_name{"${Packid}_$func_name"} ) {
718 Warn("Warning: duplicate function definition '$func_name' detected")
721 push(@Func_name, "${Packid}_$func_name");
722 push(@Func_pname, $pname);
724 $Func_name{"${Packid}_$func_name"} ++ ;
726 @args = split(/\s*,\s*/, $orig_args);
727 if (defined($class)) {
728 my $arg0 = ((defined($static) or $func_name =~ /^new/) ? "CLASS" : "THIS");
729 unshift(@args, $arg0);
730 ($orig_args = "$arg0, $orig_args") =~ s/^$arg0, $/$arg0/;
732 $orig_args =~ s/"/\\"/g;
733 $min_args = $num_args = @args;
734 foreach $i (0..$num_args-1) {
735 if ($args[$i] =~ s/\.\.\.//) {
738 if ($args[$i] eq '' && $i == $num_args - 1) {
743 if ($args[$i] =~ /^([^=]*[^\s=])\s*=\s*(.*)/s) {
746 $defaults{$args[$i]} = $2;
747 $defaults{$args[$i]} =~ s/"/\\"/g;
749 $proto_arg[$i+1] = '$' ;
751 if (defined($class)) {
752 $func_args = join(", ", @args[1..$#args]);
754 $func_args = join(", ", @args);
756 @args_match{@args} = 1..@args;
758 $PPCODE = grep(/^\s*PPCODE\s*:/, @line);
759 $ALIAS = grep(/^\s*ALIAS\s*:/, @line);
761 # print function header
763 #XS(XS_${Packid}_$func_name)
767 print Q<<"EOF" if $ALIAS ;
771 $cond = ($min_args ? qq(items < $min_args) : 0);
773 elsif ($min_args == $num_args) {
774 $cond = qq(items != $min_args);
777 $cond = qq(items < $min_args || items > $num_args);
780 print Q<<"EOF" if $except;
786 { print Q<<"EOF" if $cond }
788 # croak("Usage: %s($orig_args)", GvNAME(CvGV(cv)));
791 { print Q<<"EOF" if $cond }
793 # croak("Usage: $pname($orig_args)");
796 print Q<<"EOF" if $PPCODE;
800 # Now do a block of some sort.
803 $cond = ''; # last CASE: condidional
804 push(@line, "$END:");
805 push(@line_no, $line_no[-1]);
809 &CASE_handler if check_keyword("CASE");
814 # do initialization of input variables
822 process_keyword("INPUT|PREINIT|ALIAS|PROTOTYPE") ;
824 if (!$thisdone && defined($class)) {
825 if (defined($static) or $func_name =~ /^new/) {
827 $var_types{"CLASS"} = "char *";
828 &generate_init("char *", 1, "CLASS");
832 $var_types{"THIS"} = "$class *";
833 &generate_init("$class *", 1, "THIS");
838 if (/^\s*NOT_IMPLEMENTED_YET/) {
839 print "\n\tcroak(\"$pname: not implemented yet\");\n";
842 if ($ret_type ne "void") {
843 print "\t" . &map_type($ret_type) . "\tRETVAL;\n"
845 $args_match{"RETVAL"} = 0;
846 $var_types{"RETVAL"} = $ret_type;
849 process_keyword("INIT|ALIAS|PROTOTYPE") ;
851 if (check_keyword("PPCODE")) {
853 death ("PPCODE must be last thing") if @line;
854 print "\tPUTBACK;\n\treturn;\n";
855 } elsif (check_keyword("CODE")) {
857 } elsif (defined($class) and $func_name eq "DESTROY") {
859 print "delete THIS;\n";
862 if ($ret_type ne "void") {
866 if (defined($static)) {
867 if ($func_name =~ /^new/) {
868 $func_name = "$class";
872 } elsif (defined($class)) {
873 if ($func_name =~ /^new/) {
874 $func_name .= " $class";
879 $func_name =~ s/^($spat)//
881 print "$func_name($func_args);\n";
885 # do output variables
889 process_keyword("OUTPUT|ALIAS|PROTOTYPE");
891 # all OUTPUT done, so now push the return value on the stack
892 if ($gotRETVAL && $RETVAL_code) {
893 print "\t$RETVAL_code\n";
894 } elsif ($gotRETVAL || $wantRETVAL) {
895 &generate_output($ret_type, 0, 'RETVAL');
899 process_keyword("CLEANUP|ALIAS|PROTOTYPE") ;
901 # print function trailer
905 print Q<<EOF if $except;
908 # sprintf(errbuf, "%s: %s\\tpropagated", Xname, Xreason);
911 if (check_keyword("CASE")) {
912 blurt ("Error: No `CASE:' at top of function")
914 $_ = "CASE: $_"; # Restore CASE: label
917 last if $_ eq "$END:";
918 death(/^$BLOCK_re/o ? "Misplaced `$1:'" : "Junk at end of function");
921 print Q<<EOF if $except;
926 print Q<<EOF unless $PPCODE;
935 # Build the prototype string for the xsub
936 if ($ProtoThisXSUB) {
937 if ($ProtoThisXSUB != 1) {
938 $ProtoXSUB{$pname} = '"' . $ProtoThisXSUB . '"'
942 if ($min_args < $num_args) {
944 $proto_arg[$min_args] .= ";" ;
946 push @proto_arg, "${s}@"
949 $ProtoXSUB{$pname} = '"' . join ("", @proto_arg) . '"'
955 # print initialization routine
960 #XS(boot_$Module_cname)
963 # char* file = __FILE__;
967 print Q<<"EOF" if $WantVersionChk ;
968 # XS_VERSION_BOOTCHECK ;
972 print Q<<"EOF" if defined %XsubAliases ;
979 $pname = shift(@Func_pname);
980 my $newXS = "newXS" ;
983 if ($ProtoXSUB{$pname}) {
984 $newXS = "newXSproto" ;
985 $proto = ", $ProtoXSUB{$pname}" ;
988 if ($XsubAliases{$pname}) {
989 $XsubAliases{$pname}{$pname} = 0
990 unless defined $XsubAliases{$pname}{$pname} ;
991 while ( ($name, $value) = each %{$XsubAliases{$pname}}) {
993 # cv = newXS(\"$name\", XS_$_, file);
994 # XSANY.any_i32 = $value ;
996 print Q<<"EOF" if $proto ;
997 # sv_setpv((SV*)cv, $ProtoXSUB{$pname}) ;
1002 print " ${newXS}(\"$pname\", XS_$_, file$proto);\n";
1006 print Q<<"EOF" if defined %XsubAliases ;
1012 print "\n /* Initialisation Section */\n" ;
1013 print grep (s/$/\n/, @BootCode) ;
1014 print "\n /* End of Initialisation Section */\n\n" ;
1023 warn("Please specify prototyping behavior for $filename (see perlxs manual)\n")
1029 local($type, $num, $init) = @_;
1030 local($arg) = "ST(" . ($num - 1) . ")";
1032 eval qq/print " $init\\\n"/;
1037 # work out the line number
1038 my $line_no = $line_no[@line_no - @line -1] ;
1040 print STDERR "@_ in $filename, line $line_no\n" ;
1056 local($type, $num, $var) = @_;
1057 local($arg) = "ST(" . ($num - 1) . ")";
1058 local($argoff) = $num - 1;
1062 $type = TidyType($type) ;
1063 blurt("Error: '$type' not in typemap"), return
1064 unless defined($type_kind{$type});
1066 ($ntype = $type) =~ s/\s*\*/Ptr/g;
1067 ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
1068 $tk = $type_kind{$type};
1069 $tk =~ s/OBJ$/REF/ if $func_name =~ /DESTROY$/;
1071 blurt("Error: No INPUT definition for type '$type' found"), return
1072 unless defined $input_expr{$tk} ;
1073 $expr = $input_expr{$tk};
1074 if ($expr =~ /DO_ARRAY_ELEM/) {
1075 blurt("Error: '$subtype' not in typemap"), return
1076 unless defined($type_kind{$subtype});
1077 blurt("Error: No INPUT definition for type '$subtype' found"), return
1078 unless defined $input_expr{$type_kind{$subtype}} ;
1079 $subexpr = $input_expr{$type_kind{$subtype}};
1080 $subexpr =~ s/ntype/subtype/g;
1081 $subexpr =~ s/\$arg/ST(ix_$var)/g;
1082 $subexpr =~ s/\n\t/\n\t\t/g;
1083 $subexpr =~ s/is not of (.*")/[arg %d] is not of $1, ix_$var + 1/g;
1084 $subexpr =~ s/\$var/${var}[ix_$var - $argoff]/;
1085 $expr =~ s/DO_ARRAY_ELEM/$subexpr/;
1087 if (defined($defaults{$var})) {
1088 $expr =~ s/(\t+)/$1 /g;
1090 eval qq/print "\\t$var;\\n"/;
1091 $deferred .= eval qq/"\\n\\tif (items < $num)\\n\\t $var = $defaults{$var};\\n\\telse {\\n$expr;\\n\\t}\\n"/;
1092 } elsif ($expr !~ /^\t\$var =/) {
1093 eval qq/print "\\t$var;\\n"/;
1094 $deferred .= eval qq/"\\n$expr;\\n"/;
1096 eval qq/print "$expr;\\n"/;
1100 sub generate_output {
1101 local($type, $num, $var) = @_;
1102 local($arg) = "ST(" . ($num - ($num != 0)) . ")";
1103 local($argoff) = $num - 1;
1106 $type = TidyType($type) ;
1107 if ($type =~ /^array\(([^,]*),(.*)\)/) {
1108 print "\tsv_setpvn($arg, (char *)$var, $2 * sizeof($1)), XFree((char *)$var);\n";
1110 blurt("Error: '$type' not in typemap"), return
1111 unless defined($type_kind{$type});
1112 blurt("Error: No OUTPUT definition for type '$type' found"), return
1113 unless defined $output_expr{$type_kind{$type}} ;
1114 ($ntype = $type) =~ s/\s*\*/Ptr/g;
1115 $ntype =~ s/\(\)//g;
1116 ($subtype = $ntype) =~ s/(?:Array)?(?:Ptr)?$//;
1117 $expr = $output_expr{$type_kind{$type}};
1118 if ($expr =~ /DO_ARRAY_ELEM/) {
1119 blurt("Error: '$subtype' not in typemap"), return
1120 unless defined($type_kind{$subtype});
1121 blurt("Error: No OUTPUT definition for type '$subtype' found"), return
1122 unless defined $output_expr{$type_kind{$subtype}} ;
1123 $subexpr = $output_expr{$type_kind{$subtype}};
1124 $subexpr =~ s/ntype/subtype/g;
1125 $subexpr =~ s/\$arg/ST(ix_$var)/g;
1126 $subexpr =~ s/\$var/${var}[ix_$var]/g;
1127 $subexpr =~ s/\n\t/\n\t\t/g;
1128 $expr =~ s/DO_ARRAY_ELEM\n/$subexpr/;
1129 eval "print qq\a$expr\a";
1131 elsif ($var eq 'RETVAL') {
1132 if ($expr =~ /^\t\$arg = /) {
1133 eval "print qq\a$expr\a";
1134 print "\tsv_2mortal(ST(0));\n";
1137 print "\tST(0) = sv_newmortal();\n";
1138 eval "print qq\a$expr\a";
1141 elsif ($arg =~ /^ST\(\d+\)$/) {
1142 eval "print qq\a$expr\a";
1151 $type =~ s/^array\(([^,]*),(.*)\).*/$1 */s;
1157 # If this is VMS, the exit status has meaning to the shell, so we
1158 # use a predictable value (SS$_Normal or SS$_Abort) rather than an
1160 exit ($Is_VMS ? ($errors ? 44 : 1) : $errors) ;