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