This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Encapsulate lookups in PL_{varies,simple} within macros REGNODE_{VARIES,SIMPLE}
[perl5.git] / regcomp.pl
CommitLineData
6294c161
DM
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
36bb303b
NC
16BEGIN {
17 # Get function prototypes
9ad884cb 18 require 'regen_lib.pl';
36bb303b 19}
d09b2d29 20#use Fatal qw(open close rename chmod unlink);
03363afd
YO
21use strict;
22use warnings;
23
d09b2d29 24open DESC, 'regcomp.sym';
d09b2d29 25
03363afd 26my $ind = 0;
f9ef50a7 27my (@name,@rest,@type,@code,@args,@flags,@longj);
03363afd 28my ($desc,$lastregop);
d09b2d29 29while (<DESC>) {
03363afd
YO
30 s/#.*$//;
31 next if /^\s*$/;
32 s/\s*\z//;
33 if (/^-+\s*$/) {
34 $lastregop= $ind;
35 next;
36 }
37 unless ($lastregop) {
38 $ind++;
f8abb37e 39 ($name[$ind], $desc, $rest[$ind]) = /^(\S+)\s+([^\t]+)\s*;\s*(.*)/;
f9ef50a7
NC
40 ($type[$ind], $code[$ind], $args[$ind], $flags[$ind], $longj[$ind])
41 = split /[,\s]\s*/, $desc;
03363afd 42 } else {
f8abb37e 43 my ($type,@lists)=split /\s+/, $_;
03363afd
YO
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;
24b23f37 66 $rest[$ind]="state for $type";
03363afd
YO
67 }
68 }
69 }
70
71 }
72}
5d458dd8
YO
73# use fixed width to keep the diffs between regcomp.pl recompiles
74# as small as possible.
75my ($width,$rwidth,$twidth)=(22,12,9);
03363afd
YO
76$lastregop ||= $ind;
77my $tot = $ind;
d09b2d29 78close DESC;
03363afd
YO
79die "Too many regexp/state opcodes! Maximum is 256, but there are $lastregop in file!"
80 if $lastregop>256;
d09b2d29 81
f9ef50a7
NC
82sub process_flags {
83 my ($flag, $varname, $comment) = @_;
84 $comment = '' unless defined $comment;
85
86 $ind = 0;
87 my @selected;
88 while (++$ind <= $lastregop) {
89 push @selected, $name[$ind] if $flags[$ind] && $flags[$ind] eq $flag;
90 }
91 my $out_string = join ', ', @selected, 0;
92 $out_string =~ s/(.{1,70},) /$1\n /g;
93 return $comment . <<"EOP";
e52fc539
NC
94#define REGNODE_\U$varname\E(node) strchr((const char *)PL_${varname}, (node))
95
f9ef50a7
NC
96#ifndef DOINIT
97EXTCONST U8 PL_${varname}[];
98#else
99EXTCONST U8 PL_${varname}[] = {
100 $out_string
101};
102#endif /* DOINIT */
103
104EOP
105}
106
266db279 107my $tmp_h = 'regnodes.h-new';
d09b2d29
IZ
108
109unlink $tmp_h if -f $tmp_h;
110
424a4936 111my $out = safer_open($tmp_h);
d09b2d29 112
424a4936 113printf $out <<EOP,
37442d52
RGS
114/* -*- buffer-read-only: t -*-
115 !!!!!!! DO NOT EDIT THIS FILE !!!!!!!
9b155405 116 This file is built by regcomp.pl from regcomp.sym.
d09b2d29
IZ
117 Any changes made here will be lost!
118*/
119
6bda09f9
YO
120/* Regops and State definitions */
121
03363afd
YO
122#define %*s\t%d
123#define %*s\t%d
124
d09b2d29 125EOP
f9f4320a
YO
126 -$width, REGNODE_MAX => $lastregop - 1,
127 -$width, REGMATCH_STATE_MAX => $tot - 1
128;
d09b2d29 129
24b23f37
YO
130
131for ($ind=1; $ind <= $lastregop ; $ind++) {
03363afd 132 my $oind = $ind - 1;
424a4936 133 printf $out "#define\t%*s\t%d\t/* %#04x %s */\n",
03363afd 134 -$width, $name[$ind], $ind-1, $ind-1, $rest[$ind];
24b23f37 135}
424a4936 136print $out "\t/* ------------ States ------------- */\n";
24b23f37 137for ( ; $ind <= $tot ; $ind++) {
424a4936 138 printf $out "#define\t%*s\t(REGNODE_MAX + %d)\t/* %s */\n",
24b23f37 139 -$width, $name[$ind], $ind - $lastregop, $rest[$ind];
d09b2d29
IZ
140}
141
424a4936 142print $out <<EOP;
03363afd 143
6bda09f9 144/* PL_regkind[] What type of regop or state is this. */
d09b2d29
IZ
145
146#ifndef DOINIT
22c35a8c 147EXTCONST U8 PL_regkind[];
d09b2d29 148#else
22c35a8c 149EXTCONST U8 PL_regkind[] = {
d09b2d29
IZ
150EOP
151
152$ind = 0;
153while (++$ind <= $tot) {
424a4936 154 printf $out "\t%*s\t/* %*s */\n",
03363afd 155 -1-$twidth, "$type[$ind],", -$width, $name[$ind];
424a4936 156 print $out "\t/* ------------ States ------------- */\n"
03363afd 157 if $ind == $lastregop and $lastregop != $tot;
d09b2d29
IZ
158}
159
424a4936 160print $out <<EOP;
d09b2d29
IZ
161};
162#endif
163
6bda09f9 164/* regarglen[] - How large is the argument part of the node (in regnodes) */
d09b2d29
IZ
165
166#ifdef REG_COMP_C
29de9391 167static const U8 regarglen[] = {
d09b2d29
IZ
168EOP
169
170$ind = 0;
03363afd
YO
171while (++$ind <= $lastregop) {
172 my $size = 0;
d09b2d29
IZ
173 $size = "EXTRA_SIZE(struct regnode_$args[$ind])" if $args[$ind];
174
424a4936 175 printf $out "\t%*s\t/* %*s */\n",
03363afd 176 -37, "$size,",-$rwidth,$name[$ind];
d09b2d29
IZ
177}
178
424a4936 179print $out <<EOP;
d09b2d29
IZ
180};
181
6bda09f9
YO
182/* reg_off_by_arg[] - Which argument holds the offset to the next node */
183
29de9391 184static const char reg_off_by_arg[] = {
d09b2d29
IZ
185EOP
186
187$ind = 0;
03363afd
YO
188while (++$ind <= $lastregop) {
189 my $size = $longj[$ind] || 0;
9b155405 190
424a4936 191 printf $out "\t%d,\t/* %*s */\n",
03363afd 192 $size, -$rwidth, $name[$ind]
d09b2d29
IZ
193}
194
424a4936 195print $out <<EOP;
d09b2d29 196};
9b155405 197
13d6edb4
NC
198#endif /* REG_COMP_C */
199
6bda09f9
YO
200/* reg_name[] - Opcode/state names in string form, for debugging */
201
22429478 202#ifndef DOINIT
13d6edb4 203EXTCONST char * PL_reg_name[];
22429478 204#else
4764e399 205EXTCONST char * const PL_reg_name[] = {
9b155405
IZ
206EOP
207
208$ind = 0;
24b23f37
YO
209my $ofs = 1;
210my $sym = "";
9b155405 211while (++$ind <= $tot) {
03363afd 212 my $size = $longj[$ind] || 0;
9b155405 213
424a4936 214 printf $out "\t%*s\t/* $sym%#04x */\n",
24b23f37
YO
215 -3-$width,qq("$name[$ind]",), $ind - $ofs;
216 if ($ind == $lastregop and $lastregop != $tot) {
424a4936 217 print $out "\t/* ------------ States ------------- */\n";
24b23f37
YO
218 $ofs = $lastregop;
219 $sym = 'REGNODE_MAX +';
220 }
221
9b155405
IZ
222}
223
424a4936 224print $out <<EOP;
9b155405 225};
22429478 226#endif /* DOINIT */
d09b2d29 227
f7819f85
A
228/* PL_reg_extflags_name[] - Opcode/state names in string form, for debugging */
229
230#ifndef DOINIT
231EXTCONST char * PL_reg_extflags_name[];
232#else
233EXTCONST char * const PL_reg_extflags_name[] = {
d09b2d29
IZ
234EOP
235
f7819f85
A
236open my $fh,"<","regexp.h" or die "Can't read regexp.h: $!";
237my %rxfv;
c8e4cf8b
NC
238my $val = 0;
239my %reverse;
f7819f85
A
240while (<$fh>) {
241 if (/#define\s+(RXf_\w+)\s+(0x[A-F\d]+)/i) {
c8e4cf8b
NC
242 my $newval = eval $2;
243 if($val & $newval) {
244 die sprintf "Both $1 and $reverse{$newval} use %08X", $newval;
245 }
246 $val|=$newval;
247 $rxfv{$1}= $newval;
248 $reverse{$newval} = $1;
f7819f85
A
249 }
250}
251my %vrxf=reverse %rxfv;
424a4936 252printf $out "\t/* Bits in extflags defined: %032b */\n",$val;
f7819f85
A
253for (0..31) {
254 my $n=$vrxf{2**$_}||"UNUSED_BIT_$_";
255 $n=~s/^RXf_(PMf_)?//;
424a4936 256 printf $out qq(\t%-20s/* 0x%08x */\n),
f7819f85
A
257 qq("$n",),2**$_;
258}
259
424a4936 260print $out <<EOP;
f7819f85
A
261};
262#endif /* DOINIT */
263
f9ef50a7
NC
264EOP
265
266print $out process_flags('V', 'varies', <<'EOC');
267/* The following have no fixed length. U8 so we can do strchr() on it. */
268EOC
269
270print $out process_flags('S', 'simple', <<'EOC');
271/* The following always have a length of 1. U8 we can do strchr() on it. */
272/* (Note that length 1 means "one character" under UTF8, not "one octet".) */
273EOC
274
275print $out <<EOP;
f7819f85
A
276/* ex: set ro: */
277EOP
08858ed2 278safer_close($out);
d09b2d29 279
424a4936 280rename_if_different $tmp_h, 'regnodes.h';