3 # Regenerate (overwriting only if changed):
11 # from information stored in
18 # Accepts the standard regen_lib -q and -v args.
20 # This script is normally invoked from regen.pl.
22 require 5.004; # keep this compatible, an old perl is all we may have before
23 # we build the new one
28 # Get function prototypes
29 require 'regen/regen_lib.pl';
30 require 'regen/embed_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 full_name ($$) { # Returns the function name with potentially the
44 # prefixes 'S_' or 'Perl_'
45 my ($func, $flags) = @_;
47 return "S_$func" if $flags =~ /[si]/;
48 return "Perl_$func" if $flags =~ /[bp]/;
52 sub open_print_header {
53 my ($file, $quote) = @_;
55 return open_new($file, '>',
56 { file => $file, style => '*', by => 'regen/embed.pl',
57 from => ['data in embed.fnc', 'regen/embed.pl',
58 'regen/opcodes', 'intrpvar.h', 'perlvars.h'],
59 final => "\nEdit those files and run 'make regen_headers' to effect changes.\n",
60 copyright => [1993 .. 2009], quote => $quote });
63 my ($embed, $core, $ext, $api) = setup_embed();
67 my $pr = open_print_header("proto.h");
68 print $pr "START_EXTERN_C\n";
73 print $pr "$_->[0]\n";
77 my ($flags,$retval,$plain_func,@args) = @$_;
79 my $has_context = ( $flags !~ /n/ );
80 my $never_returns = ( $flags =~ /r/ );
81 my $commented_out = ( $flags =~ /m/ );
82 my $binarycompat = ( $flags =~ /b/ );
83 my $is_malloc = ( $flags =~ /a/ );
84 my $can_ignore = ( $flags !~ /R/ ) && !$is_malloc;
88 if (! $can_ignore && $retval eq 'void') {
89 warn "It is nonsensical to require the return value of a void function ($plain_func) to be checked";
92 my $scope_type_flag_count = 0;
93 $scope_type_flag_count++ if $flags =~ /s/;
94 $scope_type_flag_count++ if $flags =~ /i/;
95 $scope_type_flag_count++ if $flags =~ /p/;
96 warn "$plain_func: i, p, and s flags are all mutually exclusive"
97 if $scope_type_flag_count > 1;
98 my $splint_flags = "";
99 if ( $SPLINT && !$commented_out ) {
100 $splint_flags .= '/*@noreturn@*/ ' if $never_returns;
101 if ($can_ignore && ($retval ne 'void') && ($retval !~ /\*/)) {
102 $retval .= " /*\@alt void\@*/";
106 if ($flags =~ /([si])/) {
108 if ($never_returns) {
109 $type = $1 eq 's' ? "PERL_STATIC_NO_RET" : "PERL_STATIC_INLINE_NO_RET";
112 $type = $1 eq 's' ? "STATIC" : "PERL_STATIC_INLINE";
114 $retval = "$type $splint_flags$retval";
117 if ($never_returns) {
118 $retval = "PERL_CALLCONV_NO_RET $splint_flags$retval";
121 $retval = "PERL_CALLCONV $splint_flags$retval";
124 $func = full_name($plain_func, $flags);
125 $ret = "$retval\t$func(";
126 if ( $has_context ) {
127 $ret .= @args ? "pTHX_ " : "pTHX";
131 for my $arg ( @args ) {
133 if ( $arg =~ /\*/ && $arg !~ /\b(NN|NULLOK)\b/ ) {
134 warn "$func: $arg needs NN or NULLOK\n";
135 ++$unflagged_pointers;
137 my $nn = ( $arg =~ s/\s*\bNN\b\s+// );
138 push( @nonnull, $n ) if $nn;
140 my $nullok = ( $arg =~ s/\s*\bNULLOK\b\s+// ); # strip NULLOK with no effect
142 # Make sure each arg has at least a type and a var name.
143 # An arg of "int" is valid C, but want it to be "int foo".
145 $temp_arg =~ s/\*//g;
146 $temp_arg =~ s/\s*\bstruct\b\s*/ /g;
147 if ( ($temp_arg ne "...")
148 && ($temp_arg !~ /\w+\s+(\w+)(?:\[\d+\])?\s*$/) ) {
149 warn "$func: $arg ($n) doesn't have a name\n";
151 if ( $SPLINT && $nullok && !$commented_out ) {
152 $arg = '/*@null@*/ ' . $arg;
154 if (defined $1 && $nn && !($commented_out && !$binarycompat)) {
155 push @names_of_nn, $1;
158 $ret .= join ", ", @args;
161 $ret .= "void" if !$has_context;
165 if ( $flags =~ /r/ ) {
166 push @attrs, "__attribute__noreturn__";
168 if ( $flags =~ /D/ ) {
169 push @attrs, "__attribute__deprecated__";
172 push @attrs, "__attribute__malloc__";
174 if ( !$can_ignore ) {
175 push @attrs, "__attribute__warn_unused_result__";
177 if ( $flags =~ /P/ ) {
178 push @attrs, "__attribute__pure__";
180 if( $flags =~ /f/ ) {
181 my $prefix = $has_context ? 'pTHX_' : '';
183 if ($args[-1] eq '...') {
184 $args = scalar @args;
186 $args = $prefix . $args;
189 # don't check args, and guess which arg is the pattern
190 # (one of 'fmt', 'pat', 'f'),
192 my @fmts = grep $args[$_] =~ /\b(f|pat|fmt)$/, 0..$#args;
194 die "embed.pl: '$plain_func': can't determine pattern arg\n";
198 my $macro = grep($_ == $pat, @nonnull)
199 ? '__attribute__format__'
200 : '__attribute__format__null_ok__';
201 if ($plain_func =~ /strftime/) {
202 push @attrs, sprintf "%s(__strftime__,%s1,0)", $macro, $prefix;
205 push @attrs, sprintf "%s(__printf__,%s%d,%s)", $macro,
206 $prefix, $pat, $args;
211 $ret .= join( "\n", map { "\t\t\t$_" } @attrs );
214 $ret = "/* $ret */" if $commented_out;
216 $ret .= "\n#define PERL_ARGS_ASSERT_\U$plain_func\E\t\\\n\t"
217 . join '; ', map "assert($_)", @names_of_nn;
219 $ret .= @attrs ? "\n\n" : "\n";
226 # include "pp_proto.h"
231 read_only_bottom_close_and_rename($pr);
234 warn "$unflagged_pointers pointer arguments to clean up\n" if $unflagged_pointers;
237 my ($file, $pre) = @_;
240 open(FILE, "< $file")
241 or die "embed.pl: Can't open $file: $!\n";
243 s/[ \t]*#.*//; # Delete comments.
244 if (/PERLVARA?I?C?\($pre,\s*(\w+)/) {
245 warn "duplicate symbol $1 while processing $file line $.\n"
250 return sort keys %seen;
253 my @intrp = readvars 'intrpvar.h','I';
254 my @globvar = readvars 'perlvars.h','G';
257 my ($from, $to, $indent) = @_;
258 $indent = '' unless defined $indent;
259 my $t = int(length("$indent$from") / 8);
260 "#${indent}define $from" . "\t" x ($t < 3 ? 3 - $t : 1) . "$to\n";
264 my ($sym,$pre,$ptr) = @_;
265 hide("PL_$sym", "($ptr$pre$sym)");
268 my $em = open_print_header('embed.h');
271 /* (Doing namespace management portably in C is really gross.) */
273 /* By defining PERL_NO_SHORT_NAMES (not done by default) the short forms
274 * (like warn instead of Perl_warn) for the API are not defined.
275 * Not defining the short forms is a good thing for cleaner embedding. */
277 #ifndef PERL_NO_SHORT_NAMES
279 /* Hide global symbols */
286 my ($guard, $funcs) = @_;
287 print $em "$guard\n" if $guard;
293 # Indent the conditionals if we are wrapped in an #if/#endif pair.
294 $cond =~ s/#(.*)/# $1/ if $guard;
299 my ($flags,$retval,$func,@args) = @$_;
300 unless ($flags =~ /[om]/) {
301 my $args = scalar @args;
303 $ret = hide($func, full_name($func, $flags));
305 elsif ($args and $args[$args-1] =~ /\.\.\./) {
307 # we're out of luck for varargs functions under CPP
308 # So we can only do these macros for no implicit context:
309 $ret = "#ifndef PERL_IMPLICIT_CONTEXT\n"
310 . hide($func, full_name($func, $flags)) . "#endif\n";
314 my $alist = join(",", @az[0..$args-1]);
315 $ret = "#define $func($alist)";
316 my $t = int(length($ret) / 8);
317 $ret .= "\t" x ($t < 4 ? 4 - $t : 1);
318 $ret .= full_name($func, $flags) . "(aTHX";
319 $ret .= "_ " if $alist;
320 $ret .= $alist . ")\n";
325 # Prune empty #if/#endif pairs.
326 while ($lines =~ s/#\s*if[^\n]+\n#\s*endif\n//) {
328 # Merge adjacent blocks.
329 while ($lines =~ s/(#ifndef PERL_IMPLICIT_CONTEXT
332 #ifndef PERL_IMPLICIT_CONTEXT
337 print $em "#endif\n" if $guard;
341 embed_h('#if defined(PERL_CORE) || defined(PERL_EXT)', $ext);
342 embed_h('#ifdef PERL_CORE', $core);
346 #endif /* #ifndef PERL_NO_SHORT_NAMES */
348 /* Compatibility stubs. Compile extensions with -DPERL_NOCOMPAT to
352 #if !defined(PERL_CORE)
353 # define sv_setptrobj(rv,ptr,name) sv_setref_iv(rv,name,PTR2IV(ptr))
354 # define sv_setptrref(rv,ptr) sv_setref_iv(rv,NULL,PTR2IV(ptr))
357 #if !defined(PERL_CORE) && !defined(PERL_NOCOMPAT)
359 /* Compatibility for various misnamed functions. All functions
360 in the API that begin with "perl_" (not "Perl_") take an explicit
361 interpreter context pointer.
362 The following are not like that, but since they had a "perl_"
363 prefix in previous versions, we provide compatibility macros.
365 # define perl_atexit(a,b) call_atexit(a,b)
369 my ($flags, $retval, $func, @args) = @$_;
371 next unless $flags =~ /O/;
373 my $alist = join ",", @az[0..$#args];
374 my $ret = "# define perl_$func($alist)";
375 my $t = (length $ret) >> 3;
376 $ret .= "\t" x ($t < 5 ? 5 - $t : 1);
377 print $em "$ret$func($alist)\n";
382 my (%has_va, %has_nocontext);
385 ++$has_va{$_->[2]} if $_->[-1] =~ /\.\.\./;
386 ++$has_nocontext{$1} if $_->[2] =~ /(.*)_nocontext/;
389 @nocontext = sort grep {
391 && !/printf/ # Not clear to me why these are skipped but they are.
397 /* varargs functions can't be handled with CPP macros. :-(
398 This provides a set of compatibility functions that don't take
399 an extra argument but grab the context pointer using the macro
402 #if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_NO_SHORT_NAMES)
405 foreach (@nocontext) {
406 print $em hide($_, "Perl_${_}_nocontext", " ");
412 #endif /* !defined(PERL_CORE) && !defined(PERL_NOCOMPAT) */
414 #if !defined(PERL_IMPLICIT_CONTEXT)
415 /* undefined symbols, point them back at the usual ones */
418 foreach (@nocontext) {
419 print $em hide("Perl_${_}_nocontext", "Perl_$_", " ");
426 read_only_bottom_close_and_rename($em);
428 $em = open_print_header('embedvar.h');
431 /* (Doing namespace management portably in C is really gross.) */
434 The following combinations of MULTIPLICITY and PERL_IMPLICIT_CONTEXT
437 2) MULTIPLICITY # supported for compatibility
438 3) MULTIPLICITY && PERL_IMPLICIT_CONTEXT
440 All other combinations of these flags are errors.
442 only #3 is supported directly, while #2 is a special
443 case of #3 (supported by redefining vTHX appropriately).
446 #if defined(MULTIPLICITY)
447 /* cases 2 and 3 above */
449 # if defined(PERL_IMPLICIT_CONTEXT)
452 # define vTHX PERL_GET_INTERP
460 if ($sym eq 'sawampersand') {
461 print $em "#ifndef PL_sawampersand\n";
463 print $em multon($sym,'I','vTHX->');
464 if ($sym eq 'sawampersand') {
465 print $em "#endif\n";
471 #endif /* MULTIPLICITY */
473 #if defined(PERL_GLOBAL_STRUCT)
477 for $sym (@globvar) {
478 print $em "#ifdef OS2\n" if $sym eq 'sh_path';
479 print $em multon($sym, 'G','my_vars->');
480 print $em multon("G$sym",'', 'my_vars->');
481 print $em "#endif\n" if $sym eq 'sh_path';
486 #endif /* PERL_GLOBAL_STRUCT */
489 read_only_bottom_close_and_rename($em);
491 my $capih = open_print_header('perlapi.h');
493 print $capih <<'EOT';
494 /* declare accessor functions for Perl variables */
495 #ifndef __perlapi_h__
496 #define __perlapi_h__
498 #if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
506 #define PERLVAR(p,v,t) EXTERN_C t* Perl_##p##v##_ptr(pTHX);
507 #define PERLVARA(p,v,n,t) typedef t PL_##v##_t[n]; \
508 EXTERN_C PL_##v##_t* Perl_##p##v##_ptr(pTHX);
509 #define PERLVARI(p,v,t,i) PERLVAR(p,v,t)
510 #define PERLVARIC(p,v,t,i) PERLVAR(p,v, const t)
512 #include "perlvars.h"
521 #if defined(PERL_CORE)
523 /* accessor functions for Perl "global" variables */
525 /* these need to be mentioned here, or most linkers won't put them in
526 the perl executable */
528 #ifndef PERL_NO_FORCE_LINK
533 EXTCONST void * const PL_force_link_funcs[];
535 EXTCONST void * const PL_force_link_funcs[] = {
540 #define PERLVAR(p,v,t) (void*)Perl_##p##v##_ptr,
541 #define PERLVARA(p,v,n,t) PERLVAR(p,v,t)
542 #define PERLVARI(p,v,t,i) PERLVAR(p,v,t)
543 #define PERLVARIC(p,v,t,i) PERLVAR(p,v,t)
545 /* In Tru64 (__DEC && __osf__) the cc option -std1 causes that one
546 * cannot cast between void pointers and function pointers without
547 * info level warnings. The PL_force_link_funcs[] would cause a few
548 * hundred of those warnings. In code one can circumnavigate this by using
549 * unions that overlay the different pointers, but in declarations one
550 * cannot use this trick. Therefore we just disable the warning here
551 * for the duration of the PL_force_link_funcs[] declaration. */
553 #if defined(__DECC) && defined(__osf__)
555 #pragma message disable (nonstandcast)
558 #include "perlvars.h"
560 #if defined(__DECC) && defined(__osf__)
561 #pragma message restore
573 #endif /* PERL_NO_FORCE_LINK */
575 #else /* !PERL_CORE */
579 foreach $sym (@globvar) {
581 "#undef PL_$sym\n" . hide("PL_$sym", "(*Perl_G${sym}_ptr(NULL))");
584 print $capih <<'EOT';
586 #endif /* !PERL_CORE */
587 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
589 #endif /* __perlapi_h__ */
592 read_only_bottom_close_and_rename($capih);
594 my $capi = open_print_header('perlapi.c', <<'EOQ');
597 * Up to the threshold of the door there mounted a flight of twenty-seven
598 * broad stairs, hewn by some unknown art of the same black stone. This
599 * was the only entrance to the tower; ...
601 * [p.577 of _The Lord of the Rings_, III/x: "The Voice of Saruman"]
611 #if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
613 /* accessor functions for Perl "global" variables */
617 #define PERLVARI(p,v,t,i) PERLVAR(p,v,t)
621 #define PERLVAR(p,v,t) t* Perl_##p##v##_ptr(pTHX) \
622 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
623 #define PERLVARA(p,v,n,t) PL_##v##_t* Perl_##p##v##_ptr(pTHX) \
624 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
626 #define PERLVARIC(p,v,t,i) \
627 const t* Perl_##p##v##_ptr(pTHX) \
628 { PERL_UNUSED_CONTEXT; return (const t *)&(PL_##v); }
629 #include "perlvars.h"
638 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
641 read_only_bottom_close_and_rename($capi);
643 # ex: set ts=8 sts=4 sw=4 noet: