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 mapstart 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_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'],
140 while (my ($func, $names) = splice @raw_alias, 0, 2) {
141 if (ref $names eq 'ARRAY') {
143 $alias{$_} = [$func, ''];
146 while (my ($opname, $cond) = each %$names) {
147 $alias{$opname} = [$func, $cond];
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'],
159 # =================================================================
161 # Functions for processing regen/op_private data.
163 # Put them in a separate package so that croak() does the right thing
170 # the vars holding the global state built up by all the calls to addbits()
173 # map OPpLVAL_INTRO => LVINTRO
177 # the numeric values of flags - what will get output as a #define
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)
187 # %FLAGS: the main data structure. Indexed by op name, then bit index:
189 # $FLAGS{rv2av}{2} = 'OPpSLICEWARNING';
190 # bit field (bits 5 and 6):
191 # $FLAGS{rv2av}{5} = $FLAGS{rv2av}{6} = { .... };
195 # do, with checking, $LABELS{$name} = $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'";
203 $LABELS{$name} = $label;
207 # do, with checking, $DEFINES{$name} = $val
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";
215 $DEFINES{$name} = $val;
219 # intended to be called from regen/op_private; see that file for details
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};
229 my $bits = shift @args;
230 if ($bits =~ /^[0-7]$/) {
232 croak "addbits(): too few arguments for single bit flag"
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));
242 elsif ($bits =~ /^([0-7])\.\.([0-7])$/) {
244 my ($bitmin, $bitmax) = ($1,$2);
246 croak "addbits(): min bit > max bit in bit range '$bits'"
247 unless $bitmin <= $bitmax;
248 croak "addbits(): bit field argument missing"
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/;
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{$_};
263 $bitmask += (1 << $_) for $bitmin..$bitmax;
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;
274 my $max_id = (1 << ($bitmax - $bitmin + 1)) - 1;
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)";
291 # id is a fingerprint of all the content of the bit field hash
292 my $id = join ':', map defined() ? $_ : "-undef-",
295 $arg_hash->{mask_def},
296 $arg_hash->{baseshift_def},
297 $arg_hash->{bitcount_def},
300 unless (defined $BITFIELDS{$id}) {
302 if (defined $arg_hash->{mask_def}) {
303 add_define($arg_hash->{mask_def}, $bitmask);
306 if (defined $arg_hash->{baseshift_def}) {
307 add_define($arg_hash->{baseshift_def}, $bitmin);
310 if (defined $arg_hash->{bitcount_def}) {
311 add_define($arg_hash->{bitcount_def}, $bitmax-$bitmin+1);
317 for (qw(baseshift_def bitcount_def mask_def label)) {
318 $copy->{$_} = $arg_hash->{$_} if defined $arg_hash->{$_};
320 if (defined $arg_hash->{enum}) {
321 $copy->{enum} = [ @{$arg_hash->{enum}} ];
324 # and add some extra fields
326 $copy->{bitmask} = $bitmask;
327 $copy->{bitmin} = $bitmin;
328 $copy->{bitmax} = $bitmax;
330 $BITFIELDS{$id} = $copy;
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};
340 croak "addbits(): invalid bit specifier '$bits'";
346 # intended to be called from regen/op_private; see that file for details
348 sub ::ops_with_flag {
350 return grep $flags{$_} =~ /\Q$flag/, sort keys %flags;
354 # intended to be called from regen/op_private; see that file for details
356 sub ::ops_with_check {
358 return grep $check{$_} eq $c, sort keys %check;
362 # intended to be called from regen/op_private; see that file for details
365 my ($i, $arg_type) = @_;
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;
375 # output '#define OPpLVAL_INTRO 0x80' etc
380 for (sort { $DEFINES{$a} <=> $DEFINES{$b} || $a cmp $b } keys %DEFINES) {
381 printf $fh "#define %-23s 0x%02x\n", $_, $DEFINES{$_};
386 # Generate the content of B::Op_private
388 sub print_B_Op_private {
391 my $header = <<'EOF';
394 @B::Op_private - OP op_private flag definitions
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
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
411 @This module provides three global hashes:
413 @ %B::Op_private::bits
414 @ %B::Op_private::defines
415 @ %B::Op_private::labels
417 @which contain information about the per-op meanings of the bits in the
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:
425 @ $B::Op_private::bits{aelem}{7} eq 'OPpLVAL_INTRO';
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:
431 @ $bitfield = $B::Op_private::bits{aelem}{5};
432 @ $bitfield = $B::Op_private::bits{aelem}{6};
434 @The general format of this hash ref is
437 @ # The bit range and mask; these are always present.
442 @ # (The remaining keys are optional)
444 @ # The names of any defines that were requested:
445 @ mask_def => 'OPpFOO_MASK',
446 @ baseshift_def => 'OPpFOO_SHIFT',
447 @ bitcount_def => 'OPpFOO_BITS',
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.
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-
469 @This gives the value of every C<OPp> define, e.g.
471 @ $B::Op_private::defines{OPpLVAL_INTRO} == 128;
475 @This gives the short display label for each define, as used by C<B::Concise>
476 @and C<perl -Dx>, e.g.
478 @ $B::Op_private::labels{OPpLVAL_INTRO} eq 'LVINTRO';
480 @If the label equals '-', then Concise will treat the bit as a raw bit and
481 @not try to display it symbolically.
485 package B::Op_private;
490 # remove podcheck.t-defeating leading char
491 $header =~ s/^\@//gm;
493 my $v = (::perl_version())[3];
494 print $fh qq{\nour \$VERSION = "$v";\n\n};
496 # for each flag/bit combination, find the ops which use it
498 for my $op (sort keys %FLAGS) {
499 my $entry = $FLAGS{$op};
501 my $e = $entry->{$bit};
502 next unless defined $e;
503 next if ref $e; # bit field, not flag
504 push @{$combos{$e}{$bit}}, $op;
508 # dump flags used by multiple ops
509 for my $flag (sort keys %combos) {
510 for my $bit (sort keys %{$combos{$flag}}) {
511 my $ops = $combos{$flag}{$bit};
512 next unless @$ops > 1;
514 print $fh "\$bits{\$_}{$bit} = '$flag' for qw(@o);\n";
518 # dump bit field definitions
523 # stringified-ref to ref mapping
524 $bitfields{$_} = $_ for values %BITFIELDS;
526 my $s = "\nmy \@bf = (\n";
527 for my $bitfield_key (sort keys %BITFIELDS) {
528 my $bitfield = $BITFIELDS{$bitfield_key};
530 $bitfield_ix{$bitfield} = $ix;
533 for (qw(label mask_def baseshift_def bitcount_def)) {
534 next unless defined $bitfield->{$_};
535 $s .= sprintf " %-9s => '%s',\n",
538 for (qw(bitmin bitmax bitmask)) {
539 croak "panic" unless defined $bitfield->{$_};
540 $s .= sprintf " %-9s => %d,\n",
543 if (defined $bitfield->{enum}) {
544 $s .= " enum => [\n";
545 my @enum = @{$bitfield->{enum}};
548 my $name = shift @enum;
549 my $label = shift @enum;
550 $s .= sprintf " %d, %-10s, %s,\n",
551 $i, "'$name'", "'$label'";
562 # dump bitfields and remaining labels
564 for my $op (sort keys %FLAGS) {
567 my $entry = $FLAGS{$op};
570 for ($bit = 7; $bit >= 0; $bit--) {
571 next unless defined $entry->{$bit};
572 my $e = $entry->{$bit};
574 my $ix = $bitfield_ix{$e};
575 for (reverse $e->{bitmin}..$e->{bitmax}) {
577 push @vals, "\$bf[$ix]";
582 next if @{$combos{$e}{$bit}} > 1; # already output
589 $s = '@{' if @indices > 1;
591 $s .= '}' if @indices > 1;
592 $s .= '{' . join(',', @indices) . '} = ';
593 $s .= '(' if @indices > 1;
594 $s .= join ', ', @vals;
595 $s .= ')' if @indices > 1;
600 # populate %defines and %labels
602 print $fh "\n\nour %defines = (\n";
603 printf $fh " %-23s => %3d,\n", $_ , $DEFINES{$_} for sort keys %DEFINES;
604 print $fh ");\n\nour %labels = (\n";
605 printf $fh " %-23s => '%s',\n", $_ , $LABELS{$_} for sort keys %LABELS;
612 # output the contents of the assorted PL_op_private_*[] tables
614 sub print_PL_op_private_tables {
617 my $PL_op_private_labels = '';
618 my $PL_op_private_valid = '';
619 my $PL_op_private_bitdef_ix = '';
620 my $PL_op_private_bitdefs = '';
621 my $PL_op_private_bitfields = '';
626 # generate $PL_op_private_labels
630 $labs{$_} = 1 for values %LABELS; # de-duplicate labels
631 # add in bit field labels
632 for (values %BITFIELDS) {
633 next unless defined $_->{label};
634 $labs{$_->{label}} = 1;
638 for my $lab (sort keys %labs) {
639 $label_ix{$lab} = length $labels;
641 $PL_op_private_labels .=
643 . join(',', map("'$_'", split //, $lab))
649 # generate PL_op_private_bitfields
653 # stringified-ref to ref mapping
654 $bitfields{$_} = $_ for values %BITFIELDS;
657 for my $bitfield_key (sort keys %BITFIELDS) {
658 my $bf = $BITFIELDS{$bitfield_key};
659 $bitfield_ix{$bf} = $ix;
662 push @b, $bf->{bitmin},
663 defined $bf->{label} ? $label_ix{$bf->{label}} : -1;
664 my $enum = $bf->{enum};
669 my $name = shift @enum;
670 my $label = shift @enum;
671 push @b, $i, $label_ix{$label};
674 push @b, -1; # terminate enum list
676 $PL_op_private_bitfields .= " " . join(', ', @b) .",\n";
682 # generate PL_op_private_bitdefs, PL_op_private_bitdef_ix
685 my $bitdef_count = 0;
687 my %not_seen = %FLAGS;
690 for my $op (sort { $opnum{$a} <=> $opnum{$b} } keys %opnum) {
692 die "panic: opnum misorder: opnum=$opnum opnum{op}=$opnum{$op}"
693 unless $opnum == $opnum{$op};
694 delete $not_seen{$op};
697 my $entry = $FLAGS{$op};
701 for ($bit = 7; $bit >= 0; $bit--) {
702 my $e = $entry->{$bit};
703 next unless defined $e;
707 $ix = $bitfield_ix{$e};
708 die "panic: \$bit =\= $e->{bitmax}"
709 unless $bit == $e->{bitmax};
711 push @bitdefs, ( ($ix << 5) | ($bit << 2) | 2 );
715 $ix = $label_ix{$LABELS{$e}};
716 die "panic: no label ix for '$e'" unless defined $ix;
717 push @bitdefs, ( ($ix << 5) | ($bit << 2));
720 die "Too many labels or bitfields (ix=$ix): "
721 . "maybe the type of PL_op_private_bitdefs needs "
722 . "expanding from U16 to U32???";
726 $bitdefs[-1] |= 1; # stop bit
727 $index = $bitdef_count;
728 $bitdef_count += @bitdefs;
729 $PL_op_private_bitdefs .= sprintf " /* %-13s */ %s,\n",
731 join(', ', map(sprintf("0x%04x", $_), @bitdefs));
736 $PL_op_private_bitdef_ix .= sprintf " %4d, /* %s */\n", $index, $op;
739 die "panic: unprocessed ops: ". join(',', keys %not_seen);
744 # generate PL_op_private_valid
750 next unless exists $FLAGS{$op};
751 my $entry = $FLAGS{$op}{$bit};
752 next unless defined $entry;
754 # skip later entries for the same bit field
755 next if defined $last and $last == $entry;
758 defined $entry->{mask_def}
768 @flags = '0xff' if $op eq 'null' or $op eq 'custom';
770 $PL_op_private_valid .= sprintf " /* %-10s */ (%s),\n", uc($op),
771 @flags ? join('|', @flags): '0';
777 #ifndef PERL_GLOBAL_STRUCT_INIT
781 /* data about the flags in op_private */
783 EXTCONST I16 PL_op_private_bitdef_ix[];
784 EXTCONST U16 PL_op_private_bitdefs[];
785 EXTCONST char PL_op_private_labels[];
786 EXTCONST I16 PL_op_private_bitfields[];
787 EXTCONST U8 PL_op_private_valid[];
792 /* PL_op_private_labels[]: the short descriptions of private flags.
793 * All labels are concatenated into a single char array
794 * (separated by \\0's) for compactness.
797 EXTCONST char PL_op_private_labels[] = {
798 $PL_op_private_labels
803 /* PL_op_private_bitfields[]: details about each bit field type.
804 * Each defintition consists of the following list of words:
806 * label (index into PL_op_private_labels[]; -1 if no label)
807 * repeat for each enum entry (if any):
809 * enum label (index into PL_op_private_labels[])
813 EXTCONST I16 PL_op_private_bitfields[] = {
814 $PL_op_private_bitfields
818 /* PL_op_private_bitdef_ix[]: map an op number to a starting position
819 * in PL_op_private_bitdefs. If -1, the op has no bits defined */
821 EXTCONST I16 PL_op_private_bitdef_ix[] = {
822 $PL_op_private_bitdef_ix
827 /* PL_op_private_bitdefs[]: given a starting position in this array (as
828 * supplied by PL_op_private_bitdef_ix[]), each word (until a stop bit is
829 * seen) defines the meaning of a particular op_private bit for a
830 * particular op. Each word consists of:
831 * bit 0: stop bit: this is the last bit def for the current op
832 * bit 1: bitfield: if set, this defines a bit field rather than a flag
833 * bits 2..4: unsigned number in the range 0..7 which is the bit number
834 * bits 5..15: unsigned number in the range 0..2047 which is an index
835 * into PL_op_private_labels[] (for a flag), or
836 * into PL_op_private_bitfields[] (for a bit field)
839 EXTCONST U16 PL_op_private_bitdefs[] = {
840 $PL_op_private_bitdefs
844 /* PL_op_private_valid: for each op, indexed by op_type, indicate which
845 * flags bits in op_private are legal */
847 EXTCONST U8 PL_op_private_valid[] = {
851 # endif /* !DOINIT */
852 #endif /* !PERL_GLOBAL_STRUCT_INIT */
862 # =================================================================
867 # read regen/op_private data
869 # This file contains Perl code that builds up some data structures
870 # which define what bits in op_private have what meanings for each op.
871 # It populates %LABELS, %DEFINES, %FLAGS, %BITFIELDS.
873 require 'regen/op_private';
876 #print Dumper \%LABELS, \%DEFINES, \%FLAGS, \%BITFIELDS;
881 print $oc "#ifndef PERL_GLOBAL_STRUCT_INIT\n\n";
888 if (@unimplemented) {
890 foreach (@unimplemented) {
891 print $oc "#define $_ Perl_unimplemented_op\n";
893 print $oc "#endif\n";
900 my ($impl, $cond) = @{$alias{$_} || ["Perl_pp_$_", '']};
901 my $op_func = "Perl_pp_$_";
903 if ($cond ne $last_cond) {
904 # A change in condition. (including to or from no condition)
908 print $oc "$last_cond\n";
911 push @unimplemented, $op_func if $last_cond;
912 print $oc "#define $op_func $impl\n" if $impl ne $op_func;
914 # If the last op was conditional, we need to close it out:
918 print $on "typedef enum opcode {\n";
922 print $on "\t", tab(3,"OP_\U$_"), " = ", $i++, ",\n";
924 print $on "\t", tab(3,"OP_max"), "\n";
925 print $on "} opcode;\n";
926 print $on "\n#define MAXO ", scalar @ops, "\n";
927 print $on "#define OP_FREED MAXO\n";
929 # Emit op names and descriptions.
935 EXTCONST char* const PL_op_name[];
937 EXTCONST char* const PL_op_name[] = {
941 print $oc qq(\t"$_",\n);
950 EXTCONST char* const PL_op_desc[];
952 EXTCONST char* const PL_op_desc[] = {
956 my($safe_desc) = $desc{$_};
958 # Have to escape double quotes and escape characters.
959 $safe_desc =~ s/([\\"])/\\$1/g;
961 print $oc qq(\t"$safe_desc",\n);
971 #endif /* !PERL_GLOBAL_STRUCT_INIT */
974 # Emit ppcode switch array.
980 #ifdef PERL_GLOBAL_STRUCT_INIT
981 # define PERL_PPADDR_INITED
982 static const Perl_ppaddr_t Gppaddr[]
984 # ifndef PERL_GLOBAL_STRUCT
985 # define PERL_PPADDR_INITED
986 EXT Perl_ppaddr_t PL_ppaddr[] /* or perlvars.h */
988 #endif /* PERL_GLOBAL_STRUCT */
989 #if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
990 # define PERL_PPADDR_INITED
995 my $op_func = "Perl_pp_$_";
996 my $name = $alias{$_};
997 if ($name && $name->[0] ne $op_func) {
998 print $oc "\t$op_func,\t/* implemented by $name->[0] */\n";
1001 print $oc "\t$op_func,\n";
1008 #ifdef PERL_PPADDR_INITED
1012 #ifdef PERL_GLOBAL_STRUCT_INIT
1013 # define PERL_CHECK_INITED
1014 static const Perl_check_t Gcheck[]
1016 # ifndef PERL_GLOBAL_STRUCT
1017 # define PERL_CHECK_INITED
1018 EXT Perl_check_t PL_check[] /* or perlvars.h */
1021 #if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
1022 # define PERL_CHECK_INITED
1027 print $oc "\t", tab(3, "Perl_$check{$_},"), "\t/* $_ */\n";
1033 #ifdef PERL_CHECK_INITED
1035 #endif /* #ifdef PERL_CHECK_INITED */
1037 #ifndef PERL_GLOBAL_STRUCT_INIT
1040 EXTCONST U32 PL_opargs[];
1042 EXTCONST U32 PL_opargs[] = {
1045 # Emit allowed argument types.
1052 'A', 3, # array value
1053 'H', 4, # hash value
1054 'C', 5, # code value
1055 'F', 6, # file value
1056 'R', 7, # scalar reference
1066 '$', 6, # svop_or_padop
1068 '"', 8, # pvop_or_svop
1071 '%', 11, # baseop_or_unop
1072 '-', 12, # filestatop
1078 'm' => 1, # needs stack mark
1079 'f' => 2, # fold constants
1080 's' => 4, # always produces scalar
1081 't' => 8, # needs target scalar
1082 'T' => 8 | 16, # ... which may be lexical
1083 'i' => 0, # always produces integer (unused since e7311069)
1084 'I' => 32, # has corresponding int op
1085 'd' => 64, # danger, make temp copy in list assignment
1086 'u' => 128, # defaults to $_
1089 my %OP_IS_SOCKET; # /Fs/
1090 my %OP_IS_FILETEST; # /F-/
1091 my %OP_IS_FT_ACCESS; # /F-+/
1092 my %OP_IS_NUMCOMPARE; # /S</
1093 my %OP_IS_DIRHOP; # /Fd/
1100 my $flags = $flags{$op};
1101 for my $flag (keys %opflags) {
1102 if ($flags =~ s/$flag//) {
1103 die "Flag collision for '$op' ($flags{$op}, $flag)\n"
1104 if $argsum & $opflags{$flag};
1105 $argsum |= $opflags{$flag};
1108 die qq[Opcode '$op' has no class indicator ($flags{$op} => $flags)\n]
1109 unless exists $opclass{$flags};
1110 $argsum |= $opclass{$flags} << $OCSHIFT;
1111 my $argshift = $OASHIFT;
1112 for my $arg (split(' ',$args{$op})) {
1113 if ($arg =~ s/^D//) {
1114 # handle 1st, just to put D 1st.
1115 $OP_IS_DIRHOP{$op} = $opnum{$op};
1118 # record opnums of these opnames
1119 $OP_IS_SOCKET{$op} = $opnum{$op} if $arg =~ s/s//;
1120 $OP_IS_FILETEST{$op} = $opnum{$op} if $arg =~ s/-//;
1121 $OP_IS_FT_ACCESS{$op} = $opnum{$op} if $arg =~ s/\+//;
1123 elsif ($arg =~ /^S</) {
1124 $OP_IS_NUMCOMPARE{$op} = $opnum{$op} if $arg =~ s/<//;
1126 my $argnum = ($arg =~ s/\?//) ? 8 : 0;
1127 die "op = $op, arg = $arg\n"
1128 unless exists $argnum{$arg};
1129 $argnum += $argnum{$arg};
1130 die "Argument overflow for '$op'\n"
1131 if $argshift >= $ARGBITS ||
1132 $argnum > ((1 << ($ARGBITS - $argshift)) - 1);
1133 $argsum += $argnum << $argshift;
1136 $argsum = sprintf("0x%08x", $argsum);
1137 print $oc "\t", tab(3, "$argsum,"), "/* $op */\n";
1144 #endif /* !PERL_GLOBAL_STRUCT_INIT */
1149 # Emit OP_IS_* macros
1151 print $on <<'EO_OP_IS_COMMENT';
1153 /* the OP_IS_* macros are optimized to a simple range check because
1154 all the member OPs are contiguous in regen/opcodes table.
1155 opcode.pl verifies the range contiguity, or generates an OR-equals
1159 gen_op_is_macro( \%OP_IS_SOCKET, 'OP_IS_SOCKET');
1160 gen_op_is_macro( \%OP_IS_FILETEST, 'OP_IS_FILETEST');
1161 gen_op_is_macro( \%OP_IS_FT_ACCESS, 'OP_IS_FILETEST_ACCESS');
1162 gen_op_is_macro( \%OP_IS_NUMCOMPARE, 'OP_IS_NUMCOMPARE');
1163 gen_op_is_macro( \%OP_IS_DIRHOP, 'OP_IS_DIRHOP');
1165 sub gen_op_is_macro {
1166 my ($op_is, $macname) = @_;
1169 # get opnames whose numbers are lowest and highest
1170 my ($first, @rest) = sort {
1171 $op_is->{$a} <=> $op_is->{$b}
1174 my $last = pop @rest; # @rest slurped, get its last
1175 die "Invalid range of ops: $first .. $last\n" unless $last;
1177 print $on "\n#define $macname(op) \\\n\t(";
1179 # verify that op-ct matches 1st..last range (and fencepost)
1180 # (we know there are no dups)
1181 if ( $op_is->{$last} - $op_is->{$first} == scalar @rest + 1) {
1183 # contiguous ops -> optimized version
1184 print $on "(op) >= OP_" . uc($first)
1185 . " && (op) <= OP_" . uc($last);
1188 print $on join(" || \\\n\t ",
1189 map { "(op) == OP_" . uc() } sort keys %$op_is);
1195 my $pp = open_new('pp_proto.h', '>',
1196 { by => 'opcode.pl', from => 'its data' });
1201 my $name = $alias{$_} ? $alias{$_}[0] : "Perl_pp_$_";
1204 print $pp "PERL_CALLCONV OP *$_(pTHX);\n" foreach sort keys %funcs;
1208 OP_PRIVATE::print_defines($oc);
1209 OP_PRIVATE::print_PL_op_private_tables($oc);
1211 OP_PRIVATE::print_B_Op_private($oprivpm);
1213 foreach ($oc, $on, $pp, $oprivpm) {
1214 read_only_bottom_close_and_rename($_);