fix memory leak in C<eval 'return sub {...}'>
[p5sagit/p5-mst-13.2.git] / embed.pl
1 #!/usr/bin/perl -w
2
3 require 5.003;
4
5 # XXX others that may need adding
6 #       warnhook
7 #       hints
8 #       copline
9 my @extvars = qw(sv_undef sv_yes sv_no na dowarn
10                  curcop compiling 
11                  tainting tainted stack_base stack_sp sv_arenaroot
12                  no_modify
13                  curstash DBsub DBsingle debstash
14                  rsfp 
15                  stdingv
16                  defgv
17                  errgv
18                  rsfp_filters
19                  perldb
20                  diehook
21                  dirty
22                  perl_destruct_level
23                 );
24
25 sub readsyms (\%$) {
26     my ($syms, $file) = @_;
27     local (*FILE, $_);
28     open(FILE, "< $file")
29         or die "embed.pl: Can't open $file: $!\n";
30     while (<FILE>) {
31         s/[ \t]*#.*//;          # Delete comments.
32         if (/^\s*(\S+)\s*$/) {
33             my $sym = $1;
34             warn "duplicate symbol $sym while processing $file\n"
35                 if exists $$syms{$sym};
36             $$syms{$sym} = 1;
37         }
38     }
39     close(FILE);
40 }
41
42 readsyms %global, 'global.sym';
43 readsyms %global, 'pp.sym';
44
45 sub readvars(\%$$@) {
46     my ($syms, $file,$pre,$keep_pre) = @_;
47     local (*FILE, $_);
48     open(FILE, "< $file")
49         or die "embed.pl: Can't open $file: $!\n";
50     while (<FILE>) {
51         s/[ \t]*#.*//;          # Delete comments.
52         if (/PERLVARI?C?\($pre(\w+)/) {
53             my $sym = $1;
54             $sym = $pre . $sym if $keep_pre;
55             warn "duplicate symbol $sym while processing $file\n"
56                 if exists $$syms{$sym};
57             $$syms{$sym} = 1;
58         }
59     }
60     close(FILE);
61 }
62
63 my %intrp;
64 my %thread;
65
66 readvars %intrp,  'intrpvar.h','I';
67 readvars %thread, 'thrdvar.h','T';
68 readvars %globvar, 'perlvars.h','G';
69 readvars %objvar, 'intrpvar.h','pi', 1;
70
71 foreach my $sym (sort keys %intrp)
72  {
73   if (exists $global{$sym})
74    {
75     delete $global{$sym};
76     warn "$sym in {global,pp}.sym as well as intrpvar.h\n";
77    }
78  }
79
80 foreach my $sym (sort keys %globvar)
81  {
82   if (exists $global{$sym})
83    {
84     delete $global{$sym};
85     warn "$sym in {global,pp}.sym as well as perlvars.h\n";
86    }
87  }
88
89 foreach my $sym (sort keys %thread)
90  {
91   warn "$sym in intrpvar.h as well as thrdvar.h\n" if exists $intrp{$sym};
92   if (exists $global{$sym})
93    {
94     delete $global{$sym};
95     warn "$sym in {global,pp}.sym as well as thrdvar.h\n";
96    }
97  }
98
99 sub undefine ($) {
100     my ($sym) = @_;
101     "#undef  $sym\n";
102 }
103
104 sub hide ($$) {
105     my ($from, $to) = @_;
106     my $t = int(length($from) / 8);
107     "#define $from" . "\t" x ($t < 3 ? 3 - $t : 1) . "$to\n";
108 }
109
110 sub embed ($) {
111     my ($sym) = @_;
112     hide($sym, "Perl_$sym");
113 }
114
115 sub embedobj ($) {
116     my ($sym) = @_;
117     hide($sym, $sym =~ /^perl_/i ? "CPerlObj::$sym" : "CPerlObj::Perl_$sym");
118 }
119
120 sub objxsub_func ($) {
121     my ($sym) = @_;
122     undefine($sym) . hide($sym, $sym =~ /^perl_/i
123                                 ? "pPerl->$sym"
124                                 : "pPerl->Perl_$sym");
125 }
126
127 sub objxsub_var ($) {
128     my ($sym) = @_;
129     undefine("PL_$sym") . hide("PL_$sym", "pPerl->PL_$sym");
130 }
131
132 sub embedvar ($) {
133     my ($sym) = @_;
134 #   hide($sym, "Perl_$sym");
135     return '';
136 }
137
138 sub multon ($$$) {
139     my ($sym,$pre,$ptr) = @_;
140     hide("PL_$sym", "($ptr$pre$sym)");
141 }
142 sub multoff ($$) {
143     my ($sym,$pre) = @_;
144     return hide("PL_$pre$sym", "PL_$sym");
145 }
146
147 unlink 'embed.h';
148 open(EM, '> embed.h')
149     or die "Can't create embed.h: $!\n";
150
151 print EM <<'END';
152 /* !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!! 
153    This file is built by embed.pl from global.sym, pp.sym, intrpvar.h,
154    perlvars.h and thrdvar.h.  Any changes made here will be lost!
155 */
156
157 /* (Doing namespace management portably in C is really gross.) */
158
159 /* NO_EMBED is no longer supported. i.e. EMBED is always active. */
160
161 /* Hide global symbols */
162
163 #if !defined(PERL_OBJECT)
164
165 END
166
167 for $sym (sort keys %global) {
168     print EM embed($sym);
169 }
170
171 print EM <<'END';
172
173 #else   /* PERL_OBJECT */
174
175 END
176
177 # XXX these should be in a *.sym file
178 my @staticfuncs = qw(
179     perl_init_i18nl10n
180     perl_init_i18nl14n
181     perl_new_collate
182     perl_new_ctype
183     perl_new_numeric
184     perl_set_numeric_local
185     perl_set_numeric_standard
186     perl_construct
187     perl_destruct
188     perl_atexit
189     perl_free
190     perl_parse
191     perl_run
192     perl_get_sv
193     perl_get_av
194     perl_get_hv
195     perl_get_cv
196     perl_call_argv
197     perl_call_pv
198     perl_call_method
199     perl_call_sv
200     perl_eval_pv
201     perl_eval_sv
202     perl_require_pv
203
204     hsplit
205     hfreeentries
206     more_he
207     new_he
208     del_he
209     save_hek
210     mess_alloc
211     gv_init_sv
212     save_scalar_at
213     asIV
214     asUV
215     more_sv
216     more_xiv
217     more_xnv
218     more_xpv
219     more_xrv
220     new_xiv
221     new_xnv
222     new_xpv
223     new_xrv
224     del_xiv
225     del_xnv
226     del_xpv
227     del_xrv
228     sv_unglob
229     avhv_index_sv
230     do_report_used
231     do_clean_objs
232     do_clean_named_objs
233     do_clean_all
234     not_a_number
235     my_safemalloc
236     visit
237     qsortsv
238     sortcv
239     save_magic
240     magic_methpack
241     magic_methcall
242     magic_methcall
243     doform
244     doencodes
245     refto
246     seed
247     docatch
248     docatch_body
249     perl_parse_body
250     perl_run_body
251     perl_call_body
252     perl_call_xbody
253     call_list_body
254     dofindlabel
255     doparseform
256     dopoptoeval
257     dopoptolabel
258     dopoptoloop
259     dopoptosub
260     dopoptosub_at
261     free_closures
262     save_lines
263     doeval
264     doopen_pmc
265     sv_ncmp
266     sv_i_ncmp
267     amagic_ncmp
268     amagic_i_ncmp
269     amagic_cmp
270     amagic_cmp_locale
271     mul128
272     is_an_int
273     div128
274     runops_standard
275     runops_debug
276     check_uni
277     force_next
278     force_version
279     force_word
280     tokeq
281     scan_const
282     scan_formline
283     scan_heredoc
284     scan_ident
285     scan_inputsymbol
286     scan_pat
287     scan_str
288     scan_subst
289     scan_trans
290     scan_word
291     skipspace
292     checkcomma
293     force_ident
294     incline
295     intuit_method
296     intuit_more
297     lop
298     missingterm
299     no_op
300     set_csh
301     sublex_done
302     sublex_push
303     sublex_start
304     uni
305     filter_gets
306     new_constant
307     ao
308     depcom
309     win32_textfilter
310     incl_perldb
311     isa_lookup
312     get_db_sub
313     list_assignment
314     bad_type
315     modkids
316     no_fh_allowed
317     no_bareword_allowed
318     scalarboolean
319     too_few_arguments
320     too_many_arguments
321     null
322     pad_findlex
323     newDEFSVOP
324     gv_ename
325     cv_clone2
326     find_beginning
327     forbid_setid
328     incpush
329     init_interp
330     init_ids
331     init_debugger
332     init_lexer
333     init_main_stash
334     init_perllib
335     init_postdump_symbols
336     init_predump_symbols
337     my_exit_jump
338     nuke_stacks
339     open_script
340     usage
341     validate_suid
342     emulate_eaccess
343     reg
344     reganode
345     regatom
346     regbranch
347     regc
348     reguni
349     regclass
350     regclassutf8
351     regcurly
352     reg_node
353     regpiece
354     reginsert
355     regoptail
356     regset
357     regtail
358     regwhite
359     nextchar
360     dumpuntil
361     scan_commit
362     study_chunk
363     add_data
364     re_croak2
365     regpposixcc
366     clear_re
367     regmatch
368     regrepeat
369     regrepeat_hard
370     regtry
371     reginclass
372     reginclassutf8
373     regcppush
374     regcppop
375     regcp_set_to
376     cache_re
377     restore_pos
378     reghop
379     reghopmaybe
380     dump
381     do_aspawn
382     debprof
383     new_logop
384     simplify_sort
385     is_handle_constructor
386     sv_add_backref
387     sv_del_backref
388     do_trans_CC_simple
389     do_trans_CC_count
390     do_trans_CC_complex
391     do_trans_UU_simple
392     do_trans_UU_count
393     do_trans_UU_complex
394     do_trans_UC_simple
395     do_trans_CU_simple
396     do_trans_UC_trivial
397     do_trans_CU_trivial
398     unwind_handler_stack
399     restore_magic
400     restore_rsfp
401     restore_expect
402     restore_lex_expect
403     yydestruct
404     del_sv
405     fprintf
406 );
407
408 for $sym (sort(keys(%global),@staticfuncs)) {
409     print EM embedobj($sym);
410 }
411
412 print EM <<'END';
413
414 #endif  /* PERL_OBJECT */
415
416 /* compatibility stubs */
417
418 #define sv_setptrobj(rv,ptr,name)       sv_setref_iv(rv,name,(IV)ptr)
419 #define sv_setptrref(rv,ptr)            sv_setref_iv(rv,Nullch,(IV)ptr)
420
421 END
422
423 close(EM);
424
425 unlink 'embedvar.h';
426 open(EM, '> embedvar.h')
427     or die "Can't create embedvar.h: $!\n";
428
429 print EM <<'END';
430 /* !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!! 
431    This file is built by embed.pl from global.sym, pp.sym, intrpvar.h,
432    perlvars.h and thrdvar.h.  Any changes made here will be lost!
433 */
434
435 /* (Doing namespace management portably in C is really gross.) */
436
437 /* Put interpreter-specific symbols into a struct? */
438
439 #ifdef MULTIPLICITY
440
441 #ifndef USE_THREADS
442 /* If we do not have threads then per-thread vars are per-interpreter */
443
444 END
445
446 for $sym (sort keys %thread) {
447     print EM multon($sym,'T','PL_curinterp->');
448 }
449
450 print EM <<'END';
451
452 #endif /* !USE_THREADS */
453
454 /* These are always per-interpreter if there is more than one */
455
456 END
457
458 for $sym (sort keys %intrp) {
459     print EM multon($sym,'I','PL_curinterp->');
460 }
461
462 print EM <<'END';
463
464 #else   /* !MULTIPLICITY */
465
466 END
467
468 for $sym (sort keys %intrp) {
469     print EM multoff($sym,'I');
470 }
471
472 print EM <<'END';
473
474 #ifndef USE_THREADS
475
476 END
477
478 for $sym (sort keys %thread) {
479     print EM multoff($sym,'T');
480 }
481
482 print EM <<'END';
483
484 #endif /* USE_THREADS */
485
486 /* Hide what would have been interpreter-specific symbols? */
487
488 END
489
490 for $sym (sort keys %intrp) {
491     print EM embedvar($sym);
492 }
493
494 print EM <<'END';
495
496 #ifndef USE_THREADS
497
498 END
499
500 for $sym (sort keys %thread) {
501     print EM embedvar($sym);
502 }
503
504 print EM <<'END';
505
506 #endif /* USE_THREADS */
507 #endif /* MULTIPLICITY */
508
509 /* Now same trickey for per-thread variables */
510
511 #ifdef USE_THREADS
512
513 END
514
515 for $sym (sort keys %thread) {
516     print EM multon($sym,'T','thr->');
517 }
518
519 print EM <<'END';
520
521 #endif /* USE_THREADS */
522
523 #ifdef PERL_GLOBAL_STRUCT
524
525 END
526
527 for $sym (sort keys %globvar) {
528     print EM multon($sym,'G','PL_Vars.');
529 }
530
531 print EM <<'END';
532
533 #else /* !PERL_GLOBAL_STRUCT */
534
535 END
536
537 for $sym (sort keys %globvar) {
538     print EM multoff($sym,'G');
539 }
540
541 print EM <<'END';
542
543 END
544
545 for $sym (sort keys %globvar) {
546     print EM embedvar($sym);
547 }
548
549 print EM <<'END';
550
551 #endif /* PERL_GLOBAL_STRUCT */
552
553 END
554
555 print EM <<'END';
556
557 #ifdef PERL_POLLUTE             /* disabled by default in 5.006 */
558
559 END
560
561 for $sym (sort @extvars) {
562     print EM hide($sym,"PL_$sym");
563 }
564
565 print EM <<'END';
566
567 #endif /* PERL_POLLUTE */
568 END
569
570
571 close(EM);
572
573 unlink 'objXSUB.h';
574 open(OBX, '> objXSUB.h')
575     or die "Can't create objXSUB.h: $!\n";
576
577 print OBX <<'EOT';
578 /* !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!! 
579    This file is built by embed.pl from global.sym, pp.sym, intrpvar.h,
580    perlvars.h and thrdvar.h.  Any changes made here will be lost!
581 */
582
583 #ifndef __objXSUB_h__
584 #define __objXSUB_h__
585
586 /* Variables */
587
588 EOT
589
590 foreach my $sym (sort(keys(%intrp),
591                       keys(%thread),
592                       keys(%globvar),
593                       keys(%objvar)))
594 {
595     print OBX objxsub_var($sym);
596 }
597
598 print OBX <<'EOT';
599
600 /* Functions */
601
602 EOT
603
604
605 for $sym (sort(keys(%global),@staticfuncs)) {
606     print OBX objxsub_func($sym);
607 }
608
609
610 print OBX <<'EOT';
611
612 #endif  /* __objXSUB_h__ */
613 EOT
614
615 close(OBX);