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