3 # Regenerate (overwriting only if changed):
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.
15 # Accepts the standard regen_lib -q and -v args.
17 # This script is normally invoked from regen.pl.
22 # Get function prototypes
23 require 'regen/regen_lib.pl';
26 my $oc = open_new('opcode.h', '>',
27 {by => 'regen/opcode.pl', from => 'its data',
28 file => 'opcode.h', style => '*',
29 copyright => [1993 .. 2007]});
31 my $on = open_new('opnames.h', '>',
32 { by => 'regen/opcode.pl', from => 'its data', style => '*',
33 file => 'opnames.h', copyright => [1999 .. 2008] });
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",
40 file => 'lib/B/Op_private.pm',
41 copyright => [2014 .. 2014] });
43 # Read 'opcodes' data.
46 my (@ops, %desc, %check, %ckname, %flags, %args, %opnum);
48 open OPS, 'regen/opcodes' or die $!;
54 my ($key, $desc, $check, $flags, $args) = split(/\t+/, $_, 5);
55 $args = '' unless defined $args;
57 warn qq[Description "$desc" duplicates $seen{$desc}\n]
58 if $seen{$desc} and $key !~ "transr|(?:intro|clone)cv|lvref";
59 die qq[Opcode "$key" duplicates $seen{$key}\n] if $seen{$key};
60 die qq[Opcode "freed" is reserved for the slab allocator\n]
62 $seen{$desc} = qq[description of opcode "$key"];
63 $seen{$key} = qq[opcode "$key"];
68 $check{$key} = $check;
70 $flags{$key} = $flags;
78 # Format is "this function" => "does these op names"
80 Perl_do_kv => [qw( keys values )],
81 Perl_unimplemented_op => [qw(padany custom)],
82 # All the ops with a body of { return NORMAL; }
83 Perl_pp_null => [qw(scalar regcmaybe lineseq scope)],
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
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
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_nbit_or => ['nbit_xor'],
126 Perl_pp_sbit_or => ['sbit_xor'],
127 Perl_pp_rv2av => ['rv2hv'],
128 Perl_pp_akeys => ['avalues'],
129 Perl_pp_trans => [qw(trans transr)],
130 Perl_pp_chop => [qw(chop chomp)],
131 Perl_pp_schop => [qw(schop schomp)],
132 Perl_pp_bind => {connect => '#ifdef HAS_SOCKET'},
133 Perl_pp_preinc => ['i_preinc', 'predec', 'i_predec'],
134 Perl_pp_postinc => ['i_postinc', 'postdec', 'i_postdec'],
135 Perl_pp_ehostent => [qw(enetent eprotoent eservent
136 spwent epwent sgrent egrent)],
137 Perl_pp_shostent => [qw(snetent sprotoent sservent)],
138 Perl_pp_aelemfast => ['aelemfast_lex'],
139 Perl_pp_grepstart => ['mapstart'],
142 while (my ($func, $names) = splice @raw_alias, 0, 2) {
143 if (ref $names eq 'ARRAY') {
145 $alias{$_} = [$func, ''];
148 while (my ($opname, $cond) = each %$names) {
149 $alias{$opname} = [$func, $cond];
154 foreach my $sock_func (qw(socket bind listen accept shutdown
155 ssockopt getpeername)) {
156 $alias{$sock_func} = ["Perl_pp_$sock_func", '#ifdef HAS_SOCKET'],
161 # =================================================================
163 # Functions for processing regen/op_private data.
165 # Put them in a separate package so that croak() does the right thing
172 # the vars holding the global state built up by all the calls to addbits()
175 # map OPpLVAL_INTRO => LVINTRO
179 # the numeric values of flags - what will get output as a #define
182 # %BITFIELDS: the various bit field types. The key is the concatenation of
183 # all the field values that make up a bit field hash; the values are bit
184 # field hash refs. This allows us to de-dup identical bit field defs
185 # across different ops, and thus make the output tables more compact (esp
186 # important for the C version)
189 # %FLAGS: the main data structure. Indexed by op name, then bit index:
191 # $FLAGS{rv2av}{2} = 'OPpSLICEWARNING';
192 # bit field (bits 5 and 6):
193 # $FLAGS{rv2av}{5} = $FLAGS{rv2av}{6} = { .... };
197 # do, with checking, $LABELS{$name} = $label
200 my ($name, $label) = @_;
201 if (exists $LABELS{$name} and $LABELS{$name} ne $label) {
202 croak "addbits(): label for flag '$name' redefined:\n"
203 . " was '$LABELS{$name}', now '$label'";
205 $LABELS{$name} = $label;
209 # do, with checking, $DEFINES{$name} = $val
212 my ($name, $val) = @_;
213 if (exists $DEFINES{$name} && $DEFINES{$name} != $val) {
214 croak "addbits(): value for flag '$name' redefined:\n"
215 . " was $DEFINES{$name}, now $val";
217 $DEFINES{$name} = $val;
221 # intended to be called from regen/op_private; see that file for details
226 croak "too few arguments for addbits()" unless @args >= 3;
227 my $op = shift @args;
228 croak "invalid op name: '$op'" unless exists $opnum{$op};
231 my $bits = shift @args;
232 if ($bits =~ /^[0-7]$/) {
234 croak "addbits(): too few arguments for single bit flag"
236 my $flag_name = shift @args;
237 my $flag_label = shift @args;
238 add_label($flag_name, $flag_label);
239 croak "addbits(): bit $bits of $op already specified"
240 if defined $FLAGS{$op}{$bits};
241 $FLAGS{$op}{$bits} = $flag_name;
242 add_define($flag_name, (1 << $bits));
244 elsif ($bits =~ /^([0-7])\.\.([0-7])$/) {
246 my ($bitmin, $bitmax) = ($1,$2);
248 croak "addbits(): min bit > max bit in bit range '$bits'"
249 unless $bitmin <= $bitmax;
250 croak "addbits(): bit field argument missing"
253 my $arg_hash = shift @args;
254 croak "addbits(): arg to $bits must be a hash ref"
255 unless defined $arg_hash and ref($arg_hash) =~ /HASH/;
258 @valid_keys{qw(baseshift_def bitcount_def mask_def label enum)} = ();
259 for (keys %$arg_hash) {
260 croak "addbits(): unrecognised bifield key: '$_'"
261 unless exists $valid_keys{$_};
265 $bitmask += (1 << $_) for $bitmin..$bitmax;
269 if (defined $arg_hash->{enum}) {
270 my $enum = $arg_hash->{enum};
271 croak "addbits(): arg to enum must be an array ref"
272 unless defined $enum and ref($enum) =~ /ARRAY/;
273 croak "addbits(): enum list must be in triplets"
274 unless @$enum % 3 == 0;
276 my $max_id = (1 << ($bitmax - $bitmin + 1)) - 1;
280 my $enum_ix = shift @e;
281 my $enum_name = shift @e;
282 my $enum_label = shift @e;
283 croak "addbits(): enum index must be a number: '$enum_ix'"
284 unless $enum_ix =~ /^\d+$/;
285 croak "addbits(): enum index too big: '$enum_ix'"
286 unless $enum_ix <= $max_id;
287 add_label($enum_name, $enum_label);
288 add_define($enum_name, $enum_ix << $bitmin);
289 $enum_id .= "($enum_ix:$enum_name:$enum_label)";
293 # id is a fingerprint of all the content of the bit field hash
294 my $id = join ':', map defined() ? $_ : "-undef-",
297 $arg_hash->{mask_def},
298 $arg_hash->{baseshift_def},
299 $arg_hash->{bitcount_def},
302 unless (defined $BITFIELDS{$id}) {
304 if (defined $arg_hash->{mask_def}) {
305 add_define($arg_hash->{mask_def}, $bitmask);
308 if (defined $arg_hash->{baseshift_def}) {
309 add_define($arg_hash->{baseshift_def}, $bitmin);
312 if (defined $arg_hash->{bitcount_def}) {
313 add_define($arg_hash->{bitcount_def}, $bitmax-$bitmin+1);
319 for (qw(baseshift_def bitcount_def mask_def label)) {
320 $copy->{$_} = $arg_hash->{$_} if defined $arg_hash->{$_};
322 if (defined $arg_hash->{enum}) {
323 $copy->{enum} = [ @{$arg_hash->{enum}} ];
326 # and add some extra fields
328 $copy->{bitmask} = $bitmask;
329 $copy->{bitmin} = $bitmin;
330 $copy->{bitmax} = $bitmax;
332 $BITFIELDS{$id} = $copy;
335 for my $bit ($bitmin..$bitmax) {
336 croak "addbits(): bit $bit of $op already specified"
337 if defined $FLAGS{$op}{$bit};
338 $FLAGS{$op}{$bit} = $BITFIELDS{$id};
342 croak "addbits(): invalid bit specifier '$bits'";
348 # intended to be called from regen/op_private; see that file for details
350 sub ::ops_with_flag {
352 return grep $flags{$_} =~ /\Q$flag/, sort keys %flags;
356 # intended to be called from regen/op_private; see that file for details
358 sub ::ops_with_check {
360 return grep $check{$_} eq $c, sort keys %check;
364 # intended to be called from regen/op_private; see that file for details
367 my ($i, $arg_type) = @_;
369 for my $op (sort keys %args) {
370 my @args = split(' ',$args{$op});
371 push @ops, $op if defined $args[$i] and $args[$i] eq $arg_type;
377 # output '#define OPpLVAL_INTRO 0x80' etc
382 for (sort { $DEFINES{$a} <=> $DEFINES{$b} || $a cmp $b } keys %DEFINES) {
383 printf $fh "#define %-23s 0x%02x\n", $_, $DEFINES{$_};
388 # Generate the content of B::Op_private
390 sub print_B_Op_private {
393 my $header = <<'EOF';
396 @B::Op_private - OP op_private flag definitions
402 @ # flag details for bit 7 of OP_AELEM's op_private:
403 @ my $name = $B::Op_private::bits{aelem}{7}; # OPpLVAL_INTRO
404 @ my $value = $B::Op_private::defines{$name}; # 128
405 @ my $label = $B::Op_private::labels{$name}; # LVINTRO
407 @ # the bit field at bits 5..6 of OP_AELEM's op_private:
408 @ my $bf = $B::Op_private::bits{aelem}{6};
409 @ my $mask = $bf->{bitmask}; # etc
413 @This module provides four global hashes:
415 @ %B::Op_private::bits
416 @ %B::Op_private::defines
417 @ %B::Op_private::labels
418 @ %B::Op_private::ops_using
420 @which contain information about the per-op meanings of the bits in the
425 @This is indexed by op name and then bit number (0..7). For single bit flags,
426 @it returns the name of the define (if any) for that bit:
428 @ $B::Op_private::bits{aelem}{7} eq 'OPpLVAL_INTRO';
430 @For bit fields, it returns a hash ref containing details about the field.
431 @The same reference will be returned for all bit positions that make
432 @up the bit field; so for example these both return the same hash ref:
434 @ $bitfield = $B::Op_private::bits{aelem}{5};
435 @ $bitfield = $B::Op_private::bits{aelem}{6};
437 @The general format of this hash ref is
440 @ # The bit range and mask; these are always present.
445 @ # (The remaining keys are optional)
447 @ # The names of any defines that were requested:
448 @ mask_def => 'OPpFOO_MASK',
449 @ baseshift_def => 'OPpFOO_SHIFT',
450 @ bitcount_def => 'OPpFOO_BITS',
452 @ # If present, Concise etc will display the value with a 'FOO='
453 @ # prefix. If it equals '-', then Concise will treat the bit
454 @ # field as raw bits and not try to interpret it.
457 @ # If present, specifies the names of some defines and the
458 @ # display labels that are used to assign meaning to particu-
459 @ # lar integer values within the bit field; e.g. 3 is dis-
472 @This gives the value of every C<OPp> define, e.g.
474 @ $B::Op_private::defines{OPpLVAL_INTRO} == 128;
478 @This gives the short display label for each define, as used by C<B::Concise>
479 @and C<perl -Dx>, e.g.
481 @ $B::Op_private::labels{OPpLVAL_INTRO} eq 'LVINTRO';
483 @If the label equals '-', then Concise will treat the bit as a raw bit and
484 @not try to display it symbolically.
486 @=head2 C<%ops_using>
488 @For each define, this gives a reference to an array of op names that use
491 @ @ops_using_lvintro = @{ $B::Op_private::ops_using{OPp_LVAL_INTRO} };
495 package B::Op_private;
500 # remove podcheck.t-defeating leading char
501 $header =~ s/^\@//gm;
503 my $v = (::perl_version())[3];
504 print $fh qq{\nour \$VERSION = "$v";\n\n};
508 # for each flag/bit combination, find the ops which use it
510 for my $op (sort keys %FLAGS) {
511 my $entry = $FLAGS{$op};
513 my $e = $entry->{$bit};
514 next unless defined $e;
515 next if ref $e; # bit field, not flag
516 push @{$combos{$e}{$bit}}, $op;
517 push @{$ops_using{$e}}, $op;
521 # dump flags used by multiple ops
522 for my $flag (sort keys %combos) {
523 for my $bit (sort keys %{$combos{$flag}}) {
524 my $ops = $combos{$flag}{$bit};
525 next unless @$ops > 1;
527 print $fh "\$bits{\$_}{$bit} = '$flag' for qw(@o);\n";
531 # dump bit field definitions
536 # stringified-ref to ref mapping
537 $bitfields{$_} = $_ for values %BITFIELDS;
539 my $s = "\nmy \@bf = (\n";
540 for my $bitfield_key (sort keys %BITFIELDS) {
541 my $bitfield = $BITFIELDS{$bitfield_key};
543 $bitfield_ix{$bitfield} = $ix;
546 for (qw(label mask_def baseshift_def bitcount_def)) {
547 next unless defined $bitfield->{$_};
548 $s .= sprintf " %-9s => '%s',\n",
551 for (qw(bitmin bitmax bitmask)) {
552 croak "panic" unless defined $bitfield->{$_};
553 $s .= sprintf " %-9s => %d,\n",
556 if (defined $bitfield->{enum}) {
557 $s .= " enum => [\n";
558 my @enum = @{$bitfield->{enum}};
561 my $name = shift @enum;
562 my $label = shift @enum;
563 $s .= sprintf " %d, %-10s, %s,\n",
564 $i, "'$name'", "'$label'";
575 # dump bitfields and remaining labels
577 for my $op (sort keys %FLAGS) {
580 my $entry = $FLAGS{$op};
583 for ($bit = 7; $bit >= 0; $bit--) {
584 next unless defined $entry->{$bit};
585 my $e = $entry->{$bit};
587 my $ix = $bitfield_ix{$e};
588 for (reverse $e->{bitmin}..$e->{bitmax}) {
590 push @vals, "\$bf[$ix]";
595 next if @{$combos{$e}{$bit}} > 1; # already output
602 $s = '@{' if @indices > 1;
604 $s .= '}' if @indices > 1;
605 $s .= '{' . join(',', @indices) . '} = ';
606 $s .= '(' if @indices > 1;
607 $s .= join ', ', @vals;
608 $s .= ')' if @indices > 1;
613 # populate %defines and %labels
615 print $fh "\n\nour %defines = (\n";
616 printf $fh " %-23s => %3d,\n", $_ , $DEFINES{$_} for sort keys %DEFINES;
617 print $fh ");\n\nour %labels = (\n";
618 printf $fh " %-23s => '%s',\n", $_ , $LABELS{$_} for sort keys %LABELS;
622 print $fh "\n\nour %ops_using = (\n";
623 # Save memory by using the same array wherever possible.
626 for my $flag (sort keys %ops_using) {
627 my $op_list = $ops_using{$flag} = "@{$ops_using{$flag}}";
628 if (!exists $flag_by_op_list{$op_list}) {
629 $flag_by_op_list{$op_list} = $flag;
630 printf $fh " %-23s => %s,\n", $flag , "[qw($op_list)]"
633 $pending .= "\$ops_using{$flag} = "
634 . "\$ops_using{$flag_by_op_list{$op_list}};\n";
637 print $fh ");\n\n$pending";
643 # output the contents of the assorted PL_op_private_*[] tables
645 sub print_PL_op_private_tables {
648 my $PL_op_private_labels = '';
649 my $PL_op_private_valid = '';
650 my $PL_op_private_bitdef_ix = '';
651 my $PL_op_private_bitdefs = '';
652 my $PL_op_private_bitfields = '';
657 # generate $PL_op_private_labels
661 $labs{$_} = 1 for values %LABELS; # de-duplicate labels
662 # add in bit field labels
663 for (values %BITFIELDS) {
664 next unless defined $_->{label};
665 $labs{$_->{label}} = 1;
669 for my $lab (sort keys %labs) {
670 $label_ix{$lab} = length $labels;
672 $PL_op_private_labels .=
674 . join(',', map("'$_'", split //, $lab))
680 # generate PL_op_private_bitfields
684 # stringified-ref to ref mapping
685 $bitfields{$_} = $_ for values %BITFIELDS;
688 for my $bitfield_key (sort keys %BITFIELDS) {
689 my $bf = $BITFIELDS{$bitfield_key};
690 $bitfield_ix{$bf} = $ix;
693 push @b, $bf->{bitmin},
694 defined $bf->{label} ? $label_ix{$bf->{label}} : -1;
695 my $enum = $bf->{enum};
700 my $name = shift @enum;
701 my $label = shift @enum;
702 push @b, $i, $label_ix{$label};
705 push @b, -1; # terminate enum list
707 $PL_op_private_bitfields .= " " . join(', ', @b) .",\n";
713 # generate PL_op_private_bitdefs, PL_op_private_bitdef_ix
716 my $bitdef_count = 0;
718 my %not_seen = %FLAGS;
723 for my $op (sort { $opnum{$a} <=> $opnum{$b} } keys %opnum) {
725 die "panic: opnum misorder: opnum=$opnum opnum{op}=$opnum{$op}"
726 unless $opnum == $opnum{$op};
727 delete $not_seen{$op};
730 my $entry = $FLAGS{$op};
734 for ($bit = 7; $bit >= 0; $bit--) {
735 my $e = $entry->{$bit};
736 next unless defined $e;
740 $ix = $bitfield_ix{$e};
741 die "panic: \$bit =\= $e->{bitmax}"
742 unless $bit == $e->{bitmax};
744 push @bitdefs, ( ($ix << 5) | ($bit << 2) | 2 );
748 $ix = $label_ix{$LABELS{$e}};
749 die "panic: no label ix for '$e'" unless defined $ix;
750 push @bitdefs, ( ($ix << 5) | ($bit << 2));
753 die "Too many labels or bitfields (ix=$ix): "
754 . "maybe the type of PL_op_private_bitdefs needs "
755 . "expanding from U16 to U32???";
759 $bitdefs[-1] |= 1; # stop bit
760 my $key = join(', ', map(sprintf("0x%04x", $_), @bitdefs));
761 if (!$seen_bitdefs{$key}) {
762 $index = $bitdef_count;
763 $bitdef_count += @bitdefs;
765 $seen_bitdefs{$key} = [$index, $key];
768 $index = $seen_bitdefs{$key}[0];
770 push @{$seen_bitdefs{$key}}, $op;
775 $PL_op_private_bitdef_ix .= sprintf " %4d, /* %s */\n", $index, $op;
778 die "panic: unprocessed ops: ". join(',', keys %not_seen);
780 for (@seen_bitdefs) {
782 $PL_op_private_bitdefs .= " $$_[1], /* @$_[2..$#$_] */\n";
787 # generate PL_op_private_valid
793 next unless exists $FLAGS{$op};
794 my $entry = $FLAGS{$op}{$bit};
795 next unless defined $entry;
797 # skip later entries for the same bit field
798 next if defined $last and $last == $entry;
801 defined $entry->{mask_def}
811 @flags = '0xff' if $op eq 'null' or $op eq 'custom';
813 $PL_op_private_valid .= sprintf " /* %-10s */ (%s),\n", uc($op),
814 @flags ? join('|', @flags): '0';
820 #ifndef PERL_GLOBAL_STRUCT_INIT
824 /* data about the flags in op_private */
826 EXTCONST I16 PL_op_private_bitdef_ix[];
827 EXTCONST U16 PL_op_private_bitdefs[];
828 EXTCONST char PL_op_private_labels[];
829 EXTCONST I16 PL_op_private_bitfields[];
830 EXTCONST U8 PL_op_private_valid[];
835 /* PL_op_private_labels[]: the short descriptions of private flags.
836 * All labels are concatenated into a single char array
837 * (separated by \\0's) for compactness.
840 EXTCONST char PL_op_private_labels[] = {
841 $PL_op_private_labels
846 /* PL_op_private_bitfields[]: details about each bit field type.
847 * Each definition consists of the following list of words:
849 * label (index into PL_op_private_labels[]; -1 if no label)
850 * repeat for each enum entry (if any):
852 * enum label (index into PL_op_private_labels[])
856 EXTCONST I16 PL_op_private_bitfields[] = {
857 $PL_op_private_bitfields
861 /* PL_op_private_bitdef_ix[]: map an op number to a starting position
862 * in PL_op_private_bitdefs. If -1, the op has no bits defined */
864 EXTCONST I16 PL_op_private_bitdef_ix[] = {
865 $PL_op_private_bitdef_ix
870 /* PL_op_private_bitdefs[]: given a starting position in this array (as
871 * supplied by PL_op_private_bitdef_ix[]), each word (until a stop bit is
872 * seen) defines the meaning of a particular op_private bit for a
873 * particular op. Each word consists of:
874 * bit 0: stop bit: this is the last bit def for the current op
875 * bit 1: bitfield: if set, this defines a bit field rather than a flag
876 * bits 2..4: unsigned number in the range 0..7 which is the bit number
877 * bits 5..15: unsigned number in the range 0..2047 which is an index
878 * into PL_op_private_labels[] (for a flag), or
879 * into PL_op_private_bitfields[] (for a bit field)
882 EXTCONST U16 PL_op_private_bitdefs[] = {
883 $PL_op_private_bitdefs
887 /* PL_op_private_valid: for each op, indexed by op_type, indicate which
888 * flags bits in op_private are legal */
890 EXTCONST U8 PL_op_private_valid[] = {
894 # endif /* !DOINIT */
895 #endif /* !PERL_GLOBAL_STRUCT_INIT */
905 # =================================================================
910 # read regen/op_private data
912 # This file contains Perl code that builds up some data structures
913 # which define what bits in op_private have what meanings for each op.
914 # It populates %LABELS, %DEFINES, %FLAGS, %BITFIELDS.
916 require 'regen/op_private';
919 #print Dumper \%LABELS, \%DEFINES, \%FLAGS, \%BITFIELDS;
924 print $oc "#ifndef PERL_GLOBAL_STRUCT_INIT\n\n";
931 if (@unimplemented) {
933 foreach (@unimplemented) {
934 print $oc "#define $_ Perl_unimplemented_op\n";
936 print $oc "#endif\n";
943 my ($impl, $cond) = @{$alias{$_} || ["Perl_pp_$_", '']};
944 my $op_func = "Perl_pp_$_";
946 if ($cond ne $last_cond) {
947 # A change in condition. (including to or from no condition)
951 print $oc "$last_cond\n";
954 push @unimplemented, $op_func if $last_cond;
955 print $oc "#define $op_func $impl\n" if $impl ne $op_func;
957 # If the last op was conditional, we need to close it out:
961 print $on "typedef enum opcode {\n";
965 print $on "\t", tab(3,"OP_\U$_"), " = ", $i++, ",\n";
967 print $on "\t", tab(3,"OP_max"), "\n";
968 print $on "} opcode;\n";
969 print $on "\n#define MAXO ", scalar @ops, "\n";
970 print $on "#define OP_FREED MAXO\n";
972 # Emit op names and descriptions.
978 EXTCONST char* const PL_op_name[];
980 EXTCONST char* const PL_op_name[] = {
984 print $oc qq(\t"$_",\n);
993 EXTCONST char* const PL_op_desc[];
995 EXTCONST char* const PL_op_desc[] = {
999 my($safe_desc) = $desc{$_};
1001 # Have to escape double quotes and escape characters.
1002 $safe_desc =~ s/([\\"])/\\$1/g;
1004 print $oc qq(\t"$safe_desc",\n);
1014 #endif /* !PERL_GLOBAL_STRUCT_INIT */
1017 # Emit ppcode switch array.
1023 #ifdef PERL_GLOBAL_STRUCT_INIT
1024 # define PERL_PPADDR_INITED
1025 static const Perl_ppaddr_t Gppaddr[]
1027 # ifndef PERL_GLOBAL_STRUCT
1028 # define PERL_PPADDR_INITED
1029 EXT Perl_ppaddr_t PL_ppaddr[] /* or perlvars.h */
1031 #endif /* PERL_GLOBAL_STRUCT */
1032 #if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
1033 # define PERL_PPADDR_INITED
1038 my $op_func = "Perl_pp_$_";
1039 my $name = $alias{$_};
1040 if ($name && $name->[0] ne $op_func) {
1041 print $oc "\t$op_func,\t/* implemented by $name->[0] */\n";
1044 print $oc "\t$op_func,\n";
1051 #ifdef PERL_PPADDR_INITED
1055 #ifdef PERL_GLOBAL_STRUCT_INIT
1056 # define PERL_CHECK_INITED
1057 static const Perl_check_t Gcheck[]
1059 # ifndef PERL_GLOBAL_STRUCT
1060 # define PERL_CHECK_INITED
1061 EXT Perl_check_t PL_check[] /* or perlvars.h */
1064 #if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
1065 # define PERL_CHECK_INITED
1070 print $oc "\t", tab(3, "Perl_$check{$_},"), "\t/* $_ */\n";
1076 #ifdef PERL_CHECK_INITED
1078 #endif /* #ifdef PERL_CHECK_INITED */
1080 #ifndef PERL_GLOBAL_STRUCT_INIT
1083 EXTCONST U32 PL_opargs[];
1085 EXTCONST U32 PL_opargs[] = {
1088 # Emit allowed argument types.
1095 'A', 3, # array value
1096 'H', 4, # hash value
1097 'C', 5, # code value
1098 'F', 6, # file value
1099 'R', 7, # scalar reference
1109 '$', 6, # svop_or_padop
1111 '"', 8, # pvop_or_svop
1114 '%', 11, # baseop_or_unop
1115 '-', 12, # filestatop
1122 'm' => 1, # needs stack mark
1123 'f' => 2, # fold constants
1124 's' => 4, # always produces scalar
1125 't' => 8, # needs target scalar
1126 'T' => 8 | 16, # ... which may be lexical
1127 'i' => 0, # always produces integer (unused since e7311069)
1128 'I' => 32, # has corresponding int op
1129 'd' => 64, # danger, make temp copy in list assignment
1130 'u' => 128, # defaults to $_
1133 my %OP_IS_SOCKET; # /Fs/
1134 my %OP_IS_FILETEST; # /F-/
1135 my %OP_IS_FT_ACCESS; # /F-+/
1136 my %OP_IS_NUMCOMPARE; # /S</
1137 my %OP_IS_DIRHOP; # /Fd/
1138 my %OP_IS_INFIX_BIT; # /S\|/
1145 my $flags = $flags{$op};
1146 for my $flag (keys %opflags) {
1147 if ($flags =~ s/$flag//) {
1148 die "Flag collision for '$op' ($flags{$op}, $flag)\n"
1149 if $argsum & $opflags{$flag};
1150 $argsum |= $opflags{$flag};
1153 die qq[Opcode '$op' has no class indicator ($flags{$op} => $flags)\n]
1154 unless exists $opclass{$flags};
1155 $argsum |= $opclass{$flags} << $OCSHIFT;
1156 my $argshift = $OASHIFT;
1157 for my $arg (split(' ',$args{$op})) {
1158 if ($arg =~ s/^D//) {
1159 # handle 1st, just to put D 1st.
1160 $OP_IS_DIRHOP{$op} = $opnum{$op};
1163 # record opnums of these opnames
1164 $OP_IS_SOCKET{$op} = $opnum{$op} if $arg =~ s/s//;
1165 $OP_IS_FILETEST{$op} = $opnum{$op} if $arg =~ s/-//;
1166 $OP_IS_FT_ACCESS{$op} = $opnum{$op} if $arg =~ s/\+//;
1168 elsif ($arg =~ /^S./) {
1169 $OP_IS_NUMCOMPARE{$op} = $opnum{$op} if $arg =~ s/<//;
1170 $OP_IS_INFIX_BIT {$op} = $opnum{$op} if $arg =~ s/\|//;
1172 my $argnum = ($arg =~ s/\?//) ? 8 : 0;
1173 die "op = $op, arg = $arg\n"
1174 unless exists $argnum{$arg};
1175 $argnum += $argnum{$arg};
1176 die "Argument overflow for '$op'\n"
1177 if $argshift >= $ARGBITS ||
1178 $argnum > ((1 << ($ARGBITS - $argshift)) - 1);
1179 $argsum += $argnum << $argshift;
1182 $argsum = sprintf("0x%08x", $argsum);
1183 print $oc "\t", tab(3, "$argsum,"), "/* $op */\n";
1190 #endif /* !PERL_GLOBAL_STRUCT_INIT */
1195 # Emit OP_IS_* macros
1197 print $on <<'EO_OP_IS_COMMENT';
1199 /* the OP_IS_* macros are optimized to a simple range check because
1200 all the member OPs are contiguous in regen/opcodes table.
1201 opcode.pl verifies the range contiguity, or generates an OR-equals
1205 gen_op_is_macro( \%OP_IS_SOCKET, 'OP_IS_SOCKET');
1206 gen_op_is_macro( \%OP_IS_FILETEST, 'OP_IS_FILETEST');
1207 gen_op_is_macro( \%OP_IS_FT_ACCESS, 'OP_IS_FILETEST_ACCESS');
1208 gen_op_is_macro( \%OP_IS_NUMCOMPARE, 'OP_IS_NUMCOMPARE');
1209 gen_op_is_macro( \%OP_IS_DIRHOP, 'OP_IS_DIRHOP');
1210 gen_op_is_macro( \%OP_IS_INFIX_BIT, 'OP_IS_INFIX_BIT');
1212 sub gen_op_is_macro {
1213 my ($op_is, $macname) = @_;
1216 # get opnames whose numbers are lowest and highest
1217 my ($first, @rest) = sort {
1218 $op_is->{$a} <=> $op_is->{$b}
1221 my $last = pop @rest; # @rest slurped, get its last
1222 die "Invalid range of ops: $first .. $last\n" unless $last;
1224 print $on "\n#define $macname(op) \\\n\t(";
1226 # verify that op-ct matches 1st..last range (and fencepost)
1227 # (we know there are no dups)
1228 if ( $op_is->{$last} - $op_is->{$first} == scalar @rest + 1) {
1230 # contiguous ops -> optimized version
1231 print $on "(op) >= OP_" . uc($first)
1232 . " && (op) <= OP_" . uc($last);
1235 print $on join(" || \\\n\t ",
1236 map { "(op) == OP_" . uc() } sort keys %$op_is);
1242 my $pp = open_new('pp_proto.h', '>',
1243 { by => 'opcode.pl', from => 'its data' });
1248 my $name = $alias{$_} ? $alias{$_}[0] : "Perl_pp_$_";
1251 print $pp "PERL_CALLCONV OP *$_(pTHX);\n" foreach sort keys %funcs;
1255 OP_PRIVATE::print_defines($oc);
1256 OP_PRIVATE::print_PL_op_private_tables($oc);
1258 OP_PRIVATE::print_B_Op_private($oprivpm);
1260 foreach ($oc, $on, $pp, $oprivpm) {
1261 read_only_bottom_close_and_rename($_);