Commit | Line | Data |
---|---|---|
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 |
20 | BEGIN { |
21 | # Get function prototypes | |
af001346 | 22 | require 'regen/regen_lib.pl'; |
36bb303b | 23 | } |
03363afd | 24 | use strict; |
03363afd | 25 | |
d09b2d29 | 26 | open DESC, 'regcomp.sym'; |
d09b2d29 | 27 | |
03363afd | 28 | my $ind = 0; |
65aa4ca7 FC |
29 | my (@name,@rest,@type,@code,@args,@flags,@longj,@cmnt); |
30 | my ($longest_name_length,$desc,$lastregop) = 0; | |
d09b2d29 | 31 | while (<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. | |
84 | my ($width,$rwidth,$twidth)=(22,12,9); | |
03363afd YO |
85 | $lastregop ||= $ind; |
86 | my $tot = $ind; | |
d09b2d29 | 87 | close DESC; |
03363afd YO |
88 | die "Too many regexp/state opcodes! Maximum is 256, but there are $lastregop in file!" |
89 | if $lastregop>256; | |
d09b2d29 | 90 | |
f9ef50a7 NC |
91 | sub 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 | 121 | EXTCONST U8 PL_${varname}\[] __attribute__deprecated__; |
f9ef50a7 | 122 | #else |
916e4025 | 123 | EXTCONST U8 PL_${varname}\[] __attribute__deprecated__ = { |
f9ef50a7 NC |
124 | $out_string |
125 | }; | |
126 | #endif /* DOINIT */ | |
127 | ||
ded4dd2a NC |
128 | #ifndef DOINIT |
129 | EXTCONST U8 PL_${varname}_bitmask[]; | |
130 | #else | |
131 | EXTCONST U8 PL_${varname}_bitmask[] = { | |
132 | $out_mask | |
133 | }; | |
134 | #endif /* DOINIT */ | |
f9ef50a7 NC |
135 | EOP |
136 | } | |
137 | ||
cc49830d NC |
138 | my $out = open_new('regnodes.h', '>', |
139 | { by => 'regen/regcomp.pl', from => 'regcomp.sym' }); | |
424a4936 | 140 | printf $out <<EOP, |
6bda09f9 YO |
141 | /* Regops and State definitions */ |
142 | ||
03363afd YO |
143 | #define %*s\t%d |
144 | #define %*s\t%d | |
145 | ||
d09b2d29 | 146 | EOP |
f9f4320a YO |
147 | -$width, REGNODE_MAX => $lastregop - 1, |
148 | -$width, REGMATCH_STATE_MAX => $tot - 1 | |
149 | ; | |
d09b2d29 | 150 | |
24b23f37 | 151 | |
93882df0 | 152 | for ($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 | 156 | print $out "\t/* ------------ States ------------- */\n"; |
93882df0 | 157 | for ( ; $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 | 162 | print $out <<EOP; |
03363afd | 163 | |
6bda09f9 | 164 | /* PL_regkind[] What type of regop or state is this. */ |
d09b2d29 IZ |
165 | |
166 | #ifndef DOINIT | |
22c35a8c | 167 | EXTCONST U8 PL_regkind[]; |
d09b2d29 | 168 | #else |
22c35a8c | 169 | EXTCONST U8 PL_regkind[] = { |
d09b2d29 IZ |
170 | EOP |
171 | ||
172 | $ind = 0; | |
93882df0 | 173 | do { |
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 | 180 | print $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 | 187 | static const U8 regarglen[] = { |
d09b2d29 IZ |
188 | EOP |
189 | ||
190 | $ind = 0; | |
93882df0 | 191 | do { |
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 | 199 | print $out <<EOP; |
d09b2d29 IZ |
200 | }; |
201 | ||
6bda09f9 YO |
202 | /* reg_off_by_arg[] - Which argument holds the offset to the next node */ |
203 | ||
29de9391 | 204 | static const char reg_off_by_arg[] = { |
d09b2d29 IZ |
205 | EOP |
206 | ||
207 | $ind = 0; | |
93882df0 | 208 | do { |
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 | 215 | print $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 | 223 | EXTCONST char * PL_reg_name[]; |
22429478 | 224 | #else |
4764e399 | 225 | EXTCONST char * const PL_reg_name[] = { |
9b155405 IZ |
226 | EOP |
227 | ||
228 | $ind = 0; | |
93882df0 | 229 | my $ofs = 0; |
24b23f37 | 230 | my $sym = ""; |
93882df0 | 231 | do { |
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 | 244 | print $out <<EOP; |
9b155405 | 245 | }; |
22429478 | 246 | #endif /* DOINIT */ |
d09b2d29 | 247 | |
337ff307 YO |
248 | EOP |
249 | ||
250 | { | |
251 | print $out <<EOP; | |
f7819f85 A |
252 | /* PL_reg_extflags_name[] - Opcode/state names in string form, for debugging */ |
253 | ||
254 | #ifndef DOINIT | |
255 | EXTCONST char * PL_reg_extflags_name[]; | |
256 | #else | |
257 | EXTCONST char * const PL_reg_extflags_name[] = { | |
d09b2d29 IZ |
258 | EOP |
259 | ||
f7819f85 | 260 | my %rxfv; |
6a080ccd | 261 | my %definitions; # Remember what the symbol definitions are |
c8e4cf8b NC |
262 | my $val = 0; |
263 | my %reverse; | |
adc2d0c9 | 264 | my $REG_EXTFLAGS_NAME_SIZE = 0; |
1850c8f9 | 265 | foreach my $file ("op_reg_common.h", "regexp.h") { |
916e4025 NC |
266 | open FH,"<$file" or die "Can't read $file: $!"; |
267 | while (<FH>) { | |
1850c8f9 KW |
268 | |
269 | # optional leading '_'. Return symbol in $1, and strip it from | |
270 | # rest of line | |
337ff307 | 271 | if (s/^ \# \s* define \s+ ( _? RXf_ \w+ ) \s+ //xi) { |
1850c8f9 KW |
272 | chomp; |
273 | my $define = $1; | |
6976c986 YO |
274 | my $orig= $_; |
275 | s{ /\* .*? \*/ }{ }x; # Replace comments by a blank | |
1850c8f9 KW |
276 | |
277 | # Replace any prior defined symbols by their values | |
278 | foreach my $key (keys %definitions) { | |
279 | s/\b$key\b/$definitions{$key}/g; | |
280 | } | |
5c72e80d TC |
281 | |
282 | # Remove the U suffix from unsigned int literals | |
283 | s/\b([0-9]+)U\b/$1/g; | |
284 | ||
1850c8f9 | 285 | my $newval = eval $_; # Get numeric definition |
6a080ccd | 286 | |
1850c8f9 | 287 | $definitions{$define} = $newval; |
6a080ccd | 288 | |
1850c8f9 KW |
289 | next unless $_ =~ /<</; # Bit defines use left shift |
290 | if($val & $newval) { | |
6976c986 YO |
291 | my @names=($define, $reverse{$newval}); |
292 | s/PMf_// for @names; | |
293 | if ($names[0] ne $names[1]) { | |
294 | die sprintf "ERROR: both $define and $reverse{$newval} use 0x%08X (%s:%s)", $newval, $orig, $_; | |
295 | } | |
296 | next; | |
1850c8f9 KW |
297 | } |
298 | $val|=$newval; | |
299 | $rxfv{$define}= $newval; | |
300 | $reverse{$newval} = $define; | |
6a080ccd | 301 | } |
f7819f85 | 302 | } |
1850c8f9 | 303 | } |
f7819f85 | 304 | my %vrxf=reverse %rxfv; |
916e4025 | 305 | printf $out "\t/* Bits in extflags defined: %s */\n", unpack 'B*', pack 'N', $val; |
6976c986 | 306 | my %multibits; |
f7819f85 | 307 | for (0..31) { |
5458d9a0 KW |
308 | my $power_of_2 = 2**$_; |
309 | my $n=$vrxf{$power_of_2}; | |
6976c986 | 310 | my $extra = ""; |
5458d9a0 KW |
311 | if (! $n) { |
312 | ||
313 | # Here, there was no name that matched exactly the bit. It could be | |
314 | # either that it is unused, or the name matches multiple bits. | |
315 | if (! ($val & $power_of_2)) { | |
316 | $n = "UNUSED_BIT_$_"; | |
317 | } | |
318 | else { | |
319 | ||
320 | # Here, must be because it matches multiple bits. Look through | |
321 | # all possibilities until find one that matches this one. Use | |
322 | # that name, and all the bits it matches | |
323 | foreach my $name (keys %rxfv) { | |
324 | if ($rxfv{$name} & $power_of_2) { | |
6976c986 YO |
325 | $n = $name . ( $multibits{$name}++ ); |
326 | $extra= sprintf qq{ : "%s" - 0x%08x}, $name, $rxfv{$name} | |
327 | if $power_of_2 != $rxfv{$name}; | |
5458d9a0 KW |
328 | last; |
329 | } | |
330 | } | |
331 | } | |
332 | } | |
6976c986 YO |
333 | s/\bRXf_(PMf_)?// for $n, $extra; |
334 | printf $out qq(\t%-20s/* 0x%08x%s */\n), | |
335 | qq("$n",),$power_of_2, $extra; | |
adc2d0c9 | 336 | $REG_EXTFLAGS_NAME_SIZE++; |
f7819f85 A |
337 | } |
338 | ||
424a4936 | 339 | print $out <<EOP; |
f7819f85 A |
340 | }; |
341 | #endif /* DOINIT */ | |
342 | ||
f9ef50a7 | 343 | EOP |
adc2d0c9 JH |
344 | print $out <<EOQ |
345 | #ifdef DEBUGGING | |
346 | # define REG_EXTFLAGS_NAME_SIZE $REG_EXTFLAGS_NAME_SIZE | |
347 | #endif | |
348 | ||
349 | EOQ | |
337ff307 YO |
350 | } |
351 | { | |
352 | print $out <<EOP; | |
353 | /* PL_reg_intflags_name[] - Opcode/state names in string form, for debugging */ | |
354 | ||
355 | #ifndef DOINIT | |
356 | EXTCONST char * PL_reg_intflags_name[]; | |
357 | #else | |
358 | EXTCONST char * const PL_reg_intflags_name[] = { | |
359 | EOP | |
360 | ||
361 | my %rxfv; | |
362 | my %definitions; # Remember what the symbol definitions are | |
363 | my $val = 0; | |
364 | my %reverse; | |
adc2d0c9 | 365 | my $REG_INTFLAGS_NAME_SIZE = 0; |
337ff307 YO |
366 | foreach my $file ("regcomp.h") { |
367 | open my $fh, "<", $file or die "Can't read $file: $!"; | |
368 | while (<$fh>) { | |
369 | # optional leading '_'. Return symbol in $1, and strip it from | |
370 | # rest of line | |
371 | if (m/^ \# \s* define \s+ ( PREGf_ ( \w+ ) ) \s+ 0x([0-9a-f]+)(?:\s*\/\*(.*)\*\/)?/xi) { | |
372 | chomp; | |
373 | my $define = $1; | |
374 | my $abbr= $2; | |
375 | my $hex= $3; | |
376 | my $comment= $4; | |
377 | my $val= hex($hex); | |
378 | $comment= $comment ? " - $comment" : ""; | |
379 | ||
380 | printf $out qq(\t%-30s/* 0x%08x - %s%s */\n), qq("$abbr",), $val, $define, $comment; | |
adc2d0c9 | 381 | $REG_INTFLAGS_NAME_SIZE++; |
337ff307 YO |
382 | } |
383 | } | |
384 | } | |
385 | ||
386 | print $out <<EOP; | |
387 | }; | |
388 | #endif /* DOINIT */ | |
389 | ||
390 | EOP | |
adc2d0c9 JH |
391 | print $out <<EOQ; |
392 | #ifdef DEBUGGING | |
393 | # define REG_INTFLAGS_NAME_SIZE $REG_INTFLAGS_NAME_SIZE | |
394 | #endif | |
337ff307 | 395 | |
adc2d0c9 JH |
396 | EOQ |
397 | } | |
f9ef50a7 NC |
398 | |
399 | print $out process_flags('V', 'varies', <<'EOC'); | |
400 | /* The following have no fixed length. U8 so we can do strchr() on it. */ | |
401 | EOC | |
402 | ||
403 | print $out process_flags('S', 'simple', <<'EOC'); | |
ce716c52 | 404 | |
f9ef50a7 NC |
405 | /* The following always have a length of 1. U8 we can do strchr() on it. */ |
406 | /* (Note that length 1 means "one character" under UTF8, not "one octet".) */ | |
407 | EOC | |
408 | ||
ce716c52 | 409 | read_only_bottom_close_and_rename($out); |
65aa4ca7 FC |
410 | |
411 | my $guts = open_new('pod/perldebguts.pod', '>'); | |
412 | ||
413 | my $code; | |
414 | my $name_fmt = '<' x ($longest_name_length-1); | |
415 | my $descr_fmt = '<' x (58-$longest_name_length); | |
416 | eval <<EOD; | |
417 | format GuTS = | |
418 | ^*~~ | |
419 | \$cmnt[\$_] | |
95fe686d | 420 | ^$name_fmt ^<<<<<<<<< ^$descr_fmt~~ |
65aa4ca7 FC |
421 | \$name[\$_], \$code, \$rest[\$_] |
422 | . | |
423 | EOD | |
424 | ||
425 | select +(select($guts), do { | |
426 | $~ = "GuTS"; | |
427 | ||
428 | open my $oldguts, "pod/perldebguts.pod" | |
429 | or die "$0 cannot open pod/perldebguts.pod for reading: $!"; | |
430 | while(<$oldguts>) { | |
431 | print; | |
432 | last if /=for regcomp.pl begin/; | |
433 | } | |
434 | ||
435 | print <<'end'; | |
436 | ||
437 | # TYPE arg-description [num-args] [longjump-len] DESCRIPTION | |
438 | end | |
439 | for (0..$lastregop-1) { | |
440 | $code = "$code[$_] ".($args[$_]||""); | |
441 | $code .= " $longj[$_]" if $longj[$_]; | |
442 | if ($cmnt[$_] ||= "") { | |
443 | # Trim multiple blanks | |
444 | $cmnt[$_] =~ s/^\n\n+/\n/; $cmnt[$_] =~ s/\n\n+$/\n\n/ | |
445 | } | |
446 | write; | |
447 | } | |
448 | print "\n"; | |
449 | ||
450 | while(<$oldguts>) { | |
451 | last if /=for regcomp.pl end/; | |
452 | } | |
453 | do { print } while <$oldguts>; | |
454 | ||
455 | })[0]; | |
456 | ||
457 | close_and_rename($guts); |