This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldelta for 65169990
[perl5.git] / regen / embed.pl
1 #!/usr/bin/perl -w
2
3 # Regenerate (overwriting only if changed):
4 #
5 #    embed.h
6 #    embedvar.h
7 #    perlapi.c
8 #    perlapi.h
9 #    proto.h
10 #
11 # from information stored in
12 #
13 #    embed.fnc
14 #    intrpvar.h
15 #    perlvars.h
16 #    regen/opcodes
17 #
18 # Accepts the standard regen_lib -q and -v args.
19 #
20 # This script is normally invoked from regen.pl.
21
22 require 5.004;  # keep this compatible, an old perl is all we may have before
23                 # we build the new one
24
25 use strict;
26
27 BEGIN {
28     # Get function prototypes
29     require 'regen/regen_lib.pl';
30     require 'regen/embed_lib.pl';
31 }
32
33 my $SPLINT = 0; # Turn true for experimental splint support http://www.splint.org
34 my $unflagged_pointers;
35
36 #
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.
41 #
42
43 sub full_name ($$) { # Returns the function name with potentially the
44                      # prefixes 'S_' or 'Perl_'
45     my ($func, $flags) = @_;
46
47     return "Perl_$func" if $flags =~ /p/;
48     return "S_$func" if $flags =~ /[si]/;
49     return $func;
50 }
51
52 sub open_print_header {
53     my ($file, $quote) = @_;
54
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 });
61 }
62
63 my ($embed, $core, $ext, $api) = setup_embed();
64
65 # generate proto.h
66 {
67     my $pr = open_print_header("proto.h");
68     print $pr "START_EXTERN_C\n";
69     my $ret;
70
71     foreach (@$embed) {
72         if (@$_ == 1) {
73             print $pr "$_->[0]\n";
74             next;
75         }
76
77         my ($flags,$retval,$plain_func,@args) = @$_;
78         if ($flags =~ / ( [^AabDdEfiMmnOoPpRrsUXx] ) /x) {
79             warn "flag $1 is not legal (for function $plain_func)";
80         }
81         my @nonnull;
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;
88         my @names_of_nn;
89         my $func;
90
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";
93         }
94
95         warn "$plain_func: s flag is mutually exclusive from the i and p plags"
96                                             if $flags =~ /s/ && $flags =~ /[ip]/;
97         my $splint_flags = "";
98         if ( $SPLINT && !$commented_out ) {
99             $splint_flags .= '/*@noreturn@*/ ' if $never_returns;
100             if ($can_ignore && ($retval ne 'void') && ($retval !~ /\*/)) {
101                 $retval .= " /*\@alt void\@*/";
102             }
103         }
104
105         if ($flags =~ /([si])/) {
106             my $type;
107             if ($never_returns) {
108                 $type = $1 eq 's' ? "PERL_STATIC_NO_RET" : "PERL_STATIC_INLINE_NO_RET";
109             }
110             else {
111                 $type = $1 eq 's' ? "STATIC" : "PERL_STATIC_INLINE";
112             }
113             $retval = "$type $splint_flags$retval";
114         }
115         else {
116             if ($never_returns) {
117                 $retval = "PERL_CALLCONV_NO_RET $splint_flags$retval";
118             }
119             else {
120                 $retval = "PERL_CALLCONV $splint_flags$retval";
121             }
122         }
123         $func = full_name($plain_func, $flags);
124         $ret = "";
125         $ret .= "#ifndef NO_MATHOMS\n" if $binarycompat;
126         $ret .= "$retval\t$func(";
127         if ( $has_context ) {
128             $ret .= @args ? "pTHX_ " : "pTHX";
129         }
130         if (@args) {
131             my $n;
132             for my $arg ( @args ) {
133                 ++$n;
134                 if ( $arg =~ /\*/ && $arg !~ /\b(NN|NULLOK)\b/ ) {
135                     warn "$func: $arg needs NN or NULLOK\n";
136                     ++$unflagged_pointers;
137                 }
138                 my $nn = ( $arg =~ s/\s*\bNN\b\s+// );
139                 push( @nonnull, $n ) if $nn;
140
141                 my $nullok = ( $arg =~ s/\s*\bNULLOK\b\s+// ); # strip NULLOK with no effect
142
143                 # Make sure each arg has at least a type and a var name.
144                 # An arg of "int" is valid C, but want it to be "int foo".
145                 my $temp_arg = $arg;
146                 $temp_arg =~ s/\*//g;
147                 $temp_arg =~ s/\s*\bstruct\b\s*/ /g;
148                 if ( ($temp_arg ne "...")
149                      && ($temp_arg !~ /\w+\s+(\w+)(?:\[\d+\])?\s*$/) ) {
150                     warn "$func: $arg ($n) doesn't have a name\n";
151                 }
152                 if ( $SPLINT && $nullok && !$commented_out ) {
153                     $arg = '/*@null@*/ ' . $arg;
154                 }
155                 if (defined $1 && $nn && !($commented_out && !$binarycompat)) {
156                     push @names_of_nn, $1;
157                 }
158             }
159             $ret .= join ", ", @args;
160         }
161         else {
162             $ret .= "void" if !$has_context;
163         }
164         $ret .= ")";
165         my @attrs;
166         if ( $flags =~ /r/ ) {
167             push @attrs, "__attribute__noreturn__";
168         }
169         if ( $flags =~ /D/ ) {
170             push @attrs, "__attribute__deprecated__";
171         }
172         if ( $is_malloc ) {
173             push @attrs, "__attribute__malloc__";
174         }
175         if ( !$can_ignore ) {
176             push @attrs, "__attribute__warn_unused_result__";
177         }
178         if ( $flags =~ /P/ ) {
179             push @attrs, "__attribute__pure__";
180         }
181         if( $flags =~ /f/ ) {
182             my $prefix  = $has_context ? 'pTHX_' : '';
183             my ($args, $pat);
184             if ($args[-1] eq '...') {
185                 $args   = scalar @args;
186                 $pat    = $args - 1;
187                 $args   = $prefix . $args;
188             }
189             else {
190                 # don't check args, and guess which arg is the pattern
191                 # (one of 'fmt', 'pat', 'f'),
192                 $args = 0;
193                 my @fmts = grep $args[$_] =~ /\b(f|pat|fmt)$/, 0..$#args;
194                 if (@fmts != 1) {
195                     die "embed.pl: '$plain_func': can't determine pattern arg\n";
196                 }
197                 $pat = $fmts[0] + 1;
198             }
199             my $macro   = grep($_ == $pat, @nonnull)
200                                 ? '__attribute__format__'
201                                 : '__attribute__format__null_ok__';
202             if ($plain_func =~ /strftime/) {
203                 push @attrs, sprintf "%s(__strftime__,%s1,0)", $macro, $prefix;
204             }
205             else {
206                 push @attrs, sprintf "%s(__printf__,%s%d,%s)", $macro,
207                                     $prefix, $pat, $args;
208             }
209         }
210         if ( @attrs ) {
211             $ret .= "\n";
212             $ret .= join( "\n", map { "\t\t\t$_" } @attrs );
213         }
214         $ret .= ";";
215         $ret = "/* $ret */" if $commented_out;
216         if (@names_of_nn) {
217             $ret .= "\n#define PERL_ARGS_ASSERT_\U$plain_func\E\t\\\n\t"
218                 . join '; ', map "assert($_)", @names_of_nn;
219         }
220         $ret .= "\n#endif" if $binarycompat;
221         $ret .= @attrs ? "\n\n" : "\n";
222
223         print $pr $ret;
224     }
225
226     print $pr <<'EOF';
227 #ifdef PERL_CORE
228 #  include "pp_proto.h"
229 #endif
230 END_EXTERN_C
231 EOF
232
233     read_only_bottom_close_and_rename($pr);
234 }
235
236 warn "$unflagged_pointers pointer arguments to clean up\n" if $unflagged_pointers;
237
238 sub readvars {
239     my ($file, $pre) = @_;
240     local (*FILE, $_);
241     my %seen;
242     open(FILE, "< $file")
243         or die "embed.pl: Can't open $file: $!\n";
244     while (<FILE>) {
245         s/[ \t]*#.*//;          # Delete comments.
246         if (/PERLVARA?I?C?\($pre,\s*(\w+)/) {
247             warn "duplicate symbol $1 while processing $file line $.\n"
248                 if $seen{$1}++;
249         }
250     }
251     close(FILE);
252     return sort keys %seen;
253 }
254
255 my @intrp = readvars 'intrpvar.h','I';
256 my @globvar = readvars 'perlvars.h','G';
257
258 sub hide {
259     my ($from, $to, $indent) = @_;
260     $indent = '' unless defined $indent;
261     my $t = int(length("$indent$from") / 8);
262     "#${indent}define $from" . "\t" x ($t < 3 ? 3 - $t : 1) . "$to\n";
263 }
264
265 sub multon ($$$) {
266     my ($sym,$pre,$ptr) = @_;
267     hide("PL_$sym", "($ptr$pre$sym)");
268 }
269
270 my $em = open_print_header('embed.h');
271
272 print $em <<'END';
273 /* (Doing namespace management portably in C is really gross.) */
274
275 /* By defining PERL_NO_SHORT_NAMES (not done by default) the short forms
276  * (like warn instead of Perl_warn) for the API are not defined.
277  * Not defining the short forms is a good thing for cleaner embedding. */
278
279 #ifndef PERL_NO_SHORT_NAMES
280
281 /* Hide global symbols */
282
283 END
284
285 my @az = ('a'..'z');
286
287 sub embed_h {
288     my ($guard, $funcs) = @_;
289     print $em "$guard\n" if $guard;
290
291     my $lines;
292     foreach (@$funcs) {
293         if (@$_ == 1) {
294             my $cond = $_->[0];
295             # Indent the conditionals if we are wrapped in an #if/#endif pair.
296             $cond =~ s/#(.*)/#  $1/ if $guard;
297             $lines .= "$cond\n";
298             next;
299         }
300         my $ret = "";
301         my ($flags,$retval,$func,@args) = @$_;
302         unless ($flags =~ /[om]/) {
303             my $args = scalar @args;
304             if ($flags =~ /n/) {
305                 my $full_name = full_name($func, $flags);
306                 next if $full_name eq $func;    # Don't output a no-op.
307                 $ret = hide($func, $full_name);
308             }
309             elsif ($args and $args[$args-1] =~ /\.\.\./) {
310                 if ($flags =~ /p/) {
311                     # we're out of luck for varargs functions under CPP
312                     # So we can only do these macros for no implicit context:
313                     $ret = "#ifndef PERL_IMPLICIT_CONTEXT\n"
314                         . hide($func, full_name($func, $flags)) . "#endif\n";
315                 }
316             }
317             else {
318                 my $alist = join(",", @az[0..$args-1]);
319                 $ret = "#define $func($alist)";
320                 my $t = int(length($ret) / 8);
321                 $ret .=  "\t" x ($t < 4 ? 4 - $t : 1);
322                 $ret .= full_name($func, $flags) . "(aTHX";
323                 $ret .= "_ " if $alist;
324                 $ret .= $alist . ")\n";
325             }
326             $ret = "#ifndef NO_MATHOMS\n$ret#endif\n" if $flags =~ /b/;
327         }
328         $lines .= $ret;
329     }
330     # Prune empty #if/#endif pairs.
331     while ($lines =~ s/#\s*if[^\n]+\n#\s*endif\n//) {
332     }
333     # Merge adjacent blocks.
334     while ($lines =~ s/(#ifndef PERL_IMPLICIT_CONTEXT
335 [^\n]+
336 )#endif
337 #ifndef PERL_IMPLICIT_CONTEXT
338 /$1/) {
339     }
340
341     print $em $lines;
342     print $em "#endif\n" if $guard;
343 }
344
345 embed_h('', $api);
346 embed_h('#if defined(PERL_CORE) || defined(PERL_EXT)', $ext);
347 embed_h('#ifdef PERL_CORE', $core);
348
349 print $em <<'END';
350
351 #endif  /* #ifndef PERL_NO_SHORT_NAMES */
352
353 /* Compatibility stubs.  Compile extensions with -DPERL_NOCOMPAT to
354    disable them.
355  */
356
357 #if !defined(PERL_CORE)
358 #  define sv_setptrobj(rv,ptr,name)     sv_setref_iv(rv,name,PTR2IV(ptr))
359 #  define sv_setptrref(rv,ptr)          sv_setref_iv(rv,NULL,PTR2IV(ptr))
360 #endif
361
362 #if !defined(PERL_CORE) && !defined(PERL_NOCOMPAT)
363
364 /* Compatibility for various misnamed functions.  All functions
365    in the API that begin with "perl_" (not "Perl_") take an explicit
366    interpreter context pointer.
367    The following are not like that, but since they had a "perl_"
368    prefix in previous versions, we provide compatibility macros.
369  */
370 #  define perl_atexit(a,b)              call_atexit(a,b)
371 END
372
373 foreach (@$embed) {
374     my ($flags, $retval, $func, @args) = @$_;
375     next unless $func;
376     next unless $flags =~ /O/;
377
378     my $alist = join ",", @az[0..$#args];
379     my $ret = "#  define perl_$func($alist)";
380     my $t = (length $ret) >> 3;
381     $ret .=  "\t" x ($t < 5 ? 5 - $t : 1);
382     print $em "$ret$func($alist)\n";
383 }
384
385 my @nocontext;
386 {
387     my (%has_va, %has_nocontext);
388     foreach (@$embed) {
389         next unless @$_ > 1;
390         ++$has_va{$_->[2]} if $_->[-1] =~ /\.\.\./;
391         ++$has_nocontext{$1} if $_->[2] =~ /(.*)_nocontext/;
392     }
393
394     @nocontext = sort grep {
395         $has_nocontext{$_}
396             && !/printf/ # Not clear to me why these are skipped but they are.
397     } keys %has_va;
398 }
399
400 print $em <<'END';
401
402 /* varargs functions can't be handled with CPP macros. :-(
403    This provides a set of compatibility functions that don't take
404    an extra argument but grab the context pointer using the macro
405    dTHX.
406  */
407 #if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_NO_SHORT_NAMES)
408 END
409
410 foreach (@nocontext) {
411     print $em hide($_, "Perl_${_}_nocontext", "  ");
412 }
413
414 print $em <<'END';
415 #endif
416
417 #endif /* !defined(PERL_CORE) && !defined(PERL_NOCOMPAT) */
418
419 #if !defined(PERL_IMPLICIT_CONTEXT)
420 /* undefined symbols, point them back at the usual ones */
421 END
422
423 foreach (@nocontext) {
424     print $em hide("Perl_${_}_nocontext", "Perl_$_", "  ");
425 }
426
427 print $em <<'END';
428 #endif
429 END
430
431 read_only_bottom_close_and_rename($em);
432
433 $em = open_print_header('embedvar.h');
434
435 print $em <<'END';
436 /* (Doing namespace management portably in C is really gross.) */
437
438 /*
439    The following combinations of MULTIPLICITY and PERL_IMPLICIT_CONTEXT
440    are supported:
441      1) none
442      2) MULTIPLICITY    # supported for compatibility
443      3) MULTIPLICITY && PERL_IMPLICIT_CONTEXT
444
445    All other combinations of these flags are errors.
446
447    only #3 is supported directly, while #2 is a special
448    case of #3 (supported by redefining vTHX appropriately).
449 */
450
451 #if defined(MULTIPLICITY)
452 /* cases 2 and 3 above */
453
454 #  if defined(PERL_IMPLICIT_CONTEXT)
455 #    define vTHX        aTHX
456 #  else
457 #    define vTHX        PERL_GET_INTERP
458 #  endif
459
460 END
461
462 my $sym;
463
464 for $sym (@intrp) {
465     if ($sym eq 'sawampersand') {
466         print $em "#ifndef PL_sawampersand\n";
467     }
468     print $em multon($sym,'I','vTHX->');
469     if ($sym eq 'sawampersand') {
470         print $em "#endif\n";
471     }
472 }
473
474 print $em <<'END';
475
476 #endif  /* MULTIPLICITY */
477
478 #if defined(PERL_GLOBAL_STRUCT)
479
480 END
481
482 for $sym (@globvar) {
483     print $em "#ifdef OS2\n" if $sym eq 'sh_path';
484     print $em "#ifdef __VMS\n" if $sym eq 'perllib_sep';
485     print $em multon($sym,   'G','my_vars->');
486     print $em multon("G$sym",'', 'my_vars->');
487     print $em "#endif\n" if $sym eq 'sh_path';
488     print $em "#endif\n" if $sym eq 'perllib_sep';
489 }
490
491 print $em <<'END';
492
493 #endif /* PERL_GLOBAL_STRUCT */
494 END
495
496 read_only_bottom_close_and_rename($em);
497
498 my $capih = open_print_header('perlapi.h');
499
500 print $capih <<'EOT';
501 /* declare accessor functions for Perl variables */
502 #ifndef __perlapi_h__
503 #define __perlapi_h__
504
505 #if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
506
507 START_EXTERN_C
508
509 #undef PERLVAR
510 #undef PERLVARA
511 #undef PERLVARI
512 #undef PERLVARIC
513 #define PERLVAR(p,v,t)  EXTERN_C t* Perl_##p##v##_ptr(pTHX);
514 #define PERLVARA(p,v,n,t)       typedef t PL_##v##_t[n];                \
515                         EXTERN_C PL_##v##_t* Perl_##p##v##_ptr(pTHX);
516 #define PERLVARI(p,v,t,i)       PERLVAR(p,v,t)
517 #define PERLVARIC(p,v,t,i) PERLVAR(p,v, const t)
518
519 #include "perlvars.h"
520
521 #undef PERLVAR
522 #undef PERLVARA
523 #undef PERLVARI
524 #undef PERLVARIC
525
526 END_EXTERN_C
527
528 #if defined(PERL_CORE)
529
530 /* accessor functions for Perl "global" variables */
531
532 /* these need to be mentioned here, or most linkers won't put them in
533    the perl executable */
534
535 #ifndef PERL_NO_FORCE_LINK
536
537 START_EXTERN_C
538
539 #ifndef DOINIT
540 EXTCONST void * const PL_force_link_funcs[];
541 #else
542 EXTCONST void * const PL_force_link_funcs[] = {
543 #undef PERLVAR
544 #undef PERLVARA
545 #undef PERLVARI
546 #undef PERLVARIC
547 #define PERLVAR(p,v,t)          (void*)Perl_##p##v##_ptr,
548 #define PERLVARA(p,v,n,t)       PERLVAR(p,v,t)
549 #define PERLVARI(p,v,t,i)       PERLVAR(p,v,t)
550 #define PERLVARIC(p,v,t,i)      PERLVAR(p,v,t)
551
552 /* In Tru64 (__DEC && __osf__) the cc option -std1 causes that one
553  * cannot cast between void pointers and function pointers without
554  * info level warnings.  The PL_force_link_funcs[] would cause a few
555  * hundred of those warnings.  In code one can circumnavigate this by using
556  * unions that overlay the different pointers, but in declarations one
557  * cannot use this trick.  Therefore we just disable the warning here
558  * for the duration of the PL_force_link_funcs[] declaration. */
559
560 #if defined(__DECC) && defined(__osf__)
561 #pragma message save
562 #pragma message disable (nonstandcast)
563 #endif
564
565 #include "perlvars.h"
566
567 #if defined(__DECC) && defined(__osf__)
568 #pragma message restore
569 #endif
570
571 #undef PERLVAR
572 #undef PERLVARA
573 #undef PERLVARI
574 #undef PERLVARIC
575 };
576 #endif  /* DOINIT */
577
578 END_EXTERN_C
579
580 #endif  /* PERL_NO_FORCE_LINK */
581
582 #else   /* !PERL_CORE */
583
584 EOT
585
586 foreach $sym (@globvar) {
587     print $capih
588         "#undef  PL_$sym\n" . hide("PL_$sym", "(*Perl_G${sym}_ptr(NULL))");
589 }
590
591 print $capih <<'EOT';
592
593 #endif /* !PERL_CORE */
594 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
595
596 #endif /* __perlapi_h__ */
597 EOT
598
599 read_only_bottom_close_and_rename($capih);
600
601 my $capi = open_print_header('perlapi.c', <<'EOQ');
602  *
603  *
604  * Up to the threshold of the door there mounted a flight of twenty-seven
605  * broad stairs, hewn by some unknown art of the same black stone.  This
606  * was the only entrance to the tower; ...
607  *
608  *     [p.577 of _The Lord of the Rings_, III/x: "The Voice of Saruman"]
609  *
610  */
611 EOQ
612
613 print $capi <<'EOT';
614 #include "EXTERN.h"
615 #include "perl.h"
616 #include "perlapi.h"
617
618 #if defined (MULTIPLICITY) && defined (PERL_GLOBAL_STRUCT)
619
620 /* accessor functions for Perl "global" variables */
621 START_EXTERN_C
622
623 #undef PERLVARI
624 #define PERLVARI(p,v,t,i) PERLVAR(p,v,t)
625
626 #undef PERLVAR
627 #undef PERLVARA
628 #define PERLVAR(p,v,t)          t* Perl_##p##v##_ptr(pTHX)              \
629                         { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
630 #define PERLVARA(p,v,n,t)       PL_##v##_t* Perl_##p##v##_ptr(pTHX)     \
631                         { dVAR; PERL_UNUSED_CONTEXT; return &(PL_##v); }
632 #undef PERLVARIC
633 #define PERLVARIC(p,v,t,i)      \
634                         const t* Perl_##p##v##_ptr(pTHX)                \
635                         { PERL_UNUSED_CONTEXT; return (const t *)&(PL_##v); }
636 #include "perlvars.h"
637
638 #undef PERLVAR
639 #undef PERLVARA
640 #undef PERLVARI
641 #undef PERLVARIC
642
643 END_EXTERN_C
644
645 #endif /* MULTIPLICITY && PERL_GLOBAL_STRUCT */
646 EOT
647
648 read_only_bottom_close_and_rename($capi);
649
650 # ex: set ts=8 sts=4 sw=4 noet: