3 # Regenerate (overwriting only if changed):
8 # from information stored in
13 # pod/perldebguts.pod is not completely regenerated. Only the table of
14 # regexp nodes is replaced; other parts remain unchanged.
16 # Accepts the standard regen_lib -q and -v args.
18 # This script is normally invoked from regen.pl.
21 # Get function prototypes
22 require 'regen/regen_lib.pl';
26 open DESC, 'regcomp.sym';
29 my (@name,@rest,@type,@code,@args,@flags,@longj,@cmnt);
30 my ($longest_name_length,$desc,$lastregop) = 0;
31 my (%seen_op, %type_alias);
33 # Special pod comments
34 if (/^#\* ?/) { $cmnt[$ind] .= "# $'"; }
35 # Truly blank lines possibly surrounding pod comments
36 elsif (/^\s*$/) { $cmnt[$ind] .= "\n" }
38 next if /^(?:#|\s*$)/;
39 chomp; # No \z in 5.004
46 ($name[$ind], $desc, $rest[$ind]) = /^(\S+)\s+([^\t]+?)\s*;\s*(.*)/;
48 if (defined $seen_op{$name[$ind]}) {
49 die "Duplicate regop $name[$ind] in regcomp.sym line $. previously defined on line $seen_op{$name[$ind]}\n";
51 $seen_op{$name[$ind]}= $.;
54 ($type[$ind], $code[$ind], $args[$ind], $flags[$ind], $longj[$ind])
55 = split /[,\s]\s*/, $desc;
57 if (!defined $seen_op{$type[$ind]} and !defined $type_alias{$type[$ind]}) {
58 #warn "Regop type '$type[$ind]' from regcomp.sym line $. is not an existing regop, and will be aliased to $name[$ind]\n"
60 $type_alias{$type[$ind]}= $name[$ind];
63 $longest_name_length = length $name[$ind]
64 if length $name[$ind] > $longest_name_length;
67 my ($type,@lists)=split /\s+/, $_;
68 die "No list? $type" if !@lists;
69 foreach my $list (@lists) {
70 my ($names,$special)=split /:/, $list , 2;
72 foreach my $name (split /,/,$names) {
73 my $real= $name eq 'resume'
79 } elsif ($special=~/\d/) {
80 @suffix=(1..$special);
81 } elsif ($special eq 'FAIL') {
84 die "unknown :type ':$special'";
86 foreach my $suffix (@suffix) {
87 $name[$ind]="$real$suffix";
89 $rest[$ind]="state for $type";
97 # use fixed width to keep the diffs between regcomp.pl recompiles
98 # as small as possible.
99 my ($width,$rwidth,$twidth)=(22,12,9);
103 die "Too many regexp/state opcodes! Maximum is 256, but there are $lastregop in file!"
107 my ($flag, $varname, $comment) = @_;
108 $comment = '' unless defined $comment;
114 my $set = $flags[$ind] && $flags[$ind] eq $flag ? 1 : 0;
115 # Whilst I could do this with vec, I'd prefer to do longhand the arithmetic
120 substr $bitmap, ($ind >> 3);
123 substr($bitmap, ($ind >> 3), 1) = chr($current | ($set << ($ind & 7)));
125 push @selected, $name[$ind] if $set;
126 } while (++$ind < $lastregop);
127 my $out_string = join ', ', @selected, 0;
128 $out_string =~ s/(.{1,70},) /$1\n /g;
130 my $out_mask = join ', ', map {sprintf "0x%02X", ord $_} split '', $bitmap;
132 return $comment . <<"EOP";
133 #define REGNODE_\U$varname\E(node) (PL_${varname}_bitmask[(node) >> 3] & (1 << ((node) & 7)))
136 EXTCONST U8 PL_${varname}\[] __attribute__deprecated__;
138 EXTCONST U8 PL_${varname}\[] __attribute__deprecated__ = {
144 EXTCONST U8 PL_${varname}_bitmask[];
146 EXTCONST U8 PL_${varname}_bitmask[] = {
153 my $out = open_new('regnodes.h', '>',
154 { by => 'regen/regcomp.pl', from => 'regcomp.sym' });
156 /* Regops and State definitions */
162 -$width, REGNODE_MAX => $lastregop - 1,
163 -$width, REGMATCH_STATE_MAX => $tot - 1
166 my %rev_type_alias= reverse %type_alias;
167 for ($ind=0; $ind < $lastregop ; ++$ind) {
168 printf $out "#define\t%*s\t%d\t/* %#04x %s */\n",
169 -$width, $name[$ind], $ind, $ind, $rest[$ind];
170 if (defined(my $alias= $rev_type_alias{$name[$ind]})) {
171 printf $out "#define\t%*s\t%d\t/* %#04x %s */\n",
172 -$width, $alias, $ind, $ind, "type alias";
176 print $out "\t/* ------------ States ------------- */\n";
177 for ( ; $ind < $tot ; $ind++) {
178 printf $out "#define\t%*s\t(REGNODE_MAX + %d)\t/* %s */\n",
179 -$width, $name[$ind], $ind - $lastregop + 1, $rest[$ind];
184 /* PL_regkind[] What type of regop or state is this. */
187 EXTCONST U8 PL_regkind[];
189 EXTCONST U8 PL_regkind[] = {
194 printf $out "\t%*s\t/* %*s */\n",
195 -1-$twidth, "$type[$ind],", -$width, $name[$ind];
196 print $out "\t/* ------------ States ------------- */\n"
197 if $ind + 1 == $lastregop and $lastregop != $tot;
198 } while (++$ind < $tot);
204 /* regarglen[] - How large is the argument part of the node (in regnodes) */
207 static const U8 regarglen[] = {
213 $size = "EXTRA_SIZE(struct regnode_$args[$ind])" if $args[$ind];
215 printf $out "\t%*s\t/* %*s */\n",
216 -37, "$size,",-$rwidth,$name[$ind];
217 } while (++$ind < $lastregop);
222 /* reg_off_by_arg[] - Which argument holds the offset to the next node */
224 static const char reg_off_by_arg[] = {
229 my $size = $longj[$ind] || 0;
231 printf $out "\t%d,\t/* %*s */\n",
232 $size, -$rwidth, $name[$ind]
233 } while (++$ind < $lastregop);
238 #endif /* REG_COMP_C */
240 /* reg_name[] - Opcode/state names in string form, for debugging */
243 EXTCONST char * PL_reg_name[];
245 EXTCONST char * const PL_reg_name[] = {
252 my $size = $longj[$ind] || 0;
254 printf $out "\t%*s\t/* $sym%#04x */\n",
255 -3-$width,qq("$name[$ind]",), $ind - $ofs;
256 if ($ind + 1 == $lastregop and $lastregop != $tot) {
257 print $out "\t/* ------------ States ------------- */\n";
258 $ofs = $lastregop - 1;
259 $sym = 'REGNODE_MAX +';
262 } while (++$ind < $tot);
272 /* PL_reg_extflags_name[] - Opcode/state names in string form, for debugging */
275 EXTCONST char * PL_reg_extflags_name[];
277 EXTCONST char * const PL_reg_extflags_name[] = {
281 my %definitions; # Remember what the symbol definitions are
284 my $REG_EXTFLAGS_NAME_SIZE = 0;
285 foreach my $file ("op_reg_common.h", "regexp.h") {
286 open FH,"<$file" or die "Can't read $file: $!";
289 # optional leading '_'. Return symbol in $1, and strip it from
291 if (s/^ \# \s* define \s+ ( _? RXf_ \w+ ) \s+ //xi) {
295 s{ /\* .*? \*/ }{ }x; # Replace comments by a blank
297 # Replace any prior defined symbols by their values
298 foreach my $key (keys %definitions) {
299 s/\b$key\b/$definitions{$key}/g;
302 # Remove the U suffix from unsigned int literals
303 s/\b([0-9]+)U\b/$1/g;
305 my $newval = eval $_; # Get numeric definition
307 $definitions{$define} = $newval;
309 next unless $_ =~ /<</; # Bit defines use left shift
311 my @names=($define, $reverse{$newval});
313 if ($names[0] ne $names[1]) {
314 die sprintf "ERROR: both $define and $reverse{$newval} use 0x%08X (%s:%s)", $newval, $orig, $_;
319 $rxfv{$define}= $newval;
320 $reverse{$newval} = $define;
324 my %vrxf=reverse %rxfv;
325 printf $out "\t/* Bits in extflags defined: %s */\n", unpack 'B*', pack 'N', $val;
328 my $power_of_2 = 2**$_;
329 my $n=$vrxf{$power_of_2};
333 # Here, there was no name that matched exactly the bit. It could be
334 # either that it is unused, or the name matches multiple bits.
335 if (! ($val & $power_of_2)) {
336 $n = "UNUSED_BIT_$_";
340 # Here, must be because it matches multiple bits. Look through
341 # all possibilities until find one that matches this one. Use
342 # that name, and all the bits it matches
343 foreach my $name (keys %rxfv) {
344 if ($rxfv{$name} & $power_of_2) {
345 $n = $name . ( $multibits{$name}++ );
346 $extra= sprintf qq{ : "%s" - 0x%08x}, $name, $rxfv{$name}
347 if $power_of_2 != $rxfv{$name};
353 s/\bRXf_(PMf_)?// for $n, $extra;
354 printf $out qq(\t%-20s/* 0x%08x%s */\n),
355 qq("$n",),$power_of_2, $extra;
356 $REG_EXTFLAGS_NAME_SIZE++;
366 # define REG_EXTFLAGS_NAME_SIZE $REG_EXTFLAGS_NAME_SIZE
373 /* PL_reg_intflags_name[] - Opcode/state names in string form, for debugging */
376 EXTCONST char * PL_reg_intflags_name[];
378 EXTCONST char * const PL_reg_intflags_name[] = {
382 my %definitions; # Remember what the symbol definitions are
385 my $REG_INTFLAGS_NAME_SIZE = 0;
386 foreach my $file ("regcomp.h") {
387 open my $fh, "<", $file or die "Can't read $file: $!";
389 # optional leading '_'. Return symbol in $1, and strip it from
391 if (m/^ \# \s* define \s+ ( PREGf_ ( \w+ ) ) \s+ 0x([0-9a-f]+)(?:\s*\/\*(.*)\*\/)?/xi) {
398 $comment= $comment ? " - $comment" : "";
400 printf $out qq(\t%-30s/* 0x%08x - %s%s */\n), qq("$abbr",), $val, $define, $comment;
401 $REG_INTFLAGS_NAME_SIZE++;
413 # define REG_INTFLAGS_NAME_SIZE $REG_INTFLAGS_NAME_SIZE
419 print $out process_flags('V', 'varies', <<'EOC');
420 /* The following have no fixed length. U8 so we can do strchr() on it. */
423 print $out process_flags('S', 'simple', <<'EOC');
425 /* The following always have a length of 1. U8 we can do strchr() on it. */
426 /* (Note that length 1 means "one character" under UTF8, not "one octet".) */
429 read_only_bottom_close_and_rename($out);
431 my $guts = open_new('pod/perldebguts.pod', '>');
434 my $name_fmt = '<' x ($longest_name_length-1);
435 my $descr_fmt = '<' x (58-$longest_name_length);
440 ^$name_fmt ^<<<<<<<<< ^$descr_fmt~~
441 \$name[\$_], \$code, \$rest[\$_]
445 select +(select($guts), do {
448 open my $oldguts, "pod/perldebguts.pod"
449 or die "$0 cannot open pod/perldebguts.pod for reading: $!";
452 last if /=for regcomp.pl begin/;
457 # TYPE arg-description [num-args] [longjump-len] DESCRIPTION
459 for (0..$lastregop-1) {
460 $code = "$code[$_] ".($args[$_]||"");
461 $code .= " $longj[$_]" if $longj[$_];
462 if ($cmnt[$_] ||= "") {
463 # Trim multiple blanks
464 $cmnt[$_] =~ s/^\n\n+/\n/; $cmnt[$_] =~ s/\n\n+$/\n\n/
471 last if /=for regcomp.pl end/;
473 do { print } while <$oldguts>;
477 close_and_rename($guts);