home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / perl / 5.8.8 / B.pm < prev    next >
Encoding:
Perl POD Document  |  2007-03-05  |  7.1 KB  |  309 lines

  1. #      B.pm
  2. #
  3. #      Copyright (c) 1996, 1997, 1998 Malcolm Beattie
  4. #
  5. #      You may distribute under the terms of either the GNU General Public
  6. #      License or the Artistic License, as specified in the README file.
  7. #
  8. package B;
  9.  
  10. our $VERSION = '1.09_01';
  11.  
  12. use XSLoader ();
  13. require Exporter;
  14. @ISA = qw(Exporter);
  15.  
  16. # walkoptree_slow comes from B.pm (you are there),
  17. # walkoptree comes from B.xs
  18. @EXPORT_OK = qw(minus_c ppname save_BEGINs
  19.         class peekop cast_I32 cstring cchar hash threadsv_names
  20.         main_root main_start main_cv svref_2object opnumber
  21.         amagic_generation perlstring
  22.         walkoptree_slow walkoptree walkoptree_exec walksymtable
  23.         parents comppadlist sv_undef compile_stats timing_info
  24.         begin_av init_av check_av end_av regex_padav dowarn
  25.         defstash curstash warnhook diehook inc_gv
  26.         );
  27.  
  28. sub OPf_KIDS ();
  29. use strict;
  30. @B::SV::ISA = 'B::OBJECT';
  31. @B::NULL::ISA = 'B::SV';
  32. @B::PV::ISA = 'B::SV';
  33. @B::IV::ISA = 'B::SV';
  34. @B::NV::ISA = 'B::SV';
  35. @B::RV::ISA = 'B::SV';
  36. @B::PVIV::ISA = qw(B::PV B::IV);
  37. @B::PVNV::ISA = qw(B::PVIV B::NV);
  38. @B::PVMG::ISA = 'B::PVNV';
  39. # Change in the inheritance hierarchy post 5.9.0
  40. @B::PVLV::ISA = $] > 5.009 ? 'B::GV' : 'B::PVMG';
  41. @B::BM::ISA = 'B::PVMG';
  42. @B::AV::ISA = 'B::PVMG';
  43. @B::GV::ISA = 'B::PVMG';
  44. @B::HV::ISA = 'B::PVMG';
  45. @B::CV::ISA = 'B::PVMG';
  46. @B::IO::ISA = 'B::PVMG';
  47. @B::FM::ISA = 'B::CV';
  48.  
  49. @B::OP::ISA = 'B::OBJECT';
  50. @B::UNOP::ISA = 'B::OP';
  51. @B::BINOP::ISA = 'B::UNOP';
  52. @B::LOGOP::ISA = 'B::UNOP';
  53. @B::LISTOP::ISA = 'B::BINOP';
  54. @B::SVOP::ISA = 'B::OP';
  55. @B::PADOP::ISA = 'B::OP';
  56. @B::PVOP::ISA = 'B::OP';
  57. @B::LOOP::ISA = 'B::LISTOP';
  58. @B::PMOP::ISA = 'B::LISTOP';
  59. @B::COP::ISA = 'B::OP';
  60.  
  61. @B::SPECIAL::ISA = 'B::OBJECT';
  62.  
  63. {
  64.     # Stop "-w" from complaining about the lack of a real B::OBJECT class
  65.     package B::OBJECT;
  66. }
  67.  
  68. sub B::GV::SAFENAME {
  69.   my $name = (shift())->NAME;
  70.  
  71.   # The regex below corresponds to the isCONTROLVAR macro
  72.   # from toke.c
  73.  
  74.   $name =~ s/^([\cA-\cZ\c\\c[\c]\c?\c_\c^])/"^".
  75.     chr( utf8::unicode_to_native( 64 ^ ord($1) ))/e;
  76.  
  77.   # When we say unicode_to_native we really mean ascii_to_native,
  78.   # which matters iff this is a non-ASCII platform (EBCDIC).
  79.  
  80.   return $name;
  81. }
  82.  
  83. sub B::IV::int_value {
  84.   my ($self) = @_;
  85.   return (($self->FLAGS() & SVf_IVisUV()) ? $self->UVX : $self->IV);
  86. }
  87.  
  88. sub B::NULL::as_string() {""}
  89. sub B::IV::as_string()   {goto &B::IV::int_value}
  90. sub B::PV::as_string()   {goto &B::PV::PV}
  91.  
  92. my $debug;
  93. my $op_count = 0;
  94. my @parents = ();
  95.  
  96. sub debug {
  97.     my ($class, $value) = @_;
  98.     $debug = $value;
  99.     walkoptree_debug($value);
  100. }
  101.  
  102. sub class {
  103.     my $obj = shift;
  104.     my $name = ref $obj;
  105.     $name =~ s/^.*:://;
  106.     return $name;
  107. }
  108.  
  109. sub parents { \@parents }
  110.  
  111. # For debugging
  112. sub peekop {
  113.     my $op = shift;
  114.     return sprintf("%s (0x%x) %s", class($op), $$op, $op->name);
  115. }
  116.  
  117. sub walkoptree_slow {
  118.     my($op, $method, $level) = @_;
  119.     $op_count++; # just for statistics
  120.     $level ||= 0;
  121.     warn(sprintf("walkoptree: %d. %s\n", $level, peekop($op))) if $debug;
  122.     $op->$method($level);
  123.     if ($$op && ($op->flags & OPf_KIDS)) {
  124.     my $kid;
  125.     unshift(@parents, $op);
  126.     for ($kid = $op->first; $$kid; $kid = $kid->sibling) {
  127.         walkoptree_slow($kid, $method, $level + 1);
  128.     }
  129.     shift @parents;
  130.     }
  131.     if (class($op) eq 'PMOP' && ref($op->pmreplroot) && ${$op->pmreplroot}) {
  132.     unshift(@parents, $op);
  133.     walkoptree_slow($op->pmreplroot, $method, $level + 1);
  134.     shift @parents;
  135.     }
  136. }
  137.  
  138. sub compile_stats {
  139.     return "Total number of OPs processed: $op_count\n";
  140. }
  141.  
  142. sub timing_info {
  143.     my ($sec, $min, $hr) = localtime;
  144.     my ($user, $sys) = times;
  145.     sprintf("%02d:%02d:%02d user=$user sys=$sys",
  146.         $hr, $min, $sec, $user, $sys);
  147. }
  148.  
  149. my %symtable;
  150.  
  151. sub clearsym {
  152.     %symtable = ();
  153. }
  154.  
  155. sub savesym {
  156.     my ($obj, $value) = @_;
  157. #    warn(sprintf("savesym: sym_%x => %s\n", $$obj, $value)); # debug
  158.     $symtable{sprintf("sym_%x", $$obj)} = $value;
  159. }
  160.  
  161. sub objsym {
  162.     my $obj = shift;
  163.     return $symtable{sprintf("sym_%x", $$obj)};
  164. }
  165.  
  166. sub walkoptree_exec {
  167.     my ($op, $method, $level) = @_;
  168.     $level ||= 0;
  169.     my ($sym, $ppname);
  170.     my $prefix = "    " x $level;
  171.     for (; $$op; $op = $op->next) {
  172.     $sym = objsym($op);
  173.     if (defined($sym)) {
  174.         print $prefix, "goto $sym\n";
  175.         return;
  176.     }
  177.     savesym($op, sprintf("%s (0x%lx)", class($op), $$op));
  178.     $op->$method($level);
  179.     $ppname = $op->name;
  180.     if ($ppname =~
  181.         /^(d?or(assign)?|and(assign)?|mapwhile|grepwhile|entertry|range|cond_expr)$/)
  182.     {
  183.         print $prefix, uc($1), " => {\n";
  184.         walkoptree_exec($op->other, $method, $level + 1);
  185.         print $prefix, "}\n";
  186.     } elsif ($ppname eq "match" || $ppname eq "subst") {
  187.         my $pmreplstart = $op->pmreplstart;
  188.         if ($$pmreplstart) {
  189.         print $prefix, "PMREPLSTART => {\n";
  190.         walkoptree_exec($pmreplstart, $method, $level + 1);
  191.         print $prefix, "}\n";
  192.         }
  193.     } elsif ($ppname eq "substcont") {
  194.         print $prefix, "SUBSTCONT => {\n";
  195.         walkoptree_exec($op->other->pmreplstart, $method, $level + 1);
  196.         print $prefix, "}\n";
  197.         $op = $op->other;
  198.     } elsif ($ppname eq "enterloop") {
  199.         print $prefix, "REDO => {\n";
  200.         walkoptree_exec($op->redoop, $method, $level + 1);
  201.         print $prefix, "}\n", $prefix, "NEXT => {\n";
  202.         walkoptree_exec($op->nextop, $method, $level + 1);
  203.         print $prefix, "}\n", $prefix, "LAST => {\n";
  204.         walkoptree_exec($op->lastop,  $method, $level + 1);
  205.         print $prefix, "}\n";
  206.     } elsif ($ppname eq "subst") {
  207.         my $replstart = $op->pmreplstart;
  208.         if ($$replstart) {
  209.         print $prefix, "SUBST => {\n";
  210.         walkoptree_exec($replstart, $method, $level + 1);
  211.         print $prefix, "}\n";
  212.         }
  213.     }
  214.     }
  215. }
  216.  
  217. sub walksymtable {
  218.     my ($symref, $method, $recurse, $prefix) = @_;
  219.     my $sym;
  220.     my $ref;
  221.     my $fullname;
  222.     no strict 'refs';
  223.     $prefix = '' unless defined $prefix;
  224.     while (($sym, $ref) = each %$symref) {
  225.         $fullname = "*main::".$prefix.$sym;
  226.     if ($sym =~ /::$/) {
  227.         $sym = $prefix . $sym;
  228.         if ($sym ne "main::" && $sym ne "<none>::" && &$recurse($sym)) {
  229.                walksymtable(\%$fullname, $method, $recurse, $sym);
  230.         }
  231.     } else {
  232.            svref_2object(\*$fullname)->$method();
  233.     }
  234.     }
  235. }
  236.  
  237. {
  238.     package B::Section;
  239.     my $output_fh;
  240.     my %sections;
  241.  
  242.     sub new {
  243.     my ($class, $section, $symtable, $default) = @_;
  244.     $output_fh ||= FileHandle->new_tmpfile;
  245.     my $obj = bless [-1, $section, $symtable, $default], $class;
  246.     $sections{$section} = $obj;
  247.     return $obj;
  248.     }
  249.  
  250.     sub get {
  251.     my ($class, $section) = @_;
  252.     return $sections{$section};
  253.     }
  254.  
  255.     sub add {
  256.     my $section = shift;
  257.     while (defined($_ = shift)) {
  258.         print $output_fh "$section->[1]\t$_\n";
  259.         $section->[0]++;
  260.     }
  261.     }
  262.  
  263.     sub index {
  264.     my $section = shift;
  265.     return $section->[0];
  266.     }
  267.  
  268.     sub name {
  269.     my $section = shift;
  270.     return $section->[1];
  271.     }
  272.  
  273.     sub symtable {
  274.     my $section = shift;
  275.     return $section->[2];
  276.     }
  277.  
  278.     sub default {
  279.     my $section = shift;
  280.     return $section->[3];
  281.     }
  282.  
  283.     sub output {
  284.     my ($section, $fh, $format) = @_;
  285.     my $name = $section->name;
  286.     my $sym = $section->symtable || {};
  287.     my $default = $section->default;
  288.  
  289.     seek($output_fh, 0, 0);
  290.     while (<$output_fh>) {
  291.         chomp;
  292.         s/^(.*?)\t//;
  293.         if ($1 eq $name) {
  294.         s{(s\\_[0-9a-f]+)} {
  295.             exists($sym->{$1}) ? $sym->{$1} : $default;
  296.         }ge;
  297.         printf $fh $format, $_;
  298.         }
  299.     }
  300.     }
  301. }
  302.  
  303. XSLoader::load 'B';
  304.  
  305. 1;
  306.  
  307. __END__
  308.  
  309.