This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta - move split change to other perlfunc changes and add issue link
[perl5.git] / regen / mg_vtable.pl
CommitLineData
8b09643d
NC
1#!/usr/bin/perl -w
2#
3# Regenerate (overwriting only if changed):
4#
16bc0f48 5# mg_names.inc
130e6ef5 6# mg_raw.h
8b09643d 7# mg_vtable.h
1083e5c1 8# pod/perlguts.pod
8b09643d 9#
1083e5c1
FC
10# from information stored in this file. pod/perlguts.pod is not completely
11# regenerated. Only the magic table is replaced; the other parts remain
12# untouched.
8b09643d
NC
13#
14# Accepts the standard regen_lib -q and -v args.
15#
16# This script is normally invoked from regen.pl.
e558f276
DM
17#
18# Its output files contain:
19#
20# mg_names.inc
21# included by dump.c - the textual representation for each magic type.
22# Contains a list of
23#
24# { PERL_MAGIC_foo, "foo(f)" }
25#
26# pairs.
27#
28#
29# mg_raw.h
30# processed by generate_uudmap.c into mg_data.h which eventually
31# populates PL_magic_vtables[].
32#
33# Contains a list of:
34#
35# { 'f', "want_vtbl_foo | FLAGS", "description for comments" }
36#
37# triplets. FLAGS can be:
38# PERL_MAGIC_READONLY_ACCEPTABLE
39# ok set this type of magic on an SvREADONLY() SV
40# PERL_MAGIC_VALUE_MAGIC
41# this is value magic (pos, taint etc)
42# rather than container magic (%ENV, $1 etc)
43#
44#
45# mg_vtable.h
46# This contains five kinds of entries:
47#
48# #define PERL_MAGIC_arylen '#'
49# ....
50#
51# enum { /* pass one of these to get_vtbl */
52# want_vtbl_arylen,
53# ...
54# }
55#
56# PL_magic_vtable_names[] = {
57# "arylen",
58# ...
59# }
60#
61# PL_magic_vtables[] = {
62# /* per-magic sets of vtable function pointers */
63# { get, set, len, clear, free, copy, dup, local },
64# }
65#
66# define PL_vtbl_arylen PL_magic_vtables[want_vtbl_arylen]
67# ....
68#
69#
70#
71# pod/perlguts.pod
72# updates the list of magic types between
73# =for mg_vtable.pl begin
74# ...
75# =for mg_vtable.pl end
76
8b09643d
NC
77
78use strict;
79require 5.004;
80
81BEGIN {
82 # Get function prototypes
3d7c117d 83 require './regen/regen_lib.pl';
8b09643d
NC
84}
85
e558f276
DM
86# =====================================================================
87#
88# START OF CONFIGURATION DATA
89
90
91# %mg
92#
93# This hash is mainly concerned with populating all the other stuff
94# ancillary to the vtable.
95#
96# The key is the name, e.g. 'regdata' for PERL_MAGIC_regdata
97#
98# The keys of the value hash are:
99# char
100# the magic's char identifier
101#
102# desc
103# a description which appears in code comments in generated files
104#
105# readonly_acceptable
106# If true, set PERL_MAGIC_READONLY_ACCEPTABLE flag;
107# SvREADONLY() svs are allowed to have this magic added to them
108#
109# unknown_to_sv_magic
110# if true, this isn't one of the standard magic types which
111# Perl_sv_magic() knows how to deal with
112#
113# value_magic
114# If true, set PERL_MAGIC_VALUE_MAGIC flag;
115# this kind of magic is value (pos, taint etc) rather than
116# container magic (%ENV, $1 etc)
117#
118# vtable
119# name of the vtable lookup enum, e.g. 'foo' creates want_vtbl_foo
120
23cfd2fc 121my %mg =
6f83ef0e 122 (
9cce4f9a 123 sv => { char => "\0", vtable => 'sv', readonly_acceptable => 1,
9824c081 124 desc => 'Special scalar variable' },
bd6e6c12
FC
125 # overload, or type "A" magic, used to be here. Hence overloaded is
126 # often called AMAGIC internally, even though it does not use "A"
127 # magic any more.
6f83ef0e 128 overload_table => { char => 'c', vtable => 'ovrld',
9824c081 129 desc => 'Holds overload table (AMT) on stash' },
e0a73de4 130 bm => { char => 'B', vtable => 'regexp', value_magic => 1,
9824c081
MS
131 readonly_acceptable => 1,
132 desc => 'Boyer-Moore (fast string search)' },
6f83ef0e 133 regdata => { char => 'D', vtable => 'regdata',
9824c081 134 desc => "Regex match position data\n(\@+ and \@- vars)" },
6f83ef0e 135 regdatum => { char => 'd', vtable => 'regdatum',
9824c081 136 desc => 'Regex match position data element' },
6f83ef0e
NC
137 env => { char => 'E', vtable => 'env', desc => '%ENV hash' },
138 envelem => { char => 'e', vtable => 'envelem',
9824c081 139 desc => '%ENV hash element' },
eccba044 140 fm => { char => 'f', vtable => 'regexp', value_magic => 1,
9824c081 141 readonly_acceptable => 1, desc => "Formline ('compiled' format)" },
e0a73de4 142 regex_global => { char => 'g', vtable => 'mglob', value_magic => 1,
9824c081 143 readonly_acceptable => 1, desc => 'm//g target' },
6f83ef0e
NC
144 hints => { char => 'H', vtable => 'hints', desc => '%^H hash' },
145 hintselem => { char => 'h', vtable => 'hintselem',
9824c081 146 desc => '%^H hash element' },
6f83ef0e
NC
147 isa => { char => 'I', vtable => 'isa', desc => '@ISA array' },
148 isaelem => { char => 'i', vtable => 'isaelem',
9824c081 149 desc => '@ISA array element' },
e0a73de4 150 nkeys => { char => 'k', vtable => 'nkeys', value_magic => 1,
9824c081 151 desc => 'scalar(keys()) lvalue' },
f34d1562 152 dbfile => { char => 'L',
9824c081 153 desc => 'Debugger %_<filename' },
f34d1562 154 dbline => { char => 'l', vtable => 'dbline',
9824c081 155 desc => 'Debugger %_<filename element' },
6f83ef0e 156 shared => { char => 'N', desc => 'Shared between threads',
9824c081 157 unknown_to_sv_magic => 1 },
6f83ef0e 158 shared_scalar => { char => 'n', desc => 'Shared between threads',
9824c081 159 unknown_to_sv_magic => 1 },
e0a73de4 160 collxfrm => { char => 'o', vtable => 'collxfrm', value_magic => 1,
9824c081 161 desc => 'Locale transformation' },
e0a73de4 162 tied => { char => 'P', vtable => 'pack',
9824c081
MS
163 value_magic => 1, # treat as value, so 'local @tied' isn't tied
164 desc => 'Tied array or hash' },
6f83ef0e 165 tiedelem => { char => 'p', vtable => 'packelem',
9824c081 166 desc => 'Tied array or hash element' },
6f83ef0e 167 tiedscalar => { char => 'q', vtable => 'packelem',
9824c081 168 desc => 'Tied scalar or handle' },
e0a73de4 169 qr => { char => 'r', vtable => 'regexp', value_magic => 1,
9824c081 170 readonly_acceptable => 1, desc => 'Precompiled qr// regex' },
6f83ef0e
NC
171 sig => { char => 'S', desc => '%SIG hash' },
172 sigelem => { char => 's', vtable => 'sigelem',
9824c081 173 desc => '%SIG hash element' },
e0a73de4 174 taint => { char => 't', vtable => 'taint', value_magic => 1,
9824c081 175 desc => 'Taintedness' },
6f83ef0e 176 uvar => { char => 'U', vtable => 'uvar',
9824c081 177 desc => 'Available for use by extensions' },
6f83ef0e 178 uvar_elem => { char => 'u', desc => 'Reserved for use by extensions',
9824c081 179 unknown_to_sv_magic => 1 },
e0a73de4 180 vec => { char => 'v', vtable => 'vec', value_magic => 1,
9824c081 181 desc => 'vec() lvalue' },
4499db73 182 vstring => { char => 'V', value_magic => 1,
9824c081 183 desc => 'SV was vstring literal' },
e0a73de4 184 utf8 => { char => 'w', vtable => 'utf8', value_magic => 1,
9824c081 185 desc => 'Cached UTF-8 information' },
e0a73de4 186 substr => { char => 'x', vtable => 'substr', value_magic => 1,
9824c081 187 desc => 'substr() lvalue' },
e0a73de4 188 defelem => { char => 'y', vtable => 'defelem', value_magic => 1,
9824c081 189 desc => "Shadow \"foreach\" iterator variable /\nsmart parameter vivification" },
1f1dcfb5 190 nonelem => { char => 'Y', vtable => 'nonelem', value_magic => 1,
9824c081 191 desc => "Array element that does not exist" },
e0a73de4 192 arylen => { char => '#', vtable => 'arylen', value_magic => 1,
9824c081 193 desc => 'Array length ($#ary)' },
e0a73de4 194 pos => { char => '.', vtable => 'pos', value_magic => 1,
9824c081 195 desc => 'pos() lvalue' },
e0a73de4 196 backref => { char => '<', vtable => 'backref', value_magic => 1,
9824c081 197 readonly_acceptable => 1, desc => 'For weak ref data' },
e0a73de4 198 symtab => { char => ':', value_magic => 1,
9824c081 199 desc => 'Extra data for symbol tables' },
e0a73de4 200 rhash => { char => '%', value_magic => 1,
9824c081 201 desc => 'Extra data for restricted hashes' },
e0a73de4 202 arylen_p => { char => '@', value_magic => 1,
9824c081 203 desc => 'To move arylen out of XPVAV' },
1d5686ec 204 ext => { char => '~', desc => 'Available for use by extensions',
9824c081 205 readonly_acceptable => 1 },
09fb282d 206 checkcall => { char => ']', value_magic => 1, vtable => 'checkcall',
9824c081 207 desc => 'Inlining/mutation of call to this CV'},
a6d69523 208 debugvar => { char => '*', desc => '$DB::single, signal, trace vars',
9824c081 209 vtable => 'debugvar' },
9cce4f9a 210 lvref => { char => '\\', vtable => 'lvref',
9824c081 211 desc => "Lvalue reference constructor" },
6f83ef0e
NC
212);
213
e558f276
DM
214
215# %sig
216#
217# This hash is mainly concerned with populating the vtable.
218# (despite the name it has nothing to do with signals!)
219#
0a1f728a 220# These have a subtly different "namespace" from the magic types.
e558f276
DM
221#
222# The key is the name, e.g. 'regdata' for PERL_MAGIC_regdata
223# The keys of the value hash are:
224#
225# alias
226# for each entry in the anon array, add
227# add "#define want_vtbl_$_ want_vtbl_$name"
228#
229# cond
230# prefix the vtable with the specified entry (e.g. '#ifdef FOO')
231# and suffix it with '#else { 0, 0, 0, 0, 0, 0, 0, 0 } #endif'
232#
233# const
234# special-case cast a 'get' function whose signature expects
235# a pointer to constant magic, so that it can be added to a vtable
236# which expects pointers to functions without the 'const'.
237#
238# get
239# set
240# len
241# clear
242# free
243# copy
244# dup
245# local
246# For each specified method, add a vtable function pointer
247# of the form "Perl_magic_$sig{foo}{get}" etc
248
23cfd2fc 249my %sig =
8b09643d 250 (
9bb29b68 251 'sv' => {get => 'get', set => 'set'},
8b09643d
NC
252 'env' => {set => 'set_all_env', clear => 'clear_all_env'},
253 'envelem' => {set => 'setenv', clear => 'clearenv'},
254 'sigelem' => {get => 'getsig', set => 'setsig', clear => 'clearsig',
9824c081 255 cond => '#ifndef PERL_MICRO'},
8b09643d
NC
256 'pack' => {len => 'sizepack', clear => 'wipepack'},
257 'packelem' => {get => 'getpack', set => 'setpack', clear => 'clearpack'},
258 'dbline' => {set => 'setdbline'},
259 'isa' => {set => 'setisa', clear => 'clearisa'},
260 'isaelem' => {set => 'setisa'},
261 'arylen' => {get => 'getarylen', set => 'setarylen', const => 1},
83f29afa 262 'arylen_p' => {clear => 'cleararylen_p', free => 'freearylen_p'},
02a48966
DM
263 'mglob' => {set => 'setmglob',
264 free => 'freemglob' },
8b09643d
NC
265 'nkeys' => {get => 'getnkeys', set => 'setnkeys'},
266 'taint' => {get => 'gettaint', set => 'settaint'},
267 'substr' => {get => 'getsubstr', set => 'setsubstr'},
268 'vec' => {get => 'getvec', set => 'setvec'},
269 'pos' => {get => 'getpos', set => 'setpos'},
8b09643d
NC
270 'uvar' => {get => 'getuvar', set => 'setuvar'},
271 'defelem' => {get => 'getdefelem', set => 'setdefelem'},
1f1dcfb5 272 'nonelem' => {set => 'setnonelem'},
b2e9fc6f 273 'regexp' => {set => 'setregexp', alias => [qw(bm fm)]},
8b09643d
NC
274 'regdata' => {len => 'regdata_cnt'},
275 'regdatum' => {get => 'regdatum_get', set => 'regdatum_set'},
8b09643d
NC
276 'backref' => {free => 'killbackrefs'},
277 'ovrld' => {free => 'freeovrld'},
032a4919
DM
278 'utf8' => {set => 'setutf8',
279 free => 'freeutf8' },
8b09643d 280 'collxfrm' => {set => 'setcollxfrm',
a457b73c 281 free => 'freecollxfrm',
9824c081 282 cond => '#ifdef USE_LOCALE_COLLATE'},
8b09643d
NC
283 'hintselem' => {set => 'sethint', clear => 'clearhint'},
284 'hints' => {clear => 'clearhints'},
09fb282d 285 'checkcall' => {copy => 'copycallchecker'},
a6d69523 286 'debugvar' => { set => 'setdebugvar', get => 'getdebugvar' },
9cce4f9a 287 'lvref' => {set => 'setlvref'},
8b09643d
NC
288);
289
e558f276
DM
290
291# END OF CONFIGURATION DATA
292#
293# =====================================================================
294
295
296
52f49505 297my ($vt, $raw, $names) = map {
6f83ef0e 298 open_new($_, '>',
9824c081 299 { by => 'regen/mg_vtable.pl', file => $_, style => '*' });
16bc0f48 300} 'mg_vtable.h', 'mg_raw.h', 'mg_names.inc';
f1f5ddd7 301my $guts = open_new("pod/perlguts.pod", ">");
6f83ef0e 302
abf9aa7a
NC
303print $vt <<'EOH';
304/* These constants should be used in preference to raw characters
305 * when using magic. Note that some perl guts still assume
306 * certain character properties of these constants, namely that
307 * isUPPER() and toLOWER() may do useful mappings.
308 */
309
310EOH
311
6f83ef0e
NC
312# Of course, it would be *much* easier if we could output this table directly
313# here and now. However, for our sins, we try to support EBCDIC, which wouldn't
314# be *so* bad, except that there are (at least) 3 EBCDIC charset variants, and
315# they don't agree on the code point for '~'. Which we use. Great.
316# So we have to get the local build runtime to sort our table in character order
317# (And of course, just to be helpful, in POSIX BC '~' is \xFF, so we can't even
318# simplify the C code by assuming that the last element of the array is
319# predictable)
320
e558f276
DM
321# Process %mg
322
6f83ef0e 323{
e972d315
NC
324 my $longest = 0;
325 foreach (keys %mg) {
9824c081 326 $longest = length $_ if length $_ > $longest;
e972d315
NC
327 }
328
52f49505
NC
329 my $longest_p1 = $longest + 1;
330
23cfd2fc
NC
331 my %mg_order;
332 while (my ($name, $data) = each %mg) {
9824c081
MS
333 my $byte = $data->{char};
334 if ($byte =~ /[[:print:]]/) {
335 $data->{r_char} = $byte; # readable char
336 ($data->{c_char} = $byte) =~ s/([\\"])/\\$1/g; # for C strings
337 }
338 else {
339 $data->{c_char} = $data->{r_char} = '\\'.ord $byte;
340 }
341 $mg_order{(uc $byte) . $byte} = $name;
23cfd2fc 342 }
e558f276 343
63e77aaf 344 my @rows;
f2f5335a 345 my @names;
23cfd2fc 346 foreach (sort keys %mg_order) {
9824c081 347 my $name = $mg_order{$_};
f2f5335a 348 push @names, $name;
9824c081
MS
349 my $data = $mg{$name};
350 my $i = ord $data->{char};
e558f276
DM
351
352 # add entry to mg_raw.h
353
9824c081
MS
354 unless ($data->{unknown_to_sv_magic}) {
355 my $value = $data->{vtable}
356 ? "want_vtbl_$data->{vtable}" : 'magic_vtable_max';
357 $value .= ' | PERL_MAGIC_READONLY_ACCEPTABLE'
358 if $data->{readonly_acceptable};
359 $value .= ' | PERL_MAGIC_VALUE_MAGIC' if $data->{value_magic};
360 my $comment = "/* $name '$data->{r_char}' $data->{desc} */";
361 $comment =~ s/([\\"])/\\$1/g;
362 $comment =~ tr/\n/ /;
363 print $raw qq{ { '$data->{c_char}', "$value",\n "$comment" },\n};
364 }
abf9aa7a 365
e558f276
DM
366 # add #define PERL_MAGIC_foo entry to vt_table.h
367
9824c081
MS
368 my $comment = $data->{desc};
369 my $leader = ' ' x ($longest + 27);
370 $comment =~ s/\n/\n$leader/s;
371 printf $vt "#define PERL_MAGIC_%-${longest}s '%s' /* %s */\n",
372 $name, $data->{c_char}, $comment;
52f49505 373
e558f276
DM
374 # add entry to mg_names.inc
375
9824c081
MS
376 my $char = $data->{r_char};
377 $char =~ s/([\\"])/\\$1/g;
378 printf $names qq[\t{ PERL_MAGIC_%-${longest_p1}s "%s(%s)" },\n],
379 "$name,", $name, $char;
63e77aaf 380
e558f276
DM
381 # construct perlguts.pod entry
382
9824c081
MS
383 push @rows, [(sprintf "%-2s PERL_MAGIC_%s", $data->{r_char},$name),
384 $data->{vtable} ? "vtbl_$data->{vtable}" : '(none)',
385 $data->{desc}];
63e77aaf 386 }
e558f276
DM
387
388 # output @rows to perlguts.pod
389
f1f5ddd7 390 select +(select($guts), do {
9824c081
MS
391 my @header = ('(old-style char and macro)', 'MGVTBL', 'Type of magic');
392 my @widths = (0, 0);
393 foreach my $row (@rows) {
394 for (0, 1) {
395 $widths[$_] = length $row->[$_]
396 if length $row->[$_] > $widths[$_];
397 }
398 }
399 my $indent = ' ';
400 my $format
401 = sprintf "$indent%%-%ds%%-%ds%%s\n", $widths[0] + 1, $widths[1] + 1;
402 my $desc_wrap =
403 79 - 7 - (length $indent) - $widths[0] - $widths[1] - 2;
404
405 open my $oldguts, "<", "pod/perlguts.pod"
406 or die "$0 cannot open pod/perlguts.pod for reading: $!";
407 while (<$oldguts>) {
408 print;
409 last if /^=for mg_vtable.pl begin/
410 }
411
412 print "\n", $indent . "mg_type\n";
413 printf $format, @header;
414 printf $format, map {'-' x length $_} @header;
415 foreach (@rows) {
416 my ($type, $vtbl, $desc) = @$_;
417 $desc =~ tr/\n/ /;
418 my @cont;
419 if (length $desc > $desc_wrap) {
420 # If it's too long, first split on '(', if there.
421 # [Which, if there, is always short enough, currently.
422 # Make this more robust if that changes]
423 ($desc, @cont) = split /(?=\()/, $desc;
424 if (!@cont) {
425 ($desc, @cont) = $desc =~ /(.{1,$desc_wrap})(?: |\z)/g
426 }
427 }
428 printf $format, $type, $vtbl, $desc;
429 printf $format, '', '', $_ foreach @cont;
430 }
431 print "\n\n";
f2f5335a 432
eb0444cb
KW
433 my $first = 1;
434 for my $magic (sort @names) {
435 if ($first) {
436 $first = 0;
437 print "=for apidoc AmnhU||PERL_MAGIC_$magic\n";
438 }
439 else {
440 print "=for apidoc_item ||PERL_MAGIC_$magic\n";
441 }
ed48408e 442 }
f2f5335a 443 print "\n";
f1f5ddd7 444
9824c081
MS
445 while (<$oldguts>) {
446 last if /^=for mg_vtable.pl end/;
447 }
448 do { print } while <$oldguts>;
f1f5ddd7 449 })[0];
6f83ef0e 450}
8b09643d 451
e558f276
DM
452
453# Process %sig - everything goes to mg_vtable.h
454
23cfd2fc 455my @names = sort keys %sig;
ca298f7d 456{
2d1f1fe5
NC
457 my $want = join ",\n ", (map {"want_vtbl_$_"} @names), 'magic_vtable_max';
458 my $names = join qq{",\n "}, @names;
459
6f83ef0e 460 print $vt <<"EOH";
abf9aa7a 461
ca298f7d 462enum { /* pass one of these to get_vtbl */
2d1f1fe5
NC
463 $want
464};
465
466#ifdef DOINIT
bfd14e52 467EXTCONST char * const PL_magic_vtable_names[magic_vtable_max] = {
2d1f1fe5 468 "$names"
ca298f7d 469};
2d1f1fe5 470#else
bfd14e52 471EXTCONST char * const PL_magic_vtable_names[magic_vtable_max];
2d1f1fe5 472#endif
ca298f7d
NC
473
474EOH
475}
476
6f83ef0e 477print $vt <<'EOH';
8b09643d
NC
478/* These all need to be 0, not NULL, as NULL can be (void*)0, which is a
479 * pointer to data, whereas we're assigning pointers to functions, which are
480 * not the same beast. ANSI doesn't allow the assignment from one to the other.
481 * (although most, but not all, compilers are prepared to do it)
482 */
483
0ffb5b03 484/* order is:
8b09643d
NC
485 get
486 set
487 len
488 clear
489 free
490 copy
491 dup
492 local
493*/
494
b7b5e578 495#ifdef DOINIT
c7fdacb9 496EXT_MGVTBL PL_magic_vtables[magic_vtable_max] = {
8b09643d
NC
497EOH
498
b7b5e578 499my @vtable_names;
b2e9fc6f 500my @aliases;
b7b5e578 501
23cfd2fc
NC
502while (my $name = shift @names) {
503 my $data = $sig{$name};
b7b5e578 504 push @vtable_names, $name;
0a1f728a 505 my @funcs = map {
9824c081 506 $data->{$_} ? "Perl_magic_$data->{$_}" : 0;
8b09643d
NC
507 } qw(get set len clear free copy dup local);
508
0a1f728a 509 $funcs[0] = "(int (*)(pTHX_ SV *, MAGIC *))" . $funcs[0] if $data->{const};
0ffb5b03 510 my $funcs = join ", ", @funcs;
8b09643d 511
b7b5e578 512 # Because we can't have a , after the last {...}
23cfd2fc 513 my $comma = @names ? ',' : '';
b7b5e578 514
6f83ef0e
NC
515 print $vt "$data->{cond}\n" if $data->{cond};
516 print $vt " { $funcs }$comma\n";
517 print $vt <<"EOH" if $data->{cond};
0ffb5b03 518#else
b7b5e578 519 { 0, 0, 0, 0, 0, 0, 0, 0 }$comma
0ffb5b03 520#endif
b7b5e578 521EOH
b2e9fc6f 522 foreach(@{$data->{alias}}) {
9824c081
MS
523 push @aliases, "#define want_vtbl_$_ want_vtbl_$name\n";
524 push @vtable_names, $_;
b2e9fc6f 525 }
8b09643d
NC
526}
527
6f83ef0e 528print $vt <<'EOH';
b7b5e578
NC
529};
530#else
c7fdacb9 531EXT_MGVTBL PL_magic_vtables[magic_vtable_max];
b7b5e578
NC
532#endif
533
534EOH
535
6f83ef0e 536print $vt (sort @aliases), "\n";
b7b5e578 537
6f83ef0e 538print $vt "#define PL_vtbl_$_ PL_magic_vtables[want_vtbl_$_]\n"
b7b5e578
NC
539 foreach sort @vtable_names;
540
e0a73de4
NC
541# 63, not 64, As we rely on the last possible value to mean "NULL vtable"
542die "Too many vtable names" if @vtable_names > 63;
543
52f49505 544read_only_bottom_close_and_rename($_) foreach $vt, $raw, $names;
9824c081 545 close_and_rename($guts);