RE: 5.6.0 BUG: Lexical warnings aren't lexical
[p5sagit/p5-mst-13.2.git] / ext / B / B.pm
CommitLineData
a798dbf2 1# B.pm
2#
1a52ab62 3# Copyright (c) 1996, 1997, 1998 Malcolm Beattie
a798dbf2 4#
5# You may distribute under the terms of either the GNU General Public
6# License or the Artistic License, as specified in the README file.
7#
8package B;
9426adcd 9use XSLoader ();
a798dbf2 10require Exporter;
9426adcd 11@ISA = qw(Exporter);
b2590c4e 12
f72d64f0 13# walkoptree_slow comes from B.pm (you are there),
14# walkoptree comes from B.xs
f6c2d85b 15@EXPORT_OK = qw(minus_c ppname save_BEGINs
16 class peekop cast_I32 cstring cchar hash threadsv_names
b2590c4e 17 main_root main_start main_cv svref_2object opnumber
18 amagic_generation
f6c2d85b 19 walkoptree_slow walkoptree walkoptree_exec walksymtable
20 parents comppadlist sv_undef compile_stats timing_info
21 begin_av init_av end_av);
b2590c4e 22
4c1f658f 23sub OPf_KIDS ();
a798dbf2 24use strict;
25@B::SV::ISA = 'B::OBJECT';
26@B::NULL::ISA = 'B::SV';
27@B::PV::ISA = 'B::SV';
28@B::IV::ISA = 'B::SV';
29@B::NV::ISA = 'B::IV';
30@B::RV::ISA = 'B::SV';
31@B::PVIV::ISA = qw(B::PV B::IV);
32@B::PVNV::ISA = qw(B::PV B::NV);
33@B::PVMG::ISA = 'B::PVNV';
34@B::PVLV::ISA = 'B::PVMG';
35@B::BM::ISA = 'B::PVMG';
36@B::AV::ISA = 'B::PVMG';
37@B::GV::ISA = 'B::PVMG';
38@B::HV::ISA = 'B::PVMG';
39@B::CV::ISA = 'B::PVMG';
276493cb 40@B::IO::ISA = 'B::PVMG';
41@B::FM::ISA = 'B::CV';
a798dbf2 42
43@B::OP::ISA = 'B::OBJECT';
44@B::UNOP::ISA = 'B::OP';
45@B::BINOP::ISA = 'B::UNOP';
46@B::LOGOP::ISA = 'B::UNOP';
a798dbf2 47@B::LISTOP::ISA = 'B::BINOP';
48@B::SVOP::ISA = 'B::OP';
7934575e 49@B::PADOP::ISA = 'B::OP';
a798dbf2 50@B::PVOP::ISA = 'B::OP';
51@B::CVOP::ISA = 'B::OP';
52@B::LOOP::ISA = 'B::LISTOP';
53@B::PMOP::ISA = 'B::LISTOP';
54@B::COP::ISA = 'B::OP';
55
56@B::SPECIAL::ISA = 'B::OBJECT';
57
58{
59 # Stop "-w" from complaining about the lack of a real B::OBJECT class
60 package B::OBJECT;
61}
62
63my $debug;
64my $op_count = 0;
65my @parents = ();
66
67sub debug {
68 my ($class, $value) = @_;
69 $debug = $value;
70 walkoptree_debug($value);
71}
72
a798dbf2 73sub class {
74 my $obj = shift;
75 my $name = ref $obj;
76 $name =~ s/^.*:://;
77 return $name;
78}
79
80sub parents { \@parents }
81
82# For debugging
83sub peekop {
84 my $op = shift;
3f872cb9 85 return sprintf("%s (0x%x) %s", class($op), $$op, $op->name);
a798dbf2 86}
87
b2590c4e 88sub walkoptree_slow {
a798dbf2 89 my($op, $method, $level) = @_;
90 $op_count++; # just for statistics
91 $level ||= 0;
92 warn(sprintf("walkoptree: %d. %s\n", $level, peekop($op))) if $debug;
93 $op->$method($level);
94 if ($$op && ($op->flags & OPf_KIDS)) {
95 my $kid;
96 unshift(@parents, $op);
97 for ($kid = $op->first; $$kid; $kid = $kid->sibling) {
b2590c4e 98 walkoptree_slow($kid, $method, $level + 1);
a798dbf2 99 }
100 shift @parents;
101 }
102}
103
104sub compile_stats {
105 return "Total number of OPs processed: $op_count\n";
106}
107
108sub timing_info {
109 my ($sec, $min, $hr) = localtime;
110 my ($user, $sys) = times;
111 sprintf("%02d:%02d:%02d user=$user sys=$sys",
112 $hr, $min, $sec, $user, $sys);
113}
114
115my %symtable;
2b8dc4d2 116
117sub clearsym {
118 %symtable = ();
119}
120
a798dbf2 121sub savesym {
122 my ($obj, $value) = @_;
123# warn(sprintf("savesym: sym_%x => %s\n", $$obj, $value)); # debug
124 $symtable{sprintf("sym_%x", $$obj)} = $value;
125}
126
127sub objsym {
128 my $obj = shift;
129 return $symtable{sprintf("sym_%x", $$obj)};
130}
131
132sub walkoptree_exec {
133 my ($op, $method, $level) = @_;
244826eb 134 $level ||= 0;
a798dbf2 135 my ($sym, $ppname);
136 my $prefix = " " x $level;
137 for (; $$op; $op = $op->next) {
138 $sym = objsym($op);
139 if (defined($sym)) {
140 print $prefix, "goto $sym\n";
141 return;
142 }
143 savesym($op, sprintf("%s (0x%lx)", class($op), $$op));
144 $op->$method($level);
3f872cb9 145 $ppname = $op->name;
1a67a97c 146 if ($ppname =~
3f872cb9 147 /^(or|and|mapwhile|grepwhile|entertry|range|cond_expr)$/)
1a67a97c 148 {
a798dbf2 149 print $prefix, uc($1), " => {\n";
150 walkoptree_exec($op->other, $method, $level + 1);
151 print $prefix, "}\n";
3f872cb9 152 } elsif ($ppname eq "match" || $ppname eq "subst") {
a798dbf2 153 my $pmreplstart = $op->pmreplstart;
154 if ($$pmreplstart) {
155 print $prefix, "PMREPLSTART => {\n";
156 walkoptree_exec($pmreplstart, $method, $level + 1);
157 print $prefix, "}\n";
158 }
3f872cb9 159 } elsif ($ppname eq "substcont") {
a798dbf2 160 print $prefix, "SUBSTCONT => {\n";
161 walkoptree_exec($op->other->pmreplstart, $method, $level + 1);
162 print $prefix, "}\n";
163 $op = $op->other;
3f872cb9 164 } elsif ($ppname eq "enterloop") {
a798dbf2 165 print $prefix, "REDO => {\n";
166 walkoptree_exec($op->redoop, $method, $level + 1);
167 print $prefix, "}\n", $prefix, "NEXT => {\n";
168 walkoptree_exec($op->nextop, $method, $level + 1);
169 print $prefix, "}\n", $prefix, "LAST => {\n";
170 walkoptree_exec($op->lastop, $method, $level + 1);
171 print $prefix, "}\n";
3f872cb9 172 } elsif ($ppname eq "subst") {
a798dbf2 173 my $replstart = $op->pmreplstart;
174 if ($$replstart) {
175 print $prefix, "SUBST => {\n";
176 walkoptree_exec($replstart, $method, $level + 1);
177 print $prefix, "}\n";
178 }
179 }
180 }
181}
182
183sub walksymtable {
184 my ($symref, $method, $recurse, $prefix) = @_;
185 my $sym;
0cc1d052 186 my $ref;
a798dbf2 187 no strict 'vars';
188 local(*glob);
0cc1d052 189 $prefix = '' unless defined $prefix;
190 while (($sym, $ref) = each %$symref) {
8bac7e00 191 *glob = "*main::".$prefix.$sym;
a798dbf2 192 if ($sym =~ /::$/) {
193 $sym = $prefix . $sym;
b4e94495 194 if ($sym ne "main::" && $sym ne "<none>::" && &$recurse($sym)) {
a798dbf2 195 walksymtable(\%glob, $method, $recurse, $sym);
196 }
197 } else {
198 svref_2object(\*glob)->EGV->$method();
199 }
200 }
201}
202
203{
204 package B::Section;
205 my $output_fh;
206 my %sections;
207
208 sub new {
209 my ($class, $section, $symtable, $default) = @_;
210 $output_fh ||= FileHandle->new_tmpfile;
211 my $obj = bless [-1, $section, $symtable, $default], $class;
212 $sections{$section} = $obj;
213 return $obj;
214 }
215
216 sub get {
217 my ($class, $section) = @_;
218 return $sections{$section};
219 }
220
221 sub add {
222 my $section = shift;
223 while (defined($_ = shift)) {
224 print $output_fh "$section->[1]\t$_\n";
225 $section->[0]++;
226 }
227 }
228
229 sub index {
230 my $section = shift;
231 return $section->[0];
232 }
233
234 sub name {
235 my $section = shift;
236 return $section->[1];
237 }
238
239 sub symtable {
240 my $section = shift;
241 return $section->[2];
242 }
243
244 sub default {
245 my $section = shift;
246 return $section->[3];
247 }
248
249 sub output {
250 my ($section, $fh, $format) = @_;
251 my $name = $section->name;
252 my $sym = $section->symtable || {};
253 my $default = $section->default;
254
255 seek($output_fh, 0, 0);
256 while (<$output_fh>) {
257 chomp;
258 s/^(.*?)\t//;
259 if ($1 eq $name) {
260 s{(s\\_[0-9a-f]+)} {
261 exists($sym->{$1}) ? $sym->{$1} : $default;
262 }ge;
263 printf $fh $format, $_;
264 }
265 }
266 }
267}
268
9426adcd 269XSLoader::load 'B';
a798dbf2 270
2711;
7f20e9dd 272
273__END__
274
275=head1 NAME
276
277B - The Perl Compiler
278
279=head1 SYNOPSIS
280
281 use B;
282
283=head1 DESCRIPTION
284
1a52ab62 285The C<B> module supplies classes which allow a Perl program to delve
286into its own innards. It is the module used to implement the
287"backends" of the Perl compiler. Usage of the compiler does not
288require knowledge of this module: see the F<O> module for the
289user-visible part. The C<B> module is of use to those who want to
290write new compiler backends. This documentation assumes that the
291reader knows a fair amount about perl's internals including such
292things as SVs, OPs and the internal symbol table and syntax tree
293of a program.
294
295=head1 OVERVIEW OF CLASSES
296
297The C structures used by Perl's internals to hold SV and OP
298information (PVIV, AV, HV, ..., OP, SVOP, UNOP, ...) are modelled on a
299class hierarchy and the C<B> module gives access to them via a true
300object hierarchy. Structure fields which point to other objects
301(whether types of SV or types of OP) are represented by the C<B>
302module as Perl objects of the appropriate class. The bulk of the C<B>
303module is the methods for accessing fields of these structures. Note
304that all access is read-only: you cannot modify the internals by
305using this module.
306
307=head2 SV-RELATED CLASSES
308
309B::IV, B::NV, B::RV, B::PV, B::PVIV, B::PVNV, B::PVMG, B::BM, B::PVLV,
310B::AV, B::HV, B::CV, B::GV, B::FM, B::IO. These classes correspond in
311the obvious way to the underlying C structures of similar names. The
312inheritance hierarchy mimics the underlying C "inheritance". Access
313methods correspond to the underlying C macros for field access,
314usually with the leading "class indication" prefix removed (Sv, Av,
315Hv, ...). The leading prefix is only left in cases where its removal
316would cause a clash in method name. For example, C<GvREFCNT> stays
317as-is since its abbreviation would clash with the "superclass" method
318C<REFCNT> (corresponding to the C function C<SvREFCNT>).
319
320=head2 B::SV METHODS
321
322=over 4
323
324=item REFCNT
325
326=item FLAGS
327
328=back
329
330=head2 B::IV METHODS
331
332=over 4
333
334=item IV
335
336=item IVX
337
338=item needs64bits
339
340=item packiv
341
342=back
343
344=head2 B::NV METHODS
345
346=over 4
347
348=item NV
349
350=item NVX
351
352=back
353
354=head2 B::RV METHODS
355
356=over 4
357
358=item RV
359
360=back
361
362=head2 B::PV METHODS
363
364=over 4
365
366=item PV
367
368=back
369
370=head2 B::PVMG METHODS
371
372=over 4
373
374=item MAGIC
375
376=item SvSTASH
377
378=back
379
380=head2 B::MAGIC METHODS
381
382=over 4
383
384=item MOREMAGIC
385
386=item PRIVATE
387
388=item TYPE
389
390=item FLAGS
391
392=item OBJ
393
394=item PTR
395
396=back
397
398=head2 B::PVLV METHODS
399
400=over 4
401
402=item TARGOFF
403
404=item TARGLEN
405
406=item TYPE
407
408=item TARG
409
410=back
411
412=head2 B::BM METHODS
413
414=over 4
415
416=item USEFUL
417
418=item PREVIOUS
419
420=item RARE
421
422=item TABLE
423
424=back
425
426=head2 B::GV METHODS
427
428=over 4
429
87d7fd28 430=item is_empty
431
432This method returns TRUE if the GP field of the GV is NULL.
433
1a52ab62 434=item NAME
435
436=item STASH
437
438=item SV
439
440=item IO
441
442=item FORM
443
444=item AV
445
446=item HV
447
448=item EGV
449
450=item CV
451
452=item CVGEN
453
454=item LINE
455
b195d487 456=item FILE
457
1a52ab62 458=item FILEGV
459
460=item GvREFCNT
461
462=item FLAGS
463
464=back
465
466=head2 B::IO METHODS
467
468=over 4
469
470=item LINES
471
472=item PAGE
473
474=item PAGE_LEN
475
476=item LINES_LEFT
477
478=item TOP_NAME
479
480=item TOP_GV
481
482=item FMT_NAME
483
484=item FMT_GV
485
486=item BOTTOM_NAME
487
488=item BOTTOM_GV
489
490=item SUBPROCESS
491
492=item IoTYPE
493
494=item IoFLAGS
495
496=back
497
498=head2 B::AV METHODS
499
500=over 4
501
502=item FILL
503
504=item MAX
505
506=item OFF
507
508=item ARRAY
509
510=item AvFLAGS
511
512=back
513
514=head2 B::CV METHODS
515
516=over 4
517
518=item STASH
519
520=item START
521
522=item ROOT
523
524=item GV
525
57843af0 526=item FILE
527
1a52ab62 528=item DEPTH
529
530=item PADLIST
531
532=item OUTSIDE
533
534=item XSUB
535
536=item XSUBANY
537
5cfd8ad4 538=item CvFLAGS
539
de3f1649 540=item const_sv
541
1a52ab62 542=back
543
544=head2 B::HV METHODS
545
546=over 4
547
548=item FILL
549
550=item MAX
551
552=item KEYS
553
554=item RITER
555
556=item NAME
557
558=item PMROOT
559
560=item ARRAY
561
562=back
563
564=head2 OP-RELATED CLASSES
565
1a67a97c 566B::OP, B::UNOP, B::BINOP, B::LOGOP, B::LISTOP, B::PMOP,
7934575e 567B::SVOP, B::PADOP, B::PVOP, B::CVOP, B::LOOP, B::COP.
1a52ab62 568These classes correspond in
569the obvious way to the underlying C structures of similar names. The
570inheritance hierarchy mimics the underlying C "inheritance". Access
571methods correspond to the underlying C structre field names, with the
572leading "class indication" prefix removed (op_).
573
574=head2 B::OP METHODS
575
576=over 4
577
578=item next
579
580=item sibling
581
3f872cb9 582=item name
583
584This returns the op name as a string (e.g. "add", "rv2av").
585
1a52ab62 586=item ppaddr
587
dc333d64 588This returns the function name as a string (e.g. "PL_ppaddr[OP_ADD]",
589"PL_ppaddr[OP_RV2AV]").
1a52ab62 590
591=item desc
592
4369b173 593This returns the op description from the global C PL_op_desc array
1a52ab62 594(e.g. "addition" "array deref").
595
596=item targ
597
598=item type
599
600=item seq
601
602=item flags
603
604=item private
605
606=back
607
608=head2 B::UNOP METHOD
609
610=over 4
611
612=item first
613
614=back
615
616=head2 B::BINOP METHOD
617
618=over 4
619
620=item last
621
622=back
623
624=head2 B::LOGOP METHOD
625
626=over 4
627
628=item other
629
630=back
631
1a52ab62 632=head2 B::LISTOP METHOD
633
634=over 4
635
636=item children
637
638=back
639
640=head2 B::PMOP METHODS
641
642=over 4
643
644=item pmreplroot
645
646=item pmreplstart
647
648=item pmnext
649
650=item pmregexp
651
652=item pmflags
653
654=item pmpermflags
655
656=item precomp
657
658=back
659
660=head2 B::SVOP METHOD
661
662=over 4
663
664=item sv
665
065a1863 666=item gv
667
1a52ab62 668=back
669
7934575e 670=head2 B::PADOP METHOD
1a52ab62 671
672=over 4
673
7934575e 674=item padix
1a52ab62 675
676=back
677
678=head2 B::PVOP METHOD
679
680=over 4
681
682=item pv
683
684=back
685
686=head2 B::LOOP METHODS
687
688=over 4
689
690=item redoop
691
692=item nextop
693
694=item lastop
695
696=back
697
698=head2 B::COP METHODS
699
700=over 4
701
702=item label
703
704=item stash
705
57843af0 706=item file
1a52ab62 707
708=item cop_seq
709
710=item arybase
711
712=item line
713
714=back
715
716=head1 FUNCTIONS EXPORTED BY C<B>
717
718The C<B> module exports a variety of functions: some are simple
719utility functions, others provide a Perl program with a way to
720get an initial "handle" on an internal object.
721
722=over 4
723
724=item main_cv
725
726Return the (faked) CV corresponding to the main part of the Perl
727program.
728
31d7d75a 729=item init_av
730
731Returns the AV object (i.e. in class B::AV) representing INIT blocks.
732
1a52ab62 733=item main_root
734
735Returns the root op (i.e. an object in the appropriate B::OP-derived
736class) of the main part of the Perl program.
737
738=item main_start
739
740Returns the starting op of the main part of the Perl program.
741
742=item comppadlist
743
744Returns the AV object (i.e. in class B::AV) of the global comppadlist.
745
746=item sv_undef
747
748Returns the SV object corresponding to the C variable C<sv_undef>.
749
750=item sv_yes
751
752Returns the SV object corresponding to the C variable C<sv_yes>.
753
754=item sv_no
755
756Returns the SV object corresponding to the C variable C<sv_no>.
757
56eca212 758=item amagic_generation
759
760Returns the SV object corresponding to the C variable C<amagic_generation>.
761
1a52ab62 762=item walkoptree(OP, METHOD)
763
764Does a tree-walk of the syntax tree based at OP and calls METHOD on
765each op it visits. Each node is visited before its children. If
766C<walkoptree_debug> (q.v.) has been called to turn debugging on then
767the method C<walkoptree_debug> is called on each op before METHOD is
768called.
769
770=item walkoptree_debug(DEBUG)
771
772Returns the current debugging flag for C<walkoptree>. If the optional
773DEBUG argument is non-zero, it sets the debugging flag to that. See
774the description of C<walkoptree> above for what the debugging flag
775does.
776
777=item walksymtable(SYMREF, METHOD, RECURSE)
778
779Walk the symbol table starting at SYMREF and call METHOD on each
780symbol visited. When the walk reached package symbols "Foo::" it
781invokes RECURSE and only recurses into the package if that sub
782returns true.
783
784=item svref_2object(SV)
785
786Takes any Perl variable and turns it into an object in the
787appropriate B::OP-derived or B::SV-derived class. Apart from functions
788such as C<main_root>, this is the primary way to get an initial
789"handle" on a internal perl data structure which can then be followed
790with the other access methods.
791
792=item ppname(OPNUM)
793
794Return the PP function name (e.g. "pp_add") of op number OPNUM.
795
796=item hash(STR)
797
798Returns a string in the form "0x..." representing the value of the
799internal hash function used by perl on string STR.
800
801=item cast_I32(I)
802
803Casts I to the internal I32 type used by that perl.
804
805
806=item minus_c
807
808Does the equivalent of the C<-c> command-line option. Obviously, this
809is only useful in a BEGIN block or else the flag is set too late.
810
811
812=item cstring(STR)
813
814Returns a double-quote-surrounded escaped version of STR which can
815be used as a string in C source code.
816
817=item class(OBJ)
818
819Returns the class of an object without the part of the classname
820preceding the first "::". This is used to turn "B::UNOP" into
821"UNOP" for example.
822
823=item threadsv_names
824
825In a perl compiled for threads, this returns a list of the special
826per-thread threadsv variables.
827
1a52ab62 828=back
7f20e9dd 829
830=head1 AUTHOR
831
832Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
833
834=cut