blib/lib/Zero/Emulator.pm untested

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