This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Regenerate perlguts’ mg table automatically
[perl5.git] / regen / mg_vtable.pl
CommitLineData
8b09643d
NC
1#!/usr/bin/perl -w
2#
3# Regenerate (overwriting only if changed):
4#
5# mg_vtable.h
6#
7# from information stored in this file.
8#
9# Accepts the standard regen_lib -q and -v args.
10#
11# This script is normally invoked from regen.pl.
12
13use strict;
14require 5.004;
15
16BEGIN {
17 # Get function prototypes
18 require 'regen/regen_lib.pl';
19}
20
23cfd2fc 21my %mg =
6f83ef0e 22 (
82ff486e
NC
23 sv => { char => '\0', vtable => 'sv', readonly_acceptable => 1,
24 desc => 'Special scalar variable' },
6f83ef0e
NC
25 overload => { char => 'A', vtable => 'amagic', desc => '%OVERLOAD hash' },
26 overload_elem => { char => 'a', vtable => 'amagicelem',
27 desc => '%OVERLOAD hash element' },
28 overload_table => { char => 'c', vtable => 'ovrld',
29 desc => 'Holds overload table (AMT) on stash' },
e0a73de4 30 bm => { char => 'B', vtable => 'regexp', value_magic => 1,
82ff486e 31 readonly_acceptable => 1,
6f83ef0e
NC
32 desc => 'Boyer-Moore (fast string search)' },
33 regdata => { char => 'D', vtable => 'regdata',
abf9aa7a 34 desc => "Regex match position data\n(\@+ and \@- vars)" },
6f83ef0e
NC
35 regdatum => { char => 'd', vtable => 'regdatum',
36 desc => 'Regex match position data element' },
37 env => { char => 'E', vtable => 'env', desc => '%ENV hash' },
38 envelem => { char => 'e', vtable => 'envelem',
39 desc => '%ENV hash element' },
e0a73de4 40 fm => { char => 'f', vtable => 'regdata', value_magic => 1,
82ff486e 41 readonly_acceptable => 1, desc => "Formline ('compiled' format)" },
0177730e
NC
42 study => { char => 'G', vtable => 'regexp', value_magic => 1,
43 readonly_acceptable => 1, desc => 'study()ed string' },
e0a73de4 44 regex_global => { char => 'g', vtable => 'mglob', value_magic => 1,
0177730e 45 readonly_acceptable => 1, desc => 'm//g target' },
6f83ef0e
NC
46 hints => { char => 'H', vtable => 'hints', desc => '%^H hash' },
47 hintselem => { char => 'h', vtable => 'hintselem',
48 desc => '%^H hash element' },
49 isa => { char => 'I', vtable => 'isa', desc => '@ISA array' },
50 isaelem => { char => 'i', vtable => 'isaelem',
51 desc => '@ISA array element' },
e0a73de4 52 nkeys => { char => 'k', vtable => 'nkeys', value_magic => 1,
6f83ef0e 53 desc => 'scalar(keys()) lvalue' },
f34d1562 54 dbfile => { char => 'L',
6f83ef0e 55 desc => 'Debugger %_<filename' },
f34d1562
FC
56 dbline => { char => 'l', vtable => 'dbline',
57 desc => 'Debugger %_<filename element' },
6f83ef0e
NC
58 shared => { char => 'N', desc => 'Shared between threads',
59 unknown_to_sv_magic => 1 },
60 shared_scalar => { char => 'n', desc => 'Shared between threads',
61 unknown_to_sv_magic => 1 },
e0a73de4 62 collxfrm => { char => 'o', vtable => 'collxfrm', value_magic => 1,
6f83ef0e 63 desc => 'Locale transformation' },
e0a73de4
NC
64 tied => { char => 'P', vtable => 'pack',
65 value_magic => 1, # treat as value, so 'local @tied' isn't tied
66 desc => 'Tied array or hash' },
6f83ef0e
NC
67 tiedelem => { char => 'p', vtable => 'packelem',
68 desc => 'Tied array or hash element' },
69 tiedscalar => { char => 'q', vtable => 'packelem',
70 desc => 'Tied scalar or handle' },
e0a73de4
NC
71 qr => { char => 'r', vtable => 'regexp', value_magic => 1,
72 desc => 'precompiled qr// regex' },
6f83ef0e
NC
73 sig => { char => 'S', desc => '%SIG hash' },
74 sigelem => { char => 's', vtable => 'sigelem',
75 desc => '%SIG hash element' },
e0a73de4
NC
76 taint => { char => 't', vtable => 'taint', value_magic => 1,
77 desc => 'Taintedness' },
6f83ef0e
NC
78 uvar => { char => 'U', vtable => 'uvar',
79 desc => 'Available for use by extensions' },
80 uvar_elem => { char => 'u', desc => 'Reserved for use by extensions',
81 unknown_to_sv_magic => 1 },
e0a73de4
NC
82 vec => { char => 'v', vtable => 'vec', value_magic => 1,
83 desc => 'vec() lvalue' },
1e6bda93 84 vstring => { char => 'V', value_magic => 1, vtable => 'vstring',
e0a73de4
NC
85 desc => 'SV was vstring literal' },
86 utf8 => { char => 'w', vtable => 'utf8', value_magic => 1,
6f83ef0e 87 desc => 'Cached UTF-8 information' },
e0a73de4
NC
88 substr => { char => 'x', vtable => 'substr', value_magic => 1,
89 desc => 'substr() lvalue' },
90 defelem => { char => 'y', vtable => 'defelem', value_magic => 1,
abf9aa7a 91 desc => "Shadow \"foreach\" iterator variable /\nsmart parameter vivification" },
e0a73de4 92 arylen => { char => '#', vtable => 'arylen', value_magic => 1,
6f83ef0e 93 desc => 'Array length ($#ary)' },
e0a73de4
NC
94 pos => { char => '.', vtable => 'pos', value_magic => 1,
95 desc => 'pos() lvalue' },
96 backref => { char => '<', vtable => 'backref', value_magic => 1,
82ff486e 97 readonly_acceptable => 1, desc => 'for weak ref data' },
e0a73de4
NC
98 symtab => { char => ':', value_magic => 1,
99 desc => 'extra data for symbol tables' },
100 rhash => { char => '%', value_magic => 1,
101 desc => 'extra data for restricted hashes' },
102 arylen_p => { char => '@', value_magic => 1,
103 desc => 'to move arylen out of XPVAV' },
6f83ef0e 104 ext => { char => '~', desc => 'Available for use by extensions' },
e0a73de4
NC
105 checkcall => { char => ']', value_magic => 1,
106 desc => 'inlining/mutation of call to this CV'},
6f83ef0e
NC
107);
108
0a1f728a 109# These have a subtly different "namespace" from the magic types.
23cfd2fc 110my %sig =
8b09643d
NC
111 (
112 'sv' => {get => 'get', set => 'set', len => 'len'},
113 'env' => {set => 'set_all_env', clear => 'clear_all_env'},
114 'envelem' => {set => 'setenv', clear => 'clearenv'},
115 'sigelem' => {get => 'getsig', set => 'setsig', clear => 'clearsig',
116 cond => '#ifndef PERL_MICRO'},
117 'pack' => {len => 'sizepack', clear => 'wipepack'},
118 'packelem' => {get => 'getpack', set => 'setpack', clear => 'clearpack'},
119 'dbline' => {set => 'setdbline'},
120 'isa' => {set => 'setisa', clear => 'clearisa'},
121 'isaelem' => {set => 'setisa'},
122 'arylen' => {get => 'getarylen', set => 'setarylen', const => 1},
123 'arylen_p' => {free => 'freearylen_p'},
124 'mglob' => {set => 'setmglob'},
125 'nkeys' => {get => 'getnkeys', set => 'setnkeys'},
126 'taint' => {get => 'gettaint', set => 'settaint'},
127 'substr' => {get => 'getsubstr', set => 'setsubstr'},
128 'vec' => {get => 'getvec', set => 'setvec'},
129 'pos' => {get => 'getpos', set => 'setpos'},
8b09643d
NC
130 'uvar' => {get => 'getuvar', set => 'setuvar'},
131 'defelem' => {get => 'getdefelem', set => 'setdefelem'},
b2e9fc6f 132 'regexp' => {set => 'setregexp', alias => [qw(bm fm)]},
8b09643d
NC
133 'regdata' => {len => 'regdata_cnt'},
134 'regdatum' => {get => 'regdatum_get', set => 'regdatum_set'},
135 'amagic' => {set => 'setamagic', free => 'setamagic'},
136 'amagicelem' => {set => 'setamagic', free => 'setamagic'},
137 'backref' => {free => 'killbackrefs'},
138 'ovrld' => {free => 'freeovrld'},
139 'utf8' => {set => 'setutf8'},
140 'collxfrm' => {set => 'setcollxfrm',
141 cond => '#ifdef USE_LOCALE_COLLATE'},
142 'hintselem' => {set => 'sethint', clear => 'clearhint'},
143 'hints' => {clear => 'clearhints'},
1e6bda93 144 'vstring' => {set => 'setvstring'},
8b09643d
NC
145);
146
52f49505 147my ($vt, $raw, $names) = map {
6f83ef0e
NC
148 open_new($_, '>',
149 { by => 'regen/mg_vtable.pl', file => $_, style => '*' });
52f49505 150} 'mg_vtable.h', 'mg_raw.h', 'mg_names.c';
f1f5ddd7 151my $guts = open_new("pod/perlguts.pod", ">");
6f83ef0e 152
abf9aa7a
NC
153print $vt <<'EOH';
154/* These constants should be used in preference to raw characters
155 * when using magic. Note that some perl guts still assume
156 * certain character properties of these constants, namely that
157 * isUPPER() and toLOWER() may do useful mappings.
158 */
159
160EOH
161
6f83ef0e
NC
162# Of course, it would be *much* easier if we could output this table directly
163# here and now. However, for our sins, we try to support EBCDIC, which wouldn't
164# be *so* bad, except that there are (at least) 3 EBCDIC charset variants, and
165# they don't agree on the code point for '~'. Which we use. Great.
166# So we have to get the local build runtime to sort our table in character order
167# (And of course, just to be helpful, in POSIX BC '~' is \xFF, so we can't even
168# simplify the C code by assuming that the last element of the array is
169# predictable)
170
171{
e972d315
NC
172 my $longest = 0;
173 foreach (keys %mg) {
174 $longest = length $_ if length $_ > $longest;
175 }
176
52f49505
NC
177 my $longest_p1 = $longest + 1;
178
23cfd2fc
NC
179 my %mg_order;
180 while (my ($name, $data) = each %mg) {
181 my $byte = eval qq{"$data->{char}"};
182 $data->{byte} = $byte;
183 $mg_order{(uc $byte) . $byte} = $name;
184 }
63e77aaf 185 my @rows;
23cfd2fc
NC
186 foreach (sort keys %mg_order) {
187 my $name = $mg_order{$_};
188 my $data = $mg{$name};
189 my $i = ord $data->{byte};
6f83ef0e 190 unless ($data->{unknown_to_sv_magic}) {
e0a73de4 191 my $value = $data->{vtable}
6f83ef0e 192 ? "want_vtbl_$data->{vtable}" : 'magic_vtable_max';
82ff486e
NC
193 $value .= ' | PERL_MAGIC_READONLY_ACCEPTABLE'
194 if $data->{readonly_acceptable};
e0a73de4 195 $value .= ' | PERL_MAGIC_VALUE_MAGIC' if $data->{value_magic};
6f83ef0e
NC
196 my $comment = "/* $name '$data->{char}' $data->{desc} */";
197 $comment =~ s/([\\"])/\\$1/g;
abf9aa7a 198 $comment =~ tr/\n/ /;
e0a73de4 199 print $raw qq{ { '$data->{char}', "$value",\n "$comment" },\n};
6f83ef0e 200 }
abf9aa7a
NC
201
202 my $comment = $data->{desc};
203 my $leader = ' ' x ($longest + 27);
204 $comment =~ s/\n/\n$leader/s;
205 printf $vt "#define PERL_MAGIC_%-${longest}s '%s' /* %s */\n",
206 $name, $data->{char}, $comment;
52f49505
NC
207
208 my $char = $data->{char};
209 $char =~ s/([\\"])/\\$1/g;
210 printf $names qq[\t{ PERL_MAGIC_%-${longest_p1}s "%s(%s)" },\n],
211 "$name,", $name, $char;
63e77aaf
NC
212
213 push @rows, [(sprintf "%-2s PERL_MAGIC_%s", $data->{char}, $name),
214 $data->{vtable} ? "vtbl_$data->{vtable}" : '(none)',
215 $data->{desc}];
216 }
f1f5ddd7 217 select +(select($guts), do {
63e77aaf
NC
218 my @header = ('(old-style char and macro)', 'MGVTBL', 'Type of magic');
219 my @widths = (0, 0);
220 foreach my $row (@rows) {
221 for (0, 1) {
222 $widths[$_] = length $row->[$_]
223 if length $row->[$_] > $widths[$_];
224 }
225 }
f1f5ddd7 226 my $indent = ' ';
63e77aaf
NC
227 my $format
228 = sprintf "$indent%%-%ds%%-%ds%%s\n", $widths[0] + 1, $widths[1] + 1;
f1f5ddd7
FC
229 my $desc_wrap =
230 79 - 7 - (length $indent) - $widths[0] - $widths[1] - 2;
231
232 open my $oldguts, "<", "pod/perlguts.pod"
233 or die "$0 cannot open pod/perlguts.pod for reading: $!";
234 while (<$oldguts>) {
235 print;
236 last if /^=for mg_vtable.pl begin/
237 }
63e77aaf 238
f1f5ddd7 239 print "\n", $indent . "mg_type\n";
63e77aaf
NC
240 printf $format, @header;
241 printf $format, map {'-' x length $_} @header;
242 foreach (@rows) {
243 my ($type, $vtbl, $desc) = @$_;
244 $desc =~ tr/\n/ /;
245 my @cont;
246 if (length $desc > $desc_wrap) {
247 # If it's too long, first split on '(', if there.
248 # [Which, if there, is always short enough, currently.
249 # Make this more robust if that changes]
250 ($desc, @cont) = split /(?=\()/, $desc;
251 if (!@cont) {
252 ($desc, @cont) = $desc =~ /(.{1,$desc_wrap})(?: |\z)/g
253 }
254 }
255 printf $format, $type, $vtbl, $desc;
256 printf $format, '', '', $_ foreach @cont;
257 }
f1f5ddd7
FC
258 print "\n";
259
260 while (<$oldguts>) {
261 last if /^=for mg_vtable.pl end/;
262 }
263 do { print } while <$oldguts>;
264 })[0];
6f83ef0e 265}
8b09643d 266
23cfd2fc 267my @names = sort keys %sig;
ca298f7d 268{
2d1f1fe5
NC
269 my $want = join ",\n ", (map {"want_vtbl_$_"} @names), 'magic_vtable_max';
270 my $names = join qq{",\n "}, @names;
271
6f83ef0e 272 print $vt <<"EOH";
abf9aa7a 273
ca298f7d 274enum { /* pass one of these to get_vtbl */
2d1f1fe5
NC
275 $want
276};
277
278#ifdef DOINIT
279EXTCONST char *PL_magic_vtable_names[magic_vtable_max] = {
280 "$names"
ca298f7d 281};
2d1f1fe5
NC
282#else
283EXTCONST char *PL_magic_vtable_names[magic_vtable_max];
284#endif
ca298f7d
NC
285
286EOH
287}
288
6f83ef0e 289print $vt <<'EOH';
8b09643d
NC
290/* These all need to be 0, not NULL, as NULL can be (void*)0, which is a
291 * pointer to data, whereas we're assigning pointers to functions, which are
292 * not the same beast. ANSI doesn't allow the assignment from one to the other.
293 * (although most, but not all, compilers are prepared to do it)
294 */
295
0ffb5b03 296/* order is:
8b09643d
NC
297 get
298 set
299 len
300 clear
301 free
302 copy
303 dup
304 local
305*/
306
b7b5e578 307#ifdef DOINIT
c7fdacb9 308EXT_MGVTBL PL_magic_vtables[magic_vtable_max] = {
8b09643d
NC
309EOH
310
b7b5e578 311my @vtable_names;
b2e9fc6f 312my @aliases;
b7b5e578 313
23cfd2fc
NC
314while (my $name = shift @names) {
315 my $data = $sig{$name};
b7b5e578 316 push @vtable_names, $name;
0a1f728a 317 my @funcs = map {
8b09643d
NC
318 $data->{$_} ? "Perl_magic_$data->{$_}" : 0;
319 } qw(get set len clear free copy dup local);
320
0a1f728a 321 $funcs[0] = "(int (*)(pTHX_ SV *, MAGIC *))" . $funcs[0] if $data->{const};
0ffb5b03 322 my $funcs = join ", ", @funcs;
8b09643d 323
b7b5e578 324 # Because we can't have a , after the last {...}
23cfd2fc 325 my $comma = @names ? ',' : '';
b7b5e578 326
6f83ef0e
NC
327 print $vt "$data->{cond}\n" if $data->{cond};
328 print $vt " { $funcs }$comma\n";
329 print $vt <<"EOH" if $data->{cond};
0ffb5b03 330#else
b7b5e578 331 { 0, 0, 0, 0, 0, 0, 0, 0 }$comma
0ffb5b03 332#endif
b7b5e578 333EOH
b2e9fc6f
NC
334 foreach(@{$data->{alias}}) {
335 push @aliases, "#define want_vtbl_$_ want_vtbl_$name\n";
336 push @vtable_names, $_;
337 }
8b09643d
NC
338}
339
6f83ef0e 340print $vt <<'EOH';
b7b5e578
NC
341};
342#else
c7fdacb9 343EXT_MGVTBL PL_magic_vtables[magic_vtable_max];
b7b5e578
NC
344#endif
345
346EOH
347
6f83ef0e 348print $vt (sort @aliases), "\n";
b7b5e578 349
6f83ef0e 350print $vt "#define PL_vtbl_$_ PL_magic_vtables[want_vtbl_$_]\n"
b7b5e578
NC
351 foreach sort @vtable_names;
352
e0a73de4
NC
353# 63, not 64, As we rely on the last possible value to mean "NULL vtable"
354die "Too many vtable names" if @vtable_names > 63;
355
52f49505 356read_only_bottom_close_and_rename($_) foreach $vt, $raw, $names;
f1f5ddd7 357 close_and_rename($guts);