Change regcomp.pl to 0-based indexing for its arrays and loops.
[p5sagit/p5-mst-13.2.git] / regcomp.pl
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
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 use warnings;
23
24 open DESC, 'regcomp.sym';
25
26 my $ind = 0;
27 my (@name,@rest,@type,@code,@args,@flags,@longj);
28 my ($desc,$lastregop);
29 while (<DESC>) {
30     s/#.*$//;
31     next if /^\s*$/;
32     s/\s*\z//;
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   do {
89     push @selected, $name[$ind] if $flags[$ind] && $flags[$ind] eq $flag;
90   } while (++$ind < $lastregop);
91   my $out_string = join ', ', @selected, 0;
92   $out_string =~ s/(.{1,70},) /$1\n    /g;
93   return $comment . <<"EOP";
94 #define REGNODE_\U$varname\E(node) strchr((const char *)PL_${varname}, (node))
95
96 #ifndef DOINIT
97 EXTCONST U8 PL_${varname}[];
98 #else
99 EXTCONST U8 PL_${varname}[] = {
100     $out_string
101 };
102 #endif /* DOINIT */
103
104 EOP
105 }
106
107 my $tmp_h = 'regnodes.h-new';
108
109 unlink $tmp_h if -f $tmp_h;
110
111 my $out = safer_open($tmp_h);
112
113 printf $out <<EOP,
114 /* -*- buffer-read-only: t -*-
115    !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
116    This file is built by regcomp.pl from regcomp.sym.
117    Any changes made here will be lost!
118 */
119
120 /* Regops and State definitions */
121
122 #define %*s\t%d
123 #define %*s\t%d
124
125 EOP
126     -$width, REGNODE_MAX        => $lastregop - 1,
127     -$width, REGMATCH_STATE_MAX => $tot - 1
128 ;
129
130
131 for ($ind=0; $ind < $lastregop ; ++$ind) {
132   printf $out "#define\t%*s\t%d\t/* %#04x %s */\n",
133     -$width, $name[$ind], $ind, $ind, $rest[$ind];
134 }
135 print $out "\t/* ------------ States ------------- */\n";
136 for ( ; $ind < $tot ; $ind++) {
137   printf $out "#define\t%*s\t(REGNODE_MAX + %d)\t/* %s */\n",
138     -$width, $name[$ind], $ind - $lastregop + 1, $rest[$ind];
139 }
140
141 print $out <<EOP;
142
143 /* PL_regkind[] What type of regop or state is this. */
144
145 #ifndef DOINIT
146 EXTCONST U8 PL_regkind[];
147 #else
148 EXTCONST U8 PL_regkind[] = {
149 EOP
150
151 $ind = 0;
152 do {
153   printf $out "\t%*s\t/* %*s */\n",
154              -1-$twidth, "$type[$ind],", -$width, $name[$ind];
155   print $out "\t/* ------------ States ------------- */\n"
156     if $ind + 1 == $lastregop and $lastregop != $tot;
157 } while (++$ind < $tot);
158
159 print $out <<EOP;
160 };
161 #endif
162
163 /* regarglen[] - How large is the argument part of the node (in regnodes) */
164
165 #ifdef REG_COMP_C
166 static const U8 regarglen[] = {
167 EOP
168
169 $ind = 0;
170 do {
171   my $size = 0;
172   $size = "EXTRA_SIZE(struct regnode_$args[$ind])" if $args[$ind];
173   
174   printf $out "\t%*s\t/* %*s */\n",
175         -37, "$size,",-$rwidth,$name[$ind];
176 } while (++$ind < $lastregop);
177
178 print $out <<EOP;
179 };
180
181 /* reg_off_by_arg[] - Which argument holds the offset to the next node */
182
183 static const char reg_off_by_arg[] = {
184 EOP
185
186 $ind = 0;
187 do {
188   my $size = $longj[$ind] || 0;
189
190   printf $out "\t%d,\t/* %*s */\n",
191         $size, -$rwidth, $name[$ind]
192 } while (++$ind < $lastregop);
193
194 print $out <<EOP;
195 };
196
197 #endif /* REG_COMP_C */
198
199 /* reg_name[] - Opcode/state names in string form, for debugging */
200
201 #ifndef DOINIT
202 EXTCONST char * PL_reg_name[];
203 #else
204 EXTCONST char * const PL_reg_name[] = {
205 EOP
206
207 $ind = 0;
208 my $ofs = 0;
209 my $sym = "";
210 do {
211   my $size = $longj[$ind] || 0;
212
213   printf $out "\t%*s\t/* $sym%#04x */\n",
214         -3-$width,qq("$name[$ind]",), $ind - $ofs;
215   if ($ind + 1 == $lastregop and $lastregop != $tot) {
216     print $out "\t/* ------------ States ------------- */\n";
217     $ofs = $lastregop - 1;
218     $sym = 'REGNODE_MAX +';
219   }
220     
221 } while (++$ind < $tot);
222
223 print $out <<EOP;
224 };
225 #endif /* DOINIT */
226
227 /* PL_reg_extflags_name[] - Opcode/state names in string form, for debugging */
228
229 #ifndef DOINIT
230 EXTCONST char * PL_reg_extflags_name[];
231 #else
232 EXTCONST char * const PL_reg_extflags_name[] = {
233 EOP
234
235 open my $fh,"<","regexp.h" or die "Can't read regexp.h: $!";
236 my %rxfv;
237 my $val = 0;
238 my %reverse;
239 while (<$fh>) {
240     if (/#define\s+(RXf_\w+)\s+(0x[A-F\d]+)/i) {
241         my $newval = eval $2;
242         if($val & $newval) {
243             die sprintf "Both $1 and $reverse{$newval} use %08X", $newval;
244         }
245         $val|=$newval;
246         $rxfv{$1}= $newval;
247         $reverse{$newval} = $1;
248     }
249 }    
250 my %vrxf=reverse %rxfv;
251 printf $out "\t/* Bits in extflags defined: %032b */\n",$val;
252 for (0..31) {
253     my $n=$vrxf{2**$_}||"UNUSED_BIT_$_";
254     $n=~s/^RXf_(PMf_)?//;
255     printf $out qq(\t%-20s/* 0x%08x */\n), 
256         qq("$n",),2**$_;
257 }  
258  
259 print $out <<EOP;
260 };
261 #endif /* DOINIT */
262
263 EOP
264
265 print $out process_flags('V', 'varies', <<'EOC');
266 /* The following have no fixed length. U8 so we can do strchr() on it. */
267 EOC
268
269 print $out process_flags('S', 'simple', <<'EOC');
270 /* The following always have a length of 1. U8 we can do strchr() on it. */
271 /* (Note that length 1 means "one character" under UTF8, not "one octet".) */
272 EOC
273
274 print $out <<EOP;
275 /* ex: set ro: */
276 EOP
277 safer_close($out);
278
279 rename_if_different $tmp_h, 'regnodes.h';