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