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