This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Concise: improve hints decoding
[perl5.git] / ext / B / B / Concise.pm
CommitLineData
c99ca59a 1package B::Concise;
c27ea44e 2# Copyright (C) 2000-2003 Stephen McCamant. All rights reserved.
c99ca59a
SM
3# This program is free software; you can redistribute and/or modify it
4# under the same terms as Perl itself.
5
8ec8fbef
SM
6# Note: we need to keep track of how many use declarations/BEGIN
7# blocks this module uses, so we can avoid printing them when user
8# asks for the BEGIN blocks in her program. Update the comments and
9# the count in concise_specials if you add or delete one. The
10# -MO=Concise counts as use #1.
78ad9108 11
8ec8fbef
SM
12use strict; # use #2
13use warnings; # uses #3 and #4, since warnings uses Carp
78ad9108 14
8ec8fbef
SM
15use Exporter (); # use #5
16
7c2833f3 17our $VERSION = "0.998";
78ad9108 18our @ISA = qw(Exporter);
cc02ea56
JC
19our @EXPORT_OK = qw( set_style set_style_standard add_callback
20 concise_subref concise_cv concise_main
21 add_style walk_output compile reset_sequence );
22our %EXPORT_TAGS =
23 ( io => [qw( walk_output compile reset_sequence )],
24 style => [qw( add_style set_style_standard )],
25 cb => [qw( add_callback )],
26 mech => [qw( concise_subref concise_cv concise_main )], );
78ad9108 27
8ec8fbef 28# use #6
c99ca59a 29use B qw(class ppname main_start main_root main_cv cstring svref_2object
6a077020 30 SVf_IOK SVf_NOK SVf_POK SVf_IVisUV SVf_FAKE OPf_KIDS OPf_SPECIAL
4df7f6af 31 CVf_ANON PAD_FAKELEX_ANON PAD_FAKELEX_MULTI SVf_ROK);
c99ca59a 32
f95e3c3c 33my %style =
c99ca59a 34 ("terse" =>
c3caa09d
SM
35 ["(?(#label =>\n)?)(*( )*)#class (#addr) #name (?([#targ])?) "
36 . "#svclass~(?((#svaddr))?)~#svval~(?(label \"#coplabel\")?)\n",
c99ca59a
SM
37 "(*( )*)goto #class (#addr)\n",
38 "#class pp_#name"],
39 "concise" =>
d5ec2987
NC
40 ["#hyphseq2 (*( (x( ;)x))*)<#classsym> #exname#arg(?([#targarglife])?)"
41 . "~#flags(?(/#private)?)(?(:#hints)?)(x(;~->#next)x)\n"
cc02ea56 42 , " (*( )*) goto #seq\n",
c99ca59a
SM
43 "(?(<#seq>)?)#exname#arg(?([#targarglife])?)"],
44 "linenoise" =>
45 ["(x(;(*( )*))x)#noise#arg(?([#targarg])?)(x( ;\n)x)",
46 "gt_#seq ",
47 "(?(#seq)?)#noise#arg(?([#targarg])?)"],
48 "debug" =>
a854082c
MH
49 ["#class (#addr)\n\top_next\t\t#nextaddr\n\t(?(op_other\t#otheraddr\n\t)?)"
50 . "op_sibling\t#sibaddr\n\t"
35633035 51 . "op_ppaddr\tPL_ppaddr[OP_#NAME]\n\top_type\t\t#typenum\n"
d5ec2987 52 . "\top_flags\t#flagval\n\top_private\t#privval\t#hintsval\n"
c99ca59a
SM
53 . "(?(\top_first\t#firstaddr\n)?)(?(\top_last\t\t#lastaddr\n)?)"
54 . "(?(\top_sv\t\t#svaddr\n)?)",
55 " GOTO #addr\n",
56 "#addr"],
57 "env" => [$ENV{B_CONCISE_FORMAT}, $ENV{B_CONCISE_GOTO_FORMAT},
58 $ENV{B_CONCISE_TREE_FORMAT}],
59 );
60
724aa791
JC
61# Renderings, ie how Concise prints, is controlled by these vars
62# primary:
63our $stylename; # selects current style from %style
64my $order = "basic"; # how optree is walked & printed: basic, exec, tree
65
66# rendering mechanics:
67# these 'formats' are the line-rendering templates
68# they're updated from %style when $stylename changes
69my ($format, $gotofmt, $treefmt);
70
71# lesser players:
72my $base = 36; # how <sequence#> is displayed
73my $big_endian = 1; # more <sequence#> display
74my $tree_style = 0; # tree-order details
75my $banner = 1; # print banner before optree is traversed
cc02ea56 76my $do_main = 0; # force printing of main routine
f18deeb9 77my $show_src; # show source code
724aa791 78
cc02ea56 79# another factor: can affect all styles!
724aa791
JC
80our @callbacks; # allow external management
81
82set_style_standard("concise");
83
c99ca59a 84my $curcv;
c27ea44e 85my $cop_seq_base;
78ad9108
PJ
86
87sub set_style {
88 ($format, $gotofmt, $treefmt) = @_;
724aa791 89 #warn "set_style: deprecated, use set_style_standard instead\n"; # someday
f95e3c3c
JC
90 die "expecting 3 style-format args\n" unless @_ == 3;
91}
92
93sub add_style {
94 my ($newstyle,@args) = @_;
95 die "style '$newstyle' already exists, choose a new name\n"
96 if exists $style{$newstyle};
97 die "expecting 3 style-format args\n" unless @args == 3;
98 $style{$newstyle} = [@args];
724aa791 99 $stylename = $newstyle; # update rendering state
78ad9108
PJ
100}
101
31b49ad4 102sub set_style_standard {
724aa791 103 ($stylename) = @_; # update rendering state
f95e3c3c
JC
104 die "err: style '$stylename' unknown\n" unless exists $style{$stylename};
105 set_style(@{$style{$stylename}});
31b49ad4
SM
106}
107
78ad9108
PJ
108sub add_callback {
109 push @callbacks, @_;
110}
c99ca59a 111
f95e3c3c 112# output handle, used with all Concise-output printing
cc02ea56
JC
113our $walkHandle; # public for your convenience
114BEGIN { $walkHandle = \*STDOUT }
f95e3c3c
JC
115
116sub walk_output { # updates $walkHandle
117 my $handle = shift;
cc02ea56
JC
118 return $walkHandle unless $handle; # allow use as accessor
119
f95e3c3c 120 if (ref $handle eq 'SCALAR') {
2ce64696
JC
121 require Config;
122 die "no perlio in this build, can't call walk_output (\\\$scalar)\n"
123 unless $Config::Config{useperlio};
f95e3c3c 124 # in 5.8+, open(FILEHANDLE,MODE,REFERENCE) writes to string
2ce64696 125 open my $tmp, '>', $handle; # but cant re-set existing STDOUT
f95e3c3c 126 $walkHandle = $tmp; # so use my $tmp as intermediate var
cc02ea56 127 return $walkHandle;
f95e3c3c 128 }
cc02ea56 129 my $iotype = ref $handle;
f95e3c3c 130 die "expecting argument/object that can print\n"
cc02ea56
JC
131 unless $iotype eq 'GLOB' or $iotype and $handle->can('print');
132 $walkHandle = $handle;
f95e3c3c
JC
133}
134
8ec8fbef 135sub concise_subref {
c0939cee 136 my($order, $coderef, $name) = @_;
f95e3c3c 137 my $codeobj = svref_2object($coderef);
cc02ea56 138
c0939cee 139 return concise_stashref(@_)
35f75594 140 unless ref($codeobj) =~ '^B::(?:CV|FM)\z';
c0939cee 141 concise_cv_obj($order, $codeobj, $name);
8ec8fbef
SM
142}
143
cc02ea56
JC
144sub concise_stashref {
145 my($order, $h) = @_;
6cc5d258 146 local *s;
cc02ea56 147 foreach my $k (sort keys %$h) {
6cc5d258
JC
148 next unless defined $h->{$k};
149 *s = $h->{$k};
cc02ea56
JC
150 my $coderef = *s{CODE} or next;
151 reset_sequence();
152 print "FUNC: ", *s, "\n";
153 my $codeobj = svref_2object($coderef);
154 next unless ref $codeobj eq 'B::CV';
6cc5d258
JC
155 eval { concise_cv_obj($order, $codeobj, $k) };
156 warn "err $@ on $codeobj" if $@;
cc02ea56
JC
157 }
158}
159
8ec8fbef
SM
160# This should have been called concise_subref, but it was exported
161# under this name in versions before 0.56
c0939cee 162*concise_cv = \&concise_subref;
8ec8fbef
SM
163
164sub concise_cv_obj {
c0939cee
JC
165 my ($order, $cv, $name) = @_;
166 # name is either a string, or a CODE ref (copy of $cv arg??)
167
c99ca59a 168 $curcv = $cv;
d51cf0c9 169
2018a5c3 170 if (ref($cv->XSUBANY) =~ /B::(\w+)/) {
d51cf0c9
JC
171 print $walkHandle "$name is a constant sub, optimized to a $1\n";
172 return;
173 }
c0939cee
JC
174 if ($cv->XSUB) {
175 print $walkHandle "$name is XS code\n";
176 return;
177 }
178 if (class($cv->START) eq "NULL") {
179 no strict 'refs';
180 if (ref $name eq 'CODE') {
181 print $walkHandle "coderef $name has no START\n";
182 }
183 elsif (exists &$name) {
e75702e9 184 print $walkHandle "$name exists in stash, but has no START\n";
c0939cee
JC
185 }
186 else {
187 print $walkHandle "$name not in symbol table\n";
188 }
189 return;
190 }
c27ea44e 191 sequence($cv->START);
c99ca59a
SM
192 if ($order eq "exec") {
193 walk_exec($cv->START);
c0939cee
JC
194 }
195 elsif ($order eq "basic") {
196 # walk_topdown($cv->ROOT, sub { $_[0]->concise($_[1]) }, 0);
197 my $root = $cv->ROOT;
198 unless (ref $root eq 'B::NULL') {
199 walk_topdown($root, sub { $_[0]->concise($_[1]) }, 0);
200 } else {
201 print $walkHandle "B::NULL encountered doing ROOT on $cv. avoiding disaster\n";
202 }
c99ca59a 203 } else {
f95e3c3c 204 print $walkHandle tree($cv->ROOT, 0);
c99ca59a
SM
205 }
206}
207
31b49ad4
SM
208sub concise_main {
209 my($order) = @_;
210 sequence(main_start);
211 $curcv = main_cv;
212 if ($order eq "exec") {
213 return if class(main_start) eq "NULL";
214 walk_exec(main_start);
215 } elsif ($order eq "tree") {
216 return if class(main_root) eq "NULL";
f95e3c3c 217 print $walkHandle tree(main_root, 0);
31b49ad4
SM
218 } elsif ($order eq "basic") {
219 return if class(main_root) eq "NULL";
220 walk_topdown(main_root,
221 sub { $_[0]->concise($_[1]) }, 0);
222 }
223}
224
8ec8fbef
SM
225sub concise_specials {
226 my($name, $order, @cv_s) = @_;
227 my $i = 1;
228 if ($name eq "BEGIN") {
c0939cee 229 splice(@cv_s, 0, 8); # skip 7 BEGIN blocks in this file. NOW 8 ??
8ec8fbef
SM
230 } elsif ($name eq "CHECK") {
231 pop @cv_s; # skip the CHECK block that calls us
232 }
f95e3c3c
JC
233 for my $cv (@cv_s) {
234 print $walkHandle "$name $i:\n";
8ec8fbef 235 $i++;
c0939cee 236 concise_cv_obj($order, $cv, $name);
8ec8fbef
SM
237 }
238}
239
c99ca59a
SM
240my $start_sym = "\e(0"; # "\cN" sometimes also works
241my $end_sym = "\e(B"; # "\cO" respectively
242
f95e3c3c 243my @tree_decorations =
c99ca59a
SM
244 ([" ", "--", "+-", "|-", "| ", "`-", "-", 1],
245 [" ", "-", "+", "+", "|", "`", "", 0],
246 [" ", map("$start_sym$_$end_sym", "qq", "wq", "tq", "x ", "mq", "q"), 1],
247 [" ", map("$start_sym$_$end_sym", "q", "w", "t", "x", "m"), "", 0],
248 );
78ad9108 249
9e0f9750
JC
250my @render_packs; # collect -stash=<packages>
251
cc02ea56
JC
252sub compileOpts {
253 # set rendering state from options and args
c0939cee
JC
254 my (@options,@args);
255 if (@_) {
256 @options = grep(/^-/, @_);
257 @args = grep(!/^-/, @_);
258 }
c99ca59a 259 for my $o (@options) {
cc02ea56 260 # mode/order
c99ca59a
SM
261 if ($o eq "-basic") {
262 $order = "basic";
263 } elsif ($o eq "-exec") {
264 $order = "exec";
265 } elsif ($o eq "-tree") {
266 $order = "tree";
cc02ea56
JC
267 }
268 # tree-specific
269 elsif ($o eq "-compact") {
c99ca59a
SM
270 $tree_style |= 1;
271 } elsif ($o eq "-loose") {
272 $tree_style &= ~1;
273 } elsif ($o eq "-vt") {
274 $tree_style |= 2;
275 } elsif ($o eq "-ascii") {
276 $tree_style &= ~2;
cc02ea56
JC
277 }
278 # sequence numbering
279 elsif ($o =~ /^-base(\d+)$/) {
c99ca59a
SM
280 $base = $1;
281 } elsif ($o eq "-bigendian") {
282 $big_endian = 1;
283 } elsif ($o eq "-littleendian") {
284 $big_endian = 0;
cc02ea56 285 }
9e0f9750 286 # miscellaneous, presentation
cc02ea56 287 elsif ($o eq "-nobanner") {
724aa791 288 $banner = 0;
cc02ea56
JC
289 } elsif ($o eq "-banner") {
290 $banner = 1;
291 }
292 elsif ($o eq "-main") {
293 $do_main = 1;
294 } elsif ($o eq "-nomain") {
295 $do_main = 0;
f18deeb9
JC
296 } elsif ($o eq "-src") {
297 $show_src = 1;
9e0f9750
JC
298 }
299 elsif ($o =~ /^-stash=(.*)/) {
300 my $pkg = $1;
301 no strict 'refs';
902fde96 302 if (! %{$pkg.'::'}) {
f667a15a
NC
303 eval "require $pkg";
304 } else {
305 require Config;
306 if (!$Config::Config{usedl}
307 && keys %{$pkg.'::'} == 1
308 && $pkg->can('bootstrap')) {
b7b1e41b 309 # It is something that we're statically linked to, but hasn't
f667a15a
NC
310 # yet been used.
311 eval "require $pkg";
312 }
313 }
9e0f9750 314 push @render_packs, $pkg;
724aa791 315 }
cc02ea56 316 # line-style options
724aa791 317 elsif (exists $style{substr($o, 1)}) {
f95e3c3c 318 $stylename = substr($o, 1);
724aa791 319 set_style_standard($stylename);
c99ca59a
SM
320 } else {
321 warn "Option $o unrecognized";
322 }
323 }
cc02ea56
JC
324 return (@args);
325}
326
327sub compile {
328 my (@args) = compileOpts(@_);
c27ea44e 329 return sub {
cc02ea56
JC
330 my @newargs = compileOpts(@_); # accept new rendering options
331 warn "disregarding non-options: @newargs\n" if @newargs;
332
333 for my $objname (@args) {
59910b6d
JC
334 next unless $objname; # skip null args to avoid noisy responses
335
cc02ea56
JC
336 if ($objname eq "BEGIN") {
337 concise_specials("BEGIN", $order,
c0939cee
JC
338 B::begin_av->isa("B::AV") ?
339 B::begin_av->ARRAY : ());
cc02ea56
JC
340 } elsif ($objname eq "INIT") {
341 concise_specials("INIT", $order,
c0939cee
JC
342 B::init_av->isa("B::AV") ?
343 B::init_av->ARRAY : ());
cc02ea56
JC
344 } elsif ($objname eq "CHECK") {
345 concise_specials("CHECK", $order,
c0939cee
JC
346 B::check_av->isa("B::AV") ?
347 B::check_av->ARRAY : ());
676456c2
AG
348 } elsif ($objname eq "UNITCHECK") {
349 concise_specials("UNITCHECK", $order,
350 B::unitcheck_av->isa("B::AV") ?
351 B::unitcheck_av->ARRAY : ());
cc02ea56
JC
352 } elsif ($objname eq "END") {
353 concise_specials("END", $order,
c0939cee
JC
354 B::end_av->isa("B::AV") ?
355 B::end_av->ARRAY : ());
cc02ea56
JC
356 }
357 else {
358 # convert function names to subrefs
cc02ea56
JC
359 if (ref $objname) {
360 print $walkHandle "B::Concise::compile($objname)\n"
361 if $banner;
35f75594
FC
362 concise_subref($order, ($objname)x2);
363 next;
8ec8fbef 364 } else {
cc02ea56 365 $objname = "main::" . $objname unless $objname =~ /::/;
cc02ea56 366 no strict 'refs';
35f75594
FC
367 my $glob = \*$objname;
368 unless (*$glob{CODE} || *$glob{FORMAT}) {
369 print $walkHandle "$objname:\n" if $banner;
c0939cee
JC
370 print $walkHandle "err: unknown function ($objname)\n";
371 return;
372 }
35f75594
FC
373 if (my $objref = *$glob{CODE}) {
374 print $walkHandle "$objname:\n" if $banner;
375 concise_subref($order, $objref, $objname);
376 }
377 if (my $objref = *$glob{FORMAT}) {
378 print $walkHandle "$objname (FORMAT):\n"
379 if $banner;
380 concise_subref($order, $objref, $objname);
381 }
8ec8fbef 382 }
c99ca59a
SM
383 }
384 }
9e0f9750
JC
385 for my $pkg (@render_packs) {
386 no strict 'refs';
387 concise_stashref($order, \%{$pkg.'::'});
388 }
389
390 if (!@args or $do_main or @render_packs) {
f95e3c3c 391 print $walkHandle "main program:\n" if $do_main;
31b49ad4 392 concise_main($order);
c99ca59a 393 }
cc02ea56 394 return @args; # something
c99ca59a
SM
395 }
396}
397
398my %labels;
724aa791 399my $lastnext; # remembers op-chain, used to insert gotos
c99ca59a
SM
400
401my %opclass = ('OP' => "0", 'UNOP' => "1", 'BINOP' => "2", 'LOGOP' => "|",
402 'LISTOP' => "@", 'PMOP' => "/", 'SVOP' => "\$", 'GVOP' => "*",
b46e009d 403 'PVOP' => '"', 'LOOP' => "{", 'COP' => ";", 'PADOP' => "#",
2f7c6295 404 'METHOP' => '.', UNOP_AUX => '+');
c99ca59a 405
8ec8fbef 406no warnings 'qw'; # "Possible attempt to put comments..."; use #7
35fc55f1
RH
407my @linenoise =
408 qw'# () sc ( @? 1 $* gv *{ m$ m@ m% m? p/ *$ $ $# & a& pt \\ s\\ rf bl
c99ca59a
SM
409 ` *? <> ?? ?/ r/ c/ // qr s/ /c y/ = @= C sC Cp sp df un BM po +1 +I
410 -1 -I 1+ I+ 1- I- ** * i* / i/ %$ i% x + i+ - i- . " << >> < i<
411 > i> <= i, >= i. == i= != i! <? i? s< s> s, s. s= s! s? b& b^ b| -0 -i
412 ! ~ a2 si cs rd sr e^ lg sq in %x %o ab le ss ve ix ri sf FL od ch cy
413 uf lf uc lc qm @ [f [ @[ eh vl ky dl ex % ${ @{ uk pk st jn ) )[ a@
414 a% sl +] -] [- [+ so rv GS GW MS MW .. f. .f && || ^^ ?: &= |= -> s{ s}
415 v} ca wa di rs ;; ; ;d }{ { } {} f{ it {l l} rt }l }n }r dm }g }e ^o
416 ^c ^| ^# um bm t~ u~ ~d DB db ^s se ^g ^r {w }w pf pr ^O ^K ^R ^W ^d ^v
417 ^e ^t ^k t. fc ic fl .s .p .b .c .l .a .h g1 s1 g2 s2 ?. l? -R -W -X -r
418 -w -x -e -o -O -z -s -M -A -C -S -c -b -f -d -p -l -u -g -k -t -T -B cd
419 co cr u. cm ut r. l@ s@ r@ mD uD oD rD tD sD wD cD f$ w$ p$ sh e$ k$ g3
420 g4 s4 g5 s5 T@ C@ L@ G@ A@ S@ Hg Hc Hr Hw Mg Mc Ms Mr Sg Sc So rq do {e
421 e} {t t} g6 G6 6e g7 G7 7e g8 G8 8e g9 G9 9e 6s 7s 8s 9s 6E 7E 8E 9E Pn
c27ea44e 422 Pu GP SP EP Gn Gg GG SG EG g0 c$ lk t$ ;s n> // /= CO';
c99ca59a
SM
423
424my $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
425
19e169bf 426sub op_flags { # common flags (see BASOP.op_flags in op.h)
c99ca59a
SM
427 my($x) = @_;
428 my(@v);
429 push @v, "v" if ($x & 3) == 1;
430 push @v, "s" if ($x & 3) == 2;
431 push @v, "l" if ($x & 3) == 3;
432 push @v, "K" if $x & 4;
433 push @v, "P" if $x & 8;
434 push @v, "R" if $x & 16;
435 push @v, "M" if $x & 32;
436 push @v, "S" if $x & 64;
437 push @v, "*" if $x & 128;
438 return join("", @v);
439}
440
441sub base_n {
442 my $x = shift;
443 return "-" . base_n(-$x) if $x < 0;
444 my $str = "";
445 do { $str .= substr($chars, $x % $base, 1) } while $x = int($x / $base);
446 $str = reverse $str if $big_endian;
447 return $str;
448}
449
c27ea44e
SM
450my %sequence_num;
451my $seq_max = 1;
452
f95e3c3c
JC
453sub reset_sequence {
454 # reset the sequence
455 %sequence_num = ();
456 $seq_max = 1;
cc02ea56 457 $lastnext = 0;
f95e3c3c
JC
458}
459
c27ea44e
SM
460sub seq {
461 my($op) = @_;
462 return "-" if not exists $sequence_num{$$op};
463 return base_n($sequence_num{$$op});
464}
c99ca59a
SM
465
466sub walk_topdown {
467 my($op, $sub, $level) = @_;
468 $sub->($op, $level);
469 if ($op->flags & OPf_KIDS) {
470 for (my $kid = $op->first; $$kid; $kid = $kid->sibling) {
471 walk_topdown($kid, $sub, $level + 1);
472 }
473 }
98517ccb 474 if (class($op) eq "PMOP") {
3d9ca043
FC
475 my $maybe_root = $op->code_list;
476 if ( ref($maybe_root) and $maybe_root->isa("B::OP")
477 and not $op->flags & OPf_KIDS) {
478 walk_topdown($maybe_root, $sub, $level + 1);
479 }
480 $maybe_root = $op->pmreplroot;
c6e79e55
SM
481 if (ref($maybe_root) and $maybe_root->isa("B::OP")) {
482 # It really is the root of the replacement, not something
483 # else stored here for lack of space elsewhere
484 walk_topdown($maybe_root, $sub, $level + 1);
485 }
c99ca59a
SM
486 }
487}
488
489sub walklines {
490 my($ar, $level) = @_;
491 for my $l (@$ar) {
492 if (ref($l) eq "ARRAY") {
493 walklines($l, $level + 1);
494 } else {
495 $l->concise($level);
496 }
497 }
498}
499
500sub walk_exec {
501 my($top, $level) = @_;
502 my %opsseen;
503 my @lines;
504 my @todo = ([$top, \@lines]);
505 while (@todo and my($op, $targ) = @{shift @todo}) {
506 for (; $$op; $op = $op->next) {
507 last if $opsseen{$$op}++;
508 push @$targ, $op;
509 my $name = $op->name;
62e36f8a 510 if (class($op) eq "LOGOP") {
c99ca59a
SM
511 my $ar = [];
512 push @$targ, $ar;
513 push @todo, [$op->other, $ar];
514 } elsif ($name eq "subst" and $ {$op->pmreplstart}) {
515 my $ar = [];
516 push @$targ, $ar;
517 push @todo, [$op->pmreplstart, $ar];
518 } elsif ($name =~ /^enter(loop|iter)$/) {
35633035
DM
519 $labels{${$op->nextop}} = "NEXT";
520 $labels{${$op->lastop}} = "LAST";
521 $labels{${$op->redoop}} = "REDO";
c99ca59a
SM
522 }
523 }
524 }
525 walklines(\@lines, 0);
526}
527
c27ea44e
SM
528# The structure of this routine is purposely modeled after op.c's peep()
529sub sequence {
530 my($op) = @_;
531 my $oldop = 0;
532 return if class($op) eq "NULL" or exists $sequence_num{$$op};
533 for (; $$op; $op = $op->next) {
534 last if exists $sequence_num{$$op};
535 my $name = $op->name;
1cd6d13a
FC
536 $sequence_num{$$op} = $seq_max++;
537 if (class($op) eq "LOGOP") {
538 sequence($op->other);
539 } elsif (class($op) eq "LOOP") {
540 sequence($op->redoop);
541 sequence( $op->nextop);
542 sequence($op->lastop);
543 } elsif ($name eq "subst" and $ {$op->pmreplstart}) {
544 sequence($op->pmreplstart);
c27ea44e
SM
545 }
546 $oldop = $op;
547 }
548}
549
724aa791 550sub fmt_line { # generate text-line for op.
cc02ea56
JC
551 my($hr, $op, $text, $level) = @_;
552
553 $_->($hr, $op, \$text, \$level, $stylename) for @callbacks;
554
724aa791 555 return '' if $hr->{SKIP}; # suppress line if a callback said so
cc02ea56 556 return '' if $hr->{goto} and $hr->{goto} eq '-'; # no goto nowhere
f95e3c3c 557
cc02ea56 558 # spec: (?(text1#varText2)?)
c99ca59a 559 $text =~ s/\(\?\(([^\#]*?)\#(\w+)([^\#]*?)\)\?\)/
f95e3c3c
JC
560 $hr->{$2} ? $1.$hr->{$2}.$3 : ""/eg;
561
cc02ea56 562 # spec: (x(exec_text;basic_text)x)
c99ca59a 563 $text =~ s/\(x\((.*?);(.*?)\)x\)/$order eq "exec" ? $1 : $2/egs;
cc02ea56
JC
564
565 # spec: (*(text)*)
c99ca59a 566 $text =~ s/\(\*\(([^;]*?)\)\*\)/$1 x $level/egs;
cc02ea56
JC
567
568 # spec: (*(text1;text2)*)
c99ca59a 569 $text =~ s/\(\*\((.*?);(.*?)\)\*\)/$1 x ($level - 1) . $2 x ($level>0)/egs;
cc02ea56
JC
570
571 # convert #Var to tag=>val form: Var\t#var
572 $text =~ s/\#([A-Z][a-z]+)(\d+)?/\t\u$1\t\L#$1$2/gs;
573
574 # spec: #varN
724aa791
JC
575 $text =~ s/\#([a-zA-Z]+)(\d+)/sprintf("%-$2s", $hr->{$1})/eg;
576
cc02ea56
JC
577 $text =~ s/\#([a-zA-Z]+)/$hr->{$1}/eg; # populate #var's
578 $text =~ s/[ \t]*~+[ \t]*/ /g; # squeeze tildes
f18deeb9
JC
579
580 $text = "# $hr->{src}\n$text" if $show_src and $hr->{src};
581
f95e3c3c 582 chomp $text;
efef081e 583 return "$text\n" if $text ne "" and $order ne "tree";
f95e3c3c 584 return $text; # suppress empty lines
c99ca59a
SM
585}
586
f3574cc6
DM
587
588
589# use require rather than use here to avoid disturbing tests that dump
590# BEGIN blocks
591require B::Op_private;
592
593
c99ca59a 594
d5ec2987
NC
595our %hints; # used to display each COP's op_hints values
596
597# strict refs, subs, vars
88cfd030 598@hints{0x2,0x200,0x400,0x20,0x40,0x80} = ('$', '&', '*', 'x$', 'x&', 'x*');
e1dccc0d 599# integers, locale, bytes
88cfd030 600@hints{0x1,0x4,0x8,0x10} = ('i', 'l', 'b');
8b850bd5 601# block scope, localise %^H, $^OPEN (in), $^OPEN (out)
88cfd030 602@hints{0x100,0x20000,0x40000,0x80000} = ('{','%','<','>');
d5ec2987 603# overload new integer, float, binary, string, re
88cfd030 604@hints{0x1000,0x2000,0x4000,0x8000,0x10000} = ('I', 'F', 'B', 'S', 'R');
d5ec2987 605# taint and eval
88cfd030 606@hints{0x100000,0x200000} = ('T', 'E');
7c2833f3
DM
607# filetest access, use utf8, unicode_strings feature
608@hints{0x400000,0x800000,0x800} = ('X', 'U', 'us');
d5ec2987 609
7c2833f3
DM
610# pick up the feature hints constants.
611# Note that we're relying on non-API parts of feature.pm,
612# but its less naughty than just blindly copying those constants into
613# this src file.
614#
615require feature;
616
617sub hints_flags {
618 my($x) = @_;
c99ca59a 619 my @s;
7c2833f3
DM
620 for my $flag (sort {$b <=> $a} keys %hints) {
621 if ($hints{$flag} and $x & $flag and $x >= $flag) {
c99ca59a 622 $x -= $flag;
7c2833f3 623 push @s, $hints{$flag};
c99ca59a
SM
624 }
625 }
7c2833f3
DM
626 if ($x & $feature::hint_mask) {
627 push @s, "fea=" . (($x & $feature::hint_mask) >> $feature::hint_shift);
628 $x &= ~$feature::hint_mask;
629 }
630 push @s, sprintf "0x%x", $x if $x;
c99ca59a
SM
631 return join(",", @s);
632}
633
7c2833f3 634
f3574cc6
DM
635# return a string like 'LVINTRO,1' for the op $name with op_private
636# value $x
637
d5ec2987
NC
638sub private_flags {
639 my($name, $x) = @_;
f3574cc6
DM
640 my $entry = $B::Op_private::bits{$name};
641 return $x ? "$x" : '' unless $entry;
642
643 my @flags;
644 my $bit;
645 for ($bit = 7; $bit >= 0; $bit--) {
646 next unless exists $entry->{$bit};
647 my $e = $entry->{$bit};
648 if (ref($e) eq 'HASH') {
649 # bit field
650
651 my ($bitmin, $bitmax, $bitmask, $enum, $label) =
652 @{$e}{qw(bitmin bitmax bitmask enum label)};
653 $bit = $bitmin;
654 next if defined $label && $label eq '-'; # display as raw number
655
656 my $val = $x & $bitmask;
657 $x &= ~$bitmask;
658 $val >>= $bitmin;
659
660 if (defined $enum) {
661 # try to convert numeric $val into symbolic
662 my @enum = @$enum;
663 while (@enum) {
664 my $ix = shift @enum;
665 my $name = shift @enum;
666 my $label = shift @enum;
667 if ($val == $ix) {
668 $val = $label;
669 last;
670 }
671 }
672 }
673 next if $val eq '0'; # don't display anonymous zero values
674 push @flags, defined $label ? "$label=$val" : $val;
675
676 }
677 else {
678 # flag bit
679 my $label = $B::Op_private::labels{$e};
680 next if defined $label && $label eq '-'; # display as raw number
681 if ($x & (1<<$bit)) {
682 $x -= (1<<$bit);
683 push @flags, $label;
684 }
685 }
686 }
687
688 push @flags, $x if $x; # display unknown bits numerically
689 return join ",", @flags;
d5ec2987
NC
690}
691
c27ea44e 692sub concise_sv {
2db5ca0a 693 my($sv, $hr, $preferpv) = @_;
c27ea44e 694 $hr->{svclass} = class($sv);
31b49ad4
SM
695 $hr->{svclass} = "UV"
696 if $hr->{svclass} eq "IV" and $sv->FLAGS & SVf_IVisUV;
5b493bdf 697 Carp::cluck("bad concise_sv: $sv") unless $sv and $$sv;
c27ea44e 698 $hr->{svaddr} = sprintf("%#x", $$sv);
50786ba8 699 if ($hr->{svclass} eq "GV" && $sv->isGV_with_GP()) {
c27ea44e 700 my $gv = $sv;
7b696247
FC
701 my $stash = $gv->STASH;
702 if (class($stash) eq "SPECIAL") {
703 $stash = "<none>";
704 }
705 else {
706 $stash = $stash->NAME;
707 }
708 if ($stash eq "main") {
c27ea44e
SM
709 $stash = "";
710 } else {
711 $stash = $stash . "::";
712 }
713 $hr->{svval} = "*$stash" . $gv->SAFENAME;
714 return "*$stash" . $gv->SAFENAME;
715 } else {
4df7f6af
NC
716 if ($] >= 5.011) {
717 while (class($sv) eq "IV" && $sv->FLAGS & SVf_ROK) {
718 $hr->{svval} .= "\\";
719 $sv = $sv->RV;
720 }
721 } else {
722 while (class($sv) eq "RV") {
723 $hr->{svval} .= "\\";
724 $sv = $sv->RV;
725 }
c27ea44e
SM
726 }
727 if (class($sv) eq "SPECIAL") {
40b5b14f 728 $hr->{svval} .= ["Null", "sv_undef", "sv_yes", "sv_no"]->[$$sv];
8d919b0a
FC
729 } elsif ($preferpv
730 && ($sv->FLAGS & SVf_POK || class($sv) eq "REGEXP")) {
2db5ca0a 731 $hr->{svval} .= cstring($sv->PV);
c27ea44e 732 } elsif ($sv->FLAGS & SVf_NOK) {
40b5b14f 733 $hr->{svval} .= $sv->NV;
c27ea44e 734 } elsif ($sv->FLAGS & SVf_IOK) {
31b49ad4 735 $hr->{svval} .= $sv->int_value;
8d919b0a 736 } elsif ($sv->FLAGS & SVf_POK || class($sv) eq "REGEXP") {
40b5b14f 737 $hr->{svval} .= cstring($sv->PV);
31b49ad4
SM
738 } elsif (class($sv) eq "HV") {
739 $hr->{svval} .= 'HASH';
c27ea44e 740 }
cc02ea56
JC
741
742 $hr->{svval} = 'undef' unless defined $hr->{svval};
743 my $out = $hr->{svclass};
744 return $out .= " $hr->{svval}" ;
c27ea44e
SM
745 }
746}
747
f18deeb9
JC
748my %srclines;
749
750sub fill_srclines {
9e0f9750
JC
751 my $fullnm = shift;
752 if ($fullnm eq '-e') {
753 $srclines{$fullnm} = [ $fullnm, "-src not supported for -e" ];
754 return;
6cc5d258 755 }
9e0f9750 756 open (my $fh, '<', $fullnm)
6cc5d258 757 or warn "# $fullnm: $!, (chdirs not supported by this feature yet)\n"
f18deeb9
JC
758 and return;
759 my @l = <$fh>;
760 chomp @l;
9e0f9750
JC
761 unshift @l, $fullnm; # like @{_<$fullnm} in debug, array starts at 1
762 $srclines{$fullnm} = \@l;
f18deeb9
JC
763}
764
c99ca59a
SM
765sub concise_op {
766 my ($op, $level, $format) = @_;
767 my %h;
768 $h{exname} = $h{name} = $op->name;
769 $h{NAME} = uc $h{name};
770 $h{class} = class($op);
771 $h{extarg} = $h{targ} = $op->targ;
772 $h{extarg} = "" unless $h{extarg};
f3574cc6 773 $h{privval} = $op->private;
c51d52ee
FC
774 # for null ops, targ holds the old type
775 my $origname = $h{name} eq "null" && $h{targ}
776 ? substr(ppname($h{targ}), 3)
777 : $h{name};
778 $h{private} = private_flags($origname, $op->private);
f3574cc6
DM
779 if ($op->folded) {
780 $h{private} &&= "$h{private},";
781 $h{private} .= "FOLD";
782 }
783
c51d52ee
FC
784 if ($h{name} ne $origname) { # a null op
785 $h{exname} = "ex-$origname";
c99ca59a 786 $h{extarg} = "";
f3574cc6
DM
787 } elsif ($h{private} =~ /\bREFC\b/) {
788 # targ holds a reference count
789 my $refs = "ref" . ($h{targ} != 1 ? "s" : "");
790 $h{targarglife} = $h{targarg} = "$h{targ} $refs";
c99ca59a 791 } elsif ($h{targ}) {
f3574cc6
DM
792 my $count = $h{name} eq 'padrange'
793 ? ($op->private & $B::Op_private::defines{'OPpPADRANGE_COUNTMASK'})
794 : 1;
a7fd8ef6
DM
795 my (@targarg, @targarglife);
796 for my $i (0..$count-1) {
797 my ($targarg, $targarglife);
798 my $padname = (($curcv->PADLIST->ARRAY)[0]->ARRAY)[$h{targ}+$i];
53af8ccb
FC
799 if (defined $padname and class($padname) ne "SPECIAL" and
800 $padname->LEN)
801 {
a7fd8ef6
DM
802 $targarg = $padname->PVX;
803 if ($padname->FLAGS & SVf_FAKE) {
804 # These changes relate to the jumbo closure fix.
805 # See changes 19939 and 20005
806 my $fake = '';
807 $fake .= 'a'
808 if $padname->PARENT_FAKELEX_FLAGS & PAD_FAKELEX_ANON;
809 $fake .= 'm'
810 if $padname->PARENT_FAKELEX_FLAGS & PAD_FAKELEX_MULTI;
811 $fake .= ':' . $padname->PARENT_PAD_INDEX
812 if $curcv->CvFLAGS & CVf_ANON;
813 $targarglife = "$targarg:FAKE:$fake";
814 }
815 else {
816 my $intro = $padname->COP_SEQ_RANGE_LOW - $cop_seq_base;
817 my $finish = int($padname->COP_SEQ_RANGE_HIGH) - $cop_seq_base;
818 $finish = "end" if $finish == 999999999 - $cop_seq_base;
819 $targarglife = "$targarg:$intro,$finish";
820 }
821 } else {
822 $targarglife = $targarg = "t" . ($h{targ}+$i);
127212b2 823 }
a7fd8ef6
DM
824 push @targarg, $targarg;
825 push @targarglife, $targarglife;
c99ca59a 826 }
a7fd8ef6
DM
827 $h{targarg} = join '; ', @targarg;
828 $h{targarglife} = join '; ', @targarglife;
c99ca59a 829 }
4fa06845 830
c99ca59a
SM
831 $h{arg} = "";
832 $h{svclass} = $h{svaddr} = $h{svval} = "";
833 if ($h{class} eq "PMOP") {
2721a2ca 834 my $extra = '';
c99ca59a 835 my $precomp = $op->precomp;
7a9b44b9 836 if (defined $precomp) {
c27ea44e
SM
837 $precomp = cstring($precomp); # Escape literal control sequences
838 $precomp = "/$precomp/";
839 } else {
840 $precomp = "";
7a9b44b9 841 }
2721a2ca
DM
842 if ($op->name eq 'subst') {
843 if (class($op->pmreplstart) ne "NULL") {
844 undef $lastnext;
845 $extra = " replstart->" . seq($op->pmreplstart);
846 }
847 }
848 elsif ($op->name eq 'pushre') {
b2a3cfdd 849 # with C<@stash_array = split(/pat/, str);>,
c6e79e55 850 # *stash_array is stored in /pat/'s pmreplroot.
2721a2ca
DM
851 my $gv = $op->pmreplroot;
852 if (!ref($gv)) {
853 # threaded: the value is actually a pad offset for where
854 # the GV is kept (op_pmtargetoff)
855 if ($gv) {
856 $gv = (($curcv->PADLIST->ARRAY)[1]->ARRAY)[$gv]->NAME;
857 }
858 }
859 else {
860 # unthreaded: its a GV (if it exists)
861 $gv = (ref($gv) eq "B::GV") ? $gv->NAME : undef;
862 }
863 $extra = " => \@$gv" if $gv;
c99ca59a 864 }
2721a2ca 865 $h{arg} = "($precomp$extra)";
bb16bae8 866 } elsif ($h{class} eq "PVOP" and $h{name} !~ '^transr?\z') {
c99ca59a
SM
867 $h{arg} = '("' . $op->pv . '")';
868 $h{svval} = '"' . $op->pv . '"';
869 } elsif ($h{class} eq "COP") {
870 my $label = $op->label;
c3caa09d 871 $h{coplabel} = $label;
c99ca59a
SM
872 $label = $label ? "$label: " : "";
873 my $loc = $op->file;
9e0f9750 874 my $pathnm = $loc;
c99ca59a 875 $loc =~ s[.*/][];
9e0f9750
JC
876 my $ln = $op->line;
877 $loc .= ":$ln";
c99ca59a 878 my($stash, $cseq) = ($op->stash->NAME, $op->cop_seq - $cop_seq_base);
e1dccc0d 879 $h{arg} = "($label$stash $cseq $loc)";
f18deeb9 880 if ($show_src) {
9e0f9750 881 fill_srclines($pathnm) unless exists $srclines{$pathnm};
e9c69003
NC
882 # Would love to retain Jim's use of // but this code needs to be
883 # portable to 5.8.x
884 my $line = $srclines{$pathnm}[$ln];
885 $line = "-src unavailable under -e" unless defined $line;
886 $h{src} = "$ln: $line";
f18deeb9 887 }
c99ca59a
SM
888 } elsif ($h{class} eq "LOOP") {
889 $h{arg} = "(next->" . seq($op->nextop) . " last->" . seq($op->lastop)
890 . " redo->" . seq($op->redoop) . ")";
891 } elsif ($h{class} eq "LOGOP") {
892 undef $lastnext;
893 $h{arg} = "(other->" . seq($op->other) . ")";
a854082c 894 $h{otheraddr} = sprintf("%#x", $ {$op->other});
4fa06845
DM
895 if ($h{name} eq "argdefelem") {
896 # targ used for element index
897 $h{targarglife} = $h{targarg} = "";
898 $h{arg} .= "[" . $op->targ . "]";
899 }
5b493bdf
JC
900 }
901 elsif ($h{class} eq "SVOP" or $h{class} eq "PADOP") {
6a077020 902 unless ($h{name} eq 'aelemfast' and $op->flags & OPf_SPECIAL) {
5b493bdf
JC
903 my $idx = ($h{class} eq "SVOP") ? $op->targ : $op->padix;
904 if ($h{class} eq "PADOP" or !${$op->sv}) {
905 my $sv = (($curcv->PADLIST->ARRAY)[1]->ARRAY)[$idx];
b46e009d 906 $h{arg} = "[" . concise_sv($sv, \%h, 0) . "]";
6a077020
DM
907 $h{targarglife} = $h{targarg} = "";
908 } else {
b46e009d 909 $h{arg} = "(" . concise_sv($op->sv, \%h, 0) . ")";
6a077020 910 }
c99ca59a
SM
911 }
912 }
b46e009d 913 elsif ($h{class} eq "METHOP") {
810bd8b7 914 my $prefix = '';
915 if ($h{name} eq 'method_redir' or $h{name} eq 'method_redir_super') {
916 my $rclass_sv = $op->rclass;
917 $rclass_sv = (($curcv->PADLIST->ARRAY)[1]->ARRAY)[$rclass_sv]
918 unless ref $rclass_sv;
919 $prefix .= 'PACKAGE "'.$rclass_sv->PV.'", ';
920 }
7d6c333c 921 if ($h{name} ne "method") {
b46e009d 922 if (${$op->meth_sv}) {
810bd8b7 923 $h{arg} = "($prefix" . concise_sv($op->meth_sv, \%h, 1) . ")";
b46e009d 924 } else {
925 my $sv = (($curcv->PADLIST->ARRAY)[1]->ARRAY)[$op->targ];
810bd8b7 926 $h{arg} = "[$prefix" . concise_sv($sv, \%h, 1) . "]";
b46e009d 927 $h{targarglife} = $h{targarg} = "";
928 }
929 }
930 }
2f7c6295 931 elsif ($h{class} eq "UNOP_AUX") {
fedf30e1 932 $h{arg} = "(" . $op->string($curcv) . ")";
2f7c6295
DM
933 }
934
c99ca59a
SM
935 $h{seq} = $h{hyphseq} = seq($op);
936 $h{seq} = "" if $h{seq} eq "-";
35633035
DM
937 $h{opt} = $op->opt;
938 $h{label} = $labels{$$op};
c99ca59a
SM
939 $h{next} = $op->next;
940 $h{next} = (class($h{next}) eq "NULL") ? "(end)" : seq($h{next});
941 $h{nextaddr} = sprintf("%#x", $ {$op->next});
942 $h{sibaddr} = sprintf("%#x", $ {$op->sibling});
943 $h{firstaddr} = sprintf("%#x", $ {$op->first}) if $op->can("first");
944 $h{lastaddr} = sprintf("%#x", $ {$op->last}) if $op->can("last");
945
946 $h{classsym} = $opclass{$h{class}};
947 $h{flagval} = $op->flags;
948 $h{flags} = op_flags($op->flags);
d5ec2987
NC
949 if ($op->can("hints")) {
950 $h{hintsval} = $op->hints;
951 $h{hints} = hints_flags($h{hintsval});
952 } else {
953 $h{hintsval} = $h{hints} = '';
954 }
c99ca59a 955 $h{addr} = sprintf("%#x", $$op);
c99ca59a
SM
956 $h{typenum} = $op->type;
957 $h{noise} = $linenoise[$op->type];
f95e3c3c 958
cc02ea56 959 return fmt_line(\%h, $op, $format, $level);
c99ca59a
SM
960}
961
962sub B::OP::concise {
963 my($op, $level) = @_;
964 if ($order eq "exec" and $lastnext and $$lastnext != $$op) {
724aa791 965 # insert a 'goto' line
cc02ea56
JC
966 my $synth = {"seq" => seq($lastnext), "class" => class($lastnext),
967 "addr" => sprintf("%#x", $$lastnext),
968 "goto" => seq($lastnext), # simplify goto '-' removal
969 };
970 print $walkHandle fmt_line($synth, $op, $gotofmt, $level+1);
c99ca59a
SM
971 }
972 $lastnext = $op->next;
f95e3c3c 973 print $walkHandle concise_op($op, $level, $format);
c99ca59a
SM
974}
975
31b49ad4
SM
976# B::OP::terse (see Terse.pm) now just calls this
977sub b_terse {
978 my($op, $level) = @_;
979
980 # This isn't necessarily right, but there's no easy way to get
981 # from an OP to the right CV. This is a limitation of the
982 # ->terse() interface style, and there isn't much to do about
983 # it. In particular, we can die in concise_op if the main pad
984 # isn't long enough, or has the wrong kind of entries, compared to
985 # the pad a sub was compiled with. The fix for that would be to
986 # make a backwards compatible "terse" format that never even
987 # looked at the pad, just like the old B::Terse. I don't think
988 # that's worth the effort, though.
989 $curcv = main_cv unless $curcv;
990
991 if ($order eq "exec" and $lastnext and $$lastnext != $$op) {
724aa791 992 # insert a 'goto'
31b49ad4
SM
993 my $h = {"seq" => seq($lastnext), "class" => class($lastnext),
994 "addr" => sprintf("%#x", $$lastnext)};
cc02ea56
JC
995 print # $walkHandle
996 fmt_line($h, $op, $style{"terse"}[1], $level+1);
31b49ad4
SM
997 }
998 $lastnext = $op->next;
cc02ea56
JC
999 print # $walkHandle
1000 concise_op($op, $level, $style{"terse"}[0]);
31b49ad4
SM
1001}
1002
c99ca59a
SM
1003sub tree {
1004 my $op = shift;
1005 my $level = shift;
1006 my $style = $tree_decorations[$tree_style];
1007 my($space, $single, $kids, $kid, $nokid, $last, $lead, $size) = @$style;
1008 my $name = concise_op($op, $level, $treefmt);
1009 if (not $op->flags & OPf_KIDS) {
1010 return $name . "\n";
1011 }
1012 my @lines;
1013 for (my $kid = $op->first; $$kid; $kid = $kid->sibling) {
1014 push @lines, tree($kid, $level+1);
1015 }
1016 my $i;
1017 for ($i = $#lines; substr($lines[$i], 0, 1) eq " "; $i--) {
1018 $lines[$i] = $space . $lines[$i];
1019 }
1020 if ($i > 0) {
1021 $lines[$i] = $last . $lines[$i];
1022 while ($i-- > 1) {
1023 if (substr($lines[$i], 0, 1) eq " ") {
1024 $lines[$i] = $nokid . $lines[$i];
1025 } else {
f95e3c3c 1026 $lines[$i] = $kid . $lines[$i];
c99ca59a
SM
1027 }
1028 }
1029 $lines[$i] = $kids . $lines[$i];
1030 } else {
1031 $lines[0] = $single . $lines[0];
1032 }
1033 return("$name$lead" . shift @lines,
1034 map(" " x (length($name)+$size) . $_, @lines));
1035}
1036
213a1a26
SM
1037# *** Warning: fragile kludge ahead ***
1038# Because the B::* modules run in the same interpreter as the code
2814eb74
PJ
1039# they're compiling, their presence tends to distort the view we have of
1040# the code we're looking at. In particular, perl gives sequence numbers
1041# to COPs. If the program we're looking at were run on its own, this
1042# would start at 1. Because all of B::Concise and all the modules it
1043# uses are compiled first, though, by the time we get to the user's
1044# program the sequence number is already pretty high, which could be
1045# distracting if you're trying to tell OPs apart. Therefore we'd like to
1046# subtract an offset from all the sequence numbers we display, to
1047# restore the simpler view of the world. The trick is to know what that
1048# offset will be, when we're still compiling B::Concise! If we
213a1a26 1049# hardcoded a value, it would have to change every time B::Concise or
2814eb74
PJ
1050# other modules we use do. To help a little, what we do here is compile
1051# a little code at the end of the module, and compute the base sequence
1052# number for the user's program as being a small offset later, so all we
1053# have to worry about are changes in the offset.
7252851f
NC
1054
1055# [For 5.8.x and earlier perl is generating sequence numbers for all ops,
1056# and using them to reference labels]
1057
1058
213a1a26
SM
1059# When you say "perl -MO=Concise -e '$a'", the output should look like:
1060
1061# 4 <@> leave[t1] vKP/REFC ->(end)
1062# 1 <0> enter ->2
1063 #^ smallest OP sequence number should be 1
1064# 2 <;> nextstate(main 1 -e:1) v ->3
1065 # ^ smallest COP sequence number should be 1
1066# - <1> ex-rv2sv vK/1 ->4
1067# 3 <$> gvsv(*a) s ->4
1068
c27ea44e
SM
1069# If the second of the marked numbers there isn't 1, it means you need
1070# to update the corresponding magic number in the next line.
1071# Remember, this needs to stay the last things in the module.
e69a2255 1072
8635e3c2 1073my $cop_seq_mnum = 16;
e69a2255 1074$cop_seq_base = svref_2object(eval 'sub{0;}')->START->cop_seq + $cop_seq_mnum;
c99ca59a
SM
1075
10761;
1077
1078__END__
1079
1080=head1 NAME
1081
1082B::Concise - Walk Perl syntax tree, printing concise info about ops
1083
1084=head1 SYNOPSIS
1085
1086 perl -MO=Concise[,OPTIONS] foo.pl
1087
78ad9108
PJ
1088 use B::Concise qw(set_style add_callback);
1089
c99ca59a
SM
1090=head1 DESCRIPTION
1091
1092This compiler backend prints the internal OPs of a Perl program's syntax
1093tree in one of several space-efficient text formats suitable for debugging
1094the inner workings of perl or other compiler backends. It can print OPs in
1095the order they appear in the OP tree, in the order they will execute, or
1096in a text approximation to their tree structure, and the format of the
3c4b39be 1097information displayed is customizable. Its function is similar to that of
c99ca59a
SM
1098perl's B<-Dx> debugging flag or the B<B::Terse> module, but it is more
1099sophisticated and flexible.
1100
f8a679e6
RGS
1101=head1 EXAMPLE
1102
f9f861ec
JC
1103Here's two outputs (or 'renderings'), using the -exec and -basic
1104(i.e. default) formatting conventions on the same code snippet.
19e169bf
JC
1105
1106 % perl -MO=Concise,-exec -e '$a = $b + 42'
1107 1 <0> enter
1108 2 <;> nextstate(main 1 -e:1) v
1109 3 <#> gvsv[*b] s
1110 4 <$> const[IV 42] s
1111 * 5 <2> add[t3] sK/2
1112 6 <#> gvsv[*a] s
1113 7 <2> sassign vKS/2
1114 8 <@> leave[1 ref] vKP/REFC
1115
f9f861ec
JC
1116In this -exec rendering, each opcode is executed in the order shown.
1117The add opcode, marked with '*', is discussed in more detail.
19e169bf
JC
1118
1119The 1st column is the op's sequence number, starting at 1, and is
f9f861ec
JC
1120displayed in base 36 by default. Here they're purely linear; the
1121sequences are very helpful when looking at code with loops and
1122branches.
19e169bf
JC
1123
1124The symbol between angle brackets indicates the op's type, for
1125example; <2> is a BINOP, <@> a LISTOP, and <#> is a PADOP, which is
1126used in threaded perls. (see L</"OP class abbreviations">).
1127
f9f861ec 1128The opname, as in B<'add[t1]'>, may be followed by op-specific
19e169bf
JC
1129information in parentheses or brackets (ex B<'[t1]'>).
1130
f9f861ec 1131The op-flags (ex B<'sK/2'>) are described in (L</"OP flags
19e169bf 1132abbreviations">).
f8a679e6
RGS
1133
1134 % perl -MO=Concise -e '$a = $b + 42'
8ec8fbef 1135 8 <@> leave[1 ref] vKP/REFC ->(end)
f8a679e6
RGS
1136 1 <0> enter ->2
1137 2 <;> nextstate(main 1 -e:1) v ->3
1138 7 <2> sassign vKS/2 ->8
19e169bf 1139 * 5 <2> add[t1] sK/2 ->6
f8a679e6
RGS
1140 - <1> ex-rv2sv sK/1 ->4
1141 3 <$> gvsv(*b) s ->4
1142 4 <$> const(IV 42) s ->5
1143 - <1> ex-rv2sv sKRM*/1 ->7
1144 6 <$> gvsv(*a) s ->7
1145
19e169bf
JC
1146The default rendering is top-down, so they're not in execution order.
1147This form reflects the way the stack is used to parse and evaluate
1148expressions; the add operates on the two terms below it in the tree.
f8a679e6 1149
19e169bf
JC
1150Nullops appear as C<ex-opname>, where I<opname> is an op that has been
1151optimized away by perl. They're displayed with a sequence-number of
1152'-', because they are not executed (they don't appear in previous
1153example), they're printed here because they reflect the parse.
f8a679e6 1154
19e169bf
JC
1155The arrow points to the sequence number of the next op; they're not
1156displayed in -exec mode, for obvious reasons.
f8a679e6 1157
19e169bf
JC
1158Note that because this rendering was done on a non-threaded perl, the
1159PADOPs in the previous examples are now SVOPs, and some (but not all)
1160of the square brackets have been replaced by round ones. This is a
1161subtle feature to provide some visual distinction between renderings
1162on threaded and un-threaded perls.
f8a679e6 1163
f8a679e6 1164
c99ca59a
SM
1165=head1 OPTIONS
1166
1167Arguments that don't start with a hyphen are taken to be the names of
710ba574
FC
1168subroutines or formats to render; if no
1169such functions are specified, the main
9e0f9750
JC
1170body of the program (outside any subroutines, and not including use'd
1171or require'd files) is rendered. Passing C<BEGIN>, C<UNITCHECK>,
1172C<CHECK>, C<INIT>, or C<END> will cause all of the corresponding
1173special blocks to be printed. Arguments must follow options.
c99ca59a 1174
724aa791
JC
1175Options affect how things are rendered (ie printed). They're presented
1176here by their visual effect, 1st being strongest. They're grouped
1177according to how they interrelate; within each group the options are
1178mutually exclusive (unless otherwise stated).
1179
1180=head2 Options for Opcode Ordering
1181
1182These options control the 'vertical display' of opcodes. The display
1183'order' is also called 'mode' elsewhere in this document.
1184
c99ca59a
SM
1185=over 4
1186
1187=item B<-basic>
1188
1189Print OPs in the order they appear in the OP tree (a preorder
1190traversal, starting at the root). The indentation of each OP shows its
19e169bf
JC
1191level in the tree, and the '->' at the end of the line indicates the
1192next opcode in execution order. This mode is the default, so the flag
1193is included simply for completeness.
c99ca59a
SM
1194
1195=item B<-exec>
1196
1197Print OPs in the order they would normally execute (for the majority
1198of constructs this is a postorder traversal of the tree, ending at the
1199root). In most cases the OP that usually follows a given OP will
1200appear directly below it; alternate paths are shown by indentation. In
1201cases like loops when control jumps out of a linear path, a 'goto'
1202line is generated.
1203
1204=item B<-tree>
1205
1206Print OPs in a text approximation of a tree, with the root of the tree
1207at the left and 'left-to-right' order of children transformed into
1208'top-to-bottom'. Because this mode grows both to the right and down,
1209it isn't suitable for large programs (unless you have a very wide
1210terminal).
1211
724aa791
JC
1212=back
1213
1214=head2 Options for Line-Style
1215
1216These options select the line-style (or just style) used to render
1217each opcode, and dictates what info is actually printed into each line.
1218
1219=over 4
1220
1221=item B<-concise>
1222
1223Use the author's favorite set of formatting conventions. This is the
1224default, of course.
1225
1226=item B<-terse>
1227
1228Use formatting conventions that emulate the output of B<B::Terse>. The
1229basic mode is almost indistinguishable from the real B<B::Terse>, and the
1230exec mode looks very similar, but is in a more logical order and lacks
1231curly brackets. B<B::Terse> doesn't have a tree mode, so the tree mode
1232is only vaguely reminiscent of B<B::Terse>.
1233
1234=item B<-linenoise>
1235
1236Use formatting conventions in which the name of each OP, rather than being
1237written out in full, is represented by a one- or two-character abbreviation.
1238This is mainly a joke.
1239
1240=item B<-debug>
1241
1242Use formatting conventions reminiscent of B<B::Debug>; these aren't
1243very concise at all.
1244
1245=item B<-env>
1246
1247Use formatting conventions read from the environment variables
1248C<B_CONCISE_FORMAT>, C<B_CONCISE_GOTO_FORMAT>, and C<B_CONCISE_TREE_FORMAT>.
1249
1250=back
1251
1252=head2 Options for tree-specific formatting
1253
1254=over 4
1255
c99ca59a
SM
1256=item B<-compact>
1257
1258Use a tree format in which the minimum amount of space is used for the
1259lines connecting nodes (one character in most cases). This squeezes out
1260a few precious columns of screen real estate.
1261
1262=item B<-loose>
1263
1264Use a tree format that uses longer edges to separate OP nodes. This format
1265tends to look better than the compact one, especially in ASCII, and is
1266the default.
1267
1268=item B<-vt>
1269
1270Use tree connecting characters drawn from the VT100 line-drawing set.
1271This looks better if your terminal supports it.
1272
1273=item B<-ascii>
1274
1275Draw the tree with standard ASCII characters like C<+> and C<|>. These don't
1276look as clean as the VT100 characters, but they'll work with almost any
1277terminal (or the horizontal scrolling mode of less(1)) and are suitable
1278for text documentation or email. This is the default.
1279
724aa791 1280=back
c99ca59a 1281
724aa791
JC
1282These are pairwise exclusive, i.e. compact or loose, vt or ascii.
1283
1284=head2 Options controlling sequence numbering
1285
1286=over 4
c99ca59a
SM
1287
1288=item B<-base>I<n>
1289
1290Print OP sequence numbers in base I<n>. If I<n> is greater than 10, the
1291digit for 11 will be 'a', and so on. If I<n> is greater than 36, the digit
1292for 37 will be 'A', and so on until 62. Values greater than 62 are not
1293currently supported. The default is 36.
1294
1295=item B<-bigendian>
1296
1297Print sequence numbers with the most significant digit first. This is the
1298usual convention for Arabic numerals, and the default.
1299
1300=item B<-littleendian>
1301
1c2e8cca 1302Print sequence numbers with the least significant digit first. This is
724aa791 1303obviously mutually exclusive with bigendian.
c99ca59a 1304
724aa791 1305=back
c99ca59a 1306
724aa791 1307=head2 Other options
c99ca59a 1308
f18deeb9
JC
1309=over 4
1310
1311=item B<-src>
1312
e6665613
JC
1313With this option, the rendering of each statement (starting with the
1314nextstate OP) will be preceded by the 1st line of source code that
1315generates it. For example:
f18deeb9
JC
1316
1317 1 <0> enter
1318 # 1: my $i;
1319 2 <;> nextstate(main 1 junk.pl:1) v:{
1320 3 <0> padsv[$i:1,10] vM/LVINTRO
1321 # 3: for $i (0..9) {
1322 4 <;> nextstate(main 3 junk.pl:3) v:{
1323 5 <0> pushmark s
1324 6 <$> const[IV 0] s
1325 7 <$> const[IV 9] s
1326 8 <{> enteriter(next->j last->m redo->9)[$i:1,10] lKS
1327 k <0> iter s
1328 l <|> and(other->9) vK/1
1329 # 4: print "line ";
1330 9 <;> nextstate(main 2 junk.pl:4) v
1331 a <0> pushmark s
1332 b <$> const[PV "line "] s
1333 c <@> print vK
1334 # 5: print "$i\n";
e6665613 1335 ...
f18deeb9 1336
9e0f9750
JC
1337=item B<-stash="somepackage">
1338
1339With this, "somepackage" will be required, then the stash is
1340inspected, and each function is rendered.
1341
f18deeb9
JC
1342=back
1343
1344The following options are pairwise exclusive.
cc02ea56 1345
724aa791 1346=over 4
c99ca59a 1347
724aa791 1348=item B<-main>
c99ca59a 1349
724aa791 1350Include the main program in the output, even if subroutines were also
cc02ea56
JC
1351specified. This rendering is normally suppressed when a subroutine
1352name or reference is given.
1353
1354=item B<-nomain>
1355
1356This restores the default behavior after you've changed it with '-main'
1357(it's not normally needed). If no subroutine name/ref is given, main is
1358rendered, regardless of this flag.
1359
1360=item B<-nobanner>
1361
1362Renderings usually include a banner line identifying the function name
1363or stringified subref. This suppresses the printing of the banner.
1364
1365TBC: Remove the stringified coderef; while it provides a 'cookie' for
1366each function rendered, the cookies used should be 1,2,3.. not a
1367random hex-address. It also complicates string comparison of two
1368different trees.
c99ca59a 1369
724aa791 1370=item B<-banner>
c99ca59a 1371
cc02ea56
JC
1372restores default banner behavior.
1373
1374=item B<-banneris> => subref
1375
1376TBC: a hookpoint (and an option to set it) for a user-supplied
1377function to produce a banner appropriate for users needs. It's not
1378ideal, because the rendering-state variables, which are a natural
1379candidate for use in concise.t, are unavailable to the user.
c99ca59a 1380
724aa791 1381=back
c99ca59a 1382
724aa791 1383=head2 Option Stickiness
c99ca59a 1384
724aa791
JC
1385If you invoke Concise more than once in a program, you should know that
1386the options are 'sticky'. This means that the options you provide in
1387the first call will be remembered for the 2nd call, unless you
1388re-specify or change them.
c99ca59a 1389
cc02ea56
JC
1390=head1 ABBREVIATIONS
1391
1392The concise style uses symbols to convey maximum info with minimal
1393clutter (like hex addresses). With just a little practice, you can
1394start to see the flowers, not just the branches, in the trees.
1395
1396=head2 OP class abbreviations
1397
1398These symbols appear before the op-name, and indicate the
1399B:: namespace that represents the ops in your Perl code.
1400
1401 0 OP (aka BASEOP) An OP with no children
1402 1 UNOP An OP with one child
2f7c6295 1403 + UNOP_AUX A UNOP with auxillary fields
cc02ea56
JC
1404 2 BINOP An OP with two children
1405 | LOGOP A control branch OP
1406 @ LISTOP An OP that could have lots of children
1407 / PMOP An OP with a regular expression
1408 $ SVOP An OP with an SV
1409 " PVOP An OP with a string
1410 { LOOP An OP that holds pointers for a loop
1411 ; COP An OP that marks the start of a statement
1412 # PADOP An OP with a GV on the pad
b46e009d 1413 . METHOP An OP with method call info
cc02ea56
JC
1414
1415=head2 OP flags abbreviations
1416
19e169bf
JC
1417OP flags are either public or private. The public flags alter the
1418behavior of each opcode in consistent ways, and are represented by 0
1419or more single characters.
cc02ea56
JC
1420
1421 v OPf_WANT_VOID Want nothing (void context)
1422 s OPf_WANT_SCALAR Want single value (scalar context)
1423 l OPf_WANT_LIST Want list of any length (list context)
19e169bf 1424 Want is unknown
cc02ea56
JC
1425 K OPf_KIDS There is a firstborn child.
1426 P OPf_PARENS This operator was parenthesized.
1427 (Or block needs explicit scope entry.)
1428 R OPf_REF Certified reference.
1429 (Return container, not containee).
1430 M OPf_MOD Will modify (lvalue).
1431 S OPf_STACKED Some arg is arriving on the stack.
1432 * OPf_SPECIAL Do something weird for this op (see op.h)
1433
19e169bf
JC
1434Private flags, if any are set for an opcode, are displayed after a '/'
1435
1436 8 <@> leave[1 ref] vKP/REFC ->(end)
1437 7 <2> sassign vKS/2 ->8
1438
1439They're opcode specific, and occur less often than the public ones, so
1440they're represented by short mnemonics instead of single-chars; see
f3574cc6 1441B::Op_private and F<regen/op_private> for more details.
19e169bf 1442
c99ca59a
SM
1443=head1 FORMATTING SPECIFICATIONS
1444
724aa791
JC
1445For each line-style ('concise', 'terse', 'linenoise', etc.) there are
14463 format-specs which control how OPs are rendered.
1447
1448The first is the 'default' format, which is used in both basic and exec
1449modes to print all opcodes. The 2nd, goto-format, is used in exec
1450mode when branches are encountered. They're not real opcodes, and are
1451inserted to look like a closing curly brace. The tree-format is tree
1452specific.
1453
cc02ea56
JC
1454When a line is rendered, the correct format-spec is copied and scanned
1455for the following items; data is substituted in, and other
1456manipulations like basic indenting are done, for each opcode rendered.
1457
1458There are 3 kinds of items that may be populated; special patterns,
1459#vars, and literal text, which is copied verbatim. (Yes, it's a set
1460of s///g steps.)
1461
1462=head2 Special Patterns
1463
1464These items are the primitives used to perform indenting, and to
1465select text from amongst alternatives.
c99ca59a
SM
1466
1467=over 4
1468
1469=item B<(x(>I<exec_text>B<;>I<basic_text>B<)x)>
1470
1471Generates I<exec_text> in exec mode, or I<basic_text> in basic mode.
1472
1473=item B<(*(>I<text>B<)*)>
1474
1475Generates one copy of I<text> for each indentation level.
1476
1477=item B<(*(>I<text1>B<;>I<text2>B<)*)>
1478
1479Generates one fewer copies of I<text1> than the indentation level, followed
1480by one copy of I<text2> if the indentation level is more than 0.
1481
1482=item B<(?(>I<text1>B<#>I<var>I<Text2>B<)?)>
1483
1484If the value of I<var> is true (not empty or zero), generates the
1485value of I<var> surrounded by I<text1> and I<Text2>, otherwise
1486nothing.
1487
cc02ea56
JC
1488=item B<~>
1489
1490Any number of tildes and surrounding whitespace will be collapsed to
1491a single space.
1492
1493=back
1494
1495=head2 # Variables
1496
1497These #vars represent opcode properties that you may want as part of
1498your rendering. The '#' is intended as a private sigil; a #var's
1499value is interpolated into the style-line, much like "read $this".
1500
1501These vars take 3 forms:
1502
1503=over 4
1504
c99ca59a
SM
1505=item B<#>I<var>
1506
cc02ea56
JC
1507A property named 'var' is assumed to exist for the opcodes, and is
1508interpolated into the rendering.
c99ca59a
SM
1509
1510=item B<#>I<var>I<N>
1511
cc02ea56
JC
1512Generates the value of I<var>, left justified to fill I<N> spaces.
1513Note that this means while you can have properties 'foo' and 'foo2',
1514you cannot render 'foo2', but you could with 'foo2a'. You would be
1515wise not to rely on this behavior going forward ;-)
c99ca59a 1516
cc02ea56 1517=item B<#>I<Var>
c99ca59a 1518
cc02ea56
JC
1519This ucfirst form of #var generates a tag-value form of itself for
1520display; it converts '#Var' into a 'Var => #var' style, which is then
1521handled as described above. (Imp-note: #Vars cannot be used for
1522conditional-fills, because the => #var transform is done after the check
1523for #Var's value).
c99ca59a
SM
1524
1525=back
1526
cc02ea56
JC
1527The following variables are 'defined' by B::Concise; when they are
1528used in a style, their respective values are plugged into the
1529rendering of each opcode.
1530
1531Only some of these are used by the standard styles, the others are
1532provided for you to delve into optree mechanics, should you wish to
1533add a new style (see L</add_style> below) that uses them. You can
00baac8f 1534also add new ones using L</add_callback>.
c99ca59a
SM
1535
1536=over 4
1537
1538=item B<#addr>
1539
cc02ea56 1540The address of the OP, in hexadecimal.
c99ca59a
SM
1541
1542=item B<#arg>
1543
1544The OP-specific information of the OP (such as the SV for an SVOP, the
cc02ea56 1545non-local exit pointers for a LOOP, etc.) enclosed in parentheses.
c99ca59a
SM
1546
1547=item B<#class>
1548
1549The B-determined class of the OP, in all caps.
1550
f8a679e6 1551=item B<#classsym>
c99ca59a
SM
1552
1553A single symbol abbreviating the class of the OP.
1554
c3caa09d
SM
1555=item B<#coplabel>
1556
1557The label of the statement or block the OP is the start of, if any.
1558
c99ca59a
SM
1559=item B<#exname>
1560
1561The name of the OP, or 'ex-foo' if the OP is a null that used to be a foo.
1562
1563=item B<#extarg>
1564
1565The target of the OP, or nothing for a nulled OP.
1566
1567=item B<#firstaddr>
1568
19e169bf 1569The address of the OP's first child, in hexadecimal.
c99ca59a
SM
1570
1571=item B<#flags>
1572
1573The OP's flags, abbreviated as a series of symbols.
1574
1575=item B<#flagval>
1576
1577The numeric value of the OP's flags.
1578
d5ec2987
NC
1579=item B<#hints>
1580
1581The COP's hint flags, rendered with abbreviated names if possible. An empty
4f948f3a
RGS
1582string if this is not a COP. Here are the symbols used:
1583
1584 $ strict refs
1585 & strict subs
1586 * strict vars
d1718a7c
FC
1587 x$ explicit use/no strict refs
1588 x& explicit use/no strict subs
1589 x* explicit use/no strict vars
4f948f3a
RGS
1590 i integers
1591 l locale
1592 b bytes
4f948f3a
RGS
1593 { block scope
1594 % localise %^H
1595 < open in
1596 > open out
1597 I overload int
1598 F overload float
1599 B overload binary
1600 S overload string
1601 R overload re
1602 T taint
1603 E eval
1604 X filetest access
1605 U utf-8
d5ec2987 1606
7c2833f3
DM
1607 us use feature 'unicode_strings'
1608 fea=NNN feature bundle number
1609
d5ec2987
NC
1610=item B<#hintsval>
1611
1612The numeric value of the COP's hint flags, or an empty string if this is not
1613a COP.
1614
f8a679e6 1615=item B<#hyphseq>
c99ca59a
SM
1616
1617The sequence number of the OP, or a hyphen if it doesn't have one.
1618
1619=item B<#label>
1620
1621'NEXT', 'LAST', or 'REDO' if the OP is a target of one of those in exec
1622mode, or empty otherwise.
1623
1624=item B<#lastaddr>
1625
19e169bf 1626The address of the OP's last child, in hexadecimal.
c99ca59a
SM
1627
1628=item B<#name>
1629
1630The OP's name.
1631
1632=item B<#NAME>
1633
1634The OP's name, in all caps.
1635
1636=item B<#next>
1637
1638The sequence number of the OP's next OP.
1639
1640=item B<#nextaddr>
1641
19e169bf 1642The address of the OP's next OP, in hexadecimal.
c99ca59a
SM
1643
1644=item B<#noise>
1645
c27ea44e 1646A one- or two-character abbreviation for the OP's name.
c99ca59a
SM
1647
1648=item B<#private>
1649
1650The OP's private flags, rendered with abbreviated names if possible.
1651
1652=item B<#privval>
1653
1654The numeric value of the OP's private flags.
1655
1656=item B<#seq>
1657
2814eb74
PJ
1658The sequence number of the OP. Note that this is a sequence number
1659generated by B::Concise.
c99ca59a 1660
7252851f
NC
1661=item B<#seqnum>
1662
16635.8.x and earlier only. 5.9 and later do not provide this.
1664
1665The real sequence number of the OP, as a regular number and not adjusted
1666to be relative to the start of the real program. (This will generally be
1667a fairly large number because all of B<B::Concise> is compiled before
1668your program is).
1669
2814eb74 1670=item B<#opt>
c99ca59a 1671
b84c7839 1672Whether or not the op has been optimized by the peephole optimizer.
2814eb74 1673
7252851f
NC
1674Only available in 5.9 and later.
1675
c99ca59a
SM
1676=item B<#sibaddr>
1677
19e169bf 1678The address of the OP's next youngest sibling, in hexadecimal.
c99ca59a
SM
1679
1680=item B<#svaddr>
1681
19e169bf 1682The address of the OP's SV, if it has an SV, in hexadecimal.
c99ca59a
SM
1683
1684=item B<#svclass>
1685
1686The class of the OP's SV, if it has one, in all caps (e.g., 'IV').
1687
1688=item B<#svval>
1689
1690The value of the OP's SV, if it has one, in a short human-readable format.
1691
1692=item B<#targ>
1693
1694The numeric value of the OP's targ.
1695
1696=item B<#targarg>
1697
1698The name of the variable the OP's targ refers to, if any, otherwise the
1699letter t followed by the OP's targ in decimal.
1700
1701=item B<#targarglife>
1702
1703Same as B<#targarg>, but followed by the COP sequence numbers that delimit
1704the variable's lifetime (or 'end' for a variable in an open scope) for a
1705variable.
1706
1707=item B<#typenum>
1708
1709The numeric value of the OP's type, in decimal.
1710
1711=back
1712
f9f861ec
JC
1713=head1 One-Liner Command tips
1714
1715=over 4
1716
1717=item perl -MO=Concise,bar foo.pl
1718
1719Renders only bar() from foo.pl. To see main, drop the ',bar'. To see
1720both, add ',-main'
1721
1722=item perl -MDigest::MD5=md5 -MO=Concise,md5 -e1
1723
1724Identifies md5 as an XS function. The export is needed so that BC can
1725find it in main.
1726
1727=item perl -MPOSIX -MO=Concise,_POSIX_ARG_MAX -e1
1728
1729Identifies _POSIX_ARG_MAX as a constant sub, optimized to an IV.
1730Although POSIX isn't entirely consistent across platforms, this is
1731likely to be present in virtually all of them.
1732
1733=item perl -MPOSIX -MO=Concise,a -e 'print _POSIX_SAVED_IDS'
1734
1735This renders a print statement, which includes a call to the function.
1736It's identical to rendering a file with a use call and that single
1737statement, except for the filename which appears in the nextstate ops.
1738
1739=item perl -MPOSIX -MO=Concise,a -e 'sub a{_POSIX_SAVED_IDS}'
1740
1741This is B<very> similar to previous, only the first two ops differ. This
1742subroutine rendering is more representative, insofar as a single main
1743program will have many subs.
1744
6cc5d258
JC
1745=item perl -MB::Concise -e 'B::Concise::compile("-exec","-src", \%B::Concise::)->()'
1746
1747This renders all functions in the B::Concise package with the source
1748lines. It eschews the O framework so that the stashref can be passed
9e0f9750
JC
1749directly to B::Concise::compile(). See -stash option for a more
1750convenient way to render a package.
f9f861ec 1751
d5e42f17 1752=back
f9f861ec 1753
78ad9108
PJ
1754=head1 Using B::Concise outside of the O framework
1755
cc02ea56
JC
1756The common (and original) usage of B::Concise was for command-line
1757renderings of simple code, as given in EXAMPLE. But you can also use
1758B<B::Concise> from your code, and call compile() directly, and
724aa791 1759repeatedly. By doing so, you can avoid the compile-time only
cc02ea56
JC
1760operation of O.pm, and even use the debugger to step through
1761B::Concise::compile() itself.
f95e3c3c 1762
cc02ea56
JC
1763Once you're doing this, you may alter Concise output by adding new
1764rendering styles, and by optionally adding callback routines which
1765populate new variables, if such were referenced from those (just
1766added) styles.
f95e3c3c 1767
724aa791 1768=head2 Example: Altering Concise Renderings
78ad9108
PJ
1769
1770 use B::Concise qw(set_style add_callback);
cc02ea56 1771 add_style($yourStyleName => $defaultfmt, $gotofmt, $treefmt);
78ad9108 1772 add_callback
f95e3c3c
JC
1773 ( sub {
1774 my ($h, $op, $format, $level, $stylename) = @_;
78ad9108 1775 $h->{variable} = some_func($op);
cc02ea56
JC
1776 });
1777 $walker = B::Concise::compile(@options,@subnames,@subrefs);
1778 $walker->();
78ad9108 1779
f95e3c3c
JC
1780=head2 set_style()
1781
724aa791
JC
1782B<set_style> accepts 3 arguments, and updates the three format-specs
1783comprising a line-style (basic-exec, goto, tree). It has one minor
1784drawback though; it doesn't register the style under a new name. This
1785can become an issue if you render more than once and switch styles.
1786Thus you may prefer to use add_style() and/or set_style_standard()
1787instead.
1788
1789=head2 set_style_standard($name)
1790
1791This restores one of the standard line-styles: C<terse>, C<concise>,
1792C<linenoise>, C<debug>, C<env>, into effect. It also accepts style
1793names previously defined with add_style().
f95e3c3c 1794
345e2394 1795=head2 add_style ()
78ad9108 1796
f95e3c3c
JC
1797This subroutine accepts a new style name and three style arguments as
1798above, and creates, registers, and selects the newly named style. It is
1799an error to re-add a style; call set_style_standard() to switch between
1800several styles.
1801
345e2394 1802=head2 add_callback ()
f95e3c3c 1803
19e169bf
JC
1804If your newly minted styles refer to any new #variables, you'll need
1805to define a callback subroutine that will populate (or modify) those
1806variables. They are then available for use in the style you've
1807chosen.
f95e3c3c
JC
1808
1809The callbacks are called for each opcode visited by Concise, in the
1810same order as they are added. Each subroutine is passed five
1811parameters.
1812
1813 1. A hashref, containing the variable names and values which are
1814 populated into the report-line for the op
1815 2. the op, as a B<B::OP> object
1816 3. a reference to the format string
1817 4. the formatting (indent) level
1818 5. the selected stylename
78ad9108
PJ
1819
1820To define your own variables, simply add them to the hash, or change
1821existing values if you need to. The level and format are passed in as
1822references to scalars, but it is unlikely that they will need to be
1823changed or even used.
1824
724aa791 1825=head2 Running B::Concise::compile()
f95e3c3c
JC
1826
1827B<compile> accepts options as described above in L</OPTIONS>, and
1828arguments, which are either coderefs, or subroutine names.
1829
cc02ea56
JC
1830It constructs and returns a $treewalker coderef, which when invoked,
1831traverses, or walks, and renders the optrees of the given arguments to
1832STDOUT. You can reuse this, and can change the rendering style used
1833each time; thereafter the coderef renders in the new style.
f95e3c3c
JC
1834
1835B<walk_output> lets you change the print destination from STDOUT to
19e169bf
JC
1836another open filehandle, or into a string passed as a ref (unless
1837you've built perl with -Uuseperlio).
f95e3c3c 1838
555bd962
BG
1839 my $walker = B::Concise::compile('-terse','aFuncName', \&aSubRef); # 1
1840 walk_output(\my $buf);
1841 $walker->(); # 1 renders -terse
1842 set_style_standard('concise'); # 2
1843 $walker->(); # 2 renders -concise
1844 $walker->(@new); # 3 renders whatever
1845 print "3 different renderings: terse, concise, and @new: $buf\n";
cc02ea56
JC
1846
1847When $walker is called, it traverses the subroutines supplied when it
1848was created, and renders them using the current style. You can change
1849the style afterwards in several different ways:
1850
1851 1. call C<compile>, altering style or mode/order
1852 2. call C<set_style_standard>
1853 3. call $walker, passing @new options
1854
1855Passing new options to the $walker is the easiest way to change
1856amongst any pre-defined styles (the ones you add are automatically
1857recognized as options), and is the only way to alter rendering order
1858without calling compile again. Note however that rendering state is
1859still shared amongst multiple $walker objects, so they must still be
1860used in a coordinated manner.
f95e3c3c
JC
1861
1862=head2 B::Concise::reset_sequence()
1863
1864This function (not exported) lets you reset the sequence numbers (note
1865that they're numbered arbitrarily, their goal being to be human
1866readable). Its purpose is mostly to support testing, i.e. to compare
1867the concise output from two identical anonymous subroutines (but
1868different instances). Without the reset, B::Concise, seeing that
1869they're separate optrees, generates different sequence numbers in
1870the output.
1871
1872=head2 Errors
1873
9a3b3024
JC
1874Errors in rendering (non-existent function-name, non-existent coderef)
1875are written to the STDOUT, or wherever you've set it via
1876walk_output().
31b49ad4 1877
9a3b3024
JC
1878Errors using the various *style* calls, and bad args to walk_output(),
1879result in die(). Use an eval if you wish to catch these errors and
1880continue processing.
78ad9108 1881
c99ca59a
SM
1882=head1 AUTHOR
1883
31b49ad4 1884Stephen McCamant, E<lt>smcc@CSUA.Berkeley.EDUE<gt>.
c99ca59a
SM
1885
1886=cut