B::Concise - Walk Perl syntax tree, printing concise info about ops |
set_style()
add_style()
set_style_standard($name)
add_callback()
B::Concise - Walk Perl syntax tree, printing concise info about ops
perl -MO=Concise[,OPTIONS] foo.pl
use B::Concise qw(set_style add_callback);
This compiler backend prints the internal OPs of a Perl program's syntax tree in one of several space-efficient text formats suitable for debugging the inner workings of perl or other compiler backends. It can print OPs in the order they appear in the OP tree, in the order they will execute, or in a text approximation to their tree structure, and the format of the information displyed is customizable. Its function is similar to that of perl's -Dx debugging flag or the B::Terse module, but it is more sophisticated and flexible.
Here's is a short example of output, using the default formatting conventions :
% perl -MO=Concise -e '$a = $b + 42' 8 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(main 1 -e:1) v ->3 7 <2> sassign vKS/2 ->8 5 <2> add[t1] sK/2 ->6 - <1> ex-rv2sv sK/1 ->4 3 <$> gvsv(*b) s ->4 4 <$> const(IV 42) s ->5 - <1> ex-rv2sv sKRM*/1 ->7 6 <$> gvsv(*a) s ->7
Each line corresponds to an operator. Null ops appear as ex-opname
,
where opname is the op that has been optimized away by perl.
The number on the first row indicates the op's sequence number. It's given in base 36 by default.
The symbol between angle brackets indicates the op's type : for example, <2> is a BINOP, <@> a LISTOP, etc. (see OP class abbreviations).
The opname may be followed by op-specific information in parentheses
(e.g. gvsv(*b)
), and by targ information in brackets (e.g.
leave[t1]
).
Next come the op flags. The common flags are listed below
(OP flags abbreviations). The private flags follow, separated
by a slash. For example, vKP/REFC
means that the leave op has
public flags OPf_WANT_VOID, OPf_KIDS, and OPf_PARENS, and the private
flag OPpREFCOUNTED.
Finally an arrow points to the sequence number of the next op.
Arguments that don't start with a hyphen are taken to be the names of
subroutines to print the OPs of; if no such functions are specified,
the main body of the program (outside any subroutines, and not
including use'd or require'd files) is printed. Passing BEGIN
,
CHECK
, INIT
, or END
will cause all of the corresponding
special blocks to be printed.
+
and |
. These don't
look as clean as the VT100 characters, but they'll work with almost any
terminal (or the horizontal scrolling mode of less(1))
and are suitable
for text documentation or email. This is the default.
B_CONCISE_FORMAT
, B_CONCISE_GOTO_FORMAT
, and B_CONCISE_TREE_FORMAT
.
For each general style ('concise', 'terse', 'linenoise', etc.) there are three specifications: one of how OPs should appear in the basic or exec modes, one of how 'goto' lines should appear (these occur in the exec mode only), and one of how nodes should appear in tree mode. Each has the same format, described below. Any text that doesn't match a special pattern is copied verbatim.
The following variables are recognized:
v OPf_WANT_VOID Want nothing (void context) s OPf_WANT_SCALAR Want single value (scalar context) l OPf_WANT_LIST Want list of any length (list context) K OPf_KIDS There is a firstborn child. P OPf_PARENS This operator was parenthesized. (Or block needs explicit scope entry.) R OPf_REF Certified reference. (Return container, not containee). M OPf_MOD Will modify (lvalue). S OPf_STACKED Some arg is arriving on the stack. * OPf_SPECIAL Do something weird for this op (see op.h)
0 OP (aka BASEOP) An OP with no children 1 UNOP An OP with one child 2 BINOP An OP with two children | LOGOP A control branch OP @ LISTOP An OP that could have lots of children / PMOP An OP with a regular expression $ SVOP An OP with an SV " PVOP An OP with a string { LOOP An OP that holds pointers for a loop ; COP An OP that marks the start of a statement # PADOP An OP with a GV on the pad
You can use B::Concise, and call compile()
directly, thereby
avoiding the compile-only operation of O. For example, you could use
the debugger to step through B::Concise::compile() itself.
When doing so, you can alter Concise output by providing new output styles, and optionally by adding callback routines which populate new variables that may be rendered as part of those styles. For all following sections, please review FORMATTING SPECIFICATIONS.
use B::Concise qw(set_style add_callback); set_style($your_format, $your_gotofmt, $your_treefmt); add_callback ( sub { my ($h, $op, $format, $level, $stylename) = @_; $h->{variable} = some_func($op); } ); B::Concise::compile(@options)->();
set_style()
set_style accepts 3 arguments, and updates the three components of an
output style (basic-exec, goto, tree). It has one minor drawback though:
it doesn't register the style under a new name, thus you may prefer to use
add_style()
and/or set_style_standard()
instead.
add_style()
This subroutine accepts a new style name and three style arguments as
above, and creates, registers, and selects the newly named style. It is
an error to re-add a style; call set_style_standard()
to switch between
several styles.
set_style_standard($name)
This restores one of the standard styles: terse
, concise
,
linenoise
, debug
, env
, into effect. It also accepts style
names previously defined with add_style().
add_callback()
If your newly minted styles refer to any #variables, you'll need to define a callback subroutine that will populate (or modify) those variables. They are then available for use in the style you've chosen.
The callbacks are called for each opcode visited by Concise, in the same order as they are added. Each subroutine is passed five parameters.
1. A hashref, containing the variable names and values which are populated into the report-line for the op 2. the op, as a B<B::OP> object 3. a reference to the format string 4. the formatting (indent) level 5. the selected stylename
To define your own variables, simply add them to the hash, or change existing values if you need to. The level and format are passed in as references to scalars, but it is unlikely that they will need to be changed or even used.
compile accepts options as described above in OPTIONS, and arguments, which are either coderefs, or subroutine names.
compile()
constructs and returns a coderef, which when invoked, scans
the optree, and prints the results to STDOUT. Once you have the
coderef, you may change the output style; thereafter the coderef renders
in the new style.
walk_output lets you change the print destination from STDOUT to another open filehandle, or into a string passed as a ref.
walk_output(\my $buf); B::Concise::compile('-concise','funcName', \&aSubRef)->(); print "Concise Results: $buf\n";
For each subroutine visited, the opcode info is preceded by a single line containing either the subroutine name or the stringified coderef.
To switch back to one of the standard styles like concise
or
terse
, call set_style_standard
, or pass the style name into
B::Concise::compile() (as done above).
This function (not exported) lets you reset the sequence numbers (note that they're numbered arbitrarily, their goal being to be human readable). Its purpose is mostly to support testing, i.e. to compare the concise output from two identical anonymous subroutines (but different instances). Without the reset, B::Concise, seeing that they're separate optrees, generates different sequence numbers in the output.
All detected errors, (invalid arguments, internal errors, etc.) are resolved with a die($message). Use an eval if you wish to catch these errors and continue processing.
In particular, compile will die as follows if you've asked for a non-existent function-name, a non-existent coderef, or a non-CODE reference.
Stephen McCamant, <smcc@CSUA.Berkeley.EDU>.
B::Concise - Walk Perl syntax tree, printing concise info about ops |