VM/Disassembler.pm

filter: Statementstmt 18.3% 25 / 136 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 61.5% 8 / 13 Podpod 100% 2 / 2 totaltotal 14.6% 35 / 239 CC 40 SCAR 52.5
CC
40
Cov
12%
_run_text
420.0
_run_data
90.0
new
72.0
CRAP 191.5
« VM/Opcode.pm ↑ Summary VM/RegisterFile.pm »
1 # Copyright (c) 2025-2026 Philipp Schafft
2  
3 # licensed under Artistic License 2.0 (see LICENSE file)
4  
5 # ABSTRACT: module for assembling SIRTX VM code
6  
7  
8 package SIRTX::VM::Disassembler;
9  
10 1 ▶use v5.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Fcntl qw(SEEK_SET SEEK_END SEEK_CUR);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use SIRTX::VM::RegisterFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use SIRTX::VM::Opcode;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use parent 'Data::Identifier::Interface::Userdata';
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 our $VERSION = v0.13;
22  
23  
24 0 ▶sub new {
Subroutine new: not called CC=8 SCAR=42.8
Pod documented
25 0 my ($pkg, %opts) = @_;
26 my $self = bless({
27 0 max_data => delete($opts{max_data}),
28 }, $pkg);
29  
30 {
31 0 my $fh = delete $opts{in};
32 0 ▶ croak 'No input given' unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 0 0
33  
34 0 ▶ unless (ref $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh) 0 0 0
35 0 ▶ open(my $x, '<', $fh) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $x, "<", $fh or die $! 0 0 0
36 0 $fh = $x;
37 }
38  
39 0 $fh->binmode;
40 0 $self->{in} = $fh;
41 }
42  
43 {
44 0 my $fh = delete $opts{out};
45 0 ▶ croak 'No output given' unless defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 0 0
46  
47 0 ▶ unless (ref $fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $fh) 0 0 0
48 0 ▶ open(my $x, '>', $fh) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $x, ">", $fh or die $! 0 0 0
49 0 $fh = $x;
50 }
51  
52 0 $fh->binmode;
53 0 $fh->binmode(':utf8');
54 0 $self->{out} = $fh;
55 }
56  
57 0 ▶ croak 'Stray options passed' if scalar keys %opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %opts 0 0 0
58  
59 0 return $self;
60 }
61  
62  
63 0 ▶sub run {
Subroutine run: not called CC=5 SCAR=34.0
Pod documented
64 0 my ($self, @opts) = @_;
65 0 my $in = $self->{in};
66 0 my $out = $self->{out};
67 0 my $in_length = $self->_in_length;
68  
69 0 $self->{starts} = {0 => undef};
70  
71 0 ▶ croak 'Stray options passed' if scalar @opts;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @opts 0 0 0
72  
73 0 while (1) {
74 0 ▶ my $pos = $in->tell // croak 'Cannot tell on input';
Condition0%0/2
$in->tell // croak("Cannot tell on input")
l!l
00
MC/DC0%0/1
$in->tell // croak("Cannot tell on input")
$in->tell
Truth table0%0/2
A
A= $in->tell
Aresult
11
00
75  
76 0 ▶ last if $pos >= $in_length;
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= $in_length 0 0 0
77  
78 0 ▶ if (exists $self->{starts}{$pos}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'starts'}{$pos}) { } 0 0 0
79 0 $self->_run_text;
80 } else {
81 0 $self->_run_data;
82 }
83 }
84 }
85  
86 # ---- Private helpers ----
87  
88 0 ▶sub _in_length {
Subroutine _in_length: not called CC=2 SCAR=17.9
89 0 my ($self) = @_;
90  
91 0 ▶ return $self->{in_length} //= do {
Condition0%0/3
$self->{"in_length"} //= do { my $fh = $self->{"in"}; my $l; $fh->seek(0, 2); $l = $fh->tell; $fh->seek(0, 0); $l }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"in_length"} //= do { my $fh = $self->{"in"}; my $l; $fh->seek(0, 2); $l = $fh->tell; $fh->seek(0, 0); $l }
$self->{"in_length"} do { my $fh = $self->{"in"}; my $l; $fh->seek(0, 2); $l = $fh->tell; $fh->seek(0, 0); $l }
Truth table0%0/3
A //= B
A= $self->{"in_length"}B= do { my $fh = $self->{"in"}; my $l; $fh->seek(0, 2); $l = $fh->tell; $fh->seek(0, 0); $l }
ABresult
1X1
011
000
92 0 my $fh = $self->{in};
93 0 my $l;
94  
95 0 $fh->seek(0, SEEK_END);
96  
97 0 $l = $fh->tell;
98  
99 0 $fh->seek(0, SEEK_SET);
100  
101 0 $l;
102 };
103 }
104  
105 0 ▶sub _run_text {
Subroutine _run_text: not called CC=20 SCAR=60.4
106 0 my ($self) = @_;
107 0 my $in = $self->{in};
108 0 my $out = $self->{out};
109 0 my $in_length = $self->_in_length;
110  
111 0 ▶ while ($in->tell < $in_length && defined(my $opcode = SIRTX::VM::Opcode->read($in))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in->tell < $in_length and defined(my $opcode = "SIRTX::VM::Opcode"->read($in))) 0 0 0
Condition0%0/3
$in->tell < $in_length and defined(my $opcode = "SIRTX::VM::Opcode"->read($in))
!ll&&!rl&&r
000
MC/DC0%0/2
$in->tell < $in_length and defined(my $opcode = "SIRTX::VM::Opcode"->read($in))
$in->tell < $in_length defined(my $opcode = "SIRTX::VM::Opcode"->read($in))
Truth table0%0/3
A and B
A= $in->tell < $in_lengthB= defined(my $opcode = "SIRTX::VM::Opcode"->read($in))
ABresult
0X0
100
111
112 0 my %extra = $opcode->_extra;
113  
114 0 ▶ $self->{starts}{$_} //= undef for @{$extra{start_offsets}//[]};
Condition0%0/4
$extra{"start_offsets"} // []
l!l
00
$self->{"starts"}{$_} //= undef
l!l
00
MC/DC0%0/1
$extra{"start_offsets"} // []
$extra{"start_offsets"}
MC/DC0%0/1
$self->{"starts"}{$_} //= undef
$self->{"starts"}{$_}
Truth table0%0/2
A
A= $extra{"start_offsets"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{"starts"}{$_}
Aresult
11
00
115  
116  
117 0 ▶ if ($extra{type} eq 'chunk') {
Branch: 1 branch
BranchTrueFalseTotal
if ($extra{'type'} eq 'chunk') { } 0 0 0
118 0 my $length = $extra{length};
119  
120 0 ▶ if ($length >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length >= 4) 0 0 0
121 0 my $command = '.chunk';
122 0 my $data_length = $length - 4;
123 0 my ($flags, $type);
124 0 my $chunk_identifier;
125 0 my $raw;
126  
127 0 ▶ $in->read($raw, 4) == 4 or croak 'Cannot read input';
Branch: 1 branch
BranchTrueFalseTotal
$in->read($raw, 4) == 4 or croak("Cannot read input") 0 0 0
128  
129 0 ($flags, $type) = unpack('nn', $raw);
130  
131 {
132 0 my $identifier_type = $flags & ((1<<15)|(1<<14));
133 0 ▶ if ($identifier_type == ((1<<15)|(1<<14))) {
Branch: 4 branches
BranchTrueFalseTotal
if ($identifier_type == 49152) { } 0 0 0
elsif ($identifier_type == 32768) { } 0 0 0
elsif ($identifier_type == 16384) { } 0 0 0
elsif ($identifier_type == 0) { } 0 0 0
134 0 croak 'Unsupported identifier type: user defined';
135 } elsif ($identifier_type == ((1<<15)|(0<<14))) {
136 0 $command .= ' of ~'.$type;
137 } elsif ($identifier_type == ((0<<15)|(1<<14))) {
138 0 $command .= ' of sid:'.$type;
139 } elsif ($identifier_type == ((0<<15)|(0<<14))) {
140 0 $command .= ' of sni:'.$type;
141 }
142 }
143  
144 0 ▶ if ($flags & (1<<7)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 128) 0 0 0
145 0 $command .= ' standalone';
146 }
147  
148 0 ▶ if ($flags & (1<<1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 2) 0 0 0
149 0 ▶ $in->read($raw, 2) == 2 or croak 'Cannot read input';
Branch: 1 branch
BranchTrueFalseTotal
$in->read($raw, 2) == 2 or croak("Cannot read input") 0 0 0
150 0 $chunk_identifier = unpack('n', $raw);
151 0 $data_length -= 2;
152 0 $command .= ' as ~'.$chunk_identifier;
153 }
154  
155 0 ▶ if ($flags & (1<<0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 1) 0 0 0
156 0 $data_length -= 1;
157 }
158  
159 #warn sprintf('; ### flags: %04x type: %04x, chunk identifier: %s, data_length: %u', $flags, $type, $chunk_identifier // '<undef>', $data_length);
160 0 $out->say($command);
161 0 $self->_run_data($data_length);
162  
163 0 ▶ if ($flags & (1<<0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 1) 0 0 0
164 0 my $pos = $in->tell;
165 0 ▶ $in->read($raw, 1) == 1 or croak 'Cannot read input';
Branch: 1 branch
BranchTrueFalseTotal
$in->read($raw, 1) == 1 or croak("Cannot read input") 0 0 0
166 0 $out->say(sprintf(' ; at 0x%04x: Padding: 0x%02x', $pos, ord $raw));
167 }
168 0 $out->say('.endchunk');
169 }
170 } else {
171 0 $out->say($opcode->as_text);
172 0 ▶ if ($opcode->is_end_of_text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opcode->is_end_of_text) 0 0 0
173 0 $out->say('; End of text');
174 0 last;
175 }
176 }
177 }
178 }
179  
180 0 ▶sub _run_data {
Subroutine _run_data: not called CC=9 SCAR=45.0
181 0 my ($self, $todo) = @_;
182 0 my $in = $self->{in};
183 0 my $out = $self->{out};
184 0 ▶ my $pos = $in->tell // croak 'Cannot tell on input';
Condition0%0/2
$in->tell // croak("Cannot tell on input")
l!l
00
MC/DC0%0/1
$in->tell // croak("Cannot tell on input")
$in->tell
Truth table0%0/2
A
A= $in->tell
Aresult
11
00
185 0 my ($next_code) = sort { $a <=> $b } grep { $_ >= $pos } keys(%{$self->{starts}}), $self->_in_length;
186  
187 0 ▶ $todo //= $next_code - $pos;
Condition0%0/2
$todo //= $next_code - $pos
l!l
00
MC/DC0%0/1
$todo //= $next_code - $pos
$todo
Truth table0%0/2
A
A= $todo
Aresult
11
00
188  
189 0 ▶ if (defined($self->{max_data}) && $todo > $self->{max_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"max_data"} and $todo > $self->{"max_data"}) 0 0 0
Condition0%0/3
defined $self->{"max_data"} and $todo > $self->{"max_data"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"max_data"} and $todo > $self->{"max_data"}
defined $self->{"max_data"} $todo > $self->{"max_data"}
Truth table0%0/3
A and B
A= defined $self->{"max_data"}B= $todo > $self->{"max_data"}
ABresult
0X0
100
111
190 0 $out->say(sprintf('; %u bytes of data skipped', $todo));
191 0 $in->seek($todo, SEEK_CUR);
192 0 return;
193 }
194  
195 0 $out->say(sprintf('; %u bytes of data follow', $todo));
196  
197 0 ▶ while ($todo > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($todo > 0) 0 0 0
198 0 ▶ my $step = $todo > 8 ? 8 : $todo;
Branch: 1 branch
BranchTrueFalseTotal
$todo > 8 ? : 0 0 0
199 0 my $line = '.byte';
200 0 my $rendered;
201  
202 0 ▶ $in->read(my $raw, $step) == $step or croak 'Error reading data';
Branch: 1 branch
BranchTrueFalseTotal
$in->read(my $raw, $step) == $step or croak("Error reading data") 0 0 0
203  
204 0 $line .= sprintf(' 0x%02x', ord) foreach split //, $raw;
205  
206 0 $rendered = $raw =~ tr/\x21-\x7E/./rc;
207  
208 0 $line = sprintf('%-48s ; at 0x%04x: | %-8s |',
209 $line, $pos,
210 $rendered,
211 );
212  
213 0 $out->say($line);
214  
215 0 $todo -= $step;
216 0 $pos += $step;
217 }
218 }
219  
220 1 1;
221  
222 __END__
« VM/Opcode.pm ↑ Summary VM/RegisterFile.pm »