Bblock.pm 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # Maintained now in B::C by Reini Urban <rurban@x-ray.at>
  2. package B::Bblock;
  3. our $VERSION = '1.03_02';
  4. use Exporter ();
  5. @ISA = "Exporter";
  6. @EXPORT_OK = qw(find_leaders);
  7. my $have_B_Concise;
  8. use B qw(peekop walkoptree walkoptree_exec
  9. main_root main_start svref_2object
  10. OPf_SPECIAL OPf_STACKED );
  11. BEGIN {
  12. eval { require B::Concise; 1 } and $have_B_Concise = 1;
  13. B::Concise->import(qw(concise_cv concise_main set_style_standard))
  14. if $have_B_Concise;
  15. }
  16. use strict;
  17. my $bblock;
  18. my @bblock_ends;
  19. sub mark_leader {
  20. my $op = shift;
  21. if ($$op) {
  22. $bblock->{$$op} = $op;
  23. }
  24. }
  25. sub remove_sortblock {
  26. foreach ( keys %$bblock ) {
  27. my $leader = $$bblock{$_};
  28. delete $$bblock{$_} if ( $leader == 0 );
  29. }
  30. }
  31. sub find_leaders {
  32. my ( $root, $start ) = @_;
  33. $bblock = {};
  34. mark_leader($start) if ( ref $start ne "B::NULL" );
  35. walkoptree( $root, "mark_if_leader" ) if ( ( ref $root ) ne "B::NULL" );
  36. remove_sortblock();
  37. return $bblock;
  38. }
  39. # Debugging
  40. sub walk_bblocks {
  41. my ( $root, $start ) = @_;
  42. my ( $op, $lastop, $leader, $bb );
  43. $bblock = {};
  44. mark_leader($start);
  45. walkoptree( $root, "mark_if_leader" );
  46. my @leaders = values %$bblock;
  47. while ( $leader = shift @leaders ) {
  48. $lastop = $leader;
  49. $op = $leader->next;
  50. while ( $$op && !exists( $bblock->{$$op} ) ) {
  51. $bblock->{$$op} = $leader;
  52. $lastop = $op;
  53. $op = $op->next;
  54. }
  55. push( @bblock_ends, [ $leader, $lastop ] );
  56. }
  57. foreach $bb (@bblock_ends) {
  58. ( $leader, $lastop ) = @$bb;
  59. printf "%s .. %s\n", peekop($leader), peekop($lastop);
  60. for ( $op = $leader ; $$op != $$lastop ; $op = $op->next ) {
  61. printf " %s\n", peekop($op);
  62. }
  63. printf " %s\n", peekop($lastop);
  64. }
  65. }
  66. sub walk_bblocks_obj {
  67. my $cvref = shift;
  68. my $cv = svref_2object($cvref);
  69. walk_bblocks( $cv->ROOT, $cv->START );
  70. }
  71. sub B::OP::mark_if_leader { }
  72. sub B::COP::mark_if_leader {
  73. my $op = shift;
  74. if ( $op->label ) {
  75. mark_leader($op);
  76. }
  77. }
  78. sub B::LOOP::mark_if_leader {
  79. my $op = shift;
  80. mark_leader( $op->next );
  81. mark_leader( $op->nextop );
  82. mark_leader( $op->redoop );
  83. mark_leader( $op->lastop->next );
  84. }
  85. sub B::LOGOP::mark_if_leader {
  86. my $op = shift;
  87. my $opname = $op->name;
  88. mark_leader( $op->next );
  89. if ( $opname eq "entertry" ) {
  90. mark_leader( $op->other->next );
  91. }
  92. else {
  93. mark_leader( $op->other );
  94. }
  95. }
  96. sub B::LISTOP::mark_if_leader {
  97. my $op = shift;
  98. my $first = $op->first;
  99. $first = $first->next while ( $first->name eq "null" );
  100. mark_leader( $op->first ) unless ( exists( $bblock->{$$first} ) );
  101. mark_leader( $op->next );
  102. if ( $op->name eq "sort"
  103. and $op->flags & OPf_SPECIAL
  104. and $op->flags & OPf_STACKED )
  105. {
  106. my $root = $op->first->sibling->first;
  107. my $leader = $root->first;
  108. $bblock->{$$leader} = 0;
  109. }
  110. }
  111. sub B::PMOP::mark_if_leader {
  112. my $op = shift;
  113. if ( $op->name ne "pushre" ) {
  114. my $replroot = $op->pmreplroot;
  115. if ($$replroot) {
  116. mark_leader( $replroot );
  117. mark_leader( $op->next );
  118. mark_leader( $op->pmreplstart );
  119. }
  120. }
  121. }
  122. # PMOP stuff omitted
  123. sub compile {
  124. my @options = @_;
  125. B::clearsym();
  126. if ( @options and $have_B_Concise ) {
  127. return sub {
  128. my $objname;
  129. foreach $objname (@options) {
  130. $objname = "main::$objname" unless $objname =~ /::/;
  131. print "walk_bblocks $objname\n";
  132. eval "walk_bblocks_obj(\\&$objname)";
  133. die "walk_bblocks_obj(\\&$objname) failed: $@" if $@;
  134. print "-------\n";
  135. set_style_standard("terse");
  136. eval "concise_cv('exec', \\&$objname)";
  137. die "concise_cv('exec', \\&$objname) failed: $@" if $@;
  138. }
  139. }
  140. }
  141. else {
  142. return sub {
  143. walk_bblocks( main_root, main_start );
  144. print "-------\n";
  145. if ($have_B_Concise) {
  146. set_style_standard("terse");
  147. concise_main("exec");
  148. }
  149. };
  150. }
  151. }
  152. 1;
  153. __END__
  154. =head1 NAME
  155. B::Bblock - Walk basic blocks
  156. =head1 SYNOPSIS
  157. # External interface
  158. perl -MO=Bblock[,OPTIONS] foo.pl
  159. perl -MO=Bblock foo.pl prints the basic block for main_root
  160. perl -MO=Bblock,foo::mysub foo.pm prints the basic block for &pkg::mysub
  161. perl -MO=Bblock,mysub foo.pm prints the basic block for &main::mysub
  162. # Programmatic API
  163. use B::Bblock qw(find_leaders);
  164. my $leaders = find_leaders($root_op, $start_op);
  165. =head1 DESCRIPTION
  166. This module is used by the L<B::CC> backend. It walks "basic blocks".
  167. A basic block is a series of operations which is known to execute from
  168. start to finish, with no possibility of branching or halting.
  169. The block is the list of ops from the every leader up to the next.
  170. The leaders are the separator of each basic block.
  171. The module can be used either stand alone or from inside another program.
  172. Standalone it just prints the basic blocks ops in L<B::Concise>.
  173. terse format to STDOUT.
  174. Without options it starts at the main_root.
  175. Basic block leaders are:
  176. Any COP (pp_nextstate) with a non-NULL label.
  177. [The op after a pp_enter] Omit
  178. [The op after a pp_entersub. Don't count this one.]
  179. The ops pointed at by nextop, redoop and lastop->op_next of a LOOP.
  180. The ops pointed at by op_next and op_other of a LOGOP, except
  181. for pp_entertry which has op_next and op_other->op_next
  182. The op pointed at by op_pmreplstart of a PMOP.
  183. The op pointed at by op_other->op_pmreplstart of pp_substcont?
  184. [The op after a pp_return] Omit
  185. =head1 OPTIONS
  186. A comma-separated list of sub names to walk.
  187. =head2 Functions
  188. =over 4
  189. =item B<find_leaders>
  190. my $leaders = find_leaders($root_op, $start_op);
  191. Given the root of the op tree and an op from which to start
  192. processing, it will return a hash ref representing all the ops which
  193. start a block.
  194. The values of %$leaders are the op objects themselves. Keys are $$op
  195. addresses.
  196. =back
  197. =head1 AUTHOR
  198. Malcolm Beattie C<MICB at cpan.org> I<(retired)>,
  199. Reini Urban C<perl-compiler@googlegroups.com>
  200. =cut
  201. # Local Variables:
  202. # mode: cperl
  203. # cperl-indent-level: 2
  204. # fill-column: 78
  205. # End:
  206. # vim: expandtab shiftwidth=2: