blib/lib/SIRTX/VM/Opcode.pm untested

filter: Statementstmt 0.0% 0 / 576 Branchbran 0.0% 0 / 314 Conditioncond 0.0% 0 / 331 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 22 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 1257 CC 150 SCAR 76.4
CC
150
Cov
0%
from_template
4556.0
as_text
1260.0
read
272.0
CRAP 2078.9
1 # Copyright (c) 2025-2026 Philipp Schafft
2  
3 # licensed under Artistic License 2.0 (see LICENSE file)
4  
5 # ABSTRACT: module for single SIRTX VM opcodes
6  
7  
8 package SIRTX::VM::Opcode;
9  
10 use v5.16;
11 use strict;
12 use warnings;
13  
14 use Carp;
15 use Scalar::Util qw(looks_like_number);
16  
17 use SIRTX::VM::RegisterFile;
18 use SIRTX::VM::Assembler;
19  
20 use parent 'Data::Identifier::Interface::Userdata';
21  
22 our $VERSION = v0.12;
23  
24 my %_die_raen = (code => 0, P => 7, codeX => 0, S => 2, T => 4+1);
25  
26 my %_raes_to_raen = (
27 NONE => 0,
28 NOENT => 2,
29 NOSYS => 6,
30 NOTSUP => 7,
31 NOMEM => 12,
32 INVAL => 13,
33 FAULT => 18,
34 IO => 19,
35 NODATA => 25,
36 NOSPC => 38,
37 TYPEMM => 39,
38 RO => 45,
39 ILLSEQ => 56,
40 BADEXEC => 79,
41 BADFH => 83,
42 );
43  
44 our %_logicals_to_sni = (
45 sni => 10,
46 sid => 115,
47 raen => 116,
48 chat0w => 118,
49 uuid => 119,
50 uri => 121,
51 asciicp => 122,
52 oid => 120,
53 wd => 123,
54 logical => 129,
55 false => 189,
56 true => 190,
57 ac => 298,
58 accumulator => 298,
59 );
60 my %_sni_to_logicals = reverse %_logicals_to_sni;
61  
62 my %_logicals_to_sid = (
63 asi => 1,
64 tagname => 3,
65 SEEK_SET => 34,
66 SEEK_CUR => 35,
67 SEEK_END => 36,
68 backwards => 43,
69 forwards => 44,
70 black => 61,
71 white => 62,
72 grey => 63,
73 red => 119,
74 green => 120,
75 blue => 121,
76 cyan => 122,
77 magenta => 123,
78 yellow => 124,
79 orange => 125,
80 gtin => 160,
81 left => 192,
82 right => 193,
83 up => 194,
84 down => 195,
85 north => 208,
86 east => 209,
87 south => 210,
88 west => 211,
89 );
90  
91 my %_compare_flags = (
92 with => 0x0000, # no-op
93 icase => 0x0001,
94 asciz => 0x0008,
95 nulls_distinct => 0x0080,
96 nulls_equal => 0x0040,
97 nulls_first => 0x0010,
98 nulls_last => 0x0020,
99 prefix => 0x0002,
100 suffix => 0x0004,
101 seekback_end => 0x0400,
102 seekback_start => 0x0200,
103 subject => 0x0100,
104 );
105  
106 my %_logicals = (
107 (map {$_ => 'sni:'.$_logicals_to_sni{$_}} keys %_logicals_to_sni),
108 (map {$_ => 'sid:'.$_logicals_to_sid{$_}} keys %_logicals_to_sid),
109 );
110  
111 my @_simple_0 = ();
112 my @_simple_1 = (reg => 'P');
113 my @_simple_2 = (reg => 'P', reg => 'T');
114 my @_simple_3 = (reg => 'P', reg => 'S', reg => 'T');
115  
116 my %_simple_opcodes = (
117 noop => [\@_simple_0 => {first => 0, second => 0, T => 0}],
118 magic => [\@_simple_0 => {first => 0, codeX => 0, S => 0, T => 4+3, extra => "VM\r\n\xc0\n"}],
119 autodie => [\@_simple_0 => {code => 0, P => 7, codeX => 0, S => 2, T => 4+0}],
120 data_start_marker => [\@_simple_0 => {code => 0, P => 6, codeX => 0, S => 0, T => 0}],
121  
122 filesize => [[int_half => 'extra[]'] => {code => 0, P => 1, codeX => 0, S => 0, T => 0+1}],
123 section_pointer => [[int_half => 'extra[]'] => {code => 0, P => 1, codeX => 0, S => 1, T => 0+1}],
124 minimum_handles => [[int_half => 'extra[]'] => {code => 0, P => 1, codeX => 0, S => 2, T => 0+1}],
125 minimum_memory => [[int_half => 'extra[]'] => {code => 0, P => 1, codeX => 0, S => 3, T => 0+1}],
126 text_boundary => [[int_half => 'extra[]'] => {code => 0, P => 1, codeX => 0, S => 4, T => 0+1}],
127 load_boundary => [[int_half => 'extra[]'] => {code => 0, P => 1, codeX => 0, S => 5, T => 0+1}],
128  
129 rjump => [[int => 'extra[]'] => {code => 0, P => 7, codeX => 0, S => 4, T => 0+1}],
130  
131 unref => [\@_simple_1 => {code => 0, codeX => 1, S => 0, T => 0+0}],
132 rewind => [\@_simple_1 => {code => 0, codeX => 1, S => 1, T => 0+0}],
133 die => [\@_simple_1 => {code => 0, codeX => 1, S => 2, T => 4+0}],
134 exit => [\@_simple_1 => {code => 0, codeX => 1, S => 3, T => 0+0}],
135 #jump => {code => 0, codeX => 1, S => 4, T => 0+0},
136 rcall => [[int => 'extra[]'] => {code => 0, codeX => 1, S => 5, T => 0+1}],
137 trcall => [[int => 'extra[]'] => {code => 0, codeX => 1, S => 5, T => 4+1}],
138 open_context => [\@_simple_1 => {code => 1, codeX => 1, S => 7, T => 0+0}],
139  
140 replace => [\@_simple_2 => {code => 0, codeX => 2, S => 0}],
141 move => [\@_simple_2 => {code => 0, codeX => 2, S => 1}],
142 seek => [\@_simple_2 => {code => 0, codeX => 2, S => 2}],
143 tell => [\@_simple_2 => {code => 0, codeX => 2, S => 3}],
144 transfer => [\@_simple_2 => {code => 0, codeX => 2, S => 4}],
145  
146 return => [\@_simple_0 => {code => 0, P => 7, codeX => 0, S => 2, T => 0+0},
147 \@_simple_1 => {code => 0, codeX => 1, S => 2, T => 0+0}],
148  
149 contents => [\@_simple_2 => {code => 1, codeX => 2, S => 1}],
150 die => [['raen:' => 'extra[]'] => \%_die_raen],
151 substr => [[reg => 'P', reg => 'T', int => 'extra[]', int => 'extra[]'] => {code => 1, codeX => 2, S => (0+3)}],
152 open_function => [[reg => 'P', int_rel4 => 'extra[]'] => {code => 1, codeX => 1, S => 6, T => 0+1}],
153 relations => [[reg => 'P', reg => 'S', reg => 'T', undef => 'undef'] => {code => 4, codeX => 0},
154 [reg => 'P', undef => 'undef', reg => 'T', reg => 'S'] => {code => 4, codeX => 1}],
155 metadata => [\@_simple_3 => {code => 4, codeX => 2}],
156 control => [[reg => 'P', reg => 'T'] => {code => 1, codeX => 2, S => 0+0},
157 [reg => 'P', 'sni:' => 'extra[]', reg => 'T'] => {code => 1, codeX => 2, S => 4+0},
158 [reg => 'P', 'sni:' => 'extra[]'] => {code => 1, codeX => 1, S => 0, T => 0+1},
159 [reg => 'P', 'sni:' => 'extra[]', 'sni:' => 'extra[]'] => {code => 1, codeX => 1, S => 0, T => 0+2},
160 [reg => 'P', 'sni:' => 'extra[]', int => 'extra[]'] => {code => 1, codeX => 1, S => 0, T => 4+2},
161 [reg => 'P', reg => 'S', reg => 'T'] => {code => 1, codeX => 3}],
162  
163 open => [[reg => 'P', 'sni:' => 'extra[]'] => {code => 0, codeX => 1, S => 6, T => 0+1},
164 [reg => 'P', '"ns"' => 'undef', int => 'extra[]'] => {code => 0, codeX => 1, S => 6, T => 4+1},
165 [reg => 'P', '"ns"' => 'undef', int => 'extra[]', int => 'extra[]'] => {code => 0, codeX => 1, S => 6, T => 4+2}],
166 byte_transfer => [[reg => 'P', reg => 'T', int => 'extra[]', autodie => 'false'] => {code => 1, codeX => 2, S => 4+2},
167 [reg => 'P', reg => 'T', int => 'extra[]', autodie => 'true'] => {code => 1, codeX => 2, S => 4+1}],
168 call => [[reg => 'P', reg => 'T', autodie => 'false'] => {code => 0, codeX => 2, S => 5},
169 [reg => 'P', reg => 'T', autodie => 'true'] => {code => 0, codeX => 2, S => 6}],
170 jump => [[int_rel4 => 'extra[]'] => {code => 0, P => 7, codeX => 0, S => 4, T => 0+1}],
171  
172 '.not_implemented' => [\@_simple_0 => {%_die_raen, extra => [$_raes_to_raen{NOSYS}]}],
173 '.bug' => [\@_simple_0 => {%_die_raen, extra => [$_raes_to_raen{ILLSEQ}]}],
174 );
175 $_simple_opcodes{nop} = $_simple_opcodes{noop}; # alias
176  
177 my @_opcode_to_text;
178  
179 my %_synthetic = (
180 open => [
181 [reg => 1, undef => 'undef'] => ['unref', \1],
182 [reg => 1, '"false"' => 'undef'] => ['open', \1, 'sni:189'],
183 [reg => 1, '"true"' => 'undef'] => ['open', \1, 'sni:190'],
184 ],
185 add => [['"out"' => 'undef', reg => 1, reg => 2] => ['control', \1, 'sni:81', \2]],
186 sub => [['"out"' => 'undef', reg => 1, reg => 2] => ['control', \1, 'sni:82', \2]],
187 div => [['"out"' => 'undef', reg => 1, reg => 2] => ['control', \1, 'sni:83', \2]],
188 mod => [['"out"' => 'undef', reg => 1, reg => 2] => ['control', \1, 'sni:84', \2]],
189 jump => [[reg => 1] => ['seek', 'program_text', \1]],
190 return => [[undef => 'undef'] => ['return']],
191 control => [[any => 1, any => 2, any => 3, '"arg"' => 'undef'] => ['control', \1, \2, \3]],
192 push => [[reg => 1, reg => 2] => ['control', \1, 'sni:180', \2]],
193 pop => [
194 ['"out"' => 1, reg => 2] => ['control', \2, 'sni:181'],
195 ['"undef"' => 1, reg => 2] => ['control', \2, 'sni:181'], # alias
196 ],
197 setvalue => [[reg => 1, reg => 2, '"arg"' => 3] => ['control', \1, 'sni:102', \2, \3]],
198 getvalue => [['"out"' => 1, reg => 2, reg => 3] => ['control', \2, 'sni:101', \3]],
199 read_character => [['"out"' => 'undef', reg => 1] => ['control', \1, 'sni:282']],
200 write_character => [
201 [reg => 1, reg => 2] => ['control', \1, 'sni:283', \2],
202 [reg => 1, int => 2] => ['control', \1, 'sni:283', \2],
203 ],
204 read_be8 => [['"out"' => 'undef', reg => 1] => ['control', \1, 'sni:143', '8']],
205 read_be16 => [['"out"' => 'undef', reg => 1] => ['control', \1, 'sni:143', '16']],
206 );
207  
208  
209 sub new {
210 my ($pkg, %opts) = @_;
211 my $self = bless({}, $pkg);
212  
213 foreach my $key (qw(first second code codeX ST P S T size pos)) {
214 my $val = delete $opts{$key} // next;
215 unless (looks_like_number($val)) {
216 croak 'Invalid argument: '.$key.' is not a number';
217 }
218 $self->{$key} = int($val);
219 }
220  
221 $self->{extra} = delete $opts{extra};
222  
223 # TODO: Implement more checks here.
224  
225 croak 'Stray options passed' if scalar keys %opts;
226  
227 return $self;
228 }
229  
230  
231 sub from_template {
232 my ($pkg, %opts) = @_;
233 my $parts = delete $opts{parts};
234 my $asm = delete $opts{assembler};
235 my $size = delete $opts{size};
236 my $line = delete $opts{line};
237 my $out = delete $opts{out};
238 my $autodie = delete $opts{autodie};
239 my ($cmd, @args) = @{$parts};
240  
241 croak 'Stray options passed' if scalar keys %opts;
242  
243 if (defined(my $entry = $_synthetic{$cmd})) {
244 outer:
245 for (my $i = 0; $i < scalar(@{$entry}); $i += 2) {
246 my @argmap = @{$entry->[$i]};
247 my %updates;
248  
249 next unless (scalar(@args)*2) == scalar(@argmap);
250  
251 for (my $j = 0; ($j*2) < scalar(@argmap); $j++) {
252 my $type = $argmap[$j*2 + 0];
253 my $dst = $argmap[$j*2 + 1];
254 my $val = $args[$j];
255  
256 if ($val =~ /^raes:(.+)$/) {
257 if (defined(my $raen = $_raes_to_raen{uc($1)})) {
258 $val = 'raen:'.$raen;
259 }
260 } elsif ($val =~ /^logical:(.+)$/) {
261 if (defined(my $logical = $_logicals{$1})) {
262 $val = $logical;
263 }
264 }
265  
266 if ($type =~ /^".+"$/) {
267 next outer if $val ne $asm->_parse_string($type);
268 } elsif ($type eq 'any') {
269 # no-op
270 } else {
271 next outer if $asm->_get_value_type($val) ne $type;
272 }
273  
274 if ($dst eq 'undef') {
275 # ignore this value
276 } else {
277 $updates{$dst} = $val;
278 }
279 }
280  
281 ($cmd, @args) = map {ref ? $updates{${$_}} : $_} @{$entry->[$i+1]};
282 }
283 }
284  
285 # replace logics:
286 foreach my $arg (@args) {
287 if ($arg =~ /^logical:(.+)$/) {
288 if (defined(my $logical = $_logicals{$1})) {
289 $arg = $logical;
290 }
291 }
292 }
293  
294 if (defined(my $entry = $_simple_opcodes{$cmd})) {
295 outer:
296 for (my $i = 0; $i < scalar(@{$entry}); $i += 2) {
297 my @argmap = @{$entry->[$i]};
298 my %updates;
299 my $last_data;
300 my $reset_autodie;
301  
302 if (scalar(@argmap) >= 2 && $argmap[-2] eq 'autodie') {
303 next unless ((scalar(@args) + 1)*2) == scalar(@argmap);
304 } else {
305 next unless ((scalar(@args) + 0)*2) == scalar(@argmap);
306 }
307  
308 for (my $j = 0; ($j*2) < scalar(@argmap); $j++) {
309 my $type = $argmap[$j*2 + 0];
310 my $dst = $argmap[$j*2 + 1];
311 my $val = $args[$j];
312 my $data;
313 my $mods = '';
314  
315 if ($type =~ s/_(.+)$//) {
316 $mods = $1;
317 }
318  
319 if ($type ne 'autodie' && $val =~ /^raes:(.+)$/) {
320 if (defined(my $raen = $_raes_to_raen{uc($1)})) {
321 $val = 'raen:'.$raen;
322 }
323 }
324  
325 if ($type =~ /^".+"$/) {
326 next outer if $val ne $asm->_parse_string($type);
327 } elsif ($type eq 'autodie') {
328 next outer if !defined($autodie);
329 } else {
330 next outer if $asm->_get_value_type($val) ne $type;
331 }
332  
333 if ($type eq 'reg') {
334 $data = $asm->_force_mapped($val);
335 } elsif ($type eq 'int') {
336 if ($mods eq 'rel4') {
337 my $org = $out->tell + 4;
338 $data = $asm->_parse_int($val, $org) - $org;
339 if ($data & 1) {
340 croak sprintf('Bad offset: line %s: offset %i', $line, $data);
341 }
342 $data /= 2;
343 } elsif ($mods eq 'half') {
344 $data = $asm->_parse_int($val, $last_data);
345 if ($data & 1) {
346 croak sprintf('Bad value: line %s: value %i', $line, $data);
347 }
348 $data /= 2;
349 } else {
350 $data = $asm->_parse_int($val, $last_data);
351 }
352 } elsif ($type =~ /^[a-z]+:$/) {
353 (undef, $data) = $asm->_parse_id($val);
354 } elsif ($type eq 'undef') {
355 $data = undef;
356 } elsif ($type =~ /^".+"$/) {
357 $data = $val;
358 } elsif ($type eq 'autodie') {
359 $reset_autodie = $asm->_parse_bool($dst);
360 next outer if (${$autodie} xor $reset_autodie);
361 next;
362 } else {
363 croak 'BUG: Unsupported type: '.$type;
364 next outer;
365 }
366  
367 if ($dst eq 'extra[]') {
368 push(@{$updates{extra} //= []}, $data);
369 } elsif ($dst eq 'undef') {
370 # ignore this value
371 } else {
372 $updates{$dst} = $data;
373 }
374  
375 $last_data = $data;
376 }
377  
378 ${$autodie} = undef if defined($autodie) && $reset_autodie;
379 return $pkg->new(%{$entry->[$i+1]}, %updates, size => $size);
380 }
381 }
382  
383 if ($cmd eq 'compare' && scalar(@args) >= 3 && $args[0] eq 'out') {
384 my @flags = @args[3..$#args];
385 my $flags = 0;
386  
387 foreach my $flag (@flags) {
388 if (defined(my $v = $_compare_flags{$flag})) {
389 $flags |= $v;
390 } else {
391 croak sprintf('Unsupported compare flag: line %s: flag %s', $line, $flag);
392 }
393 }
394  
395 return $pkg->new(code => 3, P => $asm->_force_mapped($args[1]), codeX => 2, S => 0, T => $asm->_force_mapped($args[2]), extra => [$flags]);
396 } elsif ($cmd eq 'open' && scalar(@args) == 2 && $asm->_get_value_type($args[0]) eq 'reg' && $asm->_get_value_type($args[1]) eq 'int') {
397 my $num = $asm->_parse_int($args[1]);
398 if (($num >= 0 && $num <= 7) && (!defined($size) || $size != 4)) {
399 return $pkg->new(code => 0, P => $asm->_force_mapped($args[0]), codeX => 2, S => 7, T => $num);
400 } else {
401 return $pkg->new(code => 0, P => $asm->_force_mapped($args[0]), codeX => 1, S => 7, T => 0+1, extra => [$num]);
402 }
403 } elsif ($cmd eq 'open' && scalar(@args) == 2 && $asm->_get_value_type($args[0]) eq 'reg' && $asm->_get_value_type($args[1]) =~ /:$/) {
404 my ($type, $num) = $asm->_parse_id($args[1]);
405 my $sni = $asm->_type_to_sni($type);
406 return $pkg->new(code => 0, P => $asm->_force_mapped($args[0]), codeX => 1, S => 6, T => 0+2, extra => [$sni, $num], size => $size);
407 } elsif ($cmd eq 'jump' && scalar(@args) >= 3 && $asm->_get_value_type($args[0]) eq 'int' && ($args[1] eq 'if' || $args[1] eq 'unless')) {
408 my $org = $out->tell + 4;
409 my $extra = $asm->_parse_int($args[0], $org) - $org;
410 my @cond = @args[2..$#args];
411 my $P = 0;
412 my $S = 0;
413 my $T = 0;
414  
415 if ($extra & 1) {
416 croak sprintf('Bad offset: line %s: offset %i', $line, $extra);
417 }
418 $extra /= 2;
419  
420 if ($args[1] eq 'if') {
421 # no-op
422 } elsif ($args[1] eq 'unless') {
423 $P |= 0x1;
424 }
425  
426 while (scalar(@cond) >= 3) {
427 my $reg = shift(@cond);
428 my $op = shift(@cond);
429 my $val = shift(@cond);
430  
431 croak sprintf('Unsupported jump syntax: line %s: register %s', $line, $reg) unless $reg eq 'out';
432  
433 if ($op eq 'is') {
434 if ($val eq 'valid') {
435 $S |= 0x1;
436 } elsif ($val eq 'true') {
437 $S |= 0x2;
438 } elsif ($val eq 'notfine' || $val eq 'bad' || $val eq 'dog' || $val eq 'hotdog') {
439 $S |= 0x4;
440 } else {
441 croak sprintf('Unsupported jump syntax: line %s: is-value %s', $line, $val);
442 }
443 } elsif (($op eq '<' && $val eq '0') || ($op eq '<=' && $val eq '-1')) {
444 $T |= 0x1;
445 } elsif ($op eq '==' && $val eq '0') {
446 $T |= 0x2;
447 } elsif (($op eq '>' && $val eq '0') || ($op eq '>=' && $val =~ /^\+?1$/)) {
448 $T |= 0x4;
449 } else {
450 croak sprintf('Unsupported jump syntax: line %s: operator/value %s %s', $line, $op, $val);
451 }
452  
453 shift(@cond) if scalar(@cond) && $cond[0] eq 'or';
454 }
455  
456 if (scalar @cond) {
457 croak sprintf('Unsupported jump syntax: line %s: condition %s', $line, join(' ', @cond));
458 }
459  
460 return $pkg->new(code => 3, P => $P, codeX => 0, S => $S, T => $T, extra => [$extra]);
461 } elsif ($cmd eq 'noop' && scalar(@args) == 1 && $asm->_get_value_type($args[0]) eq 'string') {
462 my $string = $asm->_parse_string($args[0]);
463 my $l = length($string);
464  
465 if ($l > 6 || ($l & 1)) {
466 croak sprintf('Unsupported noop with data of invalid length: line %s: length %u', $line, $l);
467 }
468  
469 return $pkg->new(first => 0, codeX => 0, S => 0, T => ($l/2), extra => $string);
470 } elsif ($cmd eq 'execution_info_flags') {
471 my $flags = 0;
472  
473 foreach my $flag (@args) {
474 if ($flag eq 'resources_only') {
475 $flags |= 1<<15;
476 } elsif ($flag eq 'single_load') {
477 $flags |= 1<<14;
478 } elsif ($flag eq 'multi_session') {
479 $flags |= (1<<13) | (1<<14);
480 } else {
481 croak sprintf('Unsupported/unknown execution_info_flags flag: line %s: flag %s', $line, $flag);
482 }
483 }
484  
485 return $pkg->new(code => 0, P => 3, codeX => 0, S => 0, T => 0+1, extra => [$flags]);
486 }
487  
488 croak 'Unsupported template';
489 }
490  
491  
492 sub read {
493 my ($pkg, $fh, @opts) = @_;
494 my $data;
495 my $pos;
496  
497 croak 'Stray options passed' if scalar @opts;
498  
499 $pos = $fh->tell;
500  
501 if (defined($pos) && ($pos & 1)) {
502 croak 'Invalid aligned opcode read';
503 }
504  
505 croak 'Cannot read opcode' unless $fh->read($data, 2) == 2;
506  
507 {
508 my ($first, $second) = unpack('CC', $data);
509 my $code = ($first & 0370) >> 3;
510 my $P = ($first & 0007) >> 0;
511 my $codeX = ($second & 0300) >> 6;
512 my $S = ($second & 0070) >> 3;
513 my $T = ($second & 0007) >> 0;
514  
515 my $registers;
516 my $extra_len;
517 my $extra;
518  
519 if ($code <= 3) {
520 $registers = $codeX;
521 } elsif ($code == 4) {
522 $registers = 3;
523 } else {
524 croak sprintf('Unsupported/invalid opcode: 0x%02x%02x: Number of registers is undefined/unknown', $first, $second);
525 }
526  
527 if ($code <= 1 && $codeX <= 1) {
528 $extra_len = $T & 3;
529 } elsif ($code <= 1 && $codeX == 3) {
530 $extra_len = 0;
531 } elsif ($code == 1 && $codeX == 2 && ($S & 4) == 4) {
532 $extra_len = 1;
533 } elsif ($code == 1 && $codeX == 2 && $S == (0 + 3)) {
534 $extra_len = 2;
535 } elsif ($code == 1 && $codeX == 2 && $S == (0 + 1)) {
536 $extra_len = 0;
537 } elsif ($code == 0 && $codeX == 2) {
538 $extra_len = 0;
539 } elsif ($code == 3) {
540 $extra_len = 1;
541 } elsif ($code == 4) {
542 $extra_len = 0;
543 } else {
544 croak sprintf('Unsupported/invalid opcode: 0x%02x%02x: Size of extra is undefined/unknown', $first, $second);
545 }
546  
547 #warn sprintf('registers: %u, extra: %u', $registers, $extra_len);
548  
549 if ($extra_len) {
550 $extra_len *= 2;
551  
552 croak 'Cannot read extra' unless $fh->read($extra, $extra_len) == $extra_len;
553 }
554  
555 return $pkg->new(first => $first, second => $second, code => $code, codeX => $codeX, P => $P, S => $S, T => $T, extra => $extra, pos => $pos);
556 }
557  
558 croak 'Cannot parse opcode';
559 }
560  
561  
562 sub write {
563 my ($self, $out, @opts) = @_;
564 my $required;
565 my $size;
566  
567 croak 'Stray options passed' if scalar @opts;
568  
569 $required = $self->required_size;
570  
571 if (defined $self->{size}) {
572 $size = $self->{size};
573 } else {
574 $size = $required;
575 }
576  
577 if ($required > $size) {
578 croak sprintf('Opcode does not fit in allocated size: required %u, have %u', $required, $size);
579 } elsif ($required < $size) {
580 my $diff = $size - $required;
581 croak 'Opcode padding alignment error' if $diff & 1;
582 print $out chr(0) x $diff;
583 }
584  
585 print $out chr($self->{first}), chr($self->{second});
586  
587 if (ref $self->{extra}) {
588 print $out pack('n*', @{$self->{extra}});
589 } elsif (defined $self->{extra}) {
590 $self->{extra} .= chr(0) if length($self->{extra}) & 1;
591 print $out $self->{extra};
592 }
593 }
594  
595  
596 sub required_alignment {
597 my ($self, @opts) = @_;
598  
599 croak 'Stray options passed' if scalar @opts;
600  
601 return 2;
602 }
603  
604  
605 sub new_alignment {
606 my ($self, @opts) = @_;
607  
608 croak 'Stray options passed' if scalar @opts;
609  
610 return 2;
611 }
612  
613  
614 sub required_size {
615 my ($self, @opts) = @_;
616 my $required;
617  
618 croak 'Stray options passed' if scalar @opts;
619  
620 $self->{first} //= ($self->{code} << 3) | $self->{P};
621 $self->{ST} //= ($self->{S} << 3) | $self->{T} unless defined $self->{second};
622 $self->{second} //= ($self->{codeX} << 6) | $self->{ST};
623  
624 $required = 2;
625 if (ref $self->{extra}) {
626 $required += 2 * scalar(@{$self->{extra}});
627 } elsif (defined $self->{extra}) {
628 my $l = length($self->{extra});
629 $l++ if $l & 1;
630 $required += $l;
631 }
632  
633 return $required;
634 }
635  
636  
637 sub is_return {
638 my ($self, @opts) = @_;
639  
640 croak 'Stray options passed' if scalar @opts;
641  
642 $self->required_size;
643  
644 if ($self->{first} == 0x07 && $self->{codeX} == 0 && $self->{S} == 2) {
645 my $T = $self->{T};
646 return $T == 0 || $T == (4+1);
647 } elsif ($self->{code} == 0 && $self->{codeX} == 1) {
648 my $S = $self->{S};
649 return $S == 2 || $S == 3;
650 }
651  
652 return undef;
653 }
654  
655  
656 sub is_autodie {
657 my ($self, @opts) = @_;
658  
659 croak 'Stray options passed' if scalar @opts;
660  
661 $self->required_size;
662  
663 return $self->{first} == 0x07 && $self->{second} == 0x14;
664 }
665  
666  
667 sub is_end_of_text {
668 my ($self, @opts) = @_;
669  
670 croak 'Stray options passed' if scalar @opts;
671  
672 $self->required_size;
673  
674 if ($self->{first} == 0x06 && $self->{codeX} == 0) {
675 my $S = $self->{S};
676 return $S == 0 || $S == 7;
677 } elsif ($self->{first} == 0x00 && $self->{codeX} == 0 && $self->{S} == 3 && ($self->{T} & 4)) {
678 return 1;
679 }
680  
681 return undef;
682 }
683  
684  
685 sub as_text {
686 my ($self, @opts) = @_;
687 my $name = eval {$self->_name};
688 my $opcode_to_text = $self->{opcode_to_text};
689 my $extra = $self->{extra} // '';
690 my $command;
691  
692 croak 'Stray options passed' if scalar @opts;
693  
694 if (defined $opcode_to_text) {
695 if (scalar(@{$opcode_to_text->{argmap}}) == 0) {
696 $command = $name;
697 } else {
698 my @argmap = @{$opcode_to_text->{argmap}};
699 my @extra = unpack('n*', $extra);
700  
701 $command = $name;
702  
703 for (my $j = 0; ($j*2) < scalar(@argmap); $j++) {
704 my $type = $argmap[$j*2 + 0];
705 my $dst = $argmap[$j*2 + 1];
706  
707 if ($type eq 'reg') {
708 $command .= sprintf(' r%u', $self->{$dst});
709 } elsif ($type =~ /^(?:sni|raen):$/ && $dst eq 'extra[]') {
710 $command .= sprintf(' %s%u', $type, shift(@extra));
711 } elsif ($type eq 'int_half' && $dst eq 'extra[]') {
712 $command .= sprintf(' %u', shift(@extra)*2);
713 } elsif ($type eq 'int_rel4' && $dst eq 'extra[]') {
714 $command .= sprintf(' %u', shift(@extra)*2 + $self->{pos} + 4);
715 } elsif ($type eq 'int' && $dst eq 'extra[]') {
716 $command .= sprintf(' %u', shift(@extra));
717 } elsif ($type eq 'undef' && $dst eq 'undef') {
718 $command .= ' undef';
719 } elsif ($type =~ /^"([0-9a-zA-Z]+)"$/ && $dst eq 'undef') {
720 $command .= ' '.$1;
721 } elsif ($type eq 'autodie' && $dst eq 'true') {
722 $command =~ s/^(\S+)(\s?)/$1!$2/;
723 } elsif ($type eq 'autodie' && $dst eq 'false') {
724 $command =~ s/^(\S+)(\s?)/$1?$2/;
725 } else {
726 $command = undef;
727 last;
728 }
729 }
730 }
731 } else {
732 my $first = $self->{first};
733 my $second = $self->{second};
734 my $code = ($first & 0370) >> 3;
735 my $P = ($first & 0007) >> 0;
736 my $codeX = ($second & 0300) >> 6;
737 my $S = ($second & 0070) >> 3;
738 my $T = ($second & 0007) >> 0;
739  
740 if ($code == 0 && $codeX == 3 && defined(my $regname = SIRTX::VM::RegisterFile->_physical_name_by_number($second & 0077))) {
741 $command = sprintf('map r%u %s', $P, $regname);
742 } elsif ($first == 0 && $codeX == 0 && $S > 0 && $T == 0) {
743 $command = sprintf('.endsection ; %s', $SIRTX::VM::Assembler::_header_ids_rev{$S} // '???');
744 } elsif ($first == 0 && $codeX == 0 && $S > 0 && $T == 4) {
745 $command = sprintf('.section %s', $SIRTX::VM::Assembler::_header_ids_rev{$S} // '???');
746 } elsif ($first == 0 && $codeX == 0 && $S > 0 && ($T & 4)) {
747 $command = sprintf('.section %s %s', $SIRTX::VM::Assembler::_header_ids_rev{$S} // '???', _escape_text($extra));
748 } elsif ($first == 0 && $codeX == 0 && $S == 0 && $T > 0 && $T < 4) {
749 $command = sprintf('noop %s', _escape_text($extra));
750 } elsif ($code == 0 && $codeX == 2 && $S == 7) {
751 $command = sprintf('open r%u %u', $P, $T);
752 } elsif ($code == 0 && $codeX == 1 && $S == 6 && $T == (0+2)) {
753 my ($sni, $id) = unpack('nn', $extra);
754 $sni = $_sni_to_logicals{$sni} // $sni;
755 $command = sprintf('open r%u %s:%u', $P, $sni, $id);
756 } elsif ($code == 0 && $codeX == 1 && $S == 7 && $T == (0+1)) {
757 $command = sprintf('open r%u %u', $P, unpack('n', $extra));
758 } elsif ($code == 3 && $codeX == 2 && $S == 0) {
759 my $flags = unpack('n', $extra);
760 $command = sprintf('compare out, r%u r%i', $P, $T);
761 $command .= ' with' if $flags;
762 foreach my $key (sort keys %_compare_flags) {
763 $command .= ' '.$key if $flags & $_compare_flags{$key};
764 }
765 } elsif ($code == 3 && $codeX == 0) {
766 $command = sprintf('jump %u', unpack('n', $extra)*2 + $self->{pos} + 4);
767 if ($P & 0x01) {
768 $command .= ' unless';
769 } else {
770 $command .= ' if';
771 }
772  
773 $command .= ' out is valid or' if $S & 0x01;
774 $command .= ' out is true or' if $S & 0x02;
775 $command .= ' out is notfine or' if $S & 0x04;
776  
777 $command .= ' out < 0 or' if $T & 0x01;
778 $command .= ' out == 0 or' if $T & 0x02;
779 $command .= ' out > 0 or' if $T & 0x04;
780  
781 $command =~ s/ or$//;
782 }
783 }
784  
785 unless (defined $command) {
786 $command = sprintf('.byte 0x%02x 0x%02x', $self->{first}, $self->{second});
787 $command .= ', '.join(' ', map {sprintf('0x%02x', ord)} split(//, $self->{extra})) if defined $self->{extra};
788 }
789  
790 {
791 my $pos = defined($self->{pos}) ? sprintf('%04x', $self->{pos}) : '????';
792 $command = sprintf('%-48s ; at 0x%s: code=%2u, P=%u, codeX=%u, S=%u, T=%u, extra=[%s]',
793 $command, $pos,
794 $self->{code}, $self->{P},
795 $self->{codeX}, $self->{S}, $self->{T},
796 join(', ', map {sprintf('0x%04x', $_)} unpack('n*', $extra)),
797 );
798 }
799  
800 return $command;
801 }
802  
803 # ---- Private helpers ----
804 sub _name { # TODO: Private for now, might be exposed later on
805 my ($self, @opts) = @_;
806  
807 croak 'Stray options passed' if scalar @opts;
808  
809 return $self->{opcode_to_text}{name} if defined $self->{opcode_to_text};
810  
811 {
812 my $code;
813  
814 $self->required_size;
815  
816 $code = ($self->{first} << 8) | $self->{second};
817  
818 foreach my $entry (@_opcode_to_text) {
819 if ($entry->{masked_code} == ($code & $entry->{mask})) {
820 $self->{opcode_to_text} = $entry;
821 return $entry->{name};
822 }
823 }
824 }
825  
826 croak 'Unknown/no name for opcode';
827 }
828  
829 sub _extra {
830 my ($self, @opts) = @_;
831 my %res = (type => 'opcode');
832  
833 croak 'Stray options passed' if scalar @opts;
834  
835 $self->required_size;
836  
837 if ($self->{first} == 0x01 && $self->{codeX} == 0 && $self->{S} == 1) {
838 my $T = $self->{T};
839  
840 if ($T == 1) {
841 $res{start_offsets} = [unpack('n', $self->{extra})*2];
842 }
843 } elsif ($self->{first} == 0x06 && $self->{codeX} == 0 && $self->{S} == 7 && defined($self->{pos})) {
844 my $T = $self->{T};
845 my $length;
846  
847 if ($T == 1) {
848 $length = unpack('n', $self->{extra})*2;
849 } elsif ($T == 2) {
850 $length = unpack('N', $self->{extra})*2;
851 }
852  
853 $res{start_offsets} = [$length + $self->{pos} + 4];
854 $res{length} = $length;
855 $res{type} = 'chunk';
856 }
857  
858 return %res;
859 }
860  
861 foreach my $key (keys %_simple_opcodes) {
862 my $entry = $_simple_opcodes{$key};
863  
864 next if $key eq 'nop'; # skip alias.
865  
866 for (my $i = 0; $i < scalar(@{$entry}); $i += 2) {
867 my @argmap = @{$entry->[$i]};
868 my $opcode = $entry->[$i + 1];
869 my $first = $opcode->{first};
870 my $second = $opcode->{second};
871 my $code = $opcode->{code};
872 my $codeX = $opcode->{codeX};
873 my $ST = $opcode->{ST};
874 my $P = $opcode->{P};
875 my $S = $opcode->{S};
876 my $T = $opcode->{T};
877 my $mask = 0;
878  
879 for (my $j = 0; ($j*2) < scalar(@argmap); $j++) {
880 my $type = $argmap[$j*2 + 0];
881 my $dst = $argmap[$j*2 + 1];
882  
883 if ($dst eq 'P') {
884 $P = 0;
885 $mask |= 0x0700;
886 } elsif ($dst eq 'S') {
887 $S = 0;
888 $mask |= 0070;
889 } elsif ($dst eq 'T') {
890 $T = 0;
891 $mask |= 0007;
892 }
893 }
894  
895 $first //= ($code << 3) | $P if defined($code) && defined($P);
896 $ST //= ($S << 3) | $T if defined($S) && defined($T);
897 $second //= ($codeX << 6) | $ST if defined($codeX) && defined($ST);
898  
899 $mask ^= 0xFFFF;
900  
901 if (defined($first) && defined($second)) {
902 my $code = ($first << 8) | $second;
903  
904 push(@_opcode_to_text, {
905 masked_code => $code,
906 masked_first => $first,
907 masked_second => $second,
908 mask => $mask,
909 name => $key,
910 argmap => $entry->[$i+0],
911 opcode => $entry->[$i+1],
912 });
913 }
914 }
915 }
916  
917 sub _escape_text {
918 my ($text) = @_;
919 return '"'.join('',
920 map {
921 my $x = ord;
922 $x >= 0x20 && $x <= 0x7E && $x != 0x5C ? $_ : sprintf('\\x%02x', $x)
923 } split //, $text
924 ).'"';
925 }
926  
927 #warn sprintf('# %04x / %04x => %s', $_->{masked_code}, $_->{mask}, $_->{name}) foreach @_opcode_to_text;
928  
929 1;
930  
931 __END__