This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Generate magic_names in dump.c using mg_vtable.pl.
[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
6f83ef0e
NC
21my @mg =
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)" },
e0a73de4 42 regex_global => { char => 'g', vtable => 'mglob', value_magic => 1,
82ff486e 43 readonly_acceptable => 1,
6f83ef0e
NC
44 desc => 'm//g target / study()ed string' },
45 hints => { char => 'H', vtable => 'hints', desc => '%^H hash' },
46 hintselem => { char => 'h', vtable => 'hintselem',
47 desc => '%^H hash element' },
48 isa => { char => 'I', vtable => 'isa', desc => '@ISA array' },
49 isaelem => { char => 'i', vtable => 'isaelem',
50 desc => '@ISA array element' },
e0a73de4 51 nkeys => { char => 'k', vtable => 'nkeys', value_magic => 1,
6f83ef0e
NC
52 desc => 'scalar(keys()) lvalue' },
53 dbfile => { char => 'L', vtable => 'dbline',
54 desc => 'Debugger %_<filename' },
55 dbline => { char => 'l', desc => 'Debugger %_<filename element' },
56 shared => { char => 'N', desc => 'Shared between threads',
57 unknown_to_sv_magic => 1 },
58 shared_scalar => { char => 'n', desc => 'Shared between threads',
59 unknown_to_sv_magic => 1 },
e0a73de4 60 collxfrm => { char => 'o', vtable => 'collxfrm', value_magic => 1,
6f83ef0e 61 desc => 'Locale transformation' },
e0a73de4
NC
62 tied => { char => 'P', vtable => 'pack',
63 value_magic => 1, # treat as value, so 'local @tied' isn't tied
64 desc => 'Tied array or hash' },
6f83ef0e
NC
65 tiedelem => { char => 'p', vtable => 'packelem',
66 desc => 'Tied array or hash element' },
67 tiedscalar => { char => 'q', vtable => 'packelem',
68 desc => 'Tied scalar or handle' },
e0a73de4
NC
69 qr => { char => 'r', vtable => 'regexp', value_magic => 1,
70 desc => 'precompiled qr// regex' },
6f83ef0e
NC
71 sig => { char => 'S', desc => '%SIG hash' },
72 sigelem => { char => 's', vtable => 'sigelem',
73 desc => '%SIG hash element' },
e0a73de4
NC
74 taint => { char => 't', vtable => 'taint', value_magic => 1,
75 desc => 'Taintedness' },
6f83ef0e
NC
76 uvar => { char => 'U', vtable => 'uvar',
77 desc => 'Available for use by extensions' },
78 uvar_elem => { char => 'u', desc => 'Reserved for use by extensions',
79 unknown_to_sv_magic => 1 },
e0a73de4
NC
80 vec => { char => 'v', vtable => 'vec', value_magic => 1,
81 desc => 'vec() lvalue' },
82 vstring => { char => 'V', value_magic => 1,
83 desc => 'SV was vstring literal' },
84 utf8 => { char => 'w', vtable => 'utf8', value_magic => 1,
6f83ef0e 85 desc => 'Cached UTF-8 information' },
e0a73de4
NC
86 substr => { char => 'x', vtable => 'substr', value_magic => 1,
87 desc => 'substr() lvalue' },
88 defelem => { char => 'y', vtable => 'defelem', value_magic => 1,
abf9aa7a 89 desc => "Shadow \"foreach\" iterator variable /\nsmart parameter vivification" },
e0a73de4 90 arylen => { char => '#', vtable => 'arylen', value_magic => 1,
6f83ef0e 91 desc => 'Array length ($#ary)' },
e0a73de4
NC
92 pos => { char => '.', vtable => 'pos', value_magic => 1,
93 desc => 'pos() lvalue' },
94 backref => { char => '<', vtable => 'backref', value_magic => 1,
82ff486e 95 readonly_acceptable => 1, desc => 'for weak ref data' },
e0a73de4
NC
96 symtab => { char => ':', value_magic => 1,
97 desc => 'extra data for symbol tables' },
98 rhash => { char => '%', value_magic => 1,
99 desc => 'extra data for restricted hashes' },
100 arylen_p => { char => '@', value_magic => 1,
101 desc => 'to move arylen out of XPVAV' },
6f83ef0e 102 ext => { char => '~', desc => 'Available for use by extensions' },
e0a73de4
NC
103 checkcall => { char => ']', value_magic => 1,
104 desc => 'inlining/mutation of call to this CV'},
6f83ef0e
NC
105);
106
0a1f728a 107# These have a subtly different "namespace" from the magic types.
8b09643d
NC
108my @sig =
109 (
110 'sv' => {get => 'get', set => 'set', len => 'len'},
111 'env' => {set => 'set_all_env', clear => 'clear_all_env'},
112 'envelem' => {set => 'setenv', clear => 'clearenv'},
113 'sigelem' => {get => 'getsig', set => 'setsig', clear => 'clearsig',
114 cond => '#ifndef PERL_MICRO'},
115 'pack' => {len => 'sizepack', clear => 'wipepack'},
116 'packelem' => {get => 'getpack', set => 'setpack', clear => 'clearpack'},
117 'dbline' => {set => 'setdbline'},
118 'isa' => {set => 'setisa', clear => 'clearisa'},
119 'isaelem' => {set => 'setisa'},
120 'arylen' => {get => 'getarylen', set => 'setarylen', const => 1},
121 'arylen_p' => {free => 'freearylen_p'},
122 'mglob' => {set => 'setmglob'},
123 'nkeys' => {get => 'getnkeys', set => 'setnkeys'},
124 'taint' => {get => 'gettaint', set => 'settaint'},
125 'substr' => {get => 'getsubstr', set => 'setsubstr'},
126 'vec' => {get => 'getvec', set => 'setvec'},
127 'pos' => {get => 'getpos', set => 'setpos'},
8b09643d
NC
128 'uvar' => {get => 'getuvar', set => 'setuvar'},
129 'defelem' => {get => 'getdefelem', set => 'setdefelem'},
b2e9fc6f 130 'regexp' => {set => 'setregexp', alias => [qw(bm fm)]},
8b09643d
NC
131 'regdata' => {len => 'regdata_cnt'},
132 'regdatum' => {get => 'regdatum_get', set => 'regdatum_set'},
133 'amagic' => {set => 'setamagic', free => 'setamagic'},
134 'amagicelem' => {set => 'setamagic', free => 'setamagic'},
135 'backref' => {free => 'killbackrefs'},
136 'ovrld' => {free => 'freeovrld'},
137 'utf8' => {set => 'setutf8'},
138 'collxfrm' => {set => 'setcollxfrm',
139 cond => '#ifdef USE_LOCALE_COLLATE'},
140 'hintselem' => {set => 'sethint', clear => 'clearhint'},
141 'hints' => {clear => 'clearhints'},
142);
143
52f49505 144my ($vt, $raw, $names) = map {
6f83ef0e
NC
145 open_new($_, '>',
146 { by => 'regen/mg_vtable.pl', file => $_, style => '*' });
52f49505 147} 'mg_vtable.h', 'mg_raw.h', 'mg_names.c';
6f83ef0e 148
abf9aa7a
NC
149print $vt <<'EOH';
150/* These constants should be used in preference to raw characters
151 * when using magic. Note that some perl guts still assume
152 * certain character properties of these constants, namely that
153 * isUPPER() and toLOWER() may do useful mappings.
154 */
155
156EOH
157
158my $longest = 0;
159foreach (grep {!ref $_} @mg) {
160 $longest = length $_ if length $_ > $longest;
161}
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{
52f49505
NC
173 my $longest_p1 = $longest + 1;
174
6f83ef0e
NC
175 while (my ($name, $data) = splice @mg, 0, 2) {
176 my $i = ord eval qq{"$data->{char}"};
177 unless ($data->{unknown_to_sv_magic}) {
e0a73de4 178 my $value = $data->{vtable}
6f83ef0e 179 ? "want_vtbl_$data->{vtable}" : 'magic_vtable_max';
82ff486e
NC
180 $value .= ' | PERL_MAGIC_READONLY_ACCEPTABLE'
181 if $data->{readonly_acceptable};
e0a73de4 182 $value .= ' | PERL_MAGIC_VALUE_MAGIC' if $data->{value_magic};
6f83ef0e
NC
183 my $comment = "/* $name '$data->{char}' $data->{desc} */";
184 $comment =~ s/([\\"])/\\$1/g;
abf9aa7a 185 $comment =~ tr/\n/ /;
e0a73de4 186 print $raw qq{ { '$data->{char}', "$value",\n "$comment" },\n};
6f83ef0e 187 }
abf9aa7a
NC
188
189 my $comment = $data->{desc};
190 my $leader = ' ' x ($longest + 27);
191 $comment =~ s/\n/\n$leader/s;
192 printf $vt "#define PERL_MAGIC_%-${longest}s '%s' /* %s */\n",
193 $name, $data->{char}, $comment;
52f49505
NC
194
195 my $char = $data->{char};
196 $char =~ s/([\\"])/\\$1/g;
197 printf $names qq[\t{ PERL_MAGIC_%-${longest_p1}s "%s(%s)" },\n],
198 "$name,", $name, $char;
6f83ef0e
NC
199 }
200}
8b09643d 201
ca298f7d 202{
2d1f1fe5
NC
203 my @names = grep {!ref $_} @sig;
204 my $want = join ",\n ", (map {"want_vtbl_$_"} @names), 'magic_vtable_max';
205 my $names = join qq{",\n "}, @names;
206
6f83ef0e 207 print $vt <<"EOH";
abf9aa7a 208
ca298f7d 209enum { /* pass one of these to get_vtbl */
2d1f1fe5
NC
210 $want
211};
212
213#ifdef DOINIT
214EXTCONST char *PL_magic_vtable_names[magic_vtable_max] = {
215 "$names"
ca298f7d 216};
2d1f1fe5
NC
217#else
218EXTCONST char *PL_magic_vtable_names[magic_vtable_max];
219#endif
ca298f7d
NC
220
221EOH
222}
223
6f83ef0e 224print $vt <<'EOH';
8b09643d
NC
225/* These all need to be 0, not NULL, as NULL can be (void*)0, which is a
226 * pointer to data, whereas we're assigning pointers to functions, which are
227 * not the same beast. ANSI doesn't allow the assignment from one to the other.
228 * (although most, but not all, compilers are prepared to do it)
229 */
230
0ffb5b03 231/* order is:
8b09643d
NC
232 get
233 set
234 len
235 clear
236 free
237 copy
238 dup
239 local
240*/
241
b7b5e578 242#ifdef DOINIT
c7fdacb9 243EXT_MGVTBL PL_magic_vtables[magic_vtable_max] = {
8b09643d
NC
244EOH
245
b7b5e578 246my @vtable_names;
b2e9fc6f 247my @aliases;
b7b5e578 248
8b09643d 249while (my ($name, $data) = splice @sig, 0, 2) {
b7b5e578 250 push @vtable_names, $name;
0a1f728a 251 my @funcs = map {
8b09643d
NC
252 $data->{$_} ? "Perl_magic_$data->{$_}" : 0;
253 } qw(get set len clear free copy dup local);
254
0a1f728a 255 $funcs[0] = "(int (*)(pTHX_ SV *, MAGIC *))" . $funcs[0] if $data->{const};
0ffb5b03 256 my $funcs = join ", ", @funcs;
8b09643d 257
b7b5e578
NC
258 # Because we can't have a , after the last {...}
259 my $comma = @sig ? ',' : '';
260
6f83ef0e
NC
261 print $vt "$data->{cond}\n" if $data->{cond};
262 print $vt " { $funcs }$comma\n";
263 print $vt <<"EOH" if $data->{cond};
0ffb5b03 264#else
b7b5e578 265 { 0, 0, 0, 0, 0, 0, 0, 0 }$comma
0ffb5b03 266#endif
b7b5e578 267EOH
b2e9fc6f
NC
268 foreach(@{$data->{alias}}) {
269 push @aliases, "#define want_vtbl_$_ want_vtbl_$name\n";
270 push @vtable_names, $_;
271 }
8b09643d
NC
272}
273
6f83ef0e 274print $vt <<'EOH';
b7b5e578
NC
275};
276#else
c7fdacb9 277EXT_MGVTBL PL_magic_vtables[magic_vtable_max];
b7b5e578
NC
278#endif
279
280EOH
281
6f83ef0e 282print $vt (sort @aliases), "\n";
b7b5e578 283
6f83ef0e 284print $vt "#define PL_vtbl_$_ PL_magic_vtables[want_vtbl_$_]\n"
b7b5e578
NC
285 foreach sort @vtable_names;
286
e0a73de4
NC
287# 63, not 64, As we rely on the last possible value to mean "NULL vtable"
288die "Too many vtable names" if @vtable_names > 63;
289
52f49505 290read_only_bottom_close_and_rename($_) foreach $vt, $raw, $names;