perl
view release on metacpan or search on metacpan
ext/B/B/Concise.pm view on Meta::CPAN
$banner = 0;
} elsif ($o eq "-banner") {
$banner = 1;
}
elsif ($o eq "-main") {
$do_main = 1;
} elsif ($o eq "-nomain") {
$do_main = 0;
} elsif ($o eq "-src") {
$show_src = 1;
}
elsif ($o =~ /^-stash=(.*)/) {
my $pkg = $1;
no strict 'refs';
if (! %{$pkg.'::'}) {
eval "require $pkg";
} else {
require Config;
if (!$Config::Config{usedl}
&& keys %{$pkg.'::'} == 1
&& $pkg->can('bootstrap')) {
# It is something that we're statically linked to, but hasn't
# yet been used.
eval "require $pkg";
}
}
push @render_packs, $pkg;
}
# line-style options
elsif (exists $style{substr($o, 1)}) {
$stylename = substr($o, 1);
set_style_standard($stylename);
} else {
warn "Option $o unrecognized";
}
}
return (@args);
}
sub compile {
my (@args) = compileOpts(@_);
return sub {
my @newargs = compileOpts(@_); # accept new rendering options
warn "disregarding non-options: @newargs\n" if @newargs;
for my $objname (@args) {
next unless $objname; # skip null args to avoid noisy responses
if ($objname eq "BEGIN") {
concise_specials("BEGIN", $order,
B::begin_av->isa("B::AV") ?
B::begin_av->ARRAY : ());
} elsif ($objname eq "INIT") {
concise_specials("INIT", $order,
B::init_av->isa("B::AV") ?
B::init_av->ARRAY : ());
} elsif ($objname eq "CHECK") {
concise_specials("CHECK", $order,
B::check_av->isa("B::AV") ?
B::check_av->ARRAY : ());
} elsif ($objname eq "UNITCHECK") {
concise_specials("UNITCHECK", $order,
B::unitcheck_av->isa("B::AV") ?
B::unitcheck_av->ARRAY : ());
} elsif ($objname eq "END") {
concise_specials("END", $order,
B::end_av->isa("B::AV") ?
B::end_av->ARRAY : ());
}
else {
# convert function names to subrefs
if (ref $objname) {
print $walkHandle "B::Concise::compile($objname)\n"
if $banner;
concise_subref($order, ($objname)x2);
next;
} else {
$objname = "main::" . $objname unless $objname =~ /::/;
no strict 'refs';
my $glob = \*$objname;
unless (*$glob{CODE} || *$glob{FORMAT}) {
print $walkHandle "$objname:\n" if $banner;
print $walkHandle "err: unknown function ($objname)\n";
return;
}
if (my $objref = *$glob{CODE}) {
print $walkHandle "$objname:\n" if $banner;
concise_subref($order, $objref, $objname);
}
if (my $objref = *$glob{FORMAT}) {
print $walkHandle "$objname (FORMAT):\n"
if $banner;
concise_subref($order, $objref, $objname);
}
}
}
}
for my $pkg (@render_packs) {
no strict 'refs';
concise_stashref($order, \%{$pkg.'::'});
}
if (!@args or $do_main or @render_packs) {
print $walkHandle "main program:\n" if $do_main;
concise_main($order);
}
return @args; # something
}
}
my %labels;
my $lastnext; # remembers op-chain, used to insert gotos
my %opclass = ('OP' => "0", 'UNOP' => "1", 'BINOP' => "2", 'LOGOP' => "|",
'LISTOP' => "@", 'PMOP' => "/", 'SVOP' => "\$", 'GVOP' => "*",
'PVOP' => '"', 'LOOP' => "{", 'COP' => ";", 'PADOP' => "#",
'METHOP' => '.', UNOP_AUX => '+');
no warnings 'qw'; # "Possible attempt to put comments..."; use #7
my @linenoise =
qw'# () sc ( @? 1 $* gv *{ m$ m@ m% m? p/ *$ $ $# & a& pt \\ s\\ rf bl
` *? <> ?? ?/ r/ c/ // qr s/ /c y/ = @= C sC Cp sp df un BM po +1 +I
ext/B/B/Concise.pm view on Meta::CPAN
* 5 <2> add[t3] sK/2
6 <#> gvsv[*a] s
7 <2> sassign vKS/2
8 <@> leave[1 ref] vKP/REFC
In this -exec rendering, each opcode is executed in the order shown.
The add opcode, marked with '*', is discussed in more detail.
The 1st column is the op's sequence number, starting at 1, and is
displayed in base 36 by default. Here they're purely linear; the
sequences are very helpful when looking at code with loops and
branches.
The symbol between angle brackets indicates the op's type, for
example; <2> is a BINOP, <@> a LISTOP, and <#> is a PADOP, which is
used in threaded perls. (see L</"OP class abbreviations">).
The opname, as in B<'add[t1]'>, may be followed by op-specific
information in parentheses or brackets (ex B<'[t1]'>).
The op-flags (ex B<'sK/2'>) are described in (L</"OP flags
abbreviations">).
% 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
The default rendering is top-down, so they're not in execution order.
This form reflects the way the stack is used to parse and evaluate
expressions; the add operates on the two terms below it in the tree.
Nullops appear as C<ex-opname>, where I<opname> is an op that has been
optimized away by perl. They're displayed with a sequence-number of
'-', because they are not executed (they don't appear in previous
example), they're printed here because they reflect the parse.
The arrow points to the sequence number of the next op; they're not
displayed in -exec mode, for obvious reasons.
Note that because this rendering was done on a non-threaded perl, the
PADOPs in the previous examples are now SVOPs, and some (but not all)
of the square brackets have been replaced by round ones. This is a
subtle feature to provide some visual distinction between renderings
on threaded and un-threaded perls.
=head1 OPTIONS
Arguments that don't start with a hyphen are taken to be the names of
subroutines or formats to render; 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 rendered. Passing C<BEGIN>, C<UNITCHECK>,
C<CHECK>, C<INIT>, or C<END> will cause all of the corresponding
special blocks to be printed. Arguments must follow options.
Options affect how things are rendered (ie printed). They're presented
here by their visual effect, 1st being strongest. They're grouped
according to how they interrelate; within each group the options are
mutually exclusive (unless otherwise stated).
=head2 Options for Opcode Ordering
These options control the 'vertical display' of opcodes. The display
'order' is also called 'mode' elsewhere in this document.
=over 4
=item B<-basic>
Print OPs in the order they appear in the OP tree (a preorder
traversal, starting at the root). The indentation of each OP shows its
level in the tree, and the '->' at the end of the line indicates the
next opcode in execution order. This mode is the default, so the flag
is included simply for completeness.
=item B<-exec>
Print OPs in the order they would normally execute (for the majority
of constructs this is a postorder traversal of the tree, ending at the
root). In most cases the OP that usually follows a given OP will
appear directly below it; alternate paths are shown by indentation. In
cases like loops when control jumps out of a linear path, a 'goto'
line is generated.
=item B<-tree>
Print OPs in a text approximation of a tree, with the root of the tree
at the left and 'left-to-right' order of children transformed into
'top-to-bottom'. Because this mode grows both to the right and down,
it isn't suitable for large programs (unless you have a very wide
terminal).
=back
=head2 Options for Line-Style
These options select the line-style (or just style) used to render
each opcode, and dictates what info is actually printed into each line.
=over 4
=item B<-concise>
Use the author's favorite set of formatting conventions. This is the
default, of course.
=item B<-terse>
Use formatting conventions that emulate the output of B<B::Terse>. The
basic mode is almost indistinguishable from the real B<B::Terse>, and the
exec mode looks very similar, but is in a more logical order and lacks
curly brackets. B<B::Terse> doesn't have a tree mode, so the tree mode
( run in 0.625 second using v1.01-cache-2.11-cpan-5a3173703d6 )