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