Commit | Line | Data |
---|---|---|
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 |
16 | BEGIN { |
17 | # Get function prototypes | |
9ad884cb | 18 | require 'regen_lib.pl'; |
36bb303b | 19 | } |
d09b2d29 | 20 | #use Fatal qw(open close rename chmod unlink); |
03363afd YO |
21 | use strict; |
22 | use warnings; | |
23 | ||
d09b2d29 | 24 | open DESC, 'regcomp.sym'; |
d09b2d29 | 25 | |
03363afd | 26 | my $ind = 0; |
f9ef50a7 | 27 | my (@name,@rest,@type,@code,@args,@flags,@longj); |
03363afd | 28 | my ($desc,$lastregop); |
d09b2d29 | 29 | while (<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. | |
75 | my ($width,$rwidth,$twidth)=(22,12,9); | |
03363afd YO |
76 | $lastregop ||= $ind; |
77 | my $tot = $ind; | |
d09b2d29 | 78 | close DESC; |
03363afd YO |
79 | die "Too many regexp/state opcodes! Maximum is 256, but there are $lastregop in file!" |
80 | if $lastregop>256; | |
d09b2d29 | 81 | |
f9ef50a7 NC |
82 | sub 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 | 113 | EXTCONST U8 PL_${varname}[] __attribute__deprecated__; |
f9ef50a7 | 114 | #else |
ded4dd2a | 115 | EXTCONST U8 PL_${varname}[] __attribute__deprecated__ = { |
f9ef50a7 NC |
116 | $out_string |
117 | }; | |
118 | #endif /* DOINIT */ | |
119 | ||
ded4dd2a NC |
120 | #ifndef DOINIT |
121 | EXTCONST U8 PL_${varname}_bitmask[]; | |
122 | #else | |
123 | EXTCONST U8 PL_${varname}_bitmask[] = { | |
124 | $out_mask | |
125 | }; | |
126 | #endif /* DOINIT */ | |
127 | ||
f9ef50a7 NC |
128 | EOP |
129 | } | |
130 | ||
266db279 | 131 | my $tmp_h = 'regnodes.h-new'; |
d09b2d29 IZ |
132 | |
133 | unlink $tmp_h if -f $tmp_h; | |
134 | ||
424a4936 | 135 | my $out = safer_open($tmp_h); |
d09b2d29 | 136 | |
424a4936 | 137 | printf $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 | 149 | EOP |
f9f4320a YO |
150 | -$width, REGNODE_MAX => $lastregop - 1, |
151 | -$width, REGMATCH_STATE_MAX => $tot - 1 | |
152 | ; | |
d09b2d29 | 153 | |
24b23f37 | 154 | |
93882df0 | 155 | for ($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 | 159 | print $out "\t/* ------------ States ------------- */\n"; |
93882df0 | 160 | for ( ; $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 | 165 | print $out <<EOP; |
03363afd | 166 | |
6bda09f9 | 167 | /* PL_regkind[] What type of regop or state is this. */ |
d09b2d29 IZ |
168 | |
169 | #ifndef DOINIT | |
22c35a8c | 170 | EXTCONST U8 PL_regkind[]; |
d09b2d29 | 171 | #else |
22c35a8c | 172 | EXTCONST U8 PL_regkind[] = { |
d09b2d29 IZ |
173 | EOP |
174 | ||
175 | $ind = 0; | |
93882df0 | 176 | do { |
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 | 183 | print $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 | 190 | static const U8 regarglen[] = { |
d09b2d29 IZ |
191 | EOP |
192 | ||
193 | $ind = 0; | |
93882df0 | 194 | do { |
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 | 202 | print $out <<EOP; |
d09b2d29 IZ |
203 | }; |
204 | ||
6bda09f9 YO |
205 | /* reg_off_by_arg[] - Which argument holds the offset to the next node */ |
206 | ||
29de9391 | 207 | static const char reg_off_by_arg[] = { |
d09b2d29 IZ |
208 | EOP |
209 | ||
210 | $ind = 0; | |
93882df0 | 211 | do { |
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 | 218 | print $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 | 226 | EXTCONST char * PL_reg_name[]; |
22429478 | 227 | #else |
4764e399 | 228 | EXTCONST char * const PL_reg_name[] = { |
9b155405 IZ |
229 | EOP |
230 | ||
231 | $ind = 0; | |
93882df0 | 232 | my $ofs = 0; |
24b23f37 | 233 | my $sym = ""; |
93882df0 | 234 | do { |
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 | 247 | print $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 | |
254 | EXTCONST char * PL_reg_extflags_name[]; | |
255 | #else | |
256 | EXTCONST char * const PL_reg_extflags_name[] = { | |
d09b2d29 IZ |
257 | EOP |
258 | ||
f7819f85 A |
259 | open my $fh,"<","regexp.h" or die "Can't read regexp.h: $!"; |
260 | my %rxfv; | |
c8e4cf8b NC |
261 | my $val = 0; |
262 | my %reverse; | |
f7819f85 A |
263 | while (<$fh>) { |
264 | if (/#define\s+(RXf_\w+)\s+(0x[A-F\d]+)/i) { | |
c8e4cf8b NC |
265 | my $newval = eval $2; |
266 | if($val & $newval) { | |
267 | die sprintf "Both $1 and $reverse{$newval} use %08X", $newval; | |
268 | } | |
269 | $val|=$newval; | |
270 | $rxfv{$1}= $newval; | |
271 | $reverse{$newval} = $1; | |
f7819f85 A |
272 | } |
273 | } | |
274 | my %vrxf=reverse %rxfv; | |
424a4936 | 275 | printf $out "\t/* Bits in extflags defined: %032b */\n",$val; |
f7819f85 A |
276 | for (0..31) { |
277 | my $n=$vrxf{2**$_}||"UNUSED_BIT_$_"; | |
278 | $n=~s/^RXf_(PMf_)?//; | |
424a4936 | 279 | printf $out qq(\t%-20s/* 0x%08x */\n), |
f7819f85 A |
280 | qq("$n",),2**$_; |
281 | } | |
282 | ||
424a4936 | 283 | print $out <<EOP; |
f7819f85 A |
284 | }; |
285 | #endif /* DOINIT */ | |
286 | ||
f9ef50a7 NC |
287 | EOP |
288 | ||
289 | print $out process_flags('V', 'varies', <<'EOC'); | |
290 | /* The following have no fixed length. U8 so we can do strchr() on it. */ | |
291 | EOC | |
292 | ||
293 | print $out process_flags('S', 'simple', <<'EOC'); | |
294 | /* The following always have a length of 1. U8 we can do strchr() on it. */ | |
295 | /* (Note that length 1 means "one character" under UTF8, not "one octet".) */ | |
296 | EOC | |
297 | ||
298 | print $out <<EOP; | |
f7819f85 A |
299 | /* ex: set ro: */ |
300 | EOP | |
08858ed2 | 301 | safer_close($out); |
d09b2d29 | 302 | |
424a4936 | 303 | rename_if_different $tmp_h, 'regnodes.h'; |