This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Sort magic and magic vtable names in files generated by mg_vtable.pl
[perl5.git] / regen / embed.pl
CommitLineData
5f05dabc 1#!/usr/bin/perl -w
6294c161
DM
2#
3# Regenerate (overwriting only if changed):
4#
5# embed.h
6# embedvar.h
7# global.sym
8# perlapi.c
9# perlapi.h
10# proto.h
11#
12# from information stored in
13#
14# embed.fnc
15# intrpvar.h
16# perlvars.h
897d3989 17# regen/opcodes
6294c161 18#
6294c161
DM
19# Accepts the standard regen_lib -q and -v args.
20#
21# This script is normally invoked from regen.pl.
e50aee73 22
916e4025 23require 5.004; # keep this compatible, an old perl is all we may have before
954c1994 24 # we build the new one
5f05dabc 25
88e01c9d
AL
26use strict;
27
36bb303b
NC
28BEGIN {
29 # Get function prototypes
af001346 30 require 'regen/regen_lib.pl';
36bb303b
NC
31}
32
88e01c9d 33my $SPLINT = 0; # Turn true for experimental splint support http://www.splint.org
916e4025 34my $unflagged_pointers;
88e01c9d 35
cea2e8a9 36#
346f75ff 37# See database of global and static function prototypes in embed.fnc
cea2e8a9
GS
38# This is used to generate prototype headers under various configurations,
39# export symbols lists for different platforms, and macros to provide an
40# implicit interpreter context argument.
41#
42
5f8dc073 43sub open_print_header {
56fd1190 44 my ($file, $quote) = @_;
4373e329 45
5f8dc073
NC
46 return open_new($file, '>',
47 { file => $file, style => '*', by => 'regen/embed.pl',
48 from => ['data in embed.fnc', 'regen/embed.pl',
49 'regen/opcodes', 'intrpvar.h', 'perlvars.h'],
50 final => "\nEdit those files and run 'make regen_headers' to effect changes.\n",
51 copyright => [1993 .. 2009], quote => $quote });
52}
7f1be197 53
94bdecf9 54open IN, "embed.fnc" or die $!;
cea2e8a9 55
881a2100 56my @embed;
125218eb 57my (%has_va, %has_nocontext);
881a2100
NC
58
59while (<IN>) {
60 chomp;
61 next if /^:/;
24dd05fb 62 next if /^$/;
881a2100
NC
63 while (s|\\$||) {
64 $_ .= <IN>;
65 chomp;
66 }
67 s/\s+$//;
68 my @args;
69 if (/^\s*(#|$)/) {
70 @args = $_;
71 }
72 else {
73 @args = split /\s*\|\s*/, $_;
125218eb
NC
74 my $func = $args[2];
75 if ($func) {
76 ++$has_va{$func} if $args[-1] =~ /\.\.\./;
77 ++$has_nocontext{$1} if $func =~ /(.*)_nocontext/;
78 }
881a2100 79 }
7c662e8a
NC
80 if (@args == 1 && $args[0] !~ /^#\s*(?:if|ifdef|ifndef|else|endif)/) {
81 die "Illegal line $. '$args[0]' in embed.fnc";
82 }
881a2100
NC
83 push @embed, \@args;
84}
85
146326cb
NC
86open IN, 'regen/opcodes' or die $!;
87{
88 my %syms;
89
90 while (<IN>) {
91 chop;
92 next unless $_;
93 next if /^#/;
94 my (undef, undef, $check) = split /\t+/, $_;
95 ++$syms{$check};
96 }
97
98 foreach (keys %syms) {
99 # These are all indirectly referenced by globals.c.
100 push @embed, ['pR', 'OP *', $_, 'NN OP *o'];
101 }
102}
103close IN;
104
e8a67806 105my (@core, @ext, @api);
c527bc84 106{
e8a67806
NC
107 # Cluster entries in embed.fnc that have the same #ifdef guards.
108 # Also, split out at the top level the three classes of functions.
109 my @state;
110 my %groups;
111 my $current;
c527bc84 112 foreach (@embed) {
e8a67806
NC
113 if (@$_ > 1) {
114 push @$current, $_;
115 next;
116 }
c527bc84
NC
117 $_->[0] =~ s/^#\s+/#/;
118 $_->[0] =~ /^\S*/;
119 $_->[0] =~ s/^#ifdef\s+(\S+)/#if defined($1)/;
120 $_->[0] =~ s/^#ifndef\s+(\S+)/#if !defined($1)/;
e8a67806
NC
121 if ($_->[0] =~ /^#if\s*(.*)/) {
122 push @state, $1;
123 } elsif ($_->[0] =~ /^#else\s*$/) {
124 die "Unmatched #else in embed.fnc" unless @state;
125 $state[-1] = "!($state[-1])";
126 } elsif ($_->[0] =~ m!^#endif\s*(?:/\*.*\*/)?$!) {
127 die "Unmatched #endif in embed.fnc" unless @state;
128 pop @state;
129 } else {
130 die "Unhandled pre-processor directive '$_->[0]' in embed.fnc";
131 }
132 $current = \%groups;
133 # Nested #if blocks are effectively &&ed together
134 # For embed.fnc, ordering withing the && isn't relevant, so we can
135 # sort them to try to group more functions together.
136 my @sorted = sort @state;
137 while (my $directive = shift @sorted) {
138 $current->{$directive} ||= {};
139 $current = $current->{$directive};
140 }
141 $current->{''} ||= [];
142 $current = $current->{''};
143 }
144
145 sub add_level {
146 my ($level, $indent, $wanted) = @_;
147 my $funcs = $level->{''};
148 my @entries;
149 if ($funcs) {
150 if (!defined $wanted) {
151 @entries = @$funcs;
152 } else {
153 foreach (@$funcs) {
154 if ($_->[0] =~ /A/) {
155 push @entries, $_ if $wanted eq 'A';
156 } elsif ($_->[0] =~ /E/) {
157 push @entries, $_ if $wanted eq 'E';
158 } else {
159 push @entries, $_ if $wanted eq '';
160 }
161 }
162 }
163 @entries = sort {$a->[2] cmp $b->[2]} @entries;
164 }
165 foreach (sort grep {length $_} keys %$level) {
166 my @conditional = add_level($level->{$_}, $indent . ' ', $wanted);
167 push @entries,
168 ["#${indent}if $_"], @conditional, ["#${indent}endif"]
169 if @conditional;
170 }
171 return @entries;
c527bc84 172 }
e8a67806
NC
173 @core = add_level(\%groups, '', '');
174 @ext = add_level(\%groups, '', 'E');
175 @api = add_level(\%groups, '', 'A');
176
177 @embed = add_level(\%groups, '');
c527bc84
NC
178}
179
cea2e8a9
GS
180# walk table providing an array of components in each line to
181# subroutine, printing the result
182sub walk_table (&@) {
ce716c52 183 my ($function, $filename) = @_;
cea2e8a9 184 my $F;
cea2e8a9
GS
185 if (ref $filename) { # filehandle
186 $F = $filename;
187 }
d0ee0d3d 188 else {
5f8dc073 189 $F = open_print_header($filename);
cea2e8a9 190 }
881a2100
NC
191 foreach (@embed) {
192 my @outs = &{$function}(@$_);
1028dc3c 193 # $function->(@args) is not 5.003
d0ee0d3d 194 print $F @outs;
cea2e8a9 195 }
d0ee0d3d 196 unless (ref $filename) {
ce716c52 197 read_only_bottom_close_and_rename($F);
36bb303b 198 }
cea2e8a9
GS
199}
200
cea2e8a9 201# generate proto.h
9516dc40 202{
5f8dc073
NC
203 my $pr = open_print_header("proto.h");
204 print $pr "START_EXTERN_C\n";
f8394530 205 my $ret;
9516dc40
NC
206
207 foreach (@embed) {
208 if (@$_ == 1) {
209 print $pr "$_->[0]\n";
210 next;
211 }
212
213 my ($flags,$retval,$plain_func,@args) = @$_;
4373e329
AL
214 my @nonnull;
215 my $has_context = ( $flags !~ /n/ );
88e01c9d
AL
216 my $never_returns = ( $flags =~ /r/ );
217 my $commented_out = ( $flags =~ /m/ );
aa4ca557 218 my $binarycompat = ( $flags =~ /b/ );
88e01c9d
AL
219 my $is_malloc = ( $flags =~ /a/ );
220 my $can_ignore = ( $flags !~ /R/ ) && !$is_malloc;
7918f24d
NC
221 my @names_of_nn;
222 my $func;
88e01c9d
AL
223
224 my $splint_flags = "";
225 if ( $SPLINT && !$commented_out ) {
226 $splint_flags .= '/*@noreturn@*/ ' if $never_returns;
227 if ($can_ignore && ($retval ne 'void') && ($retval !~ /\*/)) {
228 $retval .= " /*\@alt void\@*/";
229 }
230 }
231
71556506
KW
232 if ($flags =~ /([si])/) {
233 my $type = ($1 eq 's') ? "STATIC" : "PERL_STATIC_INLINE";
234 warn "$func: i and s flags are mutually exclusive"
235 if $flags =~ /s/ && $flags =~ /i/;
236 $retval = "$type $splint_flags$retval";
7918f24d 237 $func = "S_$plain_func";
cea2e8a9 238 }
0cb96387 239 else {
88e01c9d 240 $retval = "PERL_CALLCONV $splint_flags$retval";
25b0f989 241 if ($flags =~ /[bp]/) {
7918f24d
NC
242 $func = "Perl_$plain_func";
243 } else {
244 $func = $plain_func;
0cb96387 245 }
cea2e8a9 246 }
f8394530 247 $ret = "$retval\t$func(";
4373e329
AL
248 if ( $has_context ) {
249 $ret .= @args ? "pTHX_ " : "pTHX";
cea2e8a9
GS
250 }
251 if (@args) {
4373e329
AL
252 my $n;
253 for my $arg ( @args ) {
254 ++$n;
7827dc65
AL
255 if ( $arg =~ /\*/ && $arg !~ /\b(NN|NULLOK)\b/ ) {
256 warn "$func: $arg needs NN or NULLOK\n";
7827dc65
AL
257 ++$unflagged_pointers;
258 }
88e01c9d
AL
259 my $nn = ( $arg =~ s/\s*\bNN\b\s+// );
260 push( @nonnull, $n ) if $nn;
261
262 my $nullok = ( $arg =~ s/\s*\bNULLOK\b\s+// ); # strip NULLOK with no effect
c48640ec
AL
263
264 # Make sure each arg has at least a type and a var name.
265 # An arg of "int" is valid C, but want it to be "int foo".
266 my $temp_arg = $arg;
267 $temp_arg =~ s/\*//g;
268 $temp_arg =~ s/\s*\bstruct\b\s*/ /g;
7918f24d
NC
269 if ( ($temp_arg ne "...")
270 && ($temp_arg !~ /\w+\s+(\w+)(?:\[\d+\])?\s*$/) ) {
271 warn "$func: $arg ($n) doesn't have a name\n";
c48640ec 272 }
88e01c9d
AL
273 if ( $SPLINT && $nullok && !$commented_out ) {
274 $arg = '/*@null@*/ ' . $arg;
275 }
aa4ca557 276 if (defined $1 && $nn && !($commented_out && !$binarycompat)) {
7918f24d
NC
277 push @names_of_nn, $1;
278 }
4373e329 279 }
cea2e8a9
GS
280 $ret .= join ", ", @args;
281 }
282 else {
4373e329 283 $ret .= "void" if !$has_context;
cea2e8a9
GS
284 }
285 $ret .= ")";
f54cb97a
AL
286 my @attrs;
287 if ( $flags =~ /r/ ) {
abb2c242 288 push @attrs, "__attribute__noreturn__";
f54cb97a 289 }
a5c26493
RGS
290 if ( $flags =~ /D/ ) {
291 push @attrs, "__attribute__deprecated__";
292 }
88e01c9d 293 if ( $is_malloc ) {
abb2c242 294 push @attrs, "__attribute__malloc__";
f54cb97a 295 }
88e01c9d 296 if ( !$can_ignore ) {
abb2c242 297 push @attrs, "__attribute__warn_unused_result__";
f54cb97a
AL
298 }
299 if ( $flags =~ /P/ ) {
abb2c242 300 push @attrs, "__attribute__pure__";
f54cb97a 301 }
1c846c1f 302 if( $flags =~ /f/ ) {
cdfeb707
RB
303 my $prefix = $has_context ? 'pTHX_' : '';
304 my $args = scalar @args;
305 my $pat = $args - 1;
306 my $macro = @nonnull && $nonnull[-1] == $pat
307 ? '__attribute__format__'
308 : '__attribute__format__null_ok__';
309 push @attrs, sprintf "%s(__printf__,%s%d,%s%d)", $macro,
310 $prefix, $pat, $prefix, $args;
894356b3 311 }
4373e329 312 if ( @nonnull ) {
3d42dc86 313 my @pos = map { $has_context ? "pTHX_$_" : $_ } @nonnull;
abb2c242 314 push @attrs, map { sprintf( "__attribute__nonnull__(%s)", $_ ) } @pos;
f54cb97a
AL
315 }
316 if ( @attrs ) {
317 $ret .= "\n";
318 $ret .= join( "\n", map { "\t\t\t$_" } @attrs );
4373e329 319 }
af3c7592 320 $ret .= ";";
88e01c9d 321 $ret = "/* $ret */" if $commented_out;
7918f24d
NC
322 if (@names_of_nn) {
323 $ret .= "\n#define PERL_ARGS_ASSERT_\U$plain_func\E\t\\\n\t"
324 . join '; ', map "assert($_)", @names_of_nn;
325 }
f54cb97a 326 $ret .= @attrs ? "\n\n" : "\n";
9516dc40
NC
327
328 print $pr $ret;
cea2e8a9 329 }
9516dc40 330
897d3989
NC
331 print $pr <<'EOF';
332#ifdef PERL_CORE
333# include "pp_proto.h"
334#endif
335END_EXTERN_C
897d3989 336EOF
9516dc40 337
ce716c52 338 read_only_bottom_close_and_rename($pr);
cea2e8a9
GS
339}
340
2b10efc6
NC
341# generates global.sym (API export list)
342{
343 my %seen;
344 sub write_global_sym {
2b10efc6
NC
345 if (@_ > 1) {
346 my ($flags,$retval,$func,@args) = @_;
2b10efc6
NC
347 if ($flags =~ /[AX]/ && $flags !~ /[xm]/
348 || $flags =~ /b/) { # public API, so export
9be14afe
NC
349 # If a function is defined twice, for example before and after
350 # an #else, only export its name once.
351 return '' if $seen{$func}++;
2b10efc6 352 $func = "Perl_$func" if $flags =~ /[pbX]/;
f8394530 353 return "$func\n";
2b10efc6
NC
354 }
355 }
f8394530 356 return '';
2b10efc6 357 }
cea2e8a9
GS
358}
359
7827dc65 360warn "$unflagged_pointers pointer arguments to clean up\n" if $unflagged_pointers;
ce716c52 361walk_table(\&write_global_sym, "global.sym");
cea2e8a9 362
c6af7a1a
GS
363sub readvars(\%$$@) {
364 my ($syms, $file,$pre,$keep_pre) = @_;
d4cce5f1
NIS
365 local (*FILE, $_);
366 open(FILE, "< $file")
367 or die "embed.pl: Can't open $file: $!\n";
368 while (<FILE>) {
369 s/[ \t]*#.*//; # Delete comments.
27da23d5 370 if (/PERLVARA?I?S?C?\($pre(\w+)/) {
22c35a8c 371 my $sym = $1;
c6af7a1a 372 $sym = $pre . $sym if $keep_pre;
d1594dd0 373 warn "duplicate symbol $sym while processing $file line $.\n"
22c35a8c 374 if exists $$syms{$sym};
51371543 375 $$syms{$sym} = $pre || 1;
d4cce5f1
NIS
376 }
377 }
378 close(FILE);
379}
380
381my %intrp;
88e01c9d 382my %globvar;
d4cce5f1
NIS
383
384readvars %intrp, 'intrpvar.h','I';
22239a37 385readvars %globvar, 'perlvars.h','G';
d4cce5f1 386
4543f4c0 387my $sym;
d4cce5f1 388
c6af7a1a
GS
389sub undefine ($) {
390 my ($sym) = @_;
391 "#undef $sym\n";
392}
393
125218eb
NC
394sub hide {
395 my ($from, $to, $indent) = @_;
396 $indent = '' unless defined $indent;
397 my $t = int(length("$indent$from") / 8);
398 "#${indent}define $from" . "\t" x ($t < 3 ? 3 - $t : 1) . "$to\n";
5f05dabc 399}
c6af7a1a 400
6f4183fe 401sub bincompat_var ($$) {
51371543 402 my ($pfx, $sym) = @_;
acfe0abc 403 my $arg = ($pfx eq 'G' ? 'NULL' : 'aTHX');
c5be433b 404 undefine("PL_$sym") . hide("PL_$sym", "(*Perl_${pfx}${sym}_ptr($arg))");
c6af7a1a
GS
405}
406
d4cce5f1
NIS
407sub multon ($$$) {
408 my ($sym,$pre,$ptr) = @_;
3280af22 409 hide("PL_$sym", "($ptr$pre$sym)");
5f05dabc 410}
54aff467 411
d4cce5f1
NIS
412sub multoff ($$) {
413 my ($sym,$pre) = @_;
533c011a 414 return hide("PL_$pre$sym", "PL_$sym");
5f05dabc
PP
415}
416
5f8dc073 417my $em = open_print_header('embed.h');
e50aee73 418
5f8dc073 419print $em <<'END';
e50aee73
AD
420/* (Doing namespace management portably in C is really gross.) */
421
d51482e4
JH
422/* By defining PERL_NO_SHORT_NAMES (not done by default) the short forms
423 * (like warn instead of Perl_warn) for the API are not defined.
424 * Not defining the short forms is a good thing for cleaner embedding. */
425
426#ifndef PERL_NO_SHORT_NAMES
820c3be9 427
22c35a8c 428/* Hide global symbols */
5f05dabc 429
e50aee73
AD
430END
431
cea2e8a9
GS
432my @az = ('a'..'z');
433
e8a67806
NC
434sub embed_h {
435 my ($guard, $funcs) = @_;
436 print $em "$guard\n" if $guard;
437
2a4d8072 438 my $lines;
e8a67806
NC
439 foreach (@$funcs) {
440 if (@$_ == 1) {
441 my $cond = $_->[0];
442 # Indent the conditionals if we are wrapped in an #if/#endif pair.
443 $cond =~ s/#(.*)/# $1/ if $guard;
2a4d8072 444 $lines .= "$cond\n";
e8a67806
NC
445 next;
446 }
447 my $ret = "";
448 my ($flags,$retval,$func,@args) = @$_;
af3c7592 449 unless ($flags =~ /[om]/) {
cea2e8a9 450 my $args = scalar @args;
7a5a24f7 451 if ($flags =~ /n/) {
cea2e8a9 452 if ($flags =~ /s/) {
f8394530 453 $ret = hide($func,"S_$func");
cea2e8a9
GS
454 }
455 elsif ($flags =~ /p/) {
f8394530 456 $ret = hide($func,"Perl_$func");
cea2e8a9
GS
457 }
458 }
7a5a24f7 459 elsif ($args and $args[$args-1] =~ /\.\.\./) {
e64ca59f
NC
460 if ($flags =~ /p/) {
461 # we're out of luck for varargs functions under CPP
462 # So we can only do these macros for no implicit context:
463 $ret = "#ifndef PERL_IMPLICIT_CONTEXT\n"
464 . hide($func,"Perl_$func") . "#endif\n";
465 }
7a5a24f7 466 }
cea2e8a9
GS
467 else {
468 my $alist = join(",", @az[0..$args-1]);
469 $ret = "#define $func($alist)";
470 my $t = int(length($ret) / 8);
471 $ret .= "\t" x ($t < 4 ? 4 - $t : 1);
71556506 472 if ($flags =~ /[si]/) {
cea2e8a9
GS
473 $ret .= "S_$func(aTHX";
474 }
475 elsif ($flags =~ /p/) {
476 $ret .= "Perl_$func(aTHX";
477 }
478 $ret .= "_ " if $alist;
479 $ret .= $alist . ")\n";
480 }
481 }
2a4d8072 482 $lines .= $ret;
cea2e8a9 483 }
2a4d8072
NC
484 # Prune empty #if/#endif pairs.
485 while ($lines =~ s/#\s*if[^\n]+\n#\s*endif\n//) {
486 }
b2e549c0
NC
487 # Merge adjacent blocks.
488 while ($lines =~ s/(#ifndef PERL_IMPLICIT_CONTEXT
489[^\n]+
490)#endif
491#ifndef PERL_IMPLICIT_CONTEXT
492/$1/) {
493 }
494
2a4d8072 495 print $em $lines;
e8a67806 496 print $em "#endif\n" if $guard;
da4ddda1
NC
497}
498
e8a67806
NC
499embed_h('', \@api);
500embed_h('#if defined(PERL_CORE) || defined(PERL_EXT)', \@ext);
501embed_h('#ifdef PERL_CORE', \@core);
502
424a4936 503print $em <<'END';
e50aee73 504
d51482e4 505#endif /* #ifndef PERL_NO_SHORT_NAMES */
35209cc8 506
cea2e8a9
GS
507/* Compatibility stubs. Compile extensions with -DPERL_NOCOMPAT to
508 disable them.
509 */
510
538feb02 511#if !defined(PERL_CORE)
5bc28da9 512# define sv_setptrobj(rv,ptr,name) sv_setref_iv(rv,name,PTR2IV(ptr))
a0714e2c 513# define sv_setptrref(rv,ptr) sv_setref_iv(rv,NULL,PTR2IV(ptr))
538feb02 514#endif
cea2e8a9 515
08e5223a 516#if !defined(PERL_CORE) && !defined(PERL_NOCOMPAT)
cea2e8a9
GS
517
518/* Compatibility for various misnamed functions. All functions
519 in the API that begin with "perl_" (not "Perl_") take an explicit
520 interpreter context pointer.
521 The following are not like that, but since they had a "perl_"
522 prefix in previous versions, we provide compatibility macros.
523 */
65cec589 524# define perl_atexit(a,b) call_atexit(a,b)
7b53c8ee
NC
525END
526
527walk_table {
528 my ($flags,$retval,$func,@args) = @_;
529 return unless $func;
530 return unless $flags =~ /O/;
531
532 my $alist = join ",", @az[0..$#args];
533 my $ret = "# define perl_$func($alist)";
534 my $t = (length $ret) >> 3;
535 $ret .= "\t" x ($t < 5 ? 5 - $t : 1);
536 "$ret$func($alist)\n";
1028dc3c 537} $em;
7b53c8ee
NC
538
539print $em <<'END';
cea2e8a9
GS
540
541/* varargs functions can't be handled with CPP macros. :-(
542 This provides a set of compatibility functions that don't take
543 an extra argument but grab the context pointer using the macro
544 dTHX.
545 */
d51482e4 546#if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_NO_SHORT_NAMES)
125218eb
NC
547END
548
549foreach (sort keys %has_va) {
550 next unless $has_nocontext{$_};
551 next if /printf/; # Not clear to me why these are skipped but they are.
552 print $em hide($_, "Perl_${_}_nocontext", " ");
553}
554
555print $em <<'END';
cea2e8a9
GS
556#endif
557
558#endif /* !defined(PERL_CORE) && !defined(PERL_NOCOMPAT) */
559
560#if !defined(PERL_IMPLICIT_CONTEXT)
561/* undefined symbols, point them back at the usual ones */
125218eb
NC
562END
563
564foreach (sort keys %has_va) {
565 next unless $has_nocontext{$_};
566 next if /printf/; # Not clear to me why these are skipped but they are.
567 print $em hide("Perl_${_}_nocontext", "Perl_$_", " ");
568}
569
570print $em <<'END';
cea2e8a9 571#endif
d4cce5f1
NIS
572END
573
ce716c52 574read_only_bottom_close_and_rename($em);
d4cce5f1 575
5f8dc073 576$em = open_print_header('embedvar.h');
d4cce5f1 577
5f8dc073 578print $em <<'END';
d4cce5f1
NIS
579/* (Doing namespace management portably in C is really gross.) */
580
54aff467 581/*
3db8f154
MB
582 The following combinations of MULTIPLICITY and PERL_IMPLICIT_CONTEXT
583 are supported:
54aff467
GS
584 1) none
585 2) MULTIPLICITY # supported for compatibility
586 3) MULTIPLICITY && PERL_IMPLICIT_CONTEXT
54aff467
GS
587
588 All other combinations of these flags are errors.
589
3db8f154 590 only #3 is supported directly, while #2 is a special
54aff467
GS
591 case of #3 (supported by redefining vTHX appropriately).
592*/
cea2e8a9 593
54aff467 594#if defined(MULTIPLICITY)
3db8f154 595/* cases 2 and 3 above */
cea2e8a9 596
54aff467
GS
597# if defined(PERL_IMPLICIT_CONTEXT)
598# define vTHX aTHX
599# else
600# define vTHX PERL_GET_INTERP
601# endif
cea2e8a9 602
e50aee73
AD
603END
604
d4cce5f1 605for $sym (sort keys %intrp) {
424a4936 606 print $em multon($sym,'I','vTHX->');
d4cce5f1
NIS
607}
608
424a4936 609print $em <<'END';
d4cce5f1 610
54aff467 611#else /* !MULTIPLICITY */
1d7c1841 612
3db8f154 613/* case 1 above */
5f05dabc 614
56d28764 615END
e50aee73 616
d4cce5f1 617for $sym (sort keys %intrp) {
424a4936 618 print $em multoff($sym,'I');
d4cce5f1
NIS
619}
620
424a4936 621print $em <<'END';
d4cce5f1 622
d4cce5f1
NIS
623END
624
424a4936 625print $em <<'END';
d4cce5f1 626
54aff467 627#endif /* MULTIPLICITY */
d4cce5f1 628
54aff467 629#if defined(PERL_GLOBAL_STRUCT)
22239a37
NIS
630
631END
632
633for $sym (sort keys %globvar) {
424a4936
NC
634 print $em multon($sym, 'G','my_vars->');
635 print $em multon("G$sym",'', 'my_vars->');
22239a37
NIS
636}
637
424a4936 638print $em <<'END';
22239a37
NIS
639
640#else /* !PERL_GLOBAL_STRUCT */
641
642END
643
644for $sym (sort keys %globvar) {
424a4936 645 print $em multoff($sym,'G');
22239a37
NIS
646}
647
424a4936 648print $em <<'END';
22239a37 649
22239a37 650#endif /* PERL_GLOBAL_STRUCT */
84fee439
NIS
651END
652
ce716c52 653read_only_bottom_close_and_rename($em);
c6af7a1a 654
5f8dc073 655my $capih = open_print_header('perlapi.h');
51371543 656
5f8dc073 657print $capih <<'EOT';
51371543 658/* declare accessor functions for Perl variables */
6f4183fe
GS
659#ifndef __perlapi_h__
660#define __perlapi_h__
51371543 661
87b9e160 662#if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
c5be433b 663
51371543
GS
664START_EXTERN_C
665
666#undef PERLVAR
667#undef PERLVARA
668#undef PERLVARI
669#undef PERLVARIC
27da23d5 670#undef PERLVARISC
acfe0abc 671#define PERLVAR(v,t) EXTERN_C t* Perl_##v##_ptr(pTHX);
51371543 672#define PERLVARA(v,n,t) typedef t PL_##v##_t[n]; \
acfe0abc 673 EXTERN_C PL_##v##_t* Perl_##v##_ptr(pTHX);
51371543 674#define PERLVARI(v,t,i) PERLVAR(v,t)
c5be433b 675#define PERLVARIC(v,t,i) PERLVAR(v, const t)
27da23d5
JH
676#define PERLVARISC(v,i) typedef const char PL_##v##_t[sizeof(i)]; \
677 EXTERN_C PL_##v##_t* Perl_##v##_ptr(pTHX);
51371543 678
51371543
GS
679#include "perlvars.h"
680
681#undef PERLVAR
682#undef PERLVARA
683#undef PERLVARI
684#undef PERLVARIC
27da23d5
JH
685#undef PERLVARISC
686
51371543
GS
687END_EXTERN_C
688
682fc664 689#if defined(PERL_CORE)
6f4183fe 690
87b9e160 691/* accessor functions for Perl "global" variables */
682fc664
GS
692
693/* these need to be mentioned here, or most linkers won't put them in
694 the perl executable */
695
696#ifndef PERL_NO_FORCE_LINK
697
698START_EXTERN_C
699
700#ifndef DOINIT
27da23d5 701EXTCONST void * const PL_force_link_funcs[];
682fc664 702#else
27da23d5 703EXTCONST void * const PL_force_link_funcs[] = {
682fc664
GS
704#undef PERLVAR
705#undef PERLVARA
706#undef PERLVARI
707#undef PERLVARIC
ea1f607c 708#define PERLVAR(v,t) (void*)Perl_##v##_ptr,
682fc664
GS
709#define PERLVARA(v,n,t) PERLVAR(v,t)
710#define PERLVARI(v,t,i) PERLVAR(v,t)
711#define PERLVARIC(v,t,i) PERLVAR(v,t)
27da23d5 712#define PERLVARISC(v,i) PERLVAR(v,char)
682fc664 713
3c0f78ca
JH
714/* In Tru64 (__DEC && __osf__) the cc option -std1 causes that one
715 * cannot cast between void pointers and function pointers without
716 * info level warnings. The PL_force_link_funcs[] would cause a few
717 * hundred of those warnings. In code one can circumnavigate this by using
718 * unions that overlay the different pointers, but in declarations one
719 * cannot use this trick. Therefore we just disable the warning here
720 * for the duration of the PL_force_link_funcs[] declaration. */
721
722#if defined(__DECC) && defined(__osf__)
723#pragma message save
724#pragma message disable (nonstandcast)
725#endif
726
682fc664
GS
727#include "perlvars.h"
728
3c0f78ca
JH
729#if defined(__DECC) && defined(__osf__)
730#pragma message restore
731#endif
732
682fc664
GS
733#undef PERLVAR
734#undef PERLVARA
735#undef PERLVARI
736#undef PERLVARIC
27da23d5 737#undef PERLVARISC
682fc664
GS
738};
739#endif /* DOINIT */
740
acfe0abc 741END_EXTERN_C
682fc664
GS
742
743#endif /* PERL_NO_FORCE_LINK */
744
745#else /* !PERL_CORE */
51371543
GS
746
747EOT
748
4543f4c0 749foreach $sym (sort keys %globvar) {
424a4936 750 print $capih bincompat_var('G',$sym);
6f4183fe
GS
751}
752
424a4936 753print $capih <<'EOT';
6f4183fe
GS
754
755#endif /* !PERL_CORE */
87b9e160 756#endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
6f4183fe
GS
757
758#endif /* __perlapi_h__ */
6f4183fe 759EOT
ce716c52
NC
760
761read_only_bottom_close_and_rename($capih);
51371543 762
5f8dc073 763my $capi = open_print_header('perlapi.c', <<'EOQ');
56fd1190 764 *
892eaa71
NC
765 *
766 * Up to the threshold of the door there mounted a flight of twenty-seven
767 * broad stairs, hewn by some unknown art of the same black stone. This
768 * was the only entrance to the tower; ...
769 *
770 * [p.577 of _The Lord of the Rings_, III/x: "The Voice of Saruman"]
771 *
772 */
56fd1190 773EOQ
892eaa71 774
5f8dc073 775print $capi <<'EOT';
51371543
GS
776#include "EXTERN.h"
777#include "perl.h"
778#include "perlapi.h"
779
87b9e160 780#if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
51371543 781
87b9e160 782/* accessor functions for Perl "global" variables */
51371543
GS
783START_EXTERN_C
784
51371543 785#undef PERLVARI
87b9e160 786#define PERLVARI(v,t,i) PERLVAR(v,t)
c5be433b
GS
787
788#undef PERLVAR
789#undef PERLVARA
acfe0abc 790#define PERLVAR(v,t) t* Perl_##v##_ptr(pTHX) \
96a5add6 791 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
acfe0abc 792#define PERLVARA(v,n,t) PL_##v##_t* Perl_##v##_ptr(pTHX) \
96a5add6 793 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
34f7a5fe 794#undef PERLVARIC
27da23d5
JH
795#undef PERLVARISC
796#define PERLVARIC(v,t,i) \
797 const t* Perl_##v##_ptr(pTHX) \
96a5add6 798 { PERL_UNUSED_CONTEXT; return (const t *)&(PL_##v); }
27da23d5 799#define PERLVARISC(v,i) PL_##v##_t* Perl_##v##_ptr(pTHX) \
96a5add6 800 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
51371543
GS
801#include "perlvars.h"
802
803#undef PERLVAR
804#undef PERLVARA
805#undef PERLVARI
806#undef PERLVARIC
27da23d5
JH
807#undef PERLVARISC
808
acfe0abc 809END_EXTERN_C
6f4183fe 810
87b9e160 811#endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
51371543
GS
812EOT
813
ce716c52 814read_only_bottom_close_and_rename($capi);
acfe0abc 815
1b6737cc 816# ex: set ts=8 sts=4 sw=4 noet: