ZInterpreter.pm

filter: Statementstmt 77.0% 124 / 161 Branchbran 64.1% 50 / 78 Conditioncond 31.2% 5 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 5 totaltotal 68.1% 195 / 286 CC 47 SCAR 37.8
CC
47
Cov
70%
__ANON__
74.4
generate_interpreter_code
7.3
new
6.6
CRAP 44.0
« ZHeader.pm ↑ Summary ZIO_Tools.pm »
1 package Games::Rezrov::ZInterpreter;
2 # interpret z-code
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # maybe a little faster here than during opcode call?
7  
8 1 ▶use Games::Rezrov::Inliner;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use constant OP_UNKNOWN => -1;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use constant OP_0OP => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant OP_1OP => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use constant OP_2OP => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant OP_VAR => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use constant OP_EXT => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use constant CALL_VN2 => 0x1a;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use constant CALL_VS2 => 0x0c;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 # var opcodes
20  
21 1 my $INTERPRETER_GENERATED = 0;
22  
23 1 ▶sub new {
Subroutine new: called CC=4 SCAR=18.9
Pod undocumented
24 1 my ($type, $zio) = @_;
25 1 my $self = {};
26 1 bless $self, $type;
27  
28 1 generate_interpreter_code();
29 # delay code generation until now so we can add optional code
30 # (or not include it) based on runtime options
31  
32 1 $self->zio($zio);
33  
34 1 ▶ if (my $where = Games::Rezrov::ZOptions::WRITE_OPCODES()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $where = Games::Rezrov::ZOptions::WRITE_OPCODES()) 0 1 1
35 0 ▶ if ($where eq "STDERR") {
Branch: 1 branch
BranchTrueFalseTotal
if ($where eq 'STDERR') { } 0 0 0
36 0 *Games::Rezrov::ZInterpreter::LOG = \*main::STDERR;
37 } else {
38 0 ▶ die "Can't write to $where: $!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless open LOG, ">$where" 0 0 0
39 unless open(LOG, ">$where");
40 }
41 0 my $old = select();
42 0 select LOG;
43 0 $|=1;
44 0 select $old;
45 }
46  
47 1 $self->restart(1);
48 1 $self->interpret();
49 1 return $self;
50 }
51  
52 1 ▶sub generate_interpreter_code {
Subroutine generate_interpreter_code: called CC=6 SCAR=19.9
Pod undocumented
53 1 ▶ unless ($INTERPRETER_GENERATED) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($INTERPRETER_GENERATED) 1 0 1
54 # Generate code for main interpreter routine.
55 # add optional chunks of code only if they're being used, to
56 # make the loops as tight as possible. Good for a small
57 # speed improvement.
58  
59 1 local $/ = undef;
60 1 my $inline_code = <DATA>;
61  
62 1 my $CODE;
63  
64 1 ▶ if (Games::Rezrov::ZOptions::WRITE_OPCODES()) {
Branch: 1 branch
BranchTrueFalseTotal
if (Games::Rezrov::ZOptions::WRITE_OPCODES()) 0 1 1
65 0 $CODE = '
66 printf LOG "count:%d style:%d pc:%d type:%s opcode:%d(0x%02x;raw=%d) (%s) operands:%s\n",
67 $opcode_count,
68 $op_style,
69 $start_pc,
70 $TYPE_LABELS[$op_style],
71 $opcode,
72 $opcode,
73 $orig_opcode,
74 ($generic_opcodes[$op_style]->[$opcode] ||
75 $manual_descs[$op_style]->[$opcode] || "???"),
76 join(",", @operands);
77 ';
78  
79 0 ▶ $inline_code =~ s/#WRITE_OPCODES_STUB/$CODE/ || die;
Branch: 1 branch
BranchTrueFalseTotal
$inline_code =~ s/#WRITE_OPCODES_STUB/$CODE/ or die 0 0 0
80 }
81  
82 1 ▶ if (Games::Rezrov::ZOptions::COUNT_OPCODES()) {
Branch: 1 branch
BranchTrueFalseTotal
if (Games::Rezrov::ZOptions::COUNT_OPCODES()) 0 1 1
83 0 $CODE='
84 $op_counts[$op_style]++;
85 ';
86 0 ▶ $inline_code =~ s/#COUNT_OPCODES_STUB/$CODE/ || die;
Branch: 1 branch
BranchTrueFalseTotal
$inline_code =~ s/#COUNT_OPCODES_STUB/$CODE/ or die 0 0 0
87 }
88  
89 1 Games::Rezrov::Inliner::inline(\$inline_code);
90  
91 # print $inline_code;die;
92  
93 0 ▶ eval $inline_code;
Subroutine __ANON__: called CC=36 SCAR=43.1
Subroutine zi_die: not called CC=2 SCAR=7.5
Pod undocumented
Branch: 29 branches
BranchTrueFalseTotal
if (($opcode & 128) == 0) { } 2563 1705 4268
elsif ($opcode & 64) { } 1010 695 1705
($opcode & 64) == 0 ? : 781 1782 2563
($opcode & 32) == 0 ? : 1480 1083 2563
($opcode & 32) == 0 ? : 86 924 1010
if ($opcode == 190 and $z_version >= 5) { } 0 695 695
elsif (($opcode & 48) == 48) { } 76 619 695
if ($optype > 0) { } 500 119 619
$optype == 1 ? : 14 486 500
if ($var_ops) 1010 3258 4268
if ($op_style == 3 and $opcode == 12 || $opcode == 26) { } 0 1010 1010
if ($i >= 0) 3862 58 3920
if $optype == 3 952 2910 3862
if ($optype > 0) { } 2673 237 2910
$optype == 1 ? : 227 2446 2673
if ($oc = $generic_opcodes[$op_style][$opcode]) { } 4234 34 4268
elsif ($op_style == 1) { } 29 5 34
elsif ($op_style == 3) { } 4 1 5
elsif ($op_style == 0) { } 1 0 1
if ($opcode == 11) { } 29 0 29
if ($opcode == 4) { } 4 0 4
elsif ($opcode == 22) { } 0 0 0
if ($count_opcodes and ++$input_counts > (Games::Rezrov::ZOptions::GUESS_TITLE() ? 1 : 0)) 0 4 4
Games::Rezrov::ZOptions::GUESS_TITLE() ? : 0 0 0
if ($opcode == 7) { } 0 1 1
elsif ($opcode == 10) { } 1 0 1
if (Games::Rezrov::ZOptions::END_OF_SESSION_MESSAGE()) 0 1 1
if Games::Rezrov::StoryFile::game_title() 0 1 1
if $count_opcodes 0 1 1
Condition31%5/16
$opcode == 190 and $z_version >= 5
!ll&&!rl&&r
69500
$op_style == 3 and $opcode == 12 || $opcode == 26
!ll&&!rl&&r
869240
$opcode == 12 or $opcode == 26
l!l&&r!l&&!r
00924
$count_opcodes and ++$input_counts > (Games::Rezrov::ZOptions::GUESS_TITLE() ? 1 : 0)
!ll&&!rl&&r
400
$op_counts[$key] || 0
l!l
00
$TYPE_LABELS[$style] || 'mystery'
l!l
00
MC/DC0%0/2
$opcode == 190 and $z_version >= 5
$opcode == 190 $z_version >= 5
MC/DC0%0/3
$op_style == 3 and $opcode == 12 || $opcode == 26
$op_style == 3 $opcode == 12 $opcode == 26
MC/DC0%0/2
$count_opcodes and ++$input_counts > (Games::Rezrov::ZOptions::GUESS_TITLE() ? 1 : 0)
$count_opcodes ++$input_counts > (Games::Rezrov::ZOptions::GUESS_TITLE() ? 1 : 0)
MC/DC0%0/1
$op_counts[$key] || 0
$op_counts[$key]
MC/DC0%0/1
$TYPE_LABELS[$style] || 'mystery'
$TYPE_LABELS[$style]
Truth table33%1/3
A and B
A= $opcode == 190B= $z_version >= 5
ABresult
0X0
100
111
Truth table50%2/4
A and B or C
A= $op_style == 3B= $opcode == 12C= $opcode == 26
ABCresult
0XX0
1000
11X1
1011
Truth table33%1/3
A and B
A= $count_opcodesB= ++$input_counts > (Games::Rezrov::ZOptions::GUESS_TITLE() ? 1 : 0)
ABresult
0X0
100
111
Truth table0%0/2
A
A= $op_counts[$key]
Aresult
11
00
Truth table0%0/2
A
A= $TYPE_LABELS[$style]
Aresult
11
00
94  
95 1 $INTERPRETER_GENERATED = 1;
96 }
97 }
98  
99 2 ▶sub zio {
Subroutine zio: called CC=2 SCAR=6.9
Pod undocumented
100 2 ▶ return (defined $_[1] ? $_[0]->{"zio"} = $_[1] : $_[0]->{"zio"});
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 1 1 2
101 }
102  
103 1 ▶sub restart {
Subroutine restart: called CC=2 SCAR=7.1
Pod undocumented
104 1 my ($self, $first_time) = @_;
105 1 ▶ Games::Rezrov::StoryFile::reset_storyfile() unless $first_time;
Branch: 1 branch
BranchTrueFalseTotal
unless $first_time 0 1 1
106 1 Games::Rezrov::StoryFile::reset_game();
107 }
108  
109 1 1;
110  
111 #
112 # code to be inlined starts here:
113 #
114 __DATA__
« ZHeader.pm ↑ Summary ZIO_Tools.pm »