PJVM/Bytecode/Reader.pm

filter: Statementstmt 22.8% 26 / 114 Branchbran 25.0% 4 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 29.4% 5 / 17 Podpod 0.0% 0 / 1 totaltotal 23.6% 35 / 148 CC 9 SCAR 17.2
CC
9
Cov
23%
__ANON__
12.0
__ANON__
12.0
__ANON__
12.0
CRAP 5.6
« PJVM/Runtime/Class.pm ↑ Summary PJVM/Class.pm »
1 package PJVM::Bytecode::Reader;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶sub _index_byte { ${$_[1]} += 1; return (shift @{$_[0]}); }
Subroutine _index_byte: not called CC=1 SCAR=6.9
9  
10 0 ▶sub _index_short { ${$_[1]} += 2; my ($i1, $i2) = splice @{$_[0]}, 0, 2; return ($i1 << 8 | $i2); }
Subroutine _index_short: not called CC=1 SCAR=6.9
11  
12 1 ▶sub _byte { ${$_[1]} += 1; return (shift @{$_[0]}); }
Subroutine _byte: called CC=1 SCAR=0.0
13  
14 0 ▶sub _short { ${$_[1]} += 2; my ($i1, $i2) = splice @{$_[0]}, 0, 2; return ($i1 << 8 | $i2); }
Subroutine _short: not called CC=1 SCAR=6.9
15  
16 0 ▶sub _offset_short { ${$_[1]} += 2; my ($o1, $o2) = splice @{$_[0]}, 0, 2; return ($o1 << 8 | $o2); }
Subroutine _offset_short: not called CC=1 SCAR=6.9
17  
18 0 ▶sub _offset_long {
Subroutine _offset_long: not called CC=1 SCAR=6.9
19 0 ${$_[1]} += 4;
20 0 my ($o1, $o2, $o3, $o4) = splice @{$_[0]}, 0, 4;
21 0 return ($o1 << 24 | $o2 << 16 | $o3 << 8 | $o4);
22 }
23  
24 0 ▶sub _index_byte_const {
Subroutine _index_byte_const: not called CC=1 SCAR=6.9
25 0 ${$_[1]} += 2;
26 0 return splice @{$_[0]}, 0, 2;
27 }
28  
29 0 ▶sub _index_short_count_0 {
Subroutine _index_short_count_0: not called CC=1 SCAR=6.9
30 0 ${$_[1]} += 4;
31 0 my ($i1, $i2, $count, undef) = splice @{$_[0]}, 0, 4;
32 0 return ($i1 << 8 | $i2, $count);
33 }
34  
35 0 ▶sub _index_short_count {
Subroutine _index_short_count: not called CC=1 SCAR=6.9
36 0 ${$_[1]} += 3;
37 0 my ($i1, $i2, $count) = splice @{$_[0]}, 0, 3;
38 0 return ($i1 << 8 | $i2, $count);
39 }
40  
41  
42 my %Op_transformation = (
43 0x19 => \&_index_byte, # aload
44 0xbd => \&_index_short, # anewarray
45 0x3a => \&_index_byte, # astore
46
47 0x10 => \&_byte, # bipush
48  
49 0xc0 => \&_index_short, # checkcast
50
51 0x18 => \&_index_byte, # dload
52 0x39 => \&_index_byte, # dstore
53
54 0x17 => \&_index_byte, # fload
55 0x38 => \&_index_byte, # fstore
56
57 0xb4 => \&_index_short, # getfield
58 0xb2 => \&_index_short, # getstatic
59 0xa7 => \&_offset_short, # goto
60 0xc8 => \&_offset_long, # goto_w
61
62 0xa5 => \&_offset_short, # if_acmpeq
63 0xa6 => \&_offset_short, # if_acmpne
64 0x9f => \&_offset_short, # if_icmpeq
65 0xa0 => \&_offset_short, # if_icmpne
66 0xa1 => \&_offset_short, # if_icmplt
67 0xa2 => \&_offset_short, # if_icmpge
68 0xa3 => \&_offset_short, # if_icmpgt
69 0xa4 => \&_offset_short, # if_icmple
70 0x99 => \&_offset_short, # ifeq
71 0x9a => \&_offset_short, # ifne
72 0x9b => \&_offset_short, # iflt
73 0x9c => \&_offset_short, # ifge
74 0x9d => \&_offset_short, # ifgt
75 0x9e => \&_offset_short, # ifle
76 0xc7 => \&_offset_short, # ifnotnull
77 0xc6 => \&_offset_short, # ifnull
78 0x84 => \&_index_byte_const, # iinc
79 0x15 => \&_index_byte, # iload
80 0xc1 => \&_index_short, # instanceof
81 0xb9 => \&_index_short_count_0, # invokeinterface
82 0xb7 => \&_index_short, # invokespecial
83 0xb8 => \&_index_short, # invokestatic
84 0xb6 => \&_index_short, # invokevirtual
85 0x36 => \&_index_byte, # istore
86
87 0xa8 => \&_offset_short, # jsr
88 0xc9 => \&_offset_long, # jsr_w
89
90 0x12 => \&_index_byte, # ldc
91 0x13 => \&_index_short, # ldc_w
92 0x14 => \&_index_short, # ldc2_w
93 0x16 => \&_index_short, # lload
94 0 ▶ 0xab => sub { # lookupswitch
Subroutine __ANON__: not called CC=3 SCAR=24.8
95 0 my ($ops, $ix) = @_;
96 0 my $pad = 3 - ($$ix - 1) % 4;
97 0 ▶ if ($pad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad) 0 0 0
98 0 splice @$ops, 0, $pad;
99 0 $$ix += $pad;
100 };
101
102 # default offset
103 0 my ($d1, $d2, $d3, $d4) = splice @$ops, 0, 4;
104 0 $$ix += 4;
105 0 my $default = ($d1 << 24 | $d2 << 16 | $d3 << 8 | $d4);
106  
107 # number of case <int>:
108 0 my ($n1, $n2, $n3, $n4) = splice @$ops, 0, 4;
109 0 $$ix += 4;
110 0 my $case_no = ($d1 << 24 | $d2 << 16 | $d3 << 8 | $d4);
111
112 0 my @pairs;
113 0 ▶ if ($case_no) {
Branch: 1 branch
BranchTrueFalseTotal
if ($case_no) 0 0 0
114 0 my ($i1, $i2, $i3, $i4, $o1, $o2, $o3, $o4) = splice @$ops, 0, 8;
115 0 $$ix += 8;
116 0 push @pairs, ($i1 << 24 | $i2 << 16 | $i3 << 8 | $i4), ($o1 << 24 | $o2 << 16 | $o3 << 8 | $o4);
117 }
118
119 0 return ($default, @pairs);
120 },
121 0x37 => \&_index_byte, # lstore
122
123 0xc5 => \&_index_short_count, # multianewarray
124
125 0xbb => \&_index_short, # new
126 0xbc => \&_byte, # newarray
127
128 0xb5 => \&_index_short, # putfield
129 0xb3 => \&_index_short, # putstatic
130
131 0xa9 => \&_index_byte, # ret
132
133 0x11 => \&_short, # sipush
134  
135 0 ▶ 0xaa => sub { # tableswitch
Subroutine __ANON__: not called CC=3 SCAR=24.8
136 0 my ($ops, $ix) = @_;
137 0 my $pad = 3 - ($$ix - 1) % 4;
138 0 ▶ if ($pad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad) 0 0 0
139 0 splice @$ops, 0, $pad;
140 0 $$ix += $pad;
141 };
142
143 # default offset
144 0 my ($d1, $d2, $d3, $d4) = splice @$ops, 0, 4;
145 0 $$ix += 4;
146 0 my $default = ($d1 << 24 | $d2 << 16 | $d3 << 8 | $d4);
147  
148 # low <int>:
149 0 my ($l1, $l2, $l3, $l4) = splice @$ops, 0, 4;
150 0 $$ix += 4;
151 0 my $low = ($l1 << 24 | $l2 << 16 | $l3 << 8 | $l4);
152  
153 0 my ($h1, $h2, $h3, $h4) = splice @$ops, 0, 4;
154 0 $$ix += 4;
155 0 my $high = ($h1 << 24 | $h2 << 16 | $h3 << 8 | $h4);
156
157 0 my $jump_offsets = $high - $low + 1;
158 0 my @jump_offsets;
159 0 ▶ if ($jump_offsets) {
Branch: 1 branch
BranchTrueFalseTotal
if ($jump_offsets) 0 0 0
160 0 my ($o1, $o2, $o3, $o4) = splice @$ops, 0, 4;
161 0 $$ix += 4;
162 0 push @jump_offsets, ($o1 << 24 | $o2 << 16 | $o3 << 8 | $o4);
163 }
164
165 0 return ($default, $low, $high, @jump_offsets);
166 },
167
168 0 ▶ 0xc4 => sub { # wide
Subroutine __ANON__: not called CC=3 SCAR=24.8
169 0 my ($ops, $ix) = @_;
170
171 0 my $op = shift @$ops;
172 0 $$ix++;
173
174 0 ▶ if ($op == 0x84) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op == 132) { } 0 0 0
elsif (&any(sub { $_ == $op; } , 21, 54, 23, 56, 25, 58, 22, 55, 24, 57, 169)) { } 0 0 0
175 0 my ($i1, $i2, $c1, $c2) = splice @$ops, 0, 4;
176 0 $$ix += 4;
177 0 return ($op, $i1 << 8 | $i2, $c1 << 8 | $c2);
178 }
179 0 ▶ elsif (any { $_ == $op } (0x15, 0x36, 0x17, 0x38, 0x19, 0x3a, 0x16, 0x37, 0x18, 0x39, 0xa9)) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 0 my ($i1, $i2) = splice @$ops, 0, 2;
181 0 $$ix += 2;
182 0 return ($op, $i1 << 8 | $i2);
183 }
184 else {
185 0 die "Bytecode stream error"
186 }
187 }
188 1 );
189
190 1 ▶sub read {
Subroutine read: called CC=3 SCAR=11.0
Pod undocumented
191 1 my ($pkg, $bytecode) = @_;
192
193 1 my @bytecode = unpack("C*", $bytecode);
194 1 my @ops;
195 1 my $ix = 0;
196 1 ▶ while (@bytecode) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bytecode) 2 1 3
197 2 my $opcode = shift @bytecode;
198 2 my $pc = $ix++;
199 2 my $transformer = $Op_transformation{$opcode};
200 2 ▶ my @args = defined $transformer ? $transformer->(\@bytecode, \$ix) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $transformer ? : 1 1 2
201 2 push @ops, [$opcode, @args], (undef) x ($ix - 1 - $pc);
202 }
203
204 1 return \@ops;
205 }
206  
207 1 1;
208 __END__
« PJVM/Runtime/Class.pm ↑ Summary PJVM/Class.pm »