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;
13 use Config qw(%Config);
14 use B qw(cstring cast_I32);
15 @ISA = qw(FileHandle);
19 read($fh, $data, $len);
20 croak "reached EOF while reading $len bytes" unless length($data) == $len;
27 croak "reached EOF while reading U8" unless defined($c);
33 my $str = $fh->readn(2);
34 croak "reached EOF while reading U16" unless length($str) == 2;
35 return unpack("S", $str);
41 while (defined($c = $fh->getc) && $c ne "\0") {
44 croak "reached EOF while reading double" unless defined($c);
50 my $str = $fh->readn(4);
51 croak "reached EOF while reading U32" unless length($str) == 4;
52 return unpack("L", $str);
57 my $str = $fh->readn(4);
58 croak "reached EOF while reading I32" unless length($str) == 4;
59 return unpack("l", $str);
64 my $str = $fh->readn(4);
65 croak "reached EOF while reading objindex" unless length($str) == 4;
66 return unpack("L", $str);
71 my $str = $fh->readn(4);
72 croak "reached EOF while reading opindex" unless length($str) == 4;
73 return unpack("L", $str);
78 my $str = $fh->readn(4);
79 croak "reached EOF while reading svindex" unless length($str) == 4;
80 return unpack("L", $str);
85 my $str = $fh->readn(4);
86 croak "reached EOF while reading pvindex" unless length($str) == 4;
87 return unpack("L", $str);
94 while (defined($c = $fh->getc) && $c ne "\0") {
97 croak "reached EOF while reading strconst" unless defined($c);
101 sub GET_pvcontents {}
106 my $len = $fh->GET_U32;
108 read($fh, $str, $len);
109 croak "reached EOF while reading PV" unless length($str) == $len;
110 return cstring($str);
119 while (defined($c = $fh->getc) && $c ne "\n") {
122 croak "reached EOF while reading comment" unless defined($c);
123 return cstring($str);
129 while (defined($c = $fh->getc) && $c ne "\0") {
132 croak "reached EOF while reading double" unless defined($c);
138 sub GET_op_tr_array {
140 my $len = unpack "S", $fh->readn(2);
141 my @ary = unpack "S*", $fh->readn($len*2);
142 return join(",", $len, @ary);
147 my $str = $fh->readn(8);
148 croak "reached EOF while reading I32" unless length($str) == 8;
149 return sprintf "0x%09llx", unpack("q", $str);
153 $Config{ivsize} == 4 ? &GET_I32 : &GET_IV64;
156 sub B::::GET_PADOFFSET {
157 $Config{ptrsize} == 8 ? &B::GET_IV64 : &B::GET_U32;
161 $Config{longsize} == 8 ? &B::GET_IV64 : &B::GET_U32;
165 package B::Disassembler;
168 @EXPORT_OK = qw(disassemble_fh get_header);
172 use B::Asmdata qw(%insn_data @insn_name);
174 our( $magic, $archname, $blversion, $ivsize, $ptrsize );
178 $magic = $fh->GET_U32();
179 warn( "bad magic" ) if $magic != 0x43424c50;
180 $archname = $fh->GET_strconst();
181 $blversion = $fh->GET_strconst();
182 $ivsize = $fh->GET_U32();
183 $ptrsize = $fh->GET_U32();
187 return( $magic, $archname, $blversion, $ivsize, $ptrsize);
192 my ($c, $getmeth, $insn, $arg);
193 bless $fh, "B::Disassembler::BytecodeStream";
195 while (defined($c = $fh->getc)) {
197 $insn = $insn_name[$c];
198 if (!defined($insn) || $insn eq "unused") {
199 my $pos = $fh->tell - 1;
200 die "Illegal instruction code $c at stream offset $pos\n";
202 $getmeth = $insn_data{$insn}->[2];
203 $arg = $fh->$getmeth();
218 B::Disassembler - Disassemble Perl bytecode
226 See F<ext/B/B/Disassembler.pm>.
230 Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>