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