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