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");
364 my ($syms, $file, $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 warn "duplicate symbol $sym while processing $file line $.\n"
373 if exists $$syms{$sym};
374 $$syms{$sym} = $pre || 1;
383 readvars %intrp, 'intrpvar.h','I';
384 readvars %globvar, 'perlvars.h','G';
394 my ($from, $to, $indent) = @_;
395 $indent = '' unless defined $indent;
396 my $t = int(length("$indent$from") / 8);
397 "#${indent}define $from" . "\t" x ($t < 3 ? 3 - $t : 1) . "$to\n";
400 sub bincompat_var ($$) {
401 my ($pfx, $sym) = @_;
402 my $arg = ($pfx eq 'G' ? 'NULL' : 'aTHX');
403 undefine("PL_$sym") . hide("PL_$sym", "(*Perl_${pfx}${sym}_ptr($arg))");
407 my ($sym,$pre,$ptr) = @_;
408 hide("PL_$sym", "($ptr$pre$sym)");
413 return hide("PL_$pre$sym", "PL_$sym");
416 my $em = open_print_header('embed.h');
419 /* (Doing namespace management portably in C is really gross.) */
421 /* By defining PERL_NO_SHORT_NAMES (not done by default) the short forms
422 * (like warn instead of Perl_warn) for the API are not defined.
423 * Not defining the short forms is a good thing for cleaner embedding. */
425 #ifndef PERL_NO_SHORT_NAMES
427 /* Hide global symbols */
434 my ($guard, $funcs) = @_;
435 print $em "$guard\n" if $guard;
441 # Indent the conditionals if we are wrapped in an #if/#endif pair.
442 $cond =~ s/#(.*)/# $1/ if $guard;
447 my ($flags,$retval,$func,@args) = @$_;
448 unless ($flags =~ /[om]/) {
449 my $args = scalar @args;
452 $ret = hide($func,"S_$func");
454 elsif ($flags =~ /p/) {
455 $ret = hide($func,"Perl_$func");
458 elsif ($args and $args[$args-1] =~ /\.\.\./) {
460 # we're out of luck for varargs functions under CPP
461 # So we can only do these macros for no implicit context:
462 $ret = "#ifndef PERL_IMPLICIT_CONTEXT\n"
463 . hide($func,"Perl_$func") . "#endif\n";
467 my $alist = join(",", @az[0..$args-1]);
468 $ret = "#define $func($alist)";
469 my $t = int(length($ret) / 8);
470 $ret .= "\t" x ($t < 4 ? 4 - $t : 1);
471 if ($flags =~ /[si]/) {
472 $ret .= "S_$func(aTHX";
474 elsif ($flags =~ /p/) {
475 $ret .= "Perl_$func(aTHX";
477 $ret .= "_ " if $alist;
478 $ret .= $alist . ")\n";
483 # Prune empty #if/#endif pairs.
484 while ($lines =~ s/#\s*if[^\n]+\n#\s*endif\n//) {
486 # Merge adjacent blocks.
487 while ($lines =~ s/(#ifndef PERL_IMPLICIT_CONTEXT
490 #ifndef PERL_IMPLICIT_CONTEXT
495 print $em "#endif\n" if $guard;
499 embed_h('#if defined(PERL_CORE) || defined(PERL_EXT)', \@ext);
500 embed_h('#ifdef PERL_CORE', \@core);
504 #endif /* #ifndef PERL_NO_SHORT_NAMES */
506 /* Compatibility stubs. Compile extensions with -DPERL_NOCOMPAT to
510 #if !defined(PERL_CORE)
511 # define sv_setptrobj(rv,ptr,name) sv_setref_iv(rv,name,PTR2IV(ptr))
512 # define sv_setptrref(rv,ptr) sv_setref_iv(rv,NULL,PTR2IV(ptr))
515 #if !defined(PERL_CORE) && !defined(PERL_NOCOMPAT)
517 /* Compatibility for various misnamed functions. All functions
518 in the API that begin with "perl_" (not "Perl_") take an explicit
519 interpreter context pointer.
520 The following are not like that, but since they had a "perl_"
521 prefix in previous versions, we provide compatibility macros.
523 # define perl_atexit(a,b) call_atexit(a,b)
527 my ($flags,$retval,$func,@args) = @_;
529 return unless $flags =~ /O/;
531 my $alist = join ",", @az[0..$#args];
532 my $ret = "# define perl_$func($alist)";
533 my $t = (length $ret) >> 3;
534 $ret .= "\t" x ($t < 5 ? 5 - $t : 1);
535 "$ret$func($alist)\n";
540 /* varargs functions can't be handled with CPP macros. :-(
541 This provides a set of compatibility functions that don't take
542 an extra argument but grab the context pointer using the macro
545 #if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_NO_SHORT_NAMES)
548 foreach (sort keys %has_va) {
549 next unless $has_nocontext{$_};
550 next if /printf/; # Not clear to me why these are skipped but they are.
551 print $em hide($_, "Perl_${_}_nocontext", " ");
557 #endif /* !defined(PERL_CORE) && !defined(PERL_NOCOMPAT) */
559 #if !defined(PERL_IMPLICIT_CONTEXT)
560 /* undefined symbols, point them back at the usual ones */
563 foreach (sort keys %has_va) {
564 next unless $has_nocontext{$_};
565 next if /printf/; # Not clear to me why these are skipped but they are.
566 print $em hide("Perl_${_}_nocontext", "Perl_$_", " ");
573 read_only_bottom_close_and_rename($em);
575 $em = open_print_header('embedvar.h');
578 /* (Doing namespace management portably in C is really gross.) */
581 The following combinations of MULTIPLICITY and PERL_IMPLICIT_CONTEXT
584 2) MULTIPLICITY # supported for compatibility
585 3) MULTIPLICITY && PERL_IMPLICIT_CONTEXT
587 All other combinations of these flags are errors.
589 only #3 is supported directly, while #2 is a special
590 case of #3 (supported by redefining vTHX appropriately).
593 #if defined(MULTIPLICITY)
594 /* cases 2 and 3 above */
596 # if defined(PERL_IMPLICIT_CONTEXT)
599 # define vTHX PERL_GET_INTERP
604 for $sym (sort keys %intrp) {
605 print $em multon($sym,'I','vTHX->');
610 #else /* !MULTIPLICITY */
616 for $sym (sort keys %intrp) {
617 print $em multoff($sym,'I');
626 #endif /* MULTIPLICITY */
628 #if defined(PERL_GLOBAL_STRUCT)
632 for $sym (sort keys %globvar) {
633 print $em "#ifdef OS2\n" if $sym eq 'sh_path';
634 print $em multon($sym, 'G','my_vars->');
635 print $em multon("G$sym",'', 'my_vars->');
636 print $em "#endif\n" if $sym eq 'sh_path';
641 #else /* !PERL_GLOBAL_STRUCT */
645 for $sym (sort keys %globvar) {
646 print $em "#ifdef OS2\n" if $sym eq 'sh_path';
647 print $em multoff($sym,'G');
648 print $em "#endif\n" if $sym eq 'sh_path';
653 #endif /* PERL_GLOBAL_STRUCT */
656 read_only_bottom_close_and_rename($em);
658 my $capih = open_print_header('perlapi.h');
660 print $capih <<'EOT';
661 /* declare accessor functions for Perl variables */
662 #ifndef __perlapi_h__
663 #define __perlapi_h__
665 #if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
673 #define PERLVAR(v,t) EXTERN_C t* Perl_##v##_ptr(pTHX);
674 #define PERLVARA(v,n,t) typedef t PL_##v##_t[n]; \
675 EXTERN_C PL_##v##_t* Perl_##v##_ptr(pTHX);
676 #define PERLVARI(v,t,i) PERLVAR(v,t)
677 #define PERLVARIC(v,t,i) PERLVAR(v, const t)
679 #include "perlvars.h"
688 #if defined(PERL_CORE)
690 /* accessor functions for Perl "global" variables */
692 /* these need to be mentioned here, or most linkers won't put them in
693 the perl executable */
695 #ifndef PERL_NO_FORCE_LINK
700 EXTCONST void * const PL_force_link_funcs[];
702 EXTCONST void * const PL_force_link_funcs[] = {
707 #define PERLVAR(v,t) (void*)Perl_##v##_ptr,
708 #define PERLVARA(v,n,t) PERLVAR(v,t)
709 #define PERLVARI(v,t,i) PERLVAR(v,t)
710 #define PERLVARIC(v,t,i) PERLVAR(v,t)
712 /* In Tru64 (__DEC && __osf__) the cc option -std1 causes that one
713 * cannot cast between void pointers and function pointers without
714 * info level warnings. The PL_force_link_funcs[] would cause a few
715 * hundred of those warnings. In code one can circumnavigate this by using
716 * unions that overlay the different pointers, but in declarations one
717 * cannot use this trick. Therefore we just disable the warning here
718 * for the duration of the PL_force_link_funcs[] declaration. */
720 #if defined(__DECC) && defined(__osf__)
722 #pragma message disable (nonstandcast)
725 #include "perlvars.h"
727 #if defined(__DECC) && defined(__osf__)
728 #pragma message restore
740 #endif /* PERL_NO_FORCE_LINK */
742 #else /* !PERL_CORE */
746 foreach $sym (sort keys %globvar) {
747 print $capih bincompat_var('G',$sym);
750 print $capih <<'EOT';
752 #endif /* !PERL_CORE */
753 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
755 #endif /* __perlapi_h__ */
758 read_only_bottom_close_and_rename($capih);
760 my $capi = open_print_header('perlapi.c', <<'EOQ');
763 * Up to the threshold of the door there mounted a flight of twenty-seven
764 * broad stairs, hewn by some unknown art of the same black stone. This
765 * was the only entrance to the tower; ...
767 * [p.577 of _The Lord of the Rings_, III/x: "The Voice of Saruman"]
777 #if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
779 /* accessor functions for Perl "global" variables */
783 #define PERLVARI(v,t,i) PERLVAR(v,t)
787 #define PERLVAR(v,t) t* Perl_##v##_ptr(pTHX) \
788 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
789 #define PERLVARA(v,n,t) PL_##v##_t* Perl_##v##_ptr(pTHX) \
790 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
792 #define PERLVARIC(v,t,i) \
793 const t* Perl_##v##_ptr(pTHX) \
794 { PERL_UNUSED_CONTEXT; return (const t *)&(PL_##v); }
795 #include "perlvars.h"
804 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
807 read_only_bottom_close_and_rename($capi);
809 # ex: set ts=8 sts=4 sw=4 noet: