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.
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);
16 use B qw(class ppname main_start main_root main_cv cstring svref_2object
17 SVf_IOK SVf_NOK SVf_POK SVf_IVisUV SVf_FAKE OPf_KIDS CVf_ANON);
21 ["(?(#label =>\n)?)(*( )*)#class (#addr) #name (?([#targ])?) "
22 . "#svclass~(?((#svaddr))?)~#svval~(?(label \"#coplabel\")?)\n",
23 "(*( )*)goto #class (#addr)\n",
26 ["#hyphseq2 (*( (x( ;)x))*)<#classsym> "
27 . "#exname#arg(?([#targarglife])?)~#flags(?(/#private)?)(x(;~->#next)x)\n",
28 " (*( )*) goto #seq\n",
29 "(?(<#seq>)?)#exname#arg(?([#targarglife])?)"],
31 ["(x(;(*( )*))x)#noise#arg(?([#targarg])?)(x( ;\n)x)",
33 "(?(#seq)?)#noise#arg(?([#targarg])?)"],
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)?)",
42 "env" => [$ENV{B_CONCISE_FORMAT}, $ENV{B_CONCISE_GOTO_FORMAT},
43 $ENV{B_CONCISE_TREE_FORMAT}],
46 my($format, $gotofmt, $treefmt);
52 ($format, $gotofmt, $treefmt) = @_;
55 sub set_style_standard {
57 set_style(@{$style{$name}});
65 my ($order, $cvref) = @_;
66 my $cv = svref_2object($cvref);
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);
74 print tree($cv->ROOT, 0)
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);
95 my $start_sym = "\e(0"; # "\cN" sometimes also works
96 my $end_sym = "\e(B"; # "\cO" respectively
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],
111 set_style_standard("concise");
114 my @options = grep(/^-/, @_);
115 my @args = grep(!/^-/, @_);
117 for my $o (@options) {
118 if ($o eq "-basic") {
120 } elsif ($o eq "-exec") {
122 } elsif ($o eq "-tree") {
124 } elsif ($o eq "-compact") {
126 } elsif ($o eq "-loose") {
128 } elsif ($o eq "-vt") {
130 } elsif ($o eq "-ascii") {
132 } elsif ($o eq "-main") {
134 } elsif ($o =~ /^-base(\d+)$/) {
136 } elsif ($o eq "-bigendian") {
138 } elsif ($o eq "-littleendian") {
140 } elsif (exists $style{substr($o, 1)}) {
141 set_style(@{$style{substr($o, 1)}});
143 warn "Option $o unrecognized";
148 for my $objname (@args) {
149 $objname = "main::" . $objname unless $objname =~ /::/;
151 eval "concise_cv(\$order, \\&$objname)";
152 die "concise_cv($order, \\&$objname) failed: $@" if $@;
155 if (!@args or $do_main) {
156 print "main program:\n" if $do_main;
157 concise_main($order);
165 my %opclass = ('OP' => "0", 'UNOP' => "1", 'BINOP' => "2", 'LOGOP' => "|",
166 'LISTOP' => "@", 'PMOP' => "/", 'SVOP' => "\$", 'GVOP' => "*",
167 'PVOP' => '"', 'LOOP' => "{", 'COP' => ";", 'PADOP' => "#");
169 no warnings 'qw'; # "Possible attempt to put comments..."
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';
187 my $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
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;
206 return "-" . base_n(-$x) if $x < 0;
208 do { $str .= substr($chars, $x % $base, 1) } while $x = int($x / $base);
209 $str = reverse $str if $big_endian;
218 return "-" if not exists $sequence_num{$$op};
219 return base_n($sequence_num{$$op});
223 my($op, $sub, $level) = @_;
225 if ($op->flags & OPf_KIDS) {
226 for (my $kid = $op->first; $$kid; $kid = $kid->sibling) {
227 walk_topdown($kid, $sub, $level + 1);
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);
237 my($ar, $level) = @_;
239 if (ref($l) eq "ARRAY") {
240 walklines($l, $level + 1);
248 my($top, $level) = @_;
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}++;
256 my $name = $op->name;
257 if (class($op) eq "LOGOP") {
260 push @todo, [$op->other, $ar];
261 } elsif ($name eq "subst" and $ {$op->pmreplstart}) {
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";
272 walklines(\@lines, 0);
275 # The structure of this routine is purposely modeled after op.c's peep()
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};
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";
291 } elsif (class($op) eq "LOOP") {
292 my $redoop = $op->redoop;
293 $redoop = $redoop->next while $redoop->name eq "null";
295 my $nextop = $op->nextop;
296 $nextop = $nextop->next while $nextop->name eq "null";
298 my $lastop = $op->lastop;
299 $lastop = $lastop->next while $lastop->name eq "null";
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);
312 my($hr, $fmt, $level) = @_;
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;
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", "enteriter");
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",
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",
347 $priv{$_}{16} = "TARGMY"
348 for (map(($_,"s$_"),"chop", "chomp"),
349 map(($_,"i_$_"), "postinc", "postdec", "multiply", "divide", "modulo",
350 "add", "subtract", "negate"), "pow", "concat", "stringify",
351 "left_shift", "right_shift", "bit_and", "bit_xor", "bit_or",
352 "complement", "atan2", "sin", "cos", "rand", "exp", "log", "sqrt",
353 "int", "hex", "oct", "abs", "length", "index", "rindex", "sprintf",
354 "ord", "chr", "crypt", "quotemeta", "join", "push", "unshift", "flock",
355 "chdir", "chown", "chroot", "unlink", "chmod", "utime", "rename",
356 "link", "symlink", "mkdir", "rmdir", "wait", "waitpid", "system",
357 "exec", "kill", "getppid", "getpgrp", "setpgrp", "getpriority",
358 "setpriority", "time", "sleep");
359 @{$priv{"const"}}{8,16,32,64,128} = ("STRICT","ENTERED", '$[', "BARE", "WARN");
360 $priv{"flip"}{64} = $priv{"flop"}{64} = "LINENUM";
361 $priv{"list"}{64} = "GUESSED";
362 $priv{"delete"}{64} = "SLICE";
363 $priv{"exists"}{64} = "SUB";
364 $priv{$_}{64} = "LOCALE"
365 for ("sort", "prtf", "sprintf", "slt", "sle", "seq", "sne", "sgt", "sge",
366 "scmp", "lc", "uc", "lcfirst", "ucfirst");
367 @{$priv{"sort"}}{1,2,4} = ("NUM", "INT", "REV");
368 $priv{"threadsv"}{64} = "SVREFd";
369 @{$priv{$_}}{16,32,64,128} = ("INBIN","INCR","OUTBIN","OUTCR")
370 for ("open", "backtick");
371 $priv{"exit"}{128} = "VMS";
372 $priv{$_}{2} = "FTACCESS"
373 for ("ftrread", "ftrwrite", "ftrexec", "fteread", "ftewrite", "fteexec");
378 for my $flag (128, 96, 64, 32, 16, 8, 4, 2, 1) {
379 if ($priv{$name}{$flag} and $x & $flag and $x >= $flag) {
381 push @s, $priv{$name}{$flag};
385 return join(",", @s);
390 $hr->{svclass} = class($sv);
391 $hr->{svclass} = "UV"
392 if $hr->{svclass} eq "IV" and $sv->FLAGS & SVf_IVisUV;
393 $hr->{svaddr} = sprintf("%#x", $$sv);
394 if ($hr->{svclass} eq "GV") {
396 my $stash = $gv->STASH->NAME;
397 if ($stash eq "main") {
400 $stash = $stash . "::";
402 $hr->{svval} = "*$stash" . $gv->SAFENAME;
403 return "*$stash" . $gv->SAFENAME;
405 while (class($sv) eq "RV") {
406 $hr->{svval} .= "\\";
409 if (class($sv) eq "SPECIAL") {
410 $hr->{svval} .= ["Null", "sv_undef", "sv_yes", "sv_no"]->[$$sv];
411 } elsif ($sv->FLAGS & SVf_NOK) {
412 $hr->{svval} .= $sv->NV;
413 } elsif ($sv->FLAGS & SVf_IOK) {
414 $hr->{svval} .= $sv->int_value;
415 } elsif ($sv->FLAGS & SVf_POK) {
416 $hr->{svval} .= cstring($sv->PV);
417 } elsif (class($sv) eq "HV") {
418 $hr->{svval} .= 'HASH';
420 return $hr->{svclass} . " " . $hr->{svval};
425 my ($op, $level, $format) = @_;
427 $h{exname} = $h{name} = $op->name;
428 $h{NAME} = uc $h{name};
429 $h{class} = class($op);
430 $h{extarg} = $h{targ} = $op->targ;
431 $h{extarg} = "" unless $h{extarg};
432 if ($h{name} eq "null" and $h{targ}) {
433 $h{exname} = "ex-" . substr(ppname($h{targ}), 3);
436 my $padname = (($curcv->PADLIST->ARRAY)[0]->ARRAY)[$h{targ}];
437 if (defined $padname and class($padname) ne "SPECIAL") {
438 $h{targarg} = $padname->PVX;
439 if ($padname->FLAGS & SVf_FAKE) {
441 $fake .= 'a' if $padname->IVX & 1; # PAD_FAKELEX_ANON
442 $fake .= 'm' if $padname->IVX & 2; # PAD_FAKELEX_MULTI
443 $fake .= ':' . $padname->NVX if $curcv->CvFLAGS & CVf_ANON;
444 $h{targarglife} = "$h{targarg}:FAKE:$fake";
447 my $intro = $padname->NVX - $cop_seq_base;
448 my $finish = int($padname->IVX) - $cop_seq_base;
449 $finish = "end" if $finish == 999999999 - $cop_seq_base;
450 $h{targarglife} = "$h{targarg}:$intro,$finish";
453 $h{targarglife} = $h{targarg} = "t" . $h{targ};
457 $h{svclass} = $h{svaddr} = $h{svval} = "";
458 if ($h{class} eq "PMOP") {
459 my $precomp = $op->precomp;
460 if (defined $precomp) {
461 $precomp = cstring($precomp); # Escape literal control sequences
462 $precomp = "/$precomp/";
466 my $pmreplroot = $op->pmreplroot;
468 if ($pmreplroot && $$pmreplroot && $pmreplroot->isa("B::GV")) {
469 # with C<@stash_array = split(/pat/, str);>,
470 # *stash_array is stored in pmreplroot.
471 $h{arg} = "($precomp => \@" . $pmreplroot->NAME . ")";
472 } elsif ($ {$op->pmreplstart}) {
474 $pmreplstart = "replstart->" . seq($op->pmreplstart);
475 $h{arg} = "(" . join(" ", $precomp, $pmreplstart) . ")";
477 $h{arg} = "($precomp)";
479 } elsif ($h{class} eq "PVOP" and $h{name} ne "trans") {
480 $h{arg} = '("' . $op->pv . '")';
481 $h{svval} = '"' . $op->pv . '"';
482 } elsif ($h{class} eq "COP") {
483 my $label = $op->label;
484 $h{coplabel} = $label;
485 $label = $label ? "$label: " : "";
488 $loc .= ":" . $op->line;
489 my($stash, $cseq) = ($op->stash->NAME, $op->cop_seq - $cop_seq_base);
490 my $arybase = $op->arybase;
491 $arybase = $arybase ? ' $[=' . $arybase : "";
492 $h{arg} = "($label$stash $cseq $loc$arybase)";
493 } elsif ($h{class} eq "LOOP") {
494 $h{arg} = "(next->" . seq($op->nextop) . " last->" . seq($op->lastop)
495 . " redo->" . seq($op->redoop) . ")";
496 } elsif ($h{class} eq "LOGOP") {
498 $h{arg} = "(other->" . seq($op->other) . ")";
499 } elsif ($h{class} eq "SVOP") {
501 my $sv = (($curcv->PADLIST->ARRAY)[1]->ARRAY)[$op->targ];
502 $h{arg} = "[" . concise_sv($sv, \%h) . "]";
503 $h{targarglife} = $h{targarg} = "";
505 $h{arg} = "(" . concise_sv($op->sv, \%h) . ")";
507 } elsif ($h{class} eq "PADOP") {
508 my $sv = (($curcv->PADLIST->ARRAY)[1]->ARRAY)[$op->padix];
509 $h{arg} = "[" . concise_sv($sv, \%h) . "]";
511 $h{seq} = $h{hyphseq} = seq($op);
512 $h{seq} = "" if $h{seq} eq "-";
513 $h{seqnum} = $op->seq;
514 $h{next} = $op->next;
515 $h{next} = (class($h{next}) eq "NULL") ? "(end)" : seq($h{next});
516 $h{nextaddr} = sprintf("%#x", $ {$op->next});
517 $h{sibaddr} = sprintf("%#x", $ {$op->sibling});
518 $h{firstaddr} = sprintf("%#x", $ {$op->first}) if $op->can("first");
519 $h{lastaddr} = sprintf("%#x", $ {$op->last}) if $op->can("last");
521 $h{classsym} = $opclass{$h{class}};
522 $h{flagval} = $op->flags;
523 $h{flags} = op_flags($op->flags);
524 $h{privval} = $op->private;
525 $h{private} = private_flags($h{name}, $op->private);
526 $h{addr} = sprintf("%#x", $$op);
527 $h{label} = $labels{$op->seq};
528 $h{typenum} = $op->type;
529 $h{noise} = $linenoise[$op->type];
530 $_->(\%h, $op, \$format, \$level) for @callbacks;
531 return fmt_line(\%h, $format, $level);
535 my($op, $level) = @_;
536 if ($order eq "exec" and $lastnext and $$lastnext != $$op) {
537 my $h = {"seq" => seq($lastnext), "class" => class($lastnext),
538 "addr" => sprintf("%#x", $$lastnext)};
539 print fmt_line($h, $gotofmt, $level+1);
541 $lastnext = $op->next;
542 print concise_op($op, $level, $format);
545 # B::OP::terse (see Terse.pm) now just calls this
547 my($op, $level) = @_;
549 # This isn't necessarily right, but there's no easy way to get
550 # from an OP to the right CV. This is a limitation of the
551 # ->terse() interface style, and there isn't much to do about
552 # it. In particular, we can die in concise_op if the main pad
553 # isn't long enough, or has the wrong kind of entries, compared to
554 # the pad a sub was compiled with. The fix for that would be to
555 # make a backwards compatible "terse" format that never even
556 # looked at the pad, just like the old B::Terse. I don't think
557 # that's worth the effort, though.
558 $curcv = main_cv unless $curcv;
560 if ($order eq "exec" and $lastnext and $$lastnext != $$op) {
561 my $h = {"seq" => seq($lastnext), "class" => class($lastnext),
562 "addr" => sprintf("%#x", $$lastnext)};
563 print fmt_line($h, $style{"terse"}[1], $level+1);
565 $lastnext = $op->next;
566 print concise_op($op, $level, $style{"terse"}[0]);
572 my $style = $tree_decorations[$tree_style];
573 my($space, $single, $kids, $kid, $nokid, $last, $lead, $size) = @$style;
574 my $name = concise_op($op, $level, $treefmt);
575 if (not $op->flags & OPf_KIDS) {
579 for (my $kid = $op->first; $$kid; $kid = $kid->sibling) {
580 push @lines, tree($kid, $level+1);
583 for ($i = $#lines; substr($lines[$i], 0, 1) eq " "; $i--) {
584 $lines[$i] = $space . $lines[$i];
587 $lines[$i] = $last . $lines[$i];
589 if (substr($lines[$i], 0, 1) eq " ") {
590 $lines[$i] = $nokid . $lines[$i];
592 $lines[$i] = $kid . $lines[$i];
595 $lines[$i] = $kids . $lines[$i];
597 $lines[0] = $single . $lines[0];
599 return("$name$lead" . shift @lines,
600 map(" " x (length($name)+$size) . $_, @lines));
603 # *** Warning: fragile kludge ahead ***
604 # Because the B::* modules run in the same interpreter as the code
605 # they're compiling, their presence tends to distort the view we have
606 # of the code we're looking at. In particular, perl gives sequence
607 # numbers to both OPs in general and COPs in particular. If the
608 # program we're looking at were run on its own, these numbers would
609 # start at 1. Because all of B::Concise and all the modules it uses
610 # are compiled first, though, by the time we get to the user's program
611 # the sequence numbers are alreay at pretty high numbers, which would
612 # be distracting if you're trying to tell OPs apart. Therefore we'd
613 # like to subtract an offset from all the sequence numbers we display,
614 # to restore the simpler view of the world. The trick is to know what
615 # that offset will be, when we're still compiling B::Concise! If we
616 # hardcoded a value, it would have to change every time B::Concise or
617 # other modules we use do. To help a little, what we do here is
618 # compile a little code at the end of the module, and compute the base
619 # sequence number for the user's program as being a small offset
620 # later, so all we have to worry about are changes in the offset.
621 # (Note that we now only play this game with COP sequence numbers. OP
622 # sequence numbers aren't used to refer to OPs from a distance, and
623 # they don't have much significance, so we just generate our own
624 # sequence numbers which are easier to control. This way we also don't
625 # stand in the way of a possible future removal of OP sequence
628 # When you say "perl -MO=Concise -e '$a'", the output should look like:
630 # 4 <@> leave[t1] vKP/REFC ->(end)
632 #^ smallest OP sequence number should be 1
633 # 2 <;> nextstate(main 1 -e:1) v ->3
634 # ^ smallest COP sequence number should be 1
635 # - <1> ex-rv2sv vK/1 ->4
636 # 3 <$> gvsv(*a) s ->4
638 # If the second of the marked numbers there isn't 1, it means you need
639 # to update the corresponding magic number in the next line.
640 # Remember, this needs to stay the last things in the module.
642 # Why is this different for MacOS? Does it matter?
643 my $cop_seq_mnum = $^O eq 'MacOS' ? 10 : 9;
644 $cop_seq_base = svref_2object(eval 'sub{0;}')->START->cop_seq + $cop_seq_mnum;
652 B::Concise - Walk Perl syntax tree, printing concise info about ops
656 perl -MO=Concise[,OPTIONS] foo.pl
658 use B::Concise qw(set_style add_callback);
662 This compiler backend prints the internal OPs of a Perl program's syntax
663 tree in one of several space-efficient text formats suitable for debugging
664 the inner workings of perl or other compiler backends. It can print OPs in
665 the order they appear in the OP tree, in the order they will execute, or
666 in a text approximation to their tree structure, and the format of the
667 information displyed is customizable. Its function is similar to that of
668 perl's B<-Dx> debugging flag or the B<B::Terse> module, but it is more
669 sophisticated and flexible.
673 Here's is a short example of output, using the default formatting
676 % perl -MO=Concise -e '$a = $b + 42'
677 8 <@> leave[t1] vKP/REFC ->(end)
679 2 <;> nextstate(main 1 -e:1) v ->3
680 7 <2> sassign vKS/2 ->8
681 5 <2> add[t1] sK/2 ->6
682 - <1> ex-rv2sv sK/1 ->4
684 4 <$> const(IV 42) s ->5
685 - <1> ex-rv2sv sKRM*/1 ->7
688 Each line corresponds to an operator. Null ops appear as C<ex-opname>,
689 where I<opname> is the op that has been optimized away by perl.
691 The number on the first row indicates the op's sequence number. It's
692 given in base 36 by default.
694 The symbol between angle brackets indicates the op's type : for example,
695 <2> is a BINOP, <@> a LISTOP, etc. (see L</"OP class abbreviations">).
697 The opname may be followed by op-specific information in parentheses
698 (e.g. C<gvsv(*b)>), and by targ information in brackets (e.g.
701 Next come the op flags. The common flags are listed below
702 (L</"OP flags abbreviations">). The private flags follow, separated
703 by a slash. For example, C<vKP/REFC> means that the leave op has
704 public flags OPf_WANT_VOID, OPf_KIDS, and OPf_PARENS, and the private
707 Finally an arrow points to the sequence number of the next op.
711 Arguments that don't start with a hyphen are taken to be the names of
712 subroutines to print the OPs of; if no such functions are specified, the
713 main body of the program (outside any subroutines, and not including use'd
714 or require'd files) is printed.
720 Print OPs in the order they appear in the OP tree (a preorder
721 traversal, starting at the root). The indentation of each OP shows its
722 level in the tree. This mode is the default, so the flag is included
723 simply for completeness.
727 Print OPs in the order they would normally execute (for the majority
728 of constructs this is a postorder traversal of the tree, ending at the
729 root). In most cases the OP that usually follows a given OP will
730 appear directly below it; alternate paths are shown by indentation. In
731 cases like loops when control jumps out of a linear path, a 'goto'
736 Print OPs in a text approximation of a tree, with the root of the tree
737 at the left and 'left-to-right' order of children transformed into
738 'top-to-bottom'. Because this mode grows both to the right and down,
739 it isn't suitable for large programs (unless you have a very wide
744 Use a tree format in which the minimum amount of space is used for the
745 lines connecting nodes (one character in most cases). This squeezes out
746 a few precious columns of screen real estate.
750 Use a tree format that uses longer edges to separate OP nodes. This format
751 tends to look better than the compact one, especially in ASCII, and is
756 Use tree connecting characters drawn from the VT100 line-drawing set.
757 This looks better if your terminal supports it.
761 Draw the tree with standard ASCII characters like C<+> and C<|>. These don't
762 look as clean as the VT100 characters, but they'll work with almost any
763 terminal (or the horizontal scrolling mode of less(1)) and are suitable
764 for text documentation or email. This is the default.
768 Include the main program in the output, even if subroutines were also
773 Print OP sequence numbers in base I<n>. If I<n> is greater than 10, the
774 digit for 11 will be 'a', and so on. If I<n> is greater than 36, the digit
775 for 37 will be 'A', and so on until 62. Values greater than 62 are not
776 currently supported. The default is 36.
780 Print sequence numbers with the most significant digit first. This is the
781 usual convention for Arabic numerals, and the default.
783 =item B<-littleendian>
785 Print seqence numbers with the least significant digit first.
789 Use the author's favorite set of formatting conventions. This is the
794 Use formatting conventions that emulate the output of B<B::Terse>. The
795 basic mode is almost indistinguishable from the real B<B::Terse>, and the
796 exec mode looks very similar, but is in a more logical order and lacks
797 curly brackets. B<B::Terse> doesn't have a tree mode, so the tree mode
798 is only vaguely reminiscient of B<B::Terse>.
802 Use formatting conventions in which the name of each OP, rather than being
803 written out in full, is represented by a one- or two-character abbreviation.
804 This is mainly a joke.
808 Use formatting conventions reminiscient of B<B::Debug>; these aren't
813 Use formatting conventions read from the environment variables
814 C<B_CONCISE_FORMAT>, C<B_CONCISE_GOTO_FORMAT>, and C<B_CONCISE_TREE_FORMAT>.
818 =head1 FORMATTING SPECIFICATIONS
820 For each general style ('concise', 'terse', 'linenoise', etc.) there are
821 three specifications: one of how OPs should appear in the basic or exec
822 modes, one of how 'goto' lines should appear (these occur in the exec
823 mode only), and one of how nodes should appear in tree mode. Each has the
824 same format, described below. Any text that doesn't match a special
825 pattern is copied verbatim.
829 =item B<(x(>I<exec_text>B<;>I<basic_text>B<)x)>
831 Generates I<exec_text> in exec mode, or I<basic_text> in basic mode.
833 =item B<(*(>I<text>B<)*)>
835 Generates one copy of I<text> for each indentation level.
837 =item B<(*(>I<text1>B<;>I<text2>B<)*)>
839 Generates one fewer copies of I<text1> than the indentation level, followed
840 by one copy of I<text2> if the indentation level is more than 0.
842 =item B<(?(>I<text1>B<#>I<var>I<Text2>B<)?)>
844 If the value of I<var> is true (not empty or zero), generates the
845 value of I<var> surrounded by I<text1> and I<Text2>, otherwise
850 Generates the value of the variable I<var>.
854 Generates the value of I<var>, left jutified to fill I<N> spaces.
858 Any number of tildes and surrounding whitespace will be collapsed to
863 The following variables are recognized:
869 The address of the OP, in hexidecimal.
873 The OP-specific information of the OP (such as the SV for an SVOP, the
874 non-local exit pointers for a LOOP, etc.) enclosed in paretheses.
878 The B-determined class of the OP, in all caps.
882 A single symbol abbreviating the class of the OP.
886 The label of the statement or block the OP is the start of, if any.
890 The name of the OP, or 'ex-foo' if the OP is a null that used to be a foo.
894 The target of the OP, or nothing for a nulled OP.
898 The address of the OP's first child, in hexidecimal.
902 The OP's flags, abbreviated as a series of symbols.
906 The numeric value of the OP's flags.
910 The sequence number of the OP, or a hyphen if it doesn't have one.
914 'NEXT', 'LAST', or 'REDO' if the OP is a target of one of those in exec
915 mode, or empty otherwise.
919 The address of the OP's last child, in hexidecimal.
927 The OP's name, in all caps.
931 The sequence number of the OP's next OP.
935 The address of the OP's next OP, in hexidecimal.
939 A one- or two-character abbreviation for the OP's name.
943 The OP's private flags, rendered with abbreviated names if possible.
947 The numeric value of the OP's private flags.
951 The sequence number of the OP. Note that this is now a sequence number
952 generated by B::Concise, rather than the real op_seq value (for which
957 The real sequence number of the OP, as a regular number and not adjusted
958 to be relative to the start of the real program. (This will generally be
959 a fairly large number because all of B<B::Concise> is compiled before
964 The address of the OP's next youngest sibling, in hexidecimal.
968 The address of the OP's SV, if it has an SV, in hexidecimal.
972 The class of the OP's SV, if it has one, in all caps (e.g., 'IV').
976 The value of the OP's SV, if it has one, in a short human-readable format.
980 The numeric value of the OP's targ.
984 The name of the variable the OP's targ refers to, if any, otherwise the
985 letter t followed by the OP's targ in decimal.
987 =item B<#targarglife>
989 Same as B<#targarg>, but followed by the COP sequence numbers that delimit
990 the variable's lifetime (or 'end' for a variable in an open scope) for a
995 The numeric value of the OP's type, in decimal.
1001 =head2 OP flags abbreviations
1003 v OPf_WANT_VOID Want nothing (void context)
1004 s OPf_WANT_SCALAR Want single value (scalar context)
1005 l OPf_WANT_LIST Want list of any length (list context)
1006 K OPf_KIDS There is a firstborn child.
1007 P OPf_PARENS This operator was parenthesized.
1008 (Or block needs explicit scope entry.)
1009 R OPf_REF Certified reference.
1010 (Return container, not containee).
1011 M OPf_MOD Will modify (lvalue).
1012 S OPf_STACKED Some arg is arriving on the stack.
1013 * OPf_SPECIAL Do something weird for this op (see op.h)
1015 =head2 OP class abbreviations
1017 0 OP (aka BASEOP) An OP with no children
1018 1 UNOP An OP with one child
1019 2 BINOP An OP with two children
1020 | LOGOP A control branch OP
1021 @ LISTOP An OP that could have lots of children
1022 / PMOP An OP with a regular expression
1023 $ SVOP An OP with an SV
1024 " PVOP An OP with a string
1025 { LOOP An OP that holds pointers for a loop
1026 ; COP An OP that marks the start of a statement
1027 # PADOP An OP with a GV on the pad
1029 =head1 Using B::Concise outside of the O framework
1031 It is possible to extend B<B::Concise> by using it outside of the B<O>
1032 framework and providing new styles and new variables.
1034 use B::Concise qw(set_style add_callback);
1035 set_style($format, $gotofmt, $treefmt);
1040 my ($h, $op, $level, $format) = @_;
1041 $h->{variable} = some_func($op);
1044 B::Concise::compile(@options)->();
1046 You can specify a style by calling the B<set_style> subroutine. If you
1047 have a new variable in your style, or you want to change the value of an
1048 existing variable, you will need to add a callback to specify the value
1051 This is done by calling B<add_callback> passing references to any
1052 callback subroutines. The subroutines are called in the same order as
1053 they are added. Each subroutine is passed four parameters. These are a
1054 reference to a hash, the keys of which are the names of the variables
1055 and the values of which are their values, the op, the level and the
1058 To define your own variables, simply add them to the hash, or change
1059 existing values if you need to. The level and format are passed in as
1060 references to scalars, but it is unlikely that they will need to be
1061 changed or even used.
1063 To switch back to one of the standard styles like C<concise> or
1064 C<terse>, use C<set_style_standard>.
1066 To see the output, call the subroutine returned by B<compile> in the
1067 same way that B<O> does.
1071 Stephen McCamant, E<lt>smcc@CSUA.Berkeley.EDUE<gt>.