This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
In regen scripts, print to explicit file handles instead of using select.
[perl5.git] / regen / opcode.pl
index dd53aec..4902e00 100755 (executable)
@@ -20,11 +20,8 @@ BEGIN {
     require 'regen/regen_lib.pl';
 }
 
-my $opcode_new = 'opcode.h-new';
-my $opname_new = 'opnames.h-new';
-my $oc = safer_open($opcode_new);
-my $on = safer_open($opname_new);
-select $oc;
+my $oc = safer_open('opcode.h-new', 'opcode.h');
+my $on = safer_open('opnames.h-new', 'opnames.h');
 
 # Read data.
 
@@ -69,11 +66,11 @@ my @raw_alias = (
                 Perl_pp_goto => ['dump'],
                 Perl_pp_require => ['dofile'],
                 Perl_pp_untie => ['dbmclose'],
-                Perl_pp_sysread => [qw(read recv)],
+                Perl_pp_sysread => {read => '', recv => '#ifdef HAS_SOCKET'},
                 Perl_pp_sysseek => ['seek'],
                 Perl_pp_ioctl => ['fcntl'],
-                Perl_pp_ssockopt => ['gsockopt'],
-                Perl_pp_getpeername => ['getsockname'],
+                Perl_pp_ssockopt => {gsockopt => '#ifdef HAS_SOCKET'},
+                Perl_pp_getpeername => {getsockname => '#ifdef HAS_SOCKET'},
                 Perl_pp_stat => ['lstat'],
                 Perl_pp_ftrowned => [qw(fteowned ftzero ftsock ftchr ftblk
                                         ftfile ftdir ftpipe ftsuid ftsgid
@@ -94,7 +91,7 @@ my @raw_alias = (
                 Perl_pp_ftrread => [qw(ftrwrite ftrexec fteread ftewrite
                                        fteexec)],
                 Perl_pp_shmwrite => [qw(shmread msgsnd msgrcv semop)],
-                Perl_pp_send => ['syswrite'],
+                Perl_pp_syswrite => {send => '#ifdef HAS_SOCKET'},
                 Perl_pp_defined => [qw(dor dorassign)],
                  Perl_pp_and => ['andassign'],
                 Perl_pp_or => ['orassign'],
@@ -109,86 +106,94 @@ my @raw_alias = (
                 Perl_pp_rv2av => ['rv2hv'],
                 Perl_pp_akeys => ['avalues'],
                 Perl_pp_rkeys => [qw(rvalues reach)],
-                Perl_pp_trans => ['transr'],
-                Perl_pp_chop => ['chomp'],
-                Perl_pp_schop => ['schomp'],
-                Perl_pp_bind => ['connect'],
+                Perl_pp_trans => [qw(trans transr)],
+                Perl_pp_chop => [qw(chop chomp)],
+                Perl_pp_schop => [qw(schop schomp)],
+                Perl_pp_bind => {connect => '#ifdef HAS_SOCKET'},
                 Perl_pp_preinc => ['i_preinc'],
                 Perl_pp_predec => ['i_predec'],
                 Perl_pp_postinc => ['i_postinc'],
                 Perl_pp_postdec => ['i_postdec'],
+                Perl_pp_ehostent => [qw(enetent eprotoent eservent
+                                        spwent epwent sgrent egrent)],
+                Perl_pp_shostent => [qw(snetent sprotoent sservent)],
                );
 
 while (my ($func, $names) = splice @raw_alias, 0, 2) {
-    foreach (@$names) {
-       $alias{$_} = $func;
+    if (ref $names eq 'ARRAY') {
+       foreach (@$names) {
+           $alias{$_} = [$func, ''];
+       }
+    } else {
+       while (my ($opname, $cond) = each %$names) {
+           $alias{$opname} = [$func, $cond];
+       }
     }
 }
 
-# Emit defines.
+foreach my $sock_func (qw(socket bind listen accept shutdown
+                         ssockopt getpeername)) {
+    $alias{$sock_func} = ["Perl_pp_$sock_func", '#ifdef HAS_SOCKET'],
+}
 
-print <<"END";
-/* -*- buffer-read-only: t -*-
- *
- *    opcode.h
- *
- *    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
- *    2001, 2002, 2003, 2004, 2005, 2006, 2007 by Larry Wall and others
- *
- *    You may distribute under the terms of either the GNU General Public
- *    License or the Artistic License, as specified in the README file.
- *
- * !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
- *  This file is built by regen/opcode.pl from its data.  Any changes made
- *  here will be lost!
- */
+# Emit defines.
 
-#ifndef PERL_GLOBAL_STRUCT_INIT
+print $oc read_only_top(lang => 'C', by => 'regen/opcode.pl', from => 'its data',
+                       file => 'opcode.h', style => '*',
+                       copyright => [1993 .. 2007]),
+    "#ifndef PERL_GLOBAL_STRUCT_INIT\n\n";
+
+{
+    my $last_cond = '';
+    my @unimplemented;
+
+    sub unimplemented {
+       if (@unimplemented) {
+           print $oc "#else\n";
+           foreach (@unimplemented) {
+               print $oc "#define $_ Perl_unimplemented_op\n";
+           }
+           print $oc "#endif\n";
+           @unimplemented = ();
+       }
 
-END
+    }
 
-for (@ops) {
-    print "#define Perl_pp_$_ $alias{$_}\n" if $alias{$_};
+    for (@ops) {
+       my ($impl, $cond) = @{$alias{$_} || ["Perl_pp_$_", '']};
+       my $op_func = "Perl_pp_$_";
+
+       if ($cond ne $last_cond) {
+           # A change in condition. (including to or from no condition)
+           unimplemented();
+           $last_cond = $cond;
+           if ($last_cond) {
+               print $oc "$last_cond\n";
+           }
+       }
+       push @unimplemented, $op_func if $last_cond;
+       print $oc "#define $op_func $impl\n" if $impl ne $op_func;
+    }
+    # If the last op was conditional, we need to close it out:
+    unimplemented();
 }
 
-print <<'END';
-
-PERL_PPDEF(Perl_unimplemented_op)
-
-END
-
-print $on <<"END";
-/* -*- buffer-read-only: t -*-
- *
- *    opnames.h
- *
- *    Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
- *    2007, 2008 by Larry Wall and others
- *
- *    You may distribute under the terms of either the GNU General Public
- *    License or the Artistic License, as specified in the README file.
- *
- *
- * !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
- *  This file is built by regen/opcode.pl from its data.  Any changes made
- *  here will be lost!
- */
-
-typedef enum opcode {
-END
+print $on read_only_top(lang => 'C', by => 'regen/opcode.pl',
+                       from => 'its data', style => '*',
+                       file => 'opnames.h', copyright => [1999 .. 2008]),
+    "typedef enum opcode {\n";
 
 my $i = 0;
 for (@ops) {
-    # print $on "\t", &tab(3,"OP_\U$_,"), "/* ", $i++, " */\n";
-      print $on "\t", &tab(3,"OP_\U$_"), " = ", $i++, ",\n";
+      print $on "\t", tab(3,"OP_\U$_"), " = ", $i++, ",\n";
 }
-print $on "\t", &tab(3,"OP_max"), "\n";
+print $on "\t", tab(3,"OP_max"), "\n";
 print $on "} opcode;\n";
 print $on "\n#define MAXO ", scalar @ops, "\n";
 
 # Emit op names and descriptions.
 
-print <<END;
+print $oc <<'END';
 START_EXTERN_C
 
 #ifndef DOINIT
@@ -198,16 +203,13 @@ EXTCONST char* const PL_op_name[] = {
 END
 
 for (@ops) {
-    print qq(\t"$_",\n);
+    print $oc qq(\t"$_",\n);
 }
 
-print <<END;
+print $oc <<'END';
 };
 #endif
 
-END
-
-print <<END;
 #ifndef DOINIT
 EXTCONST char* const PL_op_desc[];
 #else
@@ -220,10 +222,10 @@ for (@ops) {
     # Have to escape double quotes and escape characters.
     $safe_desc =~ s/([\\"])/\\$1/g;
 
-    print qq(\t"$safe_desc",\n);
+    print $oc qq(\t"$safe_desc",\n);
 }
 
-print <<END;
+print $oc <<'END';
 };
 #endif
 
@@ -232,21 +234,9 @@ END_EXTERN_C
 #endif /* !PERL_GLOBAL_STRUCT_INIT */
 END
 
-# Emit function declarations.
-
-#for (sort keys %ckname) {
-#    print "OP *\t", &tab(3,$_),"(pTHX_ OP* o);\n";
-#}
-#
-#print "\n";
-#
-#for (@ops) {
-#    print "OP *\t", &tab(3, "pp_$_"), "(pTHX);\n";
-#}
-
 # Emit ppcode switch array.
 
-print <<END;
+print $oc <<'END';
 
 START_EXTERN_C
 
@@ -265,26 +255,23 @@ EXT Perl_ppaddr_t PL_ppaddr[] /* or perlvars.h */
 END
 
 for (@ops) {
-    if (my $name = $alias{$_}) {
-       print "\t$name,\t/* Perl_pp_$_ */\n";
+    my $op_func = "Perl_pp_$_";
+    my $name = $alias{$_};
+    if ($name && $name->[0] ne $op_func) {
+       print $oc "\t$op_func,\t/* implemented by $name->[0] */\n";
     }
     else {
-       print "\tPerl_pp_$_,\n";
+       print $oc "\t$op_func,\n";
     }
 }
 
-print <<END;
+print $oc <<'END';
 }
 #endif
 #ifdef PERL_PPADDR_INITED
 ;
 #endif
 
-END
-
-# Emit check routines.
-
-print <<END;
 #ifdef PERL_GLOBAL_STRUCT_INIT
 #  define PERL_CHECK_INITED
 static const Perl_check_t Gcheck[]
@@ -300,23 +287,16 @@ EXT Perl_check_t PL_check[] /* or perlvars.h */
 END
 
 for (@ops) {
-    print "\t", &tab(3, "Perl_$check{$_},"), "\t/* $_ */\n";
+    print $oc "\t", tab(3, "Perl_$check{$_},"), "\t/* $_ */\n";
 }
 
-print <<END;
+print $oc <<'END';
 }
 #endif
 #ifdef PERL_CHECK_INITED
 ;
 #endif /* #ifdef PERL_CHECK_INITED */
 
-END
-
-# Emit allowed argument types.
-
-my $ARGBITS = 32;
-
-print <<END;
 #ifndef PERL_GLOBAL_STRUCT_INIT
 
 #ifndef DOINIT
@@ -325,6 +305,10 @@ EXTCONST U32 PL_opargs[];
 EXTCONST U32 PL_opargs[] = {
 END
 
+# Emit allowed argument types.
+
+my $ARGBITS = 32;
+
 my %argnum = (
     'S',  1,           # scalar
     'L',  2,           # list
@@ -402,27 +386,25 @@ for my $op (@ops) {
        $argshift += 4;
     }
     $argsum = sprintf("0x%08x", $argsum);
-    print "\t", &tab(3, "$argsum,"), "/* $op */\n";
+    print $oc "\t", tab(3, "$argsum,"), "/* $op */\n";
 }
 
-print <<END;
+print $oc <<'END';
 };
 #endif
 
 #endif /* !PERL_GLOBAL_STRUCT_INIT */
 
 END_EXTERN_C
-
 END
 
 # Emit OP_IS_* macros
 
-print $on <<EO_OP_IS_COMMENT;
+print $on <<'EO_OP_IS_COMMENT';
 
 /* the OP_IS_(SOCKET|FILETEST) macros are optimized to a simple range
     check because all the member OPs are contiguous in opcode.pl
     <OPS> table.  opcode.pl verifies the range contiguity.  */
-
 EO_OP_IS_COMMENT
 
 gen_op_is_macro( \%OP_IS_SOCKET, 'OP_IS_SOCKET');
@@ -441,7 +423,7 @@ sub gen_op_is_macro {
        my $last = pop @rest;   # @rest slurped, get its last
        die "Invalid range of ops: $first .. $last\n" unless $last;
 
-       print $on "#define $macname(op) \\\n\t(";
+       print $on "\n#define $macname(op)       \\\n\t(";
 
        # verify that op-ct matches 1st..last range (and fencepost)
        # (we know there are no dups)
@@ -449,62 +431,31 @@ sub gen_op_is_macro {
            
            # contiguous ops -> optimized version
            print $on "(op) >= OP_" . uc($first) . " && (op) <= OP_" . uc($last);
-           print $on ")\n\n";
+           print $on ")\n";
        }
        else {
            print $on join(" || \\\n\t ",
                          map { "(op) == OP_" . uc() } sort keys %$op_is);
-           print $on ")\n\n";
+           print $on ")\n";
        }
     }
 }
 
-print $oc "/* ex: set ro: */\n";
-print $on "/* ex: set ro: */\n";
+my $pp = safer_open('pp_proto.h-new', 'pp_proto.h');
 
-safer_close($oc);
-safer_close($on);
+print $pp read_only_top(lang => 'C', by => 'opcode.pl', from => 'its data');
 
-rename_if_different $opcode_new, 'opcode.h';
-rename_if_different $opname_new, 'opnames.h';
-
-my $pp_proto_new = 'pp_proto.h-new';
-
-my $pp = safer_open($pp_proto_new);
-
-print $pp <<"END";
-/* -*- buffer-read-only: t -*-
-   !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
-   This file is built by opcode.pl from its data.  Any changes made here
-   will be lost!
-*/
-
-END
-
-for (sort @ops) {
-    next if /^i_(pre|post)(inc|dec)$/;
-    next if /^custom$/;
-    print $pp "PERL_CALLCONV OP *Perl_pp_$_(pTHX);\n";
-}
-print $pp "\n/* ex: set ro: */\n";
-
-safer_close($pp);
-
-rename_if_different $pp_proto_new, 'pp_proto.h';
-
-END {
-  foreach ('opcode.h', 'opnames.h', 'pp_proto.h') {
-    1 while unlink "$_-old";
-  }
+{
+    my %funcs;
+    for (@ops) {
+       my $name = $alias{$_} ? $alias{$_}[0] : "Perl_pp_$_";
+       ++$funcs{$name};
+    }
+    print $pp "PERL_CALLCONV OP *$_(pTHX);\n" foreach sort keys %funcs;
 }
-
-###########################################################################
-sub tab {
-    my ($l, $t) = @_;
-    $t .= "\t" x ($l - (length($t) + 1) / 8);
-    $t;
+foreach ($oc, $on, $pp) {
+    read_only_bottom_close_and_rename($_);
 }
-###########################################################################
 
 # Some comments about 'T' opcode classifier: