s/use vars/our/g modules that aren't independently maintained on CPAN
[p5sagit/p5-mst-13.2.git] / ext / B / B / Disassembler.pm
CommitLineData
a798dbf2 1# Disassembler.pm
2#
3# Copyright (c) 1996 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.
7package B::Disassembler::BytecodeStream;
8use FileHandle;
9use Carp;
10use B qw(cstring cast_I32);
11@ISA = qw(FileHandle);
12sub readn {
13 my ($fh, $len) = @_;
14 my $data;
15 read($fh, $data, $len);
16 croak "reached EOF while reading $len bytes" unless length($data) == $len;
17 return $data;
18}
19
20sub GET_U8 {
21 my $fh = shift;
22 my $c = $fh->getc;
23 croak "reached EOF while reading U8" unless defined($c);
24 return ord($c);
25}
26
27sub GET_U16 {
28 my $fh = shift;
29 my $str = $fh->readn(2);
30 croak "reached EOF while reading U16" unless length($str) == 2;
31 return unpack("n", $str);
32}
33
34sub GET_U32 {
35 my $fh = shift;
36 my $str = $fh->readn(4);
37 croak "reached EOF while reading U32" unless length($str) == 4;
38 return unpack("N", $str);
39}
40
41sub GET_I32 {
42 my $fh = shift;
43 my $str = $fh->readn(4);
44 croak "reached EOF while reading I32" unless length($str) == 4;
45 return cast_I32(unpack("N", $str));
46}
47
48sub GET_objindex {
49 my $fh = shift;
50 my $str = $fh->readn(4);
51 croak "reached EOF while reading objindex" unless length($str) == 4;
52 return unpack("N", $str);
53}
54
a9a9fdd7 55sub GET_opindex {
56 my $fh = shift;
57 my $str = $fh->readn(4);
58 croak "reached EOF while reading opindex" unless length($str) == 4;
59 return unpack("N", $str);
60}
61
62sub GET_svindex {
63 my $fh = shift;
64 my $str = $fh->readn(4);
65 croak "reached EOF while reading svindex" unless length($str) == 4;
66 return unpack("N", $str);
67}
68
a798dbf2 69sub GET_strconst {
70 my $fh = shift;
71 my ($str, $c);
72 while (defined($c = $fh->getc) && $c ne "\0") {
73 $str .= $c;
74 }
75 croak "reached EOF while reading strconst" unless defined($c);
76 return cstring($str);
77}
78
79sub GET_pvcontents {}
80
81sub GET_PV {
82 my $fh = shift;
83 my $str;
84 my $len = $fh->GET_U32;
85 if ($len) {
86 read($fh, $str, $len);
87 croak "reached EOF while reading PV" unless length($str) == $len;
88 return cstring($str);
89 } else {
90 return '""';
91 }
92}
93
d53d2b93 94sub GET_comment_t {
a798dbf2 95 my $fh = shift;
96 my ($str, $c);
97 while (defined($c = $fh->getc) && $c ne "\n") {
98 $str .= $c;
99 }
100 croak "reached EOF while reading comment" unless defined($c);
101 return cstring($str);
102}
103
104sub GET_double {
105 my $fh = shift;
106 my ($str, $c);
107 while (defined($c = $fh->getc) && $c ne "\0") {
108 $str .= $c;
109 }
110 croak "reached EOF while reading double" unless defined($c);
111 return $str;
112}
113
114sub GET_none {}
115
116sub GET_op_tr_array {
117 my $fh = shift;
118 my @ary = unpack("n256", $fh->readn(256 * 2));
119 return join(",", @ary);
120}
121
122sub GET_IV64 {
123 my $fh = shift;
124 my ($hi, $lo) = unpack("NN", $fh->readn(8));
125 return sprintf("0x%4x%04x", $hi, $lo); # cheat
126}
127
128package B::Disassembler;
129use Exporter;
130@ISA = qw(Exporter);
131@EXPORT_OK = qw(disassemble_fh);
132use Carp;
133use strict;
134
135use B::Asmdata qw(%insn_data @insn_name);
136
137sub disassemble_fh {
138 my ($fh, $out) = @_;
139 my ($c, $getmeth, $insn, $arg);
140 bless $fh, "B::Disassembler::BytecodeStream";
141 while (defined($c = $fh->getc)) {
142 $c = ord($c);
143 $insn = $insn_name[$c];
144 if (!defined($insn) || $insn eq "unused") {
145 my $pos = $fh->tell - 1;
146 die "Illegal instruction code $c at stream offset $pos\n";
147 }
148 $getmeth = $insn_data{$insn}->[2];
149 $arg = $fh->$getmeth();
150 if (defined($arg)) {
151 &$out($insn, $arg);
152 } else {
153 &$out($insn);
154 }
155 }
156}
157
1581;
7f20e9dd 159
160__END__
161
162=head1 NAME
163
164B::Disassembler - Disassemble Perl bytecode
165
166=head1 SYNOPSIS
167
168 use Disassembler;
169
170=head1 DESCRIPTION
171
172See F<ext/B/B/Disassembler.pm>.
173
174=head1 AUTHOR
175
176Malcolm Beattie, C<mbeattie@sable.ox.ac.uk>
177
178=cut