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