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 $unflagged_pointers;
36 # See database of global and static function prototypes in embed.fnc
37 # This is used to generate prototype headers under various configurations,
38 # export symbols lists for different platforms, and macros to provide an
39 # implicit interpreter context argument.
43 sub die_at_end ($) { # Keeps going for now, but makes sure the regen doesn't
49 sub full_name ($$) { # Returns the function name with potentially the
50 # prefixes 'S_' or 'Perl_'
51 my ($func, $flags) = @_;
53 return "Perl_$func" if $flags =~ /p/;
54 return "S_$func" if $flags =~ /[Si]/;
58 sub open_print_header {
59 my ($file, $quote) = @_;
61 return open_new($file, '>',
62 { file => $file, style => '*', by => 'regen/embed.pl',
63 from => ['data in embed.fnc', 'regen/embed.pl',
64 'regen/opcodes', 'intrpvar.h', 'perlvars.h'],
65 final => "\nEdit those files and run 'make regen_headers' to effect changes.\n",
66 copyright => [1993 .. 2009], quote => $quote });
69 my ($embed, $core, $ext, $api) = setup_embed();
73 my $pr = open_print_header("proto.h");
74 print $pr "START_EXTERN_C\n";
79 print $pr "$_->[0]\n";
83 my ($flags,$retval,$plain_func,@args) = @$_;
84 if ($flags =~ / ( [^AabCDdEefGhiMmNnOoPpRrSsTUuWXx] ) /x) {
85 die_at_end "flag $1 is not legal (for function $plain_func)";
88 my $args_assert_line = ( $flags !~ /G/ );
89 my $has_depth = ( $flags =~ /W/ );
90 my $has_context = ( $flags !~ /T/ );
91 my $never_returns = ( $flags =~ /r/ );
92 my $binarycompat = ( $flags =~ /b/ );
93 my $commented_out = ( ! $binarycompat && $flags =~ /m/ );
94 my $is_malloc = ( $flags =~ /a/ );
95 my $can_ignore = ( $flags !~ /R/ ) && ( $flags !~ /P/ ) && !$is_malloc;
99 if (! $can_ignore && $retval eq 'void') {
100 warn "It is nonsensical to require the return value of a void function ($plain_func) to be checked";
103 die_at_end "$plain_func: S flag is mutually exclusive from the i and p plags"
104 if $flags =~ /S/ && $flags =~ /([ip])/;
105 die_at_end "$plain_func: m and $1 flags are mutually exclusive"
106 if $flags =~ /m/ && $flags =~ /([pS])/;
108 die_at_end "$plain_func: u flag only usable with m" if $flags =~ /u/ && $flags !~ /m/;
110 my $static_inline = 0;
111 if ($flags =~ /([Si])/) {
113 if ($never_returns) {
114 $type = $1 eq 'S' ? "PERL_STATIC_NO_RET" : "PERL_STATIC_INLINE_NO_RET";
117 $type = $1 eq 'S' ? "STATIC" : "PERL_STATIC_INLINE";
119 $retval = "$type $retval";
120 die_at_end "Don't declare static function '$plain_func' pure" if $flags =~ /P/;
121 $static_inline = $type eq 'PERL_STATIC_INLINE';
124 if ($never_returns) {
125 $retval = "PERL_CALLCONV_NO_RET $retval";
128 $retval = "PERL_CALLCONV $retval";
132 die_at_end "For '$plain_func', M flag requires p flag"
133 if $flags =~ /M/ && $flags !~ /p/;
134 die_at_end "For '$plain_func', b and m flags are mutually exclusive"
135 . " (try M flag)" if $flags =~ /b/ && $flags =~ /m/;
136 die_at_end "For '$plain_func', b flag without M flag requires D flag"
137 if $flags =~ /b/ && $flags !~ /M/ && $flags !~ /D/;
139 $func = full_name($plain_func, $flags);
141 $ret .= "$retval\t$func(";
142 if ( $has_context ) {
143 $ret .= @args ? "pTHX_ " : "pTHX";
147 for my $arg ( @args ) {
149 if ( $arg =~ /\*/ && $arg !~ /\b(NN|NULLOK)\b/ ) {
150 warn "$func: $arg needs NN or NULLOK\n";
151 ++$unflagged_pointers;
153 my $nn = ( $arg =~ s/\s*\bNN\b\s+// );
154 push( @nonnull, $n ) if $nn;
156 my $nullok = ( $arg =~ s/\s*\bNULLOK\b\s+// ); # strip NULLOK with no effect
158 # Make sure each arg has at least a type and a var name.
159 # An arg of "int" is valid C, but want it to be "int foo".
161 $temp_arg =~ s/\*//g;
162 $temp_arg =~ s/\s*\bstruct\b\s*/ /g;
163 if ( ($temp_arg ne "...")
164 && ($temp_arg !~ /\w+\s+(\w+)(?:\[\d+\])?\s*$/) ) {
165 die_at_end "$func: $arg ($n) doesn't have a name\n";
167 if (defined $1 && $nn && !($commented_out && !$binarycompat)) {
168 push @names_of_nn, $1;
171 $ret .= join ", ", @args;
174 $ret .= "void" if !$has_context;
176 $ret .= " _pDEPTH" if $has_depth;
179 if ( $flags =~ /r/ ) {
180 push @attrs, "__attribute__noreturn__";
182 if ( $flags =~ /D/ ) {
183 push @attrs, "__attribute__deprecated__";
186 push @attrs, "__attribute__malloc__";
188 if ( !$can_ignore ) {
189 push @attrs, "__attribute__warn_unused_result__";
191 if ( $flags =~ /P/ ) {
192 push @attrs, "__attribute__pure__";
194 if( $flags =~ /f/ ) {
195 my $prefix = $has_context ? 'pTHX_' : '';
197 if ($args[-1] eq '...') {
198 $args = scalar @args;
200 $args = $prefix . $args;
203 # don't check args, and guess which arg is the pattern
204 # (one of 'fmt', 'pat', 'f'),
206 my @fmts = grep $args[$_] =~ /\b(f|pat|fmt)$/, 0..$#args;
208 die "embed.pl: '$plain_func': can't determine pattern arg\n";
212 my $macro = grep($_ == $pat, @nonnull)
213 ? '__attribute__format__'
214 : '__attribute__format__null_ok__';
215 if ($plain_func =~ /strftime/) {
216 push @attrs, sprintf "%s(__strftime__,%s1,0)", $macro, $prefix;
219 push @attrs, sprintf "%s(__printf__,%s%d,%s)", $macro,
220 $prefix, $pat, $args;
225 $ret .= join( "\n", map { "\t\t\t$_" } @attrs );
228 $ret = "/* $ret */" if $commented_out;
230 $ret .= "\n#define PERL_ARGS_ASSERT_\U$plain_func\E"
231 if $args_assert_line || @names_of_nn;
232 $ret .= "\t\\\n\t" . join '; ', map "assert($_)", @names_of_nn
235 $ret = "#ifndef PERL_NO_INLINE_FUNCTIONS\n$ret\n#endif" if $static_inline;
236 $ret = "#ifndef NO_MATHOMS\n$ret\n#endif" if $binarycompat;
237 $ret .= @attrs ? "\n\n" : "\n";
244 # include "pp_proto.h"
249 read_only_bottom_close_and_rename($pr) if ! $error_count;
252 die_at_end "$unflagged_pointers pointer arguments to clean up\n" if $unflagged_pointers;
255 my ($file, $pre) = @_;
258 open(FILE, '<', $file)
259 or die "embed.pl: Can't open $file: $!\n";
261 s/[ \t]*#.*//; # Delete comments.
262 if (/PERLVARA?I?C?\($pre,\s*(\w+)/) {
263 die_at_end "duplicate symbol $1 while processing $file line $.\n"
268 return sort keys %seen;
271 my @intrp = readvars 'intrpvar.h','I';
272 my @globvar = readvars 'perlvars.h','G';
275 my ($from, $to, $indent) = @_;
276 $indent = '' unless defined $indent;
277 my $t = int(length("$indent$from") / 8);
278 "#${indent}define $from" . "\t" x ($t < 3 ? 3 - $t : 1) . "$to\n";
282 my ($sym,$pre,$ptr) = @_;
283 hide("PL_$sym", "($ptr$pre$sym)");
286 my $em = open_print_header('embed.h');
289 /* (Doing namespace management portably in C is really gross.) */
291 /* By defining PERL_NO_SHORT_NAMES (not done by default) the short forms
292 * (like warn instead of Perl_warn) for the API are not defined.
293 * Not defining the short forms is a good thing for cleaner embedding. */
295 #ifndef PERL_NO_SHORT_NAMES
297 /* Hide global symbols */
304 my ($guard, $funcs) = @_;
305 print $em "$guard\n" if $guard;
311 # Indent the conditionals if we are wrapped in an #if/#endif pair.
312 $cond =~ s/#(.*)/# $1/ if $guard;
317 my ($flags,$retval,$func,@args) = @$_;
318 unless ($flags =~ /[omM]/) {
319 my $args = scalar @args;
321 my $full_name = full_name($func, $flags);
322 next if $full_name eq $func; # Don't output a no-op.
323 $ret = hide($func, $full_name);
325 elsif ($args and $args[$args-1] =~ /\.\.\./) {
327 # we're out of luck for varargs functions under CPP
328 # So we can only do these macros for no implicit context:
329 $ret = "#ifndef PERL_IMPLICIT_CONTEXT\n"
330 . hide($func, full_name($func, $flags)) . "#endif\n";
334 my $alist = join(",", @az[0..$args-1]);
335 $ret = "#define $func($alist)";
336 my $t = int(length($ret) / 8);
337 $ret .= "\t" x ($t < 4 ? 4 - $t : 1);
338 $ret .= full_name($func, $flags) . "(aTHX";
339 $ret .= "_ " if $alist;
345 die "Can't use W without other args (currently)";
350 $ret = "#ifndef NO_MATHOMS\n$ret#endif\n" if $flags =~ /b/;
354 # Prune empty #if/#endif pairs.
355 while ($lines =~ s/#\s*if[^\n]+\n#\s*endif\n//) {
357 # Merge adjacent blocks.
358 while ($lines =~ s/(#ifndef PERL_IMPLICIT_CONTEXT
361 #ifndef PERL_IMPLICIT_CONTEXT
366 print $em "#endif\n" if $guard;
370 embed_h('#if defined(PERL_CORE) || defined(PERL_EXT)', $ext);
371 embed_h('#ifdef PERL_CORE', $core);
375 #endif /* #ifndef PERL_NO_SHORT_NAMES */
377 /* Compatibility stubs. Compile extensions with -DPERL_NOCOMPAT to
381 #if !defined(PERL_CORE)
382 # define sv_setptrobj(rv,ptr,name) sv_setref_iv(rv,name,PTR2IV(ptr))
383 # define sv_setptrref(rv,ptr) sv_setref_iv(rv,NULL,PTR2IV(ptr))
386 #if !defined(PERL_CORE) && !defined(PERL_NOCOMPAT)
388 /* Compatibility for various misnamed functions. All functions
389 in the API that begin with "perl_" (not "Perl_") take an explicit
390 interpreter context pointer.
391 The following are not like that, but since they had a "perl_"
392 prefix in previous versions, we provide compatibility macros.
394 # define perl_atexit(a,b) call_atexit(a,b)
398 my ($flags, $retval, $func, @args) = @$_;
400 next unless $flags =~ /O/;
402 my $alist = join ",", @az[0..$#args];
403 my $ret = "# define perl_$func($alist)";
404 my $t = (length $ret) >> 3;
405 $ret .= "\t" x ($t < 5 ? 5 - $t : 1);
406 print $em "$ret$func($alist)\n";
411 my (%has_va, %has_nocontext);
414 ++$has_va{$_->[2]} if $_->[-1] =~ /\.\.\./;
415 ++$has_nocontext{$1} if $_->[2] =~ /(.*)_nocontext/;
418 @nocontext = sort grep {
420 && !/printf/ # Not clear to me why these are skipped but they are.
426 /* varargs functions can't be handled with CPP macros. :-(
427 This provides a set of compatibility functions that don't take
428 an extra argument but grab the context pointer using the macro
431 #if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_NO_SHORT_NAMES)
434 foreach (@nocontext) {
435 print $em hide($_, "Perl_${_}_nocontext", " ");
441 #endif /* !defined(PERL_CORE) && !defined(PERL_NOCOMPAT) */
443 #if !defined(PERL_IMPLICIT_CONTEXT)
444 /* undefined symbols, point them back at the usual ones */
447 foreach (@nocontext) {
448 print $em hide("Perl_${_}_nocontext", "Perl_$_", " ");
455 read_only_bottom_close_and_rename($em) if ! $error_count;
457 $em = open_print_header('embedvar.h');
460 /* (Doing namespace management portably in C is really gross.) */
463 The following combinations of MULTIPLICITY and PERL_IMPLICIT_CONTEXT
466 2) MULTIPLICITY # supported for compatibility
467 3) MULTIPLICITY && PERL_IMPLICIT_CONTEXT
469 All other combinations of these flags are errors.
471 only #3 is supported directly, while #2 is a special
472 case of #3 (supported by redefining vTHX appropriately).
475 #if defined(MULTIPLICITY)
476 /* cases 2 and 3 above */
478 # if defined(PERL_IMPLICIT_CONTEXT)
481 # define vTHX PERL_GET_INTERP
489 if ($sym eq 'sawampersand') {
490 print $em "#ifndef PL_sawampersand\n";
492 print $em multon($sym,'I','vTHX->');
493 if ($sym eq 'sawampersand') {
494 print $em "#endif\n";
500 #endif /* MULTIPLICITY */
502 #if defined(PERL_GLOBAL_STRUCT)
506 for $sym (@globvar) {
507 print $em "#ifdef OS2\n" if $sym eq 'sh_path';
508 print $em "#ifdef __VMS\n" if $sym eq 'perllib_sep';
509 print $em multon($sym, 'G','my_vars->');
510 print $em multon("G$sym",'', 'my_vars->');
511 print $em "#endif\n" if $sym eq 'sh_path';
512 print $em "#endif\n" if $sym eq 'perllib_sep';
517 #endif /* PERL_GLOBAL_STRUCT */
520 read_only_bottom_close_and_rename($em) if ! $error_count;
522 my $capih = open_print_header('perlapi.h');
524 print $capih <<'EOT';
525 /* declare accessor functions for Perl variables */
526 #ifndef __perlapi_h__
527 #define __perlapi_h__
529 #if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
537 #define PERLVAR(p,v,t) EXTERN_C t* Perl_##p##v##_ptr(pTHX);
538 #define PERLVARA(p,v,n,t) typedef t PL_##v##_t[n]; \
539 EXTERN_C PL_##v##_t* Perl_##p##v##_ptr(pTHX);
540 #define PERLVARI(p,v,t,i) PERLVAR(p,v,t)
541 #define PERLVARIC(p,v,t,i) PERLVAR(p,v, const t)
543 #include "perlvars.h"
552 #if defined(PERL_CORE)
554 /* accessor functions for Perl "global" variables */
556 /* these need to be mentioned here, or most linkers won't put them in
557 the perl executable */
559 #ifndef PERL_NO_FORCE_LINK
564 EXTCONST void * const PL_force_link_funcs[];
566 EXTCONST void * const PL_force_link_funcs[] = {
571 #define PERLVAR(p,v,t) (void*)Perl_##p##v##_ptr,
572 #define PERLVARA(p,v,n,t) PERLVAR(p,v,t)
573 #define PERLVARI(p,v,t,i) PERLVAR(p,v,t)
574 #define PERLVARIC(p,v,t,i) PERLVAR(p,v,t)
576 /* In Tru64 (__DEC && __osf__) the cc option -std1 causes that one
577 * cannot cast between void pointers and function pointers without
578 * info level warnings. The PL_force_link_funcs[] would cause a few
579 * hundred of those warnings. In code one can circumnavigate this by using
580 * unions that overlay the different pointers, but in declarations one
581 * cannot use this trick. Therefore we just disable the warning here
582 * for the duration of the PL_force_link_funcs[] declaration. */
584 #if defined(__DECC) && defined(__osf__)
586 #pragma message disable (nonstandcast)
589 #include "perlvars.h"
591 #if defined(__DECC) && defined(__osf__)
592 #pragma message restore
604 #endif /* PERL_NO_FORCE_LINK */
606 #else /* !PERL_CORE */
610 foreach $sym (@globvar) {
612 "#undef PL_$sym\n" . hide("PL_$sym", "(*Perl_G${sym}_ptr(NULL))");
615 print $capih <<'EOT';
617 #endif /* !PERL_CORE */
618 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
620 #endif /* __perlapi_h__ */
623 read_only_bottom_close_and_rename($capih) if ! $error_count;
625 my $capi = open_print_header('perlapi.c', <<'EOQ');
628 * Up to the threshold of the door there mounted a flight of twenty-seven
629 * broad stairs, hewn by some unknown art of the same black stone. This
630 * was the only entrance to the tower; ...
632 * [p.577 of _The Lord of the Rings_, III/x: "The Voice of Saruman"]
642 #if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
644 /* accessor functions for Perl "global" variables */
648 #define PERLVARI(p,v,t,i) PERLVAR(p,v,t)
652 #define PERLVAR(p,v,t) t* Perl_##p##v##_ptr(pTHX) \
653 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
654 #define PERLVARA(p,v,n,t) PL_##v##_t* Perl_##p##v##_ptr(pTHX) \
655 { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
657 #define PERLVARIC(p,v,t,i) \
658 const t* Perl_##p##v##_ptr(pTHX) \
659 { PERL_UNUSED_CONTEXT; return (const t *)&(PL_##v); }
660 #include "perlvars.h"
669 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
672 read_only_bottom_close_and_rename($capi) if ! $error_count;
674 die "$error_count errors found" if $error_count;
676 # ex: set ts=8 sts=4 sw=4 noet: