Extra tweakage from Rafael for #19392.
[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 use strict;
7 use warnings;
8
9 use Exporter ();
10
11 our $VERSION   = "0.55";
12 our @ISA       = qw(Exporter);
13 our @EXPORT_OK = qw(set_style set_style_standard add_callback
14                     concise_cv concise_main);
15
16 use B qw(class ppname main_start main_root main_cv cstring svref_2object
17          SVf_IOK SVf_NOK SVf_POK SVf_IVisUV OPf_KIDS);
18
19 my %style = 
20   ("terse" =>
21    ["(?(#label =>\n)?)(*(    )*)#class (#addr) #name (?([#targ])?) "
22     . "#svclass~(?((#svaddr))?)~#svval~(?(label \"#coplabel\")?)\n",
23     "(*(    )*)goto #class (#addr)\n",
24     "#class pp_#name"],
25    "concise" =>
26    ["#hyphseq2 (*(   (x( ;)x))*)<#classsym> "
27     . "#exname#arg(?([#targarglife])?)~#flags(?(/#private)?)(x(;~->#next)x)\n",
28     "  (*(    )*)     goto #seq\n",
29     "(?(<#seq>)?)#exname#arg(?([#targarglife])?)"],
30    "linenoise" =>
31    ["(x(;(*( )*))x)#noise#arg(?([#targarg])?)(x( ;\n)x)",
32     "gt_#seq ",
33     "(?(#seq)?)#noise#arg(?([#targarg])?)"],
34    "debug" =>
35    ["#class (#addr)\n\top_next\t\t#nextaddr\n\top_sibling\t#sibaddr\n\t"
36     . "op_ppaddr\tPL_ppaddr[OP_#NAME]\n\top_type\t\t#typenum\n\top_seq\t\t"
37     . "#seqnum\n\top_flags\t#flagval\n\top_private\t#privval\n"
38     . "(?(\top_first\t#firstaddr\n)?)(?(\top_last\t\t#lastaddr\n)?)"
39     . "(?(\top_sv\t\t#svaddr\n)?)",
40     "    GOTO #addr\n",
41     "#addr"],
42    "env" => [$ENV{B_CONCISE_FORMAT}, $ENV{B_CONCISE_GOTO_FORMAT},
43              $ENV{B_CONCISE_TREE_FORMAT}],
44   );
45
46 my($format, $gotofmt, $treefmt);
47 my $curcv;
48 my $cop_seq_base;
49 my @callbacks;
50
51 sub set_style {
52     ($format, $gotofmt, $treefmt) = @_;
53 }
54
55 sub set_style_standard {
56     my($name) = @_;
57     set_style(@{$style{$name}});
58 }
59
60 sub add_callback {
61     push @callbacks, @_;
62 }
63
64 sub concise_cv {
65     my ($order, $cvref) = @_;
66     my $cv = svref_2object($cvref);
67     $curcv = $cv;
68     sequence($cv->START);
69     if ($order eq "exec") {
70         walk_exec($cv->START);
71     } elsif ($order eq "basic") {
72         walk_topdown($cv->ROOT, sub { $_[0]->concise($_[1]) }, 0);
73     } else {
74         print tree($cv->ROOT, 0)
75     }
76 }
77
78 sub concise_main {
79     my($order) = @_;
80     sequence(main_start);
81     $curcv = main_cv;
82     if ($order eq "exec") {
83         return if class(main_start) eq "NULL";
84         walk_exec(main_start);
85     } elsif ($order eq "tree") {
86         return if class(main_root) eq "NULL";
87         print tree(main_root, 0);
88     } elsif ($order eq "basic") {
89         return if class(main_root) eq "NULL";
90         walk_topdown(main_root,
91                      sub { $_[0]->concise($_[1]) }, 0);
92     }
93 }
94
95 my $start_sym = "\e(0"; # "\cN" sometimes also works
96 my $end_sym   = "\e(B"; # "\cO" respectively
97
98 my @tree_decorations = 
99   (["  ", "--", "+-", "|-", "| ", "`-", "-", 1],
100    [" ", "-", "+", "+", "|", "`", "", 0],
101    ["  ", map("$start_sym$_$end_sym", "qq", "wq", "tq", "x ", "mq", "q"), 1],
102    [" ", map("$start_sym$_$end_sym", "q", "w", "t", "x", "m"), "", 0],
103   );
104 my $tree_style = 0;
105
106 my $base = 36;
107 my $big_endian = 1;
108
109 my $order = "basic";
110
111 set_style_standard("concise");
112
113 sub compile {
114     my @options = grep(/^-/, @_);
115     my @args = grep(!/^-/, @_);
116     my $do_main = 0;
117     for my $o (@options) {
118         if ($o eq "-basic") {
119             $order = "basic";
120         } elsif ($o eq "-exec") {
121             $order = "exec";
122         } elsif ($o eq "-tree") {
123             $order = "tree";
124         } elsif ($o eq "-compact") {
125             $tree_style |= 1;
126         } elsif ($o eq "-loose") {
127             $tree_style &= ~1;
128         } elsif ($o eq "-vt") {
129             $tree_style |= 2;
130         } elsif ($o eq "-ascii") {
131             $tree_style &= ~2;
132         } elsif ($o eq "-main") {
133             $do_main = 1;
134         } elsif ($o =~ /^-base(\d+)$/) {
135             $base = $1;
136         } elsif ($o eq "-bigendian") {
137             $big_endian = 1;
138         } elsif ($o eq "-littleendian") {
139             $big_endian = 0;
140         } elsif (exists $style{substr($o, 1)}) {
141             set_style(@{$style{substr($o, 1)}});
142         } else {
143             warn "Option $o unrecognized";
144         }
145     }
146     return sub {
147         if (@args) {
148             for my $objname (@args) {
149                 $objname = "main::" . $objname unless $objname =~ /::/;
150                 print "$objname:\n";
151                 eval "concise_cv(\$order, \\&$objname)";
152                 die "concise_cv($order, \\&$objname) failed: $@" if $@;
153             }
154         }
155         if (!@args or $do_main) {
156             print "main program:\n" if $do_main;
157             concise_main($order);
158         }
159     }
160 }
161
162 my %labels;
163 my $lastnext;
164
165 my %opclass = ('OP' => "0", 'UNOP' => "1", 'BINOP' => "2", 'LOGOP' => "|",
166                'LISTOP' => "@", 'PMOP' => "/", 'SVOP' => "\$", 'GVOP' => "*",
167                'PVOP' => '"', 'LOOP' => "{", 'COP' => ";", 'PADOP' => "#");
168
169 no warnings 'qw'; # "Possible attempt to put comments..."
170 my @linenoise =
171   qw'#  () sc (  @? 1  $* gv *{ m$ m@ m% m? p/ *$ $  $# & a& pt \\ s\\ rf bl
172      `  *? <> ?? ?/ r/ c/ // qr s/ /c y/ =  @= C  sC Cp sp df un BM po +1 +I
173      -1 -I 1+ I+ 1- I- ** *  i* /  i/ %$ i% x  +  i+ -  i- .  "  << >> <  i<
174      >  i> <= i, >= i. == i= != i! <? i? s< s> s, s. s= s! s? b& b^ b| -0 -i
175      !  ~  a2 si cs rd sr e^ lg sq in %x %o ab le ss ve ix ri sf FL od ch cy
176      uf lf uc lc qm @  [f [  @[ eh vl ky dl ex %  ${ @{ uk pk st jn )  )[ a@
177      a% sl +] -] [- [+ so rv GS GW MS MW .. f. .f && || ^^ ?: &= |= -> s{ s}
178      v} ca wa di rs ;; ;  ;d }{ {  }  {} f{ it {l l} rt }l }n }r dm }g }e ^o
179      ^c ^| ^# um bm t~ u~ ~d DB db ^s se ^g ^r {w }w pf pr ^O ^K ^R ^W ^d ^v
180      ^e ^t ^k t. fc ic fl .s .p .b .c .l .a .h g1 s1 g2 s2 ?. l? -R -W -X -r
181      -w -x -e -o -O -z -s -M -A -C -S -c -b -f -d -p -l -u -g -k -t -T -B cd
182      co cr u. cm ut r. l@ s@ r@ mD uD oD rD tD sD wD cD f$ w$ p$ sh e$ k$ g3
183      g4 s4 g5 s5 T@ C@ L@ G@ A@ S@ Hg Hc Hr Hw Mg Mc Ms Mr Sg Sc So rq do {e
184      e} {t t} g6 G6 6e g7 G7 7e g8 G8 8e g9 G9 9e 6s 7s 8s 9s 6E 7E 8E 9E Pn
185      Pu GP SP EP Gn Gg GG SG EG g0 c$ lk t$ ;s n> // /= CO';
186
187 my $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
188
189 sub op_flags {
190     my($x) = @_;
191     my(@v);
192     push @v, "v" if ($x & 3) == 1;
193     push @v, "s" if ($x & 3) == 2;
194     push @v, "l" if ($x & 3) == 3;
195     push @v, "K" if $x & 4;
196     push @v, "P" if $x & 8;
197     push @v, "R" if $x & 16;
198     push @v, "M" if $x & 32;
199     push @v, "S" if $x & 64;
200     push @v, "*" if $x & 128;
201     return join("", @v);
202 }
203
204 sub base_n {
205     my $x = shift;
206     return "-" . base_n(-$x) if $x < 0;
207     my $str = "";
208     do { $str .= substr($chars, $x % $base, 1) } while $x = int($x / $base);
209     $str = reverse $str if $big_endian;
210     return $str;
211 }
212
213 my %sequence_num;
214 my $seq_max = 1;
215
216 sub seq {
217     my($op) = @_;
218     return "-" if not exists $sequence_num{$$op};
219     return base_n($sequence_num{$$op});
220 }
221
222 sub walk_topdown {
223     my($op, $sub, $level) = @_;
224     $sub->($op, $level);
225     if ($op->flags & OPf_KIDS) {
226         for (my $kid = $op->first; $$kid; $kid = $kid->sibling) {
227             walk_topdown($kid, $sub, $level + 1);
228         }
229     }
230     if (class($op) eq "PMOP" and $op->pmreplroot and $ {$op->pmreplroot}
231         and $op->pmreplroot->isa("B::OP")) {
232         walk_topdown($op->pmreplroot, $sub, $level + 1);
233     }
234 }
235
236 sub walklines {
237     my($ar, $level) = @_;
238     for my $l (@$ar) {
239         if (ref($l) eq "ARRAY") {
240             walklines($l, $level + 1);
241         } else {
242             $l->concise($level);
243         }
244     }
245 }
246
247 sub walk_exec {
248     my($top, $level) = @_;
249     my %opsseen;
250     my @lines;
251     my @todo = ([$top, \@lines]);
252     while (@todo and my($op, $targ) = @{shift @todo}) {
253         for (; $$op; $op = $op->next) {
254             last if $opsseen{$$op}++;
255             push @$targ, $op;
256             my $name = $op->name;
257             if (class($op) eq "LOGOP") {
258                 my $ar = [];
259                 push @$targ, $ar;
260                 push @todo, [$op->other, $ar];
261             } elsif ($name eq "subst" and $ {$op->pmreplstart}) {
262                 my $ar = [];
263                 push @$targ, $ar;
264                 push @todo, [$op->pmreplstart, $ar];
265             } elsif ($name =~ /^enter(loop|iter)$/) {
266                 $labels{$op->nextop->seq} = "NEXT";
267                 $labels{$op->lastop->seq} = "LAST";
268                 $labels{$op->redoop->seq} = "REDO";             
269             }
270         }
271     }
272     walklines(\@lines, 0);
273 }
274
275 # The structure of this routine is purposely modeled after op.c's peep()
276 sub sequence {
277     my($op) = @_;
278     my $oldop = 0;
279     return if class($op) eq "NULL" or exists $sequence_num{$$op};
280     for (; $$op; $op = $op->next) {
281         last if exists $sequence_num{$$op};
282         my $name = $op->name;
283         if ($name =~ /^(null|scalar|lineseq|scope)$/) {
284             next if $oldop and $ {$op->next};
285         } else {
286             $sequence_num{$$op} = $seq_max++;
287             if (class($op) eq "LOGOP") {
288                 my $other = $op->other;
289                 $other = $other->next while $other->name eq "null";
290                 sequence($other);
291             } elsif (class($op) eq "LOOP") {
292                 my $redoop = $op->redoop;
293                 $redoop = $redoop->next while $redoop->name eq "null";
294                 sequence($redoop);
295                 my $nextop = $op->nextop;
296                 $nextop = $nextop->next while $nextop->name eq "null";
297                 sequence($nextop);
298                 my $lastop = $op->lastop;
299                 $lastop = $lastop->next while $lastop->name eq "null";
300                 sequence($lastop);
301             } elsif ($name eq "subst" and $ {$op->pmreplstart}) {
302                 my $replstart = $op->pmreplstart;
303                 $replstart = $replstart->next while $replstart->name eq "null";
304                 sequence($replstart);
305             }
306         }
307         $oldop = $op;
308     }
309 }
310
311 sub fmt_line {
312     my($hr, $fmt, $level) = @_;
313     my $text = $fmt;
314     $text =~ s/\(\?\(([^\#]*?)\#(\w+)([^\#]*?)\)\?\)/
315       $hr->{$2} ? $1.$hr->{$2}.$3 : ""/eg;
316     $text =~ s/\(x\((.*?);(.*?)\)x\)/$order eq "exec" ? $1 : $2/egs;
317     $text =~ s/\(\*\(([^;]*?)\)\*\)/$1 x $level/egs;
318     $text =~ s/\(\*\((.*?);(.*?)\)\*\)/$1 x ($level - 1) . $2 x ($level>0)/egs;
319     $text =~ s/#([a-zA-Z]+)(\d+)/sprintf("%-$2s", $hr->{$1})/eg;
320     $text =~ s/#([a-zA-Z]+)/$hr->{$1}/eg;
321     $text =~ s/[ \t]*~+[ \t]*/ /g;
322     return $text;
323 }
324
325 my %priv;
326 $priv{$_}{128} = "LVINTRO"
327   for ("pos", "substr", "vec", "threadsv", "gvsv", "rv2sv", "rv2hv", "rv2gv",
328        "rv2av", "rv2arylen", "aelem", "helem", "aslice", "hslice", "padsv",
329        "padav", "padhv");
330 $priv{$_}{64} = "REFC" for ("leave", "leavesub", "leavesublv", "leavewrite");
331 $priv{"aassign"}{64} = "COMMON";
332 $priv{"sassign"}{64} = "BKWARD";
333 $priv{$_}{64} = "RTIME" for ("match", "subst", "substcont");
334 @{$priv{"trans"}}{1,2,4,8,16,64} = ("<UTF", ">UTF", "IDENT", "SQUASH", "DEL",
335                                     "COMPL", "GROWS");
336 $priv{"repeat"}{64} = "DOLIST";
337 $priv{"leaveloop"}{64} = "CONT";
338 @{$priv{$_}}{32,64,96} = ("DREFAV", "DREFHV", "DREFSV")
339   for ("entersub", map("rv2${_}v", "a", "s", "h", "g"), "aelem", "helem");
340 $priv{"entersub"}{16} = "DBG";
341 $priv{"entersub"}{32} = "TARG";
342 @{$priv{$_}}{4,8,128} = ("INARGS","AMPER","NO()") for ("entersub", "rv2cv");
343 $priv{"gv"}{32} = "EARLYCV";
344 $priv{"aelem"}{16} = $priv{"helem"}{16} = "LVDEFER";
345 $priv{$_}{16} = "OURINTR" for ("gvsv", "rv2sv", "rv2av", "rv2hv", "r2gv");
346 $priv{$_}{16} = "TARGMY"
347   for (map(($_,"s$_"),"chop", "chomp"),
348        map(($_,"i_$_"), "postinc", "postdec", "multiply", "divide", "modulo",
349            "add", "subtract", "negate"), "pow", "concat", "stringify",
350        "left_shift", "right_shift", "bit_and", "bit_xor", "bit_or",
351        "complement", "atan2", "sin", "cos", "rand", "exp", "log", "sqrt",
352        "int", "hex", "oct", "abs", "length", "index", "rindex", "sprintf",
353        "ord", "chr", "crypt", "quotemeta", "join", "push", "unshift", "flock",
354        "chdir", "chown", "chroot", "unlink", "chmod", "utime", "rename",
355        "link", "symlink", "mkdir", "rmdir", "wait", "waitpid", "system",
356        "exec", "kill", "getppid", "getpgrp", "setpgrp", "getpriority",
357        "setpriority", "time", "sleep");
358 @{$priv{"const"}}{8,16,32,64,128} = ("STRICT","ENTERED", '$[', "BARE", "WARN");
359 $priv{"flip"}{64} = $priv{"flop"}{64} = "LINENUM";
360 $priv{"list"}{64} = "GUESSED";
361 $priv{"delete"}{64} = "SLICE";
362 $priv{"exists"}{64} = "SUB";
363 $priv{$_}{64} = "LOCALE"
364   for ("sort", "prtf", "sprintf", "slt", "sle", "seq", "sne", "sgt", "sge",
365        "scmp", "lc", "uc", "lcfirst", "ucfirst");
366 @{$priv{"sort"}}{1,2,4} = ("NUM", "INT", "REV");
367 $priv{"threadsv"}{64} = "SVREFd";
368 @{$priv{$_}}{16,32,64,128} = ("INBIN","INCR","OUTBIN","OUTCR")
369   for ("open", "backtick");
370 $priv{"exit"}{128} = "VMS";
371 $priv{$_}{2} = "FTACCESS"
372   for ("ftrread", "ftrwrite", "ftrexec", "fteread", "ftewrite", "fteexec");
373
374 sub private_flags {
375     my($name, $x) = @_;
376     my @s;
377     for my $flag (128, 96, 64, 32, 16, 8, 4, 2, 1) {
378         if ($priv{$name}{$flag} and $x & $flag and $x >= $flag) {
379             $x -= $flag;
380             push @s, $priv{$name}{$flag};
381         }
382     }
383     push @s, $x if $x;
384     return join(",", @s);
385 }
386
387 sub concise_sv {
388     my($sv, $hr) = @_;
389     $hr->{svclass} = class($sv);
390     $hr->{svclass} = "UV"
391       if $hr->{svclass} eq "IV" and $sv->FLAGS & SVf_IVisUV;
392     $hr->{svaddr} = sprintf("%#x", $$sv);
393     if ($hr->{svclass} eq "GV") {
394         my $gv = $sv;
395         my $stash = $gv->STASH->NAME;
396         if ($stash eq "main") {
397             $stash = "";
398         } else {
399             $stash = $stash . "::";
400         }
401         $hr->{svval} = "*$stash" . $gv->SAFENAME;
402         return "*$stash" . $gv->SAFENAME;
403     } else {
404         while (class($sv) eq "RV") {
405             $hr->{svval} .= "\\";
406             $sv = $sv->RV;
407         }
408         if (class($sv) eq "SPECIAL") {
409             $hr->{svval} .= ["Null", "sv_undef", "sv_yes", "sv_no"]->[$$sv];
410         } elsif ($sv->FLAGS & SVf_NOK) {
411             $hr->{svval} .= $sv->NV;
412         } elsif ($sv->FLAGS & SVf_IOK) {
413             $hr->{svval} .= $sv->int_value;
414         } elsif ($sv->FLAGS & SVf_POK) {
415             $hr->{svval} .= cstring($sv->PV);
416         } elsif (class($sv) eq "HV") {
417             $hr->{svval} .= 'HASH';
418         }
419         return $hr->{svclass} . " " .  $hr->{svval};
420     }
421 }
422
423 sub concise_op {
424     my ($op, $level, $format) = @_;
425     my %h;
426     $h{exname} = $h{name} = $op->name;
427     $h{NAME} = uc $h{name};
428     $h{class} = class($op);
429     $h{extarg} = $h{targ} = $op->targ;
430     $h{extarg} = "" unless $h{extarg};
431     if ($h{name} eq "null" and $h{targ}) {
432         $h{exname} = "ex-" . substr(ppname($h{targ}), 3);
433         $h{extarg} = "";
434     } elsif ($h{targ}) {
435         my $padname = (($curcv->PADLIST->ARRAY)[0]->ARRAY)[$h{targ}];
436         if (defined $padname and class($padname) ne "SPECIAL") {
437             $h{targarg}  = $padname->PVX;
438             my $intro = $padname->NVX - $cop_seq_base;
439             my $finish = int($padname->IVX) - $cop_seq_base;
440             $finish = "end" if $finish == 999999999 - $cop_seq_base;
441             $h{targarglife} = "$h{targarg}:$intro,$finish";
442         } else {
443             $h{targarglife} = $h{targarg} = "t" . $h{targ};
444         }
445     }
446     $h{arg} = "";
447     $h{svclass} = $h{svaddr} = $h{svval} = "";
448     if ($h{class} eq "PMOP") {
449         my $precomp = $op->precomp;
450         if (defined $precomp) {
451             $precomp = cstring($precomp); # Escape literal control sequences
452             $precomp = "/$precomp/";
453         } else {
454             $precomp = "";
455         }
456         my $pmreplroot = $op->pmreplroot;
457         my $pmreplstart;
458         if ($pmreplroot && $$pmreplroot && $pmreplroot->isa("B::GV")) {
459             # with C<@stash_array = split(/pat/, str);>,
460             #  *stash_array is stored in pmreplroot.
461             $h{arg} = "($precomp => \@" . $pmreplroot->NAME . ")";
462         } elsif ($ {$op->pmreplstart}) {
463             undef $lastnext;
464             $pmreplstart = "replstart->" . seq($op->pmreplstart);
465             $h{arg} = "(" . join(" ", $precomp, $pmreplstart) . ")";
466         } else {
467             $h{arg} = "($precomp)";
468         }
469     } elsif ($h{class} eq "PVOP" and $h{name} ne "trans") {
470         $h{arg} = '("' . $op->pv . '")';
471         $h{svval} = '"' . $op->pv . '"';
472     } elsif ($h{class} eq "COP") {
473         my $label = $op->label;
474         $h{coplabel} = $label;
475         $label = $label ? "$label: " : "";
476         my $loc = $op->file;
477         $loc =~ s[.*/][];
478         $loc .= ":" . $op->line;
479         my($stash, $cseq) = ($op->stash->NAME, $op->cop_seq - $cop_seq_base);
480         my $arybase = $op->arybase;
481         $arybase = $arybase ? ' $[=' . $arybase : "";
482         $h{arg} = "($label$stash $cseq $loc$arybase)";
483     } elsif ($h{class} eq "LOOP") {
484         $h{arg} = "(next->" . seq($op->nextop) . " last->" . seq($op->lastop)
485           . " redo->" . seq($op->redoop) . ")";
486     } elsif ($h{class} eq "LOGOP") {
487         undef $lastnext;
488         $h{arg} = "(other->" . seq($op->other) . ")";
489     } elsif ($h{class} eq "SVOP") {
490         if (! ${$op->sv}) {
491             my $sv = (($curcv->PADLIST->ARRAY)[1]->ARRAY)[$op->targ];
492             $h{arg} = "[" . concise_sv($sv, \%h) . "]";
493             $h{targarglife} = $h{targarg} = "";
494         } else {
495             $h{arg} = "(" . concise_sv($op->sv, \%h) . ")";
496         }
497     } elsif ($h{class} eq "PADOP") {
498         my $sv = (($curcv->PADLIST->ARRAY)[1]->ARRAY)[$op->padix];
499         $h{arg} = "[" . concise_sv($sv, \%h) . "]";
500     }
501     $h{seq} = $h{hyphseq} = seq($op);
502     $h{seq} = "" if $h{seq} eq "-";
503     $h{seqnum} = $op->seq;
504     $h{next} = $op->next;
505     $h{next} = (class($h{next}) eq "NULL") ? "(end)" : seq($h{next});
506     $h{nextaddr} = sprintf("%#x", $ {$op->next});
507     $h{sibaddr} = sprintf("%#x", $ {$op->sibling});
508     $h{firstaddr} = sprintf("%#x", $ {$op->first}) if $op->can("first");
509     $h{lastaddr} = sprintf("%#x", $ {$op->last}) if $op->can("last");
510
511     $h{classsym} = $opclass{$h{class}};
512     $h{flagval} = $op->flags;
513     $h{flags} = op_flags($op->flags);
514     $h{privval} = $op->private;
515     $h{private} = private_flags($h{name}, $op->private);
516     $h{addr} = sprintf("%#x", $$op);
517     $h{label} = $labels{$op->seq};
518     $h{typenum} = $op->type;
519     $h{noise} = $linenoise[$op->type];
520     $_->(\%h, $op, \$format, \$level) for @callbacks;
521     return fmt_line(\%h, $format, $level);
522 }
523
524 sub B::OP::concise {
525     my($op, $level) = @_;
526     if ($order eq "exec" and $lastnext and $$lastnext != $$op) {
527         my $h = {"seq" => seq($lastnext), "class" => class($lastnext),
528                  "addr" => sprintf("%#x", $$lastnext)};
529         print fmt_line($h, $gotofmt, $level+1);
530     }
531     $lastnext = $op->next;
532     print concise_op($op, $level, $format);
533 }
534
535 # B::OP::terse (see Terse.pm) now just calls this
536 sub b_terse {
537     my($op, $level) = @_;
538
539     # This isn't necessarily right, but there's no easy way to get
540     # from an OP to the right CV. This is a limitation of the
541     # ->terse() interface style, and there isn't much to do about
542     # it. In particular, we can die in concise_op if the main pad
543     # isn't long enough, or has the wrong kind of entries, compared to
544     # the pad a sub was compiled with. The fix for that would be to
545     # make a backwards compatible "terse" format that never even
546     # looked at the pad, just like the old B::Terse. I don't think
547     # that's worth the effort, though.
548     $curcv = main_cv unless $curcv;
549
550     if ($order eq "exec" and $lastnext and $$lastnext != $$op) {
551         my $h = {"seq" => seq($lastnext), "class" => class($lastnext),
552                  "addr" => sprintf("%#x", $$lastnext)};
553         print fmt_line($h, $style{"terse"}[1], $level+1);
554     }
555     $lastnext = $op->next;
556     print concise_op($op, $level, $style{"terse"}[0]);
557 }
558
559 sub tree {
560     my $op = shift;
561     my $level = shift;
562     my $style = $tree_decorations[$tree_style];
563     my($space, $single, $kids, $kid, $nokid, $last, $lead, $size) = @$style;
564     my $name = concise_op($op, $level, $treefmt);
565     if (not $op->flags & OPf_KIDS) {
566         return $name . "\n";
567     }
568     my @lines;
569     for (my $kid = $op->first; $$kid; $kid = $kid->sibling) {
570         push @lines, tree($kid, $level+1);
571     }
572     my $i;
573     for ($i = $#lines; substr($lines[$i], 0, 1) eq " "; $i--) {
574         $lines[$i] = $space . $lines[$i];
575     }
576     if ($i > 0) {
577         $lines[$i] = $last . $lines[$i];
578         while ($i-- > 1) {
579             if (substr($lines[$i], 0, 1) eq " ") {
580                 $lines[$i] = $nokid . $lines[$i];
581             } else {
582                 $lines[$i] = $kid . $lines[$i];         
583             }
584         }
585         $lines[$i] = $kids . $lines[$i];
586     } else {
587         $lines[0] = $single . $lines[0];
588     }
589     return("$name$lead" . shift @lines,
590            map(" " x (length($name)+$size) . $_, @lines));
591 }
592
593 # *** Warning: fragile kludge ahead ***
594 # Because the B::* modules run in the same interpreter as the code
595 # they're compiling, their presence tends to distort the view we have
596 # of the code we're looking at. In particular, perl gives sequence
597 # numbers to both OPs in general and COPs in particular. If the
598 # program we're looking at were run on its own, these numbers would
599 # start at 1. Because all of B::Concise and all the modules it uses
600 # are compiled first, though, by the time we get to the user's program
601 # the sequence numbers are alreay at pretty high numbers, which would
602 # be distracting if you're trying to tell OPs apart. Therefore we'd
603 # like to subtract an offset from all the sequence numbers we display,
604 # to restore the simpler view of the world. The trick is to know what
605 # that offset will be, when we're still compiling B::Concise!  If we
606 # hardcoded a value, it would have to change every time B::Concise or
607 # other modules we use do. To help a little, what we do here is
608 # compile a little code at the end of the module, and compute the base
609 # sequence number for the user's program as being a small offset
610 # later, so all we have to worry about are changes in the offset.
611 # (Note that we now only play this game with COP sequence numbers. OP
612 # sequence numbers aren't used to refer to OPs from a distance, and
613 # they don't have much significance, so we just generate our own
614 # sequence numbers which are easier to control. This way we also don't
615 # stand in the way of a possible future removal of OP sequence
616 # numbers).
617
618 # When you say "perl -MO=Concise -e '$a'", the output should look like:
619
620 # 4  <@> leave[t1] vKP/REFC ->(end)
621 # 1     <0> enter ->2
622  #^ smallest OP sequence number should be 1
623 # 2     <;> nextstate(main 1 -e:1) v ->3
624  #                         ^ smallest COP sequence number should be 1
625 # -     <1> ex-rv2sv vK/1 ->4
626 # 3        <$> gvsv(*a) s ->4
627
628 # If the second of the marked numbers there isn't 1, it means you need
629 # to update the corresponding magic number in the next line.
630 # Remember, this needs to stay the last things in the module.
631
632 # Why is this different for MacOS?  Does it matter?
633 my $cop_seq_mnum = $^O eq 'MacOS' ? 10 : 9;
634 $cop_seq_base = svref_2object(eval 'sub{0;}')->START->cop_seq + $cop_seq_mnum;
635
636 1;
637
638 __END__
639
640 =head1 NAME
641
642 B::Concise - Walk Perl syntax tree, printing concise info about ops
643
644 =head1 SYNOPSIS
645
646     perl -MO=Concise[,OPTIONS] foo.pl
647
648     use B::Concise qw(set_style add_callback);
649
650 =head1 DESCRIPTION
651
652 This compiler backend prints the internal OPs of a Perl program's syntax
653 tree in one of several space-efficient text formats suitable for debugging
654 the inner workings of perl or other compiler backends. It can print OPs in
655 the order they appear in the OP tree, in the order they will execute, or
656 in a text approximation to their tree structure, and the format of the
657 information displyed is customizable. Its function is similar to that of
658 perl's B<-Dx> debugging flag or the B<B::Terse> module, but it is more
659 sophisticated and flexible.
660
661 =head1 EXAMPLE
662
663 Here's is a short example of output, using the default formatting
664 conventions :
665
666     % perl -MO=Concise -e '$a = $b + 42'
667     8  <@> leave[t1] vKP/REFC ->(end)
668     1     <0> enter ->2
669     2     <;> nextstate(main 1 -e:1) v ->3
670     7     <2> sassign vKS/2 ->8
671     5        <2> add[t1] sK/2 ->6
672     -           <1> ex-rv2sv sK/1 ->4
673     3              <$> gvsv(*b) s ->4
674     4           <$> const(IV 42) s ->5
675     -        <1> ex-rv2sv sKRM*/1 ->7
676     6           <$> gvsv(*a) s ->7
677
678 Each line corresponds to an operator. Null ops appear as C<ex-opname>,
679 where I<opname> is the op that has been optimized away by perl.
680
681 The number on the first row indicates the op's sequence number. It's
682 given in base 36 by default.
683
684 The symbol between angle brackets indicates the op's type : for example,
685 <2> is a BINOP, <@> a LISTOP, etc. (see L</"OP class abbreviations">).
686
687 The opname may be followed by op-specific information in parentheses
688 (e.g. C<gvsv(*b)>), and by targ information in brackets (e.g.
689 C<leave[t1]>).
690
691 Next come the op flags. The common flags are listed below
692 (L</"OP flags abbreviations">). The private flags follow, separated
693 by a slash. For example, C<vKP/REFC> means that the leave op has
694 public flags OPf_WANT_VOID, OPf_KIDS, and OPf_PARENS, and the private
695 flag OPpREFCOUNTED.
696
697 Finally an arrow points to the sequence number of the next op.
698
699 =head1 OPTIONS
700
701 Arguments that don't start with a hyphen are taken to be the names of
702 subroutines to print the OPs of; if no such functions are specified, the
703 main body of the program (outside any subroutines, and not including use'd
704 or require'd files) is printed.
705
706 =over 4
707
708 =item B<-basic>
709
710 Print OPs in the order they appear in the OP tree (a preorder
711 traversal, starting at the root). The indentation of each OP shows its
712 level in the tree.  This mode is the default, so the flag is included
713 simply for completeness.
714
715 =item B<-exec>
716
717 Print OPs in the order they would normally execute (for the majority
718 of constructs this is a postorder traversal of the tree, ending at the
719 root). In most cases the OP that usually follows a given OP will
720 appear directly below it; alternate paths are shown by indentation. In
721 cases like loops when control jumps out of a linear path, a 'goto'
722 line is generated.
723
724 =item B<-tree>
725
726 Print OPs in a text approximation of a tree, with the root of the tree
727 at the left and 'left-to-right' order of children transformed into
728 'top-to-bottom'. Because this mode grows both to the right and down,
729 it isn't suitable for large programs (unless you have a very wide
730 terminal).
731
732 =item B<-compact>
733
734 Use a tree format in which the minimum amount of space is used for the
735 lines connecting nodes (one character in most cases). This squeezes out
736 a few precious columns of screen real estate.
737
738 =item B<-loose>
739
740 Use a tree format that uses longer edges to separate OP nodes. This format
741 tends to look better than the compact one, especially in ASCII, and is
742 the default.
743
744 =item B<-vt>
745
746 Use tree connecting characters drawn from the VT100 line-drawing set.
747 This looks better if your terminal supports it.
748
749 =item B<-ascii>
750
751 Draw the tree with standard ASCII characters like C<+> and C<|>. These don't
752 look as clean as the VT100 characters, but they'll work with almost any
753 terminal (or the horizontal scrolling mode of less(1)) and are suitable
754 for text documentation or email. This is the default.
755
756 =item B<-main>
757
758 Include the main program in the output, even if subroutines were also
759 specified.
760
761 =item B<-base>I<n>
762
763 Print OP sequence numbers in base I<n>. If I<n> is greater than 10, the
764 digit for 11 will be 'a', and so on. If I<n> is greater than 36, the digit
765 for 37 will be 'A', and so on until 62. Values greater than 62 are not
766 currently supported. The default is 36.
767
768 =item B<-bigendian>
769
770 Print sequence numbers with the most significant digit first. This is the
771 usual convention for Arabic numerals, and the default.
772
773 =item B<-littleendian>
774
775 Print seqence numbers with the least significant digit first.
776
777 =item B<-concise>
778
779 Use the author's favorite set of formatting conventions. This is the
780 default, of course.
781
782 =item B<-terse>
783
784 Use formatting conventions that emulate the output of B<B::Terse>. The
785 basic mode is almost indistinguishable from the real B<B::Terse>, and the
786 exec mode looks very similar, but is in a more logical order and lacks
787 curly brackets. B<B::Terse> doesn't have a tree mode, so the tree mode
788 is only vaguely reminiscient of B<B::Terse>.
789
790 =item B<-linenoise>
791
792 Use formatting conventions in which the name of each OP, rather than being
793 written out in full, is represented by a one- or two-character abbreviation.
794 This is mainly a joke.
795
796 =item B<-debug>
797
798 Use formatting conventions reminiscient of B<B::Debug>; these aren't
799 very concise at all.
800
801 =item B<-env>
802
803 Use formatting conventions read from the environment variables
804 C<B_CONCISE_FORMAT>, C<B_CONCISE_GOTO_FORMAT>, and C<B_CONCISE_TREE_FORMAT>.
805
806 =back
807
808 =head1 FORMATTING SPECIFICATIONS
809
810 For each general style ('concise', 'terse', 'linenoise', etc.) there are
811 three specifications: one of how OPs should appear in the basic or exec
812 modes, one of how 'goto' lines should appear (these occur in the exec
813 mode only), and one of how nodes should appear in tree mode. Each has the
814 same format, described below. Any text that doesn't match a special
815 pattern is copied verbatim.
816
817 =over 4
818
819 =item B<(x(>I<exec_text>B<;>I<basic_text>B<)x)>
820
821 Generates I<exec_text> in exec mode, or I<basic_text> in basic mode.
822
823 =item B<(*(>I<text>B<)*)>
824
825 Generates one copy of I<text> for each indentation level.
826
827 =item B<(*(>I<text1>B<;>I<text2>B<)*)>
828
829 Generates one fewer copies of I<text1> than the indentation level, followed
830 by one copy of I<text2> if the indentation level is more than 0.
831
832 =item B<(?(>I<text1>B<#>I<var>I<Text2>B<)?)>
833
834 If the value of I<var> is true (not empty or zero), generates the
835 value of I<var> surrounded by I<text1> and I<Text2>, otherwise
836 nothing.
837
838 =item B<#>I<var>
839
840 Generates the value of the variable I<var>.
841
842 =item B<#>I<var>I<N>
843
844 Generates the value of I<var>, left jutified to fill I<N> spaces.
845
846 =item B<~>
847
848 Any number of tildes and surrounding whitespace will be collapsed to
849 a single space.
850
851 =back
852
853 The following variables are recognized:
854
855 =over 4
856
857 =item B<#addr>
858
859 The address of the OP, in hexidecimal.
860
861 =item B<#arg>
862
863 The OP-specific information of the OP (such as the SV for an SVOP, the
864 non-local exit pointers for a LOOP, etc.) enclosed in paretheses.
865
866 =item B<#class>
867
868 The B-determined class of the OP, in all caps.
869
870 =item B<#classsym>
871
872 A single symbol abbreviating the class of the OP.
873
874 =item B<#coplabel>
875
876 The label of the statement or block the OP is the start of, if any.
877
878 =item B<#exname>
879
880 The name of the OP, or 'ex-foo' if the OP is a null that used to be a foo.
881
882 =item B<#extarg>
883
884 The target of the OP, or nothing for a nulled OP.
885
886 =item B<#firstaddr>
887
888 The address of the OP's first child, in hexidecimal.
889
890 =item B<#flags>
891
892 The OP's flags, abbreviated as a series of symbols.
893
894 =item B<#flagval>
895
896 The numeric value of the OP's flags.
897
898 =item B<#hyphseq>
899
900 The sequence number of the OP, or a hyphen if it doesn't have one.
901
902 =item B<#label>
903
904 'NEXT', 'LAST', or 'REDO' if the OP is a target of one of those in exec
905 mode, or empty otherwise.
906
907 =item B<#lastaddr>
908
909 The address of the OP's last child, in hexidecimal.
910
911 =item B<#name>
912
913 The OP's name.
914
915 =item B<#NAME>
916
917 The OP's name, in all caps.
918
919 =item B<#next>
920
921 The sequence number of the OP's next OP.
922
923 =item B<#nextaddr>
924
925 The address of the OP's next OP, in hexidecimal.
926
927 =item B<#noise>
928
929 A one- or two-character abbreviation for the OP's name.
930
931 =item B<#private>
932
933 The OP's private flags, rendered with abbreviated names if possible.
934
935 =item B<#privval>
936
937 The numeric value of the OP's private flags.
938
939 =item B<#seq>
940
941 The sequence number of the OP. Note that this is now a sequence number
942 generated by B::Concise, rather than the real op_seq value (for which
943 see B<#seqnum>).
944
945 =item B<#seqnum>
946
947 The real sequence number of the OP, as a regular number and not adjusted
948 to be relative to the start of the real program. (This will generally be
949 a fairly large number because all of B<B::Concise> is compiled before
950 your program is).
951
952 =item B<#sibaddr>
953
954 The address of the OP's next youngest sibling, in hexidecimal.
955
956 =item B<#svaddr>
957
958 The address of the OP's SV, if it has an SV, in hexidecimal.
959
960 =item B<#svclass>
961
962 The class of the OP's SV, if it has one, in all caps (e.g., 'IV').
963
964 =item B<#svval>
965
966 The value of the OP's SV, if it has one, in a short human-readable format.
967
968 =item B<#targ>
969
970 The numeric value of the OP's targ.
971
972 =item B<#targarg>
973
974 The name of the variable the OP's targ refers to, if any, otherwise the
975 letter t followed by the OP's targ in decimal.
976
977 =item B<#targarglife>
978
979 Same as B<#targarg>, but followed by the COP sequence numbers that delimit
980 the variable's lifetime (or 'end' for a variable in an open scope) for a
981 variable.
982
983 =item B<#typenum>
984
985 The numeric value of the OP's type, in decimal.
986
987 =back
988
989 =head1 ABBREVIATIONS
990
991 =head2 OP flags abbreviations
992
993     v      OPf_WANT_VOID    Want nothing (void context)
994     s      OPf_WANT_SCALAR  Want single value (scalar context)
995     l      OPf_WANT_LIST    Want list of any length (list context)
996     K      OPf_KIDS         There is a firstborn child.
997     P      OPf_PARENS       This operator was parenthesized.
998                              (Or block needs explicit scope entry.)
999     R      OPf_REF          Certified reference.
1000                              (Return container, not containee).
1001     M      OPf_MOD          Will modify (lvalue).
1002     S      OPf_STACKED      Some arg is arriving on the stack.
1003     *      OPf_SPECIAL      Do something weird for this op (see op.h)
1004
1005 =head2 OP class abbreviations
1006
1007     0      OP (aka BASEOP)  An OP with no children
1008     1      UNOP             An OP with one child
1009     2      BINOP            An OP with two children
1010     |      LOGOP            A control branch OP
1011     @      LISTOP           An OP that could have lots of children
1012     /      PMOP             An OP with a regular expression
1013     $      SVOP             An OP with an SV
1014     "      PVOP             An OP with a string
1015     {      LOOP             An OP that holds pointers for a loop
1016     ;      COP              An OP that marks the start of a statement
1017     #      PADOP            An OP with a GV on the pad
1018
1019 =head1 Using B::Concise outside of the O framework
1020
1021 It is possible to extend B<B::Concise> by using it outside of the B<O>
1022 framework and providing new styles and new variables.
1023
1024     use B::Concise qw(set_style add_callback);
1025     set_style($format, $gotofmt, $treefmt);
1026     add_callback
1027     (
1028         sub
1029         {
1030             my ($h, $op, $level, $format) = @_;
1031             $h->{variable} = some_func($op);
1032         }
1033     );
1034     B::Concise::compile(@options)->();
1035
1036 You can specify a style by calling the B<set_style> subroutine.  If you
1037 have a new variable in your style, or you want to change the value of an
1038 existing variable, you will need to add a callback to specify the value
1039 for that variable.
1040
1041 This is done by calling B<add_callback> passing references to any
1042 callback subroutines.  The subroutines are called in the same order as
1043 they are added.  Each subroutine is passed four parameters.  These are a
1044 reference to a hash, the keys of which are the names of the variables
1045 and the values of which are their values, the op, the level and the
1046 format.
1047
1048 To define your own variables, simply add them to the hash, or change
1049 existing values if you need to.  The level and format are passed in as
1050 references to scalars, but it is unlikely that they will need to be
1051 changed or even used.
1052
1053 To switch back to one of the standard styles like C<concise> or
1054 C<terse>, use C<set_style_standard>.
1055
1056 To see the output, call the subroutine returned by B<compile> in the
1057 same way that B<O> does.
1058
1059 =head1 AUTHOR
1060
1061 Stephen McCamant, E<lt>smcc@CSUA.Berkeley.EDUE<gt>.
1062
1063 =cut