This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Refactor Perl_get_vtbl() to a small array lookup from a large switch statement.
[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
0a1f728a 21# These have a subtly different "namespace" from the magic types.
8b09643d
NC
22my @sig =
23 (
24 'sv' => {get => 'get', set => 'set', len => 'len'},
25 'env' => {set => 'set_all_env', clear => 'clear_all_env'},
26 'envelem' => {set => 'setenv', clear => 'clearenv'},
27 'sigelem' => {get => 'getsig', set => 'setsig', clear => 'clearsig',
28 cond => '#ifndef PERL_MICRO'},
29 'pack' => {len => 'sizepack', clear => 'wipepack'},
30 'packelem' => {get => 'getpack', set => 'setpack', clear => 'clearpack'},
31 'dbline' => {set => 'setdbline'},
32 'isa' => {set => 'setisa', clear => 'clearisa'},
33 'isaelem' => {set => 'setisa'},
34 'arylen' => {get => 'getarylen', set => 'setarylen', const => 1},
35 'arylen_p' => {free => 'freearylen_p'},
36 'mglob' => {set => 'setmglob'},
37 'nkeys' => {get => 'getnkeys', set => 'setnkeys'},
38 'taint' => {get => 'gettaint', set => 'settaint'},
39 'substr' => {get => 'getsubstr', set => 'setsubstr'},
40 'vec' => {get => 'getvec', set => 'setvec'},
41 'pos' => {get => 'getpos', set => 'setpos'},
42 'bm' => {set => 'setregexp'},
43 'fm' => {set => 'setregexp'},
44 'uvar' => {get => 'getuvar', set => 'setuvar'},
45 'defelem' => {get => 'getdefelem', set => 'setdefelem'},
46 'regexp' => {set => 'setregexp'},
47 'regdata' => {len => 'regdata_cnt'},
48 'regdatum' => {get => 'regdatum_get', set => 'regdatum_set'},
49 'amagic' => {set => 'setamagic', free => 'setamagic'},
50 'amagicelem' => {set => 'setamagic', free => 'setamagic'},
51 'backref' => {free => 'killbackrefs'},
52 'ovrld' => {free => 'freeovrld'},
53 'utf8' => {set => 'setutf8'},
54 'collxfrm' => {set => 'setcollxfrm',
55 cond => '#ifdef USE_LOCALE_COLLATE'},
56 'hintselem' => {set => 'sethint', clear => 'clearhint'},
57 'hints' => {clear => 'clearhints'},
58);
59
60my $h = open_new('mg_vtable.h', '>',
61 { by => 'regen/mg_vtable.pl', file => 'mg_vtable.h',
62 style => '*' });
63
ca298f7d
NC
64{
65 my @names = map {"want_vtbl_$_"} grep {!ref $_} @sig;
c7fdacb9 66 push @names, 'magic_vtable_max';
ca298f7d
NC
67 local $" = ",\n ";
68 print $h <<"EOH";
69enum { /* pass one of these to get_vtbl */
70 @names
71};
72
73EOH
74}
75
8b09643d
NC
76print $h <<'EOH';
77/* These all need to be 0, not NULL, as NULL can be (void*)0, which is a
78 * pointer to data, whereas we're assigning pointers to functions, which are
79 * not the same beast. ANSI doesn't allow the assignment from one to the other.
80 * (although most, but not all, compilers are prepared to do it)
81 */
82
0ffb5b03 83/* order is:
8b09643d
NC
84 get
85 set
86 len
87 clear
88 free
89 copy
90 dup
91 local
92*/
93
b7b5e578 94#ifdef DOINIT
c7fdacb9 95EXT_MGVTBL PL_magic_vtables[magic_vtable_max] = {
8b09643d
NC
96EOH
97
b7b5e578
NC
98my @vtable_names;
99
8b09643d 100while (my ($name, $data) = splice @sig, 0, 2) {
b7b5e578 101 push @vtable_names, $name;
0a1f728a 102 my @funcs = map {
8b09643d
NC
103 $data->{$_} ? "Perl_magic_$data->{$_}" : 0;
104 } qw(get set len clear free copy dup local);
105
0a1f728a 106 $funcs[0] = "(int (*)(pTHX_ SV *, MAGIC *))" . $funcs[0] if $data->{const};
0ffb5b03 107 my $funcs = join ", ", @funcs;
8b09643d 108
b7b5e578
NC
109 # Because we can't have a , after the last {...}
110 my $comma = @sig ? ',' : '';
111
8b09643d 112 print $h "$data->{cond}\n" if $data->{cond};
b7b5e578
NC
113 print $h " { $funcs }$comma\n";
114 print $h <<"EOH" if $data->{cond};
0ffb5b03 115#else
b7b5e578 116 { 0, 0, 0, 0, 0, 0, 0, 0 }$comma
0ffb5b03 117#endif
b7b5e578 118EOH
8b09643d
NC
119}
120
b7b5e578
NC
121print $h <<'EOH';
122};
123#else
c7fdacb9 124EXT_MGVTBL PL_magic_vtables[magic_vtable_max];
b7b5e578
NC
125#endif
126
127EOH
128
129
130print $h "#define PL_vtbl_$_ PL_magic_vtables[want_vtbl_$_]\n"
131 foreach sort @vtable_names;
132
8b09643d 133read_only_bottom_close_and_rename($h);