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 =~ /p/;
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) = @$_;
78 if ($flags =~ / ( [^AabDdEfiMmnOoPpRrsUXx] ) /x) {
79 warn "flag $1 is not legal (for function $plain_func)";
82 my $has_context = ( $flags !~ /n/ );
83 my $never_returns = ( $flags =~ /r/ );
84 my $binarycompat = ( $flags =~ /b/ );
85 my $commented_out = ( ! $binarycompat && $flags =~ /m/ );
86 my $is_malloc = ( $flags =~ /a/ );
87 my $can_ignore = ( $flags !~ /R/ ) && !$is_malloc;
91 if (! $can_ignore && $retval eq 'void') {
92 warn "It is nonsensical to require the return value of a void function ($plain_func) to be checked";
95 my $scope_type_flag_count = 0;
96 $scope_type_flag_count++ if $flags =~ /s/;
97 $scope_type_flag_count++ if $flags =~ /i/;
98 $scope_type_flag_count++ if $flags =~ /p/;
99 warn "$plain_func: i, p, and s flags are all mutually exclusive"
100 if $scope_type_flag_count > 1;
101 my $splint_flags = "";
102 if ( $SPLINT && !$commented_out ) {
103 $splint_flags .= '/*@noreturn@*/ ' if $never_returns;
104 if ($can_ignore && ($retval ne 'void') && ($retval !~ /\*/)) {
105 $retval .= " /*\@alt void\@*/";
109 if ($flags =~ /([si])/) {
111 if ($never_returns) {
112 $type = $1 eq 's' ? "PERL_STATIC_NO_RET" : "PERL_STATIC_INLINE_NO_RET";
115 $type = $1 eq 's' ? "STATIC" : "PERL_STATIC_INLINE";
117 $retval = "$type $splint_flags$retval";
120 if ($never_returns) {
121 $retval = "PERL_CALLCONV_NO_RET $splint_flags$retval";
124 $retval = "PERL_CALLCONV $splint_flags$retval";
127 $func = full_name($plain_func, $flags);
129 $ret .= "#ifndef NO_MATHOMS\n" if $binarycompat;
130 $ret .= "$retval\t$func(";
131 if ( $has_context ) {
132 $ret .= @args ? "pTHX_ " : "pTHX";
136 for my $arg ( @args ) {
138 if ( $arg =~ /\*/ && $arg !~ /\b(NN|NULLOK)\b/ ) {
139 warn "$func: $arg needs NN or NULLOK\n";
140 ++$unflagged_pointers;
142 my $nn = ( $arg =~ s/\s*\bNN\b\s+// );
143 push( @nonnull, $n ) if $nn;
145 my $nullok = ( $arg =~ s/\s*\bNULLOK\b\s+// ); # strip NULLOK with no effect
147 # Make sure each arg has at least a type and a var name.
148 # An arg of "int" is valid C, but want it to be "int foo".
150 $temp_arg =~ s/\*//g;
151 $temp_arg =~ s/\s*\bstruct\b\s*/ /g;
152 if ( ($temp_arg ne "...")
153 && ($temp_arg !~ /\w+\s+(\w+)(?:\[\d+\])?\s*$/) ) {
154 warn "$func: $arg ($n) doesn't have a name\n";
156 if ( $SPLINT && $nullok && !$commented_out ) {
157 $arg = '/*@null@*/ ' . $arg;
159 if (defined $1 && $nn && !($commented_out && !$binarycompat)) {
160 push @names_of_nn, $1;
163 $ret .= join ", ", @args;
166 $ret .= "void" if !$has_context;
170 if ( $flags =~ /r/ ) {
171 push @attrs, "__attribute__noreturn__";
173 if ( $flags =~ /D/ ) {
174 push @attrs, "__attribute__deprecated__";
177 push @attrs, "__attribute__malloc__";
179 if ( !$can_ignore ) {
180 push @attrs, "__attribute__warn_unused_result__";
182 if ( $flags =~ /P/ ) {
183 push @attrs, "__attribute__pure__";
185 if( $flags =~ /f/ ) {
186 my $prefix = $has_context ? 'pTHX_' : '';
188 if ($args[-1] eq '...') {
189 $args = scalar @args;
191 $args = $prefix . $args;
194 # don't check args, and guess which arg is the pattern
195 # (one of 'fmt', 'pat', 'f'),
197 my @fmts = grep $args[$_] =~ /\b(f|pat|fmt)$/, 0..$#args;
199 die "embed.pl: '$plain_func': can't determine pattern arg\n";
203 my $macro = grep($_ == $pat, @nonnull)
204 ? '__attribute__format__'
205 : '__attribute__format__null_ok__';
206 if ($plain_func =~ /strftime/) {
207 push @attrs, sprintf "%s(__strftime__,%s1,0)", $macro, $prefix;
210 push @attrs, sprintf "%s(__printf__,%s%d,%s)", $macro,
211 $prefix, $pat, $args;
216 $ret .= join( "\n", map { "\t\t\t$_" } @attrs );
219 $ret = "/* $ret */" if $commented_out;
221 $ret .= "\n#define PERL_ARGS_ASSERT_\U$plain_func\E\t\\\n\t"
222 . join '; ', map "assert($_)", @names_of_nn;
224 $ret .= "\n#endif" if $binarycompat;
225 $ret .= @attrs ? "\n\n" : "\n";
232 # include "pp_proto.h"
237 read_only_bottom_close_and_rename($pr);
240 warn "$unflagged_pointers pointer arguments to clean up\n" if $unflagged_pointers;
243 my ($file, $pre) = @_;
246 open(FILE, "< $file")
247 or die "embed.pl: Can't open $file: $!\n";
249 s/[ \t]*#.*//; # Delete comments.
250 if (/PERLVARA?I?C?\($pre,\s*(\w+)/) {
251 warn "duplicate symbol $1 while processing $file line $.\n"
256 return sort keys %seen;
259 my @intrp = readvars 'intrpvar.h','I';
260 my @globvar = readvars 'perlvars.h','G';
263 my ($from, $to, $indent) = @_;
264 $indent = '' unless defined $indent;
265 my $t = int(length("$indent$from") / 8);
266 "#${indent}define $from" . "\t" x ($t < 3 ? 3 - $t : 1) . "$to\n";
270 my ($sym,$pre,$ptr) = @_;
271 hide("PL_$sym", "($ptr$pre$sym)");
274 my $em = open_print_header('embed.h');
277 /* (Doing namespace management portably in C is really gross.) */
279 /* By defining PERL_NO_SHORT_NAMES (not done by default) the short forms
280 * (like warn instead of Perl_warn) for the API are not defined.
281 * Not defining the short forms is a good thing for cleaner embedding. */
283 #ifndef PERL_NO_SHORT_NAMES
285 /* Hide global symbols */
292 my ($guard, $funcs) = @_;
293 print $em "$guard\n" if $guard;
299 # Indent the conditionals if we are wrapped in an #if/#endif pair.
300 $cond =~ s/#(.*)/# $1/ if $guard;
305 my ($flags,$retval,$func,@args) = @$_;
306 unless ($flags =~ /[om]/) {
307 my $args = scalar @args;
309 my $full_name = full_name($func, $flags);
310 next if $full_name eq $func; # Don't output a no-op.
311 $ret = hide($func, $full_name);
313 elsif ($args and $args[$args-1] =~ /\.\.\./) {
315 # we're out of luck for varargs functions under CPP
316 # So we can only do these macros for no implicit context:
317 $ret = "#ifndef PERL_IMPLICIT_CONTEXT\n"
318 . hide($func, full_name($func, $flags)) . "#endif\n";
322 my $alist = join(",", @az[0..$args-1]);
323 $ret = "#define $func($alist)";
324 my $t = int(length($ret) / 8);
325 $ret .= "\t" x ($t < 4 ? 4 - $t : 1);
326 $ret .= full_name($func, $flags) . "(aTHX";
327 $ret .= "_ " if $alist;
328 $ret .= $alist . ")\n";
330 $ret = "#ifndef NO_MATHOMS\n$ret#endif\n" if $flags =~ /b/;
334 # Prune empty #if/#endif pairs.
335 while ($lines =~ s/#\s*if[^\n]+\n#\s*endif\n//) {
337 # Merge adjacent blocks.
338 while ($lines =~ s/(#ifndef PERL_IMPLICIT_CONTEXT
341 #ifndef PERL_IMPLICIT_CONTEXT
346 print $em "#endif\n" if $guard;
350 embed_h('#if defined(PERL_CORE) || defined(PERL_EXT)', $ext);
351 embed_h('#ifdef PERL_CORE', $core);
355 #endif /* #ifndef PERL_NO_SHORT_NAMES */
357 /* Compatibility stubs. Compile extensions with -DPERL_NOCOMPAT to
361 #if !defined(PERL_CORE)
362 # define sv_setptrobj(rv,ptr,name) sv_setref_iv(rv,name,PTR2IV(ptr))
363 # define sv_setptrref(rv,ptr) sv_setref_iv(rv,NULL,PTR2IV(ptr))
366 #if !defined(PERL_CORE) && !defined(PERL_NOCOMPAT)
368 /* Compatibility for various misnamed functions. All functions
369 in the API that begin with "perl_" (not "Perl_") take an explicit
370 interpreter context pointer.
371 The following are not like that, but since they had a "perl_"
372 prefix in previous versions, we provide compatibility macros.
374 # define perl_atexit(a,b) call_atexit(a,b)
378 my ($flags, $retval, $func, @args) = @$_;
380 next unless $flags =~ /O/;
382 my $alist = join ",", @az[0..$#args];
383 my $ret = "# define perl_$func($alist)";
384 my $t = (length $ret) >> 3;
385 $ret .= "\t" x ($t < 5 ? 5 - $t : 1);
386 print $em "$ret$func($alist)\n";
391 my (%has_va, %has_nocontext);
394 ++$has_va{$_->[2]} if $_->[-1] =~ /\.\.\./;
395 ++$has_nocontext{$1} if $_->[2] =~ /(.*)_nocontext/;
398 @nocontext = sort grep {
400 && !/printf/ # Not clear to me why these are skipped but they are.
406 /* varargs functions can't be handled with CPP macros. :-(
407 This provides a set of compatibility functions that don't take
408 an extra argument but grab the context pointer using the macro
411 #if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_NO_SHORT_NAMES)
414 foreach (@nocontext) {
415 print $em hide($_, "Perl_${_}_nocontext", " ");
421 #endif /* !defined(PERL_CORE) && !defined(PERL_NOCOMPAT) */
423 #if !defined(PERL_IMPLICIT_CONTEXT)
424 /* undefined symbols, point them back at the usual ones */
427 foreach (@nocontext) {
428 print $em hide("Perl_${_}_nocontext", "Perl_$_", " ");
435 read_only_bottom_close_and_rename($em);
437 $em = open_print_header('embedvar.h');
440 /* (Doing namespace management portably in C is really gross.) */
443 The following combinations of MULTIPLICITY and PERL_IMPLICIT_CONTEXT
446 2) MULTIPLICITY # supported for compatibility
447 3) MULTIPLICITY && PERL_IMPLICIT_CONTEXT
449 All other combinations of these flags are errors.
451 only #3 is supported directly, while #2 is a special
452 case of #3 (supported by redefining vTHX appropriately).
455 #if defined(MULTIPLICITY)
456 /* cases 2 and 3 above */
458 # if defined(PERL_IMPLICIT_CONTEXT)
461 # define vTHX PERL_GET_INTERP
469 if ($sym eq 'sawampersand') {
470 print $em "#ifndef PL_sawampersand\n";
472 print $em multon($sym,'I','vTHX->');
473 if ($sym eq 'sawampersand') {
474 print $em "#endif\n";
480 #endif /* MULTIPLICITY */
482 #if defined(PERL_GLOBAL_STRUCT)
486 for $sym (@globvar) {
487 print $em "#ifdef OS2\n" if $sym eq 'sh_path';
488 print $em multon($sym, 'G','my_vars->');
489 print $em multon("G$sym",'', 'my_vars->');
490 print $em "#endif\n" if $sym eq 'sh_path';
495 #endif /* PERL_GLOBAL_STRUCT */
498 read_only_bottom_close_and_rename($em);
500 my $capih = open_print_header('perlapi.h');
502 print $capih <<'EOT';
503 /* declare accessor functions for Perl variables */
504 #ifndef __perlapi_h__
505 #define __perlapi_h__
507 #if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
515 #define PERLVAR(p,v,t) EXTERN_C t* Perl_##p##v##_ptr(pTHX);
516 #define PERLVARA(p,v,n,t) typedef t PL_##v##_t[n]; \
517 EXTERN_C PL_##v##_t* Perl_##p##v##_ptr(pTHX);
518 #define PERLVARI(p,v,t,i) PERLVAR(p,v,t)
519 #define PERLVARIC(p,v,t,i) PERLVAR(p,v, const t)
521 #include "perlvars.h"
530 #if defined(PERL_CORE)
532 /* accessor functions for Perl "global" variables */
534 /* these need to be mentioned here, or most linkers won't put them in
535 the perl executable */
537 #ifndef PERL_NO_FORCE_LINK
542 EXTCONST void * const PL_force_link_funcs[];
544 EXTCONST void * const PL_force_link_funcs[] = {
549 #define PERLVAR(p,v,t) (void*)Perl_##p##v##_ptr,
550 #define PERLVARA(p,v,n,t) PERLVAR(p,v,t)
551 #define PERLVARI(p,v,t,i) PERLVAR(p,v,t)
552 #define PERLVARIC(p,v,t,i) PERLVAR(p,v,t)
554 /* In Tru64 (__DEC && __osf__) the cc option -std1 causes that one
555 * cannot cast between void pointers and function pointers without
556 * info level warnings. The PL_force_link_funcs[] would cause a few
557 * hundred of those warnings. In code one can circumnavigate this by using
558 * unions that overlay the different pointers, but in declarations one
559 * cannot use this trick. Therefore we just disable the warning here
560 * for the duration of the PL_force_link_funcs[] declaration. */
562 #if defined(__DECC) && defined(__osf__)
564 #pragma message disable (nonstandcast)
567 #include "perlvars.h"
569 #if defined(__DECC) && defined(__osf__)
570 #pragma message restore
582 #endif /* PERL_NO_FORCE_LINK */
584 #else /* !PERL_CORE */
588 foreach $sym (@globvar) {
590 "#undef PL_$sym\n" . hide("PL_$sym", "(*Perl_G${sym}_ptr(NULL))");
593 print $capih <<'EOT';
595 #endif /* !PERL_CORE */
596 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
598 #endif /* __perlapi_h__ */
601 read_only_bottom_close_and_rename($capih);
603 my $capi = open_print_header('perlapi.c', <<'EOQ');
606 * Up to the threshold of the door there mounted a flight of twenty-seven
607 * broad stairs, hewn by some unknown art of the same black stone. This
608 * was the only entrance to the tower; ...
610 * [p.577 of _The Lord of the Rings_, III/x: "The Voice of Saruman"]
620 #if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
622 /* accessor functions for Perl "global" variables */
626 #define PERLVARI(p,v,t,i) PERLVAR(p,v,t)
630 #define PERLVAR(p,v,t) t* Perl_##p##v##_ptr(pTHX) \
631 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
632 #define PERLVARA(p,v,n,t) PL_##v##_t* Perl_##p##v##_ptr(pTHX) \
633 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
635 #define PERLVARIC(p,v,t,i) \
636 const t* Perl_##p##v##_ptr(pTHX) \
637 { PERL_UNUSED_CONTEXT; return (const t *)&(PL_##v); }
638 #include "perlvars.h"
647 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
650 read_only_bottom_close_and_rename($capi);
652 # ex: set ts=8 sts=4 sw=4 noet: