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