3 # Regenerate (overwriting only if changed):
12 # from information stored in
19 # Accepts the standard regen_lib -q and -v args.
21 # This script is normally invoked from regen.pl.
23 require 5.004; # keep this compatible, an old perl is all we may have before
24 # we build the new one
29 # Get function prototypes
30 require 'regen/regen_lib.pl';
33 my $SPLINT = 0; # Turn true for experimental splint support http://www.splint.org
34 my $unflagged_pointers;
37 # See database of global and static function prototypes in embed.fnc
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.
47 return read_only_top(lang => ($file =~ /\.[ch]$/ ? 'C' : 'Perl'),
48 file => $file, style => '*', by => 'regen/embed.pl',
49 from => ['data in embed.fnc', 'regen/embed.pl',
50 'regen/opcodes', 'intrpvar.h', 'perlvars.h'],
51 final => "\nEdit those files and run 'make regen_headers' to effect changes.\n",
52 copyright => [1993 .. 2009]);
55 open IN, "embed.fnc" or die $!;
58 my (%has_va, %has_nocontext);
74 @args = split /\s*\|\s*/, $_;
77 ++$has_va{$func} if $args[-1] =~ /\.\.\./;
78 ++$has_nocontext{$1} if $func =~ /(.*)_nocontext/;
81 if (@args == 1 && $args[0] !~ /^#\s*(?:if|ifdef|ifndef|else|endif)/) {
82 die "Illegal line $. '$args[0]' in embed.fnc";
87 open IN, 'regen/opcodes' or die $!;
95 my (undef, undef, $check) = split /\t+/, $_;
99 foreach (keys %syms) {
100 # These are all indirectly referenced by globals.c.
101 push @embed, ['pR', 'OP *', $_, 'NN OP *o'];
106 my (@core, @ext, @api);
108 # Cluster entries in embed.fnc that have the same #ifdef guards.
109 # Also, split out at the top level the three classes of functions.
118 $_->[0] =~ s/^#\s+/#/;
120 $_->[0] =~ s/^#ifdef\s+(\S+)/#if defined($1)/;
121 $_->[0] =~ s/^#ifndef\s+(\S+)/#if !defined($1)/;
122 if ($_->[0] =~ /^#if\s*(.*)/) {
124 } elsif ($_->[0] =~ /^#else\s*$/) {
125 die "Unmatched #else in embed.fnc" unless @state;
126 $state[-1] = "!($state[-1])";
127 } elsif ($_->[0] =~ m!^#endif\s*(?:/\*.*\*/)?$!) {
128 die "Unmatched #endif in embed.fnc" unless @state;
131 die "Unhandled pre-processor directive '$_->[0]' in embed.fnc";
134 # Nested #if blocks are effectively &&ed together
135 # For embed.fnc, ordering withing the && isn't relevant, so we can
136 # sort them to try to group more functions together.
137 my @sorted = sort @state;
138 while (my $directive = shift @sorted) {
139 $current->{$directive} ||= {};
140 $current = $current->{$directive};
142 $current->{''} ||= [];
143 $current = $current->{''};
147 my ($level, $indent, $wanted) = @_;
148 my $funcs = $level->{''};
151 if (!defined $wanted) {
155 if ($_->[0] =~ /A/) {
156 push @entries, $_ if $wanted eq 'A';
157 } elsif ($_->[0] =~ /E/) {
158 push @entries, $_ if $wanted eq 'E';
160 push @entries, $_ if $wanted eq '';
164 @entries = sort {$a->[2] cmp $b->[2]} @entries;
166 foreach (sort grep {length $_} keys %$level) {
167 my @conditional = add_level($level->{$_}, $indent . ' ', $wanted);
169 ["#${indent}if $_"], @conditional, ["#${indent}endif"]
174 @core = add_level(\%groups, '', '');
175 @ext = add_level(\%groups, '', 'E');
176 @api = add_level(\%groups, '', 'A');
178 @embed = add_level(\%groups, '');
181 # walk table providing an array of components in each line to
182 # subroutine, printing the result
183 sub walk_table (&@) {
184 my ($function, $filename) = @_;
186 if (ref $filename) { # filehandle
190 $F = safer_open("$filename-new", $filename);
191 print $F do_not_edit ($filename);
194 my @outs = &{$function}(@$_);
195 # $function->(@args) is not 5.003
198 unless (ref $filename) {
199 read_only_bottom_close_and_rename($F);
205 my $pr = safer_open('proto.h-new', 'proto.h');
206 print $pr do_not_edit ("proto.h"), "START_EXTERN_C\n";
211 print $pr "$_->[0]\n";
215 my ($flags,$retval,$plain_func,@args) = @$_;
217 my $has_context = ( $flags !~ /n/ );
218 my $never_returns = ( $flags =~ /r/ );
219 my $commented_out = ( $flags =~ /m/ );
220 my $binarycompat = ( $flags =~ /b/ );
221 my $is_malloc = ( $flags =~ /a/ );
222 my $can_ignore = ( $flags !~ /R/ ) && !$is_malloc;
226 my $splint_flags = "";
227 if ( $SPLINT && !$commented_out ) {
228 $splint_flags .= '/*@noreturn@*/ ' if $never_returns;
229 if ($can_ignore && ($retval ne 'void') && ($retval !~ /\*/)) {
230 $retval .= " /*\@alt void\@*/";
234 if ($flags =~ /([si])/) {
235 my $type = ($1 eq 's') ? "STATIC" : "PERL_STATIC_INLINE";
236 warn "$func: i and s flags are mutually exclusive"
237 if $flags =~ /s/ && $flags =~ /i/;
238 $retval = "$type $splint_flags$retval";
239 $func = "S_$plain_func";
242 $retval = "PERL_CALLCONV $splint_flags$retval";
243 if ($flags =~ /[bp]/) {
244 $func = "Perl_$plain_func";
249 $ret = "$retval\t$func(";
250 if ( $has_context ) {
251 $ret .= @args ? "pTHX_ " : "pTHX";
255 for my $arg ( @args ) {
257 if ( $arg =~ /\*/ && $arg !~ /\b(NN|NULLOK)\b/ ) {
258 warn "$func: $arg needs NN or NULLOK\n";
259 ++$unflagged_pointers;
261 my $nn = ( $arg =~ s/\s*\bNN\b\s+// );
262 push( @nonnull, $n ) if $nn;
264 my $nullok = ( $arg =~ s/\s*\bNULLOK\b\s+// ); # strip NULLOK with no effect
266 # Make sure each arg has at least a type and a var name.
267 # An arg of "int" is valid C, but want it to be "int foo".
269 $temp_arg =~ s/\*//g;
270 $temp_arg =~ s/\s*\bstruct\b\s*/ /g;
271 if ( ($temp_arg ne "...")
272 && ($temp_arg !~ /\w+\s+(\w+)(?:\[\d+\])?\s*$/) ) {
273 warn "$func: $arg ($n) doesn't have a name\n";
275 if ( $SPLINT && $nullok && !$commented_out ) {
276 $arg = '/*@null@*/ ' . $arg;
278 if (defined $1 && $nn && !($commented_out && !$binarycompat)) {
279 push @names_of_nn, $1;
282 $ret .= join ", ", @args;
285 $ret .= "void" if !$has_context;
289 if ( $flags =~ /r/ ) {
290 push @attrs, "__attribute__noreturn__";
292 if ( $flags =~ /D/ ) {
293 push @attrs, "__attribute__deprecated__";
296 push @attrs, "__attribute__malloc__";
298 if ( !$can_ignore ) {
299 push @attrs, "__attribute__warn_unused_result__";
301 if ( $flags =~ /P/ ) {
302 push @attrs, "__attribute__pure__";
304 if( $flags =~ /f/ ) {
305 my $prefix = $has_context ? 'pTHX_' : '';
306 my $args = scalar @args;
308 my $macro = @nonnull && $nonnull[-1] == $pat
309 ? '__attribute__format__'
310 : '__attribute__format__null_ok__';
311 push @attrs, sprintf "%s(__printf__,%s%d,%s%d)", $macro,
312 $prefix, $pat, $prefix, $args;
315 my @pos = map { $has_context ? "pTHX_$_" : $_ } @nonnull;
316 push @attrs, map { sprintf( "__attribute__nonnull__(%s)", $_ ) } @pos;
320 $ret .= join( "\n", map { "\t\t\t$_" } @attrs );
323 $ret = "/* $ret */" if $commented_out;
325 $ret .= "\n#define PERL_ARGS_ASSERT_\U$plain_func\E\t\\\n\t"
326 . join '; ', map "assert($_)", @names_of_nn;
328 $ret .= @attrs ? "\n\n" : "\n";
335 # include "pp_proto.h"
340 read_only_bottom_close_and_rename($pr);
343 # generates global.sym (API export list)
346 sub write_global_sym {
348 my ($flags,$retval,$func,@args) = @_;
349 if ($flags =~ /[AX]/ && $flags !~ /[xm]/
350 || $flags =~ /b/) { # public API, so export
351 # If a function is defined twice, for example before and after
352 # an #else, only export its name once.
353 return '' if $seen{$func}++;
354 $func = "Perl_$func" if $flags =~ /[pbX]/;
362 warn "$unflagged_pointers pointer arguments to clean up\n" if $unflagged_pointers;
363 walk_table(\&write_global_sym, "global.sym");
365 sub readvars(\%$$@) {
366 my ($syms, $file,$pre,$keep_pre) = @_;
368 open(FILE, "< $file")
369 or die "embed.pl: Can't open $file: $!\n";
371 s/[ \t]*#.*//; # Delete comments.
372 if (/PERLVARA?I?S?C?\($pre(\w+)/) {
374 $sym = $pre . $sym if $keep_pre;
375 warn "duplicate symbol $sym while processing $file line $.\n"
376 if exists $$syms{$sym};
377 $$syms{$sym} = $pre || 1;
386 readvars %intrp, 'intrpvar.h','I';
387 readvars %globvar, 'perlvars.h','G';
397 my ($from, $to, $indent) = @_;
398 $indent = '' unless defined $indent;
399 my $t = int(length("$indent$from") / 8);
400 "#${indent}define $from" . "\t" x ($t < 3 ? 3 - $t : 1) . "$to\n";
403 sub bincompat_var ($$) {
404 my ($pfx, $sym) = @_;
405 my $arg = ($pfx eq 'G' ? 'NULL' : 'aTHX');
406 undefine("PL_$sym") . hide("PL_$sym", "(*Perl_${pfx}${sym}_ptr($arg))");
410 my ($sym,$pre,$ptr) = @_;
411 hide("PL_$sym", "($ptr$pre$sym)");
416 return hide("PL_$pre$sym", "PL_$sym");
419 my $em = safer_open('embed.h-new', 'embed.h');
421 print $em do_not_edit ("embed.h"), <<'END';
422 /* (Doing namespace management portably in C is really gross.) */
424 /* By defining PERL_NO_SHORT_NAMES (not done by default) the short forms
425 * (like warn instead of Perl_warn) for the API are not defined.
426 * Not defining the short forms is a good thing for cleaner embedding. */
428 #ifndef PERL_NO_SHORT_NAMES
430 /* Hide global symbols */
437 my ($guard, $funcs) = @_;
438 print $em "$guard\n" if $guard;
444 # Indent the conditionals if we are wrapped in an #if/#endif pair.
445 $cond =~ s/#(.*)/# $1/ if $guard;
450 my ($flags,$retval,$func,@args) = @$_;
451 unless ($flags =~ /[om]/) {
452 my $args = scalar @args;
455 $ret = hide($func,"S_$func");
457 elsif ($flags =~ /p/) {
458 $ret = hide($func,"Perl_$func");
461 elsif ($args and $args[$args-1] =~ /\.\.\./) {
463 # we're out of luck for varargs functions under CPP
464 # So we can only do these macros for no implicit context:
465 $ret = "#ifndef PERL_IMPLICIT_CONTEXT\n"
466 . hide($func,"Perl_$func") . "#endif\n";
470 my $alist = join(",", @az[0..$args-1]);
471 $ret = "#define $func($alist)";
472 my $t = int(length($ret) / 8);
473 $ret .= "\t" x ($t < 4 ? 4 - $t : 1);
474 if ($flags =~ /[si]/) {
475 $ret .= "S_$func(aTHX";
477 elsif ($flags =~ /p/) {
478 $ret .= "Perl_$func(aTHX";
480 $ret .= "_ " if $alist;
481 $ret .= $alist . ")\n";
486 # Prune empty #if/#endif pairs.
487 while ($lines =~ s/#\s*if[^\n]+\n#\s*endif\n//) {
489 # Merge adjacent blocks.
490 while ($lines =~ s/(#ifndef PERL_IMPLICIT_CONTEXT
493 #ifndef PERL_IMPLICIT_CONTEXT
498 print $em "#endif\n" if $guard;
502 embed_h('#if defined(PERL_CORE) || defined(PERL_EXT)', \@ext);
503 embed_h('#ifdef PERL_CORE', \@core);
507 #endif /* #ifndef PERL_NO_SHORT_NAMES */
509 /* Compatibility stubs. Compile extensions with -DPERL_NOCOMPAT to
513 #if !defined(PERL_CORE)
514 # define sv_setptrobj(rv,ptr,name) sv_setref_iv(rv,name,PTR2IV(ptr))
515 # define sv_setptrref(rv,ptr) sv_setref_iv(rv,NULL,PTR2IV(ptr))
518 #if !defined(PERL_CORE) && !defined(PERL_NOCOMPAT)
520 /* Compatibility for various misnamed functions. All functions
521 in the API that begin with "perl_" (not "Perl_") take an explicit
522 interpreter context pointer.
523 The following are not like that, but since they had a "perl_"
524 prefix in previous versions, we provide compatibility macros.
526 # define perl_atexit(a,b) call_atexit(a,b)
530 my ($flags,$retval,$func,@args) = @_;
532 return unless $flags =~ /O/;
534 my $alist = join ",", @az[0..$#args];
535 my $ret = "# define perl_$func($alist)";
536 my $t = (length $ret) >> 3;
537 $ret .= "\t" x ($t < 5 ? 5 - $t : 1);
538 "$ret$func($alist)\n";
543 /* varargs functions can't be handled with CPP macros. :-(
544 This provides a set of compatibility functions that don't take
545 an extra argument but grab the context pointer using the macro
548 #if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_NO_SHORT_NAMES)
551 foreach (sort keys %has_va) {
552 next unless $has_nocontext{$_};
553 next if /printf/; # Not clear to me why these are skipped but they are.
554 print $em hide($_, "Perl_${_}_nocontext", " ");
560 #endif /* !defined(PERL_CORE) && !defined(PERL_NOCOMPAT) */
562 #if !defined(PERL_IMPLICIT_CONTEXT)
563 /* undefined symbols, point them back at the usual ones */
566 foreach (sort keys %has_va) {
567 next unless $has_nocontext{$_};
568 next if /printf/; # Not clear to me why these are skipped but they are.
569 print $em hide("Perl_${_}_nocontext", "Perl_$_", " ");
576 read_only_bottom_close_and_rename($em);
578 $em = safer_open('embedvar.h-new', 'embedvar.h');
580 print $em do_not_edit ("embedvar.h"), <<'END';
581 /* (Doing namespace management portably in C is really gross.) */
584 The following combinations of MULTIPLICITY and PERL_IMPLICIT_CONTEXT
587 2) MULTIPLICITY # supported for compatibility
588 3) MULTIPLICITY && PERL_IMPLICIT_CONTEXT
590 All other combinations of these flags are errors.
592 only #3 is supported directly, while #2 is a special
593 case of #3 (supported by redefining vTHX appropriately).
596 #if defined(MULTIPLICITY)
597 /* cases 2 and 3 above */
599 # if defined(PERL_IMPLICIT_CONTEXT)
602 # define vTHX PERL_GET_INTERP
607 for $sym (sort keys %intrp) {
608 print $em multon($sym,'I','vTHX->');
613 #else /* !MULTIPLICITY */
619 for $sym (sort keys %intrp) {
620 print $em multoff($sym,'I');
629 #endif /* MULTIPLICITY */
631 #if defined(PERL_GLOBAL_STRUCT)
635 for $sym (sort keys %globvar) {
636 print $em multon($sym, 'G','my_vars->');
637 print $em multon("G$sym",'', 'my_vars->');
642 #else /* !PERL_GLOBAL_STRUCT */
646 for $sym (sort keys %globvar) {
647 print $em multoff($sym,'G');
652 #endif /* PERL_GLOBAL_STRUCT */
655 read_only_bottom_close_and_rename($em);
657 my $capi = safer_open('perlapi.c-new', 'perlapi.c');
658 my $capih = safer_open('perlapi.h-new', 'perlapi.h');
660 print $capih do_not_edit ("perlapi.h"), <<'EOT';
661 /* declare accessor functions for Perl variables */
662 #ifndef __perlapi_h__
663 #define __perlapi_h__
665 #if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
674 #define PERLVAR(v,t) EXTERN_C t* Perl_##v##_ptr(pTHX);
675 #define PERLVARA(v,n,t) typedef t PL_##v##_t[n]; \
676 EXTERN_C PL_##v##_t* Perl_##v##_ptr(pTHX);
677 #define PERLVARI(v,t,i) PERLVAR(v,t)
678 #define PERLVARIC(v,t,i) PERLVAR(v, const t)
679 #define PERLVARISC(v,i) typedef const char PL_##v##_t[sizeof(i)]; \
680 EXTERN_C PL_##v##_t* Perl_##v##_ptr(pTHX);
682 #include "perlvars.h"
692 #if defined(PERL_CORE)
694 /* accessor functions for Perl "global" variables */
696 /* these need to be mentioned here, or most linkers won't put them in
697 the perl executable */
699 #ifndef PERL_NO_FORCE_LINK
704 EXTCONST void * const PL_force_link_funcs[];
706 EXTCONST void * const PL_force_link_funcs[] = {
711 #define PERLVAR(v,t) (void*)Perl_##v##_ptr,
712 #define PERLVARA(v,n,t) PERLVAR(v,t)
713 #define PERLVARI(v,t,i) PERLVAR(v,t)
714 #define PERLVARIC(v,t,i) PERLVAR(v,t)
715 #define PERLVARISC(v,i) PERLVAR(v,char)
717 /* In Tru64 (__DEC && __osf__) the cc option -std1 causes that one
718 * cannot cast between void pointers and function pointers without
719 * info level warnings. The PL_force_link_funcs[] would cause a few
720 * hundred of those warnings. In code one can circumnavigate this by using
721 * unions that overlay the different pointers, but in declarations one
722 * cannot use this trick. Therefore we just disable the warning here
723 * for the duration of the PL_force_link_funcs[] declaration. */
725 #if defined(__DECC) && defined(__osf__)
727 #pragma message disable (nonstandcast)
730 #include "perlvars.h"
732 #if defined(__DECC) && defined(__osf__)
733 #pragma message restore
746 #endif /* PERL_NO_FORCE_LINK */
748 #else /* !PERL_CORE */
752 foreach $sym (sort keys %globvar) {
753 print $capih bincompat_var('G',$sym);
756 print $capih <<'EOT';
758 #endif /* !PERL_CORE */
759 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
761 #endif /* __perlapi_h__ */
764 read_only_bottom_close_and_rename($capih);
766 my $warning = do_not_edit ("perlapi.c");
767 $warning =~ s! \*/\n! *
769 * Up to the threshold of the door there mounted a flight of twenty-seven
770 * broad stairs, hewn by some unknown art of the same black stone. This
771 * was the only entrance to the tower; ...
773 * [p.577 of _The Lord of the Rings_, III/x: "The Voice of Saruman"]
778 print $capi $warning, <<'EOT';
783 #if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
785 /* accessor functions for Perl "global" variables */
789 #define PERLVARI(v,t,i) PERLVAR(v,t)
793 #define PERLVAR(v,t) t* Perl_##v##_ptr(pTHX) \
794 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
795 #define PERLVARA(v,n,t) PL_##v##_t* Perl_##v##_ptr(pTHX) \
796 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
799 #define PERLVARIC(v,t,i) \
800 const t* Perl_##v##_ptr(pTHX) \
801 { PERL_UNUSED_CONTEXT; return (const t *)&(PL_##v); }
802 #define PERLVARISC(v,i) PL_##v##_t* Perl_##v##_ptr(pTHX) \
803 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
804 #include "perlvars.h"
814 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
817 read_only_bottom_close_and_rename($capi);
819 # ex: set ts=8 sts=4 sw=4 noet: