Upgrade to Cwd 2.21.
[p5sagit/p5-mst-13.2.git] / ext / B / B / Concise.pm
1 package B::Concise;
2 # Copyright (C) 2000-2003 Stephen McCamant. All rights reserved.
3 # This program is free software; you can redistribute and/or modify it
4 # under the same terms as Perl itself.
5
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.
11
12 use strict; # use #2
13 use warnings; # uses #3 and #4, since warnings uses Carp
14
15 use Exporter (); # use #5
16
17 our $VERSION   = "0.63";
18 our @ISA       = qw(Exporter);
19 our @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 );
22 our %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 )],  );
27
28 # use #6
29 use B qw(class ppname main_start main_root main_cv cstring svref_2object
30          SVf_IOK SVf_NOK SVf_POK SVf_IVisUV SVf_FAKE OPf_KIDS OPf_SPECIAL
31          CVf_ANON);
32
33 my %style =
34   ("terse" =>
35    ["(?(#label =>\n)?)(*(    )*)#class (#addr) #name (?([#targ])?) "
36     . "#svclass~(?((#svaddr))?)~#svval~(?(label \"#coplabel\")?)\n",
37     "(*(    )*)goto #class (#addr)\n",
38     "#class pp_#name"],
39    "concise" =>
40    ["#hyphseq2 (*(   (x( ;)x))*)<#classsym> "
41     . "#exname#arg(?([#targarglife])?)~#flags(?(/#private)?)(x(;~->#next)x)\n"
42     , "  (*(    )*)     goto #seq\n",
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" =>
49    ["#class (#addr)\n\top_next\t\t#nextaddr\n\top_sibling\t#sibaddr\n\t"
50     . "op_ppaddr\tPL_ppaddr[OP_#NAME]\n\top_type\t\t#typenum\n"
51     . "\top_flags\t#flagval\n\top_private\t#privval\n"
52     . "(?(\top_first\t#firstaddr\n)?)(?(\top_last\t\t#lastaddr\n)?)"
53     . "(?(\top_sv\t\t#svaddr\n)?)",
54     "    GOTO #addr\n",
55     "#addr"],
56    "env" => [$ENV{B_CONCISE_FORMAT}, $ENV{B_CONCISE_GOTO_FORMAT},
57              $ENV{B_CONCISE_TREE_FORMAT}],
58   );
59
60 # Renderings, ie how Concise prints, is controlled by these vars
61 # primary:
62 our $stylename;         # selects current style from %style
63 my $order = "basic";    # how optree is walked & printed: basic, exec, tree
64
65 # rendering mechanics:
66 # these 'formats' are the line-rendering templates
67 # they're updated from %style when $stylename changes
68 my ($format, $gotofmt, $treefmt);
69
70 # lesser players:
71 my $base = 36;          # how <sequence#> is displayed
72 my $big_endian = 1;     # more <sequence#> display
73 my $tree_style = 0;     # tree-order details
74 my $banner = 1;         # print banner before optree is traversed
75 my $do_main = 0;        # force printing of main routine
76
77 # another factor: can affect all styles!
78 our @callbacks;         # allow external management
79
80 set_style_standard("concise");
81
82 my $curcv;
83 my $cop_seq_base;
84
85 sub set_style {
86     ($format, $gotofmt, $treefmt) = @_;
87     #warn "set_style: deprecated, use set_style_standard instead\n"; # someday
88     die "expecting 3 style-format args\n" unless @_ == 3;
89 }
90
91 sub add_style {
92     my ($newstyle,@args) = @_;
93     die "style '$newstyle' already exists, choose a new name\n"
94         if exists $style{$newstyle};
95     die "expecting 3 style-format args\n" unless @args == 3;
96     $style{$newstyle} = [@args];
97     $stylename = $newstyle; # update rendering state
98 }
99
100 sub set_style_standard {
101     ($stylename) = @_; # update rendering state
102     die "err: style '$stylename' unknown\n" unless exists $style{$stylename};
103     set_style(@{$style{$stylename}});
104 }
105
106 sub add_callback {
107     push @callbacks, @_;
108 }
109
110 # output handle, used with all Concise-output printing
111 our $walkHandle;        # public for your convenience
112 BEGIN { $walkHandle = \*STDOUT }
113
114 sub walk_output { # updates $walkHandle
115     my $handle = shift;
116     return $walkHandle unless $handle; # allow use as accessor
117
118     if (ref $handle eq 'SCALAR') {
119         require Config;
120         die "no perlio in this build, can't call walk_output (\\\$scalar)\n"
121             unless $Config::Config{useperlio};
122         # in 5.8+, open(FILEHANDLE,MODE,REFERENCE) writes to string
123         open my $tmp, '>', $handle;     # but cant re-set existing STDOUT
124         $walkHandle = $tmp;             # so use my $tmp as intermediate var
125         return $walkHandle;
126     }
127     my $iotype = ref $handle;
128     die "expecting argument/object that can print\n"
129         unless $iotype eq 'GLOB' or $iotype and $handle->can('print');
130     $walkHandle = $handle;
131 }
132
133 sub concise_subref {
134     my($order, $coderef) = @_;
135     my $codeobj = svref_2object($coderef);
136
137     return concise_stashref(@_) 
138         unless ref $codeobj eq 'B::CV';
139     concise_cv_obj($order, $codeobj);
140 }
141
142 sub concise_stashref {
143     my($order, $h) = @_;
144     foreach my $k (sort keys %$h) {
145         local *s = $h->{$k};
146         my $coderef = *s{CODE} or next;
147         reset_sequence();
148         print "FUNC: ", *s, "\n";
149         my $codeobj = svref_2object($coderef);
150         next unless ref $codeobj eq 'B::CV';
151         eval { concise_cv_obj($order, $codeobj) }
152         or warn "err $@ on $codeobj";
153     }
154 }
155
156 # This should have been called concise_subref, but it was exported
157 # under this name in versions before 0.56
158 sub concise_cv { concise_subref(@_); }
159
160 sub concise_cv_obj {
161     my ($order, $cv) = @_;
162     $curcv = $cv;
163     die "err: coderef has no START\n" if class($cv->START) eq "NULL";
164     sequence($cv->START);
165     if ($order eq "exec") {
166         walk_exec($cv->START);
167     } elsif ($order eq "basic") {
168         walk_topdown($cv->ROOT, sub { $_[0]->concise($_[1]) }, 0);
169     } else {
170         print $walkHandle tree($cv->ROOT, 0);
171     }
172 }
173
174 sub concise_main {
175     my($order) = @_;
176     sequence(main_start);
177     $curcv = main_cv;
178     if ($order eq "exec") {
179         return if class(main_start) eq "NULL";
180         walk_exec(main_start);
181     } elsif ($order eq "tree") {
182         return if class(main_root) eq "NULL";
183         print $walkHandle tree(main_root, 0);
184     } elsif ($order eq "basic") {
185         return if class(main_root) eq "NULL";
186         walk_topdown(main_root,
187                      sub { $_[0]->concise($_[1]) }, 0);
188     }
189 }
190
191 sub concise_specials {
192     my($name, $order, @cv_s) = @_;
193     my $i = 1;
194     if ($name eq "BEGIN") {
195         splice(@cv_s, 0, 7); # skip 7 BEGIN blocks in this file
196     } elsif ($name eq "CHECK") {
197         pop @cv_s; # skip the CHECK block that calls us
198     }
199     for my $cv (@cv_s) {
200         print $walkHandle "$name $i:\n";
201         $i++;
202         concise_cv_obj($order, $cv);
203     }
204 }
205
206 my $start_sym = "\e(0"; # "\cN" sometimes also works
207 my $end_sym   = "\e(B"; # "\cO" respectively
208
209 my @tree_decorations =
210   (["  ", "--", "+-", "|-", "| ", "`-", "-", 1],
211    [" ", "-", "+", "+", "|", "`", "", 0],
212    ["  ", map("$start_sym$_$end_sym", "qq", "wq", "tq", "x ", "mq", "q"), 1],
213    [" ", map("$start_sym$_$end_sym", "q", "w", "t", "x", "m"), "", 0],
214   );
215
216
217 sub compileOpts {
218     # set rendering state from options and args
219     my @options = grep(/^-/, @_);
220     my @args = grep(!/^-/, @_);
221     for my $o (@options) {
222         # mode/order
223         if ($o eq "-basic") {
224             $order = "basic";
225         } elsif ($o eq "-exec") {
226             $order = "exec";
227         } elsif ($o eq "-tree") {
228             $order = "tree";
229         }
230         # tree-specific
231         elsif ($o eq "-compact") {
232             $tree_style |= 1;
233         } elsif ($o eq "-loose") {
234             $tree_style &= ~1;
235         } elsif ($o eq "-vt") {
236             $tree_style |= 2;
237         } elsif ($o eq "-ascii") {
238             $tree_style &= ~2;
239         }
240         # sequence numbering
241         elsif ($o =~ /^-base(\d+)$/) {
242             $base = $1;
243         } elsif ($o eq "-bigendian") {
244             $big_endian = 1;
245         } elsif ($o eq "-littleendian") {
246             $big_endian = 0;
247         }
248         elsif ($o eq "-nobanner") {
249             $banner = 0;
250         } elsif ($o eq "-banner") {
251             $banner = 1;
252         }
253         elsif ($o eq "-main") {
254             $do_main = 1;
255         } elsif ($o eq "-nomain") {
256             $do_main = 0;
257         }
258         # line-style options
259         elsif (exists $style{substr($o, 1)}) {
260             $stylename = substr($o, 1);
261             set_style_standard($stylename);
262         } else {
263             warn "Option $o unrecognized";
264         }
265     }
266     return (@args);
267 }
268
269 sub compile {
270     my (@args) = compileOpts(@_);
271     return sub {
272         my @newargs = compileOpts(@_); # accept new rendering options
273         warn "disregarding non-options: @newargs\n" if @newargs;
274
275         for my $objname (@args) {
276             
277             if ($objname eq "BEGIN") {
278                 concise_specials("BEGIN", $order,
279                                B::begin_av->isa("B::AV") ?
280                                B::begin_av->ARRAY : ());
281             } elsif ($objname eq "INIT") {
282                 concise_specials("INIT", $order,
283                                B::init_av->isa("B::AV") ?
284                                B::init_av->ARRAY : ());
285             } elsif ($objname eq "CHECK") {
286                 concise_specials("CHECK", $order,
287                                B::check_av->isa("B::AV") ?
288                                B::check_av->ARRAY : ());
289             } elsif ($objname eq "END") {
290                 concise_specials("END", $order,
291                                      B::end_av->isa("B::AV") ?
292                                B::end_av->ARRAY : ());
293             }
294             else {
295                 # convert function names to subrefs
296                 my $objref;
297                 if (ref $objname) {
298                     print $walkHandle "B::Concise::compile($objname)\n"
299                         if $banner;
300                     $objref = $objname;
301                 } else {
302                     $objname = "main::" . $objname unless $objname =~ /::/;
303                     print $walkHandle "$objname:\n";
304                     no strict 'refs';
305                     die "err: unknown function ($objname)\n"
306                         unless *{$objname}{CODE};
307                     $objref = \&$objname;
308                 }
309                 concise_subref($order, $objref);
310             }
311         }
312         if (!@args or $do_main) {
313             print $walkHandle "main program:\n" if $do_main;
314             concise_main($order);
315         }
316         return @args;   # something
317     }
318 }
319
320 my %labels;
321 my $lastnext;   # remembers op-chain, used to insert gotos
322
323 my %opclass = ('OP' => "0", 'UNOP' => "1", 'BINOP' => "2", 'LOGOP' => "|",
324                'LISTOP' => "@", 'PMOP' => "/", 'SVOP' => "\$", 'GVOP' => "*",
325                'PVOP' => '"', 'LOOP' => "{", 'COP' => ";", 'PADOP' => "#");
326
327 no warnings 'qw'; # "Possible attempt to put comments..."; use #7
328 my @linenoise =
329   qw'#  () sc (  @? 1  $* gv *{ m$ m@ m% m? p/ *$ $  $# & a& pt \\ s\\ rf bl
330      `  *? <> ?? ?/ r/ c/ // qr s/ /c y/ =  @= C  sC Cp sp df un BM po +1 +I
331      -1 -I 1+ I+ 1- I- ** *  i* /  i/ %$ i% x  +  i+ -  i- .  "  << >> <  i<
332      >  i> <= i, >= i. == i= != i! <? i? s< s> s, s. s= s! s? b& b^ b| -0 -i
333      !  ~  a2 si cs rd sr e^ lg sq in %x %o ab le ss ve ix ri sf FL od ch cy
334      uf lf uc lc qm @  [f [  @[ eh vl ky dl ex %  ${ @{ uk pk st jn )  )[ a@
335      a% sl +] -] [- [+ so rv GS GW MS MW .. f. .f && || ^^ ?: &= |= -> s{ s}
336      v} ca wa di rs ;; ;  ;d }{ {  }  {} f{ it {l l} rt }l }n }r dm }g }e ^o
337      ^c ^| ^# um bm t~ u~ ~d DB db ^s se ^g ^r {w }w pf pr ^O ^K ^R ^W ^d ^v
338      ^e ^t ^k t. fc ic fl .s .p .b .c .l .a .h g1 s1 g2 s2 ?. l? -R -W -X -r
339      -w -x -e -o -O -z -s -M -A -C -S -c -b -f -d -p -l -u -g -k -t -T -B cd
340      co cr u. cm ut r. l@ s@ r@ mD uD oD rD tD sD wD cD f$ w$ p$ sh e$ k$ g3
341      g4 s4 g5 s5 T@ C@ L@ G@ A@ S@ Hg Hc Hr Hw Mg Mc Ms Mr Sg Sc So rq do {e
342      e} {t t} g6 G6 6e g7 G7 7e g8 G8 8e g9 G9 9e 6s 7s 8s 9s 6E 7E 8E 9E Pn
343      Pu GP SP EP Gn Gg GG SG EG g0 c$ lk t$ ;s n> // /= CO';
344
345 my $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
346
347 sub op_flags {
348     my($x) = @_;
349     my(@v);
350     push @v, "v" if ($x & 3) == 1;
351     push @v, "s" if ($x & 3) == 2;
352     push @v, "l" if ($x & 3) == 3;
353     push @v, "K" if $x & 4;
354     push @v, "P" if $x & 8;
355     push @v, "R" if $x & 16;
356     push @v, "M" if $x & 32;
357     push @v, "S" if $x & 64;
358     push @v, "*" if $x & 128;
359     return join("", @v);
360 }
361
362 sub base_n {
363     my $x = shift;
364     return "-" . base_n(-$x) if $x < 0;
365     my $str = "";
366     do { $str .= substr($chars, $x % $base, 1) } while $x = int($x / $base);
367     $str = reverse $str if $big_endian;
368     return $str;
369 }
370
371 my %sequence_num;
372 my $seq_max = 1;
373
374 sub reset_sequence {
375     # reset the sequence
376     %sequence_num = ();
377     $seq_max = 1;
378     $lastnext = 0;
379 }
380
381 sub seq {
382     my($op) = @_;
383     return "-" if not exists $sequence_num{$$op};
384     return base_n($sequence_num{$$op});
385 }
386
387 sub walk_topdown {
388     my($op, $sub, $level) = @_;
389     $sub->($op, $level);
390     if ($op->flags & OPf_KIDS) {
391         for (my $kid = $op->first; $$kid; $kid = $kid->sibling) {
392             walk_topdown($kid, $sub, $level + 1);
393         }
394     }
395     if (class($op) eq "PMOP") {
396         my $maybe_root = $op->pmreplroot;
397         if (ref($maybe_root) and $maybe_root->isa("B::OP")) {
398             # It really is the root of the replacement, not something
399             # else stored here for lack of space elsewhere
400             walk_topdown($maybe_root, $sub, $level + 1);
401         }
402     }
403 }
404
405 sub walklines {
406     my($ar, $level) = @_;
407     for my $l (@$ar) {
408         if (ref($l) eq "ARRAY") {
409             walklines($l, $level + 1);
410         } else {
411             $l->concise($level);
412         }
413     }
414 }
415
416 sub walk_exec {
417     my($top, $level) = @_;
418     my %opsseen;
419     my @lines;
420     my @todo = ([$top, \@lines]);
421     while (@todo and my($op, $targ) = @{shift @todo}) {
422         for (; $$op; $op = $op->next) {
423             last if $opsseen{$$op}++;
424             push @$targ, $op;
425             my $name = $op->name;
426             if (class($op) eq "LOGOP") {
427                 my $ar = [];
428                 push @$targ, $ar;
429                 push @todo, [$op->other, $ar];
430             } elsif ($name eq "subst" and $ {$op->pmreplstart}) {
431                 my $ar = [];
432                 push @$targ, $ar;
433                 push @todo, [$op->pmreplstart, $ar];
434             } elsif ($name =~ /^enter(loop|iter)$/) {
435                 $labels{${$op->nextop}} = "NEXT";
436                 $labels{${$op->lastop}} = "LAST";
437                 $labels{${$op->redoop}} = "REDO";
438             }
439         }
440     }
441     walklines(\@lines, 0);
442 }
443
444 # The structure of this routine is purposely modeled after op.c's peep()
445 sub sequence {
446     my($op) = @_;
447     my $oldop = 0;
448     return if class($op) eq "NULL" or exists $sequence_num{$$op};
449     for (; $$op; $op = $op->next) {
450         last if exists $sequence_num{$$op};
451         my $name = $op->name;
452         if ($name =~ /^(null|scalar|lineseq|scope)$/) {
453             next if $oldop and $ {$op->next};
454         } else {
455             $sequence_num{$$op} = $seq_max++;
456             if (class($op) eq "LOGOP") {
457                 my $other = $op->other;
458                 $other = $other->next while $other->name eq "null";
459                 sequence($other);
460             } elsif (class($op) eq "LOOP") {
461                 my $redoop = $op->redoop;
462                 $redoop = $redoop->next while $redoop->name eq "null";
463                 sequence($redoop);
464                 my $nextop = $op->nextop;
465                 $nextop = $nextop->next while $nextop->name eq "null";
466                 sequence($nextop);
467                 my $lastop = $op->lastop;
468                 $lastop = $lastop->next while $lastop->name eq "null";
469                 sequence($lastop);
470             } elsif ($name eq "subst" and $ {$op->pmreplstart}) {
471                 my $replstart = $op->pmreplstart;
472                 $replstart = $replstart->next while $replstart->name eq "null";
473                 sequence($replstart);
474             }
475         }
476         $oldop = $op;
477     }
478 }
479
480 sub fmt_line {    # generate text-line for op.
481     my($hr, $op, $text, $level) = @_;
482
483     $_->($hr, $op, \$text, \$level, $stylename) for @callbacks;
484
485     return '' if $hr->{SKIP};   # suppress line if a callback said so
486     return '' if $hr->{goto} and $hr->{goto} eq '-';    # no goto nowhere
487
488     # spec: (?(text1#varText2)?)
489     $text =~ s/\(\?\(([^\#]*?)\#(\w+)([^\#]*?)\)\?\)/
490         $hr->{$2} ? $1.$hr->{$2}.$3 : ""/eg;
491
492     # spec: (x(exec_text;basic_text)x)
493     $text =~ s/\(x\((.*?);(.*?)\)x\)/$order eq "exec" ? $1 : $2/egs;
494
495     # spec: (*(text)*)
496     $text =~ s/\(\*\(([^;]*?)\)\*\)/$1 x $level/egs;
497
498     # spec: (*(text1;text2)*)
499     $text =~ s/\(\*\((.*?);(.*?)\)\*\)/$1 x ($level - 1) . $2 x ($level>0)/egs;
500
501     # convert #Var to tag=>val form: Var\t#var
502     $text =~ s/\#([A-Z][a-z]+)(\d+)?/\t\u$1\t\L#$1$2/gs;
503
504     # spec: #varN
505     $text =~ s/\#([a-zA-Z]+)(\d+)/sprintf("%-$2s", $hr->{$1})/eg;
506
507     $text =~ s/\#([a-zA-Z]+)/$hr->{$1}/eg;      # populate #var's
508     $text =~ s/[ \t]*~+[ \t]*/ /g;              # squeeze tildes
509     chomp $text;
510     return "$text\n" if $text ne "";
511     return $text; # suppress empty lines
512 }
513
514 my %priv;
515 $priv{$_}{128} = "LVINTRO"
516   for ("pos", "substr", "vec", "threadsv", "gvsv", "rv2sv", "rv2hv", "rv2gv",
517        "rv2av", "rv2arylen", "aelem", "helem", "aslice", "hslice", "padsv",
518        "padav", "padhv", "enteriter");
519 $priv{$_}{64} = "REFC" for ("leave", "leavesub", "leavesublv", "leavewrite");
520 $priv{"aassign"}{64} = "COMMON";
521 $priv{"aassign"}{32} = "PHASH" if $] < 5.009;
522 $priv{"sassign"}{64} = "BKWARD";
523 $priv{$_}{64} = "RTIME" for ("match", "subst", "substcont");
524 @{$priv{"trans"}}{1,2,4,8,16,64} = ("<UTF", ">UTF", "IDENT", "SQUASH", "DEL",
525                                     "COMPL", "GROWS");
526 $priv{"repeat"}{64} = "DOLIST";
527 $priv{"leaveloop"}{64} = "CONT";
528 @{$priv{$_}}{32,64,96} = ("DREFAV", "DREFHV", "DREFSV")
529   for (qw(rv2gv rv2sv padsv aelem helem));
530 $priv{"entersub"}{16} = "DBG";
531 $priv{"entersub"}{32} = "TARG";
532 @{$priv{$_}}{4,8,128} = ("INARGS","AMPER","NO()") for ("entersub", "rv2cv");
533 $priv{"gv"}{32} = "EARLYCV";
534 $priv{"aelem"}{16} = $priv{"helem"}{16} = "LVDEFER";
535 $priv{$_}{16} = "OURINTR" for ("gvsv", "rv2sv", "rv2av", "rv2hv", "r2gv",
536         "enteriter");
537 $priv{$_}{16} = "TARGMY"
538   for (map(($_,"s$_"),"chop", "chomp"),
539        map(($_,"i_$_"), "postinc", "postdec", "multiply", "divide", "modulo",
540            "add", "subtract", "negate"), "pow", "concat", "stringify",
541        "left_shift", "right_shift", "bit_and", "bit_xor", "bit_or",
542        "complement", "atan2", "sin", "cos", "rand", "exp", "log", "sqrt",
543        "int", "hex", "oct", "abs", "length", "index", "rindex", "sprintf",
544        "ord", "chr", "crypt", "quotemeta", "join", "push", "unshift", "flock",
545        "chdir", "chown", "chroot", "unlink", "chmod", "utime", "rename",
546        "link", "symlink", "mkdir", "rmdir", "wait", "waitpid", "system",
547        "exec", "kill", "getppid", "getpgrp", "setpgrp", "getpriority",
548        "setpriority", "time", "sleep");
549 $priv{$_}{4} = "REVERSED" for ("enteriter", "iter");
550 @{$priv{"const"}}{8,16,32,64,128} = ("STRICT","ENTERED", '$[', "BARE", "WARN");
551 $priv{"flip"}{64} = $priv{"flop"}{64} = "LINENUM";
552 $priv{"list"}{64} = "GUESSED";
553 $priv{"delete"}{64} = "SLICE";
554 $priv{"exists"}{64} = "SUB";
555 $priv{$_}{64} = "LOCALE"
556   for ("sort", "prtf", "sprintf", "slt", "sle", "seq", "sne", "sgt", "sge",
557        "scmp", "lc", "uc", "lcfirst", "ucfirst");
558 @{$priv{"sort"}}{1,2,4,8,16} = ("NUM", "INT", "REV", "INPLACE","DESC");
559 $priv{"threadsv"}{64} = "SVREFd";
560 @{$priv{$_}}{16,32,64,128} = ("INBIN","INCR","OUTBIN","OUTCR")
561   for ("open", "backtick");
562 $priv{"exit"}{128} = "VMS";
563 $priv{$_}{2} = "FTACCESS"
564   for ("ftrread", "ftrwrite", "ftrexec", "fteread", "ftewrite", "fteexec");
565 if ($] >= 5.009) {
566   # Stacked filetests are post 5.8.x
567   $priv{$_}{4} = "FTSTACKED"
568     for ("ftrread", "ftrwrite", "ftrexec", "fteread", "ftewrite", "fteexec",
569          "ftis", "fteowned", "ftrowned", "ftzero", "ftsize", "ftmtime",
570          "ftatime", "ftctime", "ftsock", "ftchr", "ftblk", "ftfile", "ftdir",
571          "ftpipe", "ftlink", "ftsuid", "ftsgid", "ftsvtx", "fttty", "fttext",
572          "ftbinary");
573   # Lexical $_ is post 5.8.x
574   $priv{$_}{2} = "GREPLEX"
575     for ("mapwhile", "mapstart", "grepwhile", "grepstart");
576 }
577
578 sub private_flags {
579     my($name, $x) = @_;
580     my @s;
581     for my $flag (128, 96, 64, 32, 16, 8, 4, 2, 1) {
582         if ($priv{$name}{$flag} and $x & $flag and $x >= $flag) {
583             $x -= $flag;
584             push @s, $priv{$name}{$flag};
585         }
586     }
587     push @s, $x if $x;
588     return join(",", @s);
589 }
590
591 sub concise_sv {
592     my($sv, $hr) = @_;
593     $hr->{svclass} = class($sv);
594     $hr->{svclass} = "UV"
595       if $hr->{svclass} eq "IV" and $sv->FLAGS & SVf_IVisUV;
596     $hr->{svaddr} = sprintf("%#x", $$sv);
597     if ($hr->{svclass} eq "GV") {
598         my $gv = $sv;
599         my $stash = $gv->STASH->NAME;
600         if ($stash eq "main") {
601             $stash = "";
602         } else {
603             $stash = $stash . "::";
604         }
605         $hr->{svval} = "*$stash" . $gv->SAFENAME;
606         return "*$stash" . $gv->SAFENAME;
607     } else {
608         while (class($sv) eq "RV") {
609             $hr->{svval} .= "\\";
610             $sv = $sv->RV;
611         }
612         if (class($sv) eq "SPECIAL") {
613             $hr->{svval} .= ["Null", "sv_undef", "sv_yes", "sv_no"]->[$$sv];
614         } elsif ($sv->FLAGS & SVf_NOK) {
615             $hr->{svval} .= $sv->NV;
616         } elsif ($sv->FLAGS & SVf_IOK) {
617             $hr->{svval} .= $sv->int_value;
618         } elsif ($sv->FLAGS & SVf_POK) {
619             $hr->{svval} .= cstring($sv->PV);
620         } elsif (class($sv) eq "HV") {
621             $hr->{svval} .= 'HASH';
622         }
623
624         $hr->{svval} = 'undef' unless defined $hr->{svval};
625         my $out = $hr->{svclass};
626         return $out .= " $hr->{svval}" ; 
627     }
628 }
629
630 sub concise_op {
631     my ($op, $level, $format) = @_;
632     my %h;
633     $h{exname} = $h{name} = $op->name;
634     $h{NAME} = uc $h{name};
635     $h{class} = class($op);
636     $h{extarg} = $h{targ} = $op->targ;
637     $h{extarg} = "" unless $h{extarg};
638     if ($h{name} eq "null" and $h{targ}) {
639         # targ holds the old type
640         $h{exname} = "ex-" . substr(ppname($h{targ}), 3);
641         $h{extarg} = "";
642     } elsif ($op->name =~ /^leave(sub(lv)?|write)?$/) {
643         # targ potentially holds a reference count
644         if ($op->private & 64) {
645             my $refs = "ref" . ($h{targ} != 1 ? "s" : "");
646             $h{targarglife} = $h{targarg} = "$h{targ} $refs";
647         }
648     } elsif ($h{targ}) {
649         my $padname = (($curcv->PADLIST->ARRAY)[0]->ARRAY)[$h{targ}];
650         if (defined $padname and class($padname) ne "SPECIAL") {
651             $h{targarg}  = $padname->PVX;
652             if ($padname->FLAGS & SVf_FAKE) {
653                 if ($] < 5.009) {
654                     $h{targarglife} = "$h{targarg}:FAKE";
655                 } else {
656                     # These changes relate to the jumbo closure fix.
657                     # See changes 19939 and 20005
658                     my $fake = '';
659                     $fake .= 'a' if $padname->IVX & 1; # PAD_FAKELEX_ANON
660                     $fake .= 'm' if $padname->IVX & 2; # PAD_FAKELEX_MULTI
661                     $fake .= ':' . $padname->NVX if $curcv->CvFLAGS & CVf_ANON;
662                     $h{targarglife} = "$h{targarg}:FAKE:$fake";
663                 }
664             }
665             else {
666                 my $intro = $padname->NVX - $cop_seq_base;
667                 my $finish = int($padname->IVX) - $cop_seq_base;
668                 $finish = "end" if $finish == 999999999 - $cop_seq_base;
669                 $h{targarglife} = "$h{targarg}:$intro,$finish";
670             }
671         } else {
672             $h{targarglife} = $h{targarg} = "t" . $h{targ};
673         }
674     }
675     $h{arg} = "";
676     $h{svclass} = $h{svaddr} = $h{svval} = "";
677     if ($h{class} eq "PMOP") {
678         my $precomp = $op->precomp;
679         if (defined $precomp) {
680             $precomp = cstring($precomp); # Escape literal control sequences
681             $precomp = "/$precomp/";
682         } else {
683             $precomp = "";
684         }
685         my $pmreplroot = $op->pmreplroot;
686         my $pmreplstart;
687         if (ref($pmreplroot) eq "B::GV") {
688             # with C<@stash_array = split(/pat/, str);>,
689             #  *stash_array is stored in /pat/'s pmreplroot.
690             $h{arg} = "($precomp => \@" . $pmreplroot->NAME . ")";
691         } elsif (!ref($pmreplroot) and $pmreplroot) {
692             # same as the last case, except the value is actually a
693             # pad offset for where the GV is kept (this happens under
694             # ithreads)
695             my $gv = (($curcv->PADLIST->ARRAY)[1]->ARRAY)[$pmreplroot];
696             $h{arg} = "($precomp => \@" . $gv->NAME . ")";
697         } elsif ($ {$op->pmreplstart}) {
698             undef $lastnext;
699             $pmreplstart = "replstart->" . seq($op->pmreplstart);
700             $h{arg} = "(" . join(" ", $precomp, $pmreplstart) . ")";
701         } else {
702             $h{arg} = "($precomp)";
703         }
704     } elsif ($h{class} eq "PVOP" and $h{name} ne "trans") {
705         $h{arg} = '("' . $op->pv . '")';
706         $h{svval} = '"' . $op->pv . '"';
707     } elsif ($h{class} eq "COP") {
708         my $label = $op->label;
709         $h{coplabel} = $label;
710         $label = $label ? "$label: " : "";
711         my $loc = $op->file;
712         $loc =~ s[.*/][];
713         $loc .= ":" . $op->line;
714         my($stash, $cseq) = ($op->stash->NAME, $op->cop_seq - $cop_seq_base);
715         my $arybase = $op->arybase;
716         $arybase = $arybase ? ' $[=' . $arybase : "";
717         $h{arg} = "($label$stash $cseq $loc$arybase)";
718     } elsif ($h{class} eq "LOOP") {
719         $h{arg} = "(next->" . seq($op->nextop) . " last->" . seq($op->lastop)
720           . " redo->" . seq($op->redoop) . ")";
721     } elsif ($h{class} eq "LOGOP") {
722         undef $lastnext;
723         $h{arg} = "(other->" . seq($op->other) . ")";
724     } elsif ($h{class} eq "SVOP") {
725         unless ($h{name} eq 'aelemfast' and $op->flags & OPf_SPECIAL) {
726             if (! ${$op->sv}) {
727                 my $sv = (($curcv->PADLIST->ARRAY)[1]->ARRAY)[$op->targ];
728                 $h{arg} = "[" . concise_sv($sv, \%h) . "]";
729                 $h{targarglife} = $h{targarg} = "";
730             } else {
731                 $h{arg} = "(" . concise_sv($op->sv, \%h) . ")";
732             }
733         }
734     } elsif ($h{class} eq "PADOP") {
735         my $sv = (($curcv->PADLIST->ARRAY)[1]->ARRAY)[$op->padix];
736         $h{arg} = "[" . concise_sv($sv, \%h) . "]";
737     }
738     $h{seq} = $h{hyphseq} = seq($op);
739     $h{seq} = "" if $h{seq} eq "-";
740     $h{opt} = $op->opt;
741     $h{static} = $op->static;
742     $h{next} = $op->next;
743     $h{next} = (class($h{next}) eq "NULL") ? "(end)" : seq($h{next});
744     $h{nextaddr} = sprintf("%#x", $ {$op->next});
745     $h{sibaddr} = sprintf("%#x", $ {$op->sibling});
746     $h{firstaddr} = sprintf("%#x", $ {$op->first}) if $op->can("first");
747     $h{lastaddr} = sprintf("%#x", $ {$op->last}) if $op->can("last");
748
749     $h{classsym} = $opclass{$h{class}};
750     $h{flagval} = $op->flags;
751     $h{flags} = op_flags($op->flags);
752     $h{privval} = $op->private;
753     $h{private} = private_flags($h{name}, $op->private);
754     $h{addr} = sprintf("%#x", $$op);
755     $h{label} = $labels{$$op};
756     $h{typenum} = $op->type;
757     $h{noise} = $linenoise[$op->type];
758
759     return fmt_line(\%h, $op, $format, $level);
760 }
761
762 sub B::OP::concise {
763     my($op, $level) = @_;
764     if ($order eq "exec" and $lastnext and $$lastnext != $$op) {
765         # insert a 'goto' line
766         my $synth = {"seq" => seq($lastnext), "class" => class($lastnext),
767                      "addr" => sprintf("%#x", $$lastnext),
768                      "goto" => seq($lastnext), # simplify goto '-' removal
769              };
770         print $walkHandle fmt_line($synth, $op, $gotofmt, $level+1);
771     }
772     $lastnext = $op->next;
773     print $walkHandle concise_op($op, $level, $format);
774 }
775
776 # B::OP::terse (see Terse.pm) now just calls this
777 sub b_terse {
778     my($op, $level) = @_;
779
780     # This isn't necessarily right, but there's no easy way to get
781     # from an OP to the right CV. This is a limitation of the
782     # ->terse() interface style, and there isn't much to do about
783     # it. In particular, we can die in concise_op if the main pad
784     # isn't long enough, or has the wrong kind of entries, compared to
785     # the pad a sub was compiled with. The fix for that would be to
786     # make a backwards compatible "terse" format that never even
787     # looked at the pad, just like the old B::Terse. I don't think
788     # that's worth the effort, though.
789     $curcv = main_cv unless $curcv;
790
791     if ($order eq "exec" and $lastnext and $$lastnext != $$op) {
792         # insert a 'goto'
793         my $h = {"seq" => seq($lastnext), "class" => class($lastnext),
794                  "addr" => sprintf("%#x", $$lastnext)};
795         print # $walkHandle
796             fmt_line($h, $op, $style{"terse"}[1], $level+1);
797     }
798     $lastnext = $op->next;
799     print # $walkHandle 
800         concise_op($op, $level, $style{"terse"}[0]);
801 }
802
803 sub tree {
804     my $op = shift;
805     my $level = shift;
806     my $style = $tree_decorations[$tree_style];
807     my($space, $single, $kids, $kid, $nokid, $last, $lead, $size) = @$style;
808     my $name = concise_op($op, $level, $treefmt);
809     if (not $op->flags & OPf_KIDS) {
810         return $name . "\n";
811     }
812     my @lines;
813     for (my $kid = $op->first; $$kid; $kid = $kid->sibling) {
814         push @lines, tree($kid, $level+1);
815     }
816     my $i;
817     for ($i = $#lines; substr($lines[$i], 0, 1) eq " "; $i--) {
818         $lines[$i] = $space . $lines[$i];
819     }
820     if ($i > 0) {
821         $lines[$i] = $last . $lines[$i];
822         while ($i-- > 1) {
823             if (substr($lines[$i], 0, 1) eq " ") {
824                 $lines[$i] = $nokid . $lines[$i];
825             } else {
826                 $lines[$i] = $kid . $lines[$i];
827             }
828         }
829         $lines[$i] = $kids . $lines[$i];
830     } else {
831         $lines[0] = $single . $lines[0];
832     }
833     return("$name$lead" . shift @lines,
834            map(" " x (length($name)+$size) . $_, @lines));
835 }
836
837 # *** Warning: fragile kludge ahead ***
838 # Because the B::* modules run in the same interpreter as the code
839 # they're compiling, their presence tends to distort the view we have of
840 # the code we're looking at. In particular, perl gives sequence numbers
841 # to COPs. If the program we're looking at were run on its own, this
842 # would start at 1. Because all of B::Concise and all the modules it
843 # uses are compiled first, though, by the time we get to the user's
844 # program the sequence number is already pretty high, which could be
845 # distracting if you're trying to tell OPs apart. Therefore we'd like to
846 # subtract an offset from all the sequence numbers we display, to
847 # restore the simpler view of the world. The trick is to know what that
848 # offset will be, when we're still compiling B::Concise!  If we
849 # hardcoded a value, it would have to change every time B::Concise or
850 # other modules we use do. To help a little, what we do here is compile
851 # a little code at the end of the module, and compute the base sequence
852 # number for the user's program as being a small offset later, so all we
853 # have to worry about are changes in the offset.
854  
855 # When you say "perl -MO=Concise -e '$a'", the output should look like:
856
857 # 4  <@> leave[t1] vKP/REFC ->(end)
858 # 1     <0> enter ->2
859  #^ smallest OP sequence number should be 1
860 # 2     <;> nextstate(main 1 -e:1) v ->3
861  #                         ^ smallest COP sequence number should be 1
862 # -     <1> ex-rv2sv vK/1 ->4
863 # 3        <$> gvsv(*a) s ->4
864
865 # If the second of the marked numbers there isn't 1, it means you need
866 # to update the corresponding magic number in the next line.
867 # Remember, this needs to stay the last things in the module.
868
869 # Why is this different for MacOS?  Does it matter?
870 my $cop_seq_mnum = $^O eq 'MacOS' ? 12 : 11;
871 $cop_seq_base = svref_2object(eval 'sub{0;}')->START->cop_seq + $cop_seq_mnum;
872
873 1;
874
875 __END__
876
877 =head1 NAME
878
879 B::Concise - Walk Perl syntax tree, printing concise info about ops
880
881 =head1 SYNOPSIS
882
883     perl -MO=Concise[,OPTIONS] foo.pl
884
885     use B::Concise qw(set_style add_callback);
886
887 =head1 DESCRIPTION
888
889 This compiler backend prints the internal OPs of a Perl program's syntax
890 tree in one of several space-efficient text formats suitable for debugging
891 the inner workings of perl or other compiler backends. It can print OPs in
892 the order they appear in the OP tree, in the order they will execute, or
893 in a text approximation to their tree structure, and the format of the
894 information displyed is customizable. Its function is similar to that of
895 perl's B<-Dx> debugging flag or the B<B::Terse> module, but it is more
896 sophisticated and flexible.
897
898 =head1 EXAMPLE
899
900 Here's is a short example of output (aka 'rendering'), using the
901 default formatting conventions :
902
903     % perl -MO=Concise -e '$a = $b + 42'
904     8  <@> leave[1 ref] vKP/REFC ->(end)
905     1     <0> enter ->2
906     2     <;> nextstate(main 1 -e:1) v ->3
907     7     <2> sassign vKS/2 ->8
908     5        <2> add[t1] sK/2 ->6
909     -           <1> ex-rv2sv sK/1 ->4
910     3              <$> gvsv(*b) s ->4
911     4           <$> const(IV 42) s ->5
912     -        <1> ex-rv2sv sKRM*/1 ->7
913     6           <$> gvsv(*a) s ->7
914
915 Each line corresponds to an opcode. Null ops appear as C<ex-opname>,
916 where I<opname> is the op that has been optimized away by perl.
917
918 The number on the first row indicates the op's sequence number. It's
919 given in base 36 by default.
920
921 The symbol between angle brackets indicates the op's type : for example,
922 <2> is a BINOP, <@> a LISTOP, etc. (see L</"OP class abbreviations">).
923
924 The opname may be followed by op-specific information in parentheses
925 (e.g. C<gvsv(*b)>), and by targ information in brackets (e.g.
926 C<leave[t1]>).
927
928 Next come the op flags. The common flags are listed below
929 (L</"OP flags abbreviations">). The private flags follow, separated
930 by a slash. For example, C<vKP/REFC> means that the leave op has
931 public flags OPf_WANT_VOID, OPf_KIDS, and OPf_PARENS, and the private
932 flag OPpREFCOUNTED.
933
934 Finally an arrow points to the sequence number of the next op.
935
936 =head1 OPTIONS
937
938 Arguments that don't start with a hyphen are taken to be the names of
939 subroutines to print the OPs of; if no such functions are specified,
940 the main body of the program (outside any subroutines, and not
941 including use'd or require'd files) is printed. Passing C<BEGIN>,
942 C<CHECK>, C<INIT>, or C<END> will cause all of the corresponding
943 special blocks to be printed.
944
945 Options affect how things are rendered (ie printed).  They're presented
946 here by their visual effect, 1st being strongest.  They're grouped
947 according to how they interrelate; within each group the options are
948 mutually exclusive (unless otherwise stated).
949
950 =head2 Options for Opcode Ordering
951
952 These options control the 'vertical display' of opcodes.  The display
953 'order' is also called 'mode' elsewhere in this document.
954
955 =over 4
956
957 =item B<-basic>
958
959 Print OPs in the order they appear in the OP tree (a preorder
960 traversal, starting at the root). The indentation of each OP shows its
961 level in the tree.  This mode is the default, so the flag is included
962 simply for completeness.
963
964 =item B<-exec>
965
966 Print OPs in the order they would normally execute (for the majority
967 of constructs this is a postorder traversal of the tree, ending at the
968 root). In most cases the OP that usually follows a given OP will
969 appear directly below it; alternate paths are shown by indentation. In
970 cases like loops when control jumps out of a linear path, a 'goto'
971 line is generated.
972
973 =item B<-tree>
974
975 Print OPs in a text approximation of a tree, with the root of the tree
976 at the left and 'left-to-right' order of children transformed into
977 'top-to-bottom'. Because this mode grows both to the right and down,
978 it isn't suitable for large programs (unless you have a very wide
979 terminal).
980
981 =back
982
983 =head2 Options for Line-Style
984
985 These options select the line-style (or just style) used to render
986 each opcode, and dictates what info is actually printed into each line.
987
988 =over 4
989
990 =item B<-concise>
991
992 Use the author's favorite set of formatting conventions. This is the
993 default, of course.
994
995 =item B<-terse>
996
997 Use formatting conventions that emulate the output of B<B::Terse>. The
998 basic mode is almost indistinguishable from the real B<B::Terse>, and the
999 exec mode looks very similar, but is in a more logical order and lacks
1000 curly brackets. B<B::Terse> doesn't have a tree mode, so the tree mode
1001 is only vaguely reminiscent of B<B::Terse>.
1002
1003 =item B<-linenoise>
1004
1005 Use formatting conventions in which the name of each OP, rather than being
1006 written out in full, is represented by a one- or two-character abbreviation.
1007 This is mainly a joke.
1008
1009 =item B<-debug>
1010
1011 Use formatting conventions reminiscent of B<B::Debug>; these aren't
1012 very concise at all.
1013
1014 =item B<-env>
1015
1016 Use formatting conventions read from the environment variables
1017 C<B_CONCISE_FORMAT>, C<B_CONCISE_GOTO_FORMAT>, and C<B_CONCISE_TREE_FORMAT>.
1018
1019 =back
1020
1021 =head2 Options for tree-specific formatting
1022
1023 =over 4
1024
1025 =item B<-compact>
1026
1027 Use a tree format in which the minimum amount of space is used for the
1028 lines connecting nodes (one character in most cases). This squeezes out
1029 a few precious columns of screen real estate.
1030
1031 =item B<-loose>
1032
1033 Use a tree format that uses longer edges to separate OP nodes. This format
1034 tends to look better than the compact one, especially in ASCII, and is
1035 the default.
1036
1037 =item B<-vt>
1038
1039 Use tree connecting characters drawn from the VT100 line-drawing set.
1040 This looks better if your terminal supports it.
1041
1042 =item B<-ascii>
1043
1044 Draw the tree with standard ASCII characters like C<+> and C<|>. These don't
1045 look as clean as the VT100 characters, but they'll work with almost any
1046 terminal (or the horizontal scrolling mode of less(1)) and are suitable
1047 for text documentation or email. This is the default.
1048
1049 =back
1050
1051 These are pairwise exclusive, i.e. compact or loose, vt or ascii.
1052
1053 =head2 Options controlling sequence numbering
1054
1055 =over 4
1056
1057 =item B<-base>I<n>
1058
1059 Print OP sequence numbers in base I<n>. If I<n> is greater than 10, the
1060 digit for 11 will be 'a', and so on. If I<n> is greater than 36, the digit
1061 for 37 will be 'A', and so on until 62. Values greater than 62 are not
1062 currently supported. The default is 36.
1063
1064 =item B<-bigendian>
1065
1066 Print sequence numbers with the most significant digit first. This is the
1067 usual convention for Arabic numerals, and the default.
1068
1069 =item B<-littleendian>
1070
1071 Print seqence numbers with the least significant digit first.  This is
1072 obviously mutually exclusive with bigendian.
1073
1074 =back
1075
1076 =head2 Other options
1077
1078 These are pairwise exclusive.
1079
1080 =over 4
1081
1082 =item B<-main>
1083
1084 Include the main program in the output, even if subroutines were also
1085 specified.  This rendering is normally suppressed when a subroutine
1086 name or reference is given.
1087
1088 =item B<-nomain>
1089
1090 This restores the default behavior after you've changed it with '-main'
1091 (it's not normally needed).  If no subroutine name/ref is given, main is
1092 rendered, regardless of this flag.
1093
1094 =item B<-nobanner>
1095
1096 Renderings usually include a banner line identifying the function name
1097 or stringified subref.  This suppresses the printing of the banner.
1098
1099 TBC: Remove the stringified coderef; while it provides a 'cookie' for
1100 each function rendered, the cookies used should be 1,2,3.. not a
1101 random hex-address.  It also complicates string comparison of two
1102 different trees.
1103
1104 =item B<-banner>
1105
1106 restores default banner behavior.
1107
1108 =item B<-banneris> => subref
1109
1110 TBC: a hookpoint (and an option to set it) for a user-supplied
1111 function to produce a banner appropriate for users needs.  It's not
1112 ideal, because the rendering-state variables, which are a natural
1113 candidate for use in concise.t, are unavailable to the user.
1114
1115 =back
1116
1117 =head2 Option Stickiness
1118
1119 If you invoke Concise more than once in a program, you should know that
1120 the options are 'sticky'.  This means that the options you provide in
1121 the first call will be remembered for the 2nd call, unless you
1122 re-specify or change them.
1123
1124 =head1 ABBREVIATIONS
1125
1126 The concise style uses symbols to convey maximum info with minimal
1127 clutter (like hex addresses).  With just a little practice, you can
1128 start to see the flowers, not just the branches, in the trees.
1129
1130 =head2 OP class abbreviations
1131
1132 These symbols appear before the op-name, and indicate the
1133 B:: namespace that represents the ops in your Perl code.
1134
1135     0      OP (aka BASEOP)  An OP with no children
1136     1      UNOP             An OP with one child
1137     2      BINOP            An OP with two children
1138     |      LOGOP            A control branch OP
1139     @      LISTOP           An OP that could have lots of children
1140     /      PMOP             An OP with a regular expression
1141     $      SVOP             An OP with an SV
1142     "      PVOP             An OP with a string
1143     {      LOOP             An OP that holds pointers for a loop
1144     ;      COP              An OP that marks the start of a statement
1145     #      PADOP            An OP with a GV on the pad
1146
1147 =head2 OP flags abbreviations
1148
1149 These symbols represent various flags which alter behavior of the
1150 opcode, sometimes in opcode-specific ways.
1151
1152     v      OPf_WANT_VOID    Want nothing (void context)
1153     s      OPf_WANT_SCALAR  Want single value (scalar context)
1154     l      OPf_WANT_LIST    Want list of any length (list context)
1155     K      OPf_KIDS         There is a firstborn child.
1156     P      OPf_PARENS       This operator was parenthesized.
1157                              (Or block needs explicit scope entry.)
1158     R      OPf_REF          Certified reference.
1159                              (Return container, not containee).
1160     M      OPf_MOD          Will modify (lvalue).
1161     S      OPf_STACKED      Some arg is arriving on the stack.
1162     *      OPf_SPECIAL      Do something weird for this op (see op.h)
1163
1164 =head1 FORMATTING SPECIFICATIONS
1165
1166 For each line-style ('concise', 'terse', 'linenoise', etc.) there are
1167 3 format-specs which control how OPs are rendered.
1168
1169 The first is the 'default' format, which is used in both basic and exec
1170 modes to print all opcodes.  The 2nd, goto-format, is used in exec
1171 mode when branches are encountered.  They're not real opcodes, and are
1172 inserted to look like a closing curly brace.  The tree-format is tree
1173 specific.
1174
1175 When a line is rendered, the correct format-spec is copied and scanned
1176 for the following items; data is substituted in, and other
1177 manipulations like basic indenting are done, for each opcode rendered.
1178
1179 There are 3 kinds of items that may be populated; special patterns,
1180 #vars, and literal text, which is copied verbatim.  (Yes, it's a set
1181 of s///g steps.)
1182
1183 =head2 Special Patterns
1184
1185 These items are the primitives used to perform indenting, and to
1186 select text from amongst alternatives.
1187
1188 =over 4
1189
1190 =item B<(x(>I<exec_text>B<;>I<basic_text>B<)x)>
1191
1192 Generates I<exec_text> in exec mode, or I<basic_text> in basic mode.
1193
1194 =item B<(*(>I<text>B<)*)>
1195
1196 Generates one copy of I<text> for each indentation level.
1197
1198 =item B<(*(>I<text1>B<;>I<text2>B<)*)>
1199
1200 Generates one fewer copies of I<text1> than the indentation level, followed
1201 by one copy of I<text2> if the indentation level is more than 0.
1202
1203 =item B<(?(>I<text1>B<#>I<var>I<Text2>B<)?)>
1204
1205 If the value of I<var> is true (not empty or zero), generates the
1206 value of I<var> surrounded by I<text1> and I<Text2>, otherwise
1207 nothing.
1208
1209 =item B<~>
1210
1211 Any number of tildes and surrounding whitespace will be collapsed to
1212 a single space.
1213
1214 =back
1215
1216 =head2 # Variables
1217
1218 These #vars represent opcode properties that you may want as part of
1219 your rendering.  The '#' is intended as a private sigil; a #var's
1220 value is interpolated into the style-line, much like "read $this".
1221
1222 These vars take 3 forms:
1223
1224 =over 4
1225
1226 =item B<#>I<var>
1227
1228 A property named 'var' is assumed to exist for the opcodes, and is
1229 interpolated into the rendering.
1230
1231 =item B<#>I<var>I<N>
1232
1233 Generates the value of I<var>, left justified to fill I<N> spaces.
1234 Note that this means while you can have properties 'foo' and 'foo2',
1235 you cannot render 'foo2', but you could with 'foo2a'.  You would be
1236 wise not to rely on this behavior going forward ;-)
1237
1238 =item B<#>I<Var>
1239
1240 This ucfirst form of #var generates a tag-value form of itself for
1241 display; it converts '#Var' into a 'Var => #var' style, which is then
1242 handled as described above.  (Imp-note: #Vars cannot be used for
1243 conditional-fills, because the => #var transform is done after the check
1244 for #Var's value).
1245
1246 =back
1247
1248 The following variables are 'defined' by B::Concise; when they are
1249 used in a style, their respective values are plugged into the
1250 rendering of each opcode.
1251
1252 Only some of these are used by the standard styles, the others are
1253 provided for you to delve into optree mechanics, should you wish to
1254 add a new style (see L</add_style> below) that uses them.  You can
1255 also add new ones using L<add_callback>.
1256
1257 =over 4
1258
1259 =item B<#addr>
1260
1261 The address of the OP, in hexadecimal.
1262
1263 =item B<#arg>
1264
1265 The OP-specific information of the OP (such as the SV for an SVOP, the
1266 non-local exit pointers for a LOOP, etc.) enclosed in parentheses.
1267
1268 =item B<#class>
1269
1270 The B-determined class of the OP, in all caps.
1271
1272 =item B<#classsym>
1273
1274 A single symbol abbreviating the class of the OP.
1275
1276 =item B<#coplabel>
1277
1278 The label of the statement or block the OP is the start of, if any.
1279
1280 =item B<#exname>
1281
1282 The name of the OP, or 'ex-foo' if the OP is a null that used to be a foo.
1283
1284 =item B<#extarg>
1285
1286 The target of the OP, or nothing for a nulled OP.
1287
1288 =item B<#firstaddr>
1289
1290 The address of the OP's first child, in hexidecimal.
1291
1292 =item B<#flags>
1293
1294 The OP's flags, abbreviated as a series of symbols.
1295
1296 =item B<#flagval>
1297
1298 The numeric value of the OP's flags.
1299
1300 =item B<#hyphseq>
1301
1302 The sequence number of the OP, or a hyphen if it doesn't have one.
1303
1304 =item B<#label>
1305
1306 'NEXT', 'LAST', or 'REDO' if the OP is a target of one of those in exec
1307 mode, or empty otherwise.
1308
1309 =item B<#lastaddr>
1310
1311 The address of the OP's last child, in hexidecimal.
1312
1313 =item B<#name>
1314
1315 The OP's name.
1316
1317 =item B<#NAME>
1318
1319 The OP's name, in all caps.
1320
1321 =item B<#next>
1322
1323 The sequence number of the OP's next OP.
1324
1325 =item B<#nextaddr>
1326
1327 The address of the OP's next OP, in hexidecimal.
1328
1329 =item B<#noise>
1330
1331 A one- or two-character abbreviation for the OP's name.
1332
1333 =item B<#private>
1334
1335 The OP's private flags, rendered with abbreviated names if possible.
1336
1337 =item B<#privval>
1338
1339 The numeric value of the OP's private flags.
1340
1341 =item B<#seq>
1342
1343 The sequence number of the OP. Note that this is a sequence number
1344 generated by B::Concise.
1345
1346 =item B<#opt>
1347
1348 Whether or not the op has been optimised by the peephole optimiser.
1349
1350 =item B<#static>
1351
1352 Whether or not the op is statically defined.  This flag is used by the
1353 B::C compiler backend and indicates that the op should not be freed.
1354
1355 =item B<#sibaddr>
1356
1357 The address of the OP's next youngest sibling, in hexidecimal.
1358
1359 =item B<#svaddr>
1360
1361 The address of the OP's SV, if it has an SV, in hexidecimal.
1362
1363 =item B<#svclass>
1364
1365 The class of the OP's SV, if it has one, in all caps (e.g., 'IV').
1366
1367 =item B<#svval>
1368
1369 The value of the OP's SV, if it has one, in a short human-readable format.
1370
1371 =item B<#targ>
1372
1373 The numeric value of the OP's targ.
1374
1375 =item B<#targarg>
1376
1377 The name of the variable the OP's targ refers to, if any, otherwise the
1378 letter t followed by the OP's targ in decimal.
1379
1380 =item B<#targarglife>
1381
1382 Same as B<#targarg>, but followed by the COP sequence numbers that delimit
1383 the variable's lifetime (or 'end' for a variable in an open scope) for a
1384 variable.
1385
1386 =item B<#typenum>
1387
1388 The numeric value of the OP's type, in decimal.
1389
1390 =back
1391
1392 =head1 Using B::Concise outside of the O framework
1393
1394 The common (and original) usage of B::Concise was for command-line
1395 renderings of simple code, as given in EXAMPLE.  But you can also use
1396 B<B::Concise> from your code, and call compile() directly, and
1397 repeatedly.  By doing so, you can avoid the compile-time only
1398 operation of O.pm, and even use the debugger to step through
1399 B::Concise::compile() itself.
1400
1401 Once you're doing this, you may alter Concise output by adding new
1402 rendering styles, and by optionally adding callback routines which
1403 populate new variables, if such were referenced from those (just
1404 added) styles.  
1405
1406 =head2 Example: Altering Concise Renderings
1407
1408     use B::Concise qw(set_style add_callback);
1409     add_style($yourStyleName => $defaultfmt, $gotofmt, $treefmt);
1410     add_callback
1411       ( sub {
1412             my ($h, $op, $format, $level, $stylename) = @_;
1413             $h->{variable} = some_func($op);
1414         });
1415     $walker = B::Concise::compile(@options,@subnames,@subrefs);
1416     $walker->();
1417
1418 =head2 set_style()
1419
1420 B<set_style> accepts 3 arguments, and updates the three format-specs
1421 comprising a line-style (basic-exec, goto, tree).  It has one minor
1422 drawback though; it doesn't register the style under a new name.  This
1423 can become an issue if you render more than once and switch styles.
1424 Thus you may prefer to use add_style() and/or set_style_standard()
1425 instead.
1426
1427 =head2 set_style_standard($name)
1428
1429 This restores one of the standard line-styles: C<terse>, C<concise>,
1430 C<linenoise>, C<debug>, C<env>, into effect.  It also accepts style
1431 names previously defined with add_style().
1432
1433 =head2 add_style()
1434
1435 This subroutine accepts a new style name and three style arguments as
1436 above, and creates, registers, and selects the newly named style.  It is
1437 an error to re-add a style; call set_style_standard() to switch between
1438 several styles.
1439
1440 =head2 add_callback()
1441
1442 If your newly minted styles refer to any #variables, you'll need to
1443 define a callback subroutine that will populate (or modify) those
1444 variables.  They are then available for use in the style you've chosen.
1445
1446 The callbacks are called for each opcode visited by Concise, in the
1447 same order as they are added.  Each subroutine is passed five
1448 parameters.
1449
1450   1. A hashref, containing the variable names and values which are
1451      populated into the report-line for the op
1452   2. the op, as a B<B::OP> object
1453   3. a reference to the format string
1454   4. the formatting (indent) level
1455   5. the selected stylename
1456
1457 To define your own variables, simply add them to the hash, or change
1458 existing values if you need to.  The level and format are passed in as
1459 references to scalars, but it is unlikely that they will need to be
1460 changed or even used.
1461
1462 =head2 Running B::Concise::compile()
1463
1464 B<compile> accepts options as described above in L</OPTIONS>, and
1465 arguments, which are either coderefs, or subroutine names.
1466
1467 It constructs and returns a $treewalker coderef, which when invoked,
1468 traverses, or walks, and renders the optrees of the given arguments to
1469 STDOUT.  You can reuse this, and can change the rendering style used
1470 each time; thereafter the coderef renders in the new style.
1471
1472 B<walk_output> lets you change the print destination from STDOUT to
1473 another open filehandle, or (unless you've built with -Uuseperlio)
1474 into a string passed as a ref.
1475
1476     my $walker = B::Concise::compile('-terse','aFuncName', \&aSubRef);  # 1
1477     walk_output(\my $buf);
1478     $walker->();                        # 1 renders -terse
1479     set_style_standard('concise');      # 2
1480     $walker->();                        # 2 renders -concise
1481     $walker->(@new);                    # 3 renders whatever
1482     print "3 different renderings: terse, concise, and @new: $buf\n";
1483
1484 When $walker is called, it traverses the subroutines supplied when it
1485 was created, and renders them using the current style.  You can change
1486 the style afterwards in several different ways:
1487
1488   1. call C<compile>, altering style or mode/order
1489   2. call C<set_style_standard>
1490   3. call $walker, passing @new options
1491
1492 Passing new options to the $walker is the easiest way to change
1493 amongst any pre-defined styles (the ones you add are automatically
1494 recognized as options), and is the only way to alter rendering order
1495 without calling compile again.  Note however that rendering state is
1496 still shared amongst multiple $walker objects, so they must still be
1497 used in a coordinated manner.
1498
1499 =head2 B::Concise::reset_sequence()
1500
1501 This function (not exported) lets you reset the sequence numbers (note
1502 that they're numbered arbitrarily, their goal being to be human
1503 readable).  Its purpose is mostly to support testing, i.e. to compare
1504 the concise output from two identical anonymous subroutines (but
1505 different instances).  Without the reset, B::Concise, seeing that
1506 they're separate optrees, generates different sequence numbers in
1507 the output.
1508
1509 =head2 Errors
1510
1511 All detected errors, (invalid arguments, internal errors, etc.) are
1512 resolved with a die($message). Use an eval if you wish to catch these
1513 errors and continue processing.
1514
1515 In particular, B<compile> will die if you've asked for a non-existent
1516 function-name, a non-existent coderef, or a non-CODE reference.
1517
1518 =head1 AUTHOR
1519
1520 Stephen McCamant, E<lt>smcc@CSUA.Berkeley.EDUE<gt>.
1521
1522 =cut