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