| 1 | #!/usr/bin/perl -Ilib | ||
| 2 | #------------------------------------------------------------------------------- | ||
| 3 | # Assemble and execute 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 = 20230511; # 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=>75" 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 (!defined($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 ($name ne $Name) | ||
| 504 | {$exec->stackTraceAndExit("Attempting to access area: $Name using: $name"); | ||
| 505 | } | ||
| 506 | } | ||
| 507 | } | ||
| 508 | |||
| 509 | sub Zero::Emulator::Execution::getMemory($$$;$%) #P Get from memory. | ||
| 510 | {my ($exec, $area, $address, $name, %options) = @_; # Execution environment, area, address, expected name of area, options | ||
| 511 | @_ < 3 and confess "At least three parameters"; | ||
| 512 | $exec->check($area, $name); | ||
| 513 | my $g = $exec->get($area, $address); | ||
| 514 | my $n = $name // 'unknown'; | ||
| 515 | if (!defined($g) and !defined($options{undefinedOk})) | ||
| 516 | {$exec->stackTraceAndExit("Undefined memory accessed at area: $area ($n), address: $address\n"); | ||
| 517 | } | ||
| 518 | $g | ||
| 519 | } | ||
| 520 | |||
| 521 | sub Zero::Emulator::Execution::get($$$) #P Get from memory. | ||
| 522 | {my ($exec, $area, $address) = @_; # Execution environment, area, address | ||
| 523 | @_ == 3 or confess "Three parameters"; | ||
| 524 | $exec->memory->{$area}[$address]; | ||
| 525 | } | ||
| 526 | |||
| 527 | |||
| 528 | sub Zero::Emulator::Execution::set($$$) #P Set the value of an address at the specified address in memory in the current execution environment. | ||
| 529 | {my ($exec, $address, $value) = @_; # Execution environment, address specification, value | ||
| 530 | @_ == 3 or confess "Three parameters"; | ||
| 531 | my $e = $exec; | ||
| 532 | my $m = $e->memory; | ||
| 533 | my $a = $address->area; | ||
| 534 | my $l = $address->address; | ||
| 535 | $exec->lastAssignArea = $a; | ||
| 536 | $exec->lastAssignAddress = $l; | ||
| 537 | $exec->lastAssignValue = $value; | ||
| 538 | $exec->lastAssignBefore = $$m{$a}[$l]; | ||
| 539 | |||
| 540 | $$m{$a}[$l] = $value; | ||
| 541 | } | ||
| 542 | |||
| 543 | #D2 Execution # Execute assembly code in the emulator | ||
| 544 | |||
| 545 | sub Zero::Emulator::Execution::stackArea($) #P Current stack frame. | ||
| 546 | {my ($exec) = @_; # Execution environment | ||
| 547 | $exec->calls->[-1]->stackArea; # Stack area | ||
| 548 | } | ||
| 549 | |||
| 550 | sub Zero::Emulator::Execution::address($$$$) #P Record a reference to memory. | ||
| 551 | {my ($exec, $area, $address, $name) = @_; # Execution environment, area, address in area, memory | ||
| 552 | @_ == 4 or confess "Four parameters"; | ||
| 553 | genHash("Zero::Emulator::Address", # Address memory | ||
| 554 | area=> $area, # Area in memory | ||
| 555 | address=> $address, # Address within area | ||
| 556 | name=> $name // 'stackArea', # Name of area | ||
| 557 | ); | ||
| 558 | } | ||
| 559 | |||
| 560 | sub Zero::Emulator::Execution::stackTrace($;$) #P Create a stack trace. | ||
| 561 | {my ($exec, $title) = @_; # Execution environment, title | ||
| 562 | my $i = $exec->currentInstruction; | ||
| 563 | my $s = $exec->suppressOutput; # Suppress file and line numbers in dump to facilitate automated testing | ||
| 564 | my @t; | ||
| 565 | push @t, $i->contextString($exec, $title//"Stack trace"); | ||
| 566 | |||
| 567 | for my $j(reverse keys $exec->calls->@*) | ||
| 568 | {my $c = $exec->calls->[$j]; | ||
| 569 | my $i = $c->instruction; | ||
| 570 | push @t, sprintf "%5d %4d %s", $j+1, $i->number+1, $i->action if $s; | ||
| 571 | push @t, sprintf "%5d %4d %-16s at %s line %d", | ||
| 572 | $j+1, $i->number+1, $i->action, $i->file, $i->line unless $s; | ||
| 573 | } | ||
| 574 | |||
| 575 | @t | ||
| 576 | } | ||
| 577 | |||
| 578 | sub Zero::Emulator::Execution::stackTraceAndExit($$%) #P Create a stack trace and exit from the emulated program. | ||
| 579 | {my ($exec, $title, %options) = @_; # Execution environment, title, options | ||
| 580 | @_ >= 2 or confess "At least two parameters"; | ||
| 581 | |||
| 582 | my @t = $exec->stackTrace($title); | ||
| 583 | push $exec->out->@*, @t; | ||
| 584 | my $t = join "\n", @t; | ||
| 585 | |||
| 586 | if (!defined $exec->suppressOutput) | ||
| 587 | {confess $t if $options{confess}; | ||
| 588 | say STDERR $t; | ||
| 589 | } | ||
| 590 | |||
| 591 | $exec->instructionPointer = undef; # Execution terminates as soon as undefined instruction is encountered | ||
| 592 | $t | ||
| 593 | } | ||
| 594 | |||
| 595 | my $allocs = 0; my $allocsStacked = 0; # Normal arrays allocated by the user, stacked allocations are made to support subroutine calling, parameter passing, result returning. | ||
| 596 | |||
| 597 | sub Zero::Emulator::Execution::allocMemory($$;$) #P Create the name of a new memory area. | ||
| 598 | {my ($exec, $name, $stacked) = @_; # Execution environment, name of allocation, stacked if true | ||
| 599 | if ($stacked) | ||
| 600 | {my $a = $allocsStacked--; | ||
| 601 | $exec->memory->{$a} = bless [], $name; | ||
| 602 | $exec->memoryType->{$a} = $name; | ||
| 603 | return $a | ||
| 604 | } | ||
| 605 | my $a = ++$allocs; | ||
| 606 | $exec->memory ->{$a} = bless [], $name; | ||
| 607 | $exec->memoryType->{$a} = $name; | ||
| 608 | $a | ||
| 609 | } | ||
| 610 | |||
| 611 | 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. | ||
| 612 | {my ($exec, $area, $name) = @_; # Execution environment, area name, name of allocation | ||
| 613 | @_ == 3 or confess "Three parameters"; | ||
| 614 | $exec->memoryType->{$area} = $name; | ||
| 615 | $exec | ||
| 616 | } | ||
| 617 | |||
| 618 | sub Zero::Emulator::Execution::notRead() #P Record the unused memory locations in the current stack frame. | ||
| 619 | {my ($exec) = @_; # Parameters | ||
| 620 | my $area = $exec->stackArea; | ||
| 621 | # my @area = $memory{$area}->@*; # Memory in area | ||
| 622 | # my %r; # Location in stack frame=> instruction defining vasriable | ||
| 623 | # for my $a(keys @area) | ||
| 624 | # {if (my $I = $calls[-1]->variables->instructions->[$a]) | ||
| 625 | # {$r{$a} = $I; # Number of instruction creating variable | ||
| 626 | # } | ||
| 627 | # } | ||
| 628 | # | ||
| 629 | # if (my $r = $read{$area}) # Locations in this area that have ben read | ||
| 630 | # {delete $r{$_} for keys %$r; # Delete locations that have been read from | ||
| 631 | # } | ||
| 632 | # | ||
| 633 | # $notRead{$area} = {%r} if keys %r; # Record not read | ||
| 634 | {} | ||
| 635 | } | ||
| 636 | |||
| 637 | sub Zero::Emulator::Execution::rwWrite($$$) #P Observe write to memory. | ||
| 638 | {my ($exec, $area, $address) = @_; # Area in memory, address within area | ||
| 639 | my $P = $exec->rw->{$area}{$address}; | ||
| 640 | if (defined($P)) | ||
| 641 | {my $M = $exec->memory->{$area}[$address]; # If the memory address is zero we will assume that it has been cleared rather than set. | ||
| 642 | if ($M) | ||
| 643 | {my $Q = $exec->currentInstruction; | ||
| 644 | my $p = $P->contextString($exec, "Previous write"); | ||
| 645 | my $q = $Q->contextString($exec, "Current write"); | ||
| 646 | $exec->doubleWrite->{$p}{$q}++; | ||
| 647 | } | ||
| 648 | } | ||
| 649 | $exec->rw->{$area}{$address} = $exec->currentInstruction; | ||
| 650 | } | ||
| 651 | |||
| 652 | sub Zero::Emulator::Execution::markAsRead($$$) #P Mark a memory address as having been read from. | ||
| 653 | {my ($exec, $area, $address) = @_; # Area in memory, address within area | ||
| 654 | @_ == 3 or confess "Three parameters"; | ||
| 655 | delete $exec->rw->{$area}{$address}; # Clear last write operation | ||
| 656 | } | ||
| 657 | |||
| 658 | sub Zero::Emulator::Execution::rwRead($$$) #P Observe read from memory. | ||
| 659 | {my ($exec, $area, $address) = @_; # Area in memory, address within area | ||
| 660 | @_ == 3 or confess "Three parameters"; | ||
| 661 | if (defined(my $a = $exec->rw->{$area}{$address})) # Can only read from locations that actually have something in them | ||
| 662 | {$exec->markAsRead($area, $address); # Clear last write operation | ||
| 663 | $exec->read->{$area}{$address}++; # Track reads | ||
| 664 | } | ||
| 665 | } | ||
| 666 | |||
| 667 | sub Zero::Emulator::Execution::left($$;$) #P Address a memory address. | ||
| 668 | {my ($exec, $ref, $extra) = @_; # Reference, an optional extra offset to add or subtract to the final memory address | ||
| 669 | @_ == 2 or @_ == 3 or confess "Two or three parameters"; | ||
| 670 | ref($ref) =~ m((RefLeft|RefRight)\Z) | ||
| 671 | or confess "RefLeft or RefRight required, not: ".dump($ref); | ||
| 672 | my $r = $ref->address; | ||
| 673 | my $a = $r; | ||
| 674 | $a = \$r if isScalar $a; # Interpret constants as direct memory locations | ||
| 675 | my $area = $ref->area; | ||
| 676 | my $x = $extra // 0; # Default is to use the address as supplied without locating a nearby address | ||
| 677 | my $S = $exec->stackArea; # Current stack frame | ||
| 678 | |||
| 679 | my sub invalid() | ||
| 680 | {my ($p) = @_; # Parameters | ||
| 681 | my $i = $exec->currentInstruction; | ||
| 682 | my $l = $i->line; | ||
| 683 | my $f = $i->file; | ||
| 684 | $exec->stackTraceAndExit( | ||
| 685 | "Invalid left area: ".dump($area) | ||
| 686 | ." address: ".dump($a) | ||
| 687 | .(defined($extra) ? " + extra: ".dump($extra) : '')) | ||
| 688 | }; | ||
| 689 | |||
| 690 | my $M; # Memory address | ||
| 691 | if (isScalar $$a) | ||
| 692 | {$M = $$a+$x | ||
| 693 | } | ||
| 694 | elsif (isScalar $$$a) | ||
| 695 | {$exec->rwRead($S, $$$a); | ||
| 696 | $M = $exec->getMemory($S, $$$a, $ref->name)+$x | ||
| 697 | } | ||
| 698 | else | ||
| 699 | {invalid | ||
| 700 | } | ||
| 701 | |||
| 702 | if ($M < 0) # Disallow negative addresses because they mean something special to Perl | ||
| 703 | {$exec->stackTraceAndExit("Negative address for area: " | ||
| 704 | .dump($area) | ||
| 705 | .", address: ".dump($a) | ||
| 706 | ." extra:".dump($x)); | ||
| 707 | } | ||
| 708 | elsif (!defined($area)) # Current stack frame | ||
| 709 | {$exec->rwWrite( $S, $M); | ||
| 710 | return $exec->address($S, $M, $ref->name); # Stack frame | ||
| 711 | } | ||
| 712 | elsif (isScalar($area)) | ||
| 713 | {$exec->rwWrite( $area, $M); | ||
| 714 | return $exec->address($area, $M, $ref->name) # Specified constant area | ||
| 715 | } | ||
| 716 | elsif (isScalar($$area)) | ||
| 717 | {$exec->rwRead ( $S, $$area); | ||
| 718 | my $A = $exec->getMemory($S, $$area, $ref->name); | ||
| 719 | $exec->rwWrite( $A, $M); | ||
| 720 | return $exec->address($A, $M, $ref->name) # Indirect area | ||
| 721 | } | ||
| 722 | invalid; | ||
| 723 | } | ||
| 724 | |||
| 725 | sub Zero::Emulator::Execution::leftSuppress($$) #P Indicate that a memory address has been read. | ||
| 726 | {my ($exec, $ref) = @_; # Execution environment, Reference | ||
| 727 | @_ == 2 or confess "Two parameters"; | ||
| 728 | ref($ref) =~ m((RefLeft|RefRight)\Z) or confess "RefLeft or RefRight required"; | ||
| 729 | my $A = $ref->address; | ||
| 730 | my $area = $ref->area; | ||
| 731 | my $a = $A; | ||
| 732 | $a = \$A if isScalar $a; # Interpret constants as direct memory locations | ||
| 733 | |||
| 734 | my $m; | ||
| 735 | my $memory = $exec->memory; | ||
| 736 | my $stackArea = $exec->stackArea; | ||
| 737 | |||
| 738 | if (isScalar $$a) # Direct | ||
| 739 | {$m = $$a; | ||
| 740 | } | ||
| 741 | elsif (isScalar $$$a) # Indirect | ||
| 742 | {$exec->rwRead ($stackArea, $$$a); | ||
| 743 | $m = $exec->get($stackArea, $$$a, $ref->name); | ||
| 744 | } | ||
| 745 | |||
| 746 | if (defined($m)) | ||
| 747 | {if (!defined($area)) # Current stack frame | ||
| 748 | {$exec->rwRead($stackArea, $m); | ||
| 749 | } | ||
| 750 | elsif (isScalar($area)) # Direct area | ||
| 751 | {$exec->rwRead($area, $m); | ||
| 752 | } | ||
| 753 | elsif (isScalar($$area)) # Indirect area | ||
| 754 | {$exec->rwRead( $stackArea, $area); | ||
| 755 | $exec->rwRead($exec->get($stackArea, $$area), $m); | ||
| 756 | } | ||
| 757 | } | ||
| 758 | } | ||
| 759 | |||
| 760 | sub Zero::Emulator::Execution::right($$) #P Get a constant or a memory address. | ||
| 761 | {my ($exec, $ref) = @_; # Location, optional area | ||
| 762 | @_ == 2 or confess "Two parameters"; | ||
| 763 | ref($ref) =~ m((RefLeft|RefRight)\Z) or confess "RefLeft or RefRight required"; | ||
| 764 | my $a = $ref->address; | ||
| 765 | my $area = $ref->area; | ||
| 766 | my $stackArea = $exec->stackArea; | ||
| 767 | my $r; my $e = 0; my $tAddress; my $tArea; | ||
| 768 | |||
| 769 | my sub invalid() | ||
| 770 | {my $i = $exec->currentInstruction; | ||
| 771 | my $l = $i->line; | ||
| 772 | my $f = $i->file; | ||
| 773 | $exec->stackTraceAndExit( | ||
| 774 | "Invalid right area: ".dump($area) | ||
| 775 | ." address: " .dump($a) | ||
| 776 | ." stack: " .$exec->stackArea | ||
| 777 | ." error: " .dump($e) | ||
| 778 | ." target Area: ".dump($tArea) | ||
| 779 | ." address: " .dump($tAddress)); | ||
| 780 | } | ||
| 781 | |||
| 782 | if (isScalar($a)) # Constant | ||
| 783 | {#rwRead($area//&stackArea, $a) if $a =~ m(\A\-?\d+\Z); | ||
| 784 | return $a if defined $a; # Attempting to read a address that has never been set is an error | ||
| 785 | $exec->stackTraceAndExit("Undefined right address: ".dump($a)); | ||
| 786 | } | ||
| 787 | |||
| 788 | my $m; | ||
| 789 | my $memory = $exec->memory; | ||
| 790 | |||
| 791 | if (isScalar($$a)) # Direct | ||
| 792 | {$m = $$a; | ||
| 793 | } | ||
| 794 | elsif (isScalar($$$a)) # Indirect | ||
| 795 | {$exec->rwRead($stackArea, $$$a); | ||
| 796 | $m = $exec->getMemory($stackArea, $$$a); | ||
| 797 | } | ||
| 798 | else | ||
| 799 | {$exec->stackTraceAndExit("Invalid right address: ".dump($a)); | ||
| 800 | } | ||
| 801 | if (!defined($m)) | ||
| 802 | {invalid; | ||
| 803 | } | ||
| 804 | |||
| 805 | if (!defined($area)) | ||
| 806 | {$exec->rwRead($stackArea, $m); | ||
| 807 | $r = $exec->getMemory($stackArea, $m); # Indirect from stack area | ||
| 808 | $e = 1; $tAddress = $m; $tArea = $exec->stackArea; | ||
| 809 | } | ||
| 810 | elsif (isScalar($area)) | ||
| 811 | {$exec->rwRead( $area, $m); | ||
| 812 | $r = $exec->getMemory($area, $m, $ref->name); # Indirect from stack area | ||
| 813 | $e = 2; $tAddress = $m; $tArea = $area; | ||
| 814 | } | ||
| 815 | elsif (isScalar($$area)) | ||
| 816 | {$exec->rwRead($exec->stackArea, $$area); # Mark the address holding the area as having been read | ||
| 817 | if (defined(my $j = $$memory{$exec->stackArea}[$$area])) | ||
| 818 | {$exec->rwRead($j, $m); | ||
| 819 | $r = $$memory{$j}[$m]; # Indirect from indirect area | ||
| 820 | $r = $exec->getMemory($j, $m, $ref->name); # Indirect from stack area | ||
| 821 | $e = 9; $tAddress = $m; $tArea = $j; | ||
| 822 | } | ||
| 823 | } | ||
| 824 | invalid if !defined $r; | ||
| 825 | $r | ||
| 826 | } | ||
| 827 | |||
| 828 | sub Zero::Emulator::Execution::jumpOp($$$) #P Jump to the target address if the tested memory area if the condition is matched. | ||
| 829 | {my ($exec, $i, $check) = @_; # Execution environment, Instruction, check | ||
| 830 | @_ == 3 or confess "Three parameters"; | ||
| 831 | $exec->instructionPointer = $i->number + $exec->right($i->target) if &$check; # Check if condition is met | ||
| 832 | } | ||
| 833 | |||
| 834 | sub Zero::Emulator::Execution::assert($$$) #P Assert generically. | ||
| 835 | {my ($exec, $test, $sub) = @_; # Execution environment, Text of test, subroutine of test | ||
| 836 | @_ == 3 or confess "Three parameters"; | ||
| 837 | my $i = $exec->currentInstruction; | ||
| 838 | my ($a, $b) = ($exec->right($i->source), $exec->right($i->source2)); | ||
| 839 | unless($sub->($a, $b)) | ||
| 840 | {$exec->stackTraceAndExit("Assert $a $test $b failed"); | ||
| 841 | } | ||
| 842 | } | ||
| 843 | |||
| 844 | sub Zero::Emulator::Execution::assign($$$) #P Assign - check for pointless assignments. | ||
| 845 | {my ($exec, $target, $value) = @_; # Execution environment, Target of assign, value to assign | ||
| 846 | @_ == 3 or confess "Three parameters"; | ||
| 847 | ref($target) =~ m(Address)i or confess "Not an address: ".dump($target); | ||
| 848 | |||
| 849 | my $a = $target->area; | ||
| 850 | my $l = $target->address; | ||
| 851 | my $n = $target->name//'unknown'; | ||
| 852 | |||
| 853 | my $N = $exec->memoryType->{$a}; | ||
| 854 | if (!defined $N) # Check that the area has a name | ||
| 855 | {$exec->stackTraceAndExit("No name for area $a\n".dump($exec->memory)." address:".dump($target), confess=>1); | ||
| 856 | } | ||
| 857 | |||
| 858 | if ($n ne $N) # Check that the area name matches the one supplied in the address | ||
| 859 | {$exec->stackTraceAndExit | ||
| 860 | ("Area name provided: '$n' does not match actual area name '$N' for area: $a"); | ||
| 861 | } | ||
| 862 | |||
| 863 | if (!defined($value)) # Check that the assign is not pointless | ||
| 864 | {$exec->stackTraceAndExit | ||
| 865 | ("Cannot assign an undefined value to area: $a ($n), address: $l"); | ||
| 866 | } | ||
| 867 | else | ||
| 868 | {my $currently = $target->get($exec); | ||
| 869 | if (defined($currently) and $currently == $value) | ||
| 870 | {$exec->pointlessAssign->{$exec->currentInstruction->number}++; | ||
| 871 | if ($exec->stopOnError) | ||
| 872 | {$exec->stackTraceAndExit("Pointless assign of: $currently to area: $a, at: $l"); | ||
| 873 | } | ||
| 874 | } | ||
| 875 | } | ||
| 876 | |||
| 877 | if (defined $exec->watch->{$a}{$l}) # Watch for specified changes | ||
| 878 | {my @s = $exec->stackTrace("Change at watched area: $a ($n), address: $l\n"); | ||
| 879 | push @s, join ' ', "Current value:", $exec->memory->{$a}[$l]; | ||
| 880 | push @s, join ' ', "New value:", $value; | ||
| 881 | push @s, $exec->dumpMemory; | ||
| 882 | say STDERR join "\n", @s unless $exec->suppressOutput; | ||
| 883 | push $exec->out->@*, @s; | ||
| 884 | } | ||
| 885 | |||
| 886 | $exec->set($target, $value); # Actually do the assign | ||
| 887 | } | ||
| 888 | |||
| 889 | sub Zero::Emulator::Execution::allocateSystemAreas($) #P Allocate system areas for a new stack frame. | ||
| 890 | {my ($exec) = @_; # Execution environment | ||
| 891 | @_ == 1 or confess "One parameter"; | ||
| 892 | (stackArea=> $exec->allocMemory("stackArea", 1), | ||
| 893 | params=> $exec->allocMemory("params", 1), | ||
| 894 | return=> $exec->allocMemory("return", 1)); | ||
| 895 | } | ||
| 896 | |||
| 897 | sub Zero::Emulator::Execution::freeSystemAreas($$) #P Free system areas for the specified stack frame. | ||
| 898 | {my ($exec, $c) = @_; # Execution environment, stack frame | ||
| 899 | @_ == 2 or confess "Two parameters"; | ||
| 900 | $exec->notRead; # Record unread memory locations in the current stack frame | ||
| 901 | delete $exec->memory->{$_} for $c->stackArea, $c->params, $c->return; | ||
| 902 | $allocsStacked -= 3; | ||
| 903 | } | ||
| 904 | |||
| 905 | sub Zero::Emulator::Execution::currentInstruction($) #P Locate current instruction. | ||
| 906 | {my ($exec) = @_; # Execution environment | ||
| 907 | @_ == 1 or confess "One parameter"; | ||
| 908 | $exec->calls->[-1]->instruction; | ||
| 909 | } | ||
| 910 | |||
| 911 | sub Zero::Emulator::Execution::createInitialStackEntry($) #P Create the initial stack frame. | ||
| 912 | {my ($exec) = @_; # Execution environment | ||
| 913 | |||
| 914 | push $exec->calls->@*, # Variables in initial stack frame | ||
| 915 | stackFrame( | ||
| 916 | $exec->block ? (variables=> $exec->block->variables) : (), | ||
| 917 | $exec->allocateSystemAreas); | ||
| 918 | $exec | ||
| 919 | } | ||
| 920 | |||
| 921 | sub Zero::Emulator::Code::execute($%) #P Execute a block of code. | ||
| 922 | {my ($block, %options) = @_; # Block of code, execution options | ||
| 923 | |||
| 924 | $block->assemble; # Assemble if necessary | ||
| 925 | |||
| 926 | my $exec = execute(code=>$block, %options); # Execution environment | ||
| 927 | |||
| 928 | my %instructions = # Instruction definitions | ||
| 929 | (add=> sub # Add the two source operands and store the result in the target | ||
| 930 | {my $i = $exec->currentInstruction; | ||
| 931 | my $t = $exec->left($i->target); | ||
| 932 | my $a = $exec->right($i->source); | ||
| 933 | my $b = $exec->right($i->source2); | ||
| 934 | $exec->assign($t, $a + $b); | ||
| 935 | }, | ||
| 936 | |||
| 937 | subtract=> sub # Subtract the second source operand from the first and store the result in the target | ||
| 938 | {my $i = $exec->currentInstruction; | ||
| 939 | my $t = $exec->left($i->target); | ||
| 940 | my $a = $exec->right($i->source); | ||
| 941 | my $b = $exec->right($i->source2); | ||
| 942 | $exec->assign($t, $a - $b); | ||
| 943 | }, | ||
| 944 | |||
| 945 | assert=> sub # Assert | ||
| 946 | {my $i = $exec->currentInstruction; | ||
| 947 | $exec->stackTraceAndExit("Assert failed"); | ||
| 948 | }, | ||
| 949 | |||
| 950 | assertEq=> sub # Assert equals | ||
| 951 | {$exec->assert("==", sub {my ($a, $b) = @_; $a == $b}) | ||
| 952 | }, | ||
| 953 | |||
| 954 | assertNe=> sub # Assert not equals | ||
| 955 | {$exec->assert("!=", sub {my ($a, $b) = @_; $a != $b}) | ||
| 956 | }, | ||
| 957 | |||
| 958 | assertLt=> sub # Assert less than | ||
| 959 | {$exec->assert("< ", sub {my ($a, $b) = @_; $a < $b}) | ||
| 960 | }, | ||
| 961 | |||
| 962 | assertLe=> sub # Assert less than or equal | ||
| 963 | {$exec->assert("<=", sub {my ($a, $b) = @_; $a <= $b}) | ||
| 964 | }, | ||
| 965 | |||
| 966 | assertGt=> sub # Assert greater than | ||
| 967 | {$exec->assert("> ", sub {my ($a, $b) = @_; $a > $b}) | ||
| 968 | }, | ||
| 969 | |||
| 970 | assertGe=> sub # Assert greater | ||
| 971 | {$exec->assert(">=", sub {my ($a, $b) = @_; $a >= $b}) | ||
| 972 | }, | ||
| 973 | |||
| 974 | array=> sub # Create a new memory area and write its number into the address named by the target operand | ||
| 975 | {my $i = $exec->currentInstruction; | ||
| 976 | my $a = $exec->allocMemory($i->source); # The reason for this allocation | ||
| 977 | my $t = $exec->left($i->target); | ||
| 978 | |||
| 979 | my $A = $exec->memory->{$a} = []; | ||
| 980 | bless $A, $i->source; # Useful because dump then prints the type of each area for us | ||
| 981 | $exec->assign($t, $a); | ||
| 982 | $a | ||
| 983 | }, | ||
| 984 | |||
| 985 | free=> sub # Free the memory area named by the source operand | ||
| 986 | {my $i = $exec->currentInstruction; | ||
| 987 | my $area = $exec->right($i->target); # Area | ||
| 988 | my $name = $exec->right($i->source); | ||
| 989 | |||
| 990 | if (!defined($name)) # A name is required | ||
| 991 | {$exec->stackTraceAndExit("Area name required to free an area: $area"); | ||
| 992 | return; | ||
| 993 | } | ||
| 994 | |||
| 995 | if ($area !~ m(\A\d+\Z)) # User free-able area | ||
| 996 | {$exec->stackTraceAndExit("Attempting to allocate non user area: $area"); | ||
| 997 | return; | ||
| 998 | } | ||
| 999 | |||
| 1000 | my $Name = $exec->memoryType->{$area}; # Area has a name | ||
| 1001 | if (!defined($Name)) | ||
| 1002 | {$exec->stackTraceAndExit("No name associated with area: $area"); | ||
| 1003 | return; | ||
| 1004 | } | ||
| 1005 | |||
| 1006 | if ($name ne $Name) # Name matches supplied name | ||
| 1007 | {$exec->stackTraceAndExit("Wrong name: $name for area with name: $Name"); | ||
| 1008 | return; | ||
| 1009 | } | ||
| 1010 | |||
| 1011 | delete $exec->memory->{$area} # Free validly identified area | ||
| 1012 | }, | ||
| 1013 | |||
| 1014 | areaSize=> sub # Get the size of the specified area | ||
| 1015 | {my $i = $exec->currentInstruction; | ||
| 1016 | my $size = $exec->left ($i->target); # Location to store size in | ||
| 1017 | my $area = $exec->right($i->source); # Location of area | ||
| 1018 | my $name = $i->source2; # Name of area | ||
| 1019 | |||
| 1020 | if (!defined($name)) # A name is required | ||
| 1021 | {$exec->stackTraceAndExit("Area name required to size an area: ".dump($area)); | ||
| 1022 | return; | ||
| 1023 | } | ||
| 1024 | |||
| 1025 | my $Name = $exec->memoryType->{$area}; # Area has a name | ||
| 1026 | if (!defined($Name)) | ||
| 1027 | {$exec->stackTraceAndExit("No name associated with area: $area"); | ||
| 1028 | return; | ||
| 1029 | } | ||
| 1030 | |||
| 1031 | if ($name ne $Name) # Name matches supplied name | ||
| 1032 | {$exec->stackTraceAndExit("Wrong name: $name for area with name: $Name"); | ||
| 1033 | return; | ||
| 1034 | } | ||
| 1035 | |||
| 1036 | $exec->assign($size, scalar $exec->memory->{$area}->@*) # Size of area | ||
| 1037 | }, | ||
| 1038 | |||
| 1039 | resize=> sub # Resize an area | ||
| 1040 | {my $i = $exec->currentInstruction; | ||
| 1041 | my $size = $exec->right($i->source); # Size to reduce area to | ||
| 1042 | my $area = $exec->right($i->target); # Area to reduce to | ||
| 1043 | $exec->stackTraceAndExit("Attempting to resize non user area: $area") | ||
| 1044 | unless $area =~ m(\A\d+\Z); | ||
| 1045 | $#{$exec->memory->{$area}} = $size-1; | ||
| 1046 | }, | ||
| 1047 | |||
| 1048 | call=> sub # Call a subroutine | ||
| 1049 | {my $i = $exec->currentInstruction; | ||
| 1050 | my $t = $i->target->address; # Subroutine to call | ||
| 1051 | |||
| 1052 | if (isScalar($t)) | ||
| 1053 | {$exec->instructionPointer = $i->number + $t; # Relative call if we know where the subroutine is relative to the call instruction | ||
| 1054 | } | ||
| 1055 | else | ||
| 1056 | {$exec->instructionPointer = $t; # Absolute call | ||
| 1057 | } | ||
| 1058 | push $exec->calls->@*, | ||
| 1059 | stackFrame(target=>$block->code->[$exec->instructionPointer], # Create a new call stack entry | ||
| 1060 | instruction=>$i, variables=>$i->source->variables, | ||
| 1061 | $exec->allocateSystemAreas()); | ||
| 1062 | }, | ||
| 1063 | |||
| 1064 | return=> sub # Return from a subroutine call via the call stack | ||
| 1065 | {my $i = $exec->currentInstruction; | ||
| 1066 | $exec->calls or $exec->stackTraceAndExit("The call stack is empty so I do not know where to return to"); | ||
| 1067 | $exec->freeSystemAreas(pop $exec->calls->@*); | ||
| 1068 | if ($exec->calls) | ||
| 1069 | {my $c = $exec->calls->[-1]; | ||
| 1070 | $exec->instructionPointer = $c->instruction->number+1; | ||
| 1071 | } | ||
| 1072 | else | ||
| 1073 | {$exec->instructionPointer = undef; | ||
| 1074 | } | ||
| 1075 | }, | ||
| 1076 | |||
| 1077 | confess=> sub # Print the current call stack and stop | ||
| 1078 | {$exec->stackTraceAndExit("Confess at:", confess=>1); | ||
| 1079 | }, | ||
| 1080 | |||
| 1081 | trace=> sub # Start/stop/change tracing status from a program. A trace writes out which instructions have been executed and how they affected memory | ||
| 1082 | {my $i = $exec->currentInstruction; | ||
| 1083 | my $s = $exec->right($i->source) ? 1 : 0; | ||
| 1084 | $exec->trace = $s; | ||
| 1085 | my $m = join ' ', "Trace: $s"; | ||
| 1086 | say STDERR $m unless $exec->suppressOutput; | ||
| 1087 | push $exec->out->@*, $m; | ||
| 1088 | }, | ||
| 1089 | |||
| 1090 | tracePoints=> sub # Start trace points | ||
| 1091 | {my $i = $exec->currentInstruction; | ||
| 1092 | my $s = $exec->right($i->source) ? 1 : 0; | ||
| 1093 | $exec->tracePoints = $s; | ||
| 1094 | my $m = "TracePoints: $s"; | ||
| 1095 | say STDERR $m unless $exec->suppressOutput; | ||
| 1096 | push $exec->out->@*, $m; | ||
| 1097 | }, | ||
| 1098 | |||
| 1099 | tracePoint=> sub # Trace point | ||
| 1100 | {return unless $exec->tracePoints; | ||
| 1101 | my @s = $exec->stackTrace("Trace"); | ||
| 1102 | say STDERR join "\n", @s unless $exec->suppressOutput; | ||
| 1103 | push $exec->out->@*, @s; | ||
| 1104 | }, | ||
| 1105 | |||
| 1106 | dump=> sub # Dump memory | ||
| 1107 | {my $i = $exec->currentInstruction; | ||
| 1108 | my @m; | ||
| 1109 | if (ref($i->source) =~ m(Code)i) | ||
| 1110 | {$i->source->($exec); | ||
| 1111 | return; | ||
| 1112 | } | ||
| 1113 | |||
| 1114 | push @m, $i->source // "Memory dump"; | ||
| 1115 | push @m, $exec->dumpMemory; | ||
| 1116 | push @m, $exec->stackTrace; | ||
| 1117 | |||
| 1118 | say STDERR join "\n", @m unless $exec->suppressOutput; | ||
| 1119 | push $exec->out->@*, @m; | ||
| 1120 | }, | ||
| 1121 | |||
| 1122 | dumpArray=> sub # Dump array in memory | ||
| 1123 | {my $i = $exec->currentInstruction; | ||
| 1124 | my @m; | ||
| 1125 | push @m, $i->source // "Array dump"; | ||
| 1126 | my $a = $exec->right($i->target); | ||
| 1127 | push @m, dump($exec->memory->{$a}); | ||
| 1128 | push @m, $exec->stackTrace; | ||
| 1129 | |||
| 1130 | say STDERR join "\n", @m unless $exec->suppressOutput; | ||
| 1131 | push $exec->out->@*, @m; | ||
| 1132 | }, | ||
| 1133 | |||
| 1134 | dec=> sub # Decrement locations in memory. The first address is incremented by 1, the next by two, etc. | ||
| 1135 | {my $i = $exec->currentInstruction; | ||
| 1136 | my $T = $exec->right($i->target); | ||
| 1137 | my $t = $exec->left($i->target); | ||
| 1138 | $exec->set($t, defined($t) ? $T-1 : -1); | ||
| 1139 | }, | ||
| 1140 | |||
| 1141 | inc=> sub # Increment locations in memory. The first address is incremented by 1, the next by two, etc. | ||
| 1142 | {my $i = $exec->currentInstruction; | ||
| 1143 | my $T = $exec->right($i->target); | ||
| 1144 | my $t = $exec->left($i->target); | ||
| 1145 | $exec->set($t, defined($t) ? $T+1 : 1); | ||
| 1146 | }, | ||
| 1147 | |||
| 1148 | jmp=> sub # Jump to the target address | ||
| 1149 | {my $i = $exec->currentInstruction; | ||
| 1150 | my $n = $i->number; | ||
| 1151 | my $r = $exec->right($i->target); | ||
| 1152 | $exec->instructionPointer = $n + $r; | ||
| 1153 | }, | ||
| 1154 | # Conditional jumps | ||
| 1155 | jEq=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) == $exec->right($i->source2)})}, | ||
| 1156 | jNe=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) != $exec->right($i->source2)})}, | ||
| 1157 | jLe=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) <= $exec->right($i->source2)})}, | ||
| 1158 | jLt=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) < $exec->right($i->source2)})}, | ||
| 1159 | jGe=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) >= $exec->right($i->source2)})}, | ||
| 1160 | jGt=> sub {my $i = $exec->currentInstruction; $exec->jumpOp($i, sub{$exec->right($i->source) > $exec->right($i->source2)})}, | ||
| 1161 | |||
| 1162 | label=> sub # Label - no operation | ||
| 1163 | {my ($i) = @_; # Instruction | ||
| 1164 | }, | ||
| 1165 | |||
| 1166 | clear=> sub # Clear the first bytes of an area as specified by the target operand | ||
| 1167 | {my $i = $exec->currentInstruction; | ||
| 1168 | my $n = $exec->left($i->target); | ||
| 1169 | for my $a(0..$n->address-1) | ||
| 1170 | {my $A = $exec->left(RefLeft([$i->target->area, $a, q(aaa)])); | ||
| 1171 | $exec->set($A, 0); | ||
| 1172 | } | ||
| 1173 | }, | ||
| 1174 | |||
| 1175 | leAddress=> sub # Load the address component of a reference | ||
| 1176 | {my $i = $exec->currentInstruction; | ||
| 1177 | my $s = $exec->left($i->source); | ||
| 1178 | my $t = $exec->left($i->target); | ||
| 1179 | $exec->assign($t, $s->address); | ||
| 1180 | }, | ||
| 1181 | |||
| 1182 | leArea=> sub # Load the area component of an address | ||
| 1183 | {my $i = $exec->currentInstruction; | ||
| 1184 | my $s = $exec->left($i->source); | ||
| 1185 | my $t = $exec->left($i->target); | ||
| 1186 | $exec->assign($t, $s->area); | ||
| 1187 | }, | ||
| 1188 | |||
| 1189 | mov=> sub # Move data moves data from one part of memory to another - "set", by contrast, sets variables from constant values | ||
| 1190 | {my $i = $exec->currentInstruction; | ||
| 1191 | my $s = $exec->right($i->source); | ||
| 1192 | my $t = $exec->left($i->target); | ||
| 1193 | $exec->assign($t, $s); | ||
| 1194 | }, | ||
| 1195 | |||
| 1196 | not=> sub # Not in place | ||
| 1197 | {my $i = $exec->currentInstruction; | ||
| 1198 | my $s = $exec->right($i->source); | ||
| 1199 | my $t = $exec->left($i->target); | ||
| 1200 | $exec->assign($t, !$s); | ||
| 1201 | }, | ||
| 1202 | |||
| 1203 | 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 | ||
| 1204 | {my $i = $exec->currentInstruction; | ||
| 1205 | my $p = RefLeft([$exec->calls->[-2]->params, $i->source->address, | ||
| 1206 | 'params']); | ||
| 1207 | my $t = $exec->left ($i->target); | ||
| 1208 | $exec->leftSuppress ($p); # The source will be read from | ||
| 1209 | 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 | ||
| 1210 | $exec->assign($t, $s->get($exec)); | ||
| 1211 | }, | ||
| 1212 | |||
| 1213 | paramsPut=> sub # Place a parameter in the current parameter block | ||
| 1214 | {my $i = $exec->currentInstruction; | ||
| 1215 | my $p = $i->target->area // $exec->calls->[-1]->params; | ||
| 1216 | my $r = RefLeft [$p, $i->target->address, 'params']; | ||
| 1217 | $exec->leftSuppress ($r); | ||
| 1218 | my $t = $exec->left ($r); | ||
| 1219 | my $s = $exec->right($i->source); | ||
| 1220 | $exec->assign($t, $s); | ||
| 1221 | }, | ||
| 1222 | |||
| 1223 | returnGet=> sub # Get a word from the return area | ||
| 1224 | {my $i = $exec->currentInstruction; | ||
| 1225 | my $p = $exec->calls->[-1]->return; # Memory area | ||
| 1226 | my $t = $exec->left ($i->target); | ||
| 1227 | my $r = RefLeft([$p, \$i->source->address, 'return']); # The source will be read from | ||
| 1228 | $exec->leftSuppress($r); # The source will be read from | ||
| 1229 | 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 | ||
| 1230 | $exec->assign($t, $s->get($exec)); | ||
| 1231 | }, | ||
| 1232 | |||
| 1233 | returnPut=> sub # Put a word into the return area | ||
| 1234 | {my $i = $exec->currentInstruction; | ||
| 1235 | my $p = $exec->calls->[-2]->return; | ||
| 1236 | my $t = $exec->left (RefLeft([$p, $i->target->address, q(return)])); | ||
| 1237 | my $s = $exec->right($i->source); | ||
| 1238 | $exec->assign($t, $s); | ||
| 1239 | }, | ||
| 1240 | |||
| 1241 | nop=> sub # No operation | ||
| 1242 | {my ($i) = @_; # Instruction | ||
| 1243 | }, | ||
| 1244 | |||
| 1245 | out=> sub # Write source as output to an array of words | ||
| 1246 | {my $i = $exec->currentInstruction; | ||
| 1247 | my $t = $exec->right($i->source); | ||
| 1248 | lll $t unless $exec->suppressOutput; | ||
| 1249 | push $exec->out->@*, $t; | ||
| 1250 | }, | ||
| 1251 | |||
| 1252 | pop=> sub # Pop a value from the specified memory area if possible else confess | ||
| 1253 | {my $i = $exec->currentInstruction; | ||
| 1254 | my $s = $i->source; | ||
| 1255 | my $area = $i->source ? $exec->right($i->source) : &stackArea; # Memory area to pop | ||
| 1256 | if (!defined($exec->memory->{$area}) or !$exec->memory->{$area}->@*) # Stack not pop-able | ||
| 1257 | {$exec->stackTraceAndExit("Cannot pop area $area"); | ||
| 1258 | } | ||
| 1259 | my $t = $exec->left($i->target); | ||
| 1260 | my $p = pop $exec->memory->{$area}->@*; | ||
| 1261 | $exec->assign($t, $p); # Pop from memory area into indicated memory address | ||
| 1262 | }, | ||
| 1263 | |||
| 1264 | push=> sub # Push a value onto the specified memory area | ||
| 1265 | {my $i = $exec->currentInstruction; | ||
| 1266 | if ($i->target) | ||
| 1267 | {my $t = $exec->right($i->target); | ||
| 1268 | my $s = $exec->right($i->source); | ||
| 1269 | push $exec->memory->{$t}->@*, $s; | ||
| 1270 | } | ||
| 1271 | else | ||
| 1272 | {push $exec->memory->{&stackArea}->@*, right($i->source); | ||
| 1273 | } | ||
| 1274 | }, | ||
| 1275 | |||
| 1276 | shiftLeft=> sub # Shift left within an element | ||
| 1277 | {my $i = $exec->currentInstruction; | ||
| 1278 | $exec->leftSuppress ($i->target); # Make sure there something to shift | ||
| 1279 | my $t = $exec->left ($i->target); | ||
| 1280 | my $s = $exec->right($i->source); | ||
| 1281 | $exec->assign($t, $t->get($exec) << $s); | ||
| 1282 | }, | ||
| 1283 | |||
| 1284 | shiftRight=> sub # Shift right within an element | ||
| 1285 | {my $i = $exec->currentInstruction; | ||
| 1286 | $exec->leftSuppress ($i->target); # Make sure there something to shift | ||
| 1287 | my $t = $exec->left ($i->target); | ||
| 1288 | my $s = $exec->right($i->source); | ||
| 1289 | $exec->assign($t, $t->get($exec) >> $s); | ||
| 1290 | }, | ||
| 1291 | |||
| 1292 | shiftUp=> sub # Shift an element up in a memory area | ||
| 1293 | {my $i = $exec->currentInstruction; | ||
| 1294 | my $s = $exec->right($i->source); | ||
| 1295 | my $t = $exec->left($i->target); | ||
| 1296 | my $L = $exec->areaContent($t->area); # Length of area | ||
| 1297 | my $l = $t->address; | ||
| 1298 | for my $j(reverse 1..$L-$l) | ||
| 1299 | {my $s = $exec->left($i->target, $j-1); | ||
| 1300 | my $t = $exec->left($i->target, $j); | ||
| 1301 | $exec->assign($t, $s->get($exec)); | ||
| 1302 | } | ||
| 1303 | $exec->assign($t, $s); | ||
| 1304 | }, | ||
| 1305 | |||
| 1306 | shiftDown=> sub # Shift an element down in a memory area | ||
| 1307 | {my $i = $exec->currentInstruction; | ||
| 1308 | my $s = $exec->left($i->source); | ||
| 1309 | my $t = $exec->left($i->target); | ||
| 1310 | my $L = $exec->areaContent($s->area); # Length of area | ||
| 1311 | my $l = $s->address; | ||
| 1312 | my $v = $s->get($exec); | ||
| 1313 | for my $j($l..$L-2) # Each element in specified range | ||
| 1314 | {my $S = $exec->left(RefLeft([$s->area, $j+1, $s->name])); | ||
| 1315 | my $T = $exec->left(RefLeft([$s->area, $j, $s->name])); | ||
| 1316 | $exec->assign($T, $S->get($exec)); | ||
| 1317 | } | ||
| 1318 | pop $exec->memory->{$s->area}->@*; | ||
| 1319 | my $T = $exec->left($i->target); | ||
| 1320 | $exec->assign($T, $v); | ||
| 1321 | }, | ||
| 1322 | |||
| 1323 | tally=> sub # Tally instruction usage | ||
| 1324 | {my $i = $exec->currentInstruction; | ||
| 1325 | my $t = $exec->right($i->source); | ||
| 1326 | $exec->tally = $t; | ||
| 1327 | }, | ||
| 1328 | |||
| 1329 | watch=> sub # Watch a memory location for changes | ||
| 1330 | {my $i = $exec->currentInstruction; | ||
| 1331 | my $t = $exec->left($i->target); | ||
| 1332 | $exec->watch->{$t->area}{$t->address}++; | ||
| 1333 | }, | ||
| 1334 | ); | ||
| 1335 | |||
| 1336 | $exec->createInitialStackEntry; # Variables in initial stack frame | ||
| 1337 | |||
| 1338 | my $mi = $options{maximumInstructionsToExecute} // # Prevent run away executions | ||
| 1339 | maximumInstructionsToExecute; | ||
| 1340 | for my $j(1..$mi) # Each instruction in the code until we hit an undefined instruction | ||
| 1341 | {last unless defined($exec->instructionPointer); | ||
| 1342 | my $i = $exec->block->code->[$exec->instructionPointer++]; # Current instruction | ||
| 1343 | $exec->calls->[-1]->instruction = $i; | ||
| 1344 | last unless $i; | ||
| 1345 | if (my $a = $i->action) # Action | ||
| 1346 | {$exec->counts->{$a}++; $exec->count++; # Execution instruction counts | ||
| 1347 | $exec->stackTraceAndExit(qq(Invalid instruction: "$a"\n)) | ||
| 1348 | unless my $c = $instructions{$a}; | ||
| 1349 | |||
| 1350 | if (my $t = $exec->tally) # Tally instruction counts | ||
| 1351 | {if ($a !~ m(\A(label|tally)\Z)) # Omit instructions that are not tally-able | ||
| 1352 | {$exec->tallyCount++; | ||
| 1353 | $exec->tallyCounts->{$t}{$a}++; | ||
| 1354 | } | ||
| 1355 | } | ||
| 1356 | |||
| 1357 | $exec->lastAssignArea = $exec->lastAssignAddress = $exec->lastAssignValue = undef; | ||
| 1358 | $c->($i); # Execute instruction | ||
| 1359 | |||
| 1360 | $exec->instructionCounts->{$i->number}++; # Execution count by actual instruction | ||
| 1361 | |||
| 1362 | ++$i->executed; | ||
| 1363 | |||
| 1364 | if ($exec->trace) # Trace changes to memory | ||
| 1365 | {my $e = $exec->instructionCounts->{$i->number}; # Execution count for this instruction | ||
| 1366 | my $f = $exec->formatTrace; | ||
| 1367 | my $s = $exec->suppressOutput; | ||
| 1368 | my $a = $i->action; | ||
| 1369 | my $n = $i->number; | ||
| 1370 | my $m = sprintf "%4d %4d %4d %12s %20s", $j, $n, $e, $a, $f; | ||
| 1371 | $m .= sprintf " at %s line %d", $i->file, $i->line unless $s; | ||
| 1372 | $m .= "\n"; | ||
| 1373 | say STDERR $m unless $s; | ||
| 1374 | push $exec->out->@*, $m; | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | } | ||
| 1378 | confess "Out of instructions after $j"if $j >= maximumInstructionsToExecute; | ||
| 1379 | } | ||
| 1380 | |||
| 1381 | $exec->freeSystemAreas($exec->calls->[0]); # Free first stack frame | ||
| 1382 | |||
| 1383 | $exec | ||
| 1384 | } # Execution results | ||
| 1385 | |||
| 1386 | sub Zero::Emulator::Execution::formatTrace($) #P Describe last memory assignment. | ||
| 1387 | {my ($exec) = @_; # Execution | ||
| 1388 | return "" unless defined(my $area = $exec->lastAssignArea); | ||
| 1389 | return "" unless defined(my $addr = $exec->lastAssignAddress); | ||
| 1390 | return "" unless defined(my $type = $exec->memoryType->{$area}); | ||
| 1391 | return "" unless defined(my $value = $exec->lastAssignValue); | ||
| 1392 | my $B = $exec->lastAssignBefore; | ||
| 1393 | my $b = $B ? " was $B" : ""; | ||
| 1394 | sprintf "[%d, %d, %s] = %d$b", $area, $addr, $type, $value; | ||
| 1395 | } | ||
| 1396 | |||
| 1397 | my $assembly; # The current assembly | ||
| 1398 | |||
| 1399 | my sub label() # Next unique label | ||
| 1400 | {++$assembly->labelCounter; | ||
| 1401 | } | ||
| 1402 | |||
| 1403 | my sub setLabel(;$) # Set and return a label | ||
| 1404 | {my ($l) = @_; # Optional preset label | ||
| 1405 | $l //= label; # Create label if none supplied | ||
| 1406 | Label($l); # Set label | ||
| 1407 | $l # Return (new) label | ||
| 1408 | } | ||
| 1409 | |||
| 1410 | my sub xSource($) # Record a source argument | ||
| 1411 | {my ($s) = @_; # Source expression | ||
| 1412 | (q(source), RefRight $s) | ||
| 1413 | } | ||
| 1414 | |||
| 1415 | my sub xSource2($) # Record a source argument | ||
| 1416 | {my ($s) = @_; # Source expression | ||
| 1417 | (q(source2), RefRight $s) | ||
| 1418 | } | ||
| 1419 | |||
| 1420 | my sub xTarget($) # Record a target argument | ||
| 1421 | {my ($t) = @_; # Target expression | ||
| 1422 | (q(target), RefLeft $t) | ||
| 1423 | } | ||
| 1424 | |||
| 1425 | sub Start($) # Start the current assembly using the specified version of the Zero language. At the moment only version 1 works.. | ||
| 1426 | {my ($version) = @_; # Version desired - at the moment only 1 | ||
| 1427 | $version == 1 or confess "Version 1 is currently the only version available\n"; | ||
| 1428 | $allocs = $allocsStacked = 0; | ||
| 1429 | $assembly = Code; # The current assembly | ||
| 1430 | } | ||
| 1431 | |||
| 1432 | sub Add($$;$) # Add the source locations together and store in the result in the target area. | ||
| 1433 | {my ($target, $s1, $s2) = @_ == 2 ? (&Var(), @_) : @_; # Target address, source one, source two | ||
| 1434 | $assembly->instruction(action=>"add", xTarget($target), | ||
| 1435 | xSource($s1), xSource2($s2)); | ||
| 1436 | $target | ||
| 1437 | } | ||
| 1438 | |||
| 1439 | sub Subtract($$;$) # Subtract the second source address from the first and store in the result in the target area. | ||
| 1440 | {my ($target, $s1, $s2) = @_ == 2 ? (&Var(), @_) : @_; # Target address, source one, source two | ||
| 1441 | $assembly->instruction(action=>"subtract", xTarget($target), | ||
| 1442 | xSource($s1), xSource2($s2)); | ||
| 1443 | $target | ||
| 1444 | } | ||
| 1445 | |||
| 1446 | sub Array($) # Create a new memory area and write its number into the address named by the target operand. | ||
| 1447 | {my ($name) = @_; # Name of allocation | ||
| 1448 | my $t = &Var(); | ||
| 1449 | $assembly->instruction(action=>"array", target=>RefLeft($t), source=>$name); | ||
| 1450 | $t; | ||
| 1451 | } | ||
| 1452 | |||
| 1453 | sub Free($$) # Free the memory area named by the target operand after confirming that it has the name specified on the source operand. | ||
| 1454 | {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 | ||
| 1455 | $assembly->instruction(action=>"free", xTarget($target), xSource($source)); | ||
| 1456 | } | ||
| 1457 | |||
| 1458 | sub AreaSize($$) # The current size of an area. | ||
| 1459 | {my ($area, $name) = @_; # Location of area, name of area | ||
| 1460 | my $t = &Var(); | ||
| 1461 | $assembly->instruction(action=>"areaSize", # 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. | ||
| 1462 | target=>RefLeft($t), xSource($area), source2=>$name); | ||
| 1463 | $t | ||
| 1464 | } | ||
| 1465 | |||
| 1466 | sub Call($) # Call the subroutine at the target address. | ||
| 1467 | {my ($p) = @_; # Procedure description | ||
| 1468 | $assembly->instruction(action=>"call", | ||
| 1469 | target=>RefLeft($p->target), source=>$p); | ||
| 1470 | } | ||
| 1471 | |||
| 1472 | sub Confess() # Confess. | ||
| 1473 | {$assembly->instruction(action=>"confess"); | ||
| 1474 | } | ||
| 1475 | |||
| 1476 | sub Dump(;$) # Dump memory. | ||
| 1477 | {my ($title) = @_; # Title | ||
| 1478 | $assembly->instruction(action=>"dump", source=>$title); | ||
| 1479 | } | ||
| 1480 | |||
| 1481 | sub DumpArray($;$) # Dump an array. | ||
| 1482 | {my ($target, $title) = @_; # Array to dump, title of dump | ||
| 1483 | $assembly->instruction(action=>"dumpArray", target=>RefRight($target), source=>$title); | ||
| 1484 | } | ||
| 1485 | |||
| 1486 | sub Trace($) # Trace. | ||
| 1487 | {my ($source) = @_; # Trace setting | ||
| 1488 | $assembly->instruction(action=>"trace", xSource($source)); | ||
| 1489 | } | ||
| 1490 | |||
| 1491 | sub TracePoints($) # Enable trace points. | ||
| 1492 | {my ($source) = @_; # Trace points if true | ||
| 1493 | $assembly->instruction(action=>"tracePoints", xSource($source)); | ||
| 1494 | } | ||
| 1495 | |||
| 1496 | sub TracePoint(%) #P Trace point - a point in the code where the flow of execution might change. | ||
| 1497 | {my (%options) = @_; # Parameters | ||
| 1498 | $assembly->instruction(action=>"tracePoint", %options); | ||
| 1499 | } | ||
| 1500 | |||
| 1501 | sub Dec($) # Decrement the target. | ||
| 1502 | {my ($target) = @_; # Target address | ||
| 1503 | $assembly->instruction(action=>"dec", xTarget($target)) | ||
| 1504 | } | ||
| 1505 | |||
| 1506 | sub Inc($) # Increment the target. | ||
| 1507 | {my ($target) = @_; # Target address | ||
| 1508 | $assembly->instruction(action=>"inc", xTarget($target)) | ||
| 1509 | } | ||
| 1510 | |||
| 1511 | sub Jmp($) # Jump to a label. | ||
| 1512 | {my ($target) = @_; # Target address | ||
| 1513 | $assembly->instruction(action=>"jmp", xTarget($target)); | ||
| 1514 | } | ||
| 1515 | |||
| 1516 | sub Jle($$$) # Jump to a target label if the first source field is less than or equal to the second source field. | ||
| 1517 | {my ($target, $source, $source2) = @_; # Target label, source to test | ||
| 1518 | $assembly->instruction(action=>"jLe", | ||
| 1519 | xTarget($target), xSource($source), xSource2($source2)); | ||
| 1520 | } | ||
| 1521 | |||
| 1522 | sub Jlt($$$) # Jump to a target label if the first source field is less than the second source field. | ||
| 1523 | {my ($target, $source, $source2) = @_; # Target label, source to test | ||
| 1524 | $assembly->instruction(action=>"jLt", | ||
| 1525 | xTarget($target), xSource($source), xSource2($source2)); | ||
| 1526 | } | ||
| 1527 | |||
| 1528 | sub Jge($$$) # Jump to a target label if the first source field is greater than or equal to the second source field. | ||
| 1529 | {my ($target, $source, $source2) = @_; # Target label, source to test | ||
| 1530 | $assembly->instruction(action=>"jGe", | ||
| 1531 | xTarget($target), xSource($source), xSource2($source2)); | ||
| 1532 | } | ||
| 1533 | |||
| 1534 | sub Jgt($$$) # Jump to a target label if the first source field is greater than the second source field. | ||
| 1535 | {my ($target, $source, $source2) = @_; # Target label, source to test | ||
| 1536 | $assembly->instruction(action=>"jGt", | ||
| 1537 | xTarget($target), xSource($source), xSource2($source2)); | ||
| 1538 | } | ||
| 1539 | |||
| 1540 | sub Jeq($$$) # Jump to a target label if the first source field is equal to the second source field. | ||
| 1541 | {my ($target, $source, $source2) = @_; # Target label, source to test | ||
| 1542 | $assembly->instruction(action=>"jEq", | ||
| 1543 | xTarget($target), xSource($source), xSource2($source2)); | ||
| 1544 | } | ||
| 1545 | |||
| 1546 | sub Jne($$$) # Jump to a target label if the first source field is not equal to the second source field. | ||
| 1547 | {my ($target, $source, $source2) = @_; # Target label, source to test | ||
| 1548 | $assembly->instruction(action=>"jNe", | ||
| 1549 | xTarget($target), xSource($source), xSource2($source2)); | ||
| 1550 | } | ||
| 1551 | |||
| 1552 | sub Label($) # Create a label.. | ||
| 1553 | {my ($source) = @_; # Name of label | ||
| 1554 | $assembly->instruction(action=>"label", xSource($source)); | ||
| 1555 | } | ||
| 1556 | |||
| 1557 | 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.. | ||
| 1558 | {my ($target) = @_; # Target address, source address | ||
| 1559 | $assembly->instruction(action=>"clear", xTarget($target)); | ||
| 1560 | } | ||
| 1561 | |||
| 1562 | sub LeAddress($;$) { # Load the address component. | ||
| 1563 | if (@_ == 1) | ||
| 1564 | {my ($source) = @_; # Target address, source address | ||
| 1565 | my $t = &Var(); | ||
| 1566 | $assembly->instruction(action=>"leAddress", | ||
| 1567 | target=>RefLeft($t), xSource($source)); | ||
| 1568 | return $t; | ||
| 1569 | } | ||
| 1570 | elsif (@ == 2) | ||
| 1571 | {my ($target, $source) = @_; # Target address, source address | ||
| 1572 | $assembly->instruction(action=>"leAddress", | ||
| 1573 | xTarget($target), xSource($source)); | ||
| 1574 | } | ||
| 1575 | else | ||
| 1576 | {confess "One or two parameters required"; | ||
| 1577 | } | ||
| 1578 | } | ||
| 1579 | |||
| 1580 | sub LeArea($;$) { # Load the address component. | ||
| 1581 | if (@_ == 1) | ||
| 1582 | {my ($source) = @_; # Target address, source address | ||
| 1583 | my $t = &Var(); | ||
| 1584 | $assembly->instruction(action=>"leArea", | ||
| 1585 | target=>RefLeft($t), xSource($source)); | ||
| 1586 | return $t; | ||
| 1587 | } | ||
| 1588 | elsif (@ == 2) | ||
| 1589 | {my ($target, $source) = @_; # Target address, source address | ||
| 1590 | $assembly->instruction(action=>"leArea", | ||
| 1591 | xTarget($target), xSource($source)); | ||
| 1592 | } | ||
| 1593 | else | ||
| 1594 | {confess "One or two parameters required"; | ||
| 1595 | } | ||
| 1596 | } | ||
| 1597 | |||
| 1598 | sub Mov($;$) { # Copy a constant or memory address to the target address. | ||
| 1599 | if (@_ == 1) | ||
| 1600 | {my ($source) = @_; # Target address, source address | ||
| 1601 | my $t = &Var(); | ||
| 1602 | $assembly->instruction(action=>"mov", target=>RefLeft($t),xSource($source)); | ||
| 1603 | return $t; | ||
| 1604 | } | ||
| 1605 | elsif (@ == 2) | ||
| 1606 | {my ($target, $source) = @_; # Target address, source address | ||
| 1607 | $assembly->instruction(action=>"mov", xTarget($target), xSource($source)); | ||
| 1608 | } | ||
| 1609 | else | ||
| 1610 | {confess "One or two parameters required for mov"; | ||
| 1611 | } | ||
| 1612 | } | ||
| 1613 | |||
| 1614 | sub Not($) { # Move and not. | ||
| 1615 | if (@_ == 1) | ||
| 1616 | {my ($source) = @_; # Target address, source address | ||
| 1617 | my $t = &Var(); | ||
| 1618 | $assembly->instruction(action=>"not", target=>RefLeft($t),xSource($source)); | ||
| 1619 | return $t; | ||
| 1620 | } | ||
| 1621 | elsif (@ == 2) | ||
| 1622 | {my ($target, $source) = @_; # Target address, source address | ||
| 1623 | $assembly->instruction(action=>"not", xTarget($target), xSource($source)); | ||
| 1624 | } | ||
| 1625 | else | ||
| 1626 | {confess "One or two parameters required for not"; | ||
| 1627 | } | ||
| 1628 | } | ||
| 1629 | |||
| 1630 | sub Nop() # Do nothing (but do it well!). | ||
| 1631 | {$assembly->instruction(action=>"nop"); | ||
| 1632 | } | ||
| 1633 | |||
| 1634 | sub Out($) # Write memory contents to out. | ||
| 1635 | {my ($source) = @_; # Either a scalar constant or memory address to output | ||
| 1636 | $assembly->instruction(action=>"out", xSource($source)) | ||
| 1637 | } | ||
| 1638 | |||
| 1639 | sub Procedure($$) # Define a procedure. | ||
| 1640 | {my ($name, $source) = @_; # Name of procedure, source code as a subroutine# $assembly->instruction(action=>"procedure", target=>$target, source=>$source); | ||
| 1641 | if ($name and my $n = $assembly->procedures->{$name}) # Reuse existing named procedure | ||
| 1642 | {return $n; | ||
| 1643 | } | ||
| 1644 | |||
| 1645 | Jmp(my $end = label); # Jump over the code of the procedure body | ||
| 1646 | my $start = setLabel; | ||
| 1647 | my $p = procedure($start); # Procedure description | ||
| 1648 | my $save_registers = $assembly->variables; | ||
| 1649 | $assembly->variables = $p->variables; | ||
| 1650 | &$source($p); # Code of procedure called with start label as a parameter | ||
| 1651 | &Return; | ||
| 1652 | $assembly->variables = $save_registers; | ||
| 1653 | |||
| 1654 | setLabel $end; | ||
| 1655 | $assembly->procedures->{$name} = $p; # Return the start of the procedure | ||
| 1656 | } | ||
| 1657 | |||
| 1658 | sub ParamsGet($;$) { # Get a word from the parameters in the previous frame and store it in the current frame. | ||
| 1659 | if (@_ == 1) | ||
| 1660 | {my ($source) = @_; # Memory address to place parameter in, parameter number | ||
| 1661 | my $p = &Var(); | ||
| 1662 | $assembly->instruction(action=>"paramsGet", | ||
| 1663 | target=>RefLeft($p), xSource($source)); | ||
| 1664 | return $p; | ||
| 1665 | } | ||
| 1666 | elsif (@_ == 2) | ||
| 1667 | {my ($target, $source) = @_; # Memory address to place parameter in, parameter number | ||
| 1668 | $assembly->instruction(action=>"paramsGet", | ||
| 1669 | xTarget($target), xSource($source)); | ||
| 1670 | } | ||
| 1671 | else | ||
| 1672 | {confess "One or two parameters required"; | ||
| 1673 | } | ||
| 1674 | } | ||
| 1675 | |||
| 1676 | sub ParamsPut($$) # Put a word into the parameters list to make it visible in a called procedure. | ||
| 1677 | {my ($target, $source) = @_; # Parameter number, address to fetch parameter from | ||
| 1678 | $assembly->instruction(action=>"paramsPut", | ||
| 1679 | xTarget($target), xSource($source)); | ||
| 1680 | } | ||
| 1681 | |||
| 1682 | sub Return() # Return from a procedure via the call stack. | ||
| 1683 | {$assembly->instruction(action=>"return"); | ||
| 1684 | } | ||
| 1685 | |||
| 1686 | sub ReturnGet($;$) { # Get a word from the return area and save it. | ||
| 1687 | if (@_ == 1) # Create a variable | ||
| 1688 | {my ($source) = @_; # Memory address to place return value in, return value to get | ||
| 1689 | my $p = &Var(); | ||
| 1690 | $assembly->instruction(action=>"returnGet", | ||
| 1691 | target=>RefLeft($p), xSource($source)); | ||
| 1692 | return $p; | ||
| 1693 | } | ||
| 1694 | elsif (@_ == 2) | ||
| 1695 | {my ($target, $source) = @_; # Memory address to place return value in, return value to get | ||
| 1696 | $assembly->instruction(action=>"returnGet", | ||
| 1697 | xTarget($target), xSource($source)); | ||
| 1698 | } | ||
| 1699 | else | ||
| 1700 | {confess "One or two parameters required"; | ||
| 1701 | } | ||
| 1702 | } | ||
| 1703 | |||
| 1704 | sub ReturnPut($$) # Put a word into the return area. | ||
| 1705 | {my ($target, $source) = @_; # Offset in return area to write to, memory address whose contents are to be placed in the return area | ||
| 1706 | $assembly->instruction(action=>"returnPut", | ||
| 1707 | xTarget($target), xSource($source)); | ||
| 1708 | } | ||
| 1709 | |||
| 1710 | sub Resize($$) # Resize the target area to the source size. | ||
| 1711 | {my ($target, $source) = @_; # Target address, source address | ||
| 1712 | $assembly->instruction(action=>"resize", xTarget($target), xSource($source)); | ||
| 1713 | } | ||
| 1714 | |||
| 1715 | sub Pop(;$$) { # Pop the memory area specified by the source operand into the memory address specified by the target operand. | ||
| 1716 | if (@_ == 0) # Pop current stack frame into a local variable | ||
| 1717 | {my $p = &Var(); | ||
| 1718 | my $i = $assembly->instruction(action=>"pop", target=>RefLeft($p)); | ||
| 1719 | $i->source = undef; | ||
| 1720 | return $p; | ||
| 1721 | } | ||
| 1722 | elsif (@_ == 1) # Pop indicated area into a local variable | ||
| 1723 | {my ($source) = @_; # Memory address to place return value in, return value to get | ||
| 1724 | my $p = &Var(); | ||
| 1725 | $assembly->instruction(action=>"pop", target=>RefLeft($p),xSource($source)); | ||
| 1726 | return $p; | ||
| 1727 | } | ||
| 1728 | elsif (@_ == 2) | ||
| 1729 | {my ($target, $source) = @_; # Pop indicated area into target address | ||
| 1730 | $assembly->instruction(action=>"pop", xTarget($target), xSource($source)); | ||
| 1731 | } | ||
| 1732 | else | ||
| 1733 | {confess "Zero, One or two parameters required"; | ||
| 1734 | } | ||
| 1735 | } | ||
| 1736 | |||
| 1737 | sub Push($;$) { # Push the value in the current stack frame specified by the source operand onto the memory area identified by the target operand.. | ||
| 1738 | if (@_ == 1) | ||
| 1739 | {my ($source) = @_; # Push a value onto the current stack frame | ||
| 1740 | $assembly->instruction(action=>"push", xSource($source)); | ||
| 1741 | } | ||
| 1742 | elsif (@_ == 2) # Push a value onto the specified memory area | ||
| 1743 | {my ($target, $source) = @_; # Memory area to push to, memory containing value to push | ||
| 1744 | $assembly->instruction(action=>"push", xTarget($target), xSource($source)); | ||
| 1745 | } | ||
| 1746 | else | ||
| 1747 | {confess "One or two parameters required"; | ||
| 1748 | } | ||
| 1749 | } | ||
| 1750 | |||
| 1751 | sub ShiftLeft($;$) { # Shift left within an element. | ||
| 1752 | my ($target, $source) = @_; # Target to shift, amount to shift | ||
| 1753 | $assembly->instruction(action=>"shiftLeft", | ||
| 1754 | xTarget($target), xSource($source)); | ||
| 1755 | $target | ||
| 1756 | } | ||
| 1757 | |||
| 1758 | sub ShiftRight($;$) { # Shift right with an element. | ||
| 1759 | my ($target, $source) = @_; # Target to shift, amount to shift | ||
| 1760 | $assembly->instruction(action=>"shiftRight", | ||
| 1761 | xTarget($target), xSource($source)); | ||
| 1762 | $target | ||
| 1763 | } | ||
| 1764 | |||
| 1765 | sub ShiftUp($;$) # Shift an element up one in an area. | ||
| 1766 | {my ($target, $source) = @_; # Target to shift, amount to shift | ||
| 1767 | $assembly->instruction(action=>"shiftUp", | ||
| 1768 | xTarget($target), xSource($source)); | ||
| 1769 | $target | ||
| 1770 | } | ||
| 1771 | |||
| 1772 | sub ShiftDown($;$) { # Shift an element down one in an area. | ||
| 1773 | if (@_ == 1) # Create a variable | ||
| 1774 | {my ($source) = @_; # Memory address to place return value in, return value to get | ||
| 1775 | my $p = &Var(); | ||
| 1776 | $assembly->instruction(action=>"shiftDown", | ||
| 1777 | target=>RefLeft($p), xSource($source)); | ||
| 1778 | return $p; | ||
| 1779 | } | ||
| 1780 | elsif (@_ == 2) | ||
| 1781 | {my ($target, $source) = @_; # Memory address to place return value in, return value to get | ||
| 1782 | $assembly->instruction(action=>"shiftDown", | ||
| 1783 | xTarget($target), xSource($source)); | ||
| 1784 | confess "Needs work"; | ||
| 1785 | return $target; | ||
| 1786 | } | ||
| 1787 | else | ||
| 1788 | {confess "One or two parameters required"; | ||
| 1789 | } | ||
| 1790 | } | ||
| 1791 | |||
| 1792 | sub Watch($) # Shift an element down one in an area. | ||
| 1793 | {my ($target) = @_; # Memory address to watch | ||
| 1794 | $assembly->instruction(action=>"watch", xTarget($target)); | ||
| 1795 | } | ||
| 1796 | |||
| 1797 | sub Tally($) # Counts instructions when enabled. | ||
| 1798 | {my ($source) = @_; # Tally instructions when true | ||
| 1799 | $assembly->instruction(action=>"tally", xSource($source)); | ||
| 1800 | } | ||
| 1801 | |||
| 1802 | sub Then(&) # Then block. | ||
| 1803 | {my ($t) = @_; # Then block subroutine | ||
| 1804 | @_ == 1 or confess "One parameter"; | ||
| 1805 | (then=> $t) | ||
| 1806 | } | ||
| 1807 | |||
| 1808 | sub Else(&) # Else block. | ||
| 1809 | {my ($e) = @_; # Else block subroutine | ||
| 1810 | @_ == 1 or confess "One parameter"; | ||
| 1811 | (else=> $e) | ||
| 1812 | } | ||
| 1813 | |||
| 1814 | sub Ifx($$$%) #P Execute then or else clause depending on whether two memory locations are equal.. | ||
| 1815 | {my ($cmp, $a, $b, %options) = @_; # Comparison, first memory address, second memory address, then block, else block | ||
| 1816 | confess "Then required" unless $options{then}; | ||
| 1817 | if ($options{else}) | ||
| 1818 | {my $else = label; | ||
| 1819 | my $end = label; | ||
| 1820 | &$cmp($else, $a, $b); | ||
| 1821 | TracePoint level=>2; | ||
| 1822 | &{$options{then}}; | ||
| 1823 | Jmp $end; | ||
| 1824 | setLabel($else); | ||
| 1825 | TracePoint level=>2; | ||
| 1826 | &{$options{else}}; | ||
| 1827 | setLabel($end); | ||
| 1828 | } | ||
| 1829 | else | ||
| 1830 | {my $end = label; | ||
| 1831 | &$cmp($end, $a, $b); | ||
| 1832 | &{$options{then}}; | ||
| 1833 | setLabel($end); | ||
| 1834 | } | ||
| 1835 | } | ||
| 1836 | |||
| 1837 | sub IfFalse($%) # Execute then clause if the specified memory address is zero representing false. | ||
| 1838 | {my ($a, %options) = @_; # Memory address, then block, else block | ||
| 1839 | Ifx(\&Jne, $a, 0, %options); | ||
| 1840 | } | ||
| 1841 | |||
| 1842 | sub IfTrue($%) # Execute then clause if the specified memory address is not zero representing true. | ||
| 1843 | {my ($a, %options) = @_; # Memory address, then block, else block | ||
| 1844 | Ifx(\&Jeq, $a, 0, %options); | ||
| 1845 | } | ||
| 1846 | |||
| 1847 | sub IfEq($$%) # Execute then or else clause depending on whether two memory locations are equal.. | ||
| 1848 | {my ($a, $b, %options) = @_; # First memory address, second memory address, then block, else block | ||
| 1849 | Ifx(\&Jne, $a, $b, %options); | ||
| 1850 | } | ||
| 1851 | |||
| 1852 | sub IfNe($$%) # Execute then or else clause depending on whether two memory locations are not equal.. | ||
| 1853 | {my ($a, $b, %options) = @_; # First memory address, second memory address, then block, else block | ||
| 1854 | Ifx(\&Jeq, $a, $b, %options); | ||
| 1855 | } | ||
| 1856 | |||
| 1857 | sub IfLt($$%) # Execute then or else clause depending on whether two memory locations are less than.. | ||
| 1858 | {my ($a, $b, %options) = @_; # First memory address, second memory address, then block, else block | ||
| 1859 | Ifx(\&Jge, $a, $b, %options); | ||
| 1860 | } | ||
| 1861 | |||
| 1862 | sub IfLe($$%) # Execute then or else clause depending on whether two memory locations are less than or equal.. | ||
| 1863 | {my ($a, $b, %options) = @_; # First memory address, second memory address, then block, else block | ||
| 1864 | Ifx(\&Jgt, $a, $b, %options); | ||
| 1865 | } | ||
| 1866 | |||
| 1867 | sub IfGt($$%) # Execute then or else clause depending on whether two memory locations are greater than.. | ||
| 1868 | {my ($a, $b, %options) = @_; # First memory address, second memory address, then block, else block | ||
| 1869 | Ifx(\&Jle, $a, $b, %options); | ||
| 1870 | } | ||
| 1871 | |||
| 1872 | sub IfGe($$%) # Execute then or else clause depending on whether two memory locations are greater than or equal.. | ||
| 1873 | {my ($a, $b, %options) = @_; # First memory address, second memory address, then block, else block | ||
| 1874 | Ifx(\&Jlt, $a, $b, %options); | ||
| 1875 | } | ||
| 1876 | |||
| 1877 | sub AssertOp($$$) #P Assert operation. | ||
| 1878 | {my ($op, $a, $b) = @_; # Operation, First memory address, second memory address | ||
| 1879 | $assembly->instruction(action=>"assert$op", | ||
| 1880 | xSource($a), xSource2($b), level=>2); | ||
| 1881 | } | ||
| 1882 | |||
| 1883 | sub Assert(%) # Assert regardless. | ||
| 1884 | {my (%options) = @_; # Options | ||
| 1885 | $assembly->instruction(action=>"assert"); | ||
| 1886 | } | ||
| 1887 | |||
| 1888 | sub AssertEq($$%) # Assert two memory locations are equal.. | ||
| 1889 | {my ($a, $b, %options) = @_; # First memory address, second memory address | ||
| 1890 | AssertOp("Eq", $a, $b); | ||
| 1891 | } | ||
| 1892 | |||
| 1893 | sub AssertNe($$%) # Assert two memory locations are not equal.. | ||
| 1894 | {my ($a, $b, %options) = @_; # First memory address, second memory address | ||
| 1895 | AssertOp("Ne", $a, $b); | ||
| 1896 | } | ||
| 1897 | |||
| 1898 | sub AssertLt($$%) # Assert two memory locations are less than.. | ||
| 1899 | {my ($a, $b, %options) = @_; # First memory address, second memory address | ||
| 1900 | AssertOp("Lt", $a, $b); | ||
| 1901 | } | ||
| 1902 | |||
| 1903 | sub AssertLe($$%) # Assert two memory locations are less than or equal.. | ||
| 1904 | {my ($a, $b, %options) = @_; # First memory address, second memory address | ||
| 1905 | AssertOp("Le", $a, $b); | ||
| 1906 | } | ||
| 1907 | |||
| 1908 | sub AssertGt($$%) # Assert two memory locations are greater than.. | ||
| 1909 | {my ($a, $b, %options) = @_; # First memory address, second memory address | ||
| 1910 | AssertOp("Gt", $a, $b); | ||
| 1911 | } | ||
| 1912 | |||
| 1913 | sub AssertGe($$%) # Assert are greater than or equal.. | ||
| 1914 | {my ($a, $b, %options) = @_; # First memory address, second memory address | ||
| 1915 | AssertOp("Ge", $a, $b); | ||
| 1916 | } | ||
| 1917 | |||
| 1918 | sub For(&$%) # For loop 0..range-1 or in reverse. | ||
| 1919 | {my ($block, $range, %options) = @_; # Block, limit, options | ||
| 1920 | if (!exists $options{reverse}) # Ascending order | ||
| 1921 | {my $s = 0; my $e = $range; # Start, end | ||
| 1922 | ($s, $e) = @$range if ref($e) =~ m(ARRAY); # Start, end as a reference | ||
| 1923 | |||
| 1924 | my ($Start, $Check, $Next, $End) = (label, label, label, label); | ||
| 1925 | |||
| 1926 | setLabel($Start); # Start | ||
| 1927 | my $i = Mov $s; | ||
| 1928 | setLabel($Check); # Check | ||
| 1929 | Jge $End, $i, $e; | ||
| 1930 | TracePoint level=>2; | ||
| 1931 | &$block($i, $Check, $Next, $End); # Block | ||
| 1932 | setLabel($Next); | ||
| 1933 | Inc $i; # Next | ||
| 1934 | Jmp $Check; | ||
| 1935 | setLabel($End); # End | ||
| 1936 | } | ||
| 1937 | else | ||
| 1938 | {my $s = $range; my $e = 0; # Start, end | ||
| 1939 | ($e, $s) = @$range if ref($e) =~ m(ARRAY); # End, start as a reference | ||
| 1940 | |||
| 1941 | my ($Start, $Check, $Next, $End) = (label, label, label, label); | ||
| 1942 | |||
| 1943 | setLabel($Start); # Start | ||
| 1944 | my $i = Subtract $s, 1; | ||
| 1945 | Subtract $i, $s; | ||
| 1946 | setLabel($Check); # Check | ||
| 1947 | Jlt $End, $i, $e; | ||
| 1948 | TracePoint level=>2; | ||
| 1949 | &$block($i, $Check, $Next, $End); # Block | ||
| 1950 | setLabel($Next); | ||
| 1951 | Dec $i; # Next | ||
| 1952 | Jmp $Check; | ||
| 1953 | setLabel($End); # End | ||
| 1954 | } | ||
| 1955 | } | ||
| 1956 | |||
| 1957 | sub ForArray(&$$%) # For loop to process each element of the named area. | ||
| 1958 | {my ($block, $area, $name, %options) = @_; # Block of code, area, area name, options | ||
| 1959 | my $e = AreaSize $area, $name; # End | ||
| 1960 | my $s = 0; # Start | ||
| 1961 | |||
| 1962 | my ($Start, $Check, $Next, $End) = (label, label, label, label); | ||
| 1963 | |||
| 1964 | setLabel($Start); # Start | ||
| 1965 | my $i = Mov $s; | ||
| 1966 | setLabel($Check); # Check | ||
| 1967 | Jge $End, $i, $e; | ||
| 1968 | TracePoint level=>2; | ||
| 1969 | my $a = Mov [$area, \$i, $name]; | ||
| 1970 | &$block($i, $a, $Check, $Next, $End); # Block | ||
| 1971 | setLabel($Next); | ||
| 1972 | Inc $i; # Next | ||
| 1973 | Jmp $Check; | ||
| 1974 | setLabel($End); # End | ||
| 1975 | } | ||
| 1976 | |||
| 1977 | sub Good(&) # A good ending. | ||
| 1978 | {my ($good) = @_; # What to do on a good ending | ||
| 1979 | @_ == 1 or confess "One parameter"; | ||
| 1980 | (good=> $good) | ||
| 1981 | } | ||
| 1982 | |||
| 1983 | sub Bad(&) # A bad ending. | ||
| 1984 | {my ($bad) = @_; # What to do on a bad ending | ||
| 1985 | @_ == 1 or confess "One parameter"; | ||
| 1986 | (bad=> $bad) | ||
| 1987 | } | ||
| 1988 | |||
| 1989 | sub Block(&%) # Block of code that can either be restarted or come to a good or a bad ending. | ||
| 1990 | {my ($block, %options) = @_; # Block, options | ||
| 1991 | my ($Start, $Good, $Bad, $End) = (label, label, label, label); | ||
| 1992 | |||
| 1993 | my $g = $options{good}; | ||
| 1994 | my $b = $options{bad}; | ||
| 1995 | |||
| 1996 | setLabel($Start); # Start | ||
| 1997 | |||
| 1998 | TracePoint level=>2; | ||
| 1999 | &$block($Start, $Good, $Bad, $End); # Code of block | ||
| 2000 | |||
| 2001 | if ($g) # Good | ||
| 2002 | {Jmp $End; | ||
| 2003 | setLabel($Good); | ||
| 2004 | TracePoint level=>2; | ||
| 2005 | &$g; | ||
| 2006 | } | ||
| 2007 | |||
| 2008 | if ($b) # Bad | ||
| 2009 | {Jmp $End; | ||
| 2010 | setLabel($Bad); | ||
| 2011 | TracePoint level=>2; | ||
| 2012 | &$b; | ||
| 2013 | } | ||
| 2014 | setLabel($End); # End | ||
| 2015 | } | ||
| 2016 | |||
| 2017 | sub Var(;$) #P Create a variable initialized to the specified value. | ||
| 2018 | {my ($value) = @_; # Value | ||
| 2019 | my $i = $assembly->registers; | ||
| 2020 | my $a = $assembly->variables; | ||
| 2021 | my $v = $a->count-1; | ||
| 2022 | my $c = $assembly->code->@*; | ||
| 2023 | # $a->instructions->[$v] = $c; | ||
| 2024 | |||
| 2025 | Mov $i, $value if defined $value; | ||
| 2026 | $i | ||
| 2027 | } | ||
| 2028 | |||
| 2029 | sub Execute(%) # Execute the current assembly. | ||
| 2030 | {my (%options) = @_; # Options | ||
| 2031 | my $r = $assembly->execute(%options); # Execute the code in the current assembly | ||
| 2032 | if (my $out = $options{out}) | ||
| 2033 | {my $c = compareArraysAndExplain $r->out, $out; | ||
| 2034 | lll $c if $c; | ||
| 2035 | say STDERR dump($r->out) if $c; | ||
| 2036 | return 0 if $c; | ||
| 2037 | } | ||
| 2038 | if (my $memory = $options{memory}) | ||
| 2039 | {my $E = dump $memory; | ||
| 2040 | my $G = dump $r->memory; | ||
| 2041 | my $e = [split "\n", $E]; | ||
| 2042 | my $g = [split "\n", $G]; | ||
| 2043 | my $c = compareArraysAndExplain $g, $e; | ||
| 2044 | lll "$c\n$G\n" if $c; | ||
| 2045 | return 0 if $c; | ||
| 2046 | } | ||
| 2047 | return $r; | ||
| 2048 | } | ||
| 2049 | |||
| 2050 | use Exporter qw(import); | ||
| 2051 | use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||
| 2052 | |||
| 2053 | @ISA = qw(Exporter); | ||
| 2054 | @EXPORT = qw(); | ||
| 2055 | @EXPORT_OK = qw(Add AreaStructure Array 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 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); | ||
| 2056 | #say STDERR '@EXPORT_OK = qw(', (join ' ', sort @EXPORT_OK), ')'; exit; | ||
| 2057 | %EXPORT_TAGS = (all=>[@EXPORT, @EXPORT_OK]); | ||
| 2058 | |||
| 2059 | return 1 if caller; | ||
| 2060 | |||
| 2061 | Test::More->builder->output("/dev/null"); # Reduce number of confirmation messages during testing | ||
| 2062 | |||
| 2063 | eval {goto latest}; | ||
| 2064 | sub is_deeply; | ||
| 2065 | sub ok($;$); | ||
| 2066 | |||
| 2067 | #D1 Tests | ||
| 2068 | |||
| 2069 | if (1) # Address dereferencing | ||
| 2070 | {my $e = execute->createInitialStackEntry->setMemoryType(1, 'aaa'); | ||
| 2071 | my $s = $e->stackArea; | ||
| 2072 | |||
| 2073 | $e->set(RefLeft([ 1, 1, 'aaa']), 99); | ||
| 2074 | $e->set(RefLeft([$s, 2, $s]), 1); | ||
| 2075 | |||
| 2076 | is_deeply $e->right(RefRight [1, \1, 'aaa']), 99; | ||
| 2077 | is_deeply $e->right(RefRight [1, \\2, 'aaa']), 99; | ||
| 2078 | |||
| 2079 | is_deeply $e->left (RefLeft [1, \1, 'aaa']), {address=>1, area=>1, name=>"aaa"}; | ||
| 2080 | is_deeply $e->left (RefLeft [1, \\2, 'aaa']), {address=>1, area=>1, name=>"aaa"}; | ||
| 2081 | } | ||
| 2082 | |||
| 2083 | #D1 Examples | ||
| 2084 | |||
| 2085 | #latest:; | ||
| 2086 | if (1) #TOut #TStart #TExecute | ||
| 2087 | {Start 1; | ||
| 2088 | Out "hello World"; | ||
| 2089 | my $e = Execute(suppressOutput=>1); | ||
| 2090 | is_deeply $e->out, ["hello World"]; | ||
| 2091 | } | ||
| 2092 | |||
| 2093 | #latest:; | ||
| 2094 | if (1) #TNop | ||
| 2095 | {Start 1; | ||
| 2096 | Nop; | ||
| 2097 | ok Execute(out=>[]); | ||
| 2098 | } | ||
| 2099 | |||
| 2100 | #latest:; | ||
| 2101 | if (1) #TMov | ||
| 2102 | {Start 1; | ||
| 2103 | my $a = Mov 2; | ||
| 2104 | Out $a; | ||
| 2105 | |||
| 2106 | my $e = Execute(suppressOutput=>1); | ||
| 2107 | is_deeply $e->out, [2]; | ||
| 2108 | } | ||
| 2109 | |||
| 2110 | #latest:; | ||
| 2111 | if (1) | ||
| 2112 | {Start 1; #TMov | ||
| 2113 | my $a = Mov 3; | ||
| 2114 | my $b = Mov $$a; | ||
| 2115 | my $c = Mov \$b; | ||
| 2116 | Out $c; | ||
| 2117 | my $e = Execute(suppressOutput=>1); | ||
| 2118 | is_deeply $e->out, [3]; | ||
| 2119 | } | ||
| 2120 | |||
| 2121 | #latest:; | ||
| 2122 | if (1) #TAdd | ||
| 2123 | {Start 1; | ||
| 2124 | my $a = Add 3, 2; | ||
| 2125 | Out $a; | ||
| 2126 | my $e = Execute(suppressOutput=>1); | ||
| 2127 | is_deeply $e->out, [5]; | ||
| 2128 | } | ||
| 2129 | |||
| 2130 | #latest:; | ||
| 2131 | if (1) #TAdd #TSubtract | ||
| 2132 | {Start 1; | ||
| 2133 | my $a = Subtract 4, 2; | ||
| 2134 | Out $a; | ||
| 2135 | my $e = Execute(suppressOutput=>1); | ||
| 2136 | is_deeply $e->out, [2]; | ||
| 2137 | } | ||
| 2138 | |||
| 2139 | #latest:; | ||
| 2140 | if (1) #TDec | ||
| 2141 | {Start 1; | ||
| 2142 | my $a = Mov 3; | ||
| 2143 | Dec $a; | ||
| 2144 | Out $a; | ||
| 2145 | my $e = Execute(suppressOutput=>1); | ||
| 2146 | is_deeply $e->out, [2]; | ||
| 2147 | } | ||
| 2148 | |||
| 2149 | #latest:; | ||
| 2150 | if (1) #TInc | ||
| 2151 | {Start 1; | ||
| 2152 | my $a = Mov 3; | ||
| 2153 | Inc $a; | ||
| 2154 | Out $a; | ||
| 2155 | my $e = Execute(suppressOutput=>1); | ||
| 2156 | is_deeply $e->out, [4]; | ||
| 2157 | } | ||
| 2158 | |||
| 2159 | #latest:; | ||
| 2160 | if (1) #TNot | ||
| 2161 | {Start 1; | ||
| 2162 | my $a = Mov 3; | ||
| 2163 | my $b = Not $a; | ||
| 2164 | my $c = Not $b; | ||
| 2165 | Out $a; | ||
| 2166 | Out $b; | ||
| 2167 | Out $c; | ||
| 2168 | my $e = Execute(suppressOutput=>1); | ||
| 2169 | is_deeply $e->out, [3, "", 1]; | ||
| 2170 | } | ||
| 2171 | |||
| 2172 | #latest:; | ||
| 2173 | if (1) #TShiftLeft | ||
| 2174 | {Start 1; | ||
| 2175 | my $a = Mov 1; | ||
| 2176 | ShiftLeft $a, $a; | ||
| 2177 | Out $a; | ||
| 2178 | my $e = Execute(suppressOutput=>1); | ||
| 2179 | is_deeply $e->out, [2]; | ||
| 2180 | } | ||
| 2181 | |||
| 2182 | #latest:; | ||
| 2183 | if (1) #TShiftRight | ||
| 2184 | {Start 1; | ||
| 2185 | my $a = Mov 4; | ||
| 2186 | ShiftRight $a, 1; | ||
| 2187 | Out $a; | ||
| 2188 | my $e = Execute(suppressOutput=>1); | ||
| 2189 | is_deeply $e->out, [2]; | ||
| 2190 | } | ||
| 2191 | |||
| 2192 | #latest:; | ||
| 2193 | if (1) #TJmp | ||
| 2194 | {Start 1; | ||
| 2195 | Jmp (my $a = label); | ||
| 2196 | Out 1; | ||
| 2197 | Jmp (my $b = label); | ||
| 2198 | setLabel($a); | ||
| 2199 | Out 2; | ||
| 2200 | setLabel($b); | ||
| 2201 | my $e = Execute(suppressOutput=>1); | ||
| 2202 | is_deeply $e->out, [2]; | ||
| 2203 | } | ||
| 2204 | |||
| 2205 | #latest:; | ||
| 2206 | if (1) #TJLt #TLabel | ||
| 2207 | {Start 1; | ||
| 2208 | Mov 0, 1; | ||
| 2209 | Jlt ((my $a = label), \0, 2); | ||
| 2210 | Out 1; | ||
| 2211 | Jmp (my $b = label); | ||
| 2212 | setLabel($a); | ||
| 2213 | Out 2; | ||
| 2214 | setLabel($b); | ||
| 2215 | |||
| 2216 | Jgt ((my $c = label), \0, 3); | ||
| 2217 | Out 1; | ||
| 2218 | Jmp (my $d = label); | ||
| 2219 | setLabel($c); | ||
| 2220 | Out 2; | ||
| 2221 | setLabel($d); | ||
| 2222 | my $e = Execute(suppressOutput=>1); | ||
| 2223 | is_deeply $e->out, [2,1]; | ||
| 2224 | } | ||
| 2225 | |||
| 2226 | #latest:; | ||
| 2227 | if (1) #TLabel | ||
| 2228 | {Start 1; | ||
| 2229 | Mov 0, 0; | ||
| 2230 | my $a = setLabel; | ||
| 2231 | Out \0; | ||
| 2232 | Inc \0; | ||
| 2233 | Jlt $a, \0, 10; | ||
| 2234 | my $e = Execute(suppressOutput=>1); | ||
| 2235 | is_deeply $e->out, [0..9]; | ||
| 2236 | } | ||
| 2237 | |||
| 2238 | #latest:; | ||
| 2239 | if (1) #TMov | ||
| 2240 | {Start 1; | ||
| 2241 | my $a = Array "aaa"; | ||
| 2242 | Mov [$a, 1, "aaa"], 11; | ||
| 2243 | Mov 1, [$a, \1, "aaa"]; | ||
| 2244 | Out \1; | ||
| 2245 | my $e = Execute(suppressOutput=>1); | ||
| 2246 | is_deeply $e->out, [11]; | ||
| 2247 | } | ||
| 2248 | |||
| 2249 | #latest:; | ||
| 2250 | if (1) #TCall #TReturn | ||
| 2251 | {Start 1; | ||
| 2252 | my $w = Procedure 'write', sub | ||
| 2253 | {Out 'aaa'; | ||
| 2254 | Return; | ||
| 2255 | }; | ||
| 2256 | Call $w; | ||
| 2257 | my $e = Execute(suppressOutput=>1); | ||
| 2258 | is_deeply $e->out, ["aaa"]; | ||
| 2259 | } | ||
| 2260 | |||
| 2261 | #latest:; | ||
| 2262 | if (1) #TCall | ||
| 2263 | {Start 1; | ||
| 2264 | my $w = Procedure 'write', sub | ||
| 2265 | {my $a = ParamsGet 0; | ||
| 2266 | Out $a; | ||
| 2267 | Return; | ||
| 2268 | }; | ||
| 2269 | ParamsPut 0, 'bbb'; | ||
| 2270 | Call $w; | ||
| 2271 | my $e = Execute(suppressOutput=>1); | ||
| 2272 | is_deeply $e->out, ["bbb"]; | ||
| 2273 | } | ||
| 2274 | |||
| 2275 | #latest:; | ||
| 2276 | if (1) #TCall #TReturnPut #TReturnGet | ||
| 2277 | {Start 1; | ||
| 2278 | my $w = Procedure 'write', sub | ||
| 2279 | {ReturnPut 0, "ccc"; | ||
| 2280 | Return; | ||
| 2281 | }; | ||
| 2282 | Call $w; | ||
| 2283 | ReturnGet \0, 0; | ||
| 2284 | Out \0; | ||
| 2285 | my $e = Execute(suppressOutput=>1); | ||
| 2286 | is_deeply $e->out, ["ccc"]; | ||
| 2287 | } | ||
| 2288 | |||
| 2289 | #latest:; | ||
| 2290 | if (1) #TProcedure | ||
| 2291 | {Start 1; | ||
| 2292 | my $add = Procedure 'add2', sub | ||
| 2293 | {my $a = ParamsGet 0; | ||
| 2294 | my $b = Add $a, 2; | ||
| 2295 | ReturnPut 0, $b; | ||
| 2296 | Return; | ||
| 2297 | }; | ||
| 2298 | ParamsPut 0, 2; | ||
| 2299 | Call $add; | ||
| 2300 | my $c = ReturnGet 0; | ||
| 2301 | Out $c; | ||
| 2302 | my $e = Execute(suppressOutput=>1); | ||
| 2303 | is_deeply $e->out, [4]; | ||
| 2304 | } | ||
| 2305 | |||
| 2306 | #latest:; | ||
| 2307 | if (1) #TConfess | ||
| 2308 | {Start 1; | ||
| 2309 | my $c = Procedure 'confess', sub | ||
| 2310 | {Confess; | ||
| 2311 | }; | ||
| 2312 | Call $c; | ||
| 2313 | my $e = Execute(suppressOutput=>1); | ||
| 2314 | is_deeply $e->out, ["Confess at:", " 2 3 confess", " 1 6 call"]; | ||
| 2315 | } | ||
| 2316 | |||
| 2317 | #latest:; | ||
| 2318 | if (1) #TPush #TPop | ||
| 2319 | {Start 1; | ||
| 2320 | my $a = Array "aaa"; | ||
| 2321 | Push $a, 1; | ||
| 2322 | Push $a, 2; | ||
| 2323 | my $c = Pop $a; | ||
| 2324 | my $d = Pop $a; | ||
| 2325 | |||
| 2326 | Out $c; | ||
| 2327 | Out $d; | ||
| 2328 | my $e = Execute(suppressOutput=>1); | ||
| 2329 | is_deeply $e->out, [2, 1]; | ||
| 2330 | is_deeply $e->memory, { 1=> []}; | ||
| 2331 | } | ||
| 2332 | |||
| 2333 | #latest:; | ||
| 2334 | if (1) #TAlloc #TMov | ||
| 2335 | {Start 1; | ||
| 2336 | my $a = Array "alloc"; | ||
| 2337 | my $b = Mov 99; | ||
| 2338 | my $c = Mov $a; | ||
| 2339 | Mov [$a, 0, 'alloc'], $b; | ||
| 2340 | Mov [$c, 1, 'alloc'], 2; | ||
| 2341 | ok Execute(memory=> { 1=> bless([99, 2], "alloc") }); | ||
| 2342 | } | ||
| 2343 | |||
| 2344 | #latest:; | ||
| 2345 | if (1) #TFree | ||
| 2346 | {Start 1; | ||
| 2347 | my $a = Array "node"; | ||
| 2348 | Free $a, "aaa"; | ||
| 2349 | my $e = Execute(suppressOutput=>1); | ||
| 2350 | |||
| 2351 | is_deeply $e->out, [ | ||
| 2352 | "Wrong name: aaa for area with name: node", | ||
| 2353 | " 1 2 free", | ||
| 2354 | ]; | ||
| 2355 | } | ||
| 2356 | |||
| 2357 | #latest:; | ||
| 2358 | if (1) #TFree #TDump | ||
| 2359 | {Start 1; | ||
| 2360 | my $a = Array "node"; | ||
| 2361 | Out $a; | ||
| 2362 | Mov [$a, 1, 'node'], 1; | ||
| 2363 | Mov [$a, 2, 'node'], 2; | ||
| 2364 | Mov 1, [$a, \1, 'node']; | ||
| 2365 | Dump "dddd"; | ||
| 2366 | Free $a, "node"; | ||
| 2367 | my $e = Execute(suppressOutput=>1); | ||
| 2368 | |||
| 2369 | is_deeply $e->out, [ | ||
| 2370 | 1, | ||
| 2371 | "dddd", | ||
| 2372 | "-2=bless([], \"return\")", | ||
| 2373 | "-1=bless([], \"params\")", | ||
| 2374 | "0=bless([1, 1], \"stackArea\")", | ||
| 2375 | "1=bless([undef, 1, 2], \"node\")", | ||
| 2376 | "Stack trace", | ||
| 2377 | " 1 6 dump", | ||
| 2378 | ]; | ||
| 2379 | } | ||
| 2380 | |||
| 2381 | #latest:; | ||
| 2382 | if (1) # Layout | ||
| 2383 | {Start 1; | ||
| 2384 | my $a = Mov 'A'; | ||
| 2385 | my $b = Mov 'B'; | ||
| 2386 | my $c = Mov 'C'; | ||
| 2387 | Out $c; | ||
| 2388 | Out $b; | ||
| 2389 | Out $a; | ||
| 2390 | my $e = Execute(suppressOutput=>1); | ||
| 2391 | is_deeply $e->out, [qw(C B A)]; | ||
| 2392 | } | ||
| 2393 | |||
| 2394 | #latest:; | ||
| 2395 | if (1) #TIfEq #TIfNe #TIfLt #TIfLe #TIfGt #TIfGe | ||
| 2396 | {Start 1; | ||
| 2397 | my $a = Mov 1; | ||
| 2398 | my $b = Mov 2; | ||
| 2399 | IfEq $a, $a, Then {Out "Eq"}; | ||
| 2400 | IfNe $a, $a, Then {Out "Ne"}; | ||
| 2401 | IfLe $a, $a, Then {Out "Le"}; | ||
| 2402 | IfLt $a, $a, Then {Out "Lt"}; | ||
| 2403 | IfGe $a, $a, Then {Out "Ge"}; | ||
| 2404 | IfGt $a, $a, Then {Out "Gt"}; | ||
| 2405 | my $e = Execute(suppressOutput=>1); | ||
| 2406 | is_deeply $e->out, ["Eq", "Le", "Ge"]; | ||
| 2407 | } | ||
| 2408 | |||
| 2409 | #latest:; | ||
| 2410 | if (1) #TIfEq #TIfNe #TIfLt #TIfLe #TIfGt #TIfGe | ||
| 2411 | {Start 1; | ||
| 2412 | my $a = Mov 1; | ||
| 2413 | my $b = Mov 2; | ||
| 2414 | IfEq $a, $b, Then {Out "Eq"}; | ||
| 2415 | IfNe $a, $b, Then {Out "Ne"}; | ||
| 2416 | IfLe $a, $b, Then {Out "Le"}; | ||
| 2417 | IfLt $a, $b, Then {Out "Lt"}; | ||
| 2418 | IfGe $a, $b, Then {Out "Ge"}; | ||
| 2419 | IfGt $a, $b, Then {Out "Gt"}; | ||
| 2420 | my $e = Execute(suppressOutput=>1); | ||
| 2421 | is_deeply $e->out, ["Ne", "Le", "Lt"]; | ||
| 2422 | } | ||
| 2423 | |||
| 2424 | #latest:; | ||
| 2425 | if (1) #TIfEq #TIfNe #TIfLt #TIfLe #TIfGt #TIfGe | ||
| 2426 | {Start 1; | ||
| 2427 | my $a = Mov 1; | ||
| 2428 | my $b = Mov 2; | ||
| 2429 | IfEq $b, $a, Then {Out "Eq"}; | ||
| 2430 | IfNe $b, $a, Then {Out "Ne"}; | ||
| 2431 | IfLe $b, $a, Then {Out "Le"}; | ||
| 2432 | IfLt $b, $a, Then {Out "Lt"}; | ||
| 2433 | IfGe $b, $a, Then {Out "Ge"}; | ||
| 2434 | IfGt $b, $a, Then {Out "Gt"}; | ||
| 2435 | my $e = Execute(suppressOutput=>1); | ||
| 2436 | is_deeply $e->out, ["Ne", "Ge", "Gt"]; | ||
| 2437 | } | ||
| 2438 | |||
| 2439 | #latest:; | ||
| 2440 | if (1) #TIfTrue | ||
| 2441 | {Start 1; | ||
| 2442 | IfTrue 1, | ||
| 2443 | Then | ||
| 2444 | {Out 1 | ||
| 2445 | }, | ||
| 2446 | Else | ||
| 2447 | {Out 0 | ||
| 2448 | }; | ||
| 2449 | my $e = Execute(suppressOutput=>1); | ||
| 2450 | is_deeply $e->out, [1]; | ||
| 2451 | } | ||
| 2452 | |||
| 2453 | #latest:; | ||
| 2454 | if (1) #TIfFalse #TTrue #TFalse | ||
| 2455 | {Start 1; | ||
| 2456 | IfFalse 1, | ||
| 2457 | Then | ||
| 2458 | {Out 1 | ||
| 2459 | }, | ||
| 2460 | Else | ||
| 2461 | {Out 0 | ||
| 2462 | }; | ||
| 2463 | my $e = Execute(suppressOutput=>1); | ||
| 2464 | is_deeply $e->out, [0]; | ||
| 2465 | } | ||
| 2466 | |||
| 2467 | #latest:; | ||
| 2468 | if (1) #TForLoop | ||
| 2469 | {Start 1; | ||
| 2470 | For | ||
| 2471 | {my ($i) = @_; | ||
| 2472 | Out $i; | ||
| 2473 | } 10; | ||
| 2474 | my $e = Execute(suppressOutput=>1); | ||
| 2475 | is_deeply $e->out, [0..9]; | ||
| 2476 | } | ||
| 2477 | |||
| 2478 | #latest:; | ||
| 2479 | if (1) #TForLoop | ||
| 2480 | {Start 1; | ||
| 2481 | For | ||
| 2482 | {my ($i) = @_; | ||
| 2483 | Out $i; | ||
| 2484 | } 10, reverse=>1; | ||
| 2485 | my $e = Execute(suppressOutput=>1); | ||
| 2486 | is_deeply $e->out, [reverse 0..9]; | ||
| 2487 | } | ||
| 2488 | |||
| 2489 | #latest:; | ||
| 2490 | if (1) #TForLoop | ||
| 2491 | {Start 1; | ||
| 2492 | For | ||
| 2493 | {my ($i) = @_; | ||
| 2494 | Out $i; | ||
| 2495 | } [2, 10]; | ||
| 2496 | my $e = Execute(suppressOutput=>1); | ||
| 2497 | is_deeply $e->out, [2..9]; | ||
| 2498 | } | ||
| 2499 | |||
| 2500 | #latest:; | ||
| 2501 | if (1) #TAssert | ||
| 2502 | {Start 1; | ||
| 2503 | Assert; | ||
| 2504 | my $e = Execute(suppressOutput=>1); | ||
| 2505 | is_deeply $e->out, ["Assert failed", " 1 1 assert"]; | ||
| 2506 | } | ||
| 2507 | |||
| 2508 | #latest:; | ||
| 2509 | if (1) #TAssertEq | ||
| 2510 | {Start 1; | ||
| 2511 | Mov 0, 1; | ||
| 2512 | AssertEq \0, 2; | ||
| 2513 | my $e = Execute(suppressOutput=>1); | ||
| 2514 | is_deeply $e->out, ["Assert 1 == 2 failed", " 1 2 assertEq"]; | ||
| 2515 | } | ||
| 2516 | |||
| 2517 | #latest:; | ||
| 2518 | if (1) #TAssertNe | ||
| 2519 | {Start 1; | ||
| 2520 | Mov 0, 1; | ||
| 2521 | AssertNe \0, 1; | ||
| 2522 | my $e = Execute(suppressOutput=>1); | ||
| 2523 | is_deeply $e->out, ["Assert 1 != 1 failed", " 1 2 assertNe"]; | ||
| 2524 | } | ||
| 2525 | |||
| 2526 | #latest:; | ||
| 2527 | if (1) #TAssertLt | ||
| 2528 | {Start 1; | ||
| 2529 | Mov 0, 1; | ||
| 2530 | AssertLt \0, 0; | ||
| 2531 | my $e = Execute(suppressOutput=>1); | ||
| 2532 | is_deeply $e->out, ["Assert 1 < 0 failed", " 1 2 assertLt"]; | ||
| 2533 | } | ||
| 2534 | |||
| 2535 | #latest:; | ||
| 2536 | if (1) #TAssertLe | ||
| 2537 | {Start 1; | ||
| 2538 | Mov 0, 1; | ||
| 2539 | AssertLe \0, 0; | ||
| 2540 | my $e = Execute(suppressOutput=>1); | ||
| 2541 | is_deeply $e->out, ["Assert 1 <= 0 failed", " 1 2 assertLe"]; | ||
| 2542 | } | ||
| 2543 | |||
| 2544 | #latest:; | ||
| 2545 | if (1) #TAssertGt | ||
| 2546 | {Start 1; | ||
| 2547 | Mov 0, 1; | ||
| 2548 | AssertGt \0, 2; | ||
| 2549 | my $e = Execute(suppressOutput=>1); | ||
| 2550 | is_deeply $e->out, ["Assert 1 > 2 failed", " 1 2 assertGt"]; | ||
| 2551 | } | ||
| 2552 | |||
| 2553 | #latest:; | ||
| 2554 | if (1) #TAssertGe | ||
| 2555 | {Start 1; | ||
| 2556 | Mov 0, 1; | ||
| 2557 | AssertGe \0, 2; | ||
| 2558 | my $e = Execute(suppressOutput=>1); | ||
| 2559 | is_deeply $e->out, ["Assert 1 >= 2 failed", " 1 2 assertGe"]; | ||
| 2560 | } | ||
| 2561 | |||
| 2562 | #latest:; | ||
| 2563 | if (1) # Temporary variable | ||
| 2564 | {my $s = Start 1; | ||
| 2565 | my $a = Mov 1; | ||
| 2566 | my $b = Mov 2; | ||
| 2567 | Out $a; | ||
| 2568 | Out $b; | ||
| 2569 | my $e = Execute(suppressOutput=>1); | ||
| 2570 | is_deeply $e->out, [1..2]; | ||
| 2571 | } | ||
| 2572 | |||
| 2573 | #latest:; | ||
| 2574 | if (1) #TAlloc #TMov #TCall | ||
| 2575 | {Start 1; | ||
| 2576 | my $a = Array "aaa"; | ||
| 2577 | Dump "dddd"; | ||
| 2578 | my $e = Execute(suppressOutput=>1); | ||
| 2579 | is_deeply $e->out, [ | ||
| 2580 | "dddd", | ||
| 2581 | "-2=bless([], \"return\")", | ||
| 2582 | "-1=bless([], \"params\")", | ||
| 2583 | "0=bless([1], \"stackArea\")", | ||
| 2584 | "1=bless([], \"aaa\")", | ||
| 2585 | "Stack trace", | ||
| 2586 | " 1 2 dump", | ||
| 2587 | ]; | ||
| 2588 | } | ||
| 2589 | |||
| 2590 | #latest:; | ||
| 2591 | if (1) #TAlloc #TMov #TCall #TParamsPut #TParamsGet | ||
| 2592 | {Start 1; | ||
| 2593 | my $a = Array "aaa"; | ||
| 2594 | my $i = Mov 1; | ||
| 2595 | my $v = Mov 11; | ||
| 2596 | ParamsPut 0, $a; | ||
| 2597 | ParamsPut 1, $i; | ||
| 2598 | ParamsPut 2, $v; | ||
| 2599 | my $set = Procedure 'set', sub | ||
| 2600 | {my $a = ParamsGet 0; | ||
| 2601 | my $i = ParamsGet 1; | ||
| 2602 | my $v = ParamsGet 2; | ||
| 2603 | Mov [$a, \$i, 'aaa'], $v; | ||
| 2604 | Return; | ||
| 2605 | }; | ||
| 2606 | Call $set; | ||
| 2607 | my $V = Mov [$a, \$i, 'aaa']; | ||
| 2608 | AssertEq $v, $V; | ||
| 2609 | Out [$a, \$i, 'aaa']; | ||
| 2610 | my $e = Execute(suppressOutput=>1); | ||
| 2611 | is_deeply $e->out, [11]; | ||
| 2612 | } | ||
| 2613 | |||
| 2614 | #latest:; | ||
| 2615 | if (1) #TAlloc #TClear | ||
| 2616 | {Start 1; | ||
| 2617 | my $a = Array "aaa"; | ||
| 2618 | Clear [$a, 10, 'aaa']; | ||
| 2619 | my $e = Execute(suppressOutput=>1); | ||
| 2620 | is_deeply $e->memory->{1}, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | ||
| 2621 | } | ||
| 2622 | |||
| 2623 | #latest:; | ||
| 2624 | if (1) #TBlock #TGood #TBad | ||
| 2625 | {Start 1; | ||
| 2626 | Block | ||
| 2627 | {my ($start, $good, $bad, $end) = @_; | ||
| 2628 | Out 1; | ||
| 2629 | Jmp $good; | ||
| 2630 | } | ||
| 2631 | Good | ||
| 2632 | {Out 2; | ||
| 2633 | }, | ||
| 2634 | Bad | ||
| 2635 | {Out 3; | ||
| 2636 | }; | ||
| 2637 | Out 4; | ||
| 2638 | my $e = Execute(suppressOutput=>1); | ||
| 2639 | is_deeply $e->out, [1,2,4]; | ||
| 2640 | } | ||
| 2641 | |||
| 2642 | #latest:; | ||
| 2643 | if (1) #TBlock | ||
| 2644 | {Start 1; | ||
| 2645 | Block | ||
| 2646 | {my ($start, $good, $bad, $end) = @_; | ||
| 2647 | Out 1; | ||
| 2648 | Jmp $bad; | ||
| 2649 | } | ||
| 2650 | Good | ||
| 2651 | {Out 2; | ||
| 2652 | }, | ||
| 2653 | Bad | ||
| 2654 | {Out 3; | ||
| 2655 | }; | ||
| 2656 | Out 4; | ||
| 2657 | my $e = Execute(suppressOutput=>1); | ||
| 2658 | is_deeply $e->out, [1,3,4]; | ||
| 2659 | } | ||
| 2660 | |||
| 2661 | #latest:; | ||
| 2662 | if (1) #TProcedure | ||
| 2663 | {Start 1; | ||
| 2664 | for my $i(1..10) | ||
| 2665 | {Out $i; | ||
| 2666 | }; | ||
| 2667 | IfTrue 0, | ||
| 2668 | Then | ||
| 2669 | {Out 99; | ||
| 2670 | }; | ||
| 2671 | my $e = Execute(suppressOutput=>1); | ||
| 2672 | is_deeply $e->out, [1..10]; | ||
| 2673 | ok $e->analyzeExecutionResults(analyze=>3) =~ m(# 12 instructions executed); | ||
| 2674 | } | ||
| 2675 | |||
| 2676 | #latest:; | ||
| 2677 | if (1) # Double write | ||
| 2678 | {Start 1; | ||
| 2679 | Mov 1, 1; | ||
| 2680 | Mov 2, 1; | ||
| 2681 | Mov 3, 1; | ||
| 2682 | Mov 3, 1; | ||
| 2683 | Mov 1, 1; | ||
| 2684 | my $e = Execute(suppressOutput=>0); | ||
| 2685 | is_deeply keys($e->doubleWrite->%*), 1; # In area 0, variable 1 was first written by instruction 0 then again by instruction 1 once. | ||
| 2686 | } | ||
| 2687 | |||
| 2688 | #latest:; | ||
| 2689 | if (1) # Pointless assign | ||
| 2690 | {Start 1; | ||
| 2691 | Add 2, 1, 1; | ||
| 2692 | Add 2, \2, 0; | ||
| 2693 | my $e = Execute(suppressOutput=>1); | ||
| 2694 | is_deeply $e->pointlessAssign, { 1=> 1 }; | ||
| 2695 | } | ||
| 2696 | |||
| 2697 | #latest:; | ||
| 2698 | if (0) # Not read | ||
| 2699 | {Start 1; | ||
| 2700 | my $a = Mov 1; | ||
| 2701 | my $b = Mov $a; | ||
| 2702 | my $e = Execute(suppressOutput=>1); | ||
| 2703 | ok $e->notRead->{0}{1} == 1; # Area 0 == stack, variable 1 == $b generated by instruction 1 | ||
| 2704 | } | ||
| 2705 | |||
| 2706 | #latest:; | ||
| 2707 | if (1) #TAlloc #TMov #TCall | ||
| 2708 | {Start 1; | ||
| 2709 | my $set = Procedure 'set', sub | ||
| 2710 | {my $a = ParamsGet 0; | ||
| 2711 | }; | ||
| 2712 | ParamsPut 0, 1; | ||
| 2713 | Call $set; | ||
| 2714 | ParamsPut 0, 1; | ||
| 2715 | Call $set; | ||
| 2716 | my $e = Execute(suppressOutput=>1); | ||
| 2717 | is_deeply $e->out, []; | ||
| 2718 | } | ||
| 2719 | |||
| 2720 | #latest:; | ||
| 2721 | if (1) # Invalid address | ||
| 2722 | {Start 1; | ||
| 2723 | Mov 1, \0; | ||
| 2724 | my $e = Execute(suppressOutput=>1); | ||
| 2725 | my $t = join "\n", $e->out->@*; | ||
| 2726 | ok $t =~ m"Cannot assign an undefined value"; | ||
| 2727 | } | ||
| 2728 | |||
| 2729 | #latest:; | ||
| 2730 | if (1) #TLeArea #TLeAddress | ||
| 2731 | {Start 1; | ||
| 2732 | my $a = Array "array"; | ||
| 2733 | my $b = Mov 2; | ||
| 2734 | my $c = Mov 5; | ||
| 2735 | my $d = LeAddress $c; | ||
| 2736 | my $f = LeArea [$a, \0, 'array']; | ||
| 2737 | Out $d; | ||
| 2738 | Out $f; | ||
| 2739 | Mov [$a, \$b, 'array'], 22; | ||
| 2740 | Mov [$a, \$c, 'array'], 33; | ||
| 2741 | Mov [$f, \$d, 'array'], 44; | ||
| 2742 | my $e = Execute(suppressOutput=>1); | ||
| 2743 | is_deeply $e->out, [2,1]; | ||
| 2744 | is_deeply $e->memory, {1=>[undef, undef, 44, undef, undef, 33]}; | ||
| 2745 | } | ||
| 2746 | |||
| 2747 | #latest:; | ||
| 2748 | if (1) #TShiftUp | ||
| 2749 | {Start 1; | ||
| 2750 | my $a = Array "array"; | ||
| 2751 | Mov [$a, 0, 'array'], 0; | ||
| 2752 | Mov [$a, 1, 'array'], 1; | ||
| 2753 | Mov [$a, 2, 'array'], 2; | ||
| 2754 | ShiftUp [$a, 1, 'array'], 99; | ||
| 2755 | my $e = Execute(suppressOutput=>1); | ||
| 2756 | is_deeply $e->memory, {1=>[0, 99, 1, 2]}; | ||
| 2757 | } | ||
| 2758 | |||
| 2759 | #latest:; | ||
| 2760 | if (1) #TShiftDown | ||
| 2761 | {Start 1; | ||
| 2762 | my $a = Array "array"; | ||
| 2763 | Mov [$a, 0, 'array'], 0; | ||
| 2764 | Mov [$a, 1, 'array'], 99; | ||
| 2765 | Mov [$a, 2, 'array'], 2; | ||
| 2766 | my $b = ShiftDown [$a, \1, 'array']; | ||
| 2767 | Out $b; | ||
| 2768 | my $e = Execute(suppressOutput=>1); | ||
| 2769 | is_deeply $e->memory, {1=>[0, 2]}; | ||
| 2770 | is_deeply $e->out, [99]; | ||
| 2771 | } | ||
| 2772 | |||
| 2773 | #latest:; | ||
| 2774 | if (1) #TAlloc #TMov #TJeq #TJne #TJle #TJlt #TJge #TJgt | ||
| 2775 | {Start 1; | ||
| 2776 | my $a = Array "aaa"; | ||
| 2777 | my $b = Array "bbb"; | ||
| 2778 | Mov [$a, 0, 'aaa'], $b; | ||
| 2779 | Mov [$b, 0, 'bbb'], 99; | ||
| 2780 | For | ||
| 2781 | {my ($i, $check, $next, $end) = @_; | ||
| 2782 | my $c = Mov [$a, \0, 'aaa']; | ||
| 2783 | my $d = Mov [$c, \0, 'bbb']; | ||
| 2784 | Jeq $next, $d, $d; | ||
| 2785 | Jne $next, $d, $d; | ||
| 2786 | Jle $next, $d, $d; | ||
| 2787 | Jlt $next, $d, $d; | ||
| 2788 | Jge $next, $d, $d; | ||
| 2789 | Jgt $next, $d, $d; | ||
| 2790 | } 3; | ||
| 2791 | my $e = Execute(suppressOutput=>1); | ||
| 2792 | is_deeply $e->analyzeExecutionResults(doubleWrite=>3), "# 36 instructions executed"; | ||
| 2793 | is_deeply $e->memory, { 1=> bless([2], "aaa"), 2=> bless([99], "bbb") }; | ||
| 2794 | } | ||
| 2795 | |||
| 2796 | #latest:; | ||
| 2797 | if (1) #TAlloc #TMov | ||
| 2798 | {Start 1; | ||
| 2799 | my $a = Array 'aaa'; | ||
| 2800 | my $b = Mov 2; # Location to move to in a | ||
| 2801 | For | ||
| 2802 | {my ($i, $check, $next, $end) = @_; | ||
| 2803 | Mov [$a, \$b, 'aaa'], 1; | ||
| 2804 | Jeq $next, [$a, \$b, 'aaa'], 1; | ||
| 2805 | } 3; | ||
| 2806 | my $e = Execute(suppressOutput=>1); | ||
| 2807 | is_deeply $e->analyzeExecutionResults(doubleWrite=>3), "# 31 instructions executed"; | ||
| 2808 | is_deeply $e->memory, {1=> bless([undef, undef, 1], "aaa")}; | ||
| 2809 | } | ||
| 2810 | |||
| 2811 | #latest:; | ||
| 2812 | if (1) #TResize | ||
| 2813 | {Start 1; | ||
| 2814 | my $a = Array 'aaa'; | ||
| 2815 | Mov [$a, 0, 'aaa'], 1; | ||
| 2816 | Mov [$a, 1, 'aaa'], 2; | ||
| 2817 | Mov [$a, 2, 'aaa'], 3; | ||
| 2818 | Resize $a, 2; | ||
| 2819 | my $e = Execute(suppressOutput=>1); | ||
| 2820 | is_deeply $e->memory, {1=> [1, 2]}; | ||
| 2821 | } | ||
| 2822 | |||
| 2823 | #latest:; | ||
| 2824 | if (1) #TTrace #TThen #TElse | ||
| 2825 | {Start 1; | ||
| 2826 | Trace 1; | ||
| 2827 | IfEq 1, 2, | ||
| 2828 | Then | ||
| 2829 | {Mov 1, 1; | ||
| 2830 | Mov 2, 1; | ||
| 2831 | }, | ||
| 2832 | Else | ||
| 2833 | {Mov 3, 3; | ||
| 2834 | Mov 4, 4; | ||
| 2835 | }; | ||
| 2836 | IfEq 2, 2, | ||
| 2837 | Then | ||
| 2838 | {Mov 1, 1; | ||
| 2839 | Mov 2, 1; | ||
| 2840 | }, | ||
| 2841 | Else | ||
| 2842 | {Mov 3, 3; | ||
| 2843 | Mov 4, 4; | ||
| 2844 | }; | ||
| 2845 | my $e = Execute(suppressOutput=>1); | ||
| 2846 | is_deeply scalar($e->out->@*), 14; | ||
| 2847 | } | ||
| 2848 | |||
| 2849 | #latest:; | ||
| 2850 | if (1) #TWatch | ||
| 2851 | {Start 1; | ||
| 2852 | my $a = Mov 1; | ||
| 2853 | my $b = Mov 2; | ||
| 2854 | my $c = Mov 3; | ||
| 2855 | Watch $b; | ||
| 2856 | Mov $a, 4; | ||
| 2857 | Mov $b, 5; | ||
| 2858 | Mov $c, 6; | ||
| 2859 | my $e = Execute(suppressOutput=>1); | ||
| 2860 | is_deeply $e->out, | ||
| 2861 | [ | ||
| 2862 | "Change at watched area: 0 (stackArea), address: 1\n", | ||
| 2863 | " 1 6 mov", | ||
| 2864 | "Current value: 2", | ||
| 2865 | "New value: 5", | ||
| 2866 | "-2=bless([], \"return\")", | ||
| 2867 | "-1=bless([], \"params\")", | ||
| 2868 | "0=bless([4, 2, 3], \"stackArea\")", | ||
| 2869 | ]; | ||
| 2870 | } | ||
| 2871 | |||
| 2872 | #latest:; | ||
| 2873 | if (1) #TAreaSize #TForArray #TArray | ||
| 2874 | {Start 1; | ||
| 2875 | my $a = Array "aaa"; | ||
| 2876 | Mov [$a, 0, "aaa"], 1; | ||
| 2877 | Mov [$a, 1, "aaa"], 22; | ||
| 2878 | Mov [$a, 2, "aaa"], 333; | ||
| 2879 | my $n = AreaSize $a, "aaa"; | ||
| 2880 | Out $n; | ||
| 2881 | |||
| 2882 | ForArray | ||
| 2883 | {my ($i, $e, $check, $next, $end) = @_; | ||
| 2884 | IfGt $i, 1, | ||
| 2885 | Then | ||
| 2886 | {Trace 1; | ||
| 2887 | }; | ||
| 2888 | Out $i; Out $e; | ||
| 2889 | } $a, "aaa"; | ||
| 2890 | |||
| 2891 | Nop; | ||
| 2892 | my $e = Execute(suppressOutput=>1); | ||
| 2893 | is_deeply $e->memory, {1=>[1, 22, 333]}; | ||
| 2894 | |||
| 2895 | is_deeply $e->out, | ||
| 2896 | [3, 0, 1, 1, 22, | ||
| 2897 | "Trace: 1", | ||
| 2898 | " 37 14 1 trace \n", | ||
| 2899 | " 38 15 3 label \n", | ||
| 2900 | 2, | ||
| 2901 | " 39 16 3 out \n", | ||
| 2902 | 333, | ||
| 2903 | " 40 17 3 out \n", | ||
| 2904 | " 41 18 3 label \n", | ||
| 2905 | " 42 19 3 inc [0, 3, stackArea] = 3 was 2\n", | ||
| 2906 | " 43 20 3 jmp \n", | ||
| 2907 | " 44 9 4 label \n", | ||
| 2908 | " 45 10 4 jGe \n", | ||
| 2909 | " 46 21 1 label \n", | ||
| 2910 | " 47 22 1 nop \n", | ||
| 2911 | ]; | ||
| 2912 | } | ||
| 2913 | |||
| 2914 | #latest:; | ||
| 2915 | if (1) #TDumpArray #TMov | ||
| 2916 | {Start 1; | ||
| 2917 | my $a = Array "aaa"; | ||
| 2918 | Mov [$a, 0, "aaa"], 1; | ||
| 2919 | Mov [$a, 1, "aaa"], 22; | ||
| 2920 | Mov [$a, 2, "aaa"], 333; | ||
| 2921 | DumpArray $a, "AAAA"; | ||
| 2922 | my $e = Execute(suppressOutput=>1); | ||
| 2923 | |||
| 2924 | is_deeply $e->out, | ||
| 2925 | ["AAAA", | ||
| 2926 | "bless([1, 22, 333], \"aaa\")", | ||
| 2927 | "Stack trace", | ||
| 2928 | " 1 5 dumpArray", | ||
| 2929 | ]; | ||
| 2930 | } | ||
| 2931 | |||
| 2932 | #latest:; | ||
| 2933 | if (1) #TTally #TFor | ||
| 2934 | {my $N = 5; | ||
| 2935 | Start 1; | ||
| 2936 | For | ||
| 2937 | {Tally 1; | ||
| 2938 | my $a = Mov 1; | ||
| 2939 | Tally 2; | ||
| 2940 | Inc $a; | ||
| 2941 | Tally 0; | ||
| 2942 | } $N; | ||
| 2943 | my $e = Execute; | ||
| 2944 | |||
| 2945 | is_deeply $e->tallyCount, 2 * $N; | ||
| 2946 | is_deeply $e->tallyCounts,{ 1 => {mov => $N}, 2 => {inc => $N}}; | ||
| 2947 | } | ||
| 2948 | |||
| 2949 | #latest:; | ||
| 2950 | if (1) #TTracePoints | ||
| 2951 | {my $N = 5; | ||
| 2952 | Start 1; | ||
| 2953 | TracePoints 1; | ||
| 2954 | For | ||
| 2955 | {my $a = Mov 1; | ||
| 2956 | Inc $a; | ||
| 2957 | } $N; | ||
| 2958 | my $e = Execute(suppressOutput=>1); | ||
| 2959 | is_deeply $e->out, [ | ||
| 2960 | "TracePoints: 1", | ||
| 2961 | "Trace", | ||
| 2962 | " 1 6 tracePoint", | ||
| 2963 | "Trace", | ||
| 2964 | " 1 6 tracePoint", | ||
| 2965 | "Trace", | ||
| 2966 | " 1 6 tracePoint", | ||
| 2967 | "Trace", | ||
| 2968 | " 1 6 tracePoint", | ||
| 2969 | "Trace", | ||
| 2970 | " 1 6 tracePoint", | ||
| 2971 | ]; | ||
| 2972 | } |