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