This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
avoid copying the while ebcidic mapping to the stack calling get_a2n()
[perl5.git] / regen / opcode.pl
index 2d58450..a081c64 100755 (executable)
@@ -20,11 +20,14 @@ 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 = open_new('opcode.h', '>',
+                 {by => 'regen/opcode.pl', from => 'its data',
+                  file => 'opcode.h', style => '*',
+                  copyright => [1993 .. 2007]});
+
+my $on = open_new('opnames.h', '>',
+                 { by => 'regen/opcode.pl', from => 'its data', style => '*',
+                   file => 'opnames.h', copyright => [1999 .. 2008] });
 
 # Read data.
 
@@ -41,8 +44,10 @@ while (<OPS>) {
     $args = '' unless defined $args;
 
     warn qq[Description "$desc" duplicates $seen{$desc}\n]
-     if $seen{$desc} and $key ne "transr";
+     if $seen{$desc} and $key !~ "transr|(?:intro|clone)cv";
     die qq[Opcode "$key" duplicates $seen{$key}\n] if $seen{$key};
+    die qq[Opcode "freed" is reserved for the slab allocator\n]
+       if $key eq 'freed';
     $seen{$desc} = qq[description of opcode "$key"];
     $seen{$key} = qq[opcode "$key"];
 
@@ -113,11 +118,12 @@ my @raw_alias = (
                 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)],
