[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]

Re: [PATCH 5.005_63] targets in XSUBs



On Tue, 14 Dec 1999 17:08:26 EST, Ilya Zakharevich wrote:
>This is the reworked version of the patch which addresses Sarathy's demands.

Not quite up to my demands, but I'll try to be a friend. :-)

>--- ./lib/ExtUtils/xsubpp.orig	Fri Oct  1 18:13:18 1999
>+++ ./lib/ExtUtils/xsubpp	Tue Dec 14 16:57:39 1999
>@@ -63,7 +63,9 @@ Prevents the inclusion of `#line' direct
> 
> =head1 ENVIRONMENT
> 
>-No environment variables are used.
>+If C<XSUBPP_NO_TARGET> is set to a true value, the created C code will
>+not use I<target>s (see L<perlguts>). This may significantly slow down
>+the generated code (this is the way B<xsubpp> of 5.005 operated).

Environment variables are evil.  People set them, and constantly forget
they're set.


Sarathy
gsar@ActiveState.com
-----------------------------------8<-----------------------------------
Change 4731 by gsar@auger on 1999/12/28 20:23:17

	optimize XSUBs to use targets if the -nooptimize xsubpp option is
	not supplied (variant of patch suggested by Ilya Zakharevich)

Affected files ...

... //depot/perl/XSUB.h#46 edit
... //depot/perl/lib/ExtUtils/xsubpp#26 edit

Differences ...

==== //depot/perl/XSUB.h#46 (text) ====
Index: perl/XSUB.h
--- perl/XSUB.h.~1~	Wed Jan  5 11:30:44 2000
+++ perl/XSUB.h	Wed Jan  5 11:30:44 2000
@@ -17,6 +17,9 @@
 #define dXSTARG SV * targ = ((PL_op->op_private & OPpENTERSUB_HASTARG) \
 			     ? PAD_SV(PL_op->op_targ) : sv_newmortal())
 
+/* Should be used before final PUSHi etc. if not in PPCODE section. */
+#define XSprePUSH (sp = PL_stack_base + ax - 1)
+
 #define XSANY CvXSUBANY(cv)
 
 #define dXSI32 I32 ix = XSANY.any_i32

==== //depot/perl/lib/ExtUtils/xsubpp#26 (xtext) ====
Index: perl/lib/ExtUtils/xsubpp
--- perl/lib/ExtUtils/xsubpp.~1~	Wed Jan  5 11:30:44 2000
+++ perl/lib/ExtUtils/xsubpp	Wed Jan  5 11:30:44 2000
@@ -6,10 +6,12 @@
 
 =head1 SYNOPSIS
 
-B<xsubpp> [B<-v>] [B<-C++>] [B<-except>] [B<-s pattern>] [B<-prototypes>] [B<-noversioncheck>] [B<-nolinenumbers>] [B<-typemap typemap>] ... file.xs
+B<xsubpp> [B<-v>] [B<-C++>] [B<-except>] [B<-s pattern>] [B<-prototypes>] [B<-noversioncheck>] [B<-nolinenumbers>] [B<-nooptimize>] [B<-typemap typemap>] ... file.xs
 
 =head1 DESCRIPTION
 
+This compiler is typically run by the makefiles created by L<ExtUtils::MakeMaker>.
+
 I<xsubpp> will compile XS code into C code by embedding the constructs
 necessary to let C functions manipulate Perl values and creates the glue
 necessary to let Perl access those functions.  The compiler uses typemaps to
@@ -23,13 +25,15 @@
 
 =head1 OPTIONS
 
+Note that the C<XSOPT> MakeMaker option may be used to add these options to
+any makefiles generated by MakeMaker.
+
 =over 5
 
 =item B<-C++>
 
 Adds ``extern "C"'' to the C code.
 
-
 =item B<-except>
 
 Adds exception handling stubs to the C code.
@@ -59,6 +63,13 @@
 
 Prevents the inclusion of `#line' directives in the output.
 
+=item B<-nooptimize>
+
+Disables certain optimizations.  The only optimization that is currently
+affected is the use of I<target>s by the output C code (see L<perlguts>).
+This may significantly slow down the generated code, but this is the way
+B<xsubpp> of 5.005 and earlier operated.
+
 =back
 
 =head1 ENVIRONMENT
@@ -103,7 +114,7 @@
 
 $FH = 'File0000' ;
 
