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