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.
43 sub open_print_header {
44 my ($file, $quote) = @_;
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 });
54 open IN, "embed.fnc" or die $!;
57 my (%has_va, %has_nocontext);
73 @args = split /\s*\|\s*/, $_;
76 ++$has_va{$func} if $args[-1] =~ /\.\.\./;
77 ++$has_nocontext{$1} if $func =~ /(.*)_nocontext/;
80 if (@args == 1 && $args[0] !~ /^#\s*(?:if|ifdef|ifndef|else|endif)/) {
81 die "Illegal line $. '$args[0]' in embed.fnc";
86 open IN, 'regen/opcodes' or die $!;
94 my (undef, undef, $check) = split /\t+/, $_;
98 foreach (keys %syms) {
99 # These are all indirectly referenced by globals.c.
100 push @embed, ['pR', 'OP *', $_, 'NN OP *o'];
105 my (@core, @ext, @api);
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.
117 $_->[0] =~ s/^#\s+/#/;
119 $_->[0] =~ s/^#ifdef\s+(\S+)/#if defined($1)/;
120 $_->[0] =~ s/^#ifndef\s+(\S+)/#if !defined($1)/;
121 if ($_->[0] =~ /^#if\s*(.*)/) {
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;
130 die "Unhandled pre-processor directive '$_->[0]' in embed.fnc";
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};
141 $current->{''} ||= [];
142 $current = $current->{''};
146 my ($level, $indent, $wanted) = @_;
147 my $funcs = $level->{''};
150 if (!defined $wanted) {
154 if ($_->[0] =~ /A/) {
155 push @entries, $_ if $wanted eq 'A';
156 } elsif ($_->[0] =~ /E/) {
157 push @entries, $_ if $wanted eq 'E';
159 push @entries, $_ if $wanted eq '';
163 @entries = sort {$a->[2] cmp $b->[2]} @entries;
165 foreach (sort grep {length $_} keys %$level) {
166 my @conditional = add_level($level->{$_}, $indent . ' ', $wanted);
168 ["#${indent}if $_"], @conditional, ["#${indent}endif"]
173 @core = add_level(\%groups, '', '');
174 @ext = add_level(\%groups, '', 'E');
175 @api = add_level(\%groups, '', 'A');
177 @embed = add_level(\%groups, '');
180 # walk table providing an array of components in each line to
181 # subroutine, printing the result
182 sub walk_table (&@) {
183 my ($function, $filename) = @_;
185 if (ref $filename) { # filehandle
189 $F = open_print_header($filename);
192 my @outs = &{$function}(@$_);
193 # $function->(@args) is not 5.003
196 unless (ref $filename) {
197 read_only_bottom_close_and_rename($F);
203 my $pr = open_print_header("proto.h");
204 print $pr "START_EXTERN_C\n";
209 print $pr "$_->[0]\n";
213 my ($flags,$retval,$plain_func,@args) = @$_;
215 my $has_context = ( $flags !~ /n/ );
216 my $never_returns = ( $flags =~ /r/ );
217 my $commented_out = ( $flags =~ /m/ );
218 my $binarycompat = ( $flags =~ /b/ );
219 my $is_malloc = ( $flags =~ /a/ );
220 my $can_ignore = ( $flags !~ /R/ ) && !$is_malloc;
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\@*/";
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";
237 $func = "S_$plain_func";
240 $retval = "PERL_CALLCONV $splint_flags$retval";
241 if ($flags =~ /[bp]/) {
242 $func = "Perl_$plain_func";
247 $ret = "$retval\t$func(";
248 if ( $has_context ) {
249 $ret .= @args ? "pTHX_ " : "pTHX";
253 for my $arg ( @args ) {
255 if ( $arg =~ /\*/ && $arg !~ /\b(NN|NULLOK)\b/ ) {
256 warn "$func: $arg needs NN or NULLOK\n";
257 ++$unflagged_pointers;
259 my $nn = ( $arg =~ s/\s*\bNN\b\s+// );
260 push( @nonnull, $n ) if $nn;
262 my $nullok = ( $arg =~ s/\s*\bNULLOK\b\s+// ); # strip NULLOK with no effect
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".
267 $temp_arg =~ s/\*//g;
268 $temp_arg =~ s/\s*\bstruct\b\s*/ /g;
269 if ( ($temp_arg ne "...")
270 && ($temp_arg !~ /\w+\s+(\w+)(?:\[\d+\])?\s*$/) ) {
271 warn "$func: $arg ($n) doesn't have a name\n";
273 if ( $SPLINT && $nullok && !$commented_out ) {
274 $arg = '/*@null@*/ ' . $arg;
276 if (defined $1 && $nn && !($commented_out && !$binarycompat)) {
277 push @names_of_nn, $1;
280 $ret .= join ", ", @args;
283 $ret .= "void" if !$has_context;
287 if ( $flags =~ /r/ ) {
288 push @attrs, "__attribute__noreturn__";
290 if ( $flags =~ /D/ ) {
291 push @attrs, "__attribute__deprecated__";
294 push @attrs, "__attribute__malloc__";
296 if ( !$can_ignore ) {
297 push @attrs, "__attribute__warn_unused_result__";
299 if ( $flags =~ /P/ ) {
300 push @attrs, "__attribute__pure__";
302 if( $flags =~ /f/ ) {
303 my $prefix = $has_context ? 'pTHX_' : '';
304 my $args = scalar @args;
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;
313 my @pos = map { $has_context ? "pTHX_$_" : $_ } @nonnull;
314 push @attrs, map { sprintf( "__attribute__nonnull__(%s)", $_ ) } @pos;
318 $ret .= join( "\n", map { "\t\t\t$_" } @attrs );
321 $ret = "/* $ret */" if $commented_out;
323 $ret .= "\n#define PERL_ARGS_ASSERT_\U$plain_func\E\t\\\n\t"
324 . join '; ', map "assert($_)", @names_of_nn;
326 $ret .= @attrs ? "\n\n" : "\n";
333 # include "pp_proto.h"
338 read_only_bottom_close_and_rename($pr);
341 # generates global.sym (API export list)
344 sub write_global_sym {
346 my ($flags,$retval,$func,@args) = @_;
347 if ($flags =~ /[AX]/ && $flags !~ /[xm]/
348 || $flags =~ /b/) { # public API, so export
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}++;
352 $func = "Perl_$func" if $flags =~ /[pbX]/;
360 warn "$unflagged_pointers pointer arguments to clean up\n" if $unflagged_pointers;
361 walk_table(\&write_global_sym, "global.sym");
363 sub readvars(\%$$@) {
364 my ($syms, $file,$pre,$keep_pre) = @_;
366 open(FILE, "< $file")
367 or die "embed.pl: Can't open $file: $!\n";
369 s/[ \t]*#.*//; # Delete comments.
370 if (/PERLVARA?I?C?\($pre(\w+)/) {
372 $sym = $pre . $sym if $keep_pre;
373 warn "duplicate symbol $sym while processing $file line $.\n"
374 if exists $$syms{$sym};
375 $$syms{$sym} = $pre || 1;
384 readvars %intrp, 'intrpvar.h','I';
385 readvars %globvar, 'perlvars.h','G';
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";
401 sub bincompat_var ($$) {
402 my ($pfx, $sym) = @_;
403 my $arg = ($pfx eq 'G' ? 'NULL' : 'aTHX');
404 undefine("PL_$sym") . hide("PL_$sym", "(*Perl_${pfx}${sym}_ptr($arg))");
408 my ($sym,$pre,$ptr) = @_;
409 hide("PL_$sym", "($ptr$pre$sym)");
414 return hide("PL_$pre$sym", "PL_$sym");
417 my $em = open_print_header('embed.h');
420 /* (Doing namespace management portably in C is really gross.) */
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. */
426 #ifndef PERL_NO_SHORT_NAMES
428 /* Hide global symbols */
435 my ($guard, $funcs) = @_;
436 print $em "$guard\n" if $guard;
442 # Indent the conditionals if we are wrapped in an #if/#endif pair.
443 $cond =~ s/#(.*)/# $1/ if $guard;
448 my ($flags,$retval,$func,@args) = @$_;
449 unless ($flags =~ /[om]/) {
450 my $args = scalar @args;
453 $ret = hide($func,"S_$func");
455 elsif ($flags =~ /p/) {
456 $ret = hide($func,"Perl_$func");
459 elsif ($args and $args[$args-1] =~ /\.\.\./) {
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";
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);
472 if ($flags =~ /[si]/) {
473 $ret .= "S_$func(aTHX";
475 elsif ($flags =~ /p/) {
476 $ret .= "Perl_$func(aTHX";
478 $ret .= "_ " if $alist;
479 $ret .= $alist . ")\n";
484 # Prune empty #if/#endif pairs.
485 while ($lines =~ s/#\s*if[^\n]+\n#\s*endif\n//) {
487 # Merge adjacent blocks.
488 while ($lines =~ s/(#ifndef PERL_IMPLICIT_CONTEXT
491 #ifndef PERL_IMPLICIT_CONTEXT
496 print $em "#endif\n" if $guard;
500 embed_h('#if defined(PERL_CORE) || defined(PERL_EXT)', \@ext);
501 embed_h('#ifdef PERL_CORE', \@core);
505 #endif /* #ifndef PERL_NO_SHORT_NAMES */
507 /* Compatibility stubs. Compile extensions with -DPERL_NOCOMPAT to
511 #if !defined(PERL_CORE)
512 # define sv_setptrobj(rv,ptr,name) sv_setref_iv(rv,name,PTR2IV(ptr))
513 # define sv_setptrref(rv,ptr) sv_setref_iv(rv,NULL,PTR2IV(ptr))
516 #if !defined(PERL_CORE) && !defined(PERL_NOCOMPAT)
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.
524 # define perl_atexit(a,b) call_atexit(a,b)
528 my ($flags,$retval,$func,@args) = @_;
530 return unless $flags =~ /O/;
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";
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
546 #if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_NO_SHORT_NAMES)
549 foreach (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", " ");
558 #endif /* !defined(PERL_CORE) && !defined(PERL_NOCOMPAT) */
560 #if !defined(PERL_IMPLICIT_CONTEXT)
561 /* undefined symbols, point them back at the usual ones */
564 foreach (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_$_", " ");
574 read_only_bottom_close_and_rename($em);
576 $em = open_print_header('embedvar.h');
579 /* (Doing namespace management portably in C is really gross.) */
582 The following combinations of MULTIPLICITY and PERL_IMPLICIT_CONTEXT
585 2) MULTIPLICITY # supported for compatibility
586 3) MULTIPLICITY && PERL_IMPLICIT_CONTEXT
588 All other combinations of these flags are errors.
590 only #3 is supported directly, while #2 is a special
591 case of #3 (supported by redefining vTHX appropriately).
594 #if defined(MULTIPLICITY)
595 /* cases 2 and 3 above */
597 # if defined(PERL_IMPLICIT_CONTEXT)
600 # define vTHX PERL_GET_INTERP
605 for $sym (sort keys %intrp) {
606 print $em multon($sym,'I','vTHX->');
611 #else /* !MULTIPLICITY */
617 for $sym (sort keys %intrp) {
618 print $em multoff($sym,'I');
627 #endif /* MULTIPLICITY */
629 #if defined(PERL_GLOBAL_STRUCT)
633 for $sym (sort keys %globvar) {
634 print $em "#ifdef OS2\n" if $sym eq 'sh_path';
635 print $em multon($sym, 'G','my_vars->');
636 print $em multon("G$sym",'', 'my_vars->');
637 print $em "#endif\n" if $sym eq 'sh_path';
642 #else /* !PERL_GLOBAL_STRUCT */
646 for $sym (sort keys %globvar) {
647 print $em "#ifdef OS2\n" if $sym eq 'sh_path';
648 print $em multoff($sym,'G');
649 print $em "#endif\n" if $sym eq 'sh_path';
654 #endif /* PERL_GLOBAL_STRUCT */
657 read_only_bottom_close_and_rename($em);
659 my $capih = open_print_header('perlapi.h');
661 print $capih <<'EOT';
662 /* declare accessor functions for Perl variables */
663 #ifndef __perlapi_h__
664 #define __perlapi_h__
666 #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)
680 #include "perlvars.h"
689 #if defined(PERL_CORE)
691 /* accessor functions for Perl "global" variables */
693 /* these need to be mentioned here, or most linkers won't put them in
694 the perl executable */
696 #ifndef PERL_NO_FORCE_LINK
701 EXTCONST void * const PL_force_link_funcs[];
703 EXTCONST void * const PL_force_link_funcs[] = {
708 #define PERLVAR(v,t) (void*)Perl_##v##_ptr,
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)
713 /* In Tru64 (__DEC && __osf__) the cc option -std1 causes that one
714 * cannot cast between void pointers and function pointers without
715 * info level warnings. The PL_force_link_funcs[] would cause a few
716 * hundred of those warnings. In code one can circumnavigate this by using
717 * unions that overlay the different pointers, but in declarations one
718 * cannot use this trick. Therefore we just disable the warning here
719 * for the duration of the PL_force_link_funcs[] declaration. */
721 #if defined(__DECC) && defined(__osf__)
723 #pragma message disable (nonstandcast)
726 #include "perlvars.h"
728 #if defined(__DECC) && defined(__osf__)
729 #pragma message restore
741 #endif /* PERL_NO_FORCE_LINK */
743 #else /* !PERL_CORE */
747 foreach $sym (sort keys %globvar) {
748 print $capih bincompat_var('G',$sym);
751 print $capih <<'EOT';
753 #endif /* !PERL_CORE */
754 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
756 #endif /* __perlapi_h__ */
759 read_only_bottom_close_and_rename($capih);
761 my $capi = open_print_header('perlapi.c', <<'EOQ');
764 * Up to the threshold of the door there mounted a flight of twenty-seven
765 * broad stairs, hewn by some unknown art of the same black stone. This
766 * was the only entrance to the tower; ...
768 * [p.577 of _The Lord of the Rings_, III/x: "The Voice of Saruman"]
778 #if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
780 /* accessor functions for Perl "global" variables */
784 #define PERLVARI(v,t,i) PERLVAR(v,t)
788 #define PERLVAR(v,t) t* Perl_##v##_ptr(pTHX) \
789 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
790 #define PERLVARA(v,n,t) PL_##v##_t* Perl_##v##_ptr(pTHX) \
791 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
793 #define PERLVARIC(v,t,i) \
794 const t* Perl_##v##_ptr(pTHX) \
795 { PERL_UNUSED_CONTEXT; return (const t *)&(PL_##v); }
796 #include "perlvars.h"
805 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
808 read_only_bottom_close_and_rename($capi);
810 # ex: set ts=8 sts=4 sw=4 noet: