This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
pp.c: Fix Win32 compilation problems
[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
337ff307
YO
248EOP
249
250{
251print $out <<EOP;
f7819f85
A
252/* PL_reg_extflags_name[] - Opcode/state names in string form, for debugging */
253
254#ifndef DOINIT
255EXTCONST char * PL_reg_extflags_name[];
256#else
257EXTCONST char * const PL_reg_extflags_name[] = {
d09b2d29
IZ
258EOP
259
f7819f85 260my %rxfv;
6a080ccd 261my %definitions; # Remember what the symbol definitions are
c8e4cf8b
NC
262my $val = 0;
263my %reverse;
adc2d0c9 264my $REG_EXTFLAGS_NAME_SIZE = 0;
1850c8f9 265foreach 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 304my %vrxf=reverse %rxfv;
916e4025 305printf $out "\t/* Bits in extflags defined: %s */\n", unpack 'B*', pack 'N', $val;
6976c986 306my %multibits;
f7819f85 307for (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 339print $out <<EOP;
f7819f85
A
340};
341#endif /* DOINIT */
342
f9ef50a7 343EOP
adc2d0c9
JH
344print $out <<EOQ
345#ifdef DEBUGGING
346# define REG_EXTFLAGS_NAME_SIZE $REG_EXTFLAGS_NAME_SIZE
347#endif
348
349EOQ
337ff307
YO
350}
351{
352print $out <<EOP;
353/* PL_reg_intflags_name[] - Opcode/state names in string form, for debugging */
354
355#ifndef DOINIT
356EXTCONST char * PL_reg_intflags_name[];
357#else
358EXTCONST char * const PL_reg_intflags_name[] = {
359EOP
360
361my %rxfv;
362my %definitions; # Remember what the symbol definitions are
363my $val = 0;
364my %reverse;
adc2d0c9 365my $REG_INTFLAGS_NAME_SIZE = 0;
337ff307
YO
366foreach 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
386print $out <<EOP;
387};
388#endif /* DOINIT */
389
390EOP
adc2d0c9
JH
391print $out <<EOQ;
392#ifdef DEBUGGING
393# define REG_INTFLAGS_NAME_SIZE $REG_INTFLAGS_NAME_SIZE
394#endif
337ff307 395
adc2d0c9
JH
396EOQ
397}
f9ef50a7
NC
398
399print $out process_flags('V', 'varies', <<'EOC');
400/* The following have no fixed length. U8 so we can do strchr() on it. */
401EOC
402
403print $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".) */
407EOC
408
ce716c52 409read_only_bottom_close_and_rename($out);
65aa4ca7
FC
410
411my $guts = open_new('pod/perldebguts.pod', '>');
412
413my $code;
414my $name_fmt = '<' x ($longest_name_length-1);
415my $descr_fmt = '<' x (58-$longest_name_length);
416eval <<EOD;
417format GuTS =
418 ^*~~
419 \$cmnt[\$_]
95fe686d 420 ^$name_fmt ^<<<<<<<<< ^$descr_fmt~~
65aa4ca7
FC
421 \$name[\$_], \$code, \$rest[\$_]
422.
423EOD
424
425select +(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
438end
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
457close_and_rename($guts);