3 # Copyright (c) 1996 Malcolm Beattie
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 package B
::Disassembler
::BytecodeStream
;
10 use B
qw(cstring cast_I32);
11 @ISA = qw(FileHandle);
15 read($fh, $data, $len);
16 croak
"reached EOF while reading $len bytes" unless length($data) == $len;
23 croak
"reached EOF while reading U8" unless defined($c);
29 my $str = $fh->readn(2);
30 croak
"reached EOF while reading U16" unless length($str) == 2;
31 return unpack("n", $str);
36 my $str = $fh->readn(8);
37 croak
"reached EOF while reading NV" unless length($str) == 8;
38 return unpack("N", $str);
43 my $str = $fh->readn(4);
44 croak
"reached EOF while reading U32" unless length($str) == 4;
45 return unpack("N", $str);
50 my $str = $fh->readn(4);
51 croak
"reached EOF while reading I32" unless length($str) == 4;
52 return cast_I32
(unpack("N", $str));
57 my $str = $fh->readn(4);
58 croak
"reached EOF while reading objindex" unless length($str) == 4;
59 return unpack("N", $str);
64 my $str = $fh->readn(4);
65 croak
"reached EOF while reading opindex" unless length($str) == 4;
66 return unpack("N", $str);
71 my $str = $fh->readn(4);
72 croak
"reached EOF while reading svindex" unless length($str) == 4;
73 return unpack("N", $str);
79 while (defined($c = $fh->getc) && $c ne "\0") {
82 croak
"reached EOF while reading strconst" unless defined($c);
91 my $len = $fh->GET_U32;
93 read($fh, $str, $len);
94 croak
"reached EOF while reading PV" unless length($str) == $len;
104 while (defined($c = $fh->getc) && $c ne "\n") {
107 croak
"reached EOF while reading comment" unless defined($c);
108 return cstring
($str);
114 while (defined($c = $fh->getc) && $c ne "\0") {
117 croak
"reached EOF while reading double" unless defined($c);
123 sub GET_op_tr_array
{
125 my @ary = unpack("n256", $fh->readn(256 * 2));
126 return join(",", @ary);
131 my ($hi, $lo) = unpack("NN", $fh->readn(8));
132 return sprintf("0x%4x%04x", $hi, $lo); # cheat
135 package B
::Disassembler
;
138 @EXPORT_OK = qw(disassemble_fh);
142 use B
::Asmdata
qw(%insn_data @insn_name);
146 my ($c, $getmeth, $insn, $arg);
147 bless $fh, "B::Disassembler::BytecodeStream";
148 while (defined($c = $fh->getc)) {
150 $insn = $insn_name[$c];
151 if (!defined($insn) || $insn eq "unused") {
152 my $pos = $fh->tell - 1;
153 die "Illegal instruction code $c at stream offset $pos\n";
155 $getmeth = $insn_data{$insn}->[2];
156 $arg = $fh->$getmeth();
171 B::Disassembler - Disassemble Perl bytecode
179 See F<ext/B/B/Disassembler.pm>.
183 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>