This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Refactor ExtUtils::Constant::Utils backwards compatibility code.
[perl5.git] / regen / regcomp.pl
CommitLineData
916e4025 1#!/usr/bin/perl -w
6294c161
DM
2#
3# Regenerate (overwriting only if changed):
4#
5# regnodes.h
6#
7# from information stored in
8#
9# regcomp.sym
10# regexp.h
11#
12# Accepts the standard regen_lib -q and -v args.
13#
14# This script is normally invoked from regen.pl.
15
36bb303b
NC
16BEGIN {
17 # Get function prototypes
af001346 18 require 'regen/regen_lib.pl';
36bb303b 19}
d09b2d29 20#use Fatal qw(open close rename chmod unlink);
03363afd 21use strict;
03363afd 22
d09b2d29 23open DESC, 'regcomp.sym';
d09b2d29 24
03363afd 25my $ind = 0;
f9ef50a7 26my (@name,@rest,@type,@code,@args,@flags,@longj);
03363afd 27my ($desc,$lastregop);
d09b2d29 28while (<DESC>) {
03363afd
YO
29 s/#.*$//;
30 next if /^\s*$/;
916e4025
NC
31 chomp; # No \z in 5.004
32 s/\s*$//;
03363afd
YO
33 if (/^-+\s*$/) {
34 $lastregop= $ind;
35 next;
36 }
37 unless ($lastregop) {
f8abb37e 38 ($name[$ind], $desc, $rest[$ind]) = /^(\S+)\s+([^\t]+)\s*;\s*(.*)/;
f9ef50a7
NC
39 ($type[$ind], $code[$ind], $args[$ind], $flags[$ind], $longj[$ind])
40 = split /[,\s]\s*/, $desc;
93882df0 41 ++$ind;
03363afd 42 } else {
f8abb37e 43 my ($type,@lists)=split /\s+/, $_;
03363afd
YO
44 die "No list? $type" if !@lists;
45 foreach my $list (@lists) {
46 my ($names,$special)=split /:/, $list , 2;
47 $special ||= "";
48 foreach my $name (split /,/,$names) {
49 my $real= $name eq 'resume'
50 ? "resume_$type"
51 : "${type}_$name";
52 my @suffix;
53 if (!$special) {
54 @suffix=("");
55 } elsif ($special=~/\d/) {
56 @suffix=(1..$special);
57 } elsif ($special eq 'FAIL') {
58 @suffix=("","_fail");
59 } else {
60 die "unknown :type ':$special'";
61 }
62 foreach my $suffix (@suffix) {
03363afd
YO
63 $name[$ind]="$real$suffix";
64 $type[$ind]=$type;
24b23f37 65 $rest[$ind]="state for $type";
93882df0 66 ++$ind;
03363afd
YO
67 }
68 }
69 }
70
71 }
72}
5d458dd8
YO
73# use fixed width to keep the diffs between regcomp.pl recompiles
74# as small as possible.
75my ($width,$rwidth,$twidth)=(22,12,9);
03363afd
YO
76$lastregop ||= $ind;
77my $tot = $ind;
d09b2d29 78close DESC;
03363afd
YO
79die "Too many regexp/state opcodes! Maximum is 256, but there are $lastregop in file!"
80 if $lastregop>256;
d09b2d29 81
f9ef50a7
NC
82sub process_flags {
83 my ($flag, $varname, $comment) = @_;
84 $comment = '' unless defined $comment;
85
86 $ind = 0;
87 my @selected;
ded4dd2a 88 my $bitmap = '';
93882df0 89 do {
ded4dd2a
NC
90 my $set = $flags[$ind] && $flags[$ind] eq $flag ? 1 : 0;
91 # Whilst I could do this with vec, I'd prefer to do longhand the arithmetic
92 # ops in the C code.
93 my $current = do {
916e4025 94 local $^W;
ded4dd2a 95 ord do {
ded4dd2a
NC
96 substr $bitmap, ($ind >> 3);
97 }
98 };
916e4025 99 substr($bitmap, ($ind >> 3), 1) = chr($current | ($set << ($ind & 7)));
ded4dd2a
NC
100
101 push @selected, $name[$ind] if $set;
93882df0 102 } while (++$ind < $lastregop);
f9ef50a7
NC
103 my $out_string = join ', ', @selected, 0;
104 $out_string =~ s/(.{1,70},) /$1\n /g;
ded4dd2a
NC
105
106 my $out_mask = join ', ', map {sprintf "0x%02X", ord $_} split '', $bitmap;
107
f9ef50a7 108 return $comment . <<"EOP";
ded4dd2a 109#define REGNODE_\U$varname\E(node) (PL_${varname}_bitmask[(node) >> 3] & (1 << ((node) & 7)))
e52fc539 110
f9ef50a7 111#ifndef DOINIT
916e4025 112EXTCONST U8 PL_${varname}\[] __attribute__deprecated__;
f9ef50a7 113#else
916e4025 114EXTCONST U8 PL_${varname}\[] __attribute__deprecated__ = {
f9ef50a7
NC
115 $out_string
116};
117#endif /* DOINIT */
118
ded4dd2a
NC
119#ifndef DOINIT
120EXTCONST U8 PL_${varname}_bitmask[];
121#else
122EXTCONST U8 PL_${varname}_bitmask[] = {
123 $out_mask
124};
125#endif /* DOINIT */
126
f9ef50a7
NC
127EOP
128}
129
266db279 130my $tmp_h = 'regnodes.h-new';
d09b2d29
IZ
131
132unlink $tmp_h if -f $tmp_h;
133
424a4936 134my $out = safer_open($tmp_h);
d09b2d29 135
424a4936 136printf $out <<EOP,
37442d52
RGS
137/* -*- buffer-read-only: t -*-
138 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
eb2624c9 139 This file is built by regen/regcomp.pl from regcomp.sym.
d09b2d29
IZ
140 Any changes made here will be lost!
141*/
142
6bda09f9
YO
143/* Regops and State definitions */
144
03363afd
YO
145#define %*s\t%d
146#define %*s\t%d
147
d09b2d29 148EOP
f9f4320a
YO
149 -$width, REGNODE_MAX => $lastregop - 1,
150 -$width, REGMATCH_STATE_MAX => $tot - 1
151;
d09b2d29 152
24b23f37 153
93882df0 154for ($ind=0; $ind < $lastregop ; ++$ind) {
424a4936 155 printf $out "#define\t%*s\t%d\t/* %#04x %s */\n",
93882df0 156 -$width, $name[$ind], $ind, $ind, $rest[$ind];
24b23f37 157}
424a4936 158print $out "\t/* ------------ States ------------- */\n";
93882df0 159for ( ; $ind < $tot ; $ind++) {
424a4936 160 printf $out "#define\t%*s\t(REGNODE_MAX + %d)\t/* %s */\n",
93882df0 161 -$width, $name[$ind], $ind - $lastregop + 1, $rest[$ind];
d09b2d29
IZ
162}
163
424a4936 164print $out <<EOP;
03363afd 165
6bda09f9 166/* PL_regkind[] What type of regop or state is this. */
d09b2d29
IZ
167
168#ifndef DOINIT
22c35a8c 169EXTCONST U8 PL_regkind[];
d09b2d29 170#else
22c35a8c 171EXTCONST U8 PL_regkind[] = {
d09b2d29
IZ
172EOP
173
174$ind = 0;
93882df0 175do {
424a4936 176 printf $out "\t%*s\t/* %*s */\n",
03363afd 177 -1-$twidth, "$type[$ind],", -$width, $name[$ind];
424a4936 178 print $out "\t/* ------------ States ------------- */\n"
93882df0
NC
179 if $ind + 1 == $lastregop and $lastregop != $tot;
180} while (++$ind < $tot);
d09b2d29 181
424a4936 182print $out <<EOP;
d09b2d29
IZ
183};
184#endif
185
6bda09f9 186/* regarglen[] - How large is the argument part of the node (in regnodes) */
d09b2d29
IZ
187
188#ifdef REG_COMP_C
29de9391 189static const U8 regarglen[] = {
d09b2d29
IZ
190EOP
191
192$ind = 0;
93882df0 193do {
03363afd 194 my $size = 0;
d09b2d29
IZ
195 $size = "EXTRA_SIZE(struct regnode_$args[$ind])" if $args[$ind];
196
424a4936 197 printf $out "\t%*s\t/* %*s */\n",
03363afd 198 -37, "$size,",-$rwidth,$name[$ind];
93882df0 199} while (++$ind < $lastregop);
d09b2d29 200
424a4936 201print $out <<EOP;
d09b2d29
IZ
202};
203
6bda09f9
YO
204/* reg_off_by_arg[] - Which argument holds the offset to the next node */
205
29de9391 206static const char reg_off_by_arg[] = {
d09b2d29
IZ
207EOP
208
209$ind = 0;
93882df0 210do {
03363afd 211 my $size = $longj[$ind] || 0;
9b155405 212
424a4936 213 printf $out "\t%d,\t/* %*s */\n",
03363afd 214 $size, -$rwidth, $name[$ind]
93882df0 215} while (++$ind < $lastregop);
d09b2d29 216
424a4936 217print $out <<EOP;
d09b2d29 218};
9b155405 219
13d6edb4
NC
220#endif /* REG_COMP_C */
221
6bda09f9
YO
222/* reg_name[] - Opcode/state names in string form, for debugging */
223
22429478 224#ifndef DOINIT
13d6edb4 225EXTCONST char * PL_reg_name[];
22429478 226#else
4764e399 227EXTCONST char * const PL_reg_name[] = {
9b155405
IZ
228EOP
229
230$ind = 0;
93882df0 231my $ofs = 0;
24b23f37 232my $sym = "";
93882df0 233do {
03363afd 234 my $size = $longj[$ind] || 0;
9b155405 235
424a4936 236 printf $out "\t%*s\t/* $sym%#04x */\n",
24b23f37 237 -3-$width,qq("$name[$ind]",), $ind - $ofs;
93882df0 238 if ($ind + 1 == $lastregop and $lastregop != $tot) {
424a4936 239 print $out "\t/* ------------ States ------------- */\n";
93882df0 240 $ofs = $lastregop - 1;
24b23f37
YO
241 $sym = 'REGNODE_MAX +';
242 }
243
93882df0 244} while (++$ind < $tot);
9b155405 245
424a4936 246print $out <<EOP;
9b155405 247};
22429478 248#endif /* DOINIT */
d09b2d29 249
f7819f85
A
250/* PL_reg_extflags_name[] - Opcode/state names in string form, for debugging */
251
252#ifndef DOINIT
253EXTCONST char * PL_reg_extflags_name[];
254#else
255EXTCONST char * const PL_reg_extflags_name[] = {
d09b2d29
IZ
256EOP
257
f7819f85 258my %rxfv;
6a080ccd 259my %definitions; # Remember what the symbol definitions are
c8e4cf8b
NC
260my $val = 0;
261my %reverse;
1850c8f9 262foreach my $file ("op_reg_common.h", "regexp.h") {
916e4025
NC
263 open FH,"<$file" or die "Can't read $file: $!";
264 while (<FH>) {
1850c8f9
KW
265
266 # optional leading '_'. Return symbol in $1, and strip it from
267 # rest of line
268 if (s/ \#define \s+ ( _? RXf_ \w+ ) \s+ //xi) {
269 chomp;
270 my $define = $1;
271 s: / \s* \* .*? \* \s* / : :x; # Replace comments by a blank
272
273 # Replace any prior defined symbols by their values
274 foreach my $key (keys %definitions) {
275 s/\b$key\b/$definitions{$key}/g;
276 }
277 my $newval = eval $_; # Get numeric definition
6a080ccd 278
1850c8f9 279 $definitions{$define} = $newval;
6a080ccd 280
1850c8f9
KW
281 next unless $_ =~ /<</; # Bit defines use left shift
282 if($val & $newval) {
283 die sprintf "Both $define and $reverse{$newval} use %08X", $newval;
284 }
285 $val|=$newval;
286 $rxfv{$define}= $newval;
287 $reverse{$newval} = $define;
6a080ccd 288 }
f7819f85 289 }
1850c8f9 290}
f7819f85 291my %vrxf=reverse %rxfv;
916e4025 292printf $out "\t/* Bits in extflags defined: %s */\n", unpack 'B*', pack 'N', $val;
f7819f85
A
293for (0..31) {
294 my $n=$vrxf{2**$_}||"UNUSED_BIT_$_";
295 $n=~s/^RXf_(PMf_)?//;
424a4936 296 printf $out qq(\t%-20s/* 0x%08x */\n),
f7819f85
A
297 qq("$n",),2**$_;
298}
299
424a4936 300print $out <<EOP;
f7819f85
A
301};
302#endif /* DOINIT */
303
f9ef50a7
NC
304EOP
305
306print $out process_flags('V', 'varies', <<'EOC');
307/* The following have no fixed length. U8 so we can do strchr() on it. */
308EOC
309
310print $out process_flags('S', 'simple', <<'EOC');
311/* The following always have a length of 1. U8 we can do strchr() on it. */
312/* (Note that length 1 means "one character" under UTF8, not "one octet".) */
313EOC
314
315print $out <<EOP;
f7819f85
A
316/* ex: set ro: */
317EOP
08858ed2 318safer_close($out);
d09b2d29 319
424a4936 320rename_if_different $tmp_h, 'regnodes.h';