-$usage = "Usage: xsubpp [-v] [-C++] [-except] [-prototypes] [-noversioncheck] [-nolinenumbers] [-s pattern] [-typemap typemap]... file.xs\n";
+$usage = "Usage: xsubpp [-v] [-C++] [-except] [-prototypes] [-noversioncheck] [-nolinenumbers] [-nooptimize] [-s pattern] [-typemap typemap]... file.xs\n";
 
 $proto_re = "[" . quotemeta('\$%&*@;') . "]" ;
 # mjn
@@ -114,6 +125,7 @@
 $WantVersionChk = 1 ;
 $ProtoUsed = 0 ;
 $WantLineNumbers = 1 ;
+$WantOptimize = 1 ;
 SWITCH: while (@ARGV and $ARGV[0] =~ /^-./) {
     $flag = shift @ARGV;
     $flag =~ s/^-// ;
@@ -129,7 +141,9 @@
     push(@tm,shift),	next SWITCH	if $flag eq 'typemap';
     $WantLineNumbers = 0, next SWITCH	if $flag eq 'nolinenumbers';
     $WantLineNumbers = 1, next SWITCH	if $flag eq 'linenumbers';
-    (print "xsubpp version $XSUBPP_version\n"), exit  	
+    $WantOptimize = 0, next SWITCH	if $flag eq 'nooptimize';
+    $WantOptimize = 1, next SWITCH	if $flag eq 'optimize';
+    (print "xsubpp version $XSUBPP_version\n"), exit
 	if $flag eq 'v';
     die $usage;
 }
@@ -235,6 +249,24 @@
     $input_expr{$key} =~ s/\n+$//;
 }
 
+$bal = qr[(?:(?>[^()]+)|\((?p{ $bal })\))*];	# ()-balanced
+$cast = qr[(?:\(\s*SV\s*\*\s*\)\s*)?];		# Optional (SV*) cast
+$size = qr[,\s* (?p{ $bal }) ]x;		# Third arg (to setpvn)
+
+foreach $key (keys %output_expr) {
+    use re 'eval';
+
+    my ($t, $with_size, $arg, $sarg) =
+      ($output_expr{$key} =~
+	 m[^ \s+ sv_set ( [iunp] ) v (n)? 	# Type, is_setpvn
+	     \s* \( \s* $cast \$arg \s* ,
+	     \s* ( (?p{ $bal }) )		# Set from
+	     ( (?p{ $size }) )?			# Possible sizeof set-from
+	     \) \s* ; \s* $
+	  ]x);
+    $targetable{$key} = [$t, $with_size, $arg, $sarg] if $t;
+}
+
 $END = "!End!\n\n";		# "impossible" keyword (multiple newline)
 
 # Match an XS keyword
@@ -1099,6 +1131,8 @@
 				if !$retvaldone;
 			$args_match{"RETVAL"} = 0;
 			$var_types{"RETVAL"} = $ret_type;
+			print "\tdXSTARG;\n"
+				if $WantOptimize and $targetable{$type_kind{$ret_type}};
 		}
 
 		print $deferred;
@@ -1151,8 +1185,32 @@
 	if ($gotRETVAL && $RETVAL_code) {
 	    print "\t$RETVAL_code\n";
 	} elsif ($gotRETVAL || $wantRETVAL) {
-	    # RETVAL almost never needs SvSETMAGIC()
-	    &generate_output($ret_type, 0, 'RETVAL', 0);
+	    my $t = $WantOptimize && $targetable{$type_kind{$ret_type}};
+	    my $var = 'RETVAL';
+	    my $type = $ret_type;
+
+	    # 0: type, 1: with_size, 2: how, 3: how_size
+	    if ($t and not $t->[1] and $t->[0] eq 'p') {
+		# PUSHp corresponds to setpvn.  Treate setpv directly
+		my $what = eval qq("$t->[2]");
+		warn $@ if $@;
+
+		print "\tsv_setpv(TARG, $what); XSprePUSH; PUSHTARG;\n";
+	    }
+	    elsif ($t) {
+		my $what = eval qq("$t->[2]");
+		warn $@ if $@;
+
+		my $size = $t->[3];
+		$size = '' unless defined $size;
+		$size = eval qq("$size");
+		warn $@ if $@;
+		print "\tXSprePUSH; PUSH$t->[0]($what$size);\n";
+	    }
+	    else {
+		# RETVAL almost never needs SvSETMAGIC()
+		&generate_output($ret_type, 0, 'RETVAL', 0);
+	    }
 	}
 
 	# do cleanup
End of Patch.


[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]