This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Deparse.pm: Two more matchwords
[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;
744aaba0 9use strict;
28b605d8 10
a798dbf2 11require Exporter;
744aaba0 12@B::ISA = qw(Exporter);
b2590c4e 13
f72d64f0
DC
14# walkoptree_slow comes from B.pm (you are there),
15# walkoptree comes from B.xs
744aaba0
NC
16
17BEGIN {
fe02b245 18 $B::VERSION = '1.54';
4aa23ba6 19 @B::EXPORT_OK = ();
744aaba0 20
4aa23ba6
NC
21 # Our BOOT code needs $VERSION set, and will append to @EXPORT_OK.
22 # Want our constants loaded before the compiler meets OPf_KIDS below, as
23 # the combination of having the constant stay a Proxy Constant Subroutine
24 # and its value being inlined saves a little over .5K
744aaba0 25
744aaba0
NC
26 require XSLoader;
27 XSLoader::load();
28}
29
4aa23ba6
NC
30push @B::EXPORT_OK, (qw(minus_c ppname save_BEGINs
31 class peekop cast_I32 cstring cchar hash threadsv_names
32 main_root main_start main_cv svref_2object opnumber
33 sub_generation amagic_generation perlstring
34 walkoptree_slow walkoptree walkoptree_exec walksymtable
35 parents comppadlist sv_undef compile_stats timing_info
36 begin_av init_av check_av end_av regex_padav dowarn
37 defstash curstash warnhook diehook inc_gv @optype
d9cd2aeb 38 @specialsv_name unitcheck_av safename));
4aa23ba6 39
a798dbf2
MB
40@B::SV::ISA = 'B::OBJECT';
41@B::NULL::ISA = 'B::SV';
42@B::PV::ISA = 'B::SV';
43@B::IV::ISA = 'B::SV';
4edc9001 44@B::NV::ISA = 'B::SV';
4df7f6af 45# RV is eliminated with 5.11.0, but effectively is a specialisation of IV now.
3ce3ed55 46@B::RV::ISA = $] >= 5.011 ? 'B::IV' : 'B::SV';
a798dbf2 47@B::PVIV::ISA = qw(B::PV B::IV);
4edc9001 48@B::PVNV::ISA = qw(B::PVIV B::NV);
a798dbf2 49@B::PVMG::ISA = 'B::PVNV';
5c35adbb 50@B::REGEXP::ISA = 'B::PVMG' if $] >= 5.011;
38d2280f 51@B::INVLIST::ISA = 'B::PV' if $] >= 5.019;
35633035
DM
52@B::PVLV::ISA = 'B::GV';
53@B::BM::ISA = 'B::GV';
a798dbf2
MB
54@B::AV::ISA = 'B::PVMG';
55@B::GV::ISA = 'B::PVMG';
56@B::HV::ISA = 'B::PVMG';
57@B::CV::ISA = 'B::PVMG';
276493cb
SM
58@B::IO::ISA = 'B::PVMG';
59@B::FM::ISA = 'B::CV';
a798dbf2
MB
60
61@B::OP::ISA = 'B::OBJECT';
62@B::UNOP::ISA = 'B::OP';
2f7c6295 63@B::UNOP_AUX::ISA = 'B::UNOP';
a798dbf2
MB
64@B::BINOP::ISA = 'B::UNOP';
65@B::LOGOP::ISA = 'B::UNOP';
a798dbf2
MB
66@B::LISTOP::ISA = 'B::BINOP';
67@B::SVOP::ISA = 'B::OP';
7934575e 68@B::PADOP::ISA = 'B::OP';
a798dbf2 69@B::PVOP::ISA = 'B::OP';
a798dbf2
MB
70@B::LOOP::ISA = 'B::LISTOP';
71@B::PMOP::ISA = 'B::LISTOP';
72@B::COP::ISA = 'B::OP';
b46e009d 73@B::METHOP::ISA = 'B::OP';
a798dbf2
MB
74
75@B::SPECIAL::ISA = 'B::OBJECT';
76
2f7c6295
DM
77@B::optype = qw(OP UNOP BINOP LOGOP LISTOP PMOP SVOP PADOP PVOP LOOP COP
78 METHOP UNOP_AUX);
baccf54f
NC
79# bytecode.pl contained the following comment:
80# Nullsv *must* come first in the following so that the condition
81# ($$sv == 0) can continue to be used to test (sv == Nullsv).
82@B::specialsv_name = qw(Nullsv &PL_sv_undef &PL_sv_yes &PL_sv_no
83 (SV*)pWARN_ALL (SV*)pWARN_NONE (SV*)pWARN_STD);
84
a798dbf2
MB
85{
86 # Stop "-w" from complaining about the lack of a real B::OBJECT class
87 package B::OBJECT;
88}
89
002b978b 90sub B::GV::SAFENAME {
d9cd2aeb
FC
91 safename(shift()->NAME);
92}
93
94sub safename {
95 my $name = shift;
d9963e60
RH
96
97 # The regex below corresponds to the isCONTROLVAR macro
98 # from toke.c
99
2bd1cbf6
KW
100 $name =~ s/^\c?/^?/
101 or $name =~ s/^([\cA-\cZ\c\\c[\c]\c_\c^])/
102 "^" . chr( utf8::unicode_to_native( 64 ^ ord($1) ))/e;
7a9b44b9
RH
103
104 # When we say unicode_to_native we really mean ascii_to_native,
2bd1cbf6
KW
105 # which matters iff this is a non-ASCII platform (EBCDIC). '\c?' would
106 # not have to be special cased, except for non-ASCII.
7a9b44b9 107
002b978b
RH
108 return $name;
109}
110
d9963e60
RH
111sub B::IV::int_value {
112 my ($self) = @_;
113 return (($self->FLAGS() & SVf_IVisUV()) ? $self->UVX : $self->IV);
114}
115
f3402b25 116sub B::NULL::as_string() {""}
88ecb8a6
NC
117*B::IV::as_string = \*B::IV::int_value;
118*B::PV::as_string = \*B::PV::PV;
f3402b25 119
ff1a9fc0
NC
120# The input typemap checking makes no distinction between different SV types,
121# so the XS body will generate the same C code, despite the different XS
122# "types". So there is no change in behaviour from doing "newXS" like this,
123# compared with the old approach of having a (near) duplicate XS body.
124# We should fix the typemap checking.
88ecb8a6 125*B::IV::RV = \*B::PV::RV if $] > 5.012;
ff1a9fc0 126
a798dbf2
MB
127my $debug;
128my $op_count = 0;
129my @parents = ();
130
131sub debug {
132 my ($class, $value) = @_;
133 $debug = $value;
134 walkoptree_debug($value);
135}
136
a798dbf2
MB
137sub class {
138 my $obj = shift;
139 my $name = ref $obj;
140 $name =~ s/^.*:://;
141 return $name;
142}
143
144sub parents { \@parents }
145
146# For debugging
147sub peekop {
148 my $op = shift;
3f872cb9 149 return sprintf("%s (0x%x) %s", class($op), $$op, $op->name);
a798dbf2
MB
150}
151
b2590c4e 152sub walkoptree_slow {
a798dbf2
MB
153 my($op, $method, $level) = @_;
154 $op_count++; # just for statistics
155 $level ||= 0;
156 warn(sprintf("walkoptree: %d. %s\n", $level, peekop($op))) if $debug;
156f89f0 157 $op->$method($level) if $op->can($method);
a798dbf2
MB
158 if ($$op && ($op->flags & OPf_KIDS)) {
159 my $kid;
160 unshift(@parents, $op);
161 for ($kid = $op->first; $$kid; $kid = $kid->sibling) {
b2590c4e 162 walkoptree_slow($kid, $method, $level + 1);
a798dbf2
MB
163 }
164 shift @parents;
165 }
156f89f0
JJ
166 if (class($op) eq 'PMOP'
167 && ref($op->pmreplroot)
168 && ${$op->pmreplroot}
169 && $op->pmreplroot->isa( 'B::OP' ))
170 {
0091380b
RGS
171 unshift(@parents, $op);
172 walkoptree_slow($op->pmreplroot, $method, $level + 1);
173 shift @parents;
174 }
a798dbf2
MB
175}
176
177sub compile_stats {
178 return "Total number of OPs processed: $op_count\n";
179}
180
181sub timing_info {
182 my ($sec, $min, $hr) = localtime;
183 my ($user, $sys) = times;
184 sprintf("%02d:%02d:%02d user=$user sys=$sys",
185 $hr, $min, $sec, $user, $sys);
186}
187
188my %symtable;
2b8dc4d2
DM
189
190sub clearsym {
191 %symtable = ();
192}
193
a798dbf2
MB
194sub savesym {
195 my ($obj, $value) = @_;
196# warn(sprintf("savesym: sym_%x => %s\n", $$obj, $value)); # debug
197 $symtable{sprintf("sym_%x", $$obj)} = $value;
198}
199
200sub objsym {
201 my $obj = shift;
202 return $symtable{sprintf("sym_%x", $$obj)};
203}
204
205sub walkoptree_exec {
206 my ($op, $method, $level) = @_;
244826eb 207 $level ||= 0;
a798dbf2
MB
208 my ($sym, $ppname);
209 my $prefix = " " x $level;
210 for (; $$op; $op = $op->next) {
211 $sym = objsym($op);
212 if (defined($sym)) {
213 print $prefix, "goto $sym\n";
214 return;
215 }
216 savesym($op, sprintf("%s (0x%lx)", class($op), $$op));
217 $op->$method($level);
3f872cb9 218 $ppname = $op->name;
1a67a97c 219 if ($ppname =~
62e36f8a 220 /^(d?or(assign)?|and(assign)?|mapwhile|grepwhile|entertry|range|cond_expr)$/)
1a67a97c 221 {
a798dbf2
MB
222 print $prefix, uc($1), " => {\n";
223 walkoptree_exec($op->other, $method, $level + 1);
224 print $prefix, "}\n";
3f872cb9 225 } elsif ($ppname eq "match" || $ppname eq "subst") {
a798dbf2
MB
226 my $pmreplstart = $op->pmreplstart;
227 if ($$pmreplstart) {
228 print $prefix, "PMREPLSTART => {\n";
229 walkoptree_exec($pmreplstart, $method, $level + 1);
230 print $prefix, "}\n";
231 }
3f872cb9 232 } elsif ($ppname eq "substcont") {
a798dbf2
MB
233 print $prefix, "SUBSTCONT => {\n";
234 walkoptree_exec($op->other->pmreplstart, $method, $level + 1);
235 print $prefix, "}\n";
236 $op = $op->other;
3f872cb9 237 } elsif ($ppname eq "enterloop") {
a798dbf2
MB
238 print $prefix, "REDO => {\n";
239 walkoptree_exec($op->redoop, $method, $level + 1);
240 print $prefix, "}\n", $prefix, "NEXT => {\n";
241 walkoptree_exec($op->nextop, $method, $level + 1);
242 print $prefix, "}\n", $prefix, "LAST => {\n";
243 walkoptree_exec($op->lastop, $method, $level + 1);
244 print $prefix, "}\n";
3f872cb9 245 } elsif ($ppname eq "subst") {
a798dbf2
MB
246 my $replstart = $op->pmreplstart;
247 if ($$replstart) {
248 print $prefix, "SUBST => {\n";
249 walkoptree_exec($replstart, $method, $level + 1);
250 print $prefix, "}\n";
251 }
252 }
253 }
254}
255
256sub walksymtable {
257 my ($symref, $method, $recurse, $prefix) = @_;
258 my $sym;
0cc1d052 259 my $ref;
b6b0fb7b
MB
260 my $fullname;
261 no strict 'refs';
0cc1d052 262 $prefix = '' unless defined $prefix;
5cc8528c
YO
263 foreach my $sym ( sort keys %$symref ) {
264 $ref= $symref->{$sym};
b6b0fb7b 265 $fullname = "*main::".$prefix.$sym;
a798dbf2
MB
266 if ($sym =~ /::$/) {
267 $sym = $prefix . $sym;
7834d9fb 268 if (svref_2object(\*$sym)->NAME ne "main::" && $sym ne "<none>::" && &$recurse($sym)) {
b6b0fb7b 269 walksymtable(\%$fullname, $method, $recurse, $sym);
a798dbf2
MB
270 }
271 } else {
b6b0fb7b 272 svref_2object(\*$fullname)->$method();
a798dbf2
MB
273 }
274 }
275}
276
277{
278 package B::Section;
279 my $output_fh;
280 my %sections;
85cf7f2e 281
a798dbf2
MB
282 sub new {
283 my ($class, $section, $symtable, $default) = @_;
284 $output_fh ||= FileHandle->new_tmpfile;
285 my $obj = bless [-1, $section, $symtable, $default], $class;
286 $sections{$section} = $obj;
287 return $obj;
288 }
85cf7f2e 289
a798dbf2
MB
290 sub get {
291 my ($class, $section) = @_;
292 return $sections{$section};
293 }
294
295 sub add {
296 my $section = shift;
297 while (defined($_ = shift)) {
298 print $output_fh "$section->[1]\t$_\n";
299 $section->[0]++;
300 }
301 }
302
303 sub index {
304 my $section = shift;
305 return $section->[0];
306 }
307
308 sub name {
309 my $section = shift;
310 return $section->[1];
311 }
312
313 sub symtable {
314 my $section = shift;
315 return $section->[2];
316 }
85cf7f2e 317
a798dbf2
MB
318 sub default {
319 my $section = shift;
320 return $section->[3];
321 }
85cf7f2e 322
a798dbf2
MB
323 sub output {
324 my ($section, $fh, $format) = @_;
325 my $name = $section->name;
326 my $sym = $section->symtable || {};
327 my $default = $section->default;
328
329 seek($output_fh, 0, 0);
330 while (<$output_fh>) {
331 chomp;
332 s/^(.*?)\t//;
333 if ($1 eq $name) {
334 s{(s\\_[0-9a-f]+)} {
335 exists($sym->{$1}) ? $sym->{$1} : $default;
336 }ge;
337 printf $fh $format, $_;
338 }
339 }
340 }
341}
342
a798dbf2 3431;
7f20e9dd
GS
344
345__END__
346
347=head1 NAME
348
4b661dd3 349B - The Perl Compiler Backend
7f20e9dd
GS
350
351=head1 SYNOPSIS
352
353 use B;
354
355=head1 DESCRIPTION
356
1a52ab62 357The C<B> module supplies classes which allow a Perl program to delve
130592f5
FC
358into its own innards. It is the module used to implement the
359"backends" of the Perl compiler. Usage of the compiler does not
1a52ab62 360require knowledge of this module: see the F<O> module for the
130592f5
FC
361user-visible part. The C<B> module is of use to those who want to
362write new compiler backends. This documentation assumes that the
1a52ab62
MB
363reader knows a fair amount about perl's internals including such
364things as SVs, OPs and the internal symbol table and syntax tree
365of a program.
366
85cf7f2e
MJD
367=head1 OVERVIEW
368
369The C<B> module contains a set of utility functions for querying the
370current state of the Perl interpreter; typically these functions
371return objects from the B::SV and B::OP classes, or their derived
372classes. These classes in turn define methods for querying the
373resulting objects about their own internal state.
374
375=head1 Utility Functions
376
377The C<B> module exports a variety of functions: some are simple
378utility functions, others provide a Perl program with a way to
379get an initial "handle" on an internal object.
380
381=head2 Functions Returning C<B::SV>, C<B::AV>, C<B::HV>, and C<B::CV> objects
382
3d036c2b 383For descriptions of the class hierarchy of these objects and the
85cf7f2e
MJD
384methods that can be called on them, see below, L<"OVERVIEW OF
385CLASSES"> and L<"SV-RELATED CLASSES">.
386
387=over 4
388
389=item sv_undef
390
391Returns the SV object corresponding to the C variable C<sv_undef>.
392
393=item sv_yes
394
395Returns the SV object corresponding to the C variable C<sv_yes>.
396
397=item sv_no
398
399Returns the SV object corresponding to the C variable C<sv_no>.
400
401=item svref_2object(SVREF)
402
403Takes a reference to any Perl value, and turns the referred-to value
404into an object in the appropriate B::OP-derived or B::SV-derived
130592f5 405class. Apart from functions such as C<main_root>, this is the primary
85cf7f2e
MJD
406way to get an initial "handle" on an internal perl data structure
407which can then be followed with the other access methods.
408
f31c3107 409The returned object will only be valid as long as the underlying OPs
130592f5 410and SVs continue to exist. Do not attempt to use the object after the
f31c3107
SM
411underlying structures are freed.
412
85cf7f2e
MJD
413=item amagic_generation
414
415Returns the SV object corresponding to the C variable C<amagic_generation>.
66978156
FC
416As of Perl 5.18, this is just an alias to C<PL_na>, so its value is
417meaningless.
85cf7f2e 418
e13efe3c 419=item init_av
85cf7f2e
MJD
420
421Returns the AV object (i.e. in class B::AV) representing INIT blocks.
422
ece599bd
RGS
423=item check_av
424
425Returns the AV object (i.e. in class B::AV) representing CHECK blocks.
426
676456c2
AG
427=item unitcheck_av
428
429Returns the AV object (i.e. in class B::AV) representing UNITCHECK blocks.
430
85cf7f2e
MJD
431=item begin_av
432
433Returns the AV object (i.e. in class B::AV) representing BEGIN blocks.
434
435=item end_av
436
437Returns the AV object (i.e. in class B::AV) representing END blocks.
438
439=item comppadlist
440
3a910aa0
FC
441Returns the PADLIST object (i.e. in class B::PADLIST) of the global
442comppadlist. In Perl 5.16 and earlier it returns an AV object (class
443B::AV).
85cf7f2e
MJD
444
445=item regex_padav
446
447Only when perl was compiled with ithreads.
448
e13efe3c 449=item main_cv
85cf7f2e
MJD
450
451Return the (faked) CV corresponding to the main part of the Perl
452program.
453
454=back
455
456=head2 Functions for Examining the Symbol Table
457
458=over 4
459
460=item walksymtable(SYMREF, METHOD, RECURSE, PREFIX)
461
462Walk the symbol table starting at SYMREF and call METHOD on each
463symbol (a B::GV object) visited. When the walk reaches package
464symbols (such as "Foo::") it invokes RECURSE, passing in the symbol
465name, and only recurses into the package if that sub returns true.
466
467PREFIX is the name of the SYMREF you're walking.
468
469For example:
470
471 # Walk CGI's symbol table calling print_subs on each symbol.
472 # Recurse only into CGI::Util::
26d2adad
FC
473 walksymtable(\%CGI::, 'print_subs',
474 sub { $_[0] eq 'CGI::Util::' }, 'CGI::');
85cf7f2e 475
130592f5 476print_subs() is a B::GV method you have declared. Also see L<"B::GV
85cf7f2e
MJD
477Methods">, below.
478
479=back
480
481=head2 Functions Returning C<B::OP> objects or for walking op trees
482
3d036c2b 483For descriptions of the class hierarchy of these objects and the
85cf7f2e
MJD
484methods that can be called on them, see below, L<"OVERVIEW OF
485CLASSES"> and L<"OP-RELATED CLASSES">.
486
487=over 4
488
489=item main_root
490
491Returns the root op (i.e. an object in the appropriate B::OP-derived
492class) of the main part of the Perl program.
493
494=item main_start
495
496Returns the starting op of the main part of the Perl program.
497
498=item walkoptree(OP, METHOD)
499
500Does a tree-walk of the syntax tree based at OP and calls METHOD on
130592f5 501each op it visits. Each node is visited before its children. If
85cf7f2e
MJD
502C<walkoptree_debug> (see below) has been called to turn debugging on then
503the method C<walkoptree_debug> is called on each op before METHOD is
504called.
505
506=item walkoptree_debug(DEBUG)
507
130592f5
FC
508Returns the current debugging flag for C<walkoptree>. If the optional
509DEBUG argument is non-zero, it sets the debugging flag to that. See
85cf7f2e
MJD
510the description of C<walkoptree> above for what the debugging flag
511does.
512
513=back
514
515=head2 Miscellaneous Utility Functions
516
517=over 4
518
519=item ppname(OPNUM)
520
521Return the PP function name (e.g. "pp_add") of op number OPNUM.
522
523=item hash(STR)
524
525Returns a string in the form "0x..." representing the value of the
526internal hash function used by perl on string STR.
527
528=item cast_I32(I)
529
530Casts I to the internal I32 type used by that perl.
531
532=item minus_c
533
130592f5 534Does the equivalent of the C<-c> command-line option. Obviously, this
85cf7f2e
MJD
535is only useful in a BEGIN block or else the flag is set too late.
536
537=item cstring(STR)
538
539Returns a double-quote-surrounded escaped version of STR which can
540be used as a string in C source code.
541
542=item perlstring(STR)
543
544Returns a double-quote-surrounded escaped version of STR which can
545be used as a string in Perl source code.
546
d9cd2aeb
FC
547=item safename(STR)
548
549This function returns the string with the first character modified if it
550is a control character. It converts it to ^X format first, so that "\cG"
551becomes "^G". This is used internally by L<B::GV::SAFENAME|/SAFENAME>, but
552you can call it directly.
553
85cf7f2e
MJD
554=item class(OBJ)
555
556Returns the class of an object without the part of the classname
130592f5 557preceding the first C<"::">. This is used to turn C<"B::UNOP"> into
85cf7f2e
MJD
558C<"UNOP"> for example.
559
560=item threadsv_names
561
dacd2ca7
DM
562This used to provide support for the old 5.005 threading module. It now
563does nothing.
85cf7f2e
MJD
564
565=back
566
4082acab 567=head2 Exported utility variables
baccf54f
NC
568
569=over 4
570
571=item @optype
572
573 my $op_type = $optype[$op_type_num];
85cf7f2e 574
baccf54f
NC
575A simple mapping of the op type number to its type (like 'COP' or 'BINOP').
576
577=item @specialsv_name
578
579 my $sv_name = $specialsv_name[$sv_index];
580
581Certain SV types are considered 'special'. They're represented by
582B::SPECIAL and are referred to by a number from the specialsv_list.
583This array maps that number back to the name of the SV (like 'Nullsv'
584or '&PL_sv_undef').
585
586=back
85cf7f2e
MJD
587
588
1a52ab62
MB
589=head1 OVERVIEW OF CLASSES
590
591The C structures used by Perl's internals to hold SV and OP
592information (PVIV, AV, HV, ..., OP, SVOP, UNOP, ...) are modelled on a
593class hierarchy and the C<B> module gives access to them via a true
130592f5 594object hierarchy. Structure fields which point to other objects
1a52ab62 595(whether types of SV or types of OP) are represented by the C<B>
85cf7f2e
MJD
596module as Perl objects of the appropriate class.
597
598The bulk of the C<B> module is the methods for accessing fields of
599these structures.
600
601Note that all access is read-only. You cannot modify the internals by
130592f5 602using this module. Also, note that the B::OP and B::SV objects created
f31c3107
SM
603by this module are only valid for as long as the underlying objects
604exist; their creation doesn't increase the reference counts of the
130592f5 605underlying objects. Trying to access the fields of a freed object will
f31c3107 606give incomprehensible results, or worse.
1a52ab62
MB
607
608=head2 SV-RELATED CLASSES
609
6822775c 610B::IV, B::NV, B::RV, B::PV, B::PVIV, B::PVNV, B::PVMG, B::BM (5.9.5 and
130592f5 611earlier), B::PVLV, B::AV, B::HV, B::CV, B::GV, B::FM, B::IO. These classes
6822775c 612correspond in the obvious way to the underlying C structures of similar names.
130592f5 613The inheritance hierarchy mimics the underlying C "inheritance". For the
dda36756 6145.10.x branch, (I<ie> 5.10.0, 5.10.1 I<etc>) this is:
85cf7f2e 615
6822775c
NC
616 B::SV
617 |
618 +------------+------------+------------+
619 | | | |
620 B::PV B::IV B::NV B::RV
621 \ / /
622 \ / /
623 B::PVIV /
b591c46e
NC
624 \ /
625 \ /
626 \ /
627 B::PVNV
628 |
629 |
630 B::PVMG
631 |
6822775c
NC
632 +-----+-----+-----+-----+
633 | | | | |
634 B::AV B::GV B::HV B::CV B::IO
635 | |
636 | |
637 B::PVLV B::FM
638
6822775c
NC
639For 5.9.0 and earlier, PVLV is a direct subclass of PVMG, and BM is still
640present as a distinct type, so the base of this diagram is
641
642
643 |
644 |
645 B::PVMG
646 |
647 +------+-----+-----+-----+-----+-----+
648 | | | | | | |
649 B::PVLV B::BM B::AV B::GV B::HV B::CV B::IO
650 |
651 |
652 B::FM
f5ba1307 653
dda36756
NC
654For 5.11.0 and later, B::RV is abolished, and IVs can be used to store
655references, and a new type B::REGEXP is introduced, giving this structure:
656
657 B::SV
658 |
659 +------------+------------+
660 | | |
661 B::PV B::IV B::NV
662 \ / /
663 \ / /
664 B::PVIV /
665 \ /
666 \ /
667 \ /
668 B::PVNV
669 |
670 |
671 B::PVMG
672 |
673 +-------+-------+---+---+-------+-------+
674 | | | | | |
675 B::AV B::GV B::HV B::CV B::IO B::REGEXP
676 | |
677 | |
678 B::PVLV B::FM
679
f5ba1307 680
85cf7f2e 681Access methods correspond to the underlying C macros for field access,
1a52ab62 682usually with the leading "class indication" prefix removed (Sv, Av,
130592f5
FC
683Hv, ...). The leading prefix is only left in cases where its removal
684would cause a clash in method name. For example, C<GvREFCNT> stays
1a52ab62
MB
685as-is since its abbreviation would clash with the "superclass" method
686C<REFCNT> (corresponding to the C function C<SvREFCNT>).
687
85cf7f2e 688=head2 B::SV Methods
1a52ab62
MB
689
690=over 4
691
692=item REFCNT
693
694=item FLAGS
695
429a5ce7
SM
696=item object_2svref
697
698Returns a reference to the regular scalar corresponding to this
130592f5
FC
699B::SV object. In other words, this method is the inverse operation
700to the svref_2object() subroutine. This scalar and other data it points
429a5ce7
SM
701at should be considered read-only: modifying them is neither safe nor
702guaranteed to have a sensible effect.
703
1a52ab62
MB
704=back
705
85cf7f2e 706=head2 B::IV Methods
1a52ab62
MB
707
708=over 4
709
710=item IV
711
d9963e60 712Returns the value of the IV, I<interpreted as
130592f5
FC
713a signed integer>. This will be misleading
714if C<FLAGS & SVf_IVisUV>. Perhaps you want the
d9963e60
RH
715C<int_value> method instead?
716
1a52ab62
MB
717=item IVX
718
d9963e60
RH
719=item UVX
720
721=item int_value
722
723This method returns the value of the IV as an integer.
724It differs from C<IV> in that it returns the correct
725value regardless of whether it's stored signed or
726unsigned.
727
1a52ab62
MB
728=item needs64bits
729
730=item packiv
731
732=back
733
85cf7f2e 734=head2 B::NV Methods
1a52ab62
MB
735
736=over 4
737
738=item NV
739
740=item NVX
741
43e81a9a
FC
742=item COP_SEQ_RANGE_LOW
743
744=item COP_SEQ_RANGE_HIGH
745
dbac5ffe
FC
746These last two are only valid for pad name SVs. They only existed in the
747B::NV class before Perl 5.22. In 5.22 they were moved to the B::PADNAME
748class.
43e81a9a 749
1a52ab62
MB
750=back
751
85cf7f2e 752=head2 B::RV Methods
1a52ab62
MB
753
754=over 4
755
756=item RV
757
758=back
759
85cf7f2e 760=head2 B::PV Methods
1a52ab62
MB
761
762=over 4
763
764=item PV
765
130592f5 766This method is the one you usually want. It constructs a
76ef7183
JH
767string using the length and offset information in the struct:
768for ordinary scalars it will return the string that you'd see
769from Perl, even if it contains null characters.
770
9d2bbe64
MB
771=item RV
772
773Same as B::RV::RV, except that it will die() if the PV isn't
774a reference.
775
0b40bd6d
RH
776=item PVX
777
130592f5 778This method is less often useful. It assumes that the string
76ef7183
JH
779stored in the struct is null-terminated, and disregards the
780length information.
781
782It is the appropriate method to use if you need to get the name
130592f5 783of a lexical variable from a padname array. Lexical variable names
76ef7183 784are always stored with a null terminator, and the length field
5c140421
FC
785(CUR) is overloaded for other purposes and can't be relied on here.
786
787=item CUR
788
789This method returns the internal length field, which consists of the number
790of internal bytes, not necessarily the number of logical characters.
791
792=item LEN
793
794This method returns the number of bytes allocated (via malloc) for storing
795the string. This is 0 if the scalar does not "own" the string.
76ef7183 796
1a52ab62
MB
797=back
798
85cf7f2e 799=head2 B::PVMG Methods
1a52ab62
MB
800
801=over 4
802
803=item MAGIC
804
805=item SvSTASH
806
807=back
808
85cf7f2e 809=head2 B::MAGIC Methods
1a52ab62
MB
810
811=over 4
812
813=item MOREMAGIC
814
9d2bbe64
MB
815=item precomp
816
817Only valid on r-magic, returns the string that generated the regexp.
818
1a52ab62
MB
819=item PRIVATE
820
821=item TYPE
822
823=item FLAGS
824
825=item OBJ
826
9d2bbe64
MB
827Will die() if called on r-magic.
828
1a52ab62
MB
829=item PTR
830
9d2bbe64
MB
831=item REGEX
832
833Only valid on r-magic, returns the integer value of the REGEX stored
834in the MAGIC.
835
1a52ab62
MB
836=back
837
85cf7f2e 838=head2 B::PVLV Methods
1a52ab62
MB
839
840=over 4
841
842=item TARGOFF
843
844=item TARGLEN
845
846=item TYPE
847
848=item TARG
849
850=back
851
85cf7f2e 852=head2 B::BM Methods
1a52ab62
MB
853
854=over 4
855
856=item USEFUL
857
858=item PREVIOUS
859
860=item RARE
861
862=item TABLE
863
864=back
865
3f03baed
FC
866=head2 B::REGEXP Methods
867
868=over 4
869
870=item REGEX
871
872=item precomp
873
6190dd99
FC
874=item qr_anoncv
875
3f03baed
FC
876=back
877
85cf7f2e 878=head2 B::GV Methods
1a52ab62
MB
879
880=over 4
881
87d7fd28
GS
882=item is_empty
883
884This method returns TRUE if the GP field of the GV is NULL.
885
1a52ab62
MB
886=item NAME
887
002b978b
RH
888=item SAFENAME
889
890This method returns the name of the glob, but if the first
891character of the name is a control character, then it converts
892it to ^X first, so that *^G would return "^G" rather than "\cG".
893
894It's useful if you want to print out the name of a variable.
895If you restrict yourself to globs which exist at compile-time
896then the result ought to be unambiguous, because code like
897C<${"^G"} = 1> is compiled as two ops - a constant string and
898a dereference (rv2gv) - so that the glob is created at runtime.
899
900If you're working with globs at runtime, and need to disambiguate
901*^G from *{"^G"}, then you should use the raw NAME method.
902
1a52ab62
MB
903=item STASH
904
905=item SV
906
907=item IO
908
909=item FORM
910
911=item AV
912
913=item HV
914
915=item EGV
916
917=item CV
918
919=item CVGEN
920
921=item LINE
922
b195d487
GS
923=item FILE
924
1a52ab62
MB
925=item FILEGV
926
927=item GvREFCNT
928
929=item FLAGS
930
8f4983af
FC
931=item GPFLAGS
932
933This last one is present only in perl 5.22.0 and higher.
934
1a52ab62
MB
935=back
936
85cf7f2e 937=head2 B::IO Methods
1a52ab62 938
8b858c71
FC
939B::IO objects derive from IO objects and you will get more information from
940the IO object itself.
44f7f2d5
RU
941
942For example:
943
944 $gvio = B::svref_2object(\*main::stdin)->IO;
945 $IO = $gvio->object_2svref();
946 $fd = $IO->fileno();
947
1a52ab62
MB
948=over 4
949
950=item LINES
951
952=item PAGE
953
954=item PAGE_LEN
955
956=item LINES_LEFT
957
958=item TOP_NAME
959
960=item TOP_GV
961
962=item FMT_NAME
963
964=item FMT_GV
965
966=item BOTTOM_NAME
967
968=item BOTTOM_GV
969
970=item SUBPROCESS
971
972=item IoTYPE
973
44f7f2d5
RU
974A character symbolizing the type of IO Handle.
975
976 - STDIN/OUT
977 I STDIN/OUT/ERR
978 < read-only
979 > write-only
980 a append
981 + read and write
982 s socket
983 | pipe
984 I IMPLICIT
985 # NUMERIC
986 space closed handle
987 \0 closed internal handle
988
1a52ab62
MB
989=item IoFLAGS
990
9d2bbe64
MB
991=item IsSTD
992
44f7f2d5 993Takes one argument ( 'stdin' | 'stdout' | 'stderr' ) and returns true
9d2bbe64 994if the IoIFP of the object is equal to the handle whose name was
8b858c71 995passed as argument; i.e., $io->IsSTD('stderr') is true if
44f7f2d5 996IoIFP($io) == PerlIO_stderr().
9d2bbe64 997
1a52ab62
MB
998=back
999
85cf7f2e 1000=head2 B::AV Methods
1a52ab62
MB
1001
1002=over 4
1003
1004=item FILL
1005
1006=item MAX
1007
1a52ab62
MB
1008=item ARRAY
1009
429a5ce7
SM
1010=item ARRAYelt
1011
1012Like C<ARRAY>, but takes an index as an argument to get only one element,
1013rather than a list of all of them.
1014
edcc7c74
NC
1015=item OFF
1016
1017This method is deprecated if running under Perl 5.8, and is no longer present
1018if running under Perl 5.9
1019
1020=item AvFLAGS
1021
130592f5
FC
1022This method returns the AV specific
1023flags. In Perl 5.9 these are now stored
edcc7c74
NC
1024in with the main SV flags, so this method is no longer present.
1025
1a52ab62
MB
1026=back
1027
85cf7f2e 1028=head2 B::CV Methods
1a52ab62
MB
1029
1030=over 4
1031
1032=item STASH
1033
1034=item START
1035
1036=item ROOT
1037
1038=item GV
1039
57843af0
GS
1040=item FILE
1041
1a52ab62
MB
1042=item DEPTH
1043
1044=item PADLIST
1045
3a910aa0
FC
1046Returns a B::PADLIST object under Perl 5.18 or higher, or a B::AV in
1047earlier versions.
1048
1a52ab62
MB
1049=item OUTSIDE
1050
a3985cdc
DM
1051=item OUTSIDE_SEQ
1052
1a52ab62
MB
1053=item XSUB
1054
1055=item XSUBANY
1056
9d2bbe64
MB
1057For constant subroutines, returns the constant SV returned by the subroutine.
1058
5cfd8ad4
VB
1059=item CvFLAGS
1060
de3f1649
JT
1061=item const_sv
1062
486b1e7f
TC
1063=item NAME_HEK
1064
1065Returns the name of a lexical sub, otherwise C<undef>.
1066
1a52ab62
MB
1067=back
1068
85cf7f2e 1069=head2 B::HV Methods
1a52ab62
MB
1070
1071=over 4
1072
1073=item FILL
1074
1075=item MAX
1076
1077=item KEYS
1078
1079=item RITER
1080
1081=item NAME
1082
1a52ab62
MB
1083=item ARRAY
1084
edcc7c74
NC
1085=item PMROOT
1086
1087This method is not present if running under Perl 5.9, as the PMROOT
1088information is no longer stored directly in the hash.
1089
1a52ab62
MB
1090=back
1091
1092=head2 OP-RELATED CLASSES
1093
2f7c6295
DM
1094C<B::OP>, C<B::UNOP>, C<B::UNOP_AUX>, C<B::BINOP>, C<B::LOGOP>,
1095C<B::LISTOP>, C<B::PMOP>, C<B::SVOP>, C<B::PADOP>, C<B::PVOP>, C<B::LOOP>,
1096C<B::COP>, C<B::METHOP>.
85cf7f2e
MJD
1097
1098These classes correspond in the obvious way to the underlying C
130592f5 1099structures of similar names. The inheritance hierarchy mimics the
85cf7f2e
MJD
1100underlying C "inheritance":
1101
1102 B::OP
1103 |
b46e009d 1104 +----------+---------+--------+-------+---------+
1105 | | | | | |
1106 B::UNOP B::SVOP B::PADOP B::COP B::PVOP B::METHOP
2f7c6295
DM
1107 |
1108 +---+---+---------+
1109 | | |
1110 B::BINOP B::LOGOP B::UNOP_AUX
85cf7f2e
MJD
1111 |
1112 |
1113 B::LISTOP
2f7c6295
DM
1114 |
1115 +---+---+
1116 | |
1117 B::LOOP B::PMOP
85cf7f2e 1118
b84c7839 1119Access methods correspond to the underlying C structure field names,
85cf7f2e
MJD
1120with the leading "class indication" prefix (C<"op_">) removed.
1121
1122=head2 B::OP Methods
1a52ab62 1123
a60ba18b
JC
1124These methods get the values of similarly named fields within the OP
1125data structure. See top of C<op.h> for more info.
1126
1a52ab62
MB
1127=over 4
1128
1129=item next
1130
1131=item sibling
1132
29e61fd9
DM
1133=item parent
1134
1135Returns the OP's parent. If it has no parent, or if your perl wasn't built
1136with C<-DPERL_OP_PARENT>, returns NULL.
1137
3f872cb9
GS
1138=item name
1139
1140This returns the op name as a string (e.g. "add", "rv2av").
1141
1a52ab62
MB
1142=item ppaddr
1143
dc333d64
GS
1144This returns the function name as a string (e.g. "PL_ppaddr[OP_ADD]",
1145"PL_ppaddr[OP_RV2AV]").
1a52ab62
MB
1146
1147=item desc
1148
4369b173 1149This returns the op description from the global C PL_op_desc array
1a52ab62
MB
1150(e.g. "addition" "array deref").
1151
1152=item targ
1153
1154=item type
1155
a60ba18b
JC
1156=item opt
1157
1a52ab62
MB
1158=item flags
1159
1160=item private
1161
a60ba18b
JC
1162=item spare
1163
1a52ab62
MB
1164=back
1165
18aceeb0 1166=head2 B::UNOP Method
1a52ab62
MB
1167
1168=over 4
1169
1170=item first
1171
1172=back
1173
18aceeb0 1174=head2 B::UNOP_AUX Methods (since 5.22)
2f7c6295
DM
1175
1176=over 4
1177
1178=item aux_list(cv)
1179
1180This returns a list of the elements of the op's aux data structure,
1181or a null list if there is no aux. What will be returned depends on the
1182object's type, but will typically be a collection of C<B::IV>, C<B::GV>,
1183etc. objects. C<cv> is the C<B::CV> object representing the sub that the
1184op is contained within.
1185
1186=item string(cv)
1187
1188This returns a textual representation of the object (likely to b useful
1189for deparsing and debugging), or an empty string if the op type doesn't
1190support this. C<cv> is the C<B::CV> object representing the sub that the
1191op is contained within.
1192
1193=back
1194
18aceeb0 1195=head2 B::BINOP Method
1a52ab62
MB
1196
1197=over 4
1198
1199=item last
1200
1201=back
1202
18aceeb0 1203=head2 B::LOGOP Method
1a52ab62
MB
1204
1205=over 4
1206
1207=item other
1208
1209=back
1210
18aceeb0 1211=head2 B::LISTOP Method
1a52ab62
MB
1212
1213=over 4
1214
1215=item children
1216
1217=back
1218
85cf7f2e 1219=head2 B::PMOP Methods
1a52ab62
MB
1220
1221=over 4
1222
1223=item pmreplroot
1224
1225=item pmreplstart
1226
1227=item pmnext
1228
196d796c
RU
1229Only up to Perl 5.9.4
1230
1a52ab62
MB
1231=item pmflags
1232
c737faaf 1233=item extflags
1a52ab62 1234
196d796c
RU
1235Since Perl 5.9.5
1236
1a52ab62
MB
1237=item precomp
1238
651aa52e 1239=item pmoffset
9d2bbe64
MB
1240
1241Only when perl was compiled with ithreads.
1242
e07bb516
DM
1243=item code_list
1244
1245Since perl 5.17.1
1246
429ba3b2
FC
1247=item pmregexp
1248
1249Added in perl 5.22, this method returns the B::REGEXP associated with the
1250op. While PMOPs do not actually have C<pmregexp> fields under threaded
1251builds, this method returns the regexp under threads nonetheless, for
1252convenience.
1253
1a52ab62
MB
1254=back
1255
18aceeb0 1256=head2 B::SVOP Methods
1a52ab62
MB
1257
1258=over 4
1259
1260=item sv
1261
065a1863
GS
1262=item gv
1263
1a52ab62
MB
1264=back
1265
18aceeb0 1266=head2 B::PADOP Method
1a52ab62
MB
1267
1268=over 4
1269
7934575e 1270=item padix
1a52ab62
MB
1271
1272=back
1273
18aceeb0 1274=head2 B::PVOP Method
1a52ab62
MB
1275
1276=over 4
1277
1278=item pv
1279
1280=back
1281
85cf7f2e 1282=head2 B::LOOP Methods
1a52ab62
MB
1283
1284=over 4
1285
1286=item redoop
1287
1288=item nextop
1289
1290=item lastop
1291
1292=back
1293
85cf7f2e 1294=head2 B::COP Methods
1a52ab62 1295
a379c8cb
FC
1296The C<B::COP> class is used for "nextstate" and "dbstate" ops. As of Perl
12975.22, it is also used for "null" ops that started out as COPs.
1298
1a52ab62
MB
1299=over 4
1300
1301=item label
1302
1303=item stash
1304
6e6a1aef
RGS
1305=item stashpv
1306
a60c099b 1307=item stashoff (threaded only)
8df2993f 1308
57843af0 1309=item file
1a52ab62
MB
1310
1311=item cop_seq
1312
1313=item arybase
1314
1315=item line
1316
6e6a1aef
RGS
1317=item warnings
1318
1319=item io
1320
d5ec2987
NC
1321=item hints
1322
b47e7f93
RGS
1323=item hints_hash
1324
1a52ab62
MB
1325=back
1326
b46e009d 1327=head2 B::METHOP Methods (Since Perl 5.22)
1328
1329=over 4
1330
1331=item first
1332
1333=item meth_sv
1334
1335=back
1336
9b7476d7 1337=head2 PAD-RELATED CLASSES
3a910aa0 1338
9b7476d7 1339Perl 5.18 introduced a new class, B::PADLIST, returned by B::CV's
3a910aa0
FC
1340C<PADLIST> method.
1341
9b7476d7
FC
1342Perl 5.22 introduced the B::PADNAMELIST and B::PADNAME classes.
1343
3a910aa0
FC
1344=head2 B::PADLIST Methods
1345
1346=over 4
1347
1348=item MAX
1349
1350=item ARRAY
1351
9b7476d7
FC
1352A list of pads. The first one contains the names.
1353
1354The first one is a B::PADNAMELIST under Perl 5.22, and a B::AV under
1355earlier versions. The rest are currently B::AV objects, but that could
1356change in future versions.
3a910aa0
FC
1357
1358=item ARRAYelt
1359
1360Like C<ARRAY>, but takes an index as an argument to get only one element,
1361rather than a list of all of them.
1362
9b7476d7
FC
1363=item NAMES
1364
1365This method, introduced in 5.22, returns the B::PADNAMELIST. It is
1366equivalent to C<ARRAYelt> with a 0 argument.
1367
1368=item REFCNT
1369
1370=back
1371
1372=head2 B::PADNAMELIST Methods
1373
1374=over 4
1375
1376=item MAX
1377
1378=item ARRAY
1379
1380=item ARRAYelt
1381
aa572f37
FC
1382These two methods return the pad names, using B::SPECIAL objects for null
1383pointers and B::PADNAME objects otherwise.
1384
3a910aa0
FC
1385=item REFCNT
1386
1387=back
7f20e9dd 1388
dbac5ffe
FC
1389=head2 B::PADNAME Methods
1390
1391=over 4
1392
1393=item PV
1394
1395=item PVX
1396
1397=item LEN
1398
1399=item REFCNT
1400
1401=item FLAGS
1402
1403For backward-compatibility, if the PADNAMEt_OUTER flag is set, the FLAGS
1404method adds the SVf_FAKE flag, too.
1405
1406=item TYPE
1407
1408A B::HV object representing the stash for a typed lexical.
1409
1410=item SvSTASH
1411
1412A backward-compatibility alias for TYPE.
1413
1414=item OURSTASH
1415
1416A B::HV object representing the stash for 'our' variables.
1417
1418=item PROTOCV
1419
1420The prototype CV for a 'my' sub.
1421
1422=item COP_SEQ_RANGE_LOW
1423
1424=item COP_SEQ_RANGE_HIGH
1425
1426Sequence numbers representing the scope within which a lexical is visible.
1427Meaningless if PADNAMEt_OUTER is set.
1428
1429=item PARENT_PAD_INDEX
1430
1431Only meaningful if PADNAMEt_OUTER is set.
1432
1433=item PARENT_FAKELEX_FLAGS
1434
1435Only meaningful if PADNAMEt_OUTER is set.
1436
1437=back
1438
71324a3b
DM
1439=head2 $B::overlay
1440
1441Although the optree is read-only, there is an overlay facility that allows
1442you to override what values the various B::*OP methods return for a
1443particular op. C<$B::overlay> should be set to reference a two-deep hash:
1444indexed by OP address, then method name. Whenever a an op method is
1445called, the value in the hash is returned if it exists. This facility is
1446used by B::Deparse to "undo" some optimisations. For example:
1447
1448
1449 local $B::overlay = {};
1450 ...
1451 if ($op->name eq "foo") {
1452 $B::overlay->{$$op} = {
1453 name => 'bar',
1454 next => $op->next->next,
1455 };
1456 }
1457 ...
1458 $op->name # returns "bar"
1459 $op->next # returns the next op but one
1460
1461
7f20e9dd
GS
1462=head1 AUTHOR
1463
1464Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
1465
1466=cut