This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Split out study magic from pos magic.
[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' },
87 vstring => { char => 'V', value_magic => 1,
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'},
147);
148
52f49505 149my ($vt, $raw, $names) = map {
6f83ef0e
NC
150 open_new($_, '>',
151 { by => 'regen/mg_vtable.pl', file => $_, style => '*' });
52f49505 152} 'mg_vtable.h', 'mg_raw.h', 'mg_names.c';
6f83ef0e 153
abf9aa7a
NC
154print $vt <<'EOH';
155/* These constants should be used in preference to raw characters
156 * when using magic. Note that some perl guts still assume
157 * certain character properties of these constants, namely that
158 * isUPPER() and toLOWER() may do useful mappings.
159 */
160
161EOH
162
6f83ef0e
NC
163# Of course, it would be *much* easier if we could output this table directly
164# here and now. However, for our sins, we try to support EBCDIC, which wouldn't
165# be *so* bad, except that there are (at least) 3 EBCDIC charset variants, and
166# they don't agree on the code point for '~'. Which we use. Great.
167# So we have to get the local build runtime to sort our table in character order
168# (And of course, just to be helpful, in POSIX BC '~' is \xFF, so we can't even
169# simplify the C code by assuming that the last element of the array is
170# predictable)
171
172{
e972d315
NC
173 my $longest = 0;
174 foreach (keys %mg) {
175 $longest = length $_ if length $_ > $longest;
176 }
177
52f49505
NC
178 my $longest_p1 = $longest + 1;
179
23cfd2fc
NC
180 my %mg_order;
181 while (my ($name, $data) = each %mg) {
182 my $byte = eval qq{"$data->{char}"};
183 $data->{byte} = $byte;
184 $mg_order{(uc $byte) . $byte} = $name;
185 }
63e77aaf 186 my @rows;
23cfd2fc
NC
187 foreach (sort keys %mg_order) {
188 my $name = $mg_order{$_};
189 my $data = $mg{$name};
190 my $i = ord $data->{byte};
6f83ef0e 191 unless ($data->{unknown_to_sv_magic}) {
e0a73de4 192 my $value = $data->{vtable}
6f83ef0e 193 ? "want_vtbl_$data->{vtable}" : 'magic_vtable_max';
82ff486e
NC
194 $value .= ' | PERL_MAGIC_READONLY_ACCEPTABLE'
195 if $data->{readonly_acceptable};
e0a73de4 196 $value .= ' | PERL_MAGIC_VALUE_MAGIC' if $data->{value_magic};
6f83ef0e
NC
197 my $comment = "/* $name '$data->{char}' $data->{desc} */";
198 $comment =~ s/([\\"])/\\$1/g;
abf9aa7a 199 $comment =~ tr/\n/ /;
e0a73de4 200 print $raw qq{ { '$data->{char}', "$value",\n "$comment" },\n};
6f83ef0e 201 }
abf9aa7a
NC
202
203 my $comment = $data->{desc};
204 my $leader = ' ' x ($longest + 27);
205 $comment =~ s/\n/\n$leader/s;
206 printf $vt "#define PERL_MAGIC_%-${longest}s '%s' /* %s */\n",
207 $name, $data->{char}, $comment;
52f49505
NC
208
209 my $char = $data->{char};
210 $char =~ s/([\\"])/\\$1/g;
211 printf $names qq[\t{ PERL_MAGIC_%-${longest_p1}s "%s(%s)" },\n],
212 "$name,", $name, $char;
63e77aaf
NC
213
214 push @rows, [(sprintf "%-2s PERL_MAGIC_%s", $data->{char}, $name),
215 $data->{vtable} ? "vtbl_$data->{vtable}" : '(none)',
216 $data->{desc}];
217 }
218 if ($output_guts) {
219 my @header = ('(old-style char and macro)', 'MGVTBL', 'Type of magic');
220 my @widths = (0, 0);
221 foreach my $row (@rows) {
222 for (0, 1) {
223 $widths[$_] = length $row->[$_]
224 if length $row->[$_] > $widths[$_];
225 }
226 }
227 my $indent = ' ';
228 my $format
229 = sprintf "$indent%%-%ds%%-%ds%%s\n", $widths[0] + 1, $widths[1] + 1;
230 my $desc_wrap = 80 - (length $indent) - $widths[0] - $widths[1] - 2;
231
232 print $indent . "mg_type\n";
233 printf $format, @header;
234 printf $format, map {'-' x length $_} @header;
235 foreach (@rows) {
236 my ($type, $vtbl, $desc) = @$_;
237 $desc =~ tr/\n/ /;
238 my @cont;
239 if (length $desc > $desc_wrap) {
240 # If it's too long, first split on '(', if there.
241 # [Which, if there, is always short enough, currently.
242 # Make this more robust if that changes]
243 ($desc, @cont) = split /(?=\()/, $desc;
244 if (!@cont) {
245 ($desc, @cont) = $desc =~ /(.{1,$desc_wrap})(?: |\z)/g
246 }
247 }
248 printf $format, $type, $vtbl, $desc;
249 printf $format, '', '', $_ foreach @cont;
250 }
6f83ef0e
NC
251 }
252}
8b09643d 253
23cfd2fc 254my @names = sort keys %sig;
ca298f7d 255{
2d1f1fe5
NC
256 my $want = join ",\n ", (map {"want_vtbl_$_"} @names), 'magic_vtable_max';
257 my $names = join qq{",\n "}, @names;
258
6f83ef0e 259 print $vt <<"EOH";
abf9aa7a 260
ca298f7d 261enum { /* pass one of these to get_vtbl */
2d1f1fe5
NC
262 $want
263};
264
265#ifdef DOINIT
266EXTCONST char *PL_magic_vtable_names[magic_vtable_max] = {
267 "$names"
ca298f7d 268};
2d1f1fe5
NC
269#else
270EXTCONST char *PL_magic_vtable_names[magic_vtable_max];
271#endif
ca298f7d
NC
272
273EOH
274}
275
6f83ef0e 276print $vt <<'EOH';
8b09643d
NC
277/* These all need to be 0, not NULL, as NULL can be (void*)0, which is a
278 * pointer to data, whereas we're assigning pointers to functions, which are
279 * not the same beast. ANSI doesn't allow the assignment from one to the other.
280 * (although most, but not all, compilers are prepared to do it)
281 */
282
0ffb5b03 283/* order is:
8b09643d
NC
284 get
285 set
286 len
287 clear
288 free
289 copy
290 dup
291 local
292*/
293
b7b5e578 294#ifdef DOINIT
c7fdacb9 295EXT_MGVTBL PL_magic_vtables[magic_vtable_max] = {
8b09643d
NC
296EOH
297
b7b5e578 298my @vtable_names;
b2e9fc6f 299my @aliases;
b7b5e578 300
23cfd2fc
NC
301while (my $name = shift @names) {
302 my $data = $sig{$name};
b7b5e578 303 push @vtable_names, $name;
0a1f728a 304 my @funcs = map {
8b09643d
NC
305 $data->{$_} ? "Perl_magic_$data->{$_}" : 0;
306 } qw(get set len clear free copy dup local);
307
0a1f728a 308 $funcs[0] = "(int (*)(pTHX_ SV *, MAGIC *))" . $funcs[0] if $data->{const};
0ffb5b03 309 my $funcs = join ", ", @funcs;
8b09643d 310
b7b5e578 311 # Because we can't have a , after the last {...}
23cfd2fc 312 my $comma = @names ? ',' : '';
b7b5e578 313
6f83ef0e
NC
314 print $vt "$data->{cond}\n" if $data->{cond};
315 print $vt " { $funcs }$comma\n";
316 print $vt <<"EOH" if $data->{cond};
0ffb5b03 317#else
b7b5e578 318 { 0, 0, 0, 0, 0, 0, 0, 0 }$comma
0ffb5b03 319#endif
b7b5e578 320EOH
b2e9fc6f
NC
321 foreach(@{$data->{alias}}) {
322 push @aliases, "#define want_vtbl_$_ want_vtbl_$name\n";
323 push @vtable_names, $_;
324 }
8b09643d
NC
325}
326
6f83ef0e 327print $vt <<'EOH';
b7b5e578
NC
328};
329#else
c7fdacb9 330EXT_MGVTBL PL_magic_vtables[magic_vtable_max];
b7b5e578
NC
331#endif
332
333EOH
334
6f83ef0e 335print $vt (sort @aliases), "\n";
b7b5e578 336
6f83ef0e 337print $vt "#define PL_vtbl_$_ PL_magic_vtables[want_vtbl_$_]\n"
b7b5e578
NC
338 foreach sort @vtable_names;
339
e0a73de4
NC
340# 63, not 64, As we rely on the last possible value to mean "NULL vtable"
341die "Too many vtable names" if @vtable_names > 63;
342
52f49505 343read_only_bottom_close_and_rename($_) foreach $vt, $raw, $names;