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
1 #!/usr/bin/perl -w
2
3 # Regenerate (overwriting only if changed):
4 #
5 #    pod/perldebguts.pod
6 #    regnodes.h
7 #
8 # from information stored in
9 #
10 #    regcomp.sym
11 #    regexp.h
12 #
13 # pod/perldebguts.pod is not completely regenerated.  Only the table of
14 # regexp nodes is replaced; other parts remain unchanged.
15 #
16 # Accepts the standard regen_lib -q and -v args.
17 #
18 # This script is normally invoked from regen.pl.
19
20 BEGIN {
21     # Get function prototypes
22     require 'regen/regen_lib.pl';
23 }
24 use strict;
25
26 open DESC, 'regcomp.sym';
27
28 my $ind = 0;
29 my (@name,@rest,@type,@code,@args,@flags,@longj,@cmnt);
30 my ($longest_name_length,$desc,$lastregop) = 0;
31 while (<DESC>) {
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*$)/;
38     chomp; # No \z in 5.004
39     s/\s*$//;
40     if (/^-+\s*$/) {
41         $lastregop= $ind;
42         next;
43     }
44     unless ($lastregop) {
45         ($name[$ind], $desc, $rest[$ind]) = /^(\S+)\s+([^\t]+?)\s*;\s*(.*)/;
46         ($type[$ind], $code[$ind], $args[$ind], $flags[$ind], $longj[$ind])
47           = split /[,\s]\s*/, $desc;
48         $longest_name_length = length $name[$ind]
49           if length $name[$ind] > $longest_name_length;
50         ++$ind;
51     } else {
52         my ($type,@lists)=split /\s+/, $_;
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) {
72                     $name[$ind]="$real$suffix";
73                     $type[$ind]=$type;
74                     $rest[$ind]="state for $type";
75                     ++$ind;
76                 }
77             }
78         }
79         
80     }
81 }
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);
85 $lastregop ||= $ind;
86 my $tot = $ind;
87 close DESC;
88 die "Too many regexp/state opcodes! Maximum is 256, but there are $lastregop in file!"
89     if $lastregop>256;
90
91 sub process_flags {
92   my ($flag, $varname, $comment) = @_;
93   $comment = '' unless defined $comment;
94
95   $ind = 0;
96   my @selected;
97   my $bitmap = '';
98   do {
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 {
103       local $^W;
104       ord do {
105         substr $bitmap, ($ind >> 3);
106       }
107     };
108     substr($bitmap, ($ind >> 3), 1) = chr($current | ($set << ($ind & 7)));
109
110     push @selected, $name[$ind] if $set;
111   } while (++$ind < $lastregop);
112   my $out_string = join ', ', @selected, 0;
113   $out_string =~ s/(.{1,70},) /$1\n    /g;
114
115   my $out_mask = join ', ', map {sprintf "0x%02X", ord $_} split '', $bitmap;
116
117   return $comment . <<"EOP";
118 #define REGNODE_\U$varname\E(node) (PL_${varname}_bitmask[(node) >> 3] & (1 << ((node) & 7)))
119
120 #ifndef DOINIT
121 EXTCONST U8 PL_${varname}\[] __attribute__deprecated__;
122 #else
123 EXTCONST U8 PL_${varname}\[] __attribute__deprecated__ = {
124     $out_string
125 };
126 #endif /* DOINIT */
127
128 #ifndef DOINIT
129 EXTCONST U8 PL_${varname}_bitmask[];
130 #else
131 EXTCONST U8 PL_${varname}_bitmask[] = {
132     $out_mask
133 };
134 #endif /* DOINIT */
135 EOP
136 }
137
138 my $out = open_new('regnodes.h', '>',
139                    { by => 'regen/regcomp.pl', from => 'regcomp.sym' });
140 printf $out <<EOP,
141 /* Regops and State definitions */
142
143 #define %*s\t%d
144 #define %*s\t%d
145
146 EOP
147     -$width, REGNODE_MAX        => $lastregop - 1,
148     -$width, REGMATCH_STATE_MAX => $tot - 1
149 ;
150
151
152 for ($ind=0; $ind < $lastregop ; ++$ind) {
153   printf $out "#define\t%*s\t%d\t/* %#04x %s */\n",
154     -$width, $name[$ind], $ind, $ind, $rest[$ind];
155 }
156 print $out "\t/* ------------ States ------------- */\n";
157 for ( ; $ind < $tot ; $ind++) {
158   printf $out "#define\t%*s\t(REGNODE_MAX + %d)\t/* %s */\n",
159     -$width, $name[$ind], $ind - $lastregop + 1, $rest[$ind];
160 }
161
162 print $out <<EOP;
163
164 /* PL_regkind[] What type of regop or state is this. */
165
166 #ifndef DOINIT
167 EXTCONST U8 PL_regkind[];
168 #else
169 EXTCONST U8 PL_regkind[] = {
170 EOP
171
172 $ind = 0;
173 do {
174   printf $out "\t%*s\t/* %*s */\n",
175              -1-$twidth, "$type[$ind],", -$width, $name[$ind];
176   print $out "\t/* ------------ States ------------- */\n"
177     if $ind + 1 == $lastregop and $lastregop != $tot;
178 } while (++$ind < $tot);
179
180 print $out <<EOP;
181 };
182 #endif
183
184 /* regarglen[] - How large is the argument part of the node (in regnodes) */
185
186 #ifdef REG_COMP_C
187 static const U8 regarglen[] = {
188 EOP
189
190 $ind = 0;
191 do {
192   my $size = 0;
193   $size = "EXTRA_SIZE(struct regnode_$args[$ind])" if $args[$ind];
194   
195   printf $out "\t%*s\t/* %*s */\n",
196         -37, "$size,",-$rwidth,$name[$ind];
197 } while (++$ind < $lastregop);
198
199 print $out <<EOP;
200 };
201
202 /* reg_off_by_arg[] - Which argument holds the offset to the next node */
203
204 static const char reg_off_by_arg[] = {
205 EOP
206
207 $ind = 0;
208 do {
209   my $size = $longj[$ind] || 0;
210
211   printf $out "\t%d,\t/* %*s */\n",
212         $size, -$rwidth, $name[$ind]
213 } while (++$ind < $lastregop);
214
215 print $out <<EOP;
216 };
217
218 #endif /* REG_COMP_C */
219
220 /* reg_name[] - Opcode/state names in string form, for debugging */
221
222 #ifndef DOINIT
223 EXTCONST char * PL_reg_name[];
224 #else
225 EXTCONST char * const PL_reg_name[] = {
226 EOP
227
228 $ind = 0;
229 my $ofs = 0;
230 my $sym = "";
231 do {
232   my $size = $longj[$ind] || 0;
233
234   printf $out "\t%*s\t/* $sym%#04x */\n",
235         -3-$width,qq("$name[$ind]",), $ind - $ofs;
236   if ($ind + 1 == $lastregop and $lastregop != $tot) {
237     print $out "\t/* ------------ States ------------- */\n";
238     $ofs = $lastregop - 1;
239     $sym = 'REGNODE_MAX +';
240   }
241     
242 } while (++$ind < $tot);
243
244 print $out <<EOP;
245 };
246 #endif /* DOINIT */
247
248 EOP
249
250 {
251 print $out <<EOP;
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[] = {
258 EOP
259
260 my %rxfv;
261 my %definitions;    # Remember what the symbol definitions are
262 my $val = 0;
263 my %reverse;
264 my $REG_EXTFLAGS_NAME_SIZE = 0;
265 foreach my $file ("op_reg_common.h", "regexp.h") {
266     open FH,"<$file" or die "Can't read $file: $!";
267     while (<FH>) {
268
269         # optional leading '_'.  Return symbol in $1, and strip it from
270         # rest of line
271         if (s/^ \# \s* define \s+ ( _? RXf_ \w+ ) \s+ //xi) {
272             chomp;
273             my $define = $1;
274             my $orig= $_;
275             s{ /\* .*? \*/ }{ }x;    # Replace comments by a blank
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             }
281
282             # Remove the U suffix from unsigned int literals
283             s/\b([0-9]+)U\b/$1/g;
284
285             my $newval = eval $_;   # Get numeric definition
286
287             $definitions{$define} = $newval;
288
289             next unless $_ =~ /<</; # Bit defines use left shift
290             if($val & $newval) {
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;
297             }
298             $val|=$newval;
299             $rxfv{$define}= $newval;
300             $reverse{$newval} = $define;
301         }
302     }
303 }
304 my %vrxf=reverse %rxfv;
305 printf $out "\t/* Bits in extflags defined: %s */\n", unpack 'B*', pack 'N', $val;
306 my %multibits;
307 for (0..31) {
308     my $power_of_2 = 2**$_;
309     my $n=$vrxf{$power_of_2};
310     my $extra = "";
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) {
325                     $n = $name . ( $multibits{$name}++ );
326                     $extra= sprintf qq{ : "%s" - 0x%08x}, $name, $rxfv{$name}
327                         if $power_of_2 != $rxfv{$name};
328                     last;
329                 }
330             }
331         }
332     }
333     s/\bRXf_(PMf_)?// for $n, $extra;
334     printf $out qq(\t%-20s/* 0x%08x%s */\n),
335         qq("$n",),$power_of_2, $extra;
336     $REG_EXTFLAGS_NAME_SIZE++;
337 }  
338  
339 print $out <<EOP;
340 };
341 #endif /* DOINIT */
342
343 EOP
344 print $out <<EOQ
345 #ifdef DEBUGGING
346 #  define REG_EXTFLAGS_NAME_SIZE $REG_EXTFLAGS_NAME_SIZE
347 #endif
348
349 EOQ
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;
365 my $REG_INTFLAGS_NAME_SIZE = 0;
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;
381             $REG_INTFLAGS_NAME_SIZE++;
382         }
383     }
384 }
385
386 print $out <<EOP;
387 };
388 #endif /* DOINIT */
389
390 EOP
391 print $out <<EOQ;
392 #ifdef DEBUGGING
393 #  define REG_INTFLAGS_NAME_SIZE $REG_INTFLAGS_NAME_SIZE
394 #endif
395
396 EOQ
397 }
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');
404
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
409 read_only_bottom_close_and_rename($out);
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[\$_]
420  ^$name_fmt ^<<<<<<<<< ^$descr_fmt~~
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);