File Coverage

blib/lib/Games/Rezrov/Inliner.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod 0 1 0.0
total 18 19 94.7


line stmt bran cond sub pod time code
1             package Games::Rezrov::Inliner;
2              
3             # inline a few of the most frequently used z-machine memory access
4             # calls. Provides a speed improvement at the cost of more obfuscated
5             # and heinously non-OO code. Oh well.
6              
7             # only works for TRIVIAL code: will break if "arguments" for inlined
8             # routines contain parens (can't handle nesting)
9              
10             1;
11              
12             sub inline {
13 8     8 0 19 my $ref = shift;
14            
15 8         14 my $rep = 'vec($Games::Rezrov::StoryFile::STORY_BYTES, $Games::Rezrov::StoryFile::PC++, 8)';
16 8         130 $$ref =~ s/GET_BYTE\(\)/$rep/og;
17             # replaces StoryFile::get_byte() -- z-machine memory access
18            
19 8         14 $rep = '(vec($Games::Rezrov::StoryFile::STORY_BYTES, $Games::Rezrov::StoryFile::PC++, 8) << 8) + vec($Games::Rezrov::StoryFile::STORY_BYTES, $Games::Rezrov::StoryFile::PC++, 8)';
20 8         100 $$ref =~ s/GET_WORD\(\)/$rep/og;
21             # replaces StoryFile::get_word() -- z-machine memory access
22              
23 8         14 $rep = 'unpack("S", pack("s", %s))';
24 8         45 $$ref =~ s/UNSIGNED_WORD\((.*?)\)/sprintf $rep, $1/eog;
  9         126  
25             # cast a perl variable into a unsigned 16-bit word (short).
26             # Necessary to ensure the sign bit is placed at 0x8000.
27             # Replaces unsigned_word() subroutine.
28             # WILL ONLY WORK IF NO NESTED PARENS
29              
30 8         15 $rep = 'unpack("s", pack("s", %s))';
31 8         48 $$ref =~ s/SIGNED_WORD\((.*?)\)/sprintf $rep, $1/eog;
  36         199  
32             # cast a perl variable into a signed 16-bit word (short).
33             # replaces signed_word() subroutine.
34             # WILL ONLY WORK IF NO NESTED PARENS
35              
36 8         12 $rep = 'vec($Games::Rezrov::StoryFile::STORY_BYTES, %s, 8)';
37 8         98 $$ref =~ s/GET_BYTE_AT\((.*?)\)/sprintf $rep, $1/eog;
  16         120  
38             # replaces StoryFile::get_byte_at($x) -- memory access
39             # WILL ONLY WORK IF NO NESTED PARENS
40              
41 8         15 $rep = '(vec($Games::Rezrov::StoryFile::STORY_BYTES, %s, 8) << 8) + vec($Games::Rezrov::StoryFile::STORY_BYTES, %s + 1, 8)';
42 8         89 $$ref =~ s/GET_WORD_AT\((.*?)\)/sprintf $rep, $1, $1/eog;
  6         71  
43             # replaces StoryFile::get_word_at($x) -- memory access
44             # WILL ONLY WORK IF NO NESTED PARENS
45            
46             }