lib/Language/Zcode/Parser/Opcode.pm

filter: Statementstmt 94.5% 190 / 201 Branchbran 89.0% 98 / 110 Conditioncond 73.8% 31 / 42 MC/DCmcdc 42.8% 9 / 21 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 7 totaltotal 86.6% 345 / 398 CC 72 SCAR 33.2
CC
72
Cov
90%
parse_command
45.4
decode_text
17.4
num_to_var
7.7
CRAP 27.5
« lib/Language/Zcode/Runtime/Input.pm ↑ Summary lib/Language/Zcode/Parser/Perl.pm »
1 package Language::Zcode::Parser::Opcode;
2
3 =head1 NAME
4
5 Language::Zcode::Parser::Opcode - parse one opcode
6
7 =head1 DESCRIPTION
8
9 This package parses one opcode. It uses the syntax described in the
10 Z-spec's table, 14.1. It parses the opcode and its arguments into
11 a hash:
12
13 =over 4
14
15 =item opcode
16
17 Name of the opcode
18
19 =item opcode_address
20
21 Byte address of the opcode (in hex)
22
23 =item args
24
25 Arguments to a subroutine call
26
27 =item negate_jump
28
29 Negates the condition of a branch instruction
30
31 =item jump_return
32
33 Return true/false if branch condition is met, instead of jumping
34
35 =back
36
37 Other keys are (almost) identical to the arg names in the spec.
38 For example, "je a b ?(label)" yields keys a, b, and label.
39 For example2, word-index is changed to word_index to make my life easier.
40
41 =cut
42
43 # Program Counter
44 2 our $PC;
45
46 134 ▶sub parse_sub_header {
Subroutine parse_sub_header: called CC=3 SCAR=12.0
Pod undocumented
47 134 $PC = shift;
48 134 my $nl = eat_byte();
49 134 ▶ die "Bad number of locals $nl" if $nl > 15;
Branch: 1 branch
BranchTrueFalseTotal
if $nl > 15 0 134 134
50 # skip local variable values
51 134 my @locals = (0) x $nl;
52 134 ▶ if ($Language::Zcode::Util::Constants{version} <= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Language::Zcode::Util::Constants{"version"} <= 4) 0 134 134
53 0 @locals = map { &eat_word } 1..$nl
54 }
55 134 return @locals;
56 }
57
58 { # Extra scoping brace: doing all this just once instead of each time
59 # (out of thousands) that we call this sub speeds up by several times!
60
61 ##############3###### Many, many constants here...
62 2 ▶ use constant OP_UNKNOWN => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶ use constant OP_0OP => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶ use constant OP_1OP => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 ▶ use constant OP_2OP => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶ use constant OP_VAR => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 ▶ use constant OP_EXT => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
68
69 # two bits to store operand type: large or small constant, var, or none
70 2 ▶ use constant OP_TYPE_LARGE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 2 ▶ use constant OP_TYPE_SMALL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 2 ▶ use constant OP_TYPE_VAR => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 2 ▶ use constant OP_TYPE_DONE => 3; # Also, all remaining ops must also be '11'
Subroutine BEGIN: called CC=1 SCAR=0.0
74
75 2 my @TYPE_LABELS;
76 2 $TYPE_LABELS[OP_0OP] = "0OP";
77 2 $TYPE_LABELS[OP_1OP] = "1OP";
78 2 $TYPE_LABELS[OP_2OP] = "2OP";
79 2 $TYPE_LABELS[OP_VAR] = "VAR";
80 2 $TYPE_LABELS[OP_EXT] = "EXT";
81
82 # OPCODE TABLES AND INFORM ASSEMBLY SYNTAX TAKEN FROM Z-SPEC
83 # (Minor changes to text, like changing - to _)
84 # Note: if an opcode is only in certain versions, we have a hash.
85 # Keys are 3 for version 3, 3- for versions 3 and over,
86 # 1-4 for versions 1 through 4, and 5:7:8 for versions 5,7,8
87 # (Last one necessary cuz 6 has fancy opcodes that 7 and 8 don't have.)
88 # Zero-operand opcodes 0OP
89 2 my @zero_ops = (
90 'rtrue', # 0
91 'rfalse', # 1
92 # As far as I can tell, print & print_ret are always b2/b3 (0OP)
93 # so we don't need to read their strings.
94 'print (literal_string)', # 2
95 'print_ret (literal_string)', # 3
96 'nop', # 4
97 # Version 1, version 4
98 { "1-3" => 'save ?(label)', # 5
99 "4" => 'save -> (result)'
100 }, # illegal in v5+
101 { "1-3" => 'restore ?(label)', # 6
102 "4" => 'restore -> (result)'
103 }, # illegal in v5+
104 'restart', # 7
105 'ret_popped', # 8
106 { "1-4" => 'pop',
107 "5-" => 'catch -> (result)', # 9
108 },
109 'quit', # a
110 'new_line', # b
111 { "3" => 'show_status'}, # c (v3 only)
112 { "3-" => 'verify ?(label)'}, # d
113 { "5-" => 'extended'}, # e [byte 1 of extended opcode]
114 { "5-" => 'piracy ?(label)'}, # f
115 );
116
117 # One-operand opcodes 1OP
118 2 my @one_ops = (
119 'jz a ?(label)', # 0x00
120 'get_sibling object -> (result) ?(label)', # 0x01
121 'get_child object -> (result) ?(label)', # 0x02
122 'get_parent object -> (result)', # 0x03
123 'get_prop_len property_address -> (result)', # 0x04
124 'inc (variable)', # 0x05
125 'dec (variable)', # 0x06
126 'print_addr byte_address_of_string', # 0x07
127 { "4-" => 'call_1s routine -> (result)'}, # 0x08
128 'remove_obj object', # 0x09
129 'print_obj object', # 0x0a
130 'ret value', # 0x0b
131 'jump ?(label)', # 0x0c
132 'print_paddr packed_address_of_string', # 0x0d
133 'load (variable) -> (result)', # 0x0e
134 { "1-4" => 'not value -> (result)', # 0x0f
135 "5-" => 'call_1n routine',
136 },
137 );
138
139 # Two-operand opcodes 2OP
140 2 my @two_ops = (
141 '', # 0x00
142 # XXX Spec says "je a b ?(label)" but je may take up to four (?) test values
143 # (The thing tested and up to 3 to test against)
144 'je a (1-3args) ?(label)', # 0x01
145 'jl a b ?(label)', # 0x02
146 'jg a b ?(label)', # 0x03
147 'dec_chk (variable) value ?(label)', # 0x04
148 'inc_chk (variable) value ?(label)', # 0x05
149 'jin obj1 obj2 ?(label)', # 0x06
150 'test bitmap flags ?(label)', # 0x07
151 'or a b -> (result)', # 0x08
152 'and a b -> (result)', # 0x09
153 'test_attr object attribute ?(label)', # 0x0a
154 'set_attr object attribute', # 0x0b
155 'clear_attr object attribute', # 0x0c
156 'store (variable) value', # 0x0d
157 'insert_obj object destination', # 0x0e
158 'loadw array word_index -> (result)', # 0x0f
159 'loadb array byte_index -> (result)', # 0x10
160 'get_prop object property -> (result)', # 0x11
161 'get_prop_addr object property -> (result)', # 0x12
162 'get_next_prop object property -> (result)', # 0x13
163 'add a b -> (result)', # 0x14
164 'sub a b -> (result)', # 0x15
165 'mul a b -> (result)', # 0x16
166 'div a b -> (result)', # 0x17
167 'mod a b -> (result)', # 0x18
168 { "4-" => 'call_2s routine arg1 -> (result)'}, # 0x19
169 { "5-" => 'call_2n routine arg1'}, # 0x1a
170 { "5:7:8" => 'set_colour foreground background',# 0x1b
171 "6" => 'set_colour foreground background window',
172 },
173 { "5-" => 'throw value stack_frame'}, # 0x1c
174 '', # 0x1d
175 '', # 0x1e
176 '', # 0x1f
177 );
178
179 # Variable-operand opcodes VAR
180 2 my @var_ops = (
181 # Versions 1-3 use "call" instead of "call_vs". But aren't they the same?
182 #'call routine (0-3args) -> (result)',
183 'call_vs routine (0-3args) -> (result)', # 0x00
184 'storew array word_index value', # 0x01
185 'storeb array byte_index value', # 0x02
186 'put_prop object property value', # 0x03
187 # (Inform calls them sread/aread, but they're really all read
188 { "1-3" => 'read text parse',
189 "4" => 'read text parse time routine',
190 "5-" => 'read text parse time routine -> (result)', # 0x04
191 },
192 'print_char output_character_code', # 0x05
193 'print_num value', # 0x06
194 'random range -> (result)', # 0x07
195 'push value', # 0x08
196 { "1-5" => 'pull (variable)', # 0x08
197 "6" => 'pull stack -> (result)',
198 "7-9" => 'pull (variable)',
199 },
200 { "3-" => 'split_window lines'}, # 0x0a
201 { "3-" => 'set_window window'}, # 0x0b
202 { "4-" => 'call_vs2 routine (0-7args) -> (result)'}, # 0x0c
203 { "4-" => 'erase_window window'}, # 0x0d
204 # XXX translate_command will get different keys depending on version!
205 # I believe this is the only command for which this happens. All other
206 # commands you just get extra (possibly optional) args.
207 { "4:5:7:8:9" => 'erase_line value', # 0x0e
208 "6" => 'erase_line pixels',
209 },
210 { "4:5:7:8:9" => 'set_cursor line column', # 0x0f
211 "6" => 'set_cursor line column window',
212 },
213
214 { "4-" => 'get_cursor array'}, # 0x10
215 { "4-" => 'set_text_style style'}, # 0x11
216 { "4-" => 'buffer_mode flag'}, # 0x12
217 { "3-4" => 'output_stream number ', # 0x13
218 "5:7:8" => 'output_stream number table',
219 "6" => 'output_stream number table width',
220 },
221 { "3-" => 'input_stream number'}, # 0x14
222 # Spec says defined in v5, first used in v3?!
223 { "3-" => 'sound_effect number effect volume routine'}, # 0x15
224 { "4-" => 'read_char 1 time routine -> (result)'}, # 0x16
225 { "4-" => 'scan_table x table len form -> (result)'}, # 0x17
226 { "5-" => 'not value -> (result)'}, # 0x18
227 { "5-" => 'call_vn routine (0-3args)'}, # 0x19
228 { "5-" => 'call_vn2 routine (0-7args)'}, # 0x1a
229 { "5-" => 'tokenise text parse dictionary flag'}, # 0x1b
230 { "5-" => 'encode_text zscii_text length from coded_text'}, # 0x1c
231 { "5-" => 'copy_table first second size'}, # 0x1d
232 { "5-" => 'print_table zscii_text width height skip'}, # 0x1e
233 # Bug in spec?! It doesn't list label
234 { "5-" => 'check_arg_count argument_number ?(label)'}, # 0x1f
235 );
236
237 # Extended opcodes EXT
238 2 my @ext_ops = (
239 # XXX "table bytes name" are optional. IF we get that many args,
240 # fill in those values, else we just get a result & do a normal save
241 { "5-" => 'save table bytes name -> (result)'}, # 0x00
242 { "5-" => 'restore table bytes name -> (result)'}, # 0x01
243 { "5-" => 'log_shift number places -> (result)'}, # 0x02
244 { "5-" => 'art_shift number places -> (result)'}, # 0x03
245 { "5-" => 'set_font font -> (result)'}, # 0x04
246 { "6" => 'draw_picture picture_number y x'}, # 0x05
247 { "6" => 'picture_data picture_number array ?(label)'}, # 0x06
248 { "6" => 'erase_picture picture_number y x'}, # 0x07
249 { "6" => 'set_margins left right window'}, # 0x08
250 { "5-" => 'save_undo -> (result)'}, # 0x09
251 { "5-" => 'restore_undo -> (result)'}, # 0x0a
252 { "5-" => 'print_unicode char_number'}, # 0x0b
253 { "5-" => 'check_unicode char_number -> (result)'}, # 0x0c
254 '', # 0x0d
255 '', # 0x0e
256 '', # 0x0f
257
258 { "6" => 'move_window window y x'}, # 0x10
259 { "6" => 'window_size window y x'}, # 0x11
260 { "6" => 'window_style window flags operation'}, # 0x12
261 { "6" => 'get_wind_prop window property_number -> (result)'}, # 0x13
262 { "6" => 'scroll_window window pixels'}, # 0x14
263 { "6" => 'pop_stack items stack'}, # 0x15
264 { "6" => 'read_mouse array'}, # 0x16
265 { "6" => 'mouse_window window'}, # 0x17
266 { "6" => 'push_stack value stack ?(label)'}, # 0x18
267 { "6" => 'put_wind_prop window property_number value'}, # 0x19
268 { "6" => 'print_form formatted_table'}, # 0x1a
269 { "6" => 'make_menu number table ?(label)'}, # 0x1b
270 { "6" => 'picture_table table'}, # 0x1c
271 );
272
273 2 my (@generic_opcodes);
274 2 $generic_opcodes[OP_0OP] = \@zero_ops;
275 2 $generic_opcodes[OP_1OP] = \@one_ops;
276 2 $generic_opcodes[OP_2OP] = \@two_ops;
277 2 $generic_opcodes[OP_VAR] = \@var_ops;
278 2 $generic_opcodes[OP_EXT] = \@ext_ops;
279
280 2959 ▶sub parse_command {
Subroutine parse_command: called CC=45 SCAR=38.2
Pod undocumented
281 # See ZMachine spec chapter 4
282
283 ##################### OK, finally ready to start the real sub
284 2959 my %parsed = ( "opcode_address" => $PC );
285 2959 my $z_version = $Language::Zcode::Util::Constants{version};
286
287 2959 my $opcode = &eat_byte();
288 2959 my $op_style = OP_UNKNOWN;
289 2959 my @operands = ();
290 2959 my $is_var_ops = 0;
291 2959 ▶ if (($opcode & 0x80) == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (($opcode & 128) == 0) { } 834 2125 2959
elsif ($opcode & 64) { } 1077 1048 2125
elsif ($opcode == 190 and $z_version >= 5) { } 16 1032 1048
292 # If top bit is zero: opcode is "long" format, which is always 2OP
293 # ME: Handle these first as they seem to be the most common.
294 # Next two bits give operand types for the two ops
295 # type is small constant (0) or variable number (1)
296 834 ▶ @operands = (load_operand($opcode&0x40 ? OP_TYPE_VAR : OP_TYPE_SMALL),
Branch: 2 branches
BranchTrueFalseTotal
$opcode & 64 ? : 252 582 834
$opcode & 32 ? : 206 628 834
297 load_operand($opcode&0x20 ? OP_TYPE_VAR : OP_TYPE_SMALL));
298 834 $opcode &= 0x1f; # last 5 bits
299 834 $op_style = OP_2OP;
300
301 } elsif ($opcode & 0x40) {
302 # top 2 bits are both 1: "variable" format opcode. Opcode in bottom 5
303 # bits. This may actually be a 2OP opcode...
304 1077 ▶ $op_style = $opcode & 0x20 ? OP_VAR : OP_2OP;
Branch: 1 branch
BranchTrueFalseTotal
$opcode & 32 ? : 827 250 1077
305 1077 $opcode &= 0x1f;
306 1077 $is_var_ops = 1; # load operands later
307
308 ▶ } elsif ($opcode == 0xbe && $z_version >= 5) {
Condition66%2/3
$opcode == 190 and $z_version >= 5
!ll&&!rl&&r
1032016
MC/DC50%1/2
$opcode == 190 and $z_version >= 5
$opcode == 190 $z_version >= 5
Truth table66%2/3
A and B
A= $opcode == 190B= $z_version >= 5
ABresult
0X0
100
111
309 # "extended" opcode
310 16 $opcode = &eat_byte();
311 16 $op_style = OP_EXT;
312 16 $is_var_ops = 1; # load operands below
313 } else {
314 # "short" format opcode: next two bits mean zero or 1 OP
315 1032 ▶ if (($opcode & 0x30) == 0x30) {
Branch: 1 branch
BranchTrueFalseTotal
if (($opcode & 48) == 48) { } 546 486 1032
316 546 $op_style = OP_0OP;
317 } else {
318 486 $op_style = OP_1OP;
319 486 my $optype = ($opcode & 0x30) >> 4;
320 486 push @operands, &load_operand($optype);
321 }
322 1032 $opcode &= 0x0f;
323 }
324
325 # Which command is it?
326 2959 ▶ my $syntax = $generic_opcodes[$op_style]->[$opcode]
Branch: 1 branch
BranchTrueFalseTotal
my $syntax = $generic_opcodes[$op_style][$opcode] or warn("Unknown opcode $TYPE_LABELS[$op_style] $opcode"), return 2959 0 2959
327 or warn("Unknown opcode $TYPE_LABELS[$op_style] $opcode"), return;
328 # Deal with version-dependent codes
329 2959 ▶ if (ref $syntax eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $syntax eq "HASH") 864 2095 2959
330 864 my %syn = %$syntax;
331 864 my $v = $z_version; # nickname for conciseness below
332 864 $syntax = "";
333 864 foreach my $range (keys %syn) {
334 985 ▶ if (($range =~ /^(\d+)$/ && $v == $1) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($range =~ /^(\d+)$/ and $v == $1 or $range =~ /^(\d+)-$/ and $v >= $1 or $range =~ /^(\d+)-(\d+)$/ and $v >= $1 and $v <= $2 or $range =~ /:/ and $range =~ /\b$v\b/) 864 121 985
Condition83%20/24
$range =~ /^(\d+)$/ and $v == $1 or $range =~ /^(\d+)-$/ and $v >= $1 or $range =~ /^(\d+)-(\d+)$/ and $v >= $1 and $v <= $2 or $range =~ /:/ and $range =~ /\b$v\b/
l!l&&r!l&&!r
84618121
$range =~ /^(\d+)$/ and $v == $1 or $range =~ /^(\d+)-$/ and $v >= $1 or $range =~ /^(\d+)-(\d+)$/ and $v >= $1 and $v <= $2
l!l&&r!l&&!r
80640139
$range =~ /^(\d+)$/ and $v == $1 or $range =~ /^(\d+)-$/ and $v >= $1
l!l&&r!l&&!r
0806179
$range =~ /^(\d+)$/ and $v == $1
!ll&&!rl&&r
958270
$range =~ /^(\d+)-$/ and $v >= $1
!ll&&!rl&&r
1790806
$range =~ /^(\d+)-(\d+)$/ and $v >= $1 and $v <= $2
!ll&&!rl&&r
627740
$range =~ /^(\d+)-(\d+)$/ and $v >= $1
!ll&&!rl&&r
4517117
$range =~ /:/ and $range =~ /\b$v\b/
!ll&&!rl&&r
121018
MC/DC33%3/9
$range =~ /^(\d+)$/ and $v == $1 or $range =~ /^(\d+)-$/ and $v >= $1 or $range =~ /^(\d+)-(\d+)$/ and $v >= $1 and $v <= $2 or $range =~ /:/ and $range =~ /\b$v\b/
$range =~ /^(\d+)$/ $v == $1 $range =~ /^(\d+)-$/ $v >= $1 $range =~ /^(\d+)-(\d+)$/ $v >= $1 $v <= $2 $range =~ /:/ $range =~ /\b$v\b/
Truth table13%6/43
A and B or C and D or E and F and G or H and I
A= $range =~ /^(\d+)$/B= $v == $1C= $range =~ /^(\d+)-$/D= $v >= $1E= $range =~ /^(\d+)-(\d+)$/F= $v >= $1G= $v <= $2H= $range =~ /:/I= $range =~ /\b$v\b/
ABCDEFGHIresult
11XXXXXXX1
0X11XXXXX1
1011XXXXX1
0X0X111XX1
0X10111XX1
100X111XX1
1010111XX1
0X0X0XX111
0X0X10X111
0X0X110111
0X100XX111
0X1010X111
0X10110111
100X0XX111
100X10X111
100X110111
10100XX111
101010X111
1010110111
0X0X0XX0X0
0X0X0XX100
0X0X10X0X0
0X0X10X100
0X0X1100X0
0X0X110100
0X100XX0X0
0X100XX100
0X1010X0X0
0X1010X100
0X101100X0
0X10110100
100X0XX0X0
100X0XX100
100X10X0X0
100X10X100
100X1100X0
100X110100
10100XX0X0
10100XX100
101010X0X0
101010X100
10101100X0
1010110100
335 ($range =~ /^(\d+)-$/ && $v >= $1) ||
336 ($range =~ /^(\d+)-(\d+)$/ && $v >= $1 && $v <= $2) ||
337 # One day there might be a version 10, and v1 shouldn't match...
338 ($range =~ /:/ && $range =~ /\b$v\b/))
339 {
340 864 $syntax = $syn{$range};
341 864 last;
342 }
343 }
344 864 ▶ if (!$syntax) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($syntax) 0 864 864
345 0 warn("opcode $TYPE_LABELS[$op_style] $opcode illegal for v$v");
346 0 return;
347 }
348 }
349 2959 my ($command, @keys) = split " ", $syntax;
350
351 # Read leftover ops for VAR opcodes
352 2959 my ($operand_types, $i);
353 2959 ▶ if ($is_var_ops) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_var_ops) 1093 1866 2959
354 # a VAR or EXT opcode with variable argument count.
355 # Load the arguments.
356 1093 ▶ if ($op_style == OP_VAR &&
Branch: 1 branch
BranchTrueFalseTotal
if ($op_style == 3 and $command =~ /^call_v[sn]2$/) { } 16 1077 1093
Condition100%3/3
$op_style == 3 and $command =~ /^call_v[sn]2$/
!ll&&!rl&&r
26681116
MC/DC100%2/2
$op_style == 3 and $command =~ /^call_v[sn]2$/
$op_style == 3 $command =~ /^call_v[sn]2$/
Truth table100%3/3
A and B
A= $op_style == 3B= $command =~ /^call_v[sn]2$/
ABresult
0X0
100
111
357 ($command =~ /^call_v[sn]2$/)) {
358 # 4.4.3.1: there may be two bytes of operand types, allowing
359 # for up to 8 arguments. This byte will always be present,
360 # though it does NOT have to be used...
361 16 $i = 14;
362 # start shift mask: target "leftmost" 2 bits
363 16 $operand_types = &eat_word();
364 } else {
365 # 4.4.3: one byte of operand types, up to 4 args.
366 1077 $i = 6;
367 1077 $operand_types = &eat_byte();
368 }
369 # printf STDERR "%s: ", $operand_types;
370 1093 ▶ for (; $i >=0; $i -= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 3654 360 4014
371 3654 my $optype = ($operand_types >> $i) & 0x03;
372 # print STDERR "$optype\n";
373 3654 ▶ if (defined (my $op = &load_operand($optype))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $op = &load_operand($optype))) { } 2921 733 3654
374 2921 push @operands, $op;
375 } else {
376 733 last; # done getting args
377 }
378 }
379 # print STDERR "\n";
380 }
381
382 # Read any remaining args if necessary.
383 # Also, assign operands to operand names, creating %parsed
384 2959 $parsed{opcode} = $command;
385 # print "$command @operands\n";
386 2959 for my $key (@keys) {
387 5352 ▶ next if $key eq "->";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "->" 245 5107 5352
388
389 # Read branch/result args, which are not counted in the Z-code
390 # argument count bits (VAR/1OP etc.).
391 5107 ▶ if ($key eq "?(label)") {
Branch: 3 branches
BranchTrueFalseTotal
if ($key eq '?(label)') { } 466 4641 5107
elsif ($key eq '(result)') { } 245 4396 4641
elsif ($key eq '(literal_string)') { } 366 4030 4396
392 # XXX HACK! jump counts the ?(label) as an arg and
393 # reads it as a SIXTEEN-bit offset
394 # XXX Change jump's arg in @one_ops?
395 466 my $offset;
396 466 ▶ if ($command eq "jump") {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq 'jump') { } 126 340 466
397 126 $offset = shift @operands;
398 # I *think* this doesn't happen
399 126 ▶ if ($offset =~ /\D/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset =~ /\D/) 0 126 126
400 0 die "jump opcode takes a variable offset at $PC\n";
401 }
402 126 ▶ $offset -= (1<<16) if $offset & (1<<15); # SIGNED offset
Branch: 1 branch
BranchTrueFalseTotal
if $offset & 32768 14 112 126
403 # negate_jump doesn't exist
404 } else {
405 340 my $arg = eat_byte();
406 340 $parsed{"negate_jump"} = ($arg & 0x80) == 0;
407 340 $offset = $arg & 0x3f; # offset is 0-63 OR...
408 340 ▶ if (!($arg & 0x40)) { # 14-bit signed offset
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg & 64) 96 244 340
409 96 $offset <<= 8;
410 96 $offset |= eat_byte();
411 96 ▶ $offset -= (1<<14) if $offset & (1<<13); # SIGNED offset
Branch: 1 branch
BranchTrueFalseTotal
if $offset & 8192 6 90 96
412 }
413 }
414 # Offset of 1 or 0 really means return
415 466 ▶ if ($offset == 1 || $offset == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset == 1 or $offset == 0) { } 8 458 466
Condition100%3/3
$offset == 1 or $offset == 0
l!l&&r!l&&!r
44458
MC/DC100%2/2
$offset == 1 or $offset == 0
$offset == 1 $offset == 0
Truth table100%3/3
A or B
A= $offset == 1B= $offset == 0
ABresult
1X1
011
000
416 8 $parsed{"jump_return"} = $offset;
417 8 $parsed{"label"} = "";
418 } else {
419 # 4.7.2: "Address after branch data + Offset - 2"
420 # (-2 seems to apply to jump also, maybe because you read
421 # a two-byte word, then apply offset)
422 458 my $destination = $PC + $offset - 2;
423 # printf("addr: %s, PC: %x, offset: %s%x, dest: %d\n",
424 # $parsed{opcode_address}, $PC, ($offset<0 && "-"),
425 # (abs$offset), $destination);
426 458 $parsed{"label"} = $destination;
427 }
428 466 next;
429 } elsif ($key eq "(result)") {
430 # Store the raw number, which we use for call stack's store_var,
431 # as well as the variable name, like local2.
432 245 $parsed{"result_num"} = eat_byte();
433 245 $parsed{"result"} = num_to_var($parsed{"result_num"});
434 245 next;
435 } elsif ($key eq "(literal_string)") {
436 # Make this just a print_addr
437 366 $parsed{literal_string} = $PC;
438 # For debugging purposes, get the string to print
439 366 my $q = decode_text(); $q =~ s/\n/^/g;
440 366 $parsed{print_string} = $q;
441 }
442
443 # At this point, we've theoretically read all possible args.
444 # So if @operands is empty, there's an optional arg that wasn't given
445 4396 ▶ next unless @operands;
Branch: 1 branch
BranchTrueFalseTotal
unless @operands 411 3985 4396
446
447 # Now handle all the other arg types
448 3985 ▶ if ($key =~ /arg[s1]/) { # call_* has 'args', call_2* has 'arg1'
Branch: 3 branches
BranchTrueFalseTotal
if ($key =~ /arg[s1]/) { } 724 3261 3985
elsif ($key eq 'routine') { } 749 2512 3261
elsif ($key eq '(variable)') { } 812 1700 2512
449 # args are already sitting in operands
450 724 $parsed{"args"} = \@operands;
451 } elsif ($key eq "routine") {
452 749 $parsed{$key} = shift @operands;
453 } elsif ($key eq "(variable)") {
454 # Spec: "passed by reference"
455 812 $parsed{"variable"} = num_to_var(shift @operands);
456 } else {
457 1700 $parsed{$key} = shift @operands;
458 }
459 }
460
461 # Calls need to store the address of the command AFTER the call,
462 # which is where the Z-machine resumes after finishing the call.
463 # (For saves, quetzal stores the byte of the store variable in the @save)
464 2959 ▶ if ($command =~ /^call/) { $parsed{"next_pc"} = $PC }
Branch: 2 branches
BranchTrueFalseTotal
if ($command =~ /^call/) { } 749 2210 2959
elsif ($command eq 'save') { } 2 2208 2210
465 2 elsif ($command eq "save") { $parsed{"restore_pc"} = $PC-1 }
466
467 2959 if (0) { #$write_opcodes) {
468 #warn sprintf "addr:%s type:%s opcode:%02x (%s) operands:%s\n",
469 #$TYPE_LABELS[$op_style],
470 print((map {"$_=$parsed{$_} "} keys %parsed), "\n");
471 }
472
473 2959 return %parsed;
474 # async interpreter call (v4+), not implemented
475 # elsif ($op_style == OP_1OP && $opcode == 0x0b) {
476 # my $result = StoryFile::ret($operands[0]); }
477 }
478
479 } # Extra scoping brace around parse_command init stuff
480
481 # Read one operand of the given type, or
482 # return undef if given an argument of OP_TYPE_DONE
483 2 ▶sub load_operand {
Subroutine load_operand: called CC=5 SCAR=16.2
Pod undocumented
484 5808 my $op_type = shift;
485 # My kingdom for a switch!
486 5808 ▶ if ($op_type == OP_TYPE_VAR) {
Branch: 4 branches
BranchTrueFalseTotal
if ($op_type == 2) { } 942 4866 5808
elsif ($op_type == 1) { } 2662 2204 4866
elsif ($op_type == 0) { } 1471 733 2204
elsif ($op_type == 3) { } 733 0 733
487 942 return num_to_var(eat_byte());
488 } elsif ($op_type == OP_TYPE_SMALL) {
489 2662 return eat_byte();
490 } elsif ($op_type == OP_TYPE_LARGE) {
491 1471 return eat_word();
492 } elsif ($op_type == OP_TYPE_DONE) {
493 733 return undef;
494 } else {
495 0 die "Unknown arg '$op_type' to load_operands" ;
496 }
497 }
498
499 # Read a byte and move the Program Counter forward
500 8592 ▶sub eat_byte {
Subroutine eat_byte: called CC=1 SCAR=0.0
Pod undocumented
501 8592 return $Language::Zcode::Util::Memory[$PC++];
502 }
503
504 # Read a word and move the Program Counter forward
505 2865 ▶sub eat_word {
Subroutine eat_word: called CC=1 SCAR=0.0
Pod undocumented
506 2865 my $word = $Language::Zcode::Util::Memory[$PC++] << 8;
507 2865 $word += $Language::Zcode::Util::Memory[$PC++];
508 2865 return $word;
509 }
510
511 1999 ▶sub num_to_var {
Subroutine num_to_var: called CC=7 SCAR=20.4
Pod undocumented
512 1999 my $num = shift;
513 1999 ▶ if ($num =~ /^(sp|local\d+|g[a-f\d]{2})$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($num =~ /^(sp|local\d+|g[a-f\d]{2})$/) { } 66 1933 1999
elsif ($num == 0) { } 160 1773 1933
elsif ($num >= 1 and $num <= 15) { } 1440 333 1773
elsif ($num >= 16 and $num <= 255) { } 333 0 333
514 # e.g., load sp (load, store, etc. pass by reference)
515 # Can't dereference until runtime.
516 66 return "[$1]";
517 } elsif ($num == 0) {
518 160 return "sp";
519 ▶ } elsif ($num >=1 && $num <=15) {
Condition66%2/3
$num >= 1 and $num <= 15
!ll&&!rl&&r
03331440
MC/DC50%1/2
$num >= 1 and $num <= 15
$num >= 1 $num <= 15
Truth table66%2/3
A and B
A= $num >= 1B= $num <= 15
ABresult
0X0
100
111
520 1440 return "local" . ($num-1);
521 ▶ } elsif ($num >= 16 && $num <= 255) {
Condition33%1/3
$num >= 16 and $num <= 255
!ll&&!rl&&r
00333
MC/DC0%0/2
$num >= 16 and $num <= 255
$num >= 16 $num <= 255
Truth table33%1/3
A and B
A= $num >= 16B= $num <= 255
ABresult
0X0
100
111
522 333 return "g" . sprintf("%02x", $num - 16);
523 } else {
524 0 die "Illegal value '$num' passed to num_to_var";
525 }
526 }
527
528 # XXX TODO move this (back) to Language::Zcode::Util?
529 #
530 # decode and return text at this address; see spec section 3
531 # These are entries 6-32 in the 3 ZSCII alphabets
532 # XXX Differences for versions 1,2
533 366 ▶sub decode_text {
Subroutine decode_text: called CC=16 SCAR=28.6
Pod undocumented
534 366 my $buffer = "";
535 # XXX Versions 5+ may have different alphabet table.
536 366 my @alpha_table = (
537 [ 'a' .. 'z' ],
538 [ 'A' .. 'Z' ],
539 # char 6 means 10-bit ZSCII follows
540 [ undef, split//,qq{\n0123456789.,!?_#'"/\\-:()} ]
541 );
542
543 366 my ($word, $zshift, $zchar);
544 366 my $alphabet = 0;
545 366 my $abbreviation = 0;
546 366 my $two_bit_code = 0;
547 366 my $two_bit_flag = 0;
548 # XXX HACK!
549 366 my $flen = @Language::Zcode::Util::Memory;
550
551 366 while (1) {
552 1376 ▶ last if $PC >= $flen;
Branch: 1 branch
BranchTrueFalseTotal
if $PC >= $flen 0 1376 1376
553 1376 $word = eat_word();
554 # spec 3.2
555 1376 ▶ for ($zshift = 10; $zshift >= 0; $zshift -= 5) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zshift >= 0) 4128 1376 5504
556 # break word into 3 zcharacters of 5 bytes each
557 4128 $zchar = ($word >> $zshift) & 0x1f;
558 4128 ▶ if ($two_bit_flag > 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($two_bit_flag > 0) { } 152 3976 4128
elsif ($abbreviation) { } 0 3976 3976
elsif ($zchar < 6) { } 1310 2666 3976
559 # Ten-bit ZSCII character. spec 3.4
560 152 ▶ if ($two_bit_flag++ == 1) { # middle of char
Branch: 1 branch
BranchTrueFalseTotal
if ($two_bit_flag++ == 1) { } 76 76 152
561 76 $two_bit_code = $zchar << 5; # first 5 bits
562 } else { # end of char
563 76 $two_bit_code |= $zchar; # last 5
564 76 $buffer .= chr($two_bit_code);
565 76 $two_bit_code = $two_bit_flag = 0;
566 }
567 } elsif ($abbreviation) {
568 # synonym/abbreviation; spec 3.3
569 0 my $entry = (32 * ($abbreviation - 1)) + $zchar;
570 # Spec 3.3, 1.2.2: fetch and convert the "word PC" of the
571 # given entry in the abbreviations table.
572 # "word address"; only used for abbreviations (packed address
573 # rules do not apply here)
574 # my $abbrev_addr =
575 # $Language::Zcode::Util::Constants{abbrev_table_address} +
576 # $entry * 2;
577 #my $addr = Language::Zcode::Util::get_word_at($abbrev_addr) * 2;
578 #my $expanded = decode_text($addr);
579 0 $buffer .= "[abbrev $entry]";
580 #print STDERR "abbrev $abbreviation $expanded\n";
581 0 $abbreviation = 0;
582 } elsif ($zchar < 6) {
583 1310 ▶ if ($zchar == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($zchar == 0) { } 422 888 1310
elsif ($zchar == 4) { } 106 782 888
elsif ($zchar == 5) { } 782 0 782
elsif ($zchar >= 1 and $zchar <= 3) { } 0 0 0
584 422 $buffer .= " ";
585 } elsif ($zchar == 4) {
586 # spec 3.2.3: shift character; alphabet 1
587 106 $alphabet = 1;
588 } elsif ($zchar == 5) {
589 # spec 3.2.3: shift character; alphabet 2
590 782 $alphabet = 2;
591 ▶ } elsif ($zchar >= 1 && $zchar <= 3) {
Condition0%0/3
$zchar >= 1 and $zchar <= 3
!ll&&!rl&&r
000
MC/DC0%0/2
$zchar >= 1 and $zchar <= 3
$zchar >= 1 $zchar <= 3
Truth table0%0/3
A and B
A= $zchar >= 1B= $zchar <= 3
ABresult
0X0
100
111
592 # spec 3.3: next zchar is an abbreviation code
593 0 $abbreviation = $zchar;
594 }
595 } else {
596 # spec 3.5: convert remaining chars from alpha table
597 2666 $zchar -= 6;
598 # convert to string index
599 2666 ▶ if ($alphabet != 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alphabet != 2) { } 2228 438 2666
600 2228 $buffer .= $alpha_table[$alphabet]->[$zchar];
601 } else {
602 # alphabet 2; some special cases (3.5.3)
603 438 ▶ if ($zchar == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($zchar == 0) { } 76 362 438
elsif ($zchar == 1) { } 118 244 362
604 76 $two_bit_flag = 1;
605 } elsif ($zchar == 1) {
606 # Why did rezrov do this? -ADK
607 #$buffer .= chr(Games::Rezrov::ZConst::Z_NEWLINE());
608 118 $buffer .= "\n";
609 } else {
610 244 $buffer .= $alpha_table[$alphabet]->[$zchar];
611 }
612 }
613 2666 $alphabet = 0; # turn "Shift" off
614 # XXX applies to this character for version > 2 (3.2.3)
615 }
616 }
617 # Last bit set
618 1376 ▶ last if $word & 0x8000;
Branch: 1 branch
BranchTrueFalseTotal
if $word & 32768 366 1010 1376
619 }
620 366 return $buffer;
621 }
622
623 2 1;
624
« lib/Language/Zcode/Runtime/Input.pm ↑ Summary lib/Language/Zcode/Parser/Perl.pm »