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