This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
move OPpTARGET_MY comments from opcode.pl
[perl5.git] / regen / opcode.pl
CommitLineData
d6480c9d 1#!/usr/bin/perl -w
6294c161
DM
2#
3# Regenerate (overwriting only if changed):
4#
5# opcode.h
6# opnames.h
897d3989 7# pp_proto.h
f3574cc6 8# lib/B/Op_private.pm
6294c161 9#
f3574cc6
DM
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.
6294c161
DM
14#
15# Accepts the standard regen_lib -q and -v args.
16#
17# This script is normally invoked from regen.pl.
18
d6480c9d
NC
19use strict;
20
36bb303b
NC
21BEGIN {
22 # Get function prototypes
af001346 23 require 'regen/regen_lib.pl';
36bb303b 24}
79072805 25
cc49830d
NC
26my $oc = open_new('opcode.h', '>',
27 {by => 'regen/opcode.pl', from => 'its data',
28 file => 'opcode.h', style => '*',
29 copyright => [1993 .. 2007]});
30
31my $on = open_new('opnames.h', '>',
32 { by => 'regen/opcode.pl', from => 'its data', style => '*',
33 file => 'opnames.h', copyright => [1999 .. 2008] });
79072805 34
f3574cc6
DM
35my $oprivpm = open_new('lib/B/Op_private.pm', '>',
36 { by => 'regen/opcode.pl',
00fdc80f
FC
37 from => "data in\nregen/op_private "
38 ."and pod embedded in regen/opcode.pl",
f3574cc6
DM
39 style => '#',
40 file => 'lib/B/Op_private.pm',
41 copyright => [2014 .. 2014] });
42
43# Read 'opcodes' data.
79072805 44
d6480c9d 45my %seen;
e71197e2 46my (@ops, %desc, %check, %ckname, %flags, %args, %opnum);
d6480c9d 47
f8a58b02
NC
48open OPS, 'regen/opcodes' or die $!;
49
50while (<OPS>) {
79072805
LW
51 chop;
52 next unless $_;
53 next if /^#/;
d6480c9d
NC
54 my ($key, $desc, $check, $flags, $args) = split(/\t+/, $_, 5);
55 $args = '' unless defined $args;
c07a80fd 56
6342d5c5 57 warn qq[Description "$desc" duplicates $seen{$desc}\n]
13f89586 58 if $seen{$desc} and $key !~ "transr|(?:intro|clone)cv";
c07a80fd 59 die qq[Opcode "$key" duplicates $seen{$key}\n] if $seen{$key};
6e64f32b
FC
60 die qq[Opcode "freed" is reserved for the slab allocator\n]
61 if $key eq 'freed';
c07a80fd
PP
62 $seen{$desc} = qq[description of opcode "$key"];
63 $seen{$key} = qq[opcode "$key"];
64
79072805 65 push(@ops, $key);
e71197e2 66 $opnum{$key} = $#ops;
c07a80fd 67 $desc{$key} = $desc;
79072805
LW
68 $check{$key} = $check;
69 $ckname{$check}++;
70 $flags{$key} = $flags;
71 $args{$key} = $args;
72}
73
1d5774de
NC
74# Set up aliases
75
76my %alias;
77
78# Format is "this function" => "does these op names"
79my @raw_alias = (
6faeeb49 80 Perl_do_kv => [qw( keys values )],
605fa6bf 81 Perl_unimplemented_op => [qw(padany mapstart custom)],
0b612f93
NC
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'],
7627e6d0 88 Perl_pp_sysread => {read => '', recv => '#ifdef HAS_SOCKET'},
0b612f93
NC
89 Perl_pp_sysseek => ['seek'],
90 Perl_pp_ioctl => ['fcntl'],
7627e6d0
NC
91 Perl_pp_ssockopt => {gsockopt => '#ifdef HAS_SOCKET'},
92 Perl_pp_getpeername => {getsockname => '#ifdef HAS_SOCKET'},
0b612f93 93 Perl_pp_stat => ['lstat'],
f1cb2d48 94 Perl_pp_ftrowned => [qw(fteowned ftzero ftsock ftchr ftblk
17ad201a
NC
95 ftfile ftdir ftpipe ftsuid ftsgid
96 ftsvtx)],
0b612f93
NC
97 Perl_pp_fttext => ['ftbinary'],
98 Perl_pp_gmtime => ['localtime'],
99 Perl_pp_semget => [qw(shmget msgget)],
100 Perl_pp_semctl => [qw(shmctl msgctl)],
0b612f93
NC
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)],
957b0e1d 107 Perl_pp_ftis => [qw(ftsize ftmtime ftatime ftctime)],
605b9385 108 Perl_pp_chown => [qw(unlink chmod utime kill)],
ce6987d0 109 Perl_pp_link => ['symlink'],
af9e49b4
NC
110 Perl_pp_ftrread => [qw(ftrwrite ftrexec fteread ftewrite
111 fteexec)],
ca563b4e 112 Perl_pp_shmwrite => [qw(shmread msgsnd msgrcv semop)],
7627e6d0 113 Perl_pp_syswrite => {send => '#ifdef HAS_SOCKET'},
c960fc3b 114 Perl_pp_defined => [qw(dor dorassign)],
62726f23
SP
115 Perl_pp_and => ['andassign'],
116 Perl_pp_or => ['orassign'],
12e9c124 117 Perl_pp_ucfirst => ['lcfirst'],
afd9910b 118 Perl_pp_sle => [qw(slt sgt sge)],
0d863452 119 Perl_pp_print => ['say'],
2723d216 120 Perl_pp_index => ['rindex'],
daa2adfd 121 Perl_pp_oct => ['hex'],
789b4bc9 122 Perl_pp_shift => ['pop'],
71302fe3 123 Perl_pp_sin => [qw(cos exp log sqrt)],
3658c1f1 124 Perl_pp_bit_or => ['bit_xor'],
17ab7946 125 Perl_pp_rv2av => ['rv2hv'],
878d132a 126 Perl_pp_akeys => ['avalues'],
cba5a3b0 127 Perl_pp_rkeys => [qw(rvalues reach)],
7627e6d0
NC
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'},
17058fe0 132 Perl_pp_preinc => ['i_preinc', 'predec', 'i_predec'],
c22c99bc 133 Perl_pp_postinc => ['i_postinc', 'postdec', 'i_postdec'],
720d5dbf
NC
134 Perl_pp_ehostent => [qw(enetent eprotoent eservent
135 spwent epwent sgrent egrent)],
396166e1 136 Perl_pp_shostent => [qw(snetent sprotoent sservent)],
93bad3fd 137 Perl_pp_aelemfast => ['aelemfast_lex'],
605b9385 138 );
1d5774de
NC
139
140while (my ($func, $names) = splice @raw_alias, 0, 2) {
7627e6d0
NC
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 }
916e4025 149 }
1d5774de
NC
150}
151
7627e6d0
NC
152foreach 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
f3574cc6
DM
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
165package OP_PRIVATE;
166
167use 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
174my %LABELS;
175
176
177# the numeric values of flags - what will get output as a #define
178my %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)
185my %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} = { .... };
192my %FLAGS;
193
194
195# do, with checking, $LABELS{$name} = $label
196
197sub 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
209sub 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
221sub ::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
348sub ::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
356sub ::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
364sub ::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
377sub 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
388sub 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='
cdb679b6
FC
450@ # prefix. If it equals '-', then Concise will treat the bit
451@ # field as raw bits and not try to interpret it.
f3574cc6
DM
452@ label => 'FOO',
453@
2febb45a
FC
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'.
f3574cc6
DM
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
485package B::Op_private;
486
487our %bits;
488
489EOF
490 # remove podcheck.t-defeating leading char
491 $header =~ s/^\@//gm;
492 print $fh $header;
99275276
DM
493 my $v = (::perl_version())[3];
494 print $fh qq{\nour \$VERSION = "$v";\n\n};
f3574cc6
DM
495
496 # for each flag/bit combination, find the ops which use it
497 my %combos;
498 for my $op (sort keys %FLAGS) {
499 my $entry = $FLAGS{$op};
500 for my $bit (0..7) {
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;
505 }
506 }
507
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;
513 my @o = sort @$ops;
514 print $fh "\$bits{\$_}{$bit} = '$flag' for qw(@o);\n";
515 }
516 }
517
518 # dump bit field definitions
519
520 my %bitfield_ix;
521 {
522 my %bitfields;
523 # stringified-ref to ref mapping
524 $bitfields{$_} = $_ for values %BITFIELDS;
525 my $ix = -1;
526 my $s = "\nmy \@bf = (\n";
527 for my $bitfield_key (sort keys %BITFIELDS) {
528 my $bitfield = $BITFIELDS{$bitfield_key};
529 $ix++;
530 $bitfield_ix{$bitfield} = $ix;
531
532 $s .= " {\n";
533 for (qw(label mask_def baseshift_def bitcount_def)) {
534 next unless defined $bitfield->{$_};
535 $s .= sprintf " %-9s => '%s',\n",
536 $_, $bitfield->{$_};
537 }
538 for (qw(bitmin bitmax bitmask)) {
539 croak "panic" unless defined $bitfield->{$_};
540 $s .= sprintf " %-9s => %d,\n",
541 $_, $bitfield->{$_};
542 }
543 if (defined $bitfield->{enum}) {
544 $s .= " enum => [\n";
545 my @enum = @{$bitfield->{enum}};
546 while (@enum) {
547 my $i = shift @enum;
548 my $name = shift @enum;
549 my $label = shift @enum;
550 $s .= sprintf " %d, %-10s, %s,\n",
551 $i, "'$name'", "'$label'";
552 }
553 $s .= " ],\n";
554 }
555 $s .= " },\n";
556
557 }
558 $s .= ");\n";
559 print $fh "$s\n";
560 }
561
562 # dump bitfields and remaining labels
563
564 for my $op (sort keys %FLAGS) {
565 my @indices;
566 my @vals;
567 my $entry = $FLAGS{$op};
568 my $bit;
569
570 for ($bit = 7; $bit >= 0; $bit--) {
571 next unless defined $entry->{$bit};
572 my $e = $entry->{$bit};
573 if (ref $e) {
574 my $ix = $bitfield_ix{$e};
575 for (reverse $e->{bitmin}..$e->{bitmax}) {
576 push @indices, $_;
577 push @vals, "\$bf[$ix]";
578 }
579 $bit = $e->{bitmin};
580 }
581 else {
582 next if @{$combos{$e}{$bit}} > 1; # already output
583 push @indices, $bit;
584 push @vals, "'$e'";
585 }
586 }
587 if (@indices) {
588 my $s = '';
589 $s = '@{' if @indices > 1;
590 $s .= "\$bits{$op}";
591 $s .= '}' if @indices > 1;
592 $s .= '{' . join(',', @indices) . '} = ';
593 $s .= '(' if @indices > 1;
594 $s .= join ', ', @vals;
595 $s .= ')' if @indices > 1;
596 print $fh "$s;\n";
597 }
598 }
599
600 # populate %defines and %labels
601
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;
606 print $fh ");\n";
607
608}
609
610
611
612# output the contents of the assorted PL_op_private_*[] tables
613
614sub print_PL_op_private_tables {
615 my $fh = shift;
616
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 = '';
622
623 my %label_ix;
624 my %bitfield_ix;
625
626 # generate $PL_op_private_labels
627
628 {
629 my %labs;
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;
635 }
636
637 my $labels = '';
638 for my $lab (sort keys %labs) {
639 $label_ix{$lab} = length $labels;
640 $labels .= "$lab\0";
641 $PL_op_private_labels .=
642 " "
643 . join(',', map("'$_'", split //, $lab))
644 . ",'\\0',\n";
645 }
646 }
647
648
649 # generate PL_op_private_bitfields
650
651 {
652 my %bitfields;
653 # stringified-ref to ref mapping
654 $bitfields{$_} = $_ for values %BITFIELDS;
655
656 my $ix = 0;
657 for my $bitfield_key (sort keys %BITFIELDS) {
658 my $bf = $BITFIELDS{$bitfield_key};
659 $bitfield_ix{$bf} = $ix;
660
661 my @b;
662 push @b, $bf->{bitmin},
663 defined $bf->{label} ? $label_ix{$bf->{label}} : -1;
664 my $enum = $bf->{enum};
665 if (defined $enum) {
666 my @enum = @$enum;
667 while (@enum) {
668 my $i = shift @enum;
669 my $name = shift @enum;
670 my $label = shift @enum;
671 push @b, $i, $label_ix{$label};
672 }
673 }
674 push @b, -1; # terminate enum list
675
676 $PL_op_private_bitfields .= " " . join(', ', @b) .",\n";
677 $ix += @b;
678 }
679 }
680
681
682 # generate PL_op_private_bitdefs, PL_op_private_bitdef_ix
683
684 {
685 my $bitdef_count = 0;
686
687 my %not_seen = %FLAGS;
688
689 my $opnum = -1;
690 for my $op (sort { $opnum{$a} <=> $opnum{$b} } keys %opnum) {
691 $opnum++;
692 die "panic: opnum misorder: opnum=$opnum opnum{op}=$opnum{$op}"
693 unless $opnum == $opnum{$op};
694 delete $not_seen{$op};
695
696 my @bitdefs;
697 my $entry = $FLAGS{$op};
698 my $bit;
699 my $index;
700
701 for ($bit = 7; $bit >= 0; $bit--) {
702 my $e = $entry->{$bit};
703 next unless defined $e;
704
705 my $ix;
706 if (ref $e) {
707 $ix = $bitfield_ix{$e};
708 die "panic: \$bit =\= $e->{bitmax}"
709 unless $bit == $e->{bitmax};
710
711 push @bitdefs, ( ($ix << 5) | ($bit << 2) | 2 );
712 $bit = $e->{bitmin};
713 }
714 else {
715 $ix = $label_ix{$LABELS{$e}};
716 die "panic: no label ix for '$e'" unless defined $ix;
717 push @bitdefs, ( ($ix << 5) | ($bit << 2));
718 }
719 if ($ix > 2047) {
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???";
723 }
724 }
725 if (@bitdefs) {
726 $bitdefs[-1] |= 1; # stop bit
727 $index = $bitdef_count;
728 $bitdef_count += @bitdefs;
729 $PL_op_private_bitdefs .= sprintf " /* %-13s */ %s,\n",
730 $op,
731 join(', ', map(sprintf("0x%04x", $_), @bitdefs));
732 }
733 else {
734 $index = -1;
735 }
736 $PL_op_private_bitdef_ix .= sprintf " %4d, /* %s */\n", $index, $op;
737 }
738 if (%not_seen) {
739 die "panic: unprocessed ops: ". join(',', keys %not_seen);
740 }
741 }
742
743
744 # generate PL_op_private_valid
745
746 for my $op (@ops) {
747 my $last;
748 my @flags;
749 for my $bit (0..7) {
750 next unless exists $FLAGS{$op};
751 my $entry = $FLAGS{$op}{$bit};
752 next unless defined $entry;
753 if (ref $entry) {
754 # skip later entries for the same bit field
755 next if defined $last and $last == $entry;
756 $last = $entry;
757 push @flags,
758 defined $entry->{mask_def}
759 ? $entry->{mask_def}
760 : $entry->{bitmask};
761 }
762 else {
763 push @flags, $entry;
764 }
765 }
766
767 # all bets are off
768 @flags = '0xff' if $op eq 'null' or $op eq 'custom';
769
770 $PL_op_private_valid .= sprintf " /* %-10s */ (%s),\n", uc($op),
771 @flags ? join('|', @flags): '0';
772 }
773
774 print $fh <<EOF;
775START_EXTERN_C
776
777#ifndef PERL_GLOBAL_STRUCT_INIT
778
779# ifndef DOINIT
780
781/* data about the flags in op_private */
782
783EXTCONST I16 PL_op_private_bitdef_ix[];
784EXTCONST U16 PL_op_private_bitdefs[];
785EXTCONST char PL_op_private_labels[];
786EXTCONST I16 PL_op_private_bitfields[];
787EXTCONST U8 PL_op_private_valid[];
788
789# else
790
791
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.
795 */
796
797EXTCONST char PL_op_private_labels[] = {
798$PL_op_private_labels
799};
800
801
802
803/* PL_op_private_bitfields[]: details about each bit field type.
804 * Each defintition consists of the following list of words:
805 * bitmin
806 * label (index into PL_op_private_labels[]; -1 if no label)
807 * repeat for each enum entry (if any):
808 * enum value
809 * enum label (index into PL_op_private_labels[])
810 * -1
811 */
812
813EXTCONST I16 PL_op_private_bitfields[] = {
814$PL_op_private_bitfields
815};
816
817
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 */
820
821EXTCONST I16 PL_op_private_bitdef_ix[] = {
822$PL_op_private_bitdef_ix
823};
824
825
826
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)
837 */
838
839EXTCONST U16 PL_op_private_bitdefs[] = {
840$PL_op_private_bitdefs
841};
842
843
844/* PL_op_private_valid: for each op, indexed by op_type, indicate which
845 * flags bits in op_private are legal */
846
847EXTCONST U8 PL_op_private_valid[] = {
848$PL_op_private_valid
849};
850
851# endif /* !DOINIT */
852#endif /* !PERL_GLOBAL_STRUCT_INIT */
853
854END_EXTERN_C
855
856
857EOF
858
859}
860
861
862# =================================================================
863
864
865package main;
866
867# read regen/op_private data
868#
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.
872
873require 'regen/op_private';
874
875#use Data::Dumper;
876#print Dumper \%LABELS, \%DEFINES, \%FLAGS, \%BITFIELDS;
877
878
79072805
LW
879# Emit defines.
880
cc49830d 881print $oc "#ifndef PERL_GLOBAL_STRUCT_INIT\n\n";
9561d06f 882
7627e6d0
NC
883{
884 my $last_cond = '';
885 my @unimplemented;
886
887 sub unimplemented {
888 if (@unimplemented) {
2d6469fe 889 print $oc "#else\n";
7627e6d0 890 foreach (@unimplemented) {
2d6469fe 891 print $oc "#define $_ Perl_unimplemented_op\n";
7627e6d0 892 }
2d6469fe 893 print $oc "#endif\n";
7627e6d0
NC
894 @unimplemented = ();
895 }
896
897 }
898
899 for (@ops) {
900 my ($impl, $cond) = @{$alias{$_} || ["Perl_pp_$_", '']};
901 my $op_func = "Perl_pp_$_";
902
903 if ($cond ne $last_cond) {
904 # A change in condition. (including to or from no condition)
905 unimplemented();
906 $last_cond = $cond;
907 if ($last_cond) {
2d6469fe 908 print $oc "$last_cond\n";
7627e6d0
NC
909 }
910 }
911 push @unimplemented, $op_func if $last_cond;
2d6469fe 912 print $oc "#define $op_func $impl\n" if $impl ne $op_func;
7627e6d0
NC
913 }
914 # If the last op was conditional, we need to close it out:
915 unimplemented();
9561d06f
NC
916}
917
cc49830d 918print $on "typedef enum opcode {\n";
abdd5c84 919
d6480c9d 920my $i = 0;
79072805 921for (@ops) {
2d6469fe 922 print $on "\t", tab(3,"OP_\U$_"), " = ", $i++, ",\n";
79072805 923}
2d6469fe 924print $on "\t", tab(3,"OP_max"), "\n";
424a4936
NC
925print $on "} opcode;\n";
926print $on "\n#define MAXO ", scalar @ops, "\n";
6e64f32b 927print $on "#define OP_FREED MAXO\n";
79072805 928
c07a80fd 929# Emit op names and descriptions.
79072805 930
2d6469fe 931print $oc <<'END';
73c4f7a1
GS
932START_EXTERN_C
933
79072805 934#ifndef DOINIT
27da23d5 935EXTCONST char* const PL_op_name[];
79072805 936#else
27da23d5 937EXTCONST char* const PL_op_name[] = {
79072805
LW
938END
939
940for (@ops) {
2d6469fe 941 print $oc qq(\t"$_",\n);
c07a80fd
PP
942}
943
2d6469fe 944print $oc <<'END';
a33a81d0 945 "freed",
c07a80fd
PP
946};
947#endif
948
c07a80fd 949#ifndef DOINIT
27da23d5 950EXTCONST char* const PL_op_desc[];
c07a80fd 951#else
27da23d5 952EXTCONST char* const PL_op_desc[] = {
c07a80fd
PP
953END
954
955for (@ops) {
42d38218
MS
956 my($safe_desc) = $desc{$_};
957
a567e93b 958 # Have to escape double quotes and escape characters.
b0c6325e 959 $safe_desc =~ s/([\\"])/\\$1/g;
42d38218 960
2d6469fe 961 print $oc qq(\t"$safe_desc",\n);
79072805
LW
962}
963
2d6469fe 964print $oc <<'END';
a33a81d0 965 "freed op",
79072805
LW
966};
967#endif
968
73c4f7a1
GS
969END_EXTERN_C
970
27da23d5 971#endif /* !PERL_GLOBAL_STRUCT_INIT */
22c35a8c 972END
79072805 973
79072805
LW
974# Emit ppcode switch array.
975
2d6469fe 976print $oc <<'END';
79072805 977
73c4f7a1
GS
978START_EXTERN_C
979
27da23d5 980#ifdef PERL_GLOBAL_STRUCT_INIT
97aff369 981# define PERL_PPADDR_INITED
27da23d5 982static const Perl_ppaddr_t Gppaddr[]
79072805 983#else
27da23d5 984# ifndef PERL_GLOBAL_STRUCT
97aff369 985# define PERL_PPADDR_INITED
27da23d5
JH
986EXT Perl_ppaddr_t PL_ppaddr[] /* or perlvars.h */
987# endif
988#endif /* PERL_GLOBAL_STRUCT */
989#if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
97aff369 990# define PERL_PPADDR_INITED
27da23d5 991= {
79072805
LW
992END
993
994for (@ops) {
7627e6d0
NC
995 my $op_func = "Perl_pp_$_";
996 my $name = $alias{$_};
997 if ($name && $name->[0] ne $op_func) {
2d6469fe 998 print $oc "\t$op_func,\t/* implemented by $name->[0] */\n";
6faeeb49
MB
999 }
1000 else {
2d6469fe 1001 print $oc "\t$op_func,\n";
6faeeb49 1002 }
79072805
LW
1003}
1004
2d6469fe 1005print $oc <<'END';
27da23d5 1006}
79072805 1007#endif
97aff369 1008#ifdef PERL_PPADDR_INITED
27da23d5 1009;
97aff369 1010#endif
79072805 1011
27da23d5 1012#ifdef PERL_GLOBAL_STRUCT_INIT
97aff369 1013# define PERL_CHECK_INITED
27da23d5 1014static const Perl_check_t Gcheck[]
79072805 1015#else
27da23d5 1016# ifndef PERL_GLOBAL_STRUCT
97aff369 1017# define PERL_CHECK_INITED
27da23d5
JH
1018EXT Perl_check_t PL_check[] /* or perlvars.h */
1019# endif
1020#endif
1021#if (defined(DOINIT) && !defined(PERL_GLOBAL_STRUCT)) || defined(PERL_GLOBAL_STRUCT_INIT)
97aff369 1022# define PERL_CHECK_INITED
27da23d5 1023= {
79072805
LW
1024END
1025
1026for (@ops) {
2d6469fe 1027 print $oc "\t", tab(3, "Perl_$check{$_},"), "\t/* $_ */\n";
79072805
LW
1028}
1029
2d6469fe 1030print $oc <<'END';
27da23d5 1031}
79072805 1032#endif
97aff369 1033#ifdef PERL_CHECK_INITED
27da23d5 1034;
97aff369 1035#endif /* #ifdef PERL_CHECK_INITED */
79072805 1036
27da23d5
JH
1037#ifndef PERL_GLOBAL_STRUCT_INIT
1038
79072805 1039#ifndef DOINIT
1ccb7c8d 1040EXTCONST U32 PL_opargs[];
79072805 1041#else
1ccb7c8d 1042EXTCONST U32 PL_opargs[] = {
79072805
LW
1043END
1044
2d6469fe
NC
1045# Emit allowed argument types.
1046
1047my $ARGBITS = 32;
1048
d6480c9d
NC
1049my %argnum = (
1050 'S', 1, # scalar
1051 'L', 2, # list
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
79072805
LW
1057);
1058
d6480c9d 1059my %opclass = (
db173bac
MB
1060 '0', 0, # baseop
1061 '1', 1, # unop
1062 '2', 2, # binop
1063 '|', 3, # logop
1a67a97c
SM
1064 '@', 4, # listop
1065 '/', 5, # pmop
350de78d 1066 '$', 6, # svop_or_padop
7934575e 1067 '#', 7, # padop
1a67a97c
SM
1068 '"', 8, # pvop_or_svop
1069 '{', 9, # loop
1070 ';', 10, # cop
1071 '%', 11, # baseop_or_unop
1072 '-', 12, # filestatop
1073 '}', 13, # loopexop
db173bac
MB
1074);
1075
c2dedb93
MHM
1076my %opflags = (
1077 'm' => 1, # needs stack mark
1078 'f' => 2, # fold constants
1079 's' => 4, # always produces scalar
1080 't' => 8, # needs target scalar
903fd87c
NC
1081 'T' => 8 | 16, # ... which may be lexical
1082 'i' => 0, # always produces integer (unused since e7311069)
c2dedb93
MHM
1083 'I' => 32, # has corresponding int op
1084 'd' => 64, # danger, unknown side effects
1085 'u' => 128, # defaults to $_
1086);
1087
2b420b63
JC
1088my %OP_IS_SOCKET; # /Fs/
1089my %OP_IS_FILETEST; # /F-/
1090my %OP_IS_FT_ACCESS; # /F-+/
1091my %OP_IS_NUMCOMPARE; # /S</
332c2eac
JC
1092my %OP_IS_DIRHOP; # /Fd/
1093
903fd87c
NC
1094my $OCSHIFT = 8;
1095my $OASHIFT = 12;
a85d93d9 1096
c2dedb93 1097for my $op (@ops) {
d6480c9d 1098 my $argsum = 0;
c2dedb93
MHM
1099 my $flags = $flags{$op};
1100 for my $flag (keys %opflags) {
1101 if ($flags =~ s/$flag//) {
cb7b5e07 1102 die "Flag collision for '$op' ($flags{$op}, $flag)\n"
c2dedb93
MHM
1103 if $argsum & $opflags{$flag};
1104 $argsum |= $opflags{$flag};
1105 }
1106 }
cb7b5e07 1107 die qq[Opcode '$op' has no class indicator ($flags{$op} => $flags)\n]
c2dedb93
MHM
1108 unless exists $opclass{$flags};
1109 $argsum |= $opclass{$flags} << $OCSHIFT;
1110 my $argshift = $OASHIFT;
1111 for my $arg (split(' ',$args{$op})) {
332c2eac
JC
1112 if ($arg =~ s/^D//) {
1113 # handle 1st, just to put D 1st.
1114 $OP_IS_DIRHOP{$op} = $opnum{$op};
1115 }
a85d93d9 1116 if ($arg =~ /^F/) {
e71197e2
JC
1117 # record opnums of these opnames
1118 $OP_IS_SOCKET{$op} = $opnum{$op} if $arg =~ s/s//;
1119 $OP_IS_FILETEST{$op} = $opnum{$op} if $arg =~ s/-//;
6ecf81d6 1120 $OP_IS_FT_ACCESS{$op} = $opnum{$op} if $arg =~ s/\+//;
a85d93d9 1121 }
2b420b63
JC
1122 elsif ($arg =~ /^S</) {
1123 $OP_IS_NUMCOMPARE{$op} = $opnum{$op} if $arg =~ s/<//;
1124 }
d6480c9d 1125 my $argnum = ($arg =~ s/\?//) ? 8 : 0;
c2dedb93
MHM
1126 die "op = $op, arg = $arg\n"
1127 unless exists $argnum{$arg};
79072805 1128 $argnum += $argnum{$arg};
c2dedb93
MHM
1129 die "Argument overflow for '$op'\n"
1130 if $argshift >= $ARGBITS ||
1131 $argnum > ((1 << ($ARGBITS - $argshift)) - 1);
1132 $argsum += $argnum << $argshift;
1133 $argshift += 4;
79072805
LW
1134 }
1135 $argsum = sprintf("0x%08x", $argsum);
2d6469fe 1136 print $oc "\t", tab(3, "$argsum,"), "/* $op */\n";
79072805
LW
1137}
1138
2d6469fe 1139print $oc <<'END';
79072805
LW
1140};
1141#endif
73c4f7a1 1142
bae1192d
JH
1143#endif /* !PERL_GLOBAL_STRUCT_INIT */
1144
73c4f7a1 1145END_EXTERN_C
79072805
LW
1146END
1147
e71197e2
JC
1148# Emit OP_IS_* macros
1149
2d6469fe 1150print $on <<'EO_OP_IS_COMMENT';
e71197e2 1151
332c2eac
JC
1152/* the OP_IS_* macros are optimized to a simple range check because
1153 all the member OPs are contiguous in regen/opcodes table.
1154 opcode.pl verifies the range contiguity, or generates an OR-equals
1155 expression */
e71197e2
JC
1156EO_OP_IS_COMMENT
1157
1158gen_op_is_macro( \%OP_IS_SOCKET, 'OP_IS_SOCKET');
1159gen_op_is_macro( \%OP_IS_FILETEST, 'OP_IS_FILETEST');
6ecf81d6 1160gen_op_is_macro( \%OP_IS_FT_ACCESS, 'OP_IS_FILETEST_ACCESS');
2b420b63 1161gen_op_is_macro( \%OP_IS_NUMCOMPARE, 'OP_IS_NUMCOMPARE');
332c2eac 1162gen_op_is_macro( \%OP_IS_DIRHOP, 'OP_IS_DIRHOP');
e71197e2
JC
1163
1164sub gen_op_is_macro {
1165 my ($op_is, $macname) = @_;
1166 if (keys %$op_is) {
1167
1168 # get opnames whose numbers are lowest and highest
1169 my ($first, @rest) = sort {
1170 $op_is->{$a} <=> $op_is->{$b}
1171 } keys %$op_is;
1172
1173 my $last = pop @rest; # @rest slurped, get its last
cb7b5e07 1174 die "Invalid range of ops: $first .. $last\n" unless $last;
6ecf81d6 1175
ce716c52 1176 print $on "\n#define $macname(op) \\\n\t(";
6ecf81d6 1177
e71197e2
JC
1178 # verify that op-ct matches 1st..last range (and fencepost)
1179 # (we know there are no dups)
1180 if ( $op_is->{$last} - $op_is->{$first} == scalar @rest + 1) {
1181
1182 # contiguous ops -> optimized version
2b420b63
JC
1183 print $on "(op) >= OP_" . uc($first)
1184 . " && (op) <= OP_" . uc($last);
e71197e2
JC
1185 }
1186 else {
424a4936 1187 print $on join(" || \\\n\t ",
2b420b63 1188 map { "(op) == OP_" . uc() } sort keys %$op_is);
e71197e2 1189 }
2b420b63 1190 print $on ")\n";
e71197e2 1191 }
a85d93d9
JH
1192}
1193
cc49830d
NC
1194my $pp = open_new('pp_proto.h', '>',
1195 { by => 'opcode.pl', from => 'its data' });
a27f85b3 1196
981b7185
NC
1197{
1198 my %funcs;
1199 for (@ops) {
7627e6d0 1200 my $name = $alias{$_} ? $alias{$_}[0] : "Perl_pp_$_";
981b7185
NC
1201 ++$funcs{$name};
1202 }
1203 print $pp "PERL_CALLCONV OP *$_(pTHX);\n" foreach sort keys %funcs;
735e0d5c 1204}
f3574cc6
DM
1205
1206print $oc "\n\n";
1207OP_PRIVATE::print_defines($oc);
1208OP_PRIVATE::print_PL_op_private_tables($oc);
1209
1210OP_PRIVATE::print_B_Op_private($oprivpm);
1211
1212foreach ($oc, $on, $pp, $oprivpm) {
ce716c52
NC
1213 read_only_bottom_close_and_rename($_);
1214}
b162f9ea 1215