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