lib/Zero/Emulator.pm

filter: Statementstmt 2.3% 44 / 1871 Branchbran 0.9% 4 / 422 Conditioncond 0.0% 0 / 79 MC/DCmcdc 0.0% 0 / 48 Subroutinesub 3.6% 10 / 277 Podpod 1.2% 1 / 81 totaltotal 2.1% 59 / 2778 CC 262 SCAR 35.4
CC
262
Cov
2%
right
182.0
execute
182.0
RefRight
156.0
CRAP 34.3
« blib/lib/Zero/Emulator.pm ↑ Summary lib/Zero/NWayTree.pm »
1 #!/usr/bin/perl -Ilib
2 #-------------------------------------------------------------------------------
3 # Assemble and execute code written in the Zero programming language.
4 # Philip R Brenan at appaapps dot com, Appa Apps Ltd Inc., 2023
5 #-------------------------------------------------------------------------------
6 # Pointless adds and subtracts by 0. Perhaps we should flag adds and subtracts by 1 as well so we can have an instruction optimized for these variants.
7 # Assign needs to know from whence we got the value so we can write a better error message when it is no good
8 # Count number of ways an if statement actually goes.
9 1 ▶use v5.30;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 package Zero::Emulator;
11 1 our $VERSION = 20230513; # Version
12 1 ▶use warnings FATAL=>qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Carp qw(cluck confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Data::Table::Text qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶eval "use Test::More tests=>78" unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 0 1 1
18  
19 1 makeDieConfess;
20  
21 0 ▶my sub maximumInstructionsToExecute {1e6} # Maximum number of subroutines to execute
Subroutine maximumInstructionsToExecute: not called CC=1 SCAR=6.9
22  
23 sub execute(%) # Execute a block of code..
24 0 ▶ {my (%options) = @_; # Execution options
Subroutine execute: not called CC=1 SCAR=6.9
Pod undocumented
25  
26 my $exec= genHash("Zero::Emulator::Execution", # Execution results
27 calls=> [], # Call stack
28 block=> $options{code}, # Block of code to be executed
29 count=> 0, # Executed instructions count
30 counts=> {}, # Executed instructions by name counts
31 tally=> 0, # Tally executed instructions in a bin of this name
32 tallyCount=> 0, # Executed instructions tally count
33 tallyCounts=> {}, # Executed instructions by name tally counts
34 instructionPointer=> 0, # Current instruction
35 memory=> {}, # Memory contents at the end of execution
36 memoryType=> {}, # Memory contents at the end of execution
37 rw=> {}, # Read / write access to memory
38 read=> {}, # Records whether a memory address was ever read allowing us to find all the unused locations
39 notReadAddresses=> {}, # Memory addresses never read
40 out=> [], # The out channel
41 doubleWrite=> {}, # Source of double writes {instruction number} to count - an existing value was overwritten before it was used
42 pointlessAssign=> {}, # Location already has the specified value
43 suppressOutput=> $options{suppressOutput}, # If true the Out instruction will only write to the execution out array but not to stdout as well.
44 stopOnError=> $options{stopOnError}, # Stop on non fatal errors if true
45 trace=> $options{trace}, # Trace all statements
46 tracePoints=> undef, # Trace changes in execution flow
47 printNotRead=> $options{NotRead}, # Memory locations never read
48 printDoubleWrite=> $options{doubleWrite}, # Double writes: earlier instruction number to later instruction number
49 printPointlessAssign=> $options{pointlessAssign}, # Pointless assigns {instruction number} to count - address already has the specified value
50 0 watch=> {}, # Addresses to watch for changes
51 instructionCounts=> {}, # The number of times each actual instruction is executed
52 lastAssignArea=> undef, # Last assignment performed - area
53 lastAssignAddress=> undef, # Last assignment performed - address
54 lastAssignValue=> undef, # Last assignment performed - value
55 lastAssignBefore=> undef, # Prior value of memory area before assignment
56 );
57 }
58  
59 my sub Code(%) # A block of code
60 0 ▶ {my (%options) = @_; # Parameters
Subroutine Code: not called CC=1 SCAR=6.9
61  
62 0 genHash("Zero::Emulator::Code", # Description of a call stack entry
63 assembled=> undef, # Needs to be assembled unless this field is true
64 code=> [], # An array of instructions
65 variables=> AreaStructure("Variables"), # Variables in this block of code
66 labels=> {}, # Label name to instruction
67 labelCounter=> 0, # Label counter used to generate unique labels
68 files=> [], # File number to file name
69 procedures=> {}, # Procedures defined in this block of code
70 %options,
71 );
72 }
73  
74 my sub stackFrame(%) # Describe an entry on the call stack: the return address, the parameter list length, the parameter list address, the line of code from which the call was made, the file number of the file from which the call was made
75 0 ▶ {my (%options) = @_; # Parameters
Subroutine stackFrame: not called CC=1 SCAR=6.9
76  
77 genHash("Zero::Emulator::StackFrame", # Description of a stack frame. A stack frame provides the context in which a method runs.
78 target=> $options{target}, # The address of the subroutine being called
79 instruction=> $options{call}, # The address of the instruction making the call
80 stackArea=> $options{stackArea}, # Memory area containing data for this method
81 params=> $options{params}, # Memory area containing parameter list
82 return=> $options{return}, # Memory area containing returned result
83 line=> $options{line}, # The line number from which the call was made
84 file=> $options{file}, # The file number from which the call was made - this could be folded into the line number but for reasons best known to themselves people who cannot program very well often scatter projects across several files a practice that is completely pointless in this day of git and so can only lead to chaos and confusion
85 variables=> $options{variables}, # Variables local to this stack frame
86 0 );
87 }
88  
89 sub Zero::Emulator::Code::instruction($%) #P Create a new instruction..
90 0 ▶ {my ($block, %options) = @_; # Block of code descriptor, options
Subroutine instruction: not called CC=3 SCAR=24.8
91  
92 0 ▶ my ($package, $fileName, $line) = caller($options{level} // 1);
Condition0%0/2
$options{"level"} // 1
l!l
00
MC/DC0%0/1
$options{"level"} // 1
$options{"level"}
Truth table0%0/2
A
A= $options{"level"}
Aresult
11
00
93  
94 my sub stackTrace() # File numbers and line numbers of callers
95 0 ▶ {my @s;
Subroutine stackTrace: not called CC=3 SCAR=24.8
96 0 for my $c(1..99)
97 0 {my @c = caller($c);
98 0 ▶ last unless @c;
Branch: 1 branch
BranchTrueFalseTotal
unless @c 0 0 0
99 0 push @s, [$c[1], $c[2]];
100 }
101 \@s
102 0 };
103  
104 0 ▶ if ($options{action} !~ m(\Avariable\Z)i) # Non variable
Branch: 1 branch
BranchTrueFalseTotal
if (not $options{"action"} =~ /\Avariable\Z/iu) 0 0 0
105 {push $block->code->@*, my $i = genHash("Zero::Emulator::Code::Instruction", # Instruction details
106 action=> $options{action }, # Instruction name
107 number=> $options{number }, # Instruction sequence number
108 source=> $options{source }, # Source memory address
109 source2=> $options{source2 }, # Secondary source memory address
110 target=> $options{target }, # Target memory address
111 target2=> $options{target2 }, # Secondary target memory address
112 0 line=> $line, # Line in source file at which this instruction was encoded
113 file=> fne $fileName, # Source file in which instruction was encoded
114 context=> stackTrace(), # The call context in which this instruction was created
115 executed=> 0, # The number of times this instruction was executed
116 );
117 0 return $i;
118 }
119 }
120  
121 sub Zero::Emulator::Code::Instruction::contextString($$$) #P Stack trace back for this instruction..
122 0 ▶ {my ($i, $exec, $title) = @_; # Instruction, execution environment, title
Subroutine contextString: not called CC=4 SCAR=30.0
123 0 ▶ @_ == 3 or confess "Three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or confess("Three parameters") 0 0 0
124 0 my @s;
125 0 push @s, $title;
126 0 ▶ if (! $exec->suppressOutput)
Branch: 1 branch
BranchTrueFalseTotal
unless ($exec->suppressOutput) 0 0 0
127 0 {for my $c($i->context->@*)
128 0 {push @s, sprintf " at %s line %d", $$c[0], $$c[1];
129 }
130 }
131 @s
132 0 }
133  
134 sub AreaStructure($@) # Describe a data structure mapping a memory area..
135 3 ▶ {my ($structureName, @names) = @_; # Structure name, fields names
Subroutine AreaStructure: called CC=2 SCAR=6.9
Pod undocumented
136  
137 3 my $d = genHash("Zero::Emulator::AreaStructure", # Description of a data structure mapping a memory area
138 structureName=> $structureName, # Name of the structure
139 fieldOrder=> [], # Order of the elements in the structure, in effect, giving the offset of each element in the data structure
140 fieldNames=> {}, # Maps the names of the fields to their offsets in the structure
141 instructions3=> [], # The variable instruction associated with this variable
142 );
143 3 $d->field($_) for @names; # Add the field descriptions
144 3 $d
145 }
146  
147 my sub isScalar($) # Check whether an element is a scalar or an array
148 0 ▶ {my ($value) = @_; # Parameters
Subroutine isScalar: not called CC=1 SCAR=6.9
149 0 ! ref $value;
150 }
151  
152 sub Zero::Emulator::AreaStructure::count($) #P Add a field to a data structure..
153 0 ▶ {my ($d) = @_; # Area structure
Subroutine count: not called CC=1 SCAR=6.9
154 0 scalar $d->fieldOrder->@*
155 }
156  
157 sub Zero::Emulator::AreaStructure::name($$) #P Add a field to a data structure..
158 15 ▶ {my ($d, $name) = @_; # Parameters
Subroutine name: called CC=3 SCAR=11.6
159 15 ▶ @_ == 2 or confess "Two parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or confess("Two parameters") 0 15 15
160 15 ▶ if (!defined $d->fieldNames->{$name})
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $d->fieldNames->{$name}) { } 15 0 15
161 15 {$d->fieldNames->{$name} = $d->fieldOrder->@*;
162 15 push $d->fieldOrder->@*, $name;
163 }
164 else
165 0 {confess "Duplicate name: $name in structure: ".$d->name;
166 }
167 15 \($d->fieldNames->{$name})
168 }
169  
170 my sub procedure($%) # Describe a procedure
171 0 ▶ {my ($label, %options) = @_; # Start label of procedure, options describing procedure
Subroutine procedure: not called CC=1 SCAR=6.9
172  
173 0 genHash("Zero::Emulator::Procedure", # Description of a procedure
174 target=> $label, # Label to call to call this procedure
175 variables=> AreaStructure("Procedure"), # Registers local to this procedure
176 );
177 }
178  
179 sub Zero::Emulator::AreaStructure::registers($) #P Create one or more temporary variables. Need to reuse registers no longer in use..
180 0 ▶ {my ($d, $count) = @_; # Parameters
Subroutine registers: not called CC=3 SCAR=24.8
181 0 ▶ @_ == 1 or confess "One parameter";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter") 0 0 0
182 0 ▶ if (!defined($count))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $count) 0 0 0
183 0 {my $o = $d->fieldOrder->@*;
184 0 push $d->fieldOrder->@*, undef;
185 0 return \$o; # One temporary
186 }
187 0 map {__SUB__->($d)} 1..$count; # Array of temporaries
188 }
189  
190 sub Zero::Emulator::AreaStructure::offset($$) #P Offset of a field in a data structure..
191 0 ▶ {my ($d, $name) = @_; # Parameters
Subroutine offset: not called CC=3 SCAR=24.8
192 0 ▶ @_ == 2 or confess "Two parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or confess("Two parameters") 0 0 0
193 0 ▶ if (defined(my $n = $d->fieldNames->{$name})){return $n}
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n = $d->fieldNames->{$name})) 0 0 0
194 0 confess "No such name: '$name' in structure: ".$d->structureName;
195 }
196  
197 sub Zero::Emulator::AreaStructure::address($$) #P Address of a field in a data structure..
198 0 ▶ {my ($d, $name) = @_; # Parameters
Subroutine address: not called CC=3 SCAR=24.8
199 0 ▶ @_ == 2 or confess "Two parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or confess("Two parameters") 0 0 0
200 0 ▶ if (defined(my $n = $d->fieldNames->{$name})){return \$n}
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $n = $d->fieldNames->{$name})) 0 0 0
201 0 confess "No such name: '$name' in structure: ".$d->structureName;
202 }
203  
204 sub Zero::Emulator::Procedure::registers($) #P Allocate a register within a procedure..
205 0 ▶ {my ($procedure) = @_; # Procedure description
Subroutine registers: not called CC=2 SCAR=17.9
206 0 ▶ @_ == 1 or confess "One parameter";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter") 0 0 0
207 0 $procedure->variables->registers();
208 }
209  
210 my sub RefRight($) # Record a reference to a right address
211 0 ▶ {my ($r) = @_; # Reference
Subroutine RefRight: not called CC=12 SCAR=50.5
212 0 ▶ @_ == 1 or confess "One parameter required formatted as either a address or an [area, address, name]";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter required formatted as either a address or an [area, address, name]") 0 0 0
213 0 ▶ ref($r) and ref($r) !~ m(\A(array|scalar|ref)\Z)i and confess "Scalar or reference required, not: ".dump($r);
Branch: 1 branch
BranchTrueFalseTotal
ref $r and not ref($r) =~ /\A(array|scalar|ref)\Z/iu and confess('Scalar or reference required, not: ' . Zero::Emulator::dump($r)) 0 0 0
Condition0%0/3
ref $r and not ref($r) =~ /\A(array|scalar|ref)\Z/iu
!ll&&!rl&&r
000
MC/DC0%0/2
ref $r and not ref($r) =~ /\A(array|scalar|ref)\Z/iu
ref $r not ref($r) =~ /\A(array|scalar|ref)\Z/iu
Truth table0%0/3
A and not(B)
A= ref $rB= not ref($r) =~ /\A(array|scalar|ref)\Z/iu
ABresult
0X0
100
111
214  
215 0 ▶ if (ref($r) =~ m(array)i)
Branch: 1 branch
BranchTrueFalseTotal
if (ref($r) =~ /array/iu) { } 0 0 0
216 0 {my ($area, $address, $name) = @$r;
217 0 ▶ defined($area) and !defined($name) and confess "Name required for address specification: in [Area, address, name]";
Branch: 1 branch
BranchTrueFalseTotal
defined $area and not defined $name and confess("Name required for address specification: in [Area, address, name]") 0 0 0
Condition0%0/3
defined $area and not defined $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $area and not defined $name
defined $area not defined $name
Truth table0%0/3
A and not(B)
A= defined $areaB= not defined $name
ABresult
0X0
100
111
218 0 ▶ !defined($area) and defined($name) and confess "Area required for address specification: in [Area, address, name]";
Branch: 1 branch
BranchTrueFalseTotal
not defined $area and defined $name and confess("Area required for address specification: in [Area, address, name]") 0 0 0
Condition0%0/3
not defined $area and defined $name
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $area and defined $name
not defined $area defined $name
Truth table0%0/3
not(A) and B
A= not defined $areaB= defined $name
ABresult
0X0
100
111
219 0 ▶ isScalar($address) and defined($area) || defined($name) and confess "Constants cannot have an associated area";
Branch: 1 branch
BranchTrueFalseTotal
isScalar $address and defined $area || defined $name and confess("Constants cannot have an associated area") 0 0 0
Condition0%0/6
isScalar $address and defined $area || defined $name
!ll&&!rl&&r
000
defined $area or defined $name
l!l&&r!l&&!r
000
MC/DC0%0/3
isScalar $address and defined $area || defined $name
isScalar $address defined $area defined $name
Truth table0%0/4
A and B or C
A= isScalar $addressB= defined $areaC= defined $name
ABCresult
0XX0
1000
11X1
1011
220  
221 0 return genHash("Zero::Emulator::RefRight",
222 area=> $area,
223 address=> $address,
224 name=> $name,
225 variable=> 1,
226 );
227 }
228 else
229 0 {return genHash("Zero::Emulator::RefRight",
230 area=> undef,
231 address=> $r,
232 name=> 'stackArea',
233 variable=> 0,
234 );
235 }
236 }
237  
238 my sub RefLeft($) # Record a reference to a left address
239 0 ▶ {my ($r) = @_; # Reference
Subroutine RefLeft: not called CC=9 SCAR=45.0
240 0 ▶ @_ == 1 or confess "One parameter required formatted as either a address or an [area, address, name]";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter required formatted as either a address or an [area, address, name]") 0 0 0
241  
242 0 ▶ ref($r) and ref($r) !~ m(\A(array|scalar|ref)\Z)i and confess "Scalar or reference required";
Branch: 1 branch
BranchTrueFalseTotal
ref $r and not ref($r) =~ /\A(array|scalar|ref)\Z/iu and confess("Scalar or reference required") 0 0 0
Condition0%0/3
ref $r and not ref($r) =~ /\A(array|scalar|ref)\Z/iu
!ll&&!rl&&r
000
MC/DC0%0/2
ref $r and not ref($r) =~ /\A(array|scalar|ref)\Z/iu
ref $r not ref($r) =~ /\A(array|scalar|ref)\Z/iu
Truth table0%0/3
A and not(B)
A= ref $rB= not ref($r) =~ /\A(array|scalar|ref)\Z/iu
ABresult
0X0
100
111
243  
244 0 ▶ if (ref($r) =~ m(array)i)
Branch: 1 branch
BranchTrueFalseTotal
if (ref($r) =~ /array/iu) { } 0 0 0
245 0 {my ($area, $address, $name) = @$r;
246 0 ▶ defined($area) and !defined($name) and confess "Name required for area: in {area, address, name]";
Branch: 1 branch
BranchTrueFalseTotal
defined $area and not defined $name and confess("Name required for area: in {area, address, name]") 0 0 0
Condition0%0/3
defined $area and not defined $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $area and not defined $name
defined $area not defined $name
Truth table0%0/3
A and not(B)
A= defined $areaB= not defined $name
ABresult
0X0
100
111
247 0 ▶ !defined($area) and defined($name) and confess "Area required for name: in {area, address, name]";
Branch: 1 branch
BranchTrueFalseTotal
not defined $area and defined $name and confess("Area required for name: in {area, address, name]") 0 0 0
Condition0%0/3
not defined $area and defined $name
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $area and defined $name
not defined $area defined $name
Truth table0%0/3
not(A) and B
A= not defined $areaB= defined $name
ABresult
0X0
100
111
248  
249 0 return genHash("Zero::Emulator::RefLeft",
250 area=> $area,
251 address=> $address,
252 name=> $name,
253 );
254 }
255 else
256 0 {return genHash("Zero::Emulator::RefLeft",
257 area=> undef,
258 address=> $r,
259 name=> 'stackArea',
260 );
261 }
262 }
263  
264 sub Zero::Emulator::Reference::print($) #P Print the value of an address..
265 0 ▶ {my ($ref) = @_; # Reference specification
Subroutine print: not called CC=2 SCAR=17.9
266 0 ▶ @_ == 1 or confess "One parameter";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter") 0 0 0
267 0 my $a = dump($ref->area);
268 0 my $l = dump($ref->address);
269 0 my $s = "Reference area: $a, address: $l";
270 0 say STDERR $s;
271 }
272  
273 sub Zero::Emulator::Address::print($$) #P Print the value of an address in the current execution..
274 0 ▶ {my ($address, $exec) = @_; # Address specification
Subroutine print: not called CC=3 SCAR=24.8
275 0 ▶ @_ == 2 or confess "Two parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or confess("Two parameters") 0 0 0
276 0 my $e = $exec;
277 0 my $m = $e->memory;
278 0 my $t = $e->memoryType;
279 0 my $a = $address->area;
280 0 my $l = $address->address;
281 0 my $s = "Address area: $a";
282 0 ▶ $s .= "(".($$t{$a} // "unknown")."), ";
Condition0%0/2
$t->{$a} // "unknown"
l!l
00
MC/DC0%0/1
$t->{$a} // "unknown"
$t->{$a}
Truth table0%0/2
A
A= $t->{$a}
Aresult
11
00
283 0 $s .= "address: $l";
284 }
285  
286 sub Zero::Emulator::Address::get($$) #P Get the value of the specified address in memory in the specified execution environment..
287 0 ▶ {my ($address, $exec) = @_; # Address specification
Subroutine get: not called CC=2 SCAR=17.9
288 0 ▶ @_ == 2 or confess "Two parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or confess("Two parameters") 0 0 0
289 0 my $e = $exec;
290 0 my $m = $e->memory;
291 0 my $a = $address->area;
292 0 my $l = $address->address;
293 0 $$m{$a}[$l];
294 }
295  
296 sub Zero::Emulator::Address::at($$) #P Reference to the specified address in memory of current execution environment..
297 0 ▶ {my ($address, $exec) = @_; # Address specification
Subroutine at: not called CC=2 SCAR=17.9
298 0 ▶ @_ == 2 or confess "Two parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or confess("Two parameters") 0 0 0
299 0 my $e = $exec;
300 0 my $m = $e->memory;
301 0 my $a = $address->area;
302 0 my $l = $address->address;
303 0 \$$m{$a}[$l]
304 }
305  
306 sub Zero::Emulator::Procedure::call($) #P Call a procedure. Arguments are supplied by the L<ParamsPut> and L<ParamsGet> commands, return values are supplied by the L<ReturnPut> and L<ReturnGet> commands..
307 0 ▶ {my ($procedure) = @_; # Procedure description
Subroutine call: not called CC=2 SCAR=17.9
308 0 ▶ @_ == 1 or confess "One parameter";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter") 0 0 0
309 0 Zero::Emulator::Call($procedure->target);
310 }
311  
312 sub Zero::Emulator::Code::registers($) #P Allocate registers.
313 0 ▶ {my ($code, $number) = @_; # Code block, number of registers required
Subroutine registers: not called CC=2 SCAR=17.9
314 0 ▶ @_ == 1 or confess "One parameter";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter") 0 0 0
315 0 $code->variables->registers
316 }
317  
318 sub Zero::Emulator::Code::assemble($%) #P Assemble a block of code to prepare it for execution.
319 0 ▶ {my ($Block, %options) = @_; # Code block, assembly options
Subroutine assemble: not called CC=8 SCAR=42.8
320 0 ▶ return $Block if $Block->assembled; # Already assembled
Branch: 1 branch
BranchTrueFalseTotal
if $Block->assembled 0 0 0
321 0 my $code = $Block->code; # The code to be assembled
322 0 my $vars = $Block->variables; # The variables referenced by the code
323  
324 0 my %labels; # Load labels
325 0 my $stackFrame = AreaStructure("Stack"); # The current stack frame we are creating variables in
326  
327 0 for my $c(keys @$code) # Labels
328 0 {my $i = $$code[$c];
329 0 $i->number = $c;
330 0 ▶ next unless $i->action eq "label";
Branch: 1 branch
BranchTrueFalseTotal
unless $i->action eq "label" 0 0 0
331 0 $labels{$i->source->address} = $i; # Point label to instruction
332 }
333  
334 0 for my $c(keys @$code) # Target jump and call instructions
335 0 {my $i = $$code[$c];
336 0 ▶ next unless $i->action =~ m(\A(j|call))i;
Branch: 1 branch
BranchTrueFalseTotal
unless $i->action =~ /\A(j|call)/iu 0 0 0
337 0 ▶ if (my $l = $i->target->address) # Label
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = $i->target->address) 0 0 0
338 0 ▶ {if (my $t = $labels{$l}) # Found label
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $labels{$l}) { } 0 0 0
339 0 {$i->target = RefRight($t->number - $c); # Relative jump
340 }
341 else
342 0 {my $a = $i->action;
343 0 confess "No target for $a to label: $l";
344 }
345 }
346 }
347  
348 0 $Block->labels = {%labels}; # Labels created during assembly
349 0 $Block->assembled = time; # Time of assembly
350 0 $Block
351 }
352  
353 #D1 Execution
354 #D2 Analysis
355  
356 sub Zero::Emulator::Execution::areaContent($$) #P Content of an area containing a address in memory in the specified execution.
357 0 ▶ {my ($exec, $address) = @_; # Execution environment, address specification
Subroutine areaContent: not called CC=4 SCAR=30.0
358 0 ▶ @_ == 2 or confess "Two parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or confess("Two parameters") 0 0 0
359 0 my $e = $exec;
360 0 my $m = $e->memory;
361 0 ▶ my $a = isScalar($address) ? $address : $$m{$exec->stackArea}[$$address]; # Dereference area
Branch: 1 branch
BranchTrueFalseTotal
isScalar $address ? : 0 0 0
362 0 my $A = $$m{$a};
363 0 ▶ $exec->stackTraceAndExit("Invalid area: ".dump($a)."\n".dump($e->memory)) unless defined $A;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $A 0 0 0
364 0 @$A
365 }
366  
367 sub Zero::Emulator::Execution::dumpMemory($) #P Dump memory.
368 0 ▶ {my ($exec) = @_; # Execution environment
Subroutine dumpMemory: not called CC=5 SCAR=34.0
369 0 ▶ @_ == 1 or confess "One parameter";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter") 0 0 0
370 0 my $memory = $exec->memory;
371 0 my @m;
372 0 for my $area(sort {$a <=> $b} keys %$memory)
373 0 ▶ {next if $area =~ m(\Astack\Z)i;
Branch: 1 branch
BranchTrueFalseTotal
if $area =~ /\Astack\Z/iu 0 0 0
374 0 my $l = dump($$memory{$area});
375 0 ▶ $l = substr($l, 0, 100) if length($l) > 100;
Branch: 1 branch
BranchTrueFalseTotal
if length $l > 100 0 0 0
376 0 push @m, "$area=$l";
377 }
378  
379 @m
380 0 }
381  
382 sub Zero::Emulator::Execution::analyzeExecutionResultsLeast($%) #P Analyze execution results for least used code.
383 0 ▶ {my ($exec, %options) = @_; # Execution results, options
Subroutine analyzeExecutionResultsLeast: not called CC=7 SCAR=40.3
384  
385 0 my @c = $exec->block->code->@*;
386 0 my %l;
387 0 for my $i(@c) # Count executions of each instruction
388 0 ▶ {$l{$i->file}{$i->line} += $i->executed unless $i->action =~ m(\Aassert)i;
Branch: 1 branch
BranchTrueFalseTotal
unless $i->action =~ /\Aassert/iu 0 0 0
389 }
390  
391 0 my @L;
392 0 for my $f(keys %l)
393 0 {for my $l(keys $l{$f}->%*)
394 0 {push @L, [$l{$f}{$l}, $f, $l];
395 }
396 }
397 0 my @l = sort {$$a[0] <=> $$b[0]} # By frequency
398 0 sort {$$a[2] <=> $$b[2]} @L; # By line number
399  
400 0 ▶ my $N = $options{least}//1;
Condition0%0/2
$options{"least"} // 1
l!l
00
MC/DC0%0/1
$options{"least"} // 1
$options{"least"}
Truth table0%0/2
A
A= $options{"least"}
Aresult
11
00
401 0 ▶ $#l = $N if @l > $N;
Branch: 1 branch
BranchTrueFalseTotal
if @l > $N 0 0 0
402 0 map {sprintf "%4d at %s line %4d", $$_[0], $$_[1], $$_[2]} @l;
403 }
404  
405 sub Zero::Emulator::Execution::analyzeExecutionResultsMost($%) #P Analyze execution results for most used code.
406 0 ▶ {my ($exec, %options) = @_; # Execution results, options
Subroutine analyzeExecutionResultsMost: not called CC=4 SCAR=30.0
407  
408 0 my @c = $exec->block->code->@*;
409 0 my %m;
410 0 for my $i(@c) # Count executions of each instruction
411 {my $t = # Traceback
412 0 join "\n", map {sprintf " at %s line %4d", $$_[0], $$_[1]} $i->context->@*;
413 0 $m{$t} += $i->executed;
414 }
415 0 my @m = reverse sort {$$a[1] <=> $$b[1]} map {[$_, $m{$_}]} keys %m; # Sort a hash into value order
416 0 ▶ my $N = $options{most}//1;
Condition0%0/2
$options{"most"} // 1
l!l
00
MC/DC0%0/1
$options{"most"} // 1
$options{"most"}
Truth table0%0/2
A
A= $options{"most"}
Aresult
11
00
417 0 ▶ $#m = $N if @m > $N;
Branch: 1 branch
BranchTrueFalseTotal
if @m > $N 0 0 0
418 0 map{sprintf "%4d\n%s", $m[$_][1], $m[$_][0]} keys @m;
419 }
420  
421 sub Zero::Emulator::Execution::analyzeExecutionNotRead($%) #P Analyze execution results for variables never read.
422 0 ▶ {my ($exec, %options) = @_; # Execution results, options
Subroutine analyzeExecutionNotRead: not called CC=3 SCAR=24.8
423  
424 0 my @t;
425 0 my $n = $exec->notRead;
426 0 for my $areaK(sort keys %$n)
427 0 {my $area = $$n{$areaK};
428 0 for my $addressK(sort keys %$area)
429 0 {my $address = $$area{$addressK};
430 0 push @t, $exec->block->code->[$addressK]->contextString
431 ($exec, "Not read from area: $areaK, address: $addressK in context:");
432 }
433 }
434 0 @t;
435 }
436  
437 sub Zero::Emulator::Execution::analyzeExecutionResultsDoubleWrite($%) #P Analyze execution results - double writes.
438 0 ▶ {my ($exec, %options) = @_; # Execution results, options
Subroutine analyzeExecutionResultsDoubleWrite: not called CC=5 SCAR=34.0
439  
440 0 my @r;
441  
442 0 my $W = $exec->doubleWrite;
443 0 ▶ if (keys %$W)
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$W) 0 0 0
444 0 {for my $p(sort keys %$W)
445 0 {for my $q(keys $$W{$p}->%*)
446 0 {push @r, sprintf "Double write occured %d times. ", $$W{$p}{$q};
447 0 ▶ if ($p eq $q)
Branch: 1 branch
BranchTrueFalseTotal
if ($p eq $q) { } 0 0 0
448 0 {push @r, "First and second write\n$p\n";
449 }
450 else
451 0 {push @r, "First write:\n$p\n";
452 0 push @r, "Second write:\n$q\n";
453 }
454 }
455 }
456 }
457 @r
458 0 }
459  
460 sub Zero::Emulator::Execution::analyzeExecutionResults($%) #P Analyze execution results.
461 0 ▶ {my ($exec, %options) = @_; # Execution results, options
Subroutine analyzeExecutionResults: not called CC=9 SCAR=45.0
462  
463 0 my @r;
464  
465 0 if (1)
466 0 {my @l = $exec->analyzeExecutionResultsLeast(%options); # Least/most executed
467 0 my @m = $exec->analyzeExecutionResultsMost (%options);
468 0 ▶ if (@l and $options{leastExecuted})
Branch: 1 branch
BranchTrueFalseTotal
if (@l and $options{"leastExecuted"}) 0 0 0
Condition0%0/3
@l and $options{"leastExecuted"}
!ll&&!rl&&r
000
MC/DC0%0/2
@l and $options{"leastExecuted"}
@l $options{"leastExecuted"}
Truth table0%0/3
A and B
A= @lB= $options{"leastExecuted"}
ABresult
0X0
100
111
469 0 {push @r, "Least executed:";
470 0 push @r, @l;
471 }
472 0 ▶ if (@m and $options{mostExecuted})
Branch: 1 branch
BranchTrueFalseTotal
if (@m and $options{"mostExecuted"}) 0 0 0
Condition0%0/3
@m and $options{"mostExecuted"}
!ll&&!rl&&r
000
MC/DC0%0/2
@m and $options{"mostExecuted"}
@m $options{"mostExecuted"}
Truth table0%0/3
A and B
A= @mB= $options{"mostExecuted"}
ABresult
0X0
100
111
473 0 {push @r, "Most executed:";
474 0 push @r, @m;
475 }
476 }
477  
478 0 ▶ if (my @n = $exec->analyzeExecutionNotRead(%options)) # Variables not read
Branch: 1 branch
BranchTrueFalseTotal
if (my(@n) = $exec->analyzeExecutionNotRead(%options)) 0 0 0
479 0 {my $n = @n;
480 0 ▶ @n = () unless $options{notRead};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"notRead"} 0 0 0
481 0 push @r, @n;
482 0 push @r, sprintf "# %8d variables not read", $n;
483 }
484  
485 0 ▶ if (my @d = $exec->analyzeExecutionResultsDoubleWrite(%options)) # Analyze execution results - double writes
Branch: 1 branch
BranchTrueFalseTotal
if (my(@d) = $exec->analyzeExecutionResultsDoubleWrite(%options)) 0 0 0
486 0 {my $d = @d;
487 0 ▶ @d = () unless $options{doubleWrite};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"doubleWrite"} 0 0 0
488 0 push @r, @d;
489 0 push @r, sprintf "# %8d double writes", $d/2;
490 }
491  
492 0 push @r, sprintf "# %8d instructions executed", $exec->count;
493 0 join "\n", @r;
494 }
495  
496 #D2 Memory # Access memory
497  
498 sub Zero::Emulator::Execution::check($$$) #P Check that a user area access is valid.
499 0 ▶ {my ($exec, $area, $name) = @_; # Execution environment, area, expected area name
Subroutine check: not called CC=6 SCAR=37.4
500 0 ▶ @_ == 3 or confess "Three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or confess("Three parameters") 0 0 0
501 0 ▶ if ($area and $area =~ m(\A\d+\Z))
Branch: 1 branch
BranchTrueFalseTotal
if ($area and $area =~ /\A\d+\Z/u) 0 0 0
Condition0%0/3
$area and $area =~ /\A\d+\Z/u
!ll&&!rl&&r
000
MC/DC0%0/2
$area and $area =~ /\A\d+\Z/u
$area $area =~ /\A\d+\Z/u
Truth table0%0/3
A and B
A= $areaB= $area =~ /\A\d+\Z/u
ABresult
0X0
100
111
502 0 {my $Name = $exec->memoryType->{$area};
503 0 ▶ if (!defined $Name)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Name) 0 0 0
504 0 {$exec->stackTraceAndExit("Attempting to access unnamed area: $area");
505 0 return;
506 }
507 0 ▶ if ($name ne $Name)
Branch: 1 branch
BranchTrueFalseTotal
if ($name ne $Name) 0 0 0
508 0 {$exec->stackTraceAndExit("Attempting to access area: $Name using: $name");
509 }
510 }
511 }
512  
513 sub Zero::Emulator::Execution::getMemory($$$;$%) #P Get from memory.
514 0 ▶ {my ($exec, $area, $address, $name, %options) = @_; # Execution environment, area, address, expected name of area, options
Subroutine getMemory: not called CC=5 SCAR=34.0
515 0 ▶ @_ < 3 and confess "At least three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ < 3 and confess("At least three parameters") 0 0 0
516 0 $exec->check($area, $name);
517 0 my $g = $exec->get($area, $address);
518 0 ▶ my $n = $name // 'unknown';
Condition0%0/2
$name // "unknown"
l!l
00
MC/DC0%0/1
$name // "unknown"
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
519 0 ▶ if (!defined($g) and !defined($options{undefinedOk}))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $g or defined $options{"undefinedOk"}) 0 0 0
Condition0%0/3
defined $g or defined $options{"undefinedOk"}
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $g or defined $options{"undefinedOk"}
defined $g defined $options{"undefinedOk"}
Truth table0%0/3
A or B
A= defined $gB= defined $options{"undefinedOk"}
ABresult
1X1
011
000
520 0 {$exec->stackTraceAndExit("Undefined memory accessed at area: $area ($n), address: $address\n");
521 }
522 $g
523 0 }
524  
525 sub Zero::Emulator::Execution::get($$$) #P Get from memory.
526 0 ▶ {my ($exec, $area, $address) = @_; # Execution environment, area, address
Subroutine get: not called CC=2 SCAR=17.9
527 0 ▶ @_ == 3 or confess "Three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or confess("Three parameters") 0 0 0
528 0 $exec->memory->{$area}[$address];
529 }
530  
531  
532 sub Zero::Emulator::Execution::set($$$) #P Set the value of an address at the specified address in memory in the current execution environment.
533 0 ▶ {my ($exec, $address, $value) = @_; # Execution environment, address specification, value
Subroutine set: not called CC=2 SCAR=17.9
534 0 ▶ @_ == 3 or confess "Three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or confess("Three parameters") 0 0 0
535 0 my $e = $exec;
536 0 my $m = $e->memory;
537 0 my $a = $address->area;
538 0 my $l = $address->address;
539 0 $exec->lastAssignArea = $a;
540 0 $exec->lastAssignAddress = $l;
541 0 $exec->lastAssignValue = $value;
542 0 $exec->lastAssignBefore = $$m{$a}[$l];
543  
544 0 $$m{$a}[$l] = $value;
545 }
546  
547 #D2 Execution # Execute assembly code in the emulator
548  
549 sub Zero::Emulator::Execution::stackArea($) #P Current stack frame.
550 0 ▶ {my ($exec) = @_; # Execution environment
Subroutine stackArea: not called CC=1 SCAR=6.9
551 0 $exec->calls->[-1]->stackArea; # Stack area
552 }
553  
554 sub Zero::Emulator::Execution::address($$$$) #P Record a reference to memory.
555 0 ▶ {my ($exec, $area, $address, $name) = @_; # Execution environment, area, address in area, memory
Subroutine address: not called CC=3 SCAR=24.8
556 0 ▶ @_ == 4 or confess "Four parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 4 or confess("Four parameters") 0 0 0
557 0 ▶ genHash("Zero::Emulator::Address", # Address memory
Condition0%0/2
$name // "stackArea"
l!l
00
MC/DC0%0/1
$name // "stackArea"
$name
Truth table0%0/2
A
A= $name
Aresult
11
00
558 area=> $area, # Area in memory
559 address=> $address, # Address within area
560 name=> $name // 'stackArea', # Name of area
561 );
562 }
563  
564 sub Zero::Emulator::Execution::stackTrace($;$) #P Create a stack trace.
565 0 ▶ {my ($exec, $title) = @_; # Execution environment, title
Subroutine stackTrace: not called CC=5 SCAR=34.0
566 0 my $i = $exec->currentInstruction;
567 0 my $s = $exec->suppressOutput; # Suppress file and line numbers in dump to facilitate automated testing
568 0 my @t;
569 0 ▶ push @t, $i->contextString($exec, $title//"Stack trace");
Condition0%0/2
$title // "Stack trace"
l!l
00
MC/DC0%0/1
$title // "Stack trace"
$title
Truth table0%0/2
A
A= $title
Aresult
11
00
570  
571 0 for my $j(reverse keys $exec->calls->@*)
572 0 {my $c = $exec->calls->[$j];
573 0 my $i = $c->instruction;
574 0 ▶ push @t, sprintf "%5d %4d %s", $j+1, $i->number+1, $i->action if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 0 0 0
575 0 ▶ push @t, sprintf "%5d %4d %-16s at %s line %d",
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 0 0
576 $j+1, $i->number+1, $i->action, $i->file, $i->line unless $s;
577 }
578  
579 @t
580 0 }
581  
582 sub Zero::Emulator::Execution::stackTraceAndExit($$%) #P Create a stack trace and exit from the emulated program.
583 0 ▶ {my ($exec, $title, %options) = @_; # Execution environment, title, options
Subroutine stackTraceAndExit: not called CC=4 SCAR=30.0
584 0 ▶ @_ >= 2 or confess "At least two parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 or confess("At least two parameters") 0 0 0
585  
586 0 my @t = $exec->stackTrace($title);
587 0 push $exec->out->@*, @t;
588 0 my $t = join "\n", @t;
589  
590 0 ▶ if (!defined $exec->suppressOutput)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $exec->suppressOutput) 0 0 0
591 0 ▶ {confess $t if $options{confess};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"confess"} 0 0 0
592 0 say STDERR $t;
593 }
594  
595 0 $exec->instructionPointer = undef; # Execution terminates as soon as undefined instruction is encountered
596 0 $t
597 }
598  
599 1 my $allocs = 0; my $allocsStacked = 0; # Normal arrays allocated by the user, stacked allocations are made to support subroutine calling, parameter passing, result returning.
600  
601 sub Zero::Emulator::Execution::allocMemory($$;$) #P Create the name of a new memory area.
602 0 ▶ {my ($exec, $name, $stacked) = @_; # Execution environment, name of allocation, stacked if true
Subroutine allocMemory: not called CC=2 SCAR=17.9
603 0 ▶ if ($stacked)
Branch: 1 branch
BranchTrueFalseTotal
if ($stacked) 0 0 0
604 0 {my $a = $allocsStacked--;
605 0 $exec->memory->{$a} = bless [], $name;
606 0 $exec->memoryType->{$a} = $name;
607 0 return $a
608 }
609 0 my $a = ++$allocs;
610 0 $exec->memory ->{$a} = bless [], $name;
611 0 $exec->memoryType->{$a} = $name;
612 0 $a
613 }
614  
615 sub Zero::Emulator::Execution::setMemoryType($$$) #P Set the type of a memory area - a name that can be used to confirm the validity of reads and writes to that array represented by that area.
616 0 ▶ {my ($exec, $area, $name) = @_; # Execution environment, area name, name of allocation
Subroutine setMemoryType: not called CC=2 SCAR=17.9
617 0 ▶ @_ == 3 or confess "Three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or confess("Three parameters") 0 0 0
618 0 $exec->memoryType->{$area} = $name;
619 0 $exec
620 }
621  
622 sub Zero::Emulator::Execution::notRead() #P Record the unused memory locations in the current stack frame.
623 0 ▶ {my ($exec) = @_; # Parameters
Subroutine notRead: not called CC=1 SCAR=6.9
624 0 my $area = $exec->stackArea;
625 # my @area = $memory{$area}->@*; # Memory in area
626 # my %r; # Location in stack frame=> instruction defining vasriable
627 # for my $a(keys @area)
628 # {if (my $I = $calls[-1]->variables->instructions->[$a])
629 # {$r{$a} = $I; # Number of instruction creating variable
630 # }
631 # }
632 #
633 # if (my $r = $read{$area}) # Locations in this area that have ben read
634 # {delete $r{$_} for keys %$r; # Delete locations that have been read from
635 # }
636 #
637 # $notRead{$area} = {%r} if keys %r; # Record not read
638 {}
639 0 }
640  
641 sub Zero::Emulator::Execution::rwWrite($$$) #P Observe write to memory.
642 0 ▶ {my ($exec, $area, $address) = @_; # Area in memory, address within area
Subroutine rwWrite: not called CC=3 SCAR=24.8
643 0 my $P = $exec->rw->{$area}{$address};
644 0 ▶ if (defined($P))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $P) 0 0 0
645 0 {my $M = $exec->memory->{$area}[$address]; # If the memory address is zero we will assume that it has been cleared rather than set.
646 0 ▶ if ($M)
Branch: 1 branch
BranchTrueFalseTotal
if ($M) 0 0 0
647 0 {my $Q = $exec->currentInstruction;
648 0 my $p = $P->contextString($exec, "Previous write");
649 0 my $q = $Q->contextString($exec, "Current write");
650 0 $exec->doubleWrite->{$p}{$q}++;
651 }
652 }
653 0 $exec->rw->{$area}{$address} = $exec->currentInstruction;
654 }
655  
656 sub Zero::Emulator::Execution::markAsRead($$$) #P Mark a memory address as having been read from.
657 0 ▶ {my ($exec, $area, $address) = @_; # Area in memory, address within area
Subroutine markAsRead: not called CC=2 SCAR=17.9
658 0 ▶ @_ == 3 or confess "Three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or confess("Three parameters") 0 0 0
659 0 delete $exec->rw->{$area}{$address}; # Clear last write operation
660 }
661  
662 sub Zero::Emulator::Execution::rwRead($$$) #P Observe read from memory.
663 0 ▶ {my ($exec, $area, $address) = @_; # Area in memory, address within area
Subroutine rwRead: not called CC=3 SCAR=24.8
664 0 ▶ @_ == 3 or confess "Three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or confess("Three parameters") 0 0 0
665 0 ▶ if (defined(my $a = $exec->rw->{$area}{$address})) # Can only read from locations that actually have something in them
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $a = $exec->rw->{$area}{$address})) 0 0 0
666 0 {$exec->markAsRead($area, $address); # Clear last write operation
667 0 $exec->read->{$area}{$address}++; # Track reads
668 }
669 }
670  
671 sub Zero::Emulator::Execution::left($$;$) #P Address a memory address.
672 0 ▶ {my ($exec, $ref, $extra) = @_; # Reference, an optional extra offset to add or subtract to the final memory address
Subroutine left: not called CC=12 SCAR=50.5
673 0 ▶ @_ == 2 or @_ == 3 or confess "Two or three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or @_ == 3 or confess("Two or three parameters") 0 0 0
Condition0%0/3
@_ == 2 or @_ == 3
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ == 2 or @_ == 3
@_ == 2 @_ == 3
Truth table0%0/3
A or B
A= @_ == 2B= @_ == 3
ABresult
1X1
011
000
674 0 ▶ ref($ref) =~ m((RefLeft|RefRight)\Z)
Branch: 1 branch
BranchTrueFalseTotal
ref($ref) =~ /(RefLeft|RefRight)\Z/u or confess('RefLeft or RefRight required, not: ' . Zero::Emulator::dump($ref)) 0 0 0
675 or confess "RefLeft or RefRight required, not: ".dump($ref);
676 0 my $r = $ref->address;
677 0 my $a = $r;
678 0 ▶ $a = \$r if isScalar $a; # Interpret constants as direct memory locations
Branch: 1 branch
BranchTrueFalseTotal
if isScalar $a 0 0 0
679 0 my $area = $ref->area;
680 0 ▶ my $x = $extra // 0; # Default is to use the address as supplied without locating a nearby address
Condition0%0/2
$extra // 0
l!l
00
MC/DC0%0/1
$extra // 0
$extra
Truth table0%0/2
A
A= $extra
Aresult
11
00
681 0 my $S = $exec->stackArea; # Current stack frame
682  
683 my sub invalid()
684 0 ▶ {my ($p) = @_; # Parameters
Subroutine invalid: not called CC=2 SCAR=17.9
685 0 my $i = $exec->currentInstruction;
686 0 my $l = $i->line;
687 0 my $f = $i->file;
688 0 ▶ $exec->stackTraceAndExit(
Branch: 1 branch
BranchTrueFalseTotal
defined $extra ? : 0 0 0
689 "Invalid left area: ".dump($area)
690 ." address: ".dump($a)
691 .(defined($extra) ? " + extra: ".dump($extra) : ''))
692 };
693  
694 0 my $M; # Memory address
695 0 ▶ if (isScalar $$a)
Branch: 2 branches
BranchTrueFalseTotal
if (isScalar $$a) { } 0 0 0
elsif (isScalar $$$a) { } 0 0 0
696 0 {$M = $$a+$x
697 }
698 elsif (isScalar $$$a)
699 0 {$exec->rwRead($S, $$$a);
700 0 $M = $exec->getMemory($S, $$$a, $ref->name)+$x
701 }
702 else
703 0 {invalid
704 }
705  
706 0 ▶ if ($M < 0) # Disallow negative addresses because they mean something special to Perl
Branch: 4 branches
BranchTrueFalseTotal
if ($M < 0) { } 0 0 0
elsif (not defined $area) { } 0 0 0
elsif (isScalar $area) { } 0 0 0
elsif (isScalar $$area) { } 0 0 0
707 0 {$exec->stackTraceAndExit("Negative address for area: "
708 .dump($area)
709 .", address: ".dump($a)
710 ." extra:".dump($x));
711 }
712 elsif (!defined($area)) # Current stack frame
713 0 {$exec->rwWrite( $S, $M);
714 0 return $exec->address($S, $M, $ref->name); # Stack frame
715 }
716 elsif (isScalar($area))
717 0 {$exec->rwWrite( $area, $M);
718 0 return $exec->address($area, $M, $ref->name) # Specified constant area
719 }
720 elsif (isScalar($$area))
721 0 {$exec->rwRead ( $S, $$area);
722 0 my $A = $exec->getMemory($S, $$area, $ref->name);
723 0 $exec->rwWrite( $A, $M);
724 0 return $exec->address($A, $M, $ref->name) # Indirect area
725 }
726 0 invalid;
727 }
728  
729 sub Zero::Emulator::Execution::leftSuppress($$) #P Indicate that a memory address has been read.
730 0 ▶ {my ($exec, $ref) = @_; # Execution environment, Reference
Subroutine leftSuppress: not called CC=10 SCAR=47.0
731 0 ▶ @_ == 2 or confess "Two parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or confess("Two parameters") 0 0 0
732 0 ▶ ref($ref) =~ m((RefLeft|RefRight)\Z) or confess "RefLeft or RefRight required";
Branch: 1 branch
BranchTrueFalseTotal
ref($ref) =~ /(RefLeft|RefRight)\Z/u or confess("RefLeft or RefRight required") 0 0 0
733 0 my $A = $ref->address;
734 0 my $area = $ref->area;
735 0 my $a = $A;
736 0 ▶ $a = \$A if isScalar $a; # Interpret constants as direct memory locations
Branch: 1 branch
BranchTrueFalseTotal
if isScalar $a 0 0 0
737  
738 0 my $m;
739 0 my $memory = $exec->memory;
740 0 my $stackArea = $exec->stackArea;
741  
742 0 ▶ if (isScalar $$a) # Direct
Branch: 2 branches
BranchTrueFalseTotal
if (isScalar $$a) { } 0 0 0
elsif (isScalar $$$a) { } 0 0 0
743 0 {$m = $$a;
744 }
745 elsif (isScalar $$$a) # Indirect
746 0 {$exec->rwRead ($stackArea, $$$a);
747 0 $m = $exec->get($stackArea, $$$a, $ref->name);
748 }
749  
750 0 ▶ if (defined($m))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $m) 0 0 0
751 0 ▶ {if (!defined($area)) # Current stack frame
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $area) { } 0 0 0
elsif (isScalar $area) { } 0 0 0
elsif (isScalar $$area) { } 0 0 0
752 0 {$exec->rwRead($stackArea, $m);
753 }
754 elsif (isScalar($area)) # Direct area
755 0 {$exec->rwRead($area, $m);
756 }
757 elsif (isScalar($$area)) # Indirect area
758 0 {$exec->rwRead( $stackArea, $area);
759 0 $exec->rwRead($exec->get($stackArea, $$area), $m);
760 }
761 }
762 }
763  
764 sub Zero::Emulator::Execution::right($$) #P Get a constant or a memory address.
765 0 ▶ {my ($exec, $ref) = @_; # Location, optional area
Subroutine right: not called CC=13 SCAR=52.0
766 0 ▶ @_ == 2 or confess "Two parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or confess("Two parameters") 0 0 0
767 0 ▶ ref($ref) =~ m((RefLeft|RefRight)\Z) or confess "RefLeft or RefRight required";
Branch: 1 branch
BranchTrueFalseTotal
ref($ref) =~ /(RefLeft|RefRight)\Z/u or confess("RefLeft or RefRight required") 0 0 0
768 0 my $a = $ref->address;
769 0 my $area = $ref->area;
770 0 my $stackArea = $exec->stackArea;
771 0 my $r; my $e = 0; my $tAddress; my $tArea;
772  
773 my sub invalid()
774 0 ▶ {my $i = $exec->currentInstruction;
Subroutine invalid: not called CC=1 SCAR=6.9
775 0 my $l = $i->line;
776 0 my $f = $i->file;
777 0 $exec->stackTraceAndExit(
778 "Invalid right area: ".dump($area)
779 ." address: " .dump($a)
780 ." stack: " .$exec->stackArea
781 ." error: " .dump($e)
782 ." target Area: ".dump($tArea)
783 ." address: " .dump($tAddress));
784 }
785  
786 0 ▶ if (isScalar($a)) # Constant
Branch: 1 branch
BranchTrueFalseTotal
if (isScalar $a) 0 0 0
787 {#rwRead($area//&stackArea, $a) if $a =~ m(\A\-?\d+\Z);
788 0 ▶ return $a if defined $a; # Attempting to read a address that has never been set is an error
Branch: 1 branch
BranchTrueFalseTotal
if defined $a 0 0 0
789 0 $exec->stackTraceAndExit("Undefined right address: ".dump($a));
790 }
791  
792 0 my $m;
793 0 my $memory = $exec->memory;
794  
795 0 ▶ if (isScalar($$a)) # Direct
Branch: 2 branches
BranchTrueFalseTotal
if (isScalar $$a) { } 0 0 0
elsif (isScalar $$$a) { } 0 0 0
796 0 {$m = $$a;
797 }
798 elsif (isScalar($$$a)) # Indirect
799 0 {$exec->rwRead($stackArea, $$$a);
800 0 $m = $exec->getMemory($stackArea, $$$a);
801 }
802 else
803 0 {$exec->stackTraceAndExit("Invalid right address: ".dump($a));
804 }
805 0 ▶ if (!defined($m))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $m) 0 0 0
806 0 {invalid;
807 }
808  
809 0 ▶ if (!defined($area))
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $area) { } 0 0 0
elsif (isScalar $area) { } 0 0 0
elsif (isScalar $$area) { } 0 0 0
810 0 {$exec->rwRead($stackArea, $m);
811 0 $r = $exec->getMemory($stackArea, $m); # Indirect from stack area
812 0 $e = 1; $tAddress = $m; $tArea = $exec->stackArea;
813 }
814 elsif (isScalar($area))
815 0 {$exec->rwRead( $area, $m);
816 0 $r = $exec->getMemory($area, $m, $ref->name); # Indirect from stack area
817 0 $e = 2; $tAddress = $m; $tArea = $area;
818 }
819 elsif (isScalar($$area))
820 0 {$exec->rwRead($exec->stackArea, $$area); # Mark the address holding the area as having been read
821 0 ▶ if (defined(my $j = $$memory{$exec->stackArea}[$$area]))
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $j = $$memory{$exec->stackArea}[$$area])) 0 0 0
822 0 {$exec->rwRead($j, $m);
823 0 $r = $$memory{$j}[$m]; # Indirect from indirect area
824 0 $r = $exec->getMemory($j, $m, $ref->name); # Indirect from stack area
825 0 $e = 9; $tAddress = $m; $tArea = $j;
826 }
827 }
828 0 ▶ invalid if !defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 0 0 0
829 0 $r
830 }
831  
832 sub Zero::Emulator::Execution::jumpOp($$$) #P Jump to the target address if the tested memory area if the condition is matched.
833 0 ▶ {my ($exec, $i, $check) = @_; # Execution environment, Instruction, check
Subroutine jumpOp: not called CC=3 SCAR=24.8
834 0 ▶ @_ == 3 or confess "Three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or confess("Three parameters") 0 0 0
835 0 ▶ $exec->instructionPointer = $i->number + $exec->right($i->target) if &$check; # Check if condition is met
Branch: 1 branch
BranchTrueFalseTotal
if &$check 0 0 0
836 }
837  
838 sub Zero::Emulator::Execution::assert1($$$) #P Assert true or false.
839 0 ▶ {my ($exec, $test, $sub) = @_; # Execution environment, Text of test, subroutine of test
Subroutine assert1: not called CC=3 SCAR=24.8
840 0 ▶ @_ == 3 or confess "Three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or confess("Three parameters") 0 0 0
841 0 my $i = $exec->currentInstruction;
842 0 my $a = $exec->right($i->source);
843 0 ▶ unless($sub->($a))
Branch: 1 branch
BranchTrueFalseTotal
unless (&$sub($a)) 0 0 0
844 0 {$exec->stackTraceAndExit("Assert$test $a failed");
845 }
846 }
847  
848 sub Zero::Emulator::Execution::assert($$$) #P Assert generically.
849 0 ▶ {my ($exec, $test, $sub) = @_; # Execution environment, Text of test, subroutine of test
Subroutine assert: not called CC=3 SCAR=24.8
850 0 ▶ @_ == 3 or confess "Three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or confess("Three parameters") 0 0 0
851 0 my $i = $exec->currentInstruction;
852 0 my ($a, $b) = ($exec->right($i->source), $exec->right($i->source2));
853 0 ▶ unless($sub->($a, $b))
Branch: 1 branch
BranchTrueFalseTotal
unless (&$sub($a, $b)) 0 0 0
854 0 {$exec->stackTraceAndExit("Assert $a $test $b failed");
855 }
856 }
857  
858 sub Zero::Emulator::Execution::assign($$$) #P Assign - check for pointless assignments.
859 0 ▶ {my ($exec, $target, $value) = @_; # Execution environment, Target of assign, value to assign
Subroutine assign: not called CC=12 SCAR=50.5
860 0 ▶ @_ == 3 or confess "Three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or confess("Three parameters") 0 0 0
861 0 ▶ ref($target) =~ m(Address)i or confess "Not an address: ".dump($target);
Branch: 1 branch
BranchTrueFalseTotal
ref($target) =~ /Address/iu or confess('Not an address: ' . Zero::Emulator::dump($target)) 0 0 0
862  
863 0 my $a = $target->area;
864 0 my $l = $target->address;
865 0 ▶ my $n = $target->name//'unknown';
Condition0%0/2
$target->name // "unknown"
l!l
00
MC/DC0%0/1
$target->name // "unknown"
$target->name
Truth table0%0/2
A
A= $target->name
Aresult
11
00
866  
867 0 my $N = $exec->memoryType->{$a};
868 0 ▶ if (!defined $N) # Check that the area has a name
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $N) 0 0 0
869 0 {$exec->stackTraceAndExit("No name for area $a\n".dump($exec->memory)." address:".dump($target), confess=>1);
870 }
871  
872 0 ▶ if ($n ne $N) # Check that the area name matches the one supplied in the address
Branch: 1 branch
BranchTrueFalseTotal
if ($n ne $N) 0 0 0
873 0 {$exec->stackTraceAndExit
874 ("Area name provided: '$n' does not match actual area name '$N' for area: $a");
875 }
876  
877 0 ▶ if (!defined($value)) # Check that the assign is not pointless
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value) { } 0 0 0
878 0 {$exec->stackTraceAndExit
879 ("Cannot assign an undefined value to area: $a ($n), address: $l");
880 }
881 else
882 0 {my $currently = $target->get($exec);
883 0 ▶ if (defined($currently) and $currently == $value)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $currently and $currently == $value) 0 0 0
Condition0%0/3
defined $currently and $currently == $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $currently and $currently == $value
defined $currently $currently == $value
Truth table0%0/3
A and B
A= defined $currentlyB= $currently == $value
ABresult
0X0
100
111
884 0 {$exec->pointlessAssign->{$exec->currentInstruction->number}++;
885 0 ▶ if ($exec->stopOnError)
Branch: 1 branch
BranchTrueFalseTotal
if ($exec->stopOnError) 0 0 0
886 0 {$exec->stackTraceAndExit("Pointless assign of: $currently to area: $a, at: $l");
887 }
888 }
889 }
890  
891 0 ▶ if (defined $exec->watch->{$a}{$l}) # Watch for specified changes
Branch: 1 branch
BranchTrueFalseTotal
if (defined $exec->watch->{$a}{$l}) 0 0 0
892 0 {my @s = $exec->stackTrace("Change at watched area: $a ($n), address: $l\n");
893 0 push @s, join ' ', "Current value:", $exec->memory->{$a}[$l];
894 0 push @s, join ' ', "New value:", $value;
895 0 push @s, $exec->dumpMemory;
896 0 ▶ say STDERR join "\n", @s unless $exec->suppressOutput;
Branch: 1 branch
BranchTrueFalseTotal
unless $exec->suppressOutput 0 0 0
897 0 push $exec->out->@*, @s;
898 }
899  
900 0 $exec->set($target, $value); # Actually do the assign
901 }
902  
903 sub Zero::Emulator::Execution::allocateSystemAreas($) #P Allocate system areas for a new stack frame.
904 0 ▶ {my ($exec) = @_; # Execution environment
Subroutine allocateSystemAreas: not called CC=2 SCAR=17.9
905 0 ▶ @_ == 1 or confess "One parameter";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter") 0 0 0
906 0 (stackArea=> $exec->allocMemory("stackArea", 1),
907 params=> $exec->allocMemory("params", 1),
908 return=> $exec->allocMemory("return", 1));
909 }
910  
911 sub Zero::Emulator::Execution::freeSystemAreas($$) #P Free system areas for the specified stack frame.
912 0 ▶ {my ($exec, $c) = @_; # Execution environment, stack frame
Subroutine freeSystemAreas: not called CC=3 SCAR=24.8
913 0 ▶ @_ == 2 or confess "Two parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or confess("Two parameters") 0 0 0
914 0 $exec->notRead; # Record unread memory locations in the current stack frame
915 0 delete $exec->memory->{$_} for $c->stackArea, $c->params, $c->return;
916 0 $allocsStacked -= 3;
917 }
918  
919 sub Zero::Emulator::Execution::currentInstruction($) #P Locate current instruction.
920 0 ▶ {my ($exec) = @_; # Execution environment
Subroutine currentInstruction: not called CC=2 SCAR=17.9
921 0 ▶ @_ == 1 or confess "One parameter";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter") 0 0 0
922 0 $exec->calls->[-1]->instruction;
923 }
924  
925 sub Zero::Emulator::Execution::createInitialStackEntry($) #P Create the initial stack frame.
926 0 ▶ {my ($exec) = @_; # Execution environment
Subroutine createInitialStackEntry: not called CC=2 SCAR=17.9
927  
928 0 ▶ push $exec->calls->@*, # Variables in initial stack frame
Branch: 1 branch
BranchTrueFalseTotal
$exec->block ? : 0 0 0
929 stackFrame(
930 $exec->block ? (variables=> $exec->block->variables) : (),
931 $exec->allocateSystemAreas);
932 0 $exec
933 }
934  
935 sub Zero::Emulator::Execution::checkArrayName($$$) #P Check the name of an array.
936 0 ▶ {my ($exec, $area, $name) = @_; # Execution environment, array, array name
Subroutine checkArrayName: not called CC=5 SCAR=34.0
937 0 ▶ @_ == 3 or confess "Three parameters";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 3 or confess("Three parameters") 0 0 0
938  
939 0 ▶ if (!defined($name)) # A name is required
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 0 0
940 0 {$exec->stackTraceAndExit("Array name required to size an array: ".dump($area));
941 0 return 0;
942 }
943  
944 0 my $Name = $exec->memoryType->{$area}; # Area has a name
945 0 ▶ if (!defined($Name))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Name) 0 0 0
946 0 {$exec->stackTraceAndExit("No name associated with array: $area");
947 0 return 0;
948 }
949  
950 0 ▶ if ($name ne $Name) # Name matches supplied name
Branch: 1 branch
BranchTrueFalseTotal
if ($name ne $Name) 0 0 0
951 0 {$exec->stackTraceAndExit("Wrong name: $name for array with name: $Name");
952 0 return 0;
953 }
954  
955 1
956 0 }
957  
958 sub Zero::Emulator::Execution::locateAreaElement($$$) #P Locate an element in an array.
959 0 ▶ {my ($exec, $area, $op) = @_; # Execution environment, array, operation
Subroutine locateAreaElement: not called CC=3 SCAR=24.8
960 0 my @a = $exec->memory->{$area}->@*;
961 0 for my $a(keys @a) # Check each element of the array
962 0 ▶ {if ($op->($a[$a]))
Branch: 1 branch
BranchTrueFalseTotal
if (&$op($a[$a])) 0 0 0
963 0 {return $a + 1;
964 }
965 }
966 0
967 0 }
968  
969 sub Zero::Emulator::Execution::countAreaElement($$$) #P Count the number of elements in array that meet some specification.
970 0 ▶ {my ($exec, $area, $op) = @_; # Execution environment, array, operation
Subroutine countAreaElement: not called CC=3 SCAR=24.8
971 0 my @a = $exec->memory->{$area}->@*;
972 0 my $n = 0;
973  
974 0 for my $a(keys @a) # Check each element of the array
975 0 ▶ {if ($op->($a[$a]))
Branch: 1 branch
BranchTrueFalseTotal
if (&$op($a[$a])) 0 0 0
976 0 {++$n;
977 }
978 }
979  
980 $n
981 0 }
982  
983 sub Zero::Emulator::Code::execute($%) #P Execute a block of code.
984 0 ▶ {my ($block, %options) = @_; # Block of code, execution options
Subroutine execute: not called CC=13 SCAR=52.0
985  
986 0 $block->assemble; # Assemble if necessary
987  
988 0 my $exec = execute(code=>$block, %options); # Execution environment
989  
990 my %instructions = # Instruction definitions
991 (add=> sub # Add the two source operands and store the result in the target
992 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
993 0 my $t = $exec->left($i->target);
994 0 my $a = $exec->right($i->source);
995 0 my $b = $exec->right($i->source2);
996 0 $exec->assign($t, $a + $b);
997 },
998  
999 subtract=> sub # Subtract the second source operand from the first and store the result in the target
1000 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1001 0 my $t = $exec->left($i->target);
1002 0 my $a = $exec->right($i->source);
1003 0 my $b = $exec->right($i->source2);
1004 0 $exec->assign($t, $a - $b);
1005 },
1006  
1007 assert=> sub # Assert
1008 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1009 0 $exec->stackTraceAndExit("Assert failed");
1010 },
1011  
1012 assertEq=> sub # Assert equals
1013 0 ▶ {$exec->assert("==", sub {my ($a, $b) = @_; $a == $b})
Subroutine __ANON__: not called CC=1 SCAR=6.9
1014 0 ▶ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1015  
1016 assertNe=> sub # Assert not equals
1017 0 ▶ {$exec->assert("!=", sub {my ($a, $b) = @_; $a != $b})
Subroutine __ANON__: not called CC=1 SCAR=6.9
1018 0 ▶ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1019  
1020 assertLt=> sub # Assert less than
1021 0 ▶ {$exec->assert("< ", sub {my ($a, $b) = @_; $a < $b})
Subroutine __ANON__: not called CC=1 SCAR=6.9
1022 0 ▶ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1023  
1024 assertLe=> sub # Assert less than or equal
1025 0 ▶ {$exec->assert("<=", sub {my ($a, $b) = @_; $a <= $b})
Subroutine __ANON__: not called CC=1 SCAR=6.9
1026 0 ▶ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1027  
1028 assertGt=> sub # Assert greater than
1029 0 ▶ {$exec->assert("> ", sub {my ($a, $b) = @_; $a > $b})
Subroutine __ANON__: not called CC=1 SCAR=6.9
1030 0 ▶ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1031  
1032 assertGe=> sub # Assert greater
1033 0 ▶ {$exec->assert(">=", sub {my ($a, $b) = @_; $a >= $b})
Subroutine __ANON__: not called CC=1 SCAR=6.9
1034 0 ▶ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1035  
1036 assertFalse=> sub # Assert false
1037 0 ▶ {$exec->assert1("False", sub {my ($a) = @_; $a == 0})
Subroutine __ANON__: not called CC=1 SCAR=6.9
1038 0 ▶ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1039  
1040 assertTrue=> sub # Assert true
1041 0 ▶ {$exec->assert1("True", sub {my ($a) = @_; $a != 0})
Subroutine __ANON__: not called CC=1 SCAR=6.9
1042 0 ▶ },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1043  
1044 array=> sub # Create a new memory area and write its number into the address named by the target operand
1045 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1046 0 my $a = $exec->allocMemory($i->source); # The reason for this allocation
1047 0 my $t = $exec->left($i->target);
1048  
1049 0 my $A = $exec->memory->{$a} = [];
1050 0 bless $A, $i->source; # Useful because dump then prints the type of each area for us
1051 0 $exec->assign($t, $a);
1052 0 $a
1053 },
1054  
1055 free=> sub # Free the memory area named by the source operand
1056 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1057 0 my $area = $exec->right($i->target); # Area
1058 0 my $name = $exec->right($i->source);
1059  
1060 0 ▶ if ($area !~ m(\A\d+\Z)) # User free-able area
Branch: 1 branch
BranchTrueFalseTotal
unless ($area =~ /\A\d+\Z/u) 0 0 0
1061 0 {$exec->stackTraceAndExit("Attempting to allocate non user area: $area");
1062 0 return;
1063 }
1064  
1065 0 $exec->checkArrayName($area, $name); # Check that the supplied array name matches what is actually in memory
1066  
1067 0 delete $exec->memory->{$area} # Free correctly identified area
1068 },
1069  
1070 arraySize=> sub # Get the size of the specified area
1071 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1072 0 my $size = $exec->left ($i->target); # Location to store size in
1073 0 my $area = $exec->right($i->source); # Location of area
1074 0 my $name = $i->source2; # Name of area
1075  
1076 0 $exec->checkArrayName($area, $name); # Check that the supplied array name matches what is actually in memory
1077  
1078 0 $exec->assign($size, scalar $exec->memory->{$area}->@*) # Size of area
1079 },
1080  
1081 arrayIndex=> sub # Place the 1 based index of the second source operand in the array referenced by the first source operand in the target location
1082 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1083 0 my $x = $exec->left ($i->target); # Location to store index in
1084 0 my $a = $exec->right($i->source); # Location of area
1085 0 my $e = $exec->right($i->source2); # Location of element
1086  
1087 0 ▶ $exec->assign($x, $exec->locateAreaElement($a, sub{$_[0] == $e})) # Index of element
Subroutine __ANON__: not called CC=1 SCAR=6.9
1088 },
1089  
1090 arrayCountGreater=> sub # Count the number of elements in the array specified by the first source operand that are greater than the element supplied by the second source operand and place the result in the target location
1091 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1092 0 my $x = $exec->left ($i->target); # Location to store index in
1093 0 my $a = $exec->right($i->source); # Location of area
1094 0 my $e = $exec->right($i->source2); # Location of element
1095  
1096 0 ▶ $exec->assign($x, $exec->countAreaElement($a, sub{$_[0] > $e})) # Index of element
Subroutine __ANON__: not called CC=1 SCAR=6.9
1097 },
1098  
1099 arrayCountLess=> sub # Count the number of elements in the array specified by the first source operand that are less than the element supplied by the second source operand and place the result in the target location
1100 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1101 0 my $x = $exec->left ($i->target); # Location to store index in
1102 0 my $a = $exec->right($i->source); # Location of area
1103 0 my $e = $exec->right($i->source2); # Location of element
1104  
1105 0 ▶ $exec->assign($x, $exec->countAreaElement($a, sub{$_[0] < $e})) # Index of element
Subroutine __ANON__: not called CC=1 SCAR=6.9
1106 },
1107  
1108 resize=> sub # Resize an array
1109 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1110 0 my $size = $exec->right($i->source); # New size
1111 0 my $area = $exec->right($i->target); # Array to resize
1112 0 ▶ $exec->stackTraceAndExit("Attempting to resize non user area: $area")
Branch: 1 branch
BranchTrueFalseTotal
unless $area =~ /\A\d+\Z/u 0 0 0
1113 unless $area =~ m(\A\d+\Z);
1114 0 $#{$exec->memory->{$area}} = $size-1;
1115 },
1116  
1117 call=> sub # Call a subroutine
1118 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1119 0 my $t = $i->target->address; # Subroutine to call
1120  
1121 0 ▶ if (isScalar($t))
Branch: 1 branch
BranchTrueFalseTotal
if (isScalar $t) { } 0 0 0
1122 0 {$exec->instructionPointer = $i->number + $t; # Relative call if we know where the subroutine is relative to the call instruction
1123 }
1124 else
1125 0 {$exec->instructionPointer = $t; # Absolute call
1126 }
1127 0 push $exec->calls->@*,
1128 stackFrame(target=>$block->code->[$exec->instructionPointer], # Create a new call stack entry
1129 instruction=>$i, variables=>$i->source->variables,
1130 $exec->allocateSystemAreas());
1131 },
1132  
1133 return=> sub # Return from a subroutine call via the call stack
1134 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=3 SCAR=24.8
1135 0 ▶ $exec->calls or $exec->stackTraceAndExit("The call stack is empty so I do not know where to return to");
Branch: 1 branch
BranchTrueFalseTotal
$exec->calls or $exec->stackTraceAndExit("The call stack is empty so I do not know where to return to") 0 0 0
1136 0 $exec->freeSystemAreas(pop $exec->calls->@*);
1137 0 ▶ if ($exec->calls)
Branch: 1 branch
BranchTrueFalseTotal
if ($exec->calls) { } 0 0 0
1138 0 {my $c = $exec->calls->[-1];
1139 0 $exec->instructionPointer = $c->instruction->number+1;
1140 }
1141 else
1142 0 {$exec->instructionPointer = undef;
1143 }
1144 },
1145  
1146 confess=> sub # Print the current call stack and stop
1147 0 ▶ {$exec->stackTraceAndExit("Confess at:", confess=>1);
Subroutine __ANON__: not called CC=1 SCAR=6.9
1148 },
1149  
1150 trace=> sub # Start/stop/change tracing status from a program. A trace writes out which instructions have been executed and how they affected memory
1151 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=3 SCAR=24.8
1152 0 ▶ my $s = $exec->right($i->source) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$exec->right($i->source) ? : 0 0 0
1153 0 $exec->trace = $s;
1154 0 my $m = join ' ', "Trace: $s";
1155 0 ▶ say STDERR $m unless $exec->suppressOutput;
Branch: 1 branch
BranchTrueFalseTotal
unless $exec->suppressOutput 0 0 0
1156 0 push $exec->out->@*, $m;
1157 },
1158  
1159 tracePoints=> sub # Start trace points
1160 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=3 SCAR=24.8
1161 0 ▶ my $s = $exec->right($i->source) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$exec->right($i->source) ? : 0 0 0
1162 0 $exec->tracePoints = $s;
1163 0 my $m = "TracePoints: $s";
1164 0 ▶ say STDERR $m unless $exec->suppressOutput;
Branch: 1 branch
BranchTrueFalseTotal
unless $exec->suppressOutput 0 0 0
1165 0 push $exec->out->@*, $m;
1166 },
1167  
1168 tracePoint=> sub # Trace point
1169 0 ▶ {return unless $exec->tracePoints;
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless $exec->tracePoints 0 0 0
1170 0 my @s = $exec->stackTrace("Trace");
1171 0 ▶ say STDERR join "\n", @s unless $exec->suppressOutput;
Branch: 1 branch
BranchTrueFalseTotal
unless $exec->suppressOutput 0 0 0
1172 0 push $exec->out->@*, @s;
1173 },
1174  
1175 dump=> sub # Dump memory
1176 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=4 SCAR=30.0
1177 0 my @m;
1178 0 ▶ if (ref($i->source) =~ m(Code)i)
Branch: 1 branch
BranchTrueFalseTotal
if (ref($i->source) =~ /Code/iu) 0 0 0
1179 0 {$i->source->($exec);
1180 0 return;
1181 }
1182  
1183 0 ▶ push @m, $i->source // "Memory dump";
Condition0%0/2
$i->source // "Memory dump"
l!l
00
MC/DC0%0/1
$i->source // "Memory dump"
$i->source
Truth table0%0/2
A
A= $i->source
Aresult
11
00
1184 0 push @m, $exec->dumpMemory;
1185 0 push @m, $exec->stackTrace;
1186  
1187 0 ▶ say STDERR join "\n", @m unless $exec->suppressOutput;
Branch: 1 branch
BranchTrueFalseTotal
unless $exec->suppressOutput 0 0 0
1188 0 push $exec->out->@*, @m;
1189 },
1190  
1191 dumpArray=> sub # Dump array in memory
1192 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=3 SCAR=24.8
1193 0 my @m;
1194 0 ▶ push @m, $i->source // "Array dump";
Condition0%0/2
$i->source // "Array dump"
l!l
00
MC/DC0%0/1
$i->source // "Array dump"
$i->source
Truth table0%0/2
A
A= $i->source
Aresult
11
00
1195 0 my $a = $exec->right($i->target);
1196 0 push @m, dump($exec->memory->{$a});
1197 0 push @m, $exec->stackTrace;
1198  
1199 0 ▶ say STDERR join "\n", @m unless $exec->suppressOutput;
Branch: 1 branch
BranchTrueFalseTotal
unless $exec->suppressOutput 0 0 0
1200 0 push $exec->out->@*, @m;
1201 },
1202  
1203 dec=> sub # Decrement locations in memory. The first address is incremented by 1, the next by two, etc.
1204 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1205 0 my $T = $exec->right($i->target);
1206 0 my $t = $exec->left($i->target);
1207 0 ▶ $exec->set($t, defined($t) ? $T-1 : -1);
Branch: 1 branch
BranchTrueFalseTotal
defined $t ? : 0 0 0
1208 },
1209  
1210 inc=> sub # Increment locations in memory. The first address is incremented by 1, the next by two, etc.
1211 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1212 0 my $T = $exec->right($i->target);
1213 0 my $t = $exec->left($i->target);
1214 0 ▶ $exec->set($t, defined($t) ? $T+1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $t ? : 0 0 0
1215 },
1216  
1217 jmp=> sub # Jump to the target address
1218 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1219 0 my $n = $i->number;
1220 0 my $r = $exec->right($i->target);
1221 0 $exec->instructionPointer = $n + $r;
1222 },
1223 # Conditional jumps
1224 0 ▶ jEq=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) == $exec->right($i->source2)})},
Subroutine __ANON__: not called CC=1 SCAR=6.9
1225 0 ▶ jNe=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) != $exec->right($i->source2)})},
Subroutine __ANON__: not called CC=1 SCAR=6.9
1226 0 ▶ jLe=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) <= $exec->right($i->source2)})},
Subroutine __ANON__: not called CC=1 SCAR=6.9
1227 0 ▶ jLt=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) < $exec->right($i->source2)})},
Subroutine __ANON__: not called CC=1 SCAR=6.9
1228 0 ▶ jGe=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) >= $exec->right($i->source2)})},
Subroutine __ANON__: not called CC=1 SCAR=6.9
1229 0 ▶ jGt=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) > $exec->right($i->source2)})},
Subroutine __ANON__: not called CC=1 SCAR=6.9
1230 0 ▶ jFalse=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) == 0})},
Subroutine __ANON__: not called CC=1 SCAR=6.9
1231 0 ▶ jTrue=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) != 0})},
Subroutine __ANON__: not called CC=1 SCAR=6.9
1232  
1233 label=> sub # Label - no operation
1234 0 ▶ {my ($i) = @_; # Instruction
Subroutine __ANON__: not called CC=1 SCAR=6.9
1235 },
1236  
1237 clear=> sub # Clear the first bytes of an area as specified by the target operand
1238 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1239 0 my $n = $exec->left($i->target);
1240 0 for my $a(0..$n->address-1)
1241 0 {my $A = $exec->left(RefLeft([$i->target->area, $a, q(aaa)]));
1242 0 $exec->set($A, 0);
1243 }
1244 },
1245  
1246 leAddress=> sub # Load the address component of a reference
1247 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1248 0 my $s = $exec->left($i->source);
1249 0 my $t = $exec->left($i->target);
1250 0 $exec->assign($t, $s->address);
1251 },
1252  
1253 leArea=> sub # Load the area component of an address
1254 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1255 0 my $s = $exec->left($i->source);
1256 0 my $t = $exec->left($i->target);
1257 0 $exec->assign($t, $s->area);
1258 },
1259  
1260 mov=> sub # Move data moves data from one part of memory to another - "set", by contrast, sets variables from constant values
1261 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1262 0 my $s = $exec->right($i->source);
1263 0 my $t = $exec->left($i->target);
1264 0 $exec->assign($t, $s);
1265 },
1266  
1267 not=> sub # Not in place
1268 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1269 0 my $s = $exec->right($i->source);
1270 0 my $t = $exec->left($i->target);
1271 0 $exec->assign($t, !$s);
1272 },
1273  
1274 paramsGet=> sub # Get a parameter from the previous parameter block - this means that we must always have two entries on the call stack - one representing the caller of the program, the second representing the current context of the program
1275 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1276 0 my $p = RefLeft([$exec->calls->[-2]->params, $i->source->address,
1277 'params']);
1278 0 my $t = $exec->left ($i->target);
1279 0 $exec->leftSuppress ($p); # The source will be read from
1280 0 my $s = $exec->left ($p); # The source has to be a left hand side because we want to address a memory area not get a constant
1281 0 $exec->assign($t, $s->get($exec));
1282 },
1283  
1284 paramsPut=> sub # Place a parameter in the current parameter block
1285 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1286 0 ▶ my $p = $i->target->area // $exec->calls->[-1]->params;
Condition0%0/3
$i->target->area // $exec->calls->[-1]->params
l!l&&r!l&&!r
000
MC/DC0%0/2
$i->target->area // $exec->calls->[-1]->params
$i->target->area $exec->calls->[-1]->params
Truth table0%0/3
A // B
A= $i->target->areaB= $exec->calls->[-1]->params
ABresult
1X1
011
000
1287 0 my $r = RefLeft [$p, $i->target->address, 'params'];
1288 0 $exec->leftSuppress ($r);
1289 0 my $t = $exec->left ($r);
1290 0 my $s = $exec->right($i->source);
1291 0 $exec->assign($t, $s);
1292 },
1293  
1294 returnGet=> sub # Get a word from the return area
1295 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1296 0 my $p = $exec->calls->[-1]->return; # Memory area
1297 0 my $t = $exec->left ($i->target);
1298 0 my $r = RefLeft([$p, \$i->source->address, 'return']); # The source will be read from
1299 0 $exec->leftSuppress($r); # The source will be read from
1300 0 my $s = $exec->left($r); # The source has to be a left hand side because we want to address a memory area not get a constant
1301 0 $exec->assign($t, $s->get($exec));
1302 },
1303  
1304 returnPut=> sub # Put a word into the return area
1305 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1306 0 my $p = $exec->calls->[-2]->return;
1307 0 my $t = $exec->left (RefLeft([$p, $i->target->address, q(return)]));
1308 0 my $s = $exec->right($i->source);
1309 0 $exec->assign($t, $s);
1310 },
1311  
1312 nop=> sub # No operation
1313 0 ▶ {my ($i) = @_; # Instruction
Subroutine __ANON__: not called CC=1 SCAR=6.9
1314 },
1315  
1316 out=> sub # Write source as output to an array of words
1317 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1318 0 my $t = $exec->right($i->source);
1319 0 ▶ lll $t unless $exec->suppressOutput;
Branch: 1 branch
BranchTrueFalseTotal
unless $exec->suppressOutput 0 0 0
1320 0 push $exec->out->@*, $t;
1321 },
1322  
1323 pop=> sub # Pop a value from the specified memory area if possible else confess
1324 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=4 SCAR=30.0
1325 0 my $s = $i->source;
1326 0 ▶ my $area = $i->source ? $exec->right($i->source) : &stackArea; # Memory area to pop
Branch: 1 branch
BranchTrueFalseTotal
$i->source ? : 0 0 0
1327 0 ▶ if (!defined($exec->memory->{$area}) or !$exec->memory->{$area}->@*) # Stack not pop-able
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $exec->memory->{$area} and @{$exec->memory->{$area}}) 0 0 0
Condition0%0/3
defined $exec->memory->{$area} and @{$exec->memory->{$area}}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $exec->memory->{$area} and @{$exec->memory->{$area}}
defined $exec->memory->{$area} @{$exec->memory->{$area}}
Truth table0%0/3
A and B
A= defined $exec->memory->{$area}B= @{$exec->memory->{$area}}
ABresult
0X0
100
111
1328 0 {$exec->stackTraceAndExit("Cannot pop area $area");
1329 }
1330 0 my $t = $exec->left($i->target);
1331 0 my $p = pop $exec->memory->{$area}->@*;
1332 0 $exec->assign($t, $p); # Pop from memory area into indicated memory address
1333 },
1334  
1335 push=> sub # Push a value onto the specified memory area
1336 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1337 0 ▶ if ($i->target)
Branch: 1 branch
BranchTrueFalseTotal
if ($i->target) { } 0 0 0
1338 0 {my $t = $exec->right($i->target);
1339 0 my $s = $exec->right($i->source);
1340 0 push $exec->memory->{$t}->@*, $s;
1341 }
1342 else
1343 0 {push $exec->memory->{&stackArea}->@*, right($i->source);
1344 }
1345 },
1346  
1347 shiftLeft=> sub # Shift left within an element
1348 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1349 0 $exec->leftSuppress ($i->target); # Make sure there something to shift
1350 0 my $t = $exec->left ($i->target);
1351 0 my $s = $exec->right($i->source);
1352 0 $exec->assign($t, $t->get($exec) << $s);
1353 },
1354  
1355 shiftRight=> sub # Shift right within an element
1356 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1357 0 $exec->leftSuppress ($i->target); # Make sure there something to shift
1358 0 my $t = $exec->left ($i->target);
1359 0 my $s = $exec->right($i->source);
1360 0 $exec->assign($t, $t->get($exec) >> $s);
1361 },
1362  
1363 shiftUp=> sub # Shift an element up in a memory area
1364 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1365 0 my $s = $exec->right($i->source);
1366 0 my $t = $exec->left($i->target);
1367 0 my $L = $exec->areaContent($t->area); # Length of area
1368 0 my $l = $t->address;
1369 0 for my $j(reverse 1..$L-$l)
1370 0 {my $s = $exec->left($i->target, $j-1);
1371 0 my $t = $exec->left($i->target, $j);
1372 0 $exec->assign($t, $s->get($exec));
1373 }
1374 0 $exec->assign($t, $s);
1375 },
1376  
1377 shiftDown=> sub # Shift an element down in a memory area
1378 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=2 SCAR=17.9
1379 0 my $s = $exec->left($i->source);
1380 0 my $t = $exec->left($i->target);
1381 0 my $L = $exec->areaContent($s->area); # Length of area
1382 0 my $l = $s->address;
1383 0 my $v = $s->get($exec);
1384 0 for my $j($l..$L-2) # Each element in specified range
1385 0 {my $S = $exec->left(RefLeft([$s->area, $j+1, $s->name]));
1386 0 my $T = $exec->left(RefLeft([$s->area, $j, $s->name]));
1387 0 $exec->assign($T, $S->get($exec));
1388 }
1389 0 pop $exec->memory->{$s->area}->@*;
1390 0 my $T = $exec->left($i->target);
1391 0 $exec->assign($T, $v);
1392 },
1393  
1394 tally=> sub # Tally instruction usage
1395 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1396 0 my $t = $exec->right($i->source);
1397 0 $exec->tally = $t;
1398 },
1399  
1400 watch=> sub # Watch a memory location for changes
1401 0 ▶ {my $i = $exec->currentInstruction;
Subroutine __ANON__: not called CC=1 SCAR=6.9
1402 0 my $t = $exec->left($i->target);
1403 0 $exec->watch->{$t->area}{$t->address}++;
1404 },
1405 0 );
1406  
1407 0 $exec->createInitialStackEntry; # Variables in initial stack frame
1408  
1409 my $mi = $options{maximumInstructionsToExecute} // # Prevent run away executions
1410 0 ▶ maximumInstructionsToExecute;
Condition0%0/3
$options{"maximumInstructionsToExecute"} // maximumInstructionsToExecute()
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"maximumInstructionsToExecute"} // maximumInstructionsToExecute()
$options{"maximumInstructionsToExecute"} maximumInstructionsToExecute()
Truth table0%0/3
A // B
A= $options{"maximumInstructionsToExecute"}B= maximumInstructionsToExecute()
ABresult
1X1
011
000
1411 0 for my $j(1..$mi) # Each instruction in the code until we hit an undefined instruction
1412 0 ▶ {last unless defined($exec->instructionPointer);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exec->instructionPointer 0 0 0
1413 0 my $i = $exec->block->code->[$exec->instructionPointer++]; # Current instruction
1414 0 $exec->calls->[-1]->instruction = $i;
1415 0 ▶ last unless $i;
Branch: 1 branch
BranchTrueFalseTotal
unless $i 0 0 0
1416 0 ▶ if (my $a = $i->action) # Action
Branch: 1 branch
BranchTrueFalseTotal
if (my $a = $i->action) 0 0 0
1417 {$exec->stackTraceAndExit(qq(Invalid instruction: "$a"\n))
1418 0 ▶ unless my $c = $instructions{$a};
Branch: 1 branch
BranchTrueFalseTotal
unless my $c = $instructions{$a} 0 0 0
1419  
1420 0 ▶ if ($a !~ m(\A(assert.*|label|tally|trace(Points?)?)\Z)) # Omit instructions that are not tally-able
Branch: 1 branch
BranchTrueFalseTotal
unless ($a =~ /\A(assert.*|label|tally|trace(Points?)?)\Z/u) 0 0 0
1421 0 ▶ {if (my $t = $exec->tally) # Tally instruction counts
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $exec->tally) 0 0 0
1422 0 {$exec->tallyCount++;
1423 0 $exec->tallyCounts->{$t}{$a}++;
1424 }
1425 0 $exec->counts->{$a}++; $exec->count++; # Execution instruction counts
1426 }
1427  
1428 0 $exec->lastAssignArea = $exec->lastAssignAddress = $exec->lastAssignValue = undef;
1429 0 $c->($i); # Execute instruction
1430  
1431 0 $exec->instructionCounts->{$i->number}++; # Execution count by actual instruction
1432  
1433 0 ++$i->executed;
1434  
1435 0 ▶ if ($exec->trace) # Trace changes to memory
Branch: 1 branch
BranchTrueFalseTotal
if ($exec->trace) 0 0 0
1436 0 {my $e = $exec->instructionCounts->{$i->number}; # Execution count for this instruction
1437 0 my $f = $exec->formatTrace;
1438 0 my $s = $exec->suppressOutput;
1439 0 my $a = $i->action;
1440 0 my $n = $i->number;
1441 0 my $m = sprintf "%4d %4d %4d %12s %20s", $j, $n, $e, $a, $f;
1442 0 ▶ $m .= sprintf " at %s line %d", $i->file, $i->line unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 0 0
1443 0 $m .= "\n";
1444 0 ▶ say STDERR $m unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 0 0
1445 0 push $exec->out->@*, $m;
1446 }
1447  
1448 }
1449 0 ▶ confess "Out of instructions after $j"if $j >= maximumInstructionsToExecute;
Branch: 1 branch
BranchTrueFalseTotal
if $j >= maximumInstructionsToExecute() 0 0 0
1450 }
1451  
1452 0 $exec->freeSystemAreas($exec->calls->[0]); # Free first stack frame
1453  
1454 0 $exec
1455 } # Execution results
1456  
1457 sub Zero::Emulator::Execution::formatTrace($) #P Describe last memory assignment.
1458 0 ▶ {my ($exec) = @_; # Execution
Subroutine formatTrace: not called CC=6 SCAR=37.4
1459 0 ▶ return "" unless defined(my $area = $exec->lastAssignArea);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $area = $exec->lastAssignArea) 0 0 0
1460 0 ▶ return "" unless defined(my $addr = $exec->lastAssignAddress);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $addr = $exec->lastAssignAddress) 0 0 0
1461 0 ▶ return "" unless defined(my $type = $exec->memoryType->{$area});
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $type = $exec->memoryType->{$area}) 0 0 0
1462 0 ▶ return "" unless defined(my $value = $exec->lastAssignValue);
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $value = $exec->lastAssignValue) 0 0 0
1463 0 my $B = $exec->lastAssignBefore;
1464 0 ▶ my $b = $B ? " was $B" : "";
Branch: 1 branch
BranchTrueFalseTotal
$B ? : 0 0 0
1465 0 sprintf "[%d, %d, %s] = %d$b", $area, $addr, $type, $value;
1466 }
1467  
1468 1 my $assembly; # The current assembly
1469  
1470 my sub label() # Next unique label
1471 0 ▶ {++$assembly->labelCounter;
Subroutine label: not called CC=1 SCAR=6.9
1472 }
1473  
1474 my sub setLabel(;$) # Set and return a label
1475 0 ▶ {my ($l) = @_; # Optional preset label
Subroutine setLabel: not called CC=2 SCAR=17.9
1476 0 ▶ $l //= label; # Create label if none supplied
Condition0%0/3
$l //= label
l!l&&r!l&&!r
000
MC/DC0%0/2
$l //= label
$l label
Truth table0%0/3
A //= B
A= $lB= label
ABresult
1X1
011
000
1477 0 Label($l); # Set label
1478 0 $l # Return (new) label
1479 }
1480  
1481 my sub xSource($) # Record a source argument
1482 0 ▶ {my ($s) = @_; # Source expression
Subroutine xSource: not called CC=1 SCAR=6.9
1483 0 (q(source), RefRight $s)
1484 }
1485  
1486 my sub xSource2($) # Record a source argument
1487 0 ▶ {my ($s) = @_; # Source expression
Subroutine xSource2: not called CC=1 SCAR=6.9
1488 0 (q(source2), RefRight $s)
1489 }
1490  
1491 my sub xTarget($) # Record a target argument
1492 0 ▶ {my ($t) = @_; # Target expression
Subroutine xTarget: not called CC=1 SCAR=6.9
1493 0 (q(target), RefLeft $t)
1494 }
1495  
1496 sub Start($) # Start the current assembly using the specified version of the Zero language. At the moment only version 1 works..
1497 0 ▶ {my ($version) = @_; # Version desired - at the moment only 1
Subroutine Start: not called CC=2 SCAR=17.9
Pod undocumented
1498 0 ▶ $version == 1 or confess "Version 1 is currently the only version available\n";
Branch: 1 branch
BranchTrueFalseTotal
$version == 1 or confess("Version 1 is currently the only version available\n") 0 0 0
1499 0 $allocs = $allocsStacked = 0;
1500 0 $assembly = Code; # The current assembly
1501 }
1502  
1503 sub Add($$;$) # Add the source locations together and store in the result in the target area.
1504 0 ▶ {my ($target, $s1, $s2) = @_ == 2 ? (&Var(), @_) : @_; # Target address, source one, source two
Subroutine Add: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 0 0
1505 0 $assembly->instruction(action=>"add", xTarget($target),
1506 xSource($s1), xSource2($s2));
1507 0 $target
1508 }
1509  
1510 sub Subtract($$;$) # Subtract the second source address from the first and store in the result in the target area.
1511 0 ▶ {my ($target, $s1, $s2) = @_ == 2 ? (&Var(), @_) : @_; # Target address, source one, source two
Subroutine Subtract: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 0 0
1512 0 $assembly->instruction(action=>"subtract", xTarget($target),
1513 xSource($s1), xSource2($s2));
1514 0 $target
1515 }
1516  
1517 sub Array($) # Create a new memory area and write its number into the address named by the target operand.
1518 0 ▶ {my ($name) = @_; # Name of allocation
Subroutine Array: not called CC=1 SCAR=6.9
Pod undocumented
1519 0 my $t = &Var();
1520 0 $assembly->instruction(action=>"array", target=>RefLeft($t), source=>$name);
1521 0 $t;
1522 }
1523  
1524 sub Free($$) # Free the memory area named by the target operand after confirming that it has the name specified on the source operand.
1525 0 ▶ {my ($target, $source) = @_; # Target area yielding the id of the area to be freed, source area yielding the name of the area to be freed
Subroutine Free: not called CC=1 SCAR=6.9
Pod undocumented
1526 0 $assembly->instruction(action=>"free", xTarget($target), xSource($source));
1527 }
1528  
1529 sub ArraySize($$) # The current size of an array.
1530 0 ▶ {my ($area, $name) = @_; # Location of area, name of area
Subroutine ArraySize: not called CC=1 SCAR=6.9
Pod undocumented
1531 0 my $t = &Var();
1532 0 $assembly->instruction(action=>"arraySize", # Target - location to place the size in, source - address of the area, source2 - the name of the area which cannot be taken from the area of the first source operand because that area name is the name of the area that contains the location of the area we wish to work on.
1533 target=>RefLeft($t), xSource($area), source2=>$name);
1534 0 $t
1535 }
1536  
1537 sub ArrayIndex($$;$) { # Find the 1 based index of the second source operand in the array referenced by the first source operand if it is present in the array else 0 into the target location. The business of returning -1 leads to the inferno of try catch.
1538 0 ▶ if (@_ == 2)
Subroutine ArrayIndex: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
1539 0 {my ($area, $element) = @_; # Area, element to find
1540 0 my $t = &Var();
1541 0 $assembly->instruction(action=>"arrayIndex",
1542 target=>RefLeft($t), xSource($area), xSource2($element));
1543 0 $t
1544 }
1545 else
1546 0 {my ($target, $area, $element) = @_; # Target, area, element to find
1547 0 $assembly->instruction(action=>"arrayIndex",
1548 xTarget($target), xSource($area), xSource2($element));
1549 }
1550 }
1551  
1552 sub ArrayCountLess($$;$) { # Count the number of elements in the array specified by the first source operand that are less than the element supplied by the second source operand and place the result in the target location.
1553 0 ▶ if (@_ == 2)
Subroutine ArrayCountLess: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
1554 0 {my ($area, $element) = @_; # Area, element to find
1555 0 my $t = &Var();
1556 0 $assembly->instruction(action=>"arrayCountLess",
1557 target=>RefLeft($t), xSource($area), xSource2($element));
1558 0 $t
1559 }
1560 else
1561 0 {my ($target, $area, $element) = @_; # Target, area, element to find
1562 0 $assembly->instruction(action=>"arrayCountLess",
1563 xTarget($target), xSource($area), xSource2($element));
1564 }
1565 }
1566  
1567 sub ArrayCountGreater($$;$) { # Count the number of elements in the array specified by the first source operand that are greater than the element supplied by the second source operand and place the result in the target location.
1568 0 ▶ if (@_ == 2)
Subroutine ArrayCountGreater: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
1569 0 {my ($area, $element) = @_; # Area, element to find
1570 0 my $t = &Var();
1571 0 $assembly->instruction(action=>"arrayCountGreater",
1572 target=>RefLeft($t), xSource($area), xSource2($element));
1573 0 $t
1574 }
1575 else
1576 0 {my ($target, $area, $element) = @_; # Target, area, element to find
1577 0 $assembly->instruction(action=>"arrayCountGreater",
1578 xTarget($target), xSource($area), xSource2($element));
1579 }
1580 }
1581  
1582 sub Call($) # Call the subroutine at the target address.
1583 0 ▶ {my ($p) = @_; # Procedure description
Subroutine Call: not called CC=1 SCAR=6.9
Pod undocumented
1584 0 $assembly->instruction(action=>"call",
1585 target=>RefLeft($p->target), source=>$p);
1586 }
1587  
1588 sub Confess() # Confess.
1589 0 ▶ {$assembly->instruction(action=>"confess");
Subroutine Confess: not called CC=1 SCAR=6.9
Pod undocumented
1590 }
1591  
1592 sub Dump(;$) # Dump memory.
1593 0 ▶ {my ($title) = @_; # Title
Subroutine Dump: not called CC=1 SCAR=6.9
Pod undocumented
1594 0 $assembly->instruction(action=>"dump", source=>$title);
1595 }
1596  
1597 sub DumpArray($;$) # Dump an array.
1598 0 ▶ {my ($target, $title) = @_; # Array to dump, title of dump
Subroutine DumpArray: not called CC=1 SCAR=6.9
Pod undocumented
1599 0 $assembly->instruction(action=>"dumpArray", target=>RefRight($target), source=>$title);
1600 }
1601  
1602 sub Trace($) # Trace.
1603 0 ▶ {my ($source) = @_; # Trace setting
Subroutine Trace: not called CC=1 SCAR=6.9
Pod undocumented
1604 0 $assembly->instruction(action=>"trace", xSource($source));
1605 }
1606  
1607 sub TracePoints($) # Enable trace points.
1608 0 ▶ {my ($source) = @_; # Trace points if true
Subroutine TracePoints: not called CC=1 SCAR=6.9
Pod undocumented
1609 0 $assembly->instruction(action=>"tracePoints", xSource($source));
1610 }
1611  
1612 sub TracePoint(%) #P Trace point - a point in the code where the flow of execution might change.
1613 0 ▶ {my (%options) = @_; # Parameters
Subroutine TracePoint: not called CC=1 SCAR=6.9
Pod undocumented
1614 0 $assembly->instruction(action=>"tracePoint", %options);
1615 }
1616  
1617 sub Dec($) # Decrement the target.
1618 0 ▶ {my ($target) = @_; # Target address
Subroutine Dec: not called CC=1 SCAR=6.9
Pod undocumented
1619 0 $assembly->instruction(action=>"dec", xTarget($target))
1620 }
1621  
1622 sub Inc($) # Increment the target.
1623 0 ▶ {my ($target) = @_; # Target address
Subroutine Inc: not called CC=1 SCAR=6.9
Pod undocumented
1624 0 $assembly->instruction(action=>"inc", xTarget($target))
1625 }
1626  
1627 sub Jmp($) # Jump to a label.
1628 0 ▶ {my ($target) = @_; # Target address
Subroutine Jmp: not called CC=1 SCAR=6.9
Pod undocumented
1629 0 $assembly->instruction(action=>"jmp", xTarget($target));
1630 }
1631  
1632 sub Jle($$$) # Jump to a target label if the first source field is less than or equal to the second source field.
1633 0 ▶ {my ($target, $source, $source2) = @_; # Target label, source to test
Subroutine Jle: not called CC=1 SCAR=6.9
Pod undocumented
1634 0 $assembly->instruction(action=>"jLe",
1635 xTarget($target), xSource($source), xSource2($source2));
1636 }
1637  
1638 sub Jlt($$$) # Jump to a target label if the first source field is less than the second source field.
1639 0 ▶ {my ($target, $source, $source2) = @_; # Target label, source to test
Subroutine Jlt: not called CC=1 SCAR=6.9
Pod undocumented
1640 0 $assembly->instruction(action=>"jLt",
1641 xTarget($target), xSource($source), xSource2($source2));
1642 }
1643  
1644 sub Jge($$$) # Jump to a target label if the first source field is greater than or equal to the second source field.
1645 0 ▶ {my ($target, $source, $source2) = @_; # Target label, source to test
Subroutine Jge: not called CC=1 SCAR=6.9
Pod undocumented
1646 0 $assembly->instruction(action=>"jGe",
1647 xTarget($target), xSource($source), xSource2($source2));
1648 }
1649  
1650 sub Jgt($$$) # Jump to a target label if the first source field is greater than the second source field.
1651 0 ▶ {my ($target, $source, $source2) = @_; # Target label, source to test
Subroutine Jgt: not called CC=1 SCAR=6.9
Pod undocumented
1652 0 $assembly->instruction(action=>"jGt",
1653 xTarget($target), xSource($source), xSource2($source2));
1654 }
1655  
1656 sub Jeq($$$) # Jump to a target label if the first source field is equal to the second source field.
1657 0 ▶ {my ($target, $source, $source2) = @_; # Target label, source to test
Subroutine Jeq: not called CC=1 SCAR=6.9
Pod undocumented
1658 0 $assembly->instruction(action=>"jEq",
1659 xTarget($target), xSource($source), xSource2($source2));
1660 }
1661  
1662 sub Jne($$$) # Jump to a target label if the first source field is not equal to the second source field.
1663 0 ▶ {my ($target, $source, $source2) = @_; # Target label, source to test
Subroutine Jne: not called CC=1 SCAR=6.9
Pod undocumented
1664 0 $assembly->instruction(action=>"jNe",
1665 xTarget($target), xSource($source), xSource2($source2));
1666 }
1667  
1668 sub JFalse($$) # Jump to a target label if the first source field is equal to zero.
1669 0 ▶ {my ($target, $source) = @_; # Target label, source to test
Subroutine JFalse: not called CC=1 SCAR=6.9
Pod undocumented
1670 0 $assembly->instruction(action=>"jFalse", xTarget($target), xSource($source));
1671 }
1672  
1673 sub JTrue($$) # Jump to a target label if the first source field is not equal to zero.
1674 0 ▶ {my ($target, $source) = @_; # Target label, source to test
Subroutine JTrue: not called CC=1 SCAR=6.9
Pod undocumented
1675 0 $assembly->instruction(action=>"jTrue", xTarget($target), xSource($source));
1676 }
1677  
1678 sub Label($) # Create a label..
1679 0 ▶ {my ($source) = @_; # Name of label
Subroutine Label: not called CC=1 SCAR=6.9
Pod undocumented
1680 0 $assembly->instruction(action=>"label", xSource($source));
1681 }
1682  
1683 sub Clear($) # Clear the first bytes of an area. The area is specified by the first element of the address, the number of locations to clear is specified by the second element of the target address..
1684 0 ▶ {my ($target) = @_; # Target address, source address
Subroutine Clear: not called CC=1 SCAR=6.9
Pod undocumented
1685 0 $assembly->instruction(action=>"clear", xTarget($target));
1686 }
1687  
1688 sub LeAddress($;$) { # Load the address component.
1689 0 ▶ if (@_ == 1)
Subroutine LeAddress: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@= = 2) { } 0 0 0
1690 0 {my ($source) = @_; # Target address, source address
1691 0 my $t = &Var();
1692 0 $assembly->instruction(action=>"leAddress",
1693 target=>RefLeft($t), xSource($source));
1694 0 return $t;
1695 }
1696 elsif (@ == 2)
1697 0 {my ($target, $source) = @_; # Target address, source address
1698 0 $assembly->instruction(action=>"leAddress",
1699 xTarget($target), xSource($source));
1700 }
1701 else
1702 0 {confess "One or two parameters required";
1703 }
1704 }
1705  
1706 sub LeArea($;$) { # Load the address component.
1707 0 ▶ if (@_ == 1)
Subroutine LeArea: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@= = 2) { } 0 0 0
1708 0 {my ($source) = @_; # Target address, source address
1709 0 my $t = &Var();
1710 0 $assembly->instruction(action=>"leArea",
1711 target=>RefLeft($t), xSource($source));
1712 0 return $t;
1713 }
1714 elsif (@ == 2)
1715 0 {my ($target, $source) = @_; # Target address, source address
1716 0 $assembly->instruction(action=>"leArea",
1717 xTarget($target), xSource($source));
1718 }
1719 else
1720 0 {confess "One or two parameters required";
1721 }
1722 }
1723  
1724 sub Mov($;$) { # Copy a constant or memory address to the target address.
1725 0 ▶ if (@_ == 1)
Subroutine Mov: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@= = 2) { } 0 0 0
1726 0 {my ($source) = @_; # Target address, source address
1727 0 my $t = &Var();
1728 0 $assembly->instruction(action=>"mov", target=>RefLeft($t),xSource($source));
1729 0 return $t;
1730 }
1731 elsif (@ == 2)
1732 0 {my ($target, $source) = @_; # Target address, source address
1733 0 $assembly->instruction(action=>"mov", xTarget($target), xSource($source));
1734 }
1735 else
1736 0 {confess "One or two parameters required for mov";
1737 }
1738 }
1739  
1740 sub Not($) { # Move and not.
1741 0 ▶ if (@_ == 1)
Subroutine Not: not called CC=3 SCAR=24.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@= = 2) { } 0 0 0
1742 0 {my ($source) = @_; # Target address, source address
1743 0 my $t = &Var();
1744 0 $assembly->instruction(action=>"not", target=>RefLeft($t),xSource($source));
1745 0 return $t;
1746 }
1747 elsif (@ == 2)
1748 0 {my ($target, $source) = @_; # Target address, source address
1749 0 $assembly->instruction(action=>"not", xTarget($target), xSource($source));
1750 }
1751 else
1752 0 {confess "One or two parameters required for not";
1753 }
1754 }
1755  
1756 sub Nop() # Do nothing (but do it well!).
1757 0 ▶ {$assembly->instruction(action=>"nop");
Subroutine Nop: not called CC=1 SCAR=6.9
Pod undocumented
1758 }
1759  
1760 sub Out($) # Write memory contents to out.
1761 0 ▶ {my ($source) = @_; # Either a scalar constant or memory address to output
Subroutine Out: not called CC=1 SCAR=6.9
Pod undocumented
1762 0 $assembly->instruction(action=>"out", xSource($source))
1763 }
1764  
1765 sub Procedure($$) # Define a procedure.
1766 0 ▶ {my ($name, $source) = @_; # Name of procedure, source code as a subroutine# $assembly->instruction(action=>"procedure", target=>$target, source=>$source);
Subroutine Procedure: not called CC=3 SCAR=24.8
Pod undocumented
1767 0 ▶ if ($name and my $n = $assembly->procedures->{$name}) # Reuse existing named procedure
Branch: 1 branch
BranchTrueFalseTotal
if ($name and my $n = $assembly->procedures->{$name}) 0 0 0
Condition0%0/3
$name and my $n = $assembly->procedures->{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
$name and my $n = $assembly->procedures->{$name}
$name my $n = $assembly->procedures->{$name}
Truth table0%0/3
A and B
A= $nameB= my $n = $assembly->procedures->{$name}
ABresult
0X0
100
111
1768 0 {return $n;
1769 }
1770  
1771 0 Jmp(my $end = label); # Jump over the code of the procedure body
1772 0 my $start = setLabel;
1773 0 my $p = procedure($start); # Procedure description
1774 0 my $save_registers = $assembly->variables;
1775 0 $assembly->variables = $p->variables;
1776 0 &$source($p); # Code of procedure called with start label as a parameter
1777 0 &Return;
1778 0 $assembly->variables = $save_registers;
1779  
1780 0 setLabel $end;
1781 0 $assembly->procedures->{$name} = $p; # Return the start of the procedure
1782 }
1783  
1784 sub ParamsGet($;$) { # Get a word from the parameters in the previous frame and store it in the current frame.
1785 0 ▶ if (@_ == 1)
Subroutine ParamsGet: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@_ == 2) { } 0 0 0
1786 0 {my ($source) = @_; # Memory address to place parameter in, parameter number
1787 0 my $p = &Var();
1788 0 $assembly->instruction(action=>"paramsGet",
1789 target=>RefLeft($p), xSource($source));
1790 0 return $p;
1791 }
1792 elsif (@_ == 2)
1793 0 {my ($target, $source) = @_; # Memory address to place parameter in, parameter number
1794 0 $assembly->instruction(action=>"paramsGet",
1795 xTarget($target), xSource($source));
1796 }
1797 else
1798 0 {confess "One or two parameters required";
1799 }
1800 }
1801  
1802 sub ParamsPut($$) # Put a word into the parameters list to make it visible in a called procedure.
1803 0 ▶ {my ($target, $source) = @_; # Parameter number, address to fetch parameter from
Subroutine ParamsPut: not called CC=1 SCAR=6.9
Pod undocumented
1804 0 $assembly->instruction(action=>"paramsPut",
1805 xTarget($target), xSource($source));
1806 }
1807  
1808 sub Return() # Return from a procedure via the call stack.
1809 0 ▶ {$assembly->instruction(action=>"return");
Subroutine Return: not called CC=1 SCAR=6.9
Pod undocumented
1810 }
1811  
1812 sub ReturnGet($;$) { # Get a word from the return area and save it.
1813 0 ▶ if (@_ == 1) # Create a variable
Subroutine ReturnGet: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@_ == 2) { } 0 0 0
1814 0 {my ($source) = @_; # Memory address to place return value in, return value to get
1815 0 my $p = &Var();
1816 0 $assembly->instruction(action=>"returnGet",
1817 target=>RefLeft($p), xSource($source));
1818 0 return $p;
1819 }
1820 elsif (@_ == 2)
1821 0 {my ($target, $source) = @_; # Memory address to place return value in, return value to get
1822 0 $assembly->instruction(action=>"returnGet",
1823 xTarget($target), xSource($source));
1824 }
1825 else
1826 0 {confess "One or two parameters required";
1827 }
1828 }
1829  
1830 sub ReturnPut($$) # Put a word into the return area.
1831 0 ▶ {my ($target, $source) = @_; # Offset in return area to write to, memory address whose contents are to be placed in the return area
Subroutine ReturnPut: not called CC=1 SCAR=6.9
Pod undocumented
1832 0 $assembly->instruction(action=>"returnPut",
1833 xTarget($target), xSource($source));
1834 }
1835  
1836 sub Resize($$) # Resize the target area to the source size.
1837 0 ▶ {my ($target, $source) = @_; # Target address, source address
Subroutine Resize: not called CC=1 SCAR=6.9
Pod undocumented
1838 0 $assembly->instruction(action=>"resize", xTarget($target), xSource($source));
1839 }
1840  
1841 sub Pop(;$$) { # Pop the memory area specified by the source operand into the memory address specified by the target operand.
1842 0 ▶ if (@_ == 0) # Pop current stack frame into a local variable
Subroutine Pop: not called CC=4 SCAR=30.0
Pod undocumented
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 0) { } 0 0 0
elsif (@_ == 1) { } 0 0 0
elsif (@_ == 2) { } 0 0 0
1843 0 {my $p = &Var();
1844 0 my $i = $assembly->instruction(action=>"pop", target=>RefLeft($p));
1845 0 $i->source = undef;
1846 0 return $p;
1847 }
1848 elsif (@_ == 1) # Pop indicated area into a local variable
1849 0 {my ($source) = @_; # Memory address to place return value in, return value to get
1850 0 my $p = &Var();
1851 0 $assembly->instruction(action=>"pop", target=>RefLeft($p),xSource($source));
1852 0 return $p;
1853 }
1854 elsif (@_ == 2)
1855 0 {my ($target, $source) = @_; # Pop indicated area into target address
1856 0 $assembly->instruction(action=>"pop", xTarget($target), xSource($source));
1857 }
1858 else
1859 0 {confess "Zero, One or two parameters required";
1860 }
1861 }
1862  
1863 sub Push($;$) { # Push the value in the current stack frame specified by the source operand onto the memory area identified by the target operand..
1864 0 ▶ if (@_ == 1)
Subroutine Push: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@_ == 2) { } 0 0 0
1865 0 {my ($source) = @_; # Push a value onto the current stack frame
1866 0 $assembly->instruction(action=>"push", xSource($source));
1867 }
1868 elsif (@_ == 2) # Push a value onto the specified memory area
1869 0 {my ($target, $source) = @_; # Memory area to push to, memory containing value to push
1870 0 $assembly->instruction(action=>"push", xTarget($target), xSource($source));
1871 }
1872 else
1873 0 {confess "One or two parameters required";
1874 }
1875 }
1876  
1877 sub ShiftLeft($;$) { # Shift left within an element.
1878 0 ▶ my ($target, $source) = @_; # Target to shift, amount to shift
Subroutine ShiftLeft: not called CC=1 SCAR=6.9
Pod undocumented
1879 0 $assembly->instruction(action=>"shiftLeft",
1880 xTarget($target), xSource($source));
1881 0 $target
1882 }
1883  
1884 sub ShiftRight($;$) { # Shift right with an element.
1885 0 ▶ my ($target, $source) = @_; # Target to shift, amount to shift
Subroutine ShiftRight: not called CC=1 SCAR=6.9
Pod undocumented
1886 0 $assembly->instruction(action=>"shiftRight",
1887 xTarget($target), xSource($source));
1888 0 $target
1889 }
1890  
1891 sub ShiftUp($;$) # Shift an element up one in an area.
1892 0 ▶ {my ($target, $source) = @_; # Target to shift, amount to shift
Subroutine ShiftUp: not called CC=1 SCAR=6.9
Pod undocumented
1893 0 $assembly->instruction(action=>"shiftUp",
1894 xTarget($target), xSource($source));
1895 0 $target
1896 }
1897  
1898 sub ShiftDown($;$) { # Shift an element down one in an area.
1899 0 ▶ if (@_ == 1) # Create a variable
Subroutine ShiftDown: not called CC=3 SCAR=24.8
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@_ == 2) { } 0 0 0
1900 0 {my ($source) = @_; # Memory address to place return value in, return value to get
1901 0 my $p = &Var();
1902 0 $assembly->instruction(action=>"shiftDown",
1903 target=>RefLeft($p), xSource($source));
1904 0 return $p;
1905 }
1906 elsif (@_ == 2)
1907 0 {my ($target, $source) = @_; # Memory address to place return value in, return value to get
1908 0 $assembly->instruction(action=>"shiftDown",
1909 xTarget($target), xSource($source));
1910 0 confess "Needs work";
1911 0 return $target;
1912 }
1913 else
1914 0 {confess "One or two parameters required";
1915 }
1916 }
1917  
1918 sub Watch($) # Shift an element down one in an area.
1919 0 ▶ {my ($target) = @_; # Memory address to watch
Subroutine Watch: not called CC=1 SCAR=6.9
Pod undocumented
1920 0 $assembly->instruction(action=>"watch", xTarget($target));
1921 }
1922  
1923 sub Tally($) # Counts instructions when enabled.
1924 0 ▶ {my ($source) = @_; # Tally instructions when true
Subroutine Tally: not called CC=1 SCAR=6.9
Pod undocumented
1925 0 $assembly->instruction(action=>"tally", xSource($source));
1926 }
1927  
1928 sub Then(&) # Then block.
1929 0 ▶ {my ($t) = @_; # Then block subroutine
Subroutine Then: not called CC=2 SCAR=17.9
Pod undocumented
1930 0 ▶ @_ == 1 or confess "One parameter";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter") 0 0 0
1931 0 (then=> $t)
1932 }
1933  
1934 sub Else(&) # Else block.
1935 0 ▶ {my ($e) = @_; # Else block subroutine
Subroutine Else: not called CC=2 SCAR=17.9
Pod undocumented
1936 0 ▶ @_ == 1 or confess "One parameter";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter") 0 0 0
1937 0 (else=> $e)
1938 }
1939  
1940 sub Ifx($$$%) #P Execute then or else clause depending on whether two memory locations are equal..
1941 0 ▶ {my ($cmp, $a, $b, %options) = @_; # Comparison, first memory address, second memory address, then block, else block
Subroutine Ifx: not called CC=3 SCAR=24.8
Pod undocumented
1942 0 ▶ confess "Then required" unless $options{then};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"then"} 0 0 0
1943 0 ▶ if ($options{else})
Branch: 1 branch
BranchTrueFalseTotal
if ($options{'else'}) { } 0 0 0
1944 0 {my $else = label;
1945 0 my $end = label;
1946 0 &$cmp($else, $a, $b);
1947 0 TracePoint level=>2;
1948 0 &{$options{then}};
1949 0 Jmp $end;
1950 0 setLabel($else);
1951 0 TracePoint level=>2;
1952 0 &{$options{else}};
1953 0 setLabel($end);
1954 }
1955 else
1956 0 {my $end = label;
1957 0 &$cmp($end, $a, $b);
1958 0 &{$options{then}};
1959 0 setLabel($end);
1960 }
1961 }
1962  
1963 sub IfFalse($%) # Execute then clause if the specified memory address is zero representing false.
1964 0 ▶ {my ($a, %options) = @_; # Memory address, then block, else block
Subroutine IfFalse: not called CC=1 SCAR=6.9
Pod undocumented
1965 0 Ifx(\&Jne, $a, 0, %options);
1966 }
1967  
1968 sub IfTrue($%) # Execute then clause if the specified memory address is not zero representing true.
1969 0 ▶ {my ($a, %options) = @_; # Memory address, then block, else block
Subroutine IfTrue: not called CC=1 SCAR=6.9
Pod undocumented
1970 0 Ifx(\&Jeq, $a, 0, %options);
1971 }
1972  
1973 sub IfEq($$%) # Execute then or else clause depending on whether two memory locations are equal..
1974 0 ▶ {my ($a, $b, %options) = @_; # First memory address, second memory address, then block, else block
Subroutine IfEq: not called CC=1 SCAR=6.9
Pod undocumented
1975 0 Ifx(\&Jne, $a, $b, %options);
1976 }
1977  
1978 sub IfNe($$%) # Execute then or else clause depending on whether two memory locations are not equal..
1979 0 ▶ {my ($a, $b, %options) = @_; # First memory address, second memory address, then block, else block
Subroutine IfNe: not called CC=1 SCAR=6.9
Pod undocumented
1980 0 Ifx(\&Jeq, $a, $b, %options);
1981 }
1982  
1983 sub IfLt($$%) # Execute then or else clause depending on whether two memory locations are less than..
1984 0 ▶ {my ($a, $b, %options) = @_; # First memory address, second memory address, then block, else block
Subroutine IfLt: not called CC=1 SCAR=6.9
Pod undocumented
1985 0 Ifx(\&Jge, $a, $b, %options);
1986 }
1987  
1988 sub IfLe($$%) # Execute then or else clause depending on whether two memory locations are less than or equal..
1989 0 ▶ {my ($a, $b, %options) = @_; # First memory address, second memory address, then block, else block
Subroutine IfLe: not called CC=1 SCAR=6.9
Pod undocumented
1990 0 Ifx(\&Jgt, $a, $b, %options);
1991 }
1992  
1993 sub IfGt($$%) # Execute then or else clause depending on whether two memory locations are greater than..
1994 0 ▶ {my ($a, $b, %options) = @_; # First memory address, second memory address, then block, else block
Subroutine IfGt: not called CC=1 SCAR=6.9
Pod undocumented
1995 0 Ifx(\&Jle, $a, $b, %options);
1996 }
1997  
1998 sub IfGe($$%) # Execute then or else clause depending on whether two memory locations are greater than or equal..
1999 0 ▶ {my ($a, $b, %options) = @_; # First memory address, second memory address, then block, else block
Subroutine IfGe: not called CC=1 SCAR=6.9
Pod undocumented
2000 0 Ifx(\&Jlt, $a, $b, %options);
2001 }
2002  
2003 sub Assert1($$) #P Assert operation.
2004 0 ▶ {my ($op, $a) = @_; # Operation, Source operand
Subroutine Assert1: not called CC=1 SCAR=6.9
Pod undocumented
2005 0 $assembly->instruction(action=>"assert$op", xSource($a), level=>2);
2006 }
2007  
2008 sub Assert2($$$) #P Assert operation.
2009 0 ▶ {my ($op, $a, $b) = @_; # Operation, First memory address, second memory address
Subroutine Assert2: not called CC=1 SCAR=6.9
Pod undocumented
2010 0 $assembly->instruction(action=>"assert$op",
2011 xSource($a), xSource2($b), level=>2);
2012 }
2013  
2014 sub Assert(%) # Assert regardless.
2015 0 ▶ {my (%options) = @_; # Options
Subroutine Assert: not called CC=1 SCAR=6.9
Pod undocumented
2016 0 $assembly->instruction(action=>"assert");
2017 }
2018  
2019 sub AssertEq($$%) # Assert two memory locations are equal..
2020 0 ▶ {my ($a, $b, %options) = @_; # First memory address, second memory address
Subroutine AssertEq: not called CC=1 SCAR=6.9
Pod undocumented
2021 0 Assert2("Eq", $a, $b);
2022 }
2023  
2024 sub AssertNe($$%) # Assert two memory locations are not equal..
2025 0 ▶ {my ($a, $b, %options) = @_; # First memory address, second memory address
Subroutine AssertNe: not called CC=1 SCAR=6.9
Pod undocumented
2026 0 Assert2("Ne", $a, $b);
2027 }
2028  
2029 sub AssertLt($$%) # Assert two memory locations are less than..
2030 0 ▶ {my ($a, $b, %options) = @_; # First memory address, second memory address
Subroutine AssertLt: not called CC=1 SCAR=6.9
Pod undocumented
2031 0 Assert2("Lt", $a, $b);
2032 }
2033  
2034 sub AssertLe($$%) # Assert two memory locations are less than or equal..
2035 0 ▶ {my ($a, $b, %options) = @_; # First memory address, second memory address
Subroutine AssertLe: not called CC=1 SCAR=6.9
Pod undocumented
2036 0 Assert2("Le", $a, $b);
2037 }
2038  
2039 sub AssertGt($$%) # Assert two memory locations are greater than..
2040 0 ▶ {my ($a, $b, %options) = @_; # First memory address, second memory address
Subroutine AssertGt: not called CC=1 SCAR=6.9
Pod undocumented
2041 0 Assert2("Gt", $a, $b);
2042 }
2043  
2044 sub AssertGe($$%) # Assert are greater than or equal..
2045 0 ▶ {my ($a, $b, %options) = @_; # First memory address, second memory address
Subroutine AssertGe: not called CC=1 SCAR=6.9
Pod undocumented
2046 0 Assert2("Ge", $a, $b);
2047 }
2048  
2049 sub AssertTrue($%) # Assert true.
2050 0 ▶ {my ($a, %options) = @_; # Source operand
Subroutine AssertTrue: not called CC=1 SCAR=6.9
Pod undocumented
2051 0 Assert1("True", $a);
2052 }
2053  
2054 sub AssertFalse($%) # Assert false.
2055 0 ▶ {my ($a, %options) = @_; # Source operand
Subroutine AssertFalse: not called CC=1 SCAR=6.9
Pod undocumented
2056 0 Assert1("False", $a);
2057 }
2058  
2059 sub For(&$%) # For loop 0..range-1 or in reverse.
2060 0 ▶ {my ($block, $range, %options) = @_; # Block, limit, options
Subroutine For: not called CC=4 SCAR=30.0
Pod undocumented
2061 0 ▶ if (!exists $options{reverse}) # Ascending order
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $options{'reverse'}) { } 0 0 0
2062 0 {my $s = 0; my $e = $range; # Start, end
2063 0 ▶ ($s, $e) = @$range if ref($e) =~ m(ARRAY); # Start, end as a reference
Branch: 1 branch
BranchTrueFalseTotal
if ref($e) =~ /ARRAY/u 0 0 0
2064  
2065 0 my ($Start, $Check, $Next, $End) = (label, label, label, label);
2066  
2067 0 setLabel($Start); # Start
2068 0 my $i = Mov $s;
2069 0 setLabel($Check); # Check
2070 0 Jge $End, $i, $e;
2071 0 TracePoint level=>2;
2072 0 &$block($i, $Check, $Next, $End); # Block
2073 0 setLabel($Next);
2074 0 Inc $i; # Next
2075 0 Jmp $Check;
2076 0 setLabel($End); # End
2077 }
2078 else
2079 0 {my $s = $range; my $e = 0; # Start, end
2080 0 ▶ ($e, $s) = @$range if ref($e) =~ m(ARRAY); # End, start as a reference
Branch: 1 branch
BranchTrueFalseTotal
if ref($e) =~ /ARRAY/u 0 0 0
2081  
2082 0 my ($Start, $Check, $Next, $End) = (label, label, label, label);
2083  
2084 0 setLabel($Start); # Start
2085 0 my $i = Subtract $s, 1;
2086 0 Subtract $i, $s;
2087 0 setLabel($Check); # Check
2088 0 Jlt $End, $i, $e;
2089 0 TracePoint level=>2;
2090 0 &$block($i, $Check, $Next, $End); # Block
2091 0 setLabel($Next);
2092 0 Dec $i; # Next
2093 0 Jmp $Check;
2094 0 setLabel($End); # End
2095 }
2096 }
2097  
2098 sub ForArray(&$$%) # For loop to process each element of the named area.
2099 0 ▶ {my ($block, $area, $name, %options) = @_; # Block of code, area, area name, options
Subroutine ForArray: not called CC=1 SCAR=6.9
Pod undocumented
2100 0 my $e = ArraySize $area, $name; # End
2101 0 my $s = 0; # Start
2102  
2103 0 my ($Start, $Check, $Next, $End) = (label, label, label, label);
2104  
2105 0 setLabel($Start); # Start
2106 0 my $i = Mov $s;
2107 0 setLabel($Check); # Check
2108 0 Jge $End, $i, $e;
2109 0 TracePoint level=>2;
2110 0 my $a = Mov [$area, \$i, $name];
2111 0 &$block($i, $a, $Check, $Next, $End); # Block
2112 0 setLabel($Next);
2113 0 Inc $i; # Next
2114 0 Jmp $Check;
2115 0 setLabel($End); # End
2116 }
2117  
2118 sub Good(&) # A good ending.
2119 0 ▶ {my ($good) = @_; # What to do on a good ending
Subroutine Good: not called CC=2 SCAR=17.9
Pod undocumented
2120 0 ▶ @_ == 1 or confess "One parameter";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter") 0 0 0
2121 0 (good=> $good)
2122 }
2123  
2124 sub Bad(&) # A bad ending.
2125 0 ▶ {my ($bad) = @_; # What to do on a bad ending
Subroutine Bad: not called CC=2 SCAR=17.9
Pod undocumented
2126 0 ▶ @_ == 1 or confess "One parameter";
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or confess("One parameter") 0 0 0
2127 0 (bad=> $bad)
2128 }
2129  
2130 sub Block(&%) # Block of code that can either be restarted or come to a good or a bad ending.
2131 0 ▶ {my ($block, %options) = @_; # Block, options
Subroutine Block: not called CC=3 SCAR=24.8
Pod undocumented
2132 0 my ($Start, $Good, $Bad, $End) = (label, label, label, label);
2133  
2134 0 my $g = $options{good};
2135 0 my $b = $options{bad};
2136  
2137 0 setLabel($Start); # Start
2138  
2139 0 TracePoint level=>2;
2140 0 &$block($Start, $Good, $Bad, $End); # Code of block
2141  
2142 0 ▶ if ($g) # Good
Branch: 1 branch
BranchTrueFalseTotal
if ($g) 0 0 0
2143 0 {Jmp $End;
2144 0 setLabel($Good);
2145 0 TracePoint level=>2;
2146 0 &$g;
2147 }
2148  
2149 0 ▶ if ($b) # Bad
Branch: 1 branch
BranchTrueFalseTotal
if ($b) 0 0 0
2150 0 {Jmp $End;
2151 0 setLabel($Bad);
2152 0 TracePoint level=>2;
2153 0 &$b;
2154 }
2155 0 setLabel($End); # End
2156 }
2157  
2158 sub Var(;$) #P Create a variable initialized to the specified value.
2159 0 ▶ {my ($value) = @_; # Value
Subroutine Var: not called CC=2 SCAR=17.9
Pod undocumented
2160 0 my $i = $assembly->registers;
2161 0 my $a = $assembly->variables;
2162 0 my $v = $a->count-1;
2163 0 my $c = $assembly->code->@*;
2164 # $a->instructions->[$v] = $c;
2165  
2166 0 ▶ Mov $i, $value if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
2167 0 $i
2168 }
2169  
2170 sub Execute(%) # Execute the current assembly.
2171 0 ▶ {my (%options) = @_; # Options
Subroutine Execute: not called CC=8 SCAR=42.8
Pod undocumented
2172 0 my $r = $assembly->execute(%options); # Execute the code in the current assembly
2173 0 ▶ if (my $out = $options{out})
Branch: 1 branch
BranchTrueFalseTotal
if (my $out = $options{"out"}) 0 0 0
2174 0 {my $c = compareArraysAndExplain $r->out, $out;
2175 0 ▶ lll $c if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
2176 0 ▶ say STDERR dump($r->out) if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
2177 0 ▶ return 0 if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
2178 }
2179 0 ▶ if (my $memory = $options{memory})
Branch: 1 branch
BranchTrueFalseTotal
if (my $memory = $options{"memory"}) 0 0 0
2180 0 {my $E = dump $memory;
2181 0 my $G = dump $r->memory;
2182 0 my $e = [split "\n", $E];
2183 0 my $g = [split "\n", $G];
2184 0 my $c = compareArraysAndExplain $g, $e;
2185 0 ▶ lll "$c\n$G\n" if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
2186 0 ▶ return 0 if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 0 0 0
2187 }
2188 0 return $r;
2189 }
2190  
2191 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
2192 1 ▶use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
2193  
2194 1 @ISA = qw(Exporter);
2195 1 @EXPORT = qw();
2196 1 @EXPORT_OK = qw(Add AreaStructure Array ArrayIndex ArrayCountLess ArrayCountGreater Assert AssertEq AssertGe AssertGt AssertLe AssertLt AssertNe Bad Block Call Clear Confess Dec Dump DumpArray Else Execute For ForArray Free Good IfEq IfFalse IfGe IfGt IfLe IfLt IfNe IfTrue Ifx Inc Jeq Jge Jgt Jle Jlt Jmp Jne JTrue JFalse Label Mov Nop Not Out ParamsGet ParamsPut Pop Procedure Push Resize Return ReturnGet ReturnPut ShiftDown ShiftLeft ShiftRight ShiftUp Start Subtract Tally Then Trace TracePoints Var Watch);
2197 #say STDERR '@EXPORT_OK = qw(', (join ' ', sort @EXPORT_OK), ')'; exit;
2198 1 %EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]);
2199  
2200 1 ▶return 1 if caller;
Branch: 1 branch
BranchTrueFalseTotal
if caller 1 0 1
2201  
2202 0 Test::More->builder->output("/dev/null"); # Reduce number of confirmation messages during testing
2203  
2204 0 eval {goto latest};
2205 sub is_deeply;
2206 sub ok($;$);
2207  
2208 #D1 Tests
2209  
2210 0 if (1) # Address dereferencing
2211 0 {my $e = execute->createInitialStackEntry->setMemoryType(1, 'aaa');
2212 0 my $s = $e->stackArea;
2213  
2214 0 $e->set(RefLeft([ 1, 1, 'aaa']), 99);
2215 0 $e->set(RefLeft([$s, 2, $s]), 1);
2216  
2217 0 is_deeply $e->right(RefRight [1, \1, 'aaa']), 99;
2218 0 is_deeply $e->right(RefRight [1, \\2, 'aaa']), 99;
2219  
2220 0 is_deeply $e->left (RefLeft [1, \1, 'aaa']), {address=>1, area=>1, name=>"aaa"};
2221 0 is_deeply $e->left (RefLeft [1, \\2, 'aaa']), {address=>1, area=>1, name=>"aaa"};
2222 }
2223  
2224 #D1 Examples
2225  
2226 #latest:;
2227 0 if (1) #TOut #TStart #TExecute
2228 0 {Start 1;
2229 0 Out "hello World";
2230 0 my $e = Execute(suppressOutput=>1);
2231 0 is_deeply $e->out, ["hello World"];
2232 }
2233  
2234 #latest:;
2235 0 if (1) #TNop
2236 0 {Start 1;
2237 0 Nop;
2238 0 ok Execute(out=>[]);
2239 }
2240  
2241 #latest:;
2242 0 if (1) #TMov
2243 0 {Start 1;
2244 0 my $a = Mov 2;
2245 0 Out $a;
2246  
2247 0 my $e = Execute(suppressOutput=>1);
2248 0 is_deeply $e->out, [2];
2249 }
2250  
2251 #latest:;
2252 0 if (1)
2253 0 {Start 1; #TMov
2254 0 my $a = Mov 3;
2255 0 my $b = Mov $$a;
2256 0 my $c = Mov \$b;
2257 0 Out $c;
2258 0 my $e = Execute(suppressOutput=>1);
2259 0 is_deeply $e->out, [3];
2260 }
2261  
2262 #latest:;
2263 0 if (1) #TAdd
2264 0 {Start 1;
2265 0 my $a = Add 3, 2;
2266 0 Out $a;
2267 0 my $e = Execute(suppressOutput=>1);
2268 0 is_deeply $e->out, [5];
2269 }
2270  
2271 #latest:;
2272 0 if (1) #TAdd #TSubtract
2273 0 {Start 1;
2274 0 my $a = Subtract 4, 2;
2275 0 Out $a;
2276 0 my $e = Execute(suppressOutput=>1);
2277 0 is_deeply $e->out, [2];
2278 }
2279  
2280 #latest:;
2281 0 if (1) #TDec
2282 0 {Start 1;
2283 0 my $a = Mov 3;
2284 0 Dec $a;
2285 0 Out $a;
2286 0 my $e = Execute(suppressOutput=>1);
2287 0 is_deeply $e->out, [2];
2288 }
2289  
2290 #latest:;
2291 0 if (1) #TInc
2292 0 {Start 1;
2293 0 my $a = Mov 3;
2294 0 Inc $a;
2295 0 Out $a;
2296 0 my $e = Execute(suppressOutput=>1);
2297 0 is_deeply $e->out, [4];
2298 }
2299  
2300 #latest:;
2301 0 if (1) #TNot
2302 0 {Start 1;
2303 0 my $a = Mov 3;
2304 0 my $b = Not $a;
2305 0 my $c = Not $b;
2306 0 Out $a;
2307 0 Out $b;
2308 0 Out $c;
2309 0 my $e = Execute(suppressOutput=>1);
2310 0 is_deeply $e->out, [3, "", 1];
2311 }
2312  
2313 #latest:;
2314 0 if (1) #TShiftLeft
2315 0 {Start 1;
2316 0 my $a = Mov 1;
2317 0 ShiftLeft $a, $a;
2318 0 Out $a;
2319 0 my $e = Execute(suppressOutput=>1);
2320 0 is_deeply $e->out, [2];
2321 }
2322  
2323 #latest:;
2324 0 if (1) #TShiftRight
2325 0 {Start 1;
2326 0 my $a = Mov 4;
2327 0 ShiftRight $a, 1;
2328 0 Out $a;
2329 0 my $e = Execute(suppressOutput=>1);
2330 0 is_deeply $e->out, [2];
2331 }
2332  
2333 #latest:;
2334 0 if (1) #TJmp
2335 0 {Start 1;
2336 0 Jmp (my $a = label);
2337 0 Out 1;
2338 0 Jmp (my $b = label);
2339 0 setLabel($a);
2340 0 Out 2;
2341 0 setLabel($b);
2342 0 my $e = Execute(suppressOutput=>1);
2343 0 is_deeply $e->out, [2];
2344 }
2345  
2346 #latest:;
2347 0 if (1) #TJLt #TLabel
2348 0 {Start 1;
2349 0 Mov 0, 1;
2350 0 Jlt ((my $a = label), \0, 2);
2351 0 Out 1;
2352 0 Jmp (my $b = label);
2353 0 setLabel($a);
2354 0 Out 2;
2355 0 setLabel($b);
2356  
2357 0 Jgt ((my $c = label), \0, 3);
2358 0 Out 1;
2359 0 Jmp (my $d = label);
2360 0 setLabel($c);
2361 0 Out 2;
2362 0 setLabel($d);
2363 0 my $e = Execute(suppressOutput=>1);
2364 0 is_deeply $e->out, [2,1];
2365 }
2366  
2367 #latest:;
2368 0 if (1) #TLabel
2369 0 {Start 1;
2370 0 Mov 0, 0;
2371 0 my $a = setLabel;
2372 0 Out \0;
2373 0 Inc \0;
2374 0 Jlt $a, \0, 10;
2375 0 my $e = Execute(suppressOutput=>1);
2376 0 is_deeply $e->out, [0..9];
2377 }
2378  
2379 #latest:;
2380 0 if (1) #TMov
2381 0 {Start 1;
2382 0 my $a = Array "aaa";
2383 0 Mov [$a, 1, "aaa"], 11;
2384 0 Mov 1, [$a, \1, "aaa"];
2385 0 Out \1;
2386 0 my $e = Execute(suppressOutput=>1);
2387 0 is_deeply $e->out, [11];
2388 }
2389  
2390 #latest:;
2391 0 if (1) #TCall #TReturn
2392 0 {Start 1;
2393 my $w = Procedure 'write', sub
2394 0 ▶ {Out 'aaa';
Subroutine __ANON__: not called CC=1 SCAR=6.9
2395 0 Return;
2396 0 };
2397 0 Call $w;
2398 0 my $e = Execute(suppressOutput=>1);
2399 0 is_deeply $e->out, ["aaa"];
2400 }
2401  
2402 #latest:;
2403 0 if (1) #TCall
2404 0 {Start 1;
2405 my $w = Procedure 'write', sub
2406 0 ▶ {my $a = ParamsGet 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2407 0 Out $a;
2408 0 Return;
2409 0 };
2410 0 ParamsPut 0, 'bbb';
2411 0 Call $w;
2412 0 my $e = Execute(suppressOutput=>1);
2413 0 is_deeply $e->out, ["bbb"];
2414 }
2415  
2416 #latest:;
2417 0 if (1) #TCall #TReturnPut #TReturnGet
2418 0 {Start 1;
2419 my $w = Procedure 'write', sub
2420 0 ▶ {ReturnPut 0, "ccc";
Subroutine __ANON__: not called CC=1 SCAR=6.9
2421 0 Return;
2422 0 };
2423 0 Call $w;
2424 0 ReturnGet \0, 0;
2425 0 Out \0;
2426 0 my $e = Execute(suppressOutput=>1);
2427 0 is_deeply $e->out, ["ccc"];
2428 }
2429  
2430 #latest:;
2431 0 if (1) #TProcedure
2432 0 {Start 1;
2433 my $add = Procedure 'add2', sub
2434 0 ▶ {my $a = ParamsGet 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2435 0 my $b = Add $a, 2;
2436 0 ReturnPut 0, $b;
2437 0 Return;
2438 0 };
2439 0 ParamsPut 0, 2;
2440 0 Call $add;
2441 0 my $c = ReturnGet 0;
2442 0 Out $c;
2443 0 my $e = Execute(suppressOutput=>1);
2444 0 is_deeply $e->out, [4];
2445 }
2446  
2447 #latest:;
2448 0 if (1) #TConfess
2449 0 {Start 1;
2450 my $c = Procedure 'confess', sub
2451 0 ▶ {Confess;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2452 0 };
2453 0 Call $c;
2454 0 my $e = Execute(suppressOutput=>1);
2455 0 is_deeply $e->out, ["Confess at:", " 2 3 confess", " 1 6 call"];
2456 }
2457  
2458 #latest:;
2459 0 if (1) #TPush #TPop
2460 0 {Start 1;
2461 0 my $a = Array "aaa";
2462 0 Push $a, 1;
2463 0 Push $a, 2;
2464 0 my $c = Pop $a;
2465 0 my $d = Pop $a;
2466  
2467 0 Out $c;
2468 0 Out $d;
2469 0 my $e = Execute(suppressOutput=>1);
2470 0 is_deeply $e->out, [2, 1];
2471 0 is_deeply $e->memory, { 1=> []};
2472 }
2473  
2474 #latest:;
2475 0 if (1) #TAlloc #TMov
2476 0 {Start 1;
2477 0 my $a = Array "alloc";
2478 0 my $b = Mov 99;
2479 0 my $c = Mov $a;
2480 0 Mov [$a, 0, 'alloc'], $b;
2481 0 Mov [$c, 1, 'alloc'], 2;
2482 0 ok Execute(memory=> { 1=> bless([99, 2], "alloc") });
2483 }
2484  
2485 #latest:;
2486 0 if (1) #TFree
2487 0 {Start 1;
2488 0 my $a = Array "node";
2489 0 Free $a, "aaa";
2490 0 my $e = Execute(suppressOutput=>1);
2491  
2492 0 is_deeply $e->out, [
2493 "Wrong name: aaa for array with name: node",
2494 " 1 2 free",
2495 ];
2496 }
2497  
2498 #latest:;
2499 0 if (1) #TFree #TDump
2500 0 {Start 1;
2501 0 my $a = Array "node";
2502 0 Out $a;
2503 0 Mov [$a, 1, 'node'], 1;
2504 0 Mov [$a, 2, 'node'], 2;
2505 0 Mov 1, [$a, \1, 'node'];
2506 0 Dump "dddd";
2507 0 Free $a, "node";
2508 0 my $e = Execute(suppressOutput=>1);
2509  
2510 0 is_deeply $e->out, [
2511 1,
2512 "dddd",
2513 "-2=bless([], \"return\")",
2514 "-1=bless([], \"params\")",
2515 "0=bless([1, 1], \"stackArea\")",
2516 "1=bless([undef, 1, 2], \"node\")",
2517 "Stack trace",
2518 " 1 6 dump",
2519 ];
2520 }
2521  
2522 #latest:;
2523 0 if (1) # Layout
2524 0 {Start 1;
2525 0 my $a = Mov 'A';
2526 0 my $b = Mov 'B';
2527 0 my $c = Mov 'C';
2528 0 Out $c;
2529 0 Out $b;
2530 0 Out $a;
2531 0 my $e = Execute(suppressOutput=>1);
2532 0 is_deeply $e->out, [qw(C B A)];
2533 }
2534  
2535 #latest:;
2536 0 if (1) #TIfEq #TIfNe #TIfLt #TIfLe #TIfGt #TIfGe
2537 0 {Start 1;
2538 0 my $a = Mov 1;
2539 0 my $b = Mov 2;
2540 0 ▶ IfEq $a, $a, Then {Out "Eq"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2541 0 ▶ IfNe $a, $a, Then {Out "Ne"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2542 0 ▶ IfLe $a, $a, Then {Out "Le"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2543 0 ▶ IfLt $a, $a, Then {Out "Lt"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2544 0 ▶ IfGe $a, $a, Then {Out "Ge"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2545 0 ▶ IfGt $a, $a, Then {Out "Gt"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2546 0 my $e = Execute(suppressOutput=>1);
2547 0 is_deeply $e->out, ["Eq", "Le", "Ge"];
2548 }
2549  
2550 #latest:;
2551 0 if (1) #TIfEq #TIfNe #TIfLt #TIfLe #TIfGt #TIfGe
2552 0 {Start 1;
2553 0 my $a = Mov 1;
2554 0 my $b = Mov 2;
2555 0 ▶ IfEq $a, $b, Then {Out "Eq"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2556 0 ▶ IfNe $a, $b, Then {Out "Ne"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2557 0 ▶ IfLe $a, $b, Then {Out "Le"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2558 0 ▶ IfLt $a, $b, Then {Out "Lt"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2559 0 ▶ IfGe $a, $b, Then {Out "Ge"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2560 0 ▶ IfGt $a, $b, Then {Out "Gt"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2561 0 my $e = Execute(suppressOutput=>1);
2562 0 is_deeply $e->out, ["Ne", "Le", "Lt"];
2563 }
2564  
2565 #latest:;
2566 0 if (1) #TIfEq #TIfNe #TIfLt #TIfLe #TIfGt #TIfGe
2567 0 {Start 1;
2568 0 my $a = Mov 1;
2569 0 my $b = Mov 2;
2570 0 ▶ IfEq $b, $a, Then {Out "Eq"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2571 0 ▶ IfNe $b, $a, Then {Out "Ne"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2572 0 ▶ IfLe $b, $a, Then {Out "Le"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2573 0 ▶ IfLt $b, $a, Then {Out "Lt"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2574 0 ▶ IfGe $b, $a, Then {Out "Ge"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2575 0 ▶ IfGt $b, $a, Then {Out "Gt"};
Subroutine __ANON__: not called CC=1 SCAR=6.9
2576 0 my $e = Execute(suppressOutput=>1);
2577 0 is_deeply $e->out, ["Ne", "Ge", "Gt"];
2578 }
2579  
2580 #latest:;
2581 0 if (1) #TIfTrue
2582 0 {Start 1;
2583 IfTrue 1,
2584 Then
2585 0 ▶ {Out 1
Subroutine __ANON__: not called CC=1 SCAR=6.9
2586 },
2587 Else
2588 0 ▶ {Out 0
Subroutine __ANON__: not called CC=1 SCAR=6.9
2589 0 };
2590 0 my $e = Execute(suppressOutput=>1);
2591 0 is_deeply $e->out, [1];
2592 }
2593  
2594 #latest:;
2595 0 if (1) #TIfFalse #TTrue #TFalse
2596 0 {Start 1;
2597 IfFalse 1,
2598 Then
2599 0 ▶ {Out 1
Subroutine __ANON__: not called CC=1 SCAR=6.9
2600 },
2601 Else
2602 0 ▶ {Out 0
Subroutine __ANON__: not called CC=1 SCAR=6.9
2603 0 };
2604 0 my $e = Execute(suppressOutput=>1);
2605 0 is_deeply $e->out, [0];
2606 }
2607  
2608 #latest:;
2609 0 if (1) #TForLoop
2610 0 {Start 1;
2611 For
2612 0 ▶ {my ($i) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2613 0 Out $i;
2614 0 } 10;
2615 0 my $e = Execute(suppressOutput=>1);
2616 0 is_deeply $e->out, [0..9];
2617 }
2618  
2619 #latest:;
2620 0 if (1) #TForLoop
2621 0 {Start 1;
2622 For
2623 0 ▶ {my ($i) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2624 0 Out $i;
2625 0 } 10, reverse=>1;
2626 0 my $e = Execute(suppressOutput=>1);
2627 0 is_deeply $e->out, [reverse 0..9];
2628 }
2629  
2630 #latest:;
2631 0 if (1) #TForLoop
2632 0 {Start 1;
2633 For
2634 0 ▶ {my ($i) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2635 0 Out $i;
2636 0 } [2, 10];
2637 0 my $e = Execute(suppressOutput=>1);
2638 0 is_deeply $e->out, [2..9];
2639 }
2640  
2641 #latest:;
2642 0 if (1) #TAssert
2643 0 {Start 1;
2644 0 Assert;
2645 0 my $e = Execute(suppressOutput=>1);
2646 0 is_deeply $e->out, ["Assert failed", " 1 1 assert"];
2647 }
2648  
2649 #latest:;
2650 0 if (1) #TAssertEq
2651 0 {Start 1;
2652 0 Mov 0, 1;
2653 0 AssertEq \0, 2;
2654 0 my $e = Execute(suppressOutput=>1);
2655 0 is_deeply $e->out, ["Assert 1 == 2 failed", " 1 2 assertEq"];
2656 }
2657  
2658 #latest:;
2659 0 if (1) #TAssertNe
2660 0 {Start 1;
2661 0 Mov 0, 1;
2662 0 AssertNe \0, 1;
2663 0 my $e = Execute(suppressOutput=>1);
2664 0 is_deeply $e->out, ["Assert 1 != 1 failed", " 1 2 assertNe"];
2665 }
2666  
2667 #latest:;
2668 0 if (1) #TAssertLt
2669 0 {Start 1;
2670 0 Mov 0, 1;
2671 0 AssertLt \0, 0;
2672 0 my $e = Execute(suppressOutput=>1);
2673 0 is_deeply $e->out, ["Assert 1 < 0 failed", " 1 2 assertLt"];
2674 }
2675  
2676 #latest:;
2677 0 if (1) #TAssertLe
2678 0 {Start 1;
2679 0 Mov 0, 1;
2680 0 AssertLe \0, 0;
2681 0 my $e = Execute(suppressOutput=>1);
2682 0 is_deeply $e->out, ["Assert 1 <= 0 failed", " 1 2 assertLe"];
2683 }
2684  
2685 #latest:;
2686 0 if (1) #TAssertGt
2687 0 {Start 1;
2688 0 Mov 0, 1;
2689 0 AssertGt \0, 2;
2690 0 my $e = Execute(suppressOutput=>1);
2691 0 is_deeply $e->out, ["Assert 1 > 2 failed", " 1 2 assertGt"];
2692 }
2693  
2694 #latest:;
2695 0 if (1) #TAssertGe
2696 0 {Start 1;
2697 0 Mov 0, 1;
2698 0 AssertGe \0, 2;
2699 0 my $e = Execute(suppressOutput=>1);
2700 0 is_deeply $e->out, ["Assert 1 >= 2 failed", " 1 2 assertGe"];
2701 }
2702  
2703 #latest:;
2704 0 if (1) #TAssertTrue
2705 0 {Start 1;
2706 0 AssertFalse 0;
2707 0 AssertTrue 0;
2708 0 my $e = Execute(suppressOutput=>1, trace=>1);
2709 0 is_deeply $e->out,
2710 [ " 1 0 1 assertFalse \n",
2711 "AssertTrue 0 failed",
2712 " 1 2 assertTrue",
2713 " 2 1 1 assertTrue \n"];
2714 }
2715  
2716 #latest:;
2717 0 if (1) #TAssertFalse
2718 0 {Start 1;
2719 0 AssertTrue 1;
2720 0 AssertFalse 1;
2721 0 my $e = Execute(suppressOutput=>1, trace=>1);
2722 0 is_deeply $e->out,
2723 [ " 1 0 1 assertTrue \n",
2724 "AssertFalse 1 failed",
2725 " 1 2 assertFalse",
2726 " 2 1 1 assertFalse \n"];
2727  
2728 }
2729  
2730 #latest:;
2731 0 if (1) # Temporary variable
2732 0 {my $s = Start 1;
2733 0 my $a = Mov 1;
2734 0 my $b = Mov 2;
2735 0 Out $a;
2736 0 Out $b;
2737 0 my $e = Execute(suppressOutput=>1);
2738 0 is_deeply $e->out, [1..2];
2739 }
2740  
2741 #latest:;
2742 0 if (1) #TAlloc #TMov #TCall
2743 0 {Start 1;
2744 0 my $a = Array "aaa";
2745 0 Dump "dddd";
2746 0 my $e = Execute(suppressOutput=>1);
2747 0 is_deeply $e->out, [
2748 "dddd",
2749 "-2=bless([], \"return\")",
2750 "-1=bless([], \"params\")",
2751 "0=bless([1], \"stackArea\")",
2752 "1=bless([], \"aaa\")",
2753 "Stack trace",
2754 " 1 2 dump",
2755 ];
2756 }
2757  
2758 #latest:;
2759 0 if (1) #TAlloc #TMov #TCall #TParamsPut #TParamsGet
2760 0 {Start 1;
2761 0 my $a = Array "aaa";
2762 0 my $i = Mov 1;
2763 0 my $v = Mov 11;
2764 0 ParamsPut 0, $a;
2765 0 ParamsPut 1, $i;
2766 0 ParamsPut 2, $v;
2767 my $set = Procedure 'set', sub
2768 0 ▶ {my $a = ParamsGet 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2769 0 my $i = ParamsGet 1;
2770 0 my $v = ParamsGet 2;
2771 0 Mov [$a, \$i, 'aaa'], $v;
2772 0 Return;
2773 0 };
2774 0 Call $set;
2775 0 my $V = Mov [$a, \$i, 'aaa'];
2776 0 AssertEq $v, $V;
2777 0 Out [$a, \$i, 'aaa'];
2778 0 my $e = Execute(suppressOutput=>1);
2779 0 is_deeply $e->out, [11];
2780 }
2781  
2782 #latest:;
2783 0 if (1) #TAlloc #TClear
2784 0 {Start 1;
2785 0 my $a = Array "aaa";
2786 0 Clear [$a, 10, 'aaa'];
2787 0 my $e = Execute(suppressOutput=>1);
2788 0 is_deeply $e->memory->{1}, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
2789 }
2790  
2791 #latest:;
2792 0 if (1) #TBlock #TGood #TBad
2793 0 {Start 1;
2794 Block
2795 0 ▶ {my ($start, $good, $bad, $end) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2796 0 Out 1;
2797 0 Jmp $good;
2798 }
2799 Good
2800 0 ▶ {Out 2;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2801 },
2802 Bad
2803 0 ▶ {Out 3;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2804 0 };
2805 0 Out 4;
2806 0 my $e = Execute(suppressOutput=>1);
2807 0 is_deeply $e->out, [1,2,4];
2808 }
2809  
2810 #latest:;
2811 0 if (1) #TBlock
2812 0 {Start 1;
2813 Block
2814 0 ▶ {my ($start, $good, $bad, $end) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2815 0 Out 1;
2816 0 Jmp $bad;
2817 }
2818 Good
2819 0 ▶ {Out 2;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2820 },
2821 Bad
2822 0 ▶ {Out 3;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2823 0 };
2824 0 Out 4;
2825 0 my $e = Execute(suppressOutput=>1);
2826 0 is_deeply $e->out, [1,3,4];
2827 }
2828  
2829 #latest:;
2830 0 if (1) #TProcedure
2831 0 {Start 1;
2832 0 for my $i(1..10)
2833 0 {Out $i;
2834 };
2835 IfTrue 0,
2836 Then
2837 0 ▶ {Out 99;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2838 0 };
2839 0 my $e = Execute(suppressOutput=>1);
2840 0 is_deeply $e->out, [1..10];
2841 }
2842  
2843 #latest:;
2844 0 if (1) # Double write
2845 0 {Start 1;
2846 0 Mov 1, 1;
2847 0 Mov 2, 1;
2848 0 Mov 3, 1;
2849 0 Mov 3, 1;
2850 0 Mov 1, 1;
2851 0 my $e = Execute(suppressOutput=>1);
2852 0 is_deeply keys($e->doubleWrite->%*), 1; # In area 0, variable 1 was first written by instruction 0 then again by instruction 1 once.
2853 }
2854  
2855 #latest:;
2856 0 if (1) # Pointless assign
2857 0 {Start 1;
2858 0 Add 2, 1, 1;
2859 0 Add 2, \2, 0;
2860 0 my $e = Execute(suppressOutput=>1);
2861 0 is_deeply $e->pointlessAssign, { 1=> 1 };
2862 }
2863  
2864 #latest:;
2865 0 if (0) # Not read
2866 {Start 1;
2867 my $a = Mov 1;
2868 my $b = Mov $a;
2869 my $e = Execute(suppressOutput=>1);
2870 ok $e->notRead->{0}{1} == 1; # Area 0 == stack, variable 1 == $b generated by instruction 1
2871 }
2872  
2873 #latest:;
2874 0 if (1) #TAlloc #TMov #TCall
2875 0 {Start 1;
2876 my $set = Procedure 'set', sub
2877 0 ▶ {my $a = ParamsGet 0;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2878 0 };
2879 0 ParamsPut 0, 1;
2880 0 Call $set;
2881 0 ParamsPut 0, 1;
2882 0 Call $set;
2883 0 my $e = Execute(suppressOutput=>1);
2884 0 is_deeply $e->out, [];
2885 }
2886  
2887 #latest:;
2888 0 if (1) # Invalid address
2889 0 {Start 1;
2890 0 Mov 1, \0;
2891 0 my $e = Execute(suppressOutput=>1);
2892 0 my $t = join "\n", $e->out->@*;
2893 0 ok $t =~ m"Cannot assign an undefined value";
2894 }
2895  
2896 #latest:;
2897 0 if (1) #TLeArea #TLeAddress
2898 0 {Start 1;
2899 0 my $a = Array "array";
2900 0 my $b = Mov 2;
2901 0 my $c = Mov 5;
2902 0 my $d = LeAddress $c;
2903 0 my $f = LeArea [$a, \0, 'array'];
2904 0 Out $d;
2905 0 Out $f;
2906 0 Mov [$a, \$b, 'array'], 22;
2907 0 Mov [$a, \$c, 'array'], 33;
2908 0 Mov [$f, \$d, 'array'], 44;
2909 0 my $e = Execute(suppressOutput=>1);
2910 0 is_deeply $e->out, [2,1];
2911 0 is_deeply $e->memory, {1=>[undef, undef, 44, undef, undef, 33]};
2912 }
2913  
2914 #latest:;
2915 0 if (1) #TShiftUp
2916 0 {Start 1;
2917 0 my $a = Array "array";
2918 0 Mov [$a, 0, 'array'], 0;
2919 0 Mov [$a, 1, 'array'], 1;
2920 0 Mov [$a, 2, 'array'], 2;
2921 0 ShiftUp [$a, 1, 'array'], 99;
2922 0 my $e = Execute(suppressOutput=>1);
2923 0 is_deeply $e->memory, {1=>[0, 99, 1, 2]};
2924 }
2925  
2926 #latest:;
2927 0 if (1) #TShiftDown
2928 0 {Start 1;
2929 0 my $a = Array "array";
2930 0 Mov [$a, 0, 'array'], 0;
2931 0 Mov [$a, 1, 'array'], 99;
2932 0 Mov [$a, 2, 'array'], 2;
2933 0 my $b = ShiftDown [$a, \1, 'array'];
2934 0 Out $b;
2935 0 my $e = Execute(suppressOutput=>1);
2936 0 is_deeply $e->memory, {1=>[0, 2]};
2937 0 is_deeply $e->out, [99];
2938 }
2939  
2940 #latest:;
2941 0 if (1) #TAlloc #TMov #TJeq #TJne #TJle #TJlt #TJge #TJgt
2942 0 {Start 1;
2943 0 my $a = Array "aaa";
2944 0 my $b = Array "bbb";
2945 0 Mov [$a, 0, 'aaa'], $b;
2946 0 Mov [$b, 0, 'bbb'], 99;
2947 For
2948 0 ▶ {my ($i, $check, $next, $end) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2949 0 my $c = Mov [$a, \0, 'aaa'];
2950 0 my $d = Mov [$c, \0, 'bbb'];
2951 0 Jeq $next, $d, $d;
2952 0 Jne $next, $d, $d;
2953 0 Jle $next, $d, $d;
2954 0 Jlt $next, $d, $d;
2955 0 Jge $next, $d, $d;
2956 0 Jgt $next, $d, $d;
2957 0 } 3;
2958 0 my $e = Execute(suppressOutput=>1);
2959 0 is_deeply $e->analyzeExecutionResults(doubleWrite=>3), "# 24 instructions executed";
2960 0 is_deeply $e->memory, { 1=> bless([2], "aaa"), 2=> bless([99], "bbb") };
2961 }
2962  
2963 #latest:;
2964 0 if (1) #TJTrue #TJFalse
2965 0 {Start 1;
2966 0 my $a = Mov 1;
2967 Block
2968 0 ▶ {my ($start, $good, $bad, $end) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2969 0 JTrue $end, $a;
2970 0 Out 1;
2971 0 };
2972 Block
2973 0 ▶ {my ($start, $good, $bad, $end) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2974 0 JFalse $end, $a;
2975 0 Out 2;
2976 0 };
2977 0 Mov $a, 0;
2978 Block
2979 0 ▶ {my ($start, $good, $bad, $end) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2980 0 JTrue $end, $a;
2981 0 Out 3;
2982 0 };
2983 Block
2984 0 ▶ {my ($start, $good, $bad, $end) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2985 0 JFalse $end, $a;
2986 0 Out 4;
2987 0 };
2988 0 my $e = Execute(suppressOutput=>1);
2989 0 is_deeply $e->out, [2, 3];
2990 }
2991  
2992 #latest:;
2993 0 if (1) #TAlloc #TMov
2994 0 {Start 1;
2995 0 my $a = Array 'aaa';
2996 0 my $b = Mov 2; # Location to move to in a
2997 For
2998 0 ▶ {my ($i, $check, $next, $end) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
2999 0 Mov [$a, \$b, 'aaa'], 1;
3000 0 Jeq $next, [$a, \$b, 'aaa'], 1;
3001 0 } 3;
3002 0 my $e = Execute(suppressOutput=>1);
3003 0 is_deeply $e->analyzeExecutionResults(doubleWrite=>3), "# 19 instructions executed";
3004 0 is_deeply $e->memory, {1=> bless([undef, undef, 1], "aaa")};
3005 }
3006  
3007 #latest:;
3008 0 if (1) #TResize
3009 0 {Start 1;
3010 0 my $a = Array 'aaa';
3011 0 Mov [$a, 0, 'aaa'], 1;
3012 0 Mov [$a, 1, 'aaa'], 2;
3013 0 Mov [$a, 2, 'aaa'], 3;
3014 0 Resize $a, 2;
3015 0 my $e = Execute(suppressOutput=>1);
3016 0 is_deeply $e->memory, {1=> [1, 2]};
3017 }
3018  
3019 #latest:;
3020 0 if (1) #TTrace #TThen #TElse
3021 0 {Start 1;
3022 0 Trace 1;
3023 IfEq 1, 2,
3024 Then
3025 0 ▶ {Mov 1, 1;
Subroutine __ANON__: not called CC=1 SCAR=6.9
3026 0 Mov 2, 1;
3027 },
3028 Else
3029 0 ▶ {Mov 3, 3;
Subroutine __ANON__: not called CC=1 SCAR=6.9
3030 0 Mov 4, 4;
3031 0 };
3032 IfEq 2, 2,
3033 Then
3034 0 ▶ {Mov 1, 1;
Subroutine __ANON__: not called CC=1 SCAR=6.9
3035 0 Mov 2, 1;
3036 },
3037 Else
3038 0 ▶ {Mov 3, 3;
Subroutine __ANON__: not called CC=1 SCAR=6.9
3039 0 Mov 4, 4;
3040 0 };
3041 0 my $e = Execute(suppressOutput=>1);
3042 0 is_deeply scalar($e->out->@*), 14;
3043 }
3044  
3045 #latest:;
3046 0 if (1) #TWatch
3047 0 {Start 1;
3048 0 my $a = Mov 1;
3049 0 my $b = Mov 2;
3050 0 my $c = Mov 3;
3051 0 Watch $b;
3052 0 Mov $a, 4;
3053 0 Mov $b, 5;
3054 0 Mov $c, 6;
3055 0 my $e = Execute(suppressOutput=>1);
3056 0 is_deeply $e->out,
3057 [
3058 "Change at watched area: 0 (stackArea), address: 1\n",
3059 " 1 6 mov",
3060 "Current value: 2",
3061 "New value: 5",
3062 "-2=bless([], \"return\")",
3063 "-1=bless([], \"params\")",
3064 "0=bless([4, 2, 3], \"stackArea\")",
3065 ];
3066 }
3067  
3068 #latest:;
3069 0 if (1) #TArraySize #TForArray #TArray
3070 0 {Start 1;
3071 0 my $a = Array "aaa";
3072 0 Mov [$a, 0, "aaa"], 1;
3073 0 Mov [$a, 1, "aaa"], 22;
3074 0 Mov [$a, 2, "aaa"], 333;
3075 0 my $n = ArraySize $a, "aaa";
3076 0 Out $n;
3077  
3078 ForArray
3079 0 ▶ {my ($i, $e, $check, $next, $end) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
3080 IfGt $i, 1,
3081 Then
3082 0 ▶ {Trace 1;
Subroutine __ANON__: not called CC=1 SCAR=6.9
3083 0 };
3084 0 Out $i; Out $e;
3085 0 } $a, "aaa";
3086  
3087 0 Nop;
3088 0 my $e = Execute(suppressOutput=>1);
3089 0 is_deeply $e->memory, {1=>[1, 22, 333]};
3090  
3091 0 is_deeply $e->out,
3092 [3, 0, 1, 1, 22,
3093 "Trace: 1",
3094 " 37 14 1 trace \n",
3095 " 38 15 3 label \n",
3096 2,
3097 " 39 16 3 out \n",
3098 333,
3099 " 40 17 3 out \n",
3100 " 41 18 3 label \n",
3101 " 42 19 3 inc [0, 3, stackArea] = 3 was 2\n",
3102 " 43 20 3 jmp \n",
3103 " 44 9 4 label \n",
3104 " 45 10 4 jGe \n",
3105 " 46 21 1 label \n",
3106 " 47 22 1 nop \n",
3107 ];
3108 }
3109  
3110 #latest:;
3111 0 if (1) #TDumpArray #TMov
3112 0 {Start 1;
3113 0 my $a = Array "aaa";
3114 0 Mov [$a, 0, "aaa"], 1;
3115 0 Mov [$a, 1, "aaa"], 22;
3116 0 Mov [$a, 2, "aaa"], 333;
3117 0 DumpArray $a, "AAAA";
3118 0 my $e = Execute(suppressOutput=>1);
3119  
3120 0 is_deeply $e->out,
3121 ["AAAA",
3122 "bless([1, 22, 333], \"aaa\")",
3123 "Stack trace",
3124 " 1 5 dumpArray",
3125 ];
3126 }
3127  
3128  
3129 # 0 1 2
3130 # 10 20 30
3131 # 5=0 15=1 25=2 35=3
3132  
3133 #latest:;
3134 0 if (1) #TArrayIndex #TArrayCountLess #TArrayCountGreater
3135 0 {Start 1;
3136 0 my $a = Array "aaa";
3137 0 Mov [$a, 0, "aaa"], 10;
3138 0 Mov [$a, 1, "aaa"], 20;
3139 0 Mov [$a, 2, "aaa"], 30;
3140  
3141 0 Out ArrayIndex $a, 30;
3142 0 Out ArrayIndex $a, 20;
3143 0 Out ArrayIndex $a, 10;
3144 0 Out ArrayIndex $a, 15;
3145  
3146 0 Out ArrayCountLess $a, 35;
3147 0 Out ArrayCountLess $a, 25;
3148 0 Out ArrayCountLess $a, 15;
3149 0 Out ArrayCountLess $a, 5;
3150  
3151 0 Out ArrayCountGreater $a, 35;
3152 0 Out ArrayCountGreater $a, 25;
3153 0 Out ArrayCountGreater $a, 15;
3154 0 Out ArrayCountGreater $a, 5;
3155  
3156 0 my $e = Execute(suppressOutput=>1);
3157 0 is_deeply $e->out, [3,2,1,0, 3,2,1,0, 0,1,2,3];
3158 }
3159  
3160 #latest:;
3161 0 if (1) #TTally #TFor
3162 0 {my $N = 5;
3163 0 Start 1;
3164 For
3165 0 ▶ {Tally 1;
Subroutine __ANON__: not called CC=1 SCAR=6.9
3166 0 my $a = Mov 1;
3167 0 Tally 2;
3168 0 Inc $a;
3169 0 Tally 0;
3170 0 } $N;
3171 0 my $e = Execute;
3172  
3173 0 is_deeply $e->tallyCount, 2 * $N;
3174 0 is_deeply $e->tallyCounts,{ 1 => {mov => $N}, 2 => {inc => $N}};
3175 }
3176  
3177 #latest:;
3178 0 if (1) #TTracePoints
3179 0 {my $N = 5;
3180 0 Start 1;
3181 0 TracePoints 1;
3182 For
3183 0 ▶ {my $a = Mov 1;
Subroutine __ANON__: not called CC=1 SCAR=6.9
3184 0 Inc $a;
3185 0 } $N;
3186 0 my $e = Execute(suppressOutput=>1);
3187 0 is_deeply $e->out, [
3188 "TracePoints: 1",
3189 "Trace",
3190 " 1 6 tracePoint",
3191 "Trace",
3192 " 1 6 tracePoint",
3193 "Trace",
3194 " 1 6 tracePoint",
3195 "Trace",
3196 " 1 6 tracePoint",
3197 "Trace",
3198 " 1 6 tracePoint",
3199 ];
3200 }
« blib/lib/Zero/Emulator.pm ↑ Summary lib/Zero/NWayTree.pm »