This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Wrap long pod lines in B::Op_private for real
[perl5.git] / regen / opcode.pl
1 #!/usr/bin/perl -w
2
3 # Regenerate (overwriting only if changed):
4 #
5 #    opcode.h
6 #    opnames.h
7 #    pp_proto.h
8 #    lib/B/Op_private.pm
9 #
10 # from:
11 #  * information stored in regen/opcodes;
12 #  * information stored in regen/op_private (which is actually perl code);
13 #  * the values hardcoded into this script in @raw_alias.
14 #
15 # Accepts the standard regen_lib -q and -v args.
16 #
17 # This script is normally invoked from regen.pl.
18
19 use strict;
20
21 BEGIN {
22     # Get function prototypes
23     require 'regen/regen_lib.pl';
24 }
25
26 my $oc = open_new('opcode.h', '>',
27                   {by => 'regen/opcode.pl', from => 'its data',
28                    file => 'opcode.h', style => '*',
29                    copyright => [1993 .. 2007]});
30
31 my $on = open_new('opnames.h', '>',
32                   { by => 'regen/opcode.pl', from => 'its data', style => '*',
33                     file => 'opnames.h', copyright => [1999 .. 2008] });
34
35 my $oprivpm = open_new('lib/B/Op_private.pm', '>',
36                   { by => 'regen/opcode.pl',
37                     from => "data in\nregen/op_private "
38                            ."and pod embedded in regen/opcode.pl",
39                     style => '#',
40                     file => 'lib/B/Op_private.pm',
41                     copyright => [2014 .. 2014] });
42
43 # Read 'opcodes' data.
44
45 my %seen;
46 my (@ops, %desc, %check, %ckname, %flags, %args, %opnum);
47
48 open OPS, 'regen/opcodes' or die $!;
49
50 while (<OPS>) {
51     chop;
52     next unless $_;
53     next if /^#/;
54     my ($key, $desc, $check, $flags, $args) = split(/\t+/, $_, 5);
55     $args = '' unless defined $args;
56
57     warn qq[Description "$desc" duplicates $seen{$desc}\n]
58      if $seen{$desc} and $key !~ "transr|(?:intro|clone)cv";
59     die qq[Opcode "$key" duplicates $seen{$key}\n] if $seen{$key};
60     die qq[Opcode "freed" is reserved for the slab allocator\n]
61         if $key eq 'freed';
62     $seen{$desc} = qq[description of opcode "$key"];
63     $seen{$key} = qq[opcode "$key"];
64
65     push(@ops, $key);
66     $opnum{$key} = $#ops;
67     $desc{$key} = $desc;
68     $check{$key} = $check;
69     $ckname{$check}++;
70     $flags{$key} = $flags;
71     $args{$key} = $args;
72 }
73
74 # Set up aliases
75
76 my %alias;
77
78 # Format is "this function" => "does these op names"
79 my @raw_alias = (
80                  Perl_do_kv => [qw( keys values )],
81                  Perl_unimplemented_op => [qw(padany mapstart custom)],
82                  # All the ops with a body of { return NORMAL; }
83                  Perl_pp_null => [qw(scalar regcmaybe lineseq scope)],
84
85                  Perl_pp_goto => ['dump'],
86                  Perl_pp_require => ['dofile'],
87                  Perl_pp_untie => ['dbmclose'],
88                  Perl_pp_sysread => {read => '', recv => '#ifdef HAS_SOCKET'},
89                  Perl_pp_sysseek => ['seek'],
90                  Perl_pp_ioctl => ['fcntl'],
91                  Perl_pp_ssockopt => {gsockopt => '#ifdef HAS_SOCKET'},
92                  Perl_pp_getpeername => {getsockname => '#ifdef HAS_SOCKET'},
93                  Perl_pp_stat => ['lstat'],
94                  Perl_pp_ftrowned => [qw(fteowned ftzero ftsock ftchr ftblk
95                                          ftfile ftdir ftpipe ftsuid ftsgid
96                                          ftsvtx)],
97                  Perl_pp_fttext => ['ftbinary'],
98                  Perl_pp_gmtime => ['localtime'],
99                  Perl_pp_semget => [qw(shmget msgget)],
100                  Perl_pp_semctl => [qw(shmctl msgctl)],
101                  Perl_pp_ghostent => [qw(ghbyname ghbyaddr)],
102                  Perl_pp_gnetent => [qw(gnbyname gnbyaddr)],
103                  Perl_pp_gprotoent => [qw(gpbyname gpbynumber)],
104                  Perl_pp_gservent => [qw(gsbyname gsbyport)],
105                  Perl_pp_gpwent => [qw(gpwnam gpwuid)],
106                  Perl_pp_ggrent => [qw(ggrnam ggrgid)],
107                  Perl_pp_ftis => [qw(ftsize ftmtime ftatime ftctime)],
108                  Perl_pp_chown => [qw(unlink chmod utime kill)],
109                  Perl_pp_link => ['symlink'],
110                  Perl_pp_ftrread => [qw(ftrwrite ftrexec fteread ftewrite
111                                         fteexec)],
112                  Perl_pp_shmwrite => [qw(shmread msgsnd msgrcv semop)],
113                  Perl_pp_syswrite => {send => '#ifdef HAS_SOCKET'},
114                  Perl_pp_defined => [qw(dor dorassign)],
115                  Perl_pp_and => ['andassign'],
116                  Perl_pp_or => ['orassign'],
117                  Perl_pp_ucfirst => ['lcfirst'],
118                  Perl_pp_sle => [qw(slt sgt sge)],
119                  Perl_pp_print => ['say'],
120                  Perl_pp_index => ['rindex'],
121                  Perl_pp_oct => ['hex'],
122                  Perl_pp_shift => ['pop'],
123                  Perl_pp_sin => [qw(cos exp log sqrt)],
124                  Perl_pp_bit_or => ['bit_xor'],
125                  Perl_pp_rv2av => ['rv2hv'],
126                  Perl_pp_akeys => ['avalues'],
127                  Perl_pp_rkeys => [qw(rvalues reach)],
128                  Perl_pp_trans => [qw(trans transr)],
129                  Perl_pp_chop => [qw(chop chomp)],
130                  Perl_pp_schop => [qw(schop schomp)],
131                  Perl_pp_bind => {connect => '#ifdef HAS_SOCKET'},
132                  Perl_pp_preinc => ['i_preinc', 'predec', 'i_predec'],
133                  Perl_pp_postinc => ['i_postinc', 'postdec', 'i_postdec'],
134                  Perl_pp_ehostent => [qw(enetent eprotoent eservent
135                                          spwent epwent sgrent egrent)],
136                  Perl_pp_shostent => [qw(snetent sprotoent sservent)],
137                  Perl_pp_aelemfast => ['aelemfast_lex'],
138                 );
139
140 while (my ($func, $names) = splice @raw_alias, 0, 2) {
141     if (ref $names eq 'ARRAY') {
142         foreach (@$names) {
143             $alias{$_} = [$func, ''];
144         }
145     } else {
146         while (my ($opname, $cond) = each %$names) {
147             $alias{$opname} = [$func, $cond];
148         }
149     }
150 }
151
152 foreach my $sock_func (qw(socket bind listen accept shutdown
153                           ssockopt getpeername)) {
154     $alias{$sock_func} = ["Perl_pp_$sock_func", '#ifdef HAS_SOCKET'],
155 }
156
157
158
159 # =================================================================
160 #
161 # Functions for processing regen/op_private data.
162 #
163 # Put them in a separate package so that croak() does the right thing
164
165 package OP_PRIVATE;
166
167 use Carp;
168
169
170 # the vars holding the global state built up by all the calls to addbits()
171
172
173 # map OPpLVAL_INTRO => LVINTRO
174 my %LABELS;
175
176
177 # the numeric values of flags - what will get output as a #define
178 my %DEFINES;
179
180 # %BITFIELDS: the various bit field types. The key is the concatenation of
181 # all the field values that make up a bit field hash; the values are bit
182 # field hash refs.  This allows us to de-dup identical bit field defs
183 # across different ops, and thus make the output tables more compact (esp
184 # important for the C version)
185 my %BITFIELDS;
186
187 # %FLAGS: the main data structure. Indexed by op name, then bit index:
188 # single bit flag:
189 #   $FLAGS{rv2av}{2} = 'OPpSLICEWARNING';
190 # bit field (bits 5 and 6):
191 #   $FLAGS{rv2av}{5} = $FLAGS{rv2av}{6} = { .... };
192 my %FLAGS;
193
194
195 # do, with checking, $LABELS{$name} = $label
196
197 sub add_label {
198     my ($name, $label) = @_;
199     if (exists $LABELS{$name} and $LABELS{$name} ne $label) {
200         croak "addbits(): label for flag '$name' redefined:\n"
201         .  "  was '$LABELS{$name}', now '$label'";
202     }
203     $LABELS{$name} = $label;
204 }
205
206 #
207 # do, with checking, $DEFINES{$name} = $val
208
209 sub add_define {
210     my ($name, $val) = @_;
211     if (exists $DEFINES{$name} && $DEFINES{$name} != $val) {
212         croak "addbits(): value for flag '$name' redefined:\n"
213         .  "  was $DEFINES{$name}, now $val";
214     }
215     $DEFINES{$name} = $val;
216 }
217
218
219 # intended to be called from regen/op_private; see that file for details
220
221 sub ::addbits {
222     my @args = @_;
223
224     croak "too few arguments for addbits()" unless @args >= 3;
225     my $op = shift @args;
226     croak "invalid op name: '$op'" unless exists $opnum{$op};
227
228     while (@args) {
229         my $bits = shift @args;
230         if ($bits =~ /^[0-7]$/) {
231             # single bit
232             croak "addbits(): too few arguments for single bit flag"
233                 unless @args >= 2;
234             my $flag_name   = shift @args;
235             my $flag_label  = shift @args;
236             add_label($flag_name, $flag_label);
237             croak "addbits(): bit $bits of $op already specified"
238                 if defined $FLAGS{$op}{$bits};
239             $FLAGS{$op}{$bits} = $flag_name;
240             add_define($flag_name, (1 << $bits));
241         }
242         elsif ($bits =~ /^([0-7])\.\.([0-7])$/) {
243             # bit range
244             my ($bitmin, $bitmax) = ($1,$2);
245
246             croak "addbits(): min bit > max bit in bit range '$bits'"
247                 unless $bitmin <= $bitmax;
248             croak "addbits(): bit field argument missing"
249                 unless @args >= 1;
250
251             my $arg_hash = shift @args;
252             croak "addbits(): arg to $bits must be a hash ref"
253                 unless defined $arg_hash and ref($arg_hash) =~ /HASH/;
254
255             my %valid_keys;
256             @valid_keys{qw(baseshift_def bitcount_def mask_def label enum)} = ();
257             for (keys %$arg_hash) {
258                 croak "addbits(): unrecognised bifield key: '$_'"
259                     unless exists $valid_keys{$_};
260             }
261
262             my $bitmask = 0;
263             $bitmask += (1 << $_) for $bitmin..$bitmax;
264
265             my $enum_id ='';
266
267             if (defined $arg_hash->{enum}) {
268                 my $enum = $arg_hash->{enum};
269                 croak "addbits(): arg to enum must be an array ref"
270                     unless defined $enum and ref($enum) =~ /ARRAY/;
271                 croak "addbits(): enum list must be in triplets"
272                     unless @$enum % 3 == 0;
273
274                 my $max_id = (1 << ($bitmax - $bitmin + 1)) - 1;
275
276                 my @e = @$enum;
277                 while (@e) {
278                     my $enum_ix     = shift @e;
279                     my $enum_name   = shift @e;
280                     my $enum_label  = shift @e;
281                     croak "addbits(): enum index must be a number: '$enum_ix'"
282                         unless $enum_ix =~ /^\d+$/;
283                     croak "addbits(): enum index too big: '$enum_ix'"
284                         unless $enum_ix  <= $max_id;
285                     add_label($enum_name, $enum_label);
286                     add_define($enum_name, $enum_ix << $bitmin);
287                     $enum_id .= "($enum_ix:$enum_name:$enum_label)";
288                 }
289             }
290
291             # id is a fingerprint of all the content of the bit field hash
292             my $id = join ':', map defined() ? $_ : "-undef-",
293                 $bitmin, $bitmax,
294                 $arg_hash->{label},
295                 $arg_hash->{mask_def},
296                 $arg_hash->{baseshift_def},
297                 $arg_hash->{bitcount_def},
298                 $enum_id;
299
300             unless (defined $BITFIELDS{$id}) {
301
302                 if (defined $arg_hash->{mask_def}) {
303                     add_define($arg_hash->{mask_def}, $bitmask);
304                 }
305
306                 if (defined $arg_hash->{baseshift_def}) {
307                     add_define($arg_hash->{baseshift_def}, $bitmin);
308                 }
309
310                 if (defined $arg_hash->{bitcount_def}) {
311                     add_define($arg_hash->{bitcount_def}, $bitmax-$bitmin+1);
312                 }
313
314                 # create deep copy
315
316                 my $copy = {};
317                 for (qw(baseshift_def  bitcount_def mask_def label)) {
318                     $copy->{$_} = $arg_hash->{$_} if defined $arg_hash->{$_};
319                 }
320                 if (defined $arg_hash->{enum}) {
321                     $copy->{enum} = [ @{$arg_hash->{enum}} ];
322                 }
323
324                 # and add some extra fields
325
326                 $copy->{bitmask} = $bitmask;
327                 $copy->{bitmin} = $bitmin;
328                 $copy->{bitmax} = $bitmax;
329
330                 $BITFIELDS{$id} = $copy;
331             }
332
333             for my $bit ($bitmin..$bitmax) {
334                 croak "addbits(): bit $bit of $op already specified"
335                     if defined $FLAGS{$op}{$bit};
336                 $FLAGS{$op}{$bit} = $BITFIELDS{$id};
337             }
338         }
339         else {
340             croak "addbits(): invalid bit specifier '$bits'";
341         }
342     }
343 }
344
345
346 # intended to be called from regen/op_private; see that file for details
347
348 sub ::ops_with_flag {
349     my $flag = shift;
350     return grep $flags{$_} =~ /\Q$flag/, sort keys %flags;
351 }
352
353
354 # intended to be called from regen/op_private; see that file for details
355
356 sub ::ops_with_check {
357     my $c = shift;
358     return grep $check{$_} eq $c, sort keys %check;
359 }
360
361
362 # intended to be called from regen/op_private; see that file for details
363
364 sub ::ops_with_arg {
365     my ($i, $arg_type) = @_;
366     my @ops;
367     for my $op (sort keys %args) {
368         my @args = split(' ',$args{$op});
369         push @ops, $op if defined $args[$i] and $args[$i] eq $arg_type;
370     }
371     @ops;
372 }
373
374
375 # output '#define OPpLVAL_INTRO 0x80' etc
376
377 sub print_defines {
378     my $fh = shift;
379
380     for (sort { $DEFINES{$a} <=> $DEFINES{$b} || $a cmp $b } keys %DEFINES) {
381         printf $fh "#define %-23s 0x%02x\n", $_, $DEFINES{$_};
382     }
383 }
384
385
386 # Generate the content of B::Op_private
387
388 sub print_B_Op_private {
389     my $fh = shift;
390
391     my $header = <<'EOF';
392 @=head1 NAME
393 @
394 @B::Op_private -  OP op_private flag definitions
395 @
396 @=head1 SYNOPSIS
397 @
398 @    use B::Op_private;
399 @
400 @    # flag details for bit 7 of OP_AELEM's op_private:
401 @    my $name  = $B::Op_private::bits{aelem}{7}; # OPpLVAL_INTRO
402 @    my $value = $B::Op_private::defines{$name}; # 128
403 @    my $label = $B::Op_private::labels{$name};  # LVINTRO
404 @
405 @    # the bit field at bits 5..6 of OP_AELEM's op_private:
406 @    my $bf  = $B::Op_private::bits{aelem}{6};
407 @    my $mask = $bf->{bitmask}; # etc
408 @
409 @=head1 DESCRIPTION
410 @
411 @This module provides three global hashes:
412 @
413 @    %B::Op_private::bits
414 @    %B::Op_private::defines
415 @    %B::Op_private::labels
416 @
417 @which contain information about the per-op meanings of the bits in the
418 @op_private field.
419 @
420 @=head2 C<%bits>
421 @
422 @This is indexed by op name and then bit number (0..7). For single bit flags,
423 @it returns the name of the define (if any) for that bit:
424 @
425 @   $B::Op_private::bits{aelem}{7} eq 'OPpLVAL_INTRO';
426 @
427 @For bit fields, it returns a hash ref containing details about the field.
428 @The same reference will be returned for all bit positions that make
429 @up the bit field; so for example these both return the same hash ref:
430 @
431 @    $bitfield = $B::Op_private::bits{aelem}{5};
432 @    $bitfield = $B::Op_private::bits{aelem}{6};
433 @
434 @The general format of this hash ref is
435 @
436 @    {
437 @        # The bit range and mask; these are always present.
438 @        bitmin        => 5,
439 @        bitmax        => 6,
440 @        bitmask       => 0x60,
441 @
442 @        # (The remaining keys are optional)
443 @
444 @        # The names of any defines that were requested:
445 @        mask_def      => 'OPpFOO_MASK',
446 @        baseshift_def => 'OPpFOO_SHIFT',
447 @        bitcount_def  => 'OPpFOO_BITS',
448 @
449 @        # If present, Concise etc will display the value with a 'FOO='
450 @        # prefix. If it equals '-', then Concise will treat the bit
451 @        # field as raw bits and not try to interpret it.
452 @        label         => 'FOO',
453 @
454 @        # If present, specifies the names of some defines and the
455 @        # display labels that are used to assign meaning to particu-
456 @        # lar integer values within the bit field; e.g. 3 is dis-
457 @        # played as 'C'.
458 @        enum          => [ qw(
459 @                             1   OPpFOO_A  A
460 @                             2   OPpFOO_B  B
461 @                             3   OPpFOO_C  C
462 @                         )],
463 @
464 @    };
465 @
466 @
467 @=head2 C<%defines>
468 @
469 @This gives the value of every C<OPp> define, e.g.
470 @
471 @    $B::Op_private::defines{OPpLVAL_INTRO} == 128;
472 @
473 @=head2 C<%labels>
474 @
475 @This gives the short display label for each define, as used by C<B::Concise>
476 @and C<perl -Dx>, e.g.
477 @
478 @    $B::Op_private::labels{OPpLVAL_INTRO} eq 'LVINTRO';
479 @
480 @If the label equals '-', then Concise will treat the bit as a raw bit and
481 @not try to display it symbolically.
482 @
483 @=cut
484
485 package B::Op_private;
486
487 our %bits;
488
489 EOF
490     # remove podcheck.t-defeating leading char
491     $header =~ s/^\@//gm;
492     print $fh $header;
493
494     # for each flag/bit combination, find the ops which use it
495     my %combos;
496     for my $op (sort keys %FLAGS) {
497         my $entry = $FLAGS{$op};
498         for my $bit (0..7) {
499             my $e = $entry->{$bit};
500             next unless defined $e;
501             next if ref $e; # bit field, not flag
502             push @{$combos{$e}{$bit}}, $op;
503         }
504     }
505
506     # dump flags used by multiple ops
507     for my $flag (sort keys %combos) {
508         for my $bit (sort keys %{$combos{$flag}}) {
509             my $ops = $combos{$flag}{$bit};
510             next unless @$ops > 1;
511             my @o = sort @$ops;
512             print $fh "\$bits{\$_}{$bit} = '$flag' for qw(@o);\n";
513         }
514     }
515
516     # dump bit field definitions
517
518     my %bitfield_ix;
519     {
520         my %bitfields;
521         # stringified-ref to ref mapping
522         $bitfields{$_} = $_ for values %BITFIELDS;
523         my $ix = -1;
524         my $s = "\nmy \@bf = (\n";
525         for my $bitfield_key (sort keys %BITFIELDS) {
526             my $bitfield = $BITFIELDS{$bitfield_key};
527             $ix++;
528             $bitfield_ix{$bitfield} = $ix;
529
530             $s .= "    {\n";
531             for (qw(label mask_def baseshift_def bitcount_def)) {
532                 next unless defined $bitfield->{$_};
533                 $s .= sprintf "        %-9s => '%s',\n",
534                             $_,  $bitfield->{$_};
535             }
536             for (qw(bitmin bitmax bitmask)) {
537                 croak "panic" unless defined $bitfield->{$_};
538                 $s .= sprintf "        %-9s => %d,\n",
539                             $_,  $bitfield->{$_};
540             }
541             if (defined $bitfield->{enum}) {
542                 $s .= "        enum      => [\n";
543                 my @enum = @{$bitfield->{enum}};
544                 while (@enum) {
545                     my $i     = shift @enum;
546                     my $name  = shift @enum;
547                     my $label = shift @enum;
548                     $s .= sprintf "            %d, %-10s, %s,\n",
549                             $i, "'$name'", "'$label'";
550                 }
551                 $s .= "        ],\n";
552             }
553             $s .= "    },\n";
554
555         }
556         $s .= ");\n";
557         print $fh "$s\n";
558     }
559
560     # dump bitfields and remaining labels
561
562     for my $op (sort keys %FLAGS) {
563         my @indices;
564         my @vals;
565         my $entry = $FLAGS{$op};
566         my $bit;
567
568         for ($bit = 7; $bit >= 0; $bit--) {
569             next unless defined $entry->{$bit};
570             my $e = $entry->{$bit};
571             if (ref $e) {
572                 my $ix = $bitfield_ix{$e};
573                 for (reverse $e->{bitmin}..$e->{bitmax}) {
574                     push @indices,  $_;
575                     push @vals, "\$bf[$ix]";
576                 }
577                 $bit = $e->{bitmin};
578             }
579             else {
580                 next if @{$combos{$e}{$bit}} > 1;  # already output
581                 push @indices, $bit;
582                 push @vals, "'$e'";
583             }
584         }
585         if (@indices) {
586             my $s = '';
587             $s = '@{' if @indices > 1;
588             $s .= "\$bits{$op}";
589             $s .= '}' if @indices > 1;
590             $s .= '{' . join(',', @indices) . '} = ';
591             $s .= '(' if @indices > 1;
592             $s .= join ', ', @vals;
593             $s .= ')' if @indices > 1;
594             print $fh "$s;\n";
595         }
596     }
597
598     # populate %defines and %labels
599
600     print  $fh "\n\nour %defines = (\n";
601     printf $fh "    %-23s  => %3d,\n", $_ , $DEFINES{$_} for sort keys %DEFINES;
602     print  $fh ");\n\nour %labels = (\n";
603     printf $fh "    %-23s  => '%s',\n", $_ , $LABELS{$_}  for sort keys %LABELS;
604     print  $fh ");\n";
605
606 }
607
608
609
610 # output the contents of the assorted PL_op_private_*[] tables
611
612 sub print_PL_op_private_tables {
613     my $fh = shift;
614
615     my $PL_op_private_labels     = '';
616     my $PL_op_private_valid      = '';
617     my $PL_op_private_bitdef_ix  = '';
618     my $PL_op_private_bitdefs    = '';
619     my $PL_op_private_bitfields  = '';
620
621     my %label_ix;
622     my %bitfield_ix;
623
624     # generate $PL_op_private_labels
625
626     {
627         my %labs;
628         $labs{$_} = 1 for values %LABELS; # de-duplicate labels
629         # add in bit field labels
630         for (values %BITFIELDS) {
631             next unless defined $_->{label};
632             $labs{$_->{label}} = 1;
633         }
634
635         my $labels = '';
636         for my $lab (sort keys %labs) {
637             $label_ix{$lab} = length $labels;
638             $labels .= "$lab\0";
639             $PL_op_private_labels .=
640                   "    "
641                 . join(',', map("'$_'", split //, $lab))
642                 . ",'\\0',\n";
643         }
644     }
645
646
647     # generate PL_op_private_bitfields
648
649     {
650         my %bitfields;
651         # stringified-ref to ref mapping
652         $bitfields{$_} = $_ for values %BITFIELDS;
653
654         my $ix = 0;
655         for my $bitfield_key (sort keys %BITFIELDS) {
656             my $bf = $BITFIELDS{$bitfield_key};
657             $bitfield_ix{$bf} = $ix;
658
659             my @b;
660             push @b, $bf->{bitmin},
661                 defined $bf->{label} ? $label_ix{$bf->{label}} : -1;
662             my $enum = $bf->{enum};
663             if (defined $enum) {
664                 my @enum = @$enum;
665                 while (@enum) {
666                     my $i     = shift @enum;
667                     my $name  = shift @enum;
668                     my $label = shift @enum;
669                     push @b, $i, $label_ix{$label};
670                 }
671             }
672             push @b, -1; # terminate enum list
673
674             $PL_op_private_bitfields .= "    " .  join(', ', @b) .",\n";
675             $ix += @b;
676         }
677     }
678
679
680     # generate PL_op_private_bitdefs, PL_op_private_bitdef_ix
681
682     {
683         my $bitdef_count = 0;
684
685         my %not_seen = %FLAGS;
686
687         my $opnum = -1;
688         for my $op (sort { $opnum{$a} <=> $opnum{$b} } keys %opnum) {
689             $opnum++;
690             die "panic: opnum misorder: opnum=$opnum opnum{op}=$opnum{$op}"
691                 unless $opnum == $opnum{$op};
692             delete $not_seen{$op};
693
694             my @bitdefs;
695             my $entry = $FLAGS{$op};
696             my $bit;
697             my $index;
698
699             for ($bit = 7; $bit >= 0; $bit--) {
700                 my $e = $entry->{$bit};
701                 next unless defined $e;
702
703                 my $ix;
704                 if (ref $e) {
705                     $ix = $bitfield_ix{$e};
706                     die "panic: \$bit =\= $e->{bitmax}"
707                         unless $bit == $e->{bitmax};
708
709                     push @bitdefs, ( ($ix << 5) | ($bit << 2) | 2 );
710                     $bit = $e->{bitmin};
711                 }
712                 else {
713                     $ix = $label_ix{$LABELS{$e}};
714                     die "panic: no label ix for '$e'" unless defined $ix;
715                     push @bitdefs, ( ($ix << 5) | ($bit << 2));
716                 }
717                 if ($ix > 2047) {
718                     die "Too many labels or bitfields (ix=$ix): "
719                     . "maybe the type of PL_op_private_bitdefs needs "
720                     . "expanding from U16 to U32???";
721                 }
722             }
723             if (@bitdefs) {
724                 $bitdefs[-1] |= 1; # stop bit
725                 $index = $bitdef_count;
726                 $bitdef_count += @bitdefs;
727                 $PL_op_private_bitdefs .= sprintf "    /* %-13s */ %s,\n",
728                         $op,
729                         join(', ', map(sprintf("0x%04x", $_), @bitdefs));
730             }
731             else {
732                 $index = -1;
733             }
734             $PL_op_private_bitdef_ix .= sprintf "    %4d, /* %s */\n", $index, $op;
735         }
736         if (%not_seen) {
737             die "panic: unprocessed ops: ". join(',', keys %not_seen);
738         }
739     }
740
741
742     # generate PL_op_private_valid
743
744     for my $op (@ops) {
745         my $last;
746         my @flags;
747         for my $bit (0..7) {
748             next unless exists $FLAGS{$op};
749             my $entry = $FLAGS{$op}{$bit};
750             next unless defined $entry;
751             if (ref $entry) {
752                 # skip later entries for the same bit field
753                 next if defined $last and $last == $entry;
754                 $last = $entry;
755                 push @flags,
756                     defined $entry->{mask_def}
757                         ? $entry->{mask_def}
758                         : $entry->{bitmask};
759             }
760             else {
761                 push @flags, $entry;
762             }
763         }
764
765         # all bets are off
766         @flags = '0xff' if $op eq 'null' or $op eq 'custom';
767
768         $PL_op_private_valid .= sprintf "    /* %-10s */ (%s),\n", uc($op),
769                                     @flags ? join('|', @flags): '0';
770     }
771
772     print $fh <<EOF;
773 START_EXTERN_C
774
775 #ifndef PERL_GLOBAL_STRUCT_INIT
776
777 #  ifndef DOINIT
778
779 /* data about the flags in op_private */
780
781 EXTCONST I16  PL_op_private_bitdef_ix[];
782 EXTCONST U16  PL_op_private_bitdefs[];
783 EXTCONST char PL_op_private_labels[];
784 EXTCONST I16  PL_op_private_bitfields[];
785 EXTCONST U8   PL_op_private_valid[];
786
787 #  else
788
789
790 /* PL_op_private_labels[]: the short descriptions of private flags.
791  * All labels are concatenated into a single char array
792  * (separated by \\0's) for compactness.
793  */
794
795 EXTCONST char PL_op_private_labels[] = {
796 $PL_op_private_labels
797 };
798
799
800
801 /* PL_op_private_bitfields[]: details about each bit field type.
802  * Each defintition consists of the following list of words:
803  *    bitmin
804  *    label (index into PL_op_private_labels[]; -1 if no label)
805  *    repeat for each enum entry (if any):
806  *       enum value
807  *       enum label (index into PL_op_private_labels[])
808  *    -1
809  */
810
811 EXTCONST I16 PL_op_private_bitfields[] = {
812 $PL_op_private_bitfields
813 };
814
815
816 /* PL_op_private_bitdef_ix[]: map an op number to a starting position
817  * in PL_op_private_bitdefs.  If -1, the op has no bits defined */
818
819 EXTCONST I16  PL_op_private_bitdef_ix[] = {
820 $PL_op_private_bitdef_ix
821 };
822
823
824
825 /* PL_op_private_bitdefs[]: given a starting position in this array (as
826  * supplied by PL_op_private_bitdef_ix[]), each word (until a stop bit is
827  * seen) defines the meaning of a particular op_private bit for a
828  * particular op. Each word consists of:
829  *  bit  0:     stop bit: this is the last bit def for the current op
830  *  bit  1:     bitfield: if set, this defines a bit field rather than a flag
831  *  bits 2..4:  unsigned number in the range 0..7 which is the bit number
832  *  bits 5..15: unsigned number in the range 0..2047 which is an index
833  *              into PL_op_private_labels[]    (for a flag), or
834  *              into PL_op_private_bitfields[] (for a bit field)
835  */
836
837 EXTCONST U16  PL_op_private_bitdefs[] = {
838 $PL_op_private_bitdefs
839 };
840
841
842 /* PL_op_private_valid: for each op, indexed by op_type, indicate which
843  * flags bits in op_private are legal */
844
845 EXTCONST U8 PL_op_private_valid[] = {
846 $PL_op_private_valid
847 };
848
849 #  endif /* !DOINIT */
850 #endif /* !PERL_GLOBAL_STRUCT_INIT */
851
852 END_EXTERN_C
853
854
855 EOF
856
857 }
858
859
860 # =================================================================
861
862
863 package main;
864
865 # read regen/op_private data
866 #
867 # This file contains Perl code that builds up some data structures
868 # which define what bits in op_private have what meanings for each op.
869 # It populates %LABELS, %DEFINES, %FLAGS, %BITFIELDS.
870
871 require 'regen/op_private';
872
873 #use Data::Dumper;
874 #print Dumper \%LABELS, \%DEFINES, \%FLAGS, \%BITFIELDS;
875
876
877 # Emit defines.
878
879 print $oc    "#ifndef PERL_GLOBAL_STRUCT_INIT\n\n";
880
881 {
882     my $last_cond = '';
883     my @unimplemented;
884
885     sub unimplemented {
886         if (@unimplemented) {
887             print $oc "#else\n";
888             foreach (@unimplemented) {
889                 print $oc "#define $_ Perl_unimplemented_op\n";
890             }
891             print $oc "#endif\n";
892             @unimplemented = ();
893         }
894
895     }
896
897     for (@ops) {
898         my ($impl, $cond) = @{$alias{$_} || ["Perl_pp_$_", '']};
899         my $op_func = "Perl_pp_$_";
900
901         if ($cond ne $last_cond) {
902             # A change in condition. (including to or from no condition)
903             unimplemented();
904             $last_cond = $cond;
905             if ($last_cond) {
906                 print $oc "$last_cond\n";
907             }
908         }
909         push @unimplemented, $op_func if $last_cond;
910         print $oc "#define $op_func $impl\n" if $impl ne $op_func;
911     }
912     # If the last op was conditional, we need to close it out:
913     unimplemented();
914 }
915
916 print $on "typedef enum opcode {\n";
917
918 my $i = 0;
919 for (@ops) {
920       print $on "\t", tab(3,"OP_\U$_"), " = ", $i++, ",\n";
921 }
922 print $on "\t", tab(3,"OP_max"), "\n";
923 print $on "} opcode;\n";
924 print $on "\n#define MAXO ", scalar @ops, "\n";
925 print $on "#define OP_FREED MAXO\n";
926
927 # Emit op names and descriptions.
928
929 print $oc <<'END';
930 START_EXTERN_C
931
932 #ifndef DOINIT
933 EXTCONST char* const PL_op_name[];
934 #else
935 EXTCONST char* const PL_op_name[] = {
936 END
937
938 for (@ops) {
939     print $oc qq(\t"$_",\n);
940 }
941
942 print $oc <<'END';
943         "freed",
944 };
945 #endif
946
947 #ifndef DOINIT
948 EXTCONST char* const PL_op_desc[];
949 #else
950 EXTCONST char* const PL_op_desc[] = {
951 END
952
953 for (@ops) {
954     my($safe_desc) = $desc{$_};
955
956     # Have to escape double quotes and escape characters.
957     $safe_desc =~ s/([\\"])/\\$1/g;
958
959     print $oc qq(\t"$safe_desc",\n);
960 }
961
962 print $oc <<'END';
963         "freed op",
964 };
965 #endif
966
967 END_EXTERN_C
968
969 #endif /* !PERL_GLOBAL_STRUCT_INIT */
970 END
971
972 # Emit ppcode switch array.
973
974 print $oc <<'END';
975
976 START_EXTERN_C
977
978 #ifdef PERL_GLOBAL_STRUCT_INIT
979 #  define PERL_PPADDR_INITED
980 static const Perl_ppaddr_t Gppaddr[]
981 #else
982 #  ifndef PERL_GLOBAL_STRUCT
983 #    define PERL_PPADDR_INITED
984 EXT Perl_ppaddr_t PL_ppaddr[] /* or perlvars.h */
985 #  endif
986 #endif /* PERL_GLOBAL_STRUCT */
987 #if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
988 #  define PERL_PPADDR_INITED
989 = {
990 END
991
992 for (@ops) {
993     my $op_func = "Perl_pp_$_";
994     my $name = $alias{$_};
995     if ($name && $name->[0] ne $op_func) {
996         print $oc "\t$op_func,\t/* implemented by $name->[0] */\n";
997     }
998     else {
999         print $oc "\t$op_func,\n";
1000     }
1001 }
1002
1003 print $oc <<'END';
1004 }
1005 #endif
1006 #ifdef PERL_PPADDR_INITED
1007 ;
1008 #endif
1009
1010 #ifdef PERL_GLOBAL_STRUCT_INIT
1011 #  define PERL_CHECK_INITED
1012 static const Perl_check_t Gcheck[]
1013 #else
1014 #  ifndef PERL_GLOBAL_STRUCT
1015 #    define PERL_CHECK_INITED
1016 EXT Perl_check_t PL_check[] /* or perlvars.h */
1017 #  endif
1018 #endif
1019 #if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
1020 #  define PERL_CHECK_INITED
1021 = {
1022 END
1023
1024 for (@ops) {
1025     print $oc "\t", tab(3, "Perl_$check{$_},"), "\t/* $_ */\n";
1026 }
1027
1028 print $oc <<'END';
1029 }
1030 #endif
1031 #ifdef PERL_CHECK_INITED
1032 ;
1033 #endif /* #ifdef PERL_CHECK_INITED */
1034
1035 #ifndef PERL_GLOBAL_STRUCT_INIT
1036
1037 #ifndef DOINIT
1038 EXTCONST U32 PL_opargs[];
1039 #else
1040 EXTCONST U32 PL_opargs[] = {
1041 END
1042
1043 # Emit allowed argument types.
1044
1045 my $ARGBITS = 32;
1046
1047 my %argnum = (
1048     'S',  1,            # scalar
1049     'L',  2,            # list
1050     'A',  3,            # array value
1051     'H',  4,            # hash value
1052     'C',  5,            # code value
1053     'F',  6,            # file value
1054     'R',  7,            # scalar reference
1055 );
1056
1057 my %opclass = (
1058     '0',  0,            # baseop
1059     '1',  1,            # unop
1060     '2',  2,            # binop
1061     '|',  3,            # logop
1062     '@',  4,            # listop
1063     '/',  5,            # pmop
1064     '$',  6,            # svop_or_padop
1065     '#',  7,            # padop
1066     '"',  8,            # pvop_or_svop
1067     '{',  9,            # loop
1068     ';',  10,           # cop
1069     '%',  11,           # baseop_or_unop
1070     '-',  12,           # filestatop
1071     '}',  13,           # loopexop
1072 );
1073
1074 my %opflags = (
1075     'm' =>   1,         # needs stack mark
1076     'f' =>   2,         # fold constants
1077     's' =>   4,         # always produces scalar
1078     't' =>   8,         # needs target scalar
1079     'T' =>   8 | 16,    # ... which may be lexical
1080     'i' =>   0,         # always produces integer (unused since e7311069)
1081     'I' =>  32,         # has corresponding int op
1082     'd' =>  64,         # danger, unknown side effects
1083     'u' => 128,         # defaults to $_
1084 );
1085
1086 my %OP_IS_SOCKET;       # /Fs/
1087 my %OP_IS_FILETEST;     # /F-/
1088 my %OP_IS_FT_ACCESS;    # /F-+/
1089 my %OP_IS_NUMCOMPARE;   # /S</
1090 my %OP_IS_DIRHOP;       # /Fd/
1091
1092 my $OCSHIFT = 8;
1093 my $OASHIFT = 12;
1094
1095 for my $op (@ops) {
1096     my $argsum = 0;
1097     my $flags = $flags{$op};
1098     for my $flag (keys %opflags) {
1099         if ($flags =~ s/$flag//) {
1100             die "Flag collision for '$op' ($flags{$op}, $flag)\n"
1101                 if $argsum & $opflags{$flag};
1102             $argsum |= $opflags{$flag};
1103         }
1104     }
1105     die qq[Opcode '$op' has no class indicator ($flags{$op} => $flags)\n]
1106         unless exists $opclass{$flags};
1107     $argsum |= $opclass{$flags} << $OCSHIFT;
1108     my $argshift = $OASHIFT;
1109     for my $arg (split(' ',$args{$op})) {
1110         if ($arg =~ s/^D//) {
1111             # handle 1st, just to put D 1st.
1112             $OP_IS_DIRHOP{$op}   = $opnum{$op};
1113         }
1114         if ($arg =~ /^F/) {
1115             # record opnums of these opnames
1116             $OP_IS_SOCKET{$op}   = $opnum{$op} if $arg =~ s/s//;
1117             $OP_IS_FILETEST{$op} = $opnum{$op} if $arg =~ s/-//;
1118             $OP_IS_FT_ACCESS{$op} = $opnum{$op} if $arg =~ s/\+//;
1119         }
1120         elsif ($arg =~ /^S</) {
1121             $OP_IS_NUMCOMPARE{$op} = $opnum{$op} if $arg =~ s/<//;
1122         }
1123         my $argnum = ($arg =~ s/\?//) ? 8 : 0;
1124         die "op = $op, arg = $arg\n"
1125             unless exists $argnum{$arg};
1126         $argnum += $argnum{$arg};
1127         die "Argument overflow for '$op'\n"
1128             if $argshift >= $ARGBITS ||
1129                $argnum > ((1 << ($ARGBITS - $argshift)) - 1);
1130         $argsum += $argnum << $argshift;
1131         $argshift += 4;
1132     }
1133     $argsum = sprintf("0x%08x", $argsum);
1134     print $oc "\t", tab(3, "$argsum,"), "/* $op */\n";
1135 }
1136
1137 print $oc <<'END';
1138 };
1139 #endif
1140
1141 #endif /* !PERL_GLOBAL_STRUCT_INIT */
1142
1143 END_EXTERN_C
1144 END
1145
1146 # Emit OP_IS_* macros
1147
1148 print $on <<'EO_OP_IS_COMMENT';
1149
1150 /* the OP_IS_* macros are optimized to a simple range check because
1151     all the member OPs are contiguous in regen/opcodes table.
1152     opcode.pl verifies the range contiguity, or generates an OR-equals
1153     expression */
1154 EO_OP_IS_COMMENT
1155
1156 gen_op_is_macro( \%OP_IS_SOCKET, 'OP_IS_SOCKET');
1157 gen_op_is_macro( \%OP_IS_FILETEST, 'OP_IS_FILETEST');
1158 gen_op_is_macro( \%OP_IS_FT_ACCESS, 'OP_IS_FILETEST_ACCESS');
1159 gen_op_is_macro( \%OP_IS_NUMCOMPARE, 'OP_IS_NUMCOMPARE');
1160 gen_op_is_macro( \%OP_IS_DIRHOP, 'OP_IS_DIRHOP');
1161
1162 sub gen_op_is_macro {
1163     my ($op_is, $macname) = @_;
1164     if (keys %$op_is) {
1165         
1166         # get opnames whose numbers are lowest and highest
1167         my ($first, @rest) = sort {
1168             $op_is->{$a} <=> $op_is->{$b}
1169         } keys %$op_is;
1170         
1171         my $last = pop @rest;   # @rest slurped, get its last
1172         die "Invalid range of ops: $first .. $last\n" unless $last;
1173
1174         print $on "\n#define $macname(op)       \\\n\t(";
1175
1176         # verify that op-ct matches 1st..last range (and fencepost)
1177         # (we know there are no dups)
1178         if ( $op_is->{$last} - $op_is->{$first} == scalar @rest + 1) {
1179             
1180             # contiguous ops -> optimized version
1181             print $on "(op) >= OP_" . uc($first)
1182                 . " && (op) <= OP_" . uc($last);
1183         }
1184         else {
1185             print $on join(" || \\\n\t ",
1186                            map { "(op) == OP_" . uc() } sort keys %$op_is);
1187         }
1188         print $on ")\n";
1189     }
1190 }
1191
1192 my $pp = open_new('pp_proto.h', '>',
1193                   { by => 'opcode.pl', from => 'its data' });
1194
1195 {
1196     my %funcs;
1197     for (@ops) {
1198         my $name = $alias{$_} ? $alias{$_}[0] : "Perl_pp_$_";
1199         ++$funcs{$name};
1200     }
1201     print $pp "PERL_CALLCONV OP *$_(pTHX);\n" foreach sort keys %funcs;
1202 }
1203
1204 print $oc "\n\n";
1205 OP_PRIVATE::print_defines($oc);
1206 OP_PRIVATE::print_PL_op_private_tables($oc);
1207
1208 OP_PRIVATE::print_B_Op_private($oprivpm);
1209
1210 foreach ($oc, $on, $pp, $oprivpm) {
1211     read_only_bottom_close_and_rename($_);
1212 }
1213
1214 # Some comments about 'T' opcode classifier:
1215
1216 # Safe to set if the ppcode uses:
1217 #       tryAMAGICbin, tryAMAGICun, SETn, SETi, SETu, PUSHn, PUSHTARG, SETTARG,
1218 #       SETs(TARG), XPUSHn, XPUSHu,
1219
1220 # Unsafe to set if the ppcode uses dTARG or [X]RETPUSH[YES|NO|UNDEF]
1221
1222 # lt and friends do SETs (including ncmp, but not scmp)
1223
1224 # Additional mode of failure: the opcode can modify TARG before it "used"
1225 # all the arguments (or may call an external function which does the same).
1226 # If the target coincides with one of the arguments ==> kaboom.
1227
1228 # pp.c  pos substr each not OK (RETPUSHUNDEF)
1229 #       substr vec also not OK due to LV to target (are they???)
1230 #       ref not OK (RETPUSHNO)
1231 #       trans not OK (dTARG; TARG = sv_newmortal();)
1232 #       ucfirst etc not OK: TMP arg processed inplace
1233 #       quotemeta not OK (unsafe when TARG == arg)
1234 #       each repeat not OK too due to list context
1235 #       pack split - unknown whether they are safe
1236 #       sprintf: is calling do_sprintf(TARG,...) which can act on TARG
1237 #         before other args are processed.
1238
1239 #       Suspicious wrt "additional mode of failure" (and only it):
1240 #       schop, chop, postinc/dec, bit_and etc, negate, complement.
1241
1242 #       Also suspicious: 4-arg substr, sprintf, uc/lc (POK_only), reverse, pack.
1243
1244 #       substr/vec: doing TAINT_off()???
1245
1246 # pp_hot.c
1247 #       readline - unknown whether it is safe
1248 #       match subst not OK (dTARG)
1249 #       grepwhile not OK (not always setting)
1250 #       join not OK (unsafe when TARG == arg)
1251
1252 #       Suspicious wrt "additional mode of failure": concat (dealt with
1253 #       in ck_sassign()), join (same).
1254
1255 # pp_ctl.c
1256 #       mapwhile flip caller not OK (not always setting)
1257
1258 # pp_sys.c
1259 #       backtick glob warn die not OK (not always setting)
1260 #       warn not OK (RETPUSHYES)
1261 #       open fileno getc sysread syswrite ioctl accept shutdown
1262 #        ftsize(etc) readlink telldir fork alarm getlogin not OK (RETPUSHUNDEF)
1263 #       umask select not OK (XPUSHs(&PL_sv_undef);)
1264 #       fileno getc sysread syswrite tell not OK (meth("FILENO" "GETC"))
1265 #       sselect shm* sem* msg* syscall - unknown whether they are safe
1266 #       gmtime not OK (list context)
1267
1268 #       Suspicious wrt "additional mode of failure": warn, die, select.