This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Regenerate the regnode table in perldebguts.pod automatically
[perl5.git] / regen / regcomp.pl
CommitLineData
916e4025 1#!/usr/bin/perl -w
6294c161
DM
2#
3# Regenerate (overwriting only if changed):
4#
65aa4ca7 5# pod/perldebguts.pod
6294c161
DM
6# regnodes.h
7#
8# from information stored in
9#
10# regcomp.sym
11# regexp.h
12#
65aa4ca7
FC
13# pod/perldebguts.pod is not completely regenerated. Only the table of
14# regexp nodes is replaced; other parts remain unchanged.
15#
6294c161
DM
16# Accepts the standard regen_lib -q and -v args.
17#
18# This script is normally invoked from regen.pl.
19
36bb303b
NC
20BEGIN {
21 # Get function prototypes
af001346 22 require 'regen/regen_lib.pl';
36bb303b 23}
03363afd 24use strict;
03363afd 25
d09b2d29 26open DESC, 'regcomp.sym';
d09b2d29 27
03363afd 28my $ind = 0;
65aa4ca7
FC
29my (@name,@rest,@type,@code,@args,@flags,@longj,@cmnt);
30my ($longest_name_length,$desc,$lastregop) = 0;
d09b2d29 31while (<DESC>) {
65aa4ca7
FC
32 # Special pod comments
33 if (/^#\* ?/) { $cmnt[$ind] .= "# $'"; }
34 # Truly blank lines possibly surrounding pod comments
35 elsif (/^\s*$/) { $cmnt[$ind] .= "\n" }
36
37 next if /^(?:#|\s*$)/;
916e4025
NC
38 chomp; # No \z in 5.004
39 s/\s*$//;
03363afd
YO
40 if (/^-+\s*$/) {
41 $lastregop= $ind;
42 next;
43 }
44 unless ($lastregop) {
44f5ace3 45 ($name[$ind], $desc, $rest[$ind]) = /^(\S+)\s+([^\t]+?)\s*;\s*(.*)/;
f9ef50a7
NC
46 ($type[$ind], $code[$ind], $args[$ind], $flags[$ind], $longj[$ind])
47 = split /[,\s]\s*/, $desc;
65aa4ca7
FC
48 $longest_name_length = length $name[$ind]
49 if length $name[$ind] > $longest_name_length;
93882df0 50 ++$ind;
03363afd 51 } else {
f8abb37e 52 my ($type,@lists)=split /\s+/, $_;
03363afd
YO
53 die "No list? $type" if !@lists;
54 foreach my $list (@lists) {
55 my ($names,$special)=split /:/, $list , 2;
56 $special ||= "";
57 foreach my $name (split /,/,$names) {
58 my $real= $name eq 'resume'
59 ? "resume_$type"
60 : "${type}_$name";
61 my @suffix;
62 if (!$special) {
63 @suffix=("");
64 } elsif ($special=~/\d/) {
65 @suffix=(1..$special);
66 } elsif ($special eq 'FAIL') {
67 @suffix=("","_fail");
68 } else {
69 die "unknown :type ':$special'";
70 }
71 foreach my $suffix (@suffix) {
03363afd
YO
72 $name[$ind]="$real$suffix";
73 $type[$ind]=$type;
24b23f37 74 $rest[$ind]="state for $type";
93882df0 75 ++$ind;
03363afd
YO
76 }
77 }
78 }
79
80 }
81}
5d458dd8
YO
82# use fixed width to keep the diffs between regcomp.pl recompiles
83# as small as possible.
84my ($width,$rwidth,$twidth)=(22,12,9);
03363afd
YO
85$lastregop ||= $ind;
86my $tot = $ind;
d09b2d29 87close DESC;
03363afd
YO
88die "Too many regexp/state opcodes! Maximum is 256, but there are $lastregop in file!"
89 if $lastregop>256;
d09b2d29 90
f9ef50a7
NC
91sub process_flags {
92 my ($flag, $varname, $comment) = @_;
93 $comment = '' unless defined $comment;
94
95 $ind = 0;
96 my @selected;
ded4dd2a 97 my $bitmap = '';
93882df0 98 do {
ded4dd2a
NC
99 my $set = $flags[$ind] && $flags[$ind] eq $flag ? 1 : 0;
100 # Whilst I could do this with vec, I'd prefer to do longhand the arithmetic
101 # ops in the C code.
102 my $current = do {
916e4025 103 local $^W;
ded4dd2a 104 ord do {
ded4dd2a
NC
105 substr $bitmap, ($ind >> 3);
106 }
107 };
916e4025 108 substr($bitmap, ($ind >> 3), 1) = chr($current | ($set << ($ind & 7)));
ded4dd2a
NC
109
110 push @selected, $name[$ind] if $set;
93882df0 111 } while (++$ind < $lastregop);
f9ef50a7
NC
112 my $out_string = join ', ', @selected, 0;
113 $out_string =~ s/(.{1,70},) /$1\n /g;
ded4dd2a
NC
114
115 my $out_mask = join ', ', map {sprintf "0x%02X", ord $_} split '', $bitmap;
116
f9ef50a7 117 return $comment . <<"EOP";
ded4dd2a 118#define REGNODE_\U$varname\E(node) (PL_${varname}_bitmask[(node) >> 3] & (1 << ((node) & 7)))
e52fc539 119
f9ef50a7 120#ifndef DOINIT
916e4025 121EXTCONST U8 PL_${varname}\[] __attribute__deprecated__;
f9ef50a7 122#else
916e4025 123EXTCONST U8 PL_${varname}\[] __attribute__deprecated__ = {
f9ef50a7
NC
124 $out_string
125};
126#endif /* DOINIT */
127
ded4dd2a
NC
128#ifndef DOINIT
129EXTCONST U8 PL_${varname}_bitmask[];
130#else
131EXTCONST U8 PL_${varname}_bitmask[] = {
132 $out_mask
133};
134#endif /* DOINIT */
f9ef50a7
NC
135EOP
136}
137
cc49830d
NC
138my $out = open_new('regnodes.h', '>',
139 { by => 'regen/regcomp.pl', from => 'regcomp.sym' });
424a4936 140printf $out <<EOP,
6bda09f9
YO
141/* Regops and State definitions */
142
03363afd
YO
143#define %*s\t%d
144#define %*s\t%d
145
d09b2d29 146EOP
f9f4320a
YO
147 -$width, REGNODE_MAX => $lastregop - 1,
148 -$width, REGMATCH_STATE_MAX => $tot - 1
149;
d09b2d29 150
24b23f37 151
93882df0 152for ($ind=0; $ind < $lastregop ; ++$ind) {
424a4936 153 printf $out "#define\t%*s\t%d\t/* %#04x %s */\n",
93882df0 154 -$width, $name[$ind], $ind, $ind, $rest[$ind];
24b23f37 155}
424a4936 156print $out "\t/* ------------ States ------------- */\n";
93882df0 157for ( ; $ind < $tot ; $ind++) {
424a4936 158 printf $out "#define\t%*s\t(REGNODE_MAX + %d)\t/* %s */\n",
93882df0 159 -$width, $name[$ind], $ind - $lastregop + 1, $rest[$ind];
d09b2d29
IZ
160}
161
424a4936 162print $out <<EOP;
03363afd 163
6bda09f9 164/* PL_regkind[] What type of regop or state is this. */
d09b2d29
IZ
165
166#ifndef DOINIT
22c35a8c 167EXTCONST U8 PL_regkind[];
d09b2d29 168#else
22c35a8c 169EXTCONST U8 PL_regkind[] = {
d09b2d29
IZ
170EOP
171
172$ind = 0;
93882df0 173do {
424a4936 174 printf $out "\t%*s\t/* %*s */\n",
03363afd 175 -1-$twidth, "$type[$ind],", -$width, $name[$ind];
424a4936 176 print $out "\t/* ------------ States ------------- */\n"
93882df0
NC
177 if $ind + 1 == $lastregop and $lastregop != $tot;
178} while (++$ind < $tot);
d09b2d29 179
424a4936 180print $out <<EOP;
d09b2d29
IZ
181};
182#endif
183
6bda09f9 184/* regarglen[] - How large is the argument part of the node (in regnodes) */
d09b2d29
IZ
185
186#ifdef REG_COMP_C
29de9391 187static const U8 regarglen[] = {
d09b2d29
IZ
188EOP
189
190$ind = 0;
93882df0 191do {
03363afd 192 my $size = 0;
d09b2d29
IZ
193 $size = "EXTRA_SIZE(struct regnode_$args[$ind])" if $args[$ind];
194
424a4936 195 printf $out "\t%*s\t/* %*s */\n",
03363afd 196 -37, "$size,",-$rwidth,$name[$ind];
93882df0 197} while (++$ind < $lastregop);
d09b2d29 198
424a4936 199print $out <<EOP;
d09b2d29
IZ
200};
201
6bda09f9
YO
202/* reg_off_by_arg[] - Which argument holds the offset to the next node */
203
29de9391 204static const char reg_off_by_arg[] = {
d09b2d29
IZ
205EOP
206
207$ind = 0;
93882df0 208do {
03363afd 209 my $size = $longj[$ind] || 0;
9b155405 210
424a4936 211 printf $out "\t%d,\t/* %*s */\n",
03363afd 212 $size, -$rwidth, $name[$ind]
93882df0 213} while (++$ind < $lastregop);
d09b2d29 214
424a4936 215print $out <<EOP;
d09b2d29 216};
9b155405 217
13d6edb4
NC
218#endif /* REG_COMP_C */
219
6bda09f9
YO
220/* reg_name[] - Opcode/state names in string form, for debugging */
221
22429478 222#ifndef DOINIT
13d6edb4 223EXTCONST char * PL_reg_name[];
22429478 224#else
4764e399 225EXTCONST char * const PL_reg_name[] = {
9b155405
IZ
226EOP
227
228$ind = 0;
93882df0 229my $ofs = 0;
24b23f37 230my $sym = "";
93882df0 231do {
03363afd 232 my $size = $longj[$ind] || 0;
9b155405 233
424a4936 234 printf $out "\t%*s\t/* $sym%#04x */\n",
24b23f37 235 -3-$width,qq("$name[$ind]",), $ind - $ofs;
93882df0 236 if ($ind + 1 == $lastregop and $lastregop != $tot) {
424a4936 237 print $out "\t/* ------------ States ------------- */\n";
93882df0 238 $ofs = $lastregop - 1;
24b23f37
YO
239 $sym = 'REGNODE_MAX +';
240 }
241
93882df0 242} while (++$ind < $tot);
9b155405 243
424a4936 244print $out <<EOP;
9b155405 245};
22429478 246#endif /* DOINIT */
d09b2d29 247
f7819f85
A
248/* PL_reg_extflags_name[] - Opcode/state names in string form, for debugging */
249
250#ifndef DOINIT
251EXTCONST char * PL_reg_extflags_name[];
252#else
253EXTCONST char * const PL_reg_extflags_name[] = {
d09b2d29
IZ
254EOP
255
f7819f85 256my %rxfv;
6a080ccd 257my %definitions; # Remember what the symbol definitions are
c8e4cf8b
NC
258my $val = 0;
259my %reverse;
1850c8f9 260foreach my $file ("op_reg_common.h", "regexp.h") {
916e4025
NC
261 open FH,"<$file" or die "Can't read $file: $!";
262 while (<FH>) {
1850c8f9
KW
263
264 # optional leading '_'. Return symbol in $1, and strip it from
265 # rest of line
5255171e 266 if (s/ \# \s* define \s+ ( _? RXf_ \w+ ) \s+ //xi) {
1850c8f9
KW
267 chomp;
268 my $define = $1;
269 s: / \s* \* .*? \* \s* / : :x; # Replace comments by a blank
270
271 # Replace any prior defined symbols by their values
272 foreach my $key (keys %definitions) {
273 s/\b$key\b/$definitions{$key}/g;
274 }
5c72e80d
TC
275
276 # Remove the U suffix from unsigned int literals
277 s/\b([0-9]+)U\b/$1/g;
278
1850c8f9 279 my $newval = eval $_; # Get numeric definition
6a080ccd 280
1850c8f9 281 $definitions{$define} = $newval;
6a080ccd 282
1850c8f9
KW
283 next unless $_ =~ /<</; # Bit defines use left shift
284 if($val & $newval) {
285 die sprintf "Both $define and $reverse{$newval} use %08X", $newval;
286 }
287 $val|=$newval;
288 $rxfv{$define}= $newval;
289 $reverse{$newval} = $define;
6a080ccd 290 }
f7819f85 291 }
1850c8f9 292}
f7819f85 293my %vrxf=reverse %rxfv;
916e4025 294printf $out "\t/* Bits in extflags defined: %s */\n", unpack 'B*', pack 'N', $val;
f7819f85 295for (0..31) {
5458d9a0
KW
296 my $power_of_2 = 2**$_;
297 my $n=$vrxf{$power_of_2};
298 if (! $n) {
299
300 # Here, there was no name that matched exactly the bit. It could be
301 # either that it is unused, or the name matches multiple bits.
302 if (! ($val & $power_of_2)) {
303 $n = "UNUSED_BIT_$_";
304 }
305 else {
306
307 # Here, must be because it matches multiple bits. Look through
308 # all possibilities until find one that matches this one. Use
309 # that name, and all the bits it matches
310 foreach my $name (keys %rxfv) {
311 if ($rxfv{$name} & $power_of_2) {
312 $n = $name;
313 $power_of_2 = $rxfv{$name};
314 last;
315 }
316 }
317 }
318 }
f7819f85 319 $n=~s/^RXf_(PMf_)?//;
424a4936 320 printf $out qq(\t%-20s/* 0x%08x */\n),
5458d9a0 321 qq("$n",),$power_of_2;
f7819f85
A
322}
323
424a4936 324print $out <<EOP;
f7819f85
A
325};
326#endif /* DOINIT */
327
f9ef50a7
NC
328EOP
329
330print $out process_flags('V', 'varies', <<'EOC');
331/* The following have no fixed length. U8 so we can do strchr() on it. */
332EOC
333
334print $out process_flags('S', 'simple', <<'EOC');
ce716c52 335
f9ef50a7
NC
336/* The following always have a length of 1. U8 we can do strchr() on it. */
337/* (Note that length 1 means "one character" under UTF8, not "one octet".) */
338EOC
339
ce716c52 340read_only_bottom_close_and_rename($out);
65aa4ca7
FC
341
342my $guts = open_new('pod/perldebguts.pod', '>');
343
344my $code;
345my $name_fmt = '<' x ($longest_name_length-1);
346my $descr_fmt = '<' x (58-$longest_name_length);
347eval <<EOD;
348format GuTS =
349 ^*~~
350 \$cmnt[\$_]
351 ^<<<<<<<<<<<<<<<<< ^<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
352 \$name[\$_], \$code, \$rest[\$_]
353.
354EOD
355
356select +(select($guts), do {
357 $~ = "GuTS";
358
359 open my $oldguts, "pod/perldebguts.pod"
360 or die "$0 cannot open pod/perldebguts.pod for reading: $!";
361 while(<$oldguts>) {
362 print;
363 last if /=for regcomp.pl begin/;
364 }
365
366 print <<'end';
367
368 # TYPE arg-description [num-args] [longjump-len] DESCRIPTION
369end
370 for (0..$lastregop-1) {
371 $code = "$code[$_] ".($args[$_]||"");
372 $code .= " $longj[$_]" if $longj[$_];
373 if ($cmnt[$_] ||= "") {
374 # Trim multiple blanks
375 $cmnt[$_] =~ s/^\n\n+/\n/; $cmnt[$_] =~ s/\n\n+$/\n\n/
376 }
377 write;
378 }
379 print "\n";
380
381 while(<$oldguts>) {
382 last if /=for regcomp.pl end/;
383 }
384 do { print } while <$oldguts>;
385
386})[0];
387
388close_and_rename($guts);