This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
add Perl_magic_methcall
[perl5.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,@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         $ind++;
39         ($name[$ind], $desc, $rest[$ind]) = split /\t+/, $_, 3;  
40         ($type[$ind], $code[$ind], $args[$ind], $longj[$ind]) 
41           = split /[,\s]\s*/, $desc, 4;
42     } else {
43         my ($type,@lists)=split /\s*\t+\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                     $ind++;
64                     $name[$ind]="$real$suffix";
65                     $type[$ind]=$type;
66                     $rest[$ind]="state for $type";
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 my $tmp_h = 'regnodes.h-new';
83
84 unlink $tmp_h if -f $tmp_h;
85
86 my $out = safer_open($tmp_h);
87
88 printf $out <<EOP,
89 /* -*- buffer-read-only: t -*-
90    !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
91    This file is built by regcomp.pl from regcomp.sym.
92    Any changes made here will be lost!
93 */
94
95 /* Regops and State definitions */
96
97 #define %*s\t%d
98 #define %*s\t%d
99
100 EOP
101     -$width, REGNODE_MAX        => $lastregop - 1,
102     -$width, REGMATCH_STATE_MAX => $tot - 1
103 ;
104
105
106 for ($ind=1; $ind <= $lastregop ; $ind++) {
107   my $oind = $ind - 1;
108   printf $out "#define\t%*s\t%d\t/* %#04x %s */\n",
109     -$width, $name[$ind], $ind-1, $ind-1, $rest[$ind];
110 }
111 print $out "\t/* ------------ States ------------- */\n";
112 for ( ; $ind <= $tot ; $ind++) {
113   printf $out "#define\t%*s\t(REGNODE_MAX + %d)\t/* %s */\n",
114     -$width, $name[$ind], $ind - $lastregop, $rest[$ind];
115 }
116
117 print $out <<EOP;
118
119 /* PL_regkind[] What type of regop or state is this. */
120
121 #ifndef DOINIT
122 EXTCONST U8 PL_regkind[];
123 #else
124 EXTCONST U8 PL_regkind[] = {
125 EOP
126
127 $ind = 0;
128 while (++$ind <= $tot) {
129   printf $out "\t%*s\t/* %*s */\n",
130              -1-$twidth, "$type[$ind],", -$width, $name[$ind];
131   print $out "\t/* ------------ States ------------- */\n"
132     if $ind == $lastregop and $lastregop != $tot;
133 }
134
135 print $out <<EOP;
136 };
137 #endif
138
139 /* regarglen[] - How large is the argument part of the node (in regnodes) */
140
141 #ifdef REG_COMP_C
142 static const U8 regarglen[] = {
143 EOP
144
145 $ind = 0;
146 while (++$ind <= $lastregop) {
147   my $size = 0;
148   $size = "EXTRA_SIZE(struct regnode_$args[$ind])" if $args[$ind];
149   
150   printf $out "\t%*s\t/* %*s */\n",
151         -37, "$size,",-$rwidth,$name[$ind];
152 }
153
154 print $out <<EOP;
155 };
156
157 /* reg_off_by_arg[] - Which argument holds the offset to the next node */
158
159 static const char reg_off_by_arg[] = {
160 EOP
161
162 $ind = 0;
163 while (++$ind <= $lastregop) {
164   my $size = $longj[$ind] || 0;
165
166   printf $out "\t%d,\t/* %*s */\n",
167         $size, -$rwidth, $name[$ind]
168 }
169
170 print $out <<EOP;
171 };
172
173 #endif /* REG_COMP_C */
174
175 /* reg_name[] - Opcode/state names in string form, for debugging */
176
177 #ifndef DOINIT
178 EXTCONST char * PL_reg_name[];
179 #else
180 EXTCONST char * const PL_reg_name[] = {
181 EOP
182
183 $ind = 0;
184 my $ofs = 1;
185 my $sym = "";
186 while (++$ind <= $tot) {
187   my $size = $longj[$ind] || 0;
188
189   printf $out "\t%*s\t/* $sym%#04x */\n",
190         -3-$width,qq("$name[$ind]",), $ind - $ofs;
191   if ($ind == $lastregop and $lastregop != $tot) {
192     print $out "\t/* ------------ States ------------- */\n";
193     $ofs = $lastregop;
194     $sym = 'REGNODE_MAX +';
195   }
196     
197 }
198
199 print $out <<EOP;
200 };
201 #endif /* DOINIT */
202
203 /* PL_reg_extflags_name[] - Opcode/state names in string form, for debugging */
204
205 #ifndef DOINIT
206 EXTCONST char * PL_reg_extflags_name[];
207 #else
208 EXTCONST char * const PL_reg_extflags_name[] = {
209 EOP
210
211 open my $fh,"<","regexp.h" or die "Can't read regexp.h: $!";
212 my %rxfv;
213 my $val = 0;
214 my %reverse;
215 while (<$fh>) {
216     if (/#define\s+(RXf_\w+)\s+(0x[A-F\d]+)/i) {
217         my $newval = eval $2;
218         if($val & $newval) {
219             die sprintf "Both $1 and $reverse{$newval} use %08X", $newval;
220         }
221         $val|=$newval;
222         $rxfv{$1}= $newval;
223         $reverse{$newval} = $1;
224     }
225 }    
226 my %vrxf=reverse %rxfv;
227 printf $out "\t/* Bits in extflags defined: %032b */\n",$val;
228 for (0..31) {
229     my $n=$vrxf{2**$_}||"UNUSED_BIT_$_";
230     $n=~s/^RXf_(PMf_)?//;
231     printf $out qq(\t%-20s/* 0x%08x */\n), 
232         qq("$n",),2**$_;
233 }  
234  
235 print $out <<EOP;
236 };
237 #endif /* DOINIT */
238
239 /* ex: set ro: */
240 EOP
241 safer_close($out);
242
243 rename_if_different $tmp_h, 'regnodes.h';