This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Refactor common parts of op.h, regexp.h into new .h
[perl5.git] / regcomp.pl
CommitLineData
6294c161
DM
1#!/usr/bin/perl
2#
3# Regenerate (overwriting only if changed):
4#
5# regnodes.h
6#
7# from information stored in
8#
9# regcomp.sym
10# regexp.h
11#
12# Accepts the standard regen_lib -q and -v args.
13#
14# This script is normally invoked from regen.pl.
15
36bb303b
NC
16BEGIN {
17 # Get function prototypes
9ad884cb 18 require 'regen_lib.pl';
36bb303b 19}
d09b2d29 20#use Fatal qw(open close rename chmod unlink);
03363afd
YO
21use strict;
22use warnings;
23
d09b2d29 24open DESC, 'regcomp.sym';
d09b2d29 25
03363afd 26my $ind = 0;
f9ef50a7 27my (@name,@rest,@type,@code,@args,@flags,@longj);
03363afd 28my ($desc,$lastregop);
d09b2d29 29while (<DESC>) {
03363afd
YO
30 s/#.*$//;
31 next if /^\s*$/;
32 s/\s*\z//;
33 if (/^-+\s*$/) {
34 $lastregop= $ind;
35 next;
36 }
37 unless ($lastregop) {
f8abb37e 38 ($name[$ind], $desc, $rest[$ind]) = /^(\S+)\s+([^\t]+)\s*;\s*(.*)/;
f9ef50a7
NC
39 ($type[$ind], $code[$ind], $args[$ind], $flags[$ind], $longj[$ind])
40 = split /[,\s]\s*/, $desc;
93882df0 41 ++$ind;
03363afd 42 } else {
f8abb37e 43 my ($type,@lists)=split /\s+/, $_;
03363afd
YO
44 die "No list? $type" if !@lists;
45 foreach my $list (@lists) {
46 my ($names,$special)=split /:/, $list , 2;
47 $special ||= "";
48 foreach my $name (split /,/,$names) {
49 my $real= $name eq 'resume'
50 ? "resume_$type"
51 : "${type}_$name";
52 my @suffix;
53 if (!$special) {
54 @suffix=("");
55 } elsif ($special=~/\d/) {
56 @suffix=(1..$special);
57 } elsif ($special eq 'FAIL') {
58 @suffix=("","_fail");
59 } else {
60 die "unknown :type ':$special'";
61 }
62 foreach my $suffix (@suffix) {
03363afd
YO
63 $name[$ind]="$real$suffix";
64 $type[$ind]=$type;
24b23f37 65 $rest[$ind]="state for $type";
93882df0 66 ++$ind;
03363afd
YO
67 }
68 }
69 }
70
71 }
72}
5d458dd8
YO
73# use fixed width to keep the diffs between regcomp.pl recompiles
74# as small as possible.
75my ($width,$rwidth,$twidth)=(22,12,9);
03363afd
YO
76$lastregop ||= $ind;
77my $tot = $ind;
d09b2d29 78close DESC;
03363afd
YO
79die "Too many regexp/state opcodes! Maximum is 256, but there are $lastregop in file!"
80 if $lastregop>256;
d09b2d29 81
f9ef50a7
NC
82sub process_flags {
83 my ($flag, $varname, $comment) = @_;
84 $comment = '' unless defined $comment;
85
86 $ind = 0;
87 my @selected;
ded4dd2a 88 my $bitmap = '';
93882df0 89 do {
ded4dd2a
NC
90 my $set = $flags[$ind] && $flags[$ind] eq $flag ? 1 : 0;
91 # Whilst I could do this with vec, I'd prefer to do longhand the arithmetic
92 # ops in the C code.
93 my $current = do {
94 no warnings 'uninitialized';
95 ord do {
96 no warnings 'substr';
97 substr $bitmap, ($ind >> 3);
98 }
99 };
100 substr $bitmap, ($ind >> 3), 1, chr($current | ($set << ($ind & 7)));
101
102 push @selected, $name[$ind] if $set;
93882df0 103 } while (++$ind < $lastregop);
f9ef50a7
NC
104 my $out_string = join ', ', @selected, 0;
105 $out_string =~ s/(.{1,70},) /$1\n /g;
ded4dd2a
NC
106
107 my $out_mask = join ', ', map {sprintf "0x%02X", ord $_} split '', $bitmap;
108
f9ef50a7 109 return $comment . <<"EOP";
ded4dd2a 110#define REGNODE_\U$varname\E(node) (PL_${varname}_bitmask[(node) >> 3] & (1 << ((node) & 7)))
e52fc539 111
f9ef50a7 112#ifndef DOINIT
ded4dd2a 113EXTCONST U8 PL_${varname}[] __attribute__deprecated__;
f9ef50a7 114#else
ded4dd2a 115EXTCONST U8 PL_${varname}[] __attribute__deprecated__ = {
f9ef50a7
NC
116 $out_string
117};
118#endif /* DOINIT */
119
ded4dd2a
NC
120#ifndef DOINIT
121EXTCONST U8 PL_${varname}_bitmask[];
122#else
123EXTCONST U8 PL_${varname}_bitmask[] = {
124 $out_mask
125};
126#endif /* DOINIT */
127
f9ef50a7
NC
128EOP
129}
130
266db279 131my $tmp_h = 'regnodes.h-new';
d09b2d29
IZ
132
133unlink $tmp_h if -f $tmp_h;
134
424a4936 135my $out = safer_open($tmp_h);
d09b2d29 136
424a4936 137printf $out <<EOP,
37442d52
RGS
138/* -*- buffer-read-only: t -*-
139 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
9b155405 140 This file is built by regcomp.pl from regcomp.sym.
d09b2d29
IZ
141 Any changes made here will be lost!
142*/
143
6bda09f9
YO
144/* Regops and State definitions */
145
03363afd
YO
146#define %*s\t%d
147#define %*s\t%d
148
d09b2d29 149EOP
f9f4320a
YO
150 -$width, REGNODE_MAX => $lastregop - 1,
151 -$width, REGMATCH_STATE_MAX => $tot - 1
152;
d09b2d29 153
24b23f37 154
93882df0 155for ($ind=0; $ind < $lastregop ; ++$ind) {
424a4936 156 printf $out "#define\t%*s\t%d\t/* %#04x %s */\n",
93882df0 157 -$width, $name[$ind], $ind, $ind, $rest[$ind];
24b23f37 158}
424a4936 159print $out "\t/* ------------ States ------------- */\n";
93882df0 160for ( ; $ind < $tot ; $ind++) {
424a4936 161 printf $out "#define\t%*s\t(REGNODE_MAX + %d)\t/* %s */\n",
93882df0 162 -$width, $name[$ind], $ind - $lastregop + 1, $rest[$ind];
d09b2d29
IZ
163}
164
424a4936 165print $out <<EOP;
03363afd 166
6bda09f9 167/* PL_regkind[] What type of regop or state is this. */
d09b2d29
IZ
168
169#ifndef DOINIT
22c35a8c 170EXTCONST U8 PL_regkind[];
d09b2d29 171#else
22c35a8c 172EXTCONST U8 PL_regkind[] = {
d09b2d29
IZ
173EOP
174
175$ind = 0;
93882df0 176do {
424a4936 177 printf $out "\t%*s\t/* %*s */\n",
03363afd 178 -1-$twidth, "$type[$ind],", -$width, $name[$ind];
424a4936 179 print $out "\t/* ------------ States ------------- */\n"
93882df0
NC
180 if $ind + 1 == $lastregop and $lastregop != $tot;
181} while (++$ind < $tot);
d09b2d29 182
424a4936 183print $out <<EOP;
d09b2d29
IZ
184};
185#endif
186
6bda09f9 187/* regarglen[] - How large is the argument part of the node (in regnodes) */
d09b2d29
IZ
188
189#ifdef REG_COMP_C
29de9391 190static const U8 regarglen[] = {
d09b2d29
IZ
191EOP
192
193$ind = 0;
93882df0 194do {
03363afd 195 my $size = 0;
d09b2d29
IZ
196 $size = "EXTRA_SIZE(struct regnode_$args[$ind])" if $args[$ind];
197
424a4936 198 printf $out "\t%*s\t/* %*s */\n",
03363afd 199 -37, "$size,",-$rwidth,$name[$ind];
93882df0 200} while (++$ind < $lastregop);
d09b2d29 201
424a4936 202print $out <<EOP;
d09b2d29
IZ
203};
204
6bda09f9
YO
205/* reg_off_by_arg[] - Which argument holds the offset to the next node */
206
29de9391 207static const char reg_off_by_arg[] = {
d09b2d29
IZ
208EOP
209
210$ind = 0;
93882df0 211do {
03363afd 212 my $size = $longj[$ind] || 0;
9b155405 213
424a4936 214 printf $out "\t%d,\t/* %*s */\n",
03363afd 215 $size, -$rwidth, $name[$ind]
93882df0 216} while (++$ind < $lastregop);
d09b2d29 217
424a4936 218print $out <<EOP;
d09b2d29 219};
9b155405 220
13d6edb4
NC
221#endif /* REG_COMP_C */
222
6bda09f9
YO
223/* reg_name[] - Opcode/state names in string form, for debugging */
224
22429478 225#ifndef DOINIT
13d6edb4 226EXTCONST char * PL_reg_name[];
22429478 227#else
4764e399 228EXTCONST char * const PL_reg_name[] = {
9b155405
IZ
229EOP
230
231$ind = 0;
93882df0 232my $ofs = 0;
24b23f37 233my $sym = "";
93882df0 234do {
03363afd 235 my $size = $longj[$ind] || 0;
9b155405 236
424a4936 237 printf $out "\t%*s\t/* $sym%#04x */\n",
24b23f37 238 -3-$width,qq("$name[$ind]",), $ind - $ofs;
93882df0 239 if ($ind + 1 == $lastregop and $lastregop != $tot) {
424a4936 240 print $out "\t/* ------------ States ------------- */\n";
93882df0 241 $ofs = $lastregop - 1;
24b23f37
YO
242 $sym = 'REGNODE_MAX +';
243 }
244
93882df0 245} while (++$ind < $tot);
9b155405 246
424a4936 247print $out <<EOP;
9b155405 248};
22429478 249#endif /* DOINIT */
d09b2d29 250
f7819f85
A
251/* PL_reg_extflags_name[] - Opcode/state names in string form, for debugging */
252
253#ifndef DOINIT
254EXTCONST char * PL_reg_extflags_name[];
255#else
256EXTCONST char * const PL_reg_extflags_name[] = {
d09b2d29
IZ
257EOP
258
f7819f85 259my %rxfv;
6a080ccd 260my %definitions; # Remember what the symbol definitions are
c8e4cf8b
NC
261my $val = 0;
262my %reverse;
1850c8f9
KW
263foreach my $file ("op_reg_common.h", "regexp.h") {
264 open my $fh,"<", $file or die "Can't read $file: $!";
265 while (<$fh>) {
266
267 # optional leading '_'. Return symbol in $1, and strip it from
268 # rest of line
269 if (s/ \#define \s+ ( _? RXf_ \w+ ) \s+ //xi) {
270 chomp;
271 my $define = $1;
272 s: / \s* \* .*? \* \s* / : :x; # Replace comments by a blank
273
274 # Replace any prior defined symbols by their values
275 foreach my $key (keys %definitions) {
276 s/\b$key\b/$definitions{$key}/g;
277 }
278 my $newval = eval $_; # Get numeric definition
6a080ccd 279
1850c8f9 280 $definitions{$define} = $newval;
6a080ccd 281
1850c8f9
KW
282 next unless $_ =~ /<</; # Bit defines use left shift
283 if($val & $newval) {
284 die sprintf "Both $define and $reverse{$newval} use %08X", $newval;
285 }
286 $val|=$newval;
287 $rxfv{$define}= $newval;
288 $reverse{$newval} = $define;
6a080ccd 289 }
f7819f85 290 }
1850c8f9 291}
f7819f85 292my %vrxf=reverse %rxfv;
424a4936 293printf $out "\t/* Bits in extflags defined: %032b */\n",$val;
f7819f85
A
294for (0..31) {
295 my $n=$vrxf{2**$_}||"UNUSED_BIT_$_";
296 $n=~s/^RXf_(PMf_)?//;
424a4936 297 printf $out qq(\t%-20s/* 0x%08x */\n),
f7819f85
A
298 qq("$n",),2**$_;
299}
300
424a4936 301print $out <<EOP;
f7819f85
A
302};
303#endif /* DOINIT */
304
f9ef50a7
NC
305EOP
306
307print $out process_flags('V', 'varies', <<'EOC');
308/* The following have no fixed length. U8 so we can do strchr() on it. */
309EOC
310
311print $out process_flags('S', 'simple', <<'EOC');
312/* The following always have a length of 1. U8 we can do strchr() on it. */
313/* (Note that length 1 means "one character" under UTF8, not "one octet".) */
314EOC
315
316print $out <<EOP;
f7819f85
A
317/* ex: set ro: */
318EOP
08858ed2 319safer_close($out);
d09b2d29 320
424a4936 321rename_if_different $tmp_h, 'regnodes.h';