+                Perl_pp_preinc => ['i_preinc', 'predec', 'i_predec'],
+                Perl_pp_postinc => ['i_postinc', 'postdec', 'i_postdec'],
+                Perl_pp_ehostent => [qw(enetent eprotoent eservent
+                                        spwent epwent sgrent egrent)],
+                Perl_pp_shostent => [qw(snetent sprotoent sservent)],
+                Perl_pp_aelemfast => ['aelemfast_lex'],
                );
 
 while (my ($func, $names) = splice @raw_alias, 0, 2) {
@@ -139,25 +145,7 @@ foreach my $sock_func (qw(socket bind listen accept shutdown
 
 # Emit defines.
 
-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!
- */
-
-#ifndef PERL_GLOBAL_STRUCT_INIT
-
-END
+print $oc    "#ifndef PERL_GLOBAL_STRUCT_INIT\n\n";
 
 {
     my $last_cond = '';
@@ -165,11 +153,11 @@ END
 
     sub unimplemented {
        if (@unimplemented) {
-           print "#else\n";
+           print $oc "#else\n";
            foreach (@unimplemented) {
-               print "#define $_ Perl_unimplemented_op\n";
+               print $oc "#define $_ Perl_unimplemented_op\n";
            }
-           print "#endif\n";
+           print $oc "#endif\n";
            @unimplemented = ();
        }
 
@@ -184,47 +172,30 @@ END
            unimplemented();
            $last_cond = $cond;
            if ($last_cond) {
-               print "$last_cond\n";
+               print $oc "$last_cond\n";
            }
        }
        push @unimplemented, $op_func if $last_cond;
-       print "#define $op_func $impl\n" if $impl ne $op_func;
+       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 $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 "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_max"), "\n";
+print $on "\t", tab(3,"OP_max"), "\n";
 print $on "} opcode;\n";
 print $on "\n#define MAXO ", scalar @ops, "\n";
+print $on "#define OP_FREED MAXO\n";
 
 # Emit op names and descriptions.
 
-print <<END;
+print $oc <<'END';
 START_EXTERN_C
 
 #ifndef DOINIT
@@ -234,16 +205,14 @@ EXTCONST char* const PL_op_name[] = {
 END
 
 for (@ops) {
-    print qq(\t"$_",\n);
+    print $oc qq(\t"$_",\n);
 }
 
-print <<END;
+print $oc <<'END';
+       "freed",
 };
 #endif
 
-END
-
-print <<END;
 #ifndef DOINIT
 EXTCONST char* const PL_op_desc[];
 #else
@@ -256,10 +225,11 @@ 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';
+       "freed op",
 };
 #endif
 
@@ -270,7 +240,7 @@ END
 
 # Emit ppcode switch array.
 
-print <<END;
+print $oc <<'END';
 
 START_EXTERN_C
 
@@ -292,25 +262,20 @@ for (@ops) {
     my $op_func = "Perl_pp_$_";
     my $name = $alias{$_};
     if ($name && $name->[0] ne $op_func) {
-       print "\t$op_func,\t/* implemented by $name->[0] */\n";
+       print $oc "\t$op_func,\t/* implemented by $name->[0] */\n";
     }
     else {
-       print "\t$op_func,\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[]
@@ -326,23 +291,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
@@ -351,6 +309,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
@@ -390,9 +352,12 @@ my %opflags = (
     'u' => 128,                # defaults to $_
 );
 
-my %OP_IS_SOCKET;
-my %OP_IS_FILETEST;
-my %OP_IS_FT_ACCESS;
+my %OP_IS_SOCKET;      # /Fs/
+my %OP_IS_FILETEST;    # /F-/
+my %OP_IS_FT_ACCESS;   # /F-+/
+my %OP_IS_NUMCOMPARE;  # /S</
+my %OP_IS_DIRHOP;      # /Fd/
+
 my $OCSHIFT = 8;
 my $OASHIFT = 12;
 
@@ -411,12 +376,19 @@ for my $op (@ops) {
     $argsum |= $opclass{$flags} << $OCSHIFT;
     my $argshift = $OASHIFT;
     for my $arg (split(' ',$args{$op})) {
+       if ($arg =~ s/^D//) {
+           # handle 1st, just to put D 1st.
+           $OP_IS_DIRHOP{$op}   = $opnum{$op};
+       }
        if ($arg =~ /^F/) {
            # record opnums of these opnames
            $OP_IS_SOCKET{$op}   = $opnum{$op} if $arg =~ s/s//;
            $OP_IS_FILETEST{$op} = $opnum{$op} if $arg =~ s/-//;
            $OP_IS_FT_ACCESS{$op} = $opnum{$op} if $arg =~ s/\+//;
         }
+       elsif ($arg =~ /^S</) {
+           $OP_IS_NUMCOMPARE{$op} = $opnum{$op} if $arg =~ s/<//;
+       }
        my $argnum = ($arg =~ s/\?//) ? 8 : 0;
         die "op = $op, arg = $arg\n"
            unless exists $argnum{$arg};
@@ -428,32 +400,33 @@ 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;
-
-/* 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.  */
+print $on <<'EO_OP_IS_COMMENT';
 
+/* the OP_IS_* macros are optimized to a simple range check because
+    all the member OPs are contiguous in regen/opcodes table.
+    opcode.pl verifies the range contiguity, or generates an OR-equals
+    expression */
 EO_OP_IS_COMMENT
 
 gen_op_is_macro( \%OP_IS_SOCKET, 'OP_IS_SOCKET');
 gen_op_is_macro( \%OP_IS_FILETEST, 'OP_IS_FILETEST');
 gen_op_is_macro( \%OP_IS_FT_ACCESS, 'OP_IS_FILETEST_ACCESS');
+gen_op_is_macro( \%OP_IS_NUMCOMPARE, 'OP_IS_NUMCOMPARE');
+gen_op_is_macro( \%OP_IS_DIRHOP, 'OP_IS_DIRHOP');
 
 sub gen_op_is_macro {
     my ($op_is, $macname) = @_;
@@ -467,45 +440,26 @@ 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)
        if ( $op_is->{$last} - $op_is->{$first} == scalar @rest + 1) {
            
            # contiguous ops -> optimized version
-           print $on "(op) >= OP_" . uc($first) . " && (op) <= OP_" . uc($last);
-           print $on ")\n\n";
+           print $on "(op) >= OP_" . uc($first)
+               . " && (op) <= OP_" . uc($last);
        }
        else {
            print $on join(" || \\\n\t ",
-                         map { "(op) == OP_" . uc() } sort keys %$op_is);
-           print $on ")\n\n";
+                          map { "(op) == OP_" . uc() } sort keys %$op_is);
        }
+       print $on ")\n";
     }
 }
 
-print $oc "/* ex: set ro: */\n";
-print $on "/* ex: set ro: */\n";
-
-safer_close($oc);
-safer_close($on);
-
-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
+my $pp = open_new('pp_proto.h', '>',
+                 { by => 'opcode.pl', from => 'its data' });
 
 {
     my %funcs;
@@ -515,25 +469,9 @@ END
     }
     print $pp "PERL_CALLCONV OP *$_(pTHX);\n" foreach sort keys %funcs;
 }
-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";
-  }
-}
-
-###########################################################################
-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: