1 #!/usr/bin/env python 2 3 """ 4 A really simple virtual processor employing a simple set of instructions which 5 ignore low-level operations and merely concentrate on variable access, structure 6 access, structure allocation and function invocations. 7 8 Copyright (C) 2007, 2008, 2009 Paul Boddie <paul@boddie.org.uk> 9 10 This program is free software; you can redistribute it and/or modify it under 11 the terms of the GNU General Public License as published by the Free Software 12 Foundation; either version 3 of the License, or (at your option) any later 13 version. 14 15 This program is distributed in the hope that it will be useful, but WITHOUT 16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 17 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 18 details. 19 20 You should have received a copy of the GNU General Public License along with 21 this program. If not, see <http://www.gnu.org/licenses/>. 22 23 -------- 24 25 The execution model of the virtual processor involves the following things: 26 27 * Memory contains constants, global variable 28 references and program code 29 30 * PC (program counter) stack contains the return address associated 31 with each function invocation 32 33 * Frame stack contains invocation frames in use and in 34 preparation plus temporary storage 35 36 * Local frame pointer stack refers to the frames in the frame stack 37 38 * Invocation frame pointer stack 39 40 * Exception handler stack 41 42 * Exception handler locals stack refers to the state of the local frame 43 pointer stack 44 45 * Exception handler PC stack refers to the state of the PC stack 46 47 * Registers: current value, 48 boolean status value, 49 source value, 50 current result, 51 current exception, 52 current callable 53 """ 54 55 from micropython.program import DataObject # for creating "nice" new objects 56 57 class IllegalInstruction(Exception): 58 pass 59 60 class IllegalAddress(Exception): 61 def __init__(self, address): 62 self.address = address 63 def __repr__(self): 64 return "IllegalAddress(%r)" % self.address 65 def __str__(self): 66 return repr(self) 67 68 class EmptyPCStack(Exception): 69 pass 70 71 class EmptyFrameStack(Exception): 72 pass 73 74 class BreakpointReached(Exception): 75 pass 76 77 class RSVPMachine: 78 79 "A really simple virtual processor." 80 81 def __init__(self, memory, objlist, paramlist, true_constant, false_constant, pc=None, debug=0): 82 83 """ 84 Initialise the processor with a 'memory' (a list of values containing 85 instructions and data), the object and parameter lists 'objlist' and 86 'paramlist', the addresses 'true_constant' and 'false_constant', and the 87 optional program counter 'pc'. 88 """ 89 90 self.memory = memory 91 self._objlist = objlist 92 self._paramlist = paramlist 93 self.objlist = objlist.as_raw() 94 self.paramlist = paramlist.as_raw() 95 self.true_constant = true_constant 96 self.false_constant = false_constant 97 98 self.pc = pc or 0 99 self.debug = debug 100 101 # Stacks. 102 103 self.pc_stack = [] 104 self.frame_stack = [] 105 self.local_sp_stack = [0] 106 self.invocation_sp_stack = [] 107 self.handler_stack = [len(self.memory) - 1] # final handler is the end of the code 108 self.handler_local_sp_stack = [] 109 self.handler_pc_stack = [] 110 111 # Registers. 112 113 self.instruction = None 114 self.operand = None 115 self.value = None 116 self.status = None 117 self.source = None 118 self.callable = None 119 self.result = None 120 self.exception = None 121 122 # Constants. 123 124 self.attr_error = objlist.access("__builtins__", "AttributeError").get_value().location 125 self.type_error = objlist.access("__builtins__", "TypeError").get_value().location 126 127 # Native class constants. 128 129 cls = objlist.access("__builtins__", "int") 130 self.int_class_location = cls and cls.get_value() and cls.get_value().location 131 self.int_instance_location = cls and cls.get_value() and cls.get_value().instance_template_location 132 cls = objlist.access("__builtins__", "list") 133 self.list_instance_location = cls and cls.get_value() and cls.get_value().instance_template_location 134 135 # Debugging attributes. 136 137 self.breakpoints = set() 138 139 # Debugging methods. 140 141 def dump(self): 142 print "PC", self.pc, "->", self.load(self.pc) 143 print "PC stack", self.pc_stack 144 print "Frame stack", self.frame_stack 145 print "Local stack pointers", self.local_sp_stack 146 print "Invocation stack pointers", self.invocation_sp_stack 147 print "Handler stack", self.handler_stack 148 print "Handler frame stack", self.handler_local_sp_stack 149 print "Handler PC stack", self.handler_pc_stack 150 print 151 print "Instruction", self.instruction 152 print "Operand", self.operand 153 print "Value", self.value 154 print "Status", self.status 155 print "Source", self.source 156 print "Callable", self.callable 157 print "Result", self.result 158 print "Exception", self.exception 159 160 def show(self): 161 self.show_memory(self.memory, 0) 162 163 def show_pc(self, run_in=10): 164 start = max(0, self.pc - run_in) 165 end = self.pc + run_in 166 memory = self.memory[start:end] 167 self.show_memory(memory, start) 168 169 def show_memory(self, memory, start): 170 for i, x in enumerate(memory): 171 location = start + i 172 if location == self.pc: 173 print "->", 174 else: 175 print " ", 176 print "%5d %r" % (location, x) 177 178 def step(self, dump=0): 179 self.execute() 180 self.show_pc() 181 if dump: 182 self.dump() 183 184 def set_break(self, location): 185 self.breakpoints.add(location) 186 187 # Internal operations. 188 189 def load(self, address): 190 191 "Return the value at the given 'address'." 192 193 try: 194 return self.memory[address] 195 except IndexError: 196 raise IllegalAddress(address) 197 except TypeError: 198 raise IllegalAddress(address) 199 200 def save(self, address, value): 201 202 "Save to the given 'address' the specified 'value'." 203 204 try: 205 self.memory[address] = value 206 except IndexError: 207 raise IllegalAddress(address) 208 except TypeError: 209 raise IllegalAddress(address) 210 211 def new(self, size): 212 213 """ 214 Allocate space of the given 'size', returning the address of the space. 215 """ 216 217 addr = len(self.memory) 218 for i in range(0, size): 219 self.memory.append(None) 220 return addr 221 222 def push_pc(self, data): 223 224 "Push 'data' onto the PC stack." 225 226 self.pc_stack.append(data) 227 228 def pull_pc(self): 229 230 "Pull a value from the PC stack and return it." 231 232 try: 233 return self.pc_stack.pop() 234 except IndexError: 235 raise EmptyPCStack 236 237 def run(self): 238 239 "Execute code in the memory, starting from the current PC address." 240 241 try: 242 while 1: 243 self.execute() 244 except EmptyPCStack: 245 pass 246 247 def execute(self): 248 249 "Execute code in the memory at the current PC address." 250 251 if self.pc in self.breakpoints: 252 self.breakpoints.remove(self.pc) 253 raise BreakpointReached 254 255 self.instruction = self.load(self.pc) 256 257 # Process any inputs of the instruction. 258 259 self.process_inputs() 260 261 # Perform the instruction itself. 262 263 next_pc = self.perform(self.instruction) 264 265 # Update the program counter. 266 267 if next_pc is None: 268 self.pc += 1 269 else: 270 self.pc = next_pc 271 272 def get_method(self, instruction): 273 274 "Return the handler method for the given 'instruction'." 275 276 instruction_name = instruction.__class__.__name__ 277 if self.debug: 278 print "%8d %s" % (self.pc, instruction_name) 279 method = getattr(self, instruction_name, None) 280 if method is None: 281 raise IllegalInstruction, (self.pc, instruction_name) 282 return method 283 284 def perform(self, instruction): 285 286 "Perform the 'instruction', returning the next PC value or None." 287 288 self.operand = instruction.get_operand() 289 method = self.get_method(instruction) 290 return method() 291 292 def process_inputs(self): 293 294 """ 295 Process any inputs of the current instruction. This permits any directly 296 connected sub-instructions to produce the effects that separate 297 instructions would otherwise have. 298 """ 299 300 value = self.value 301 if self.instruction.source is not None: 302 self.perform(self.instruction.source) 303 self.source = self.value 304 self.value = value 305 if self.instruction.input is not None: 306 self.perform(self.instruction.input) 307 308 def jump(self, addr, next): 309 310 """ 311 Jump to the subroutine at (or identified by) 'addr'. If 'addr' 312 identifies a library function then invoke the library function and set 313 PC to 'next' afterwards; otherwise, set PC to 'addr'. 314 """ 315 316 # Trap library functions introduced through the use of strings instead 317 # of proper locations. 318 319 if isinstance(addr, str): 320 handler = self.native_functions[addr](self) 321 if handler is None: 322 return next 323 else: 324 return handler 325 else: 326 self.push_pc(self.pc + 1) 327 return addr 328 329 # Instructions. 330 331 def LoadConst(self): 332 self.value = None, self.operand # context of constant is not interesting 333 334 def LoadName(self): 335 frame = self.local_sp_stack[-1] 336 self.value = self.frame_stack[frame + self.operand] 337 338 def StoreName(self): 339 frame = self.local_sp_stack[-1] 340 self.frame_stack[frame + self.operand] = self.source 341 342 LoadTemp = LoadName 343 344 def StoreTemp(self): 345 frame = self.local_sp_stack[-1] 346 self.frame_stack[frame + self.operand] = self.value 347 348 def LoadAddress(self): 349 # Preserve context (potentially null). 350 self.value = self.load(self.operand) 351 352 def LoadAddressContext(self): 353 context, ref = self.load(self.operand) 354 inst_context, inst_ref = self.value 355 self.value = inst_ref, ref 356 357 def LoadAddressContextCond(self): 358 context, ref = self.load(self.operand) 359 inst_context, inst_ref = self.value 360 self.value = self._LoadAddressContextCond(context, ref, inst_context, inst_ref) 361 362 def StoreAddress(self): 363 # Preserve context. 364 self.save(self.operand, self.source) 365 366 def MakeObject(self): 367 size = self.operand 368 context, ref = self.value 369 # NOTE: Referencing the instance template. 370 addr = self._MakeObject(size, ref - 1) 371 # Introduce object as context for the new object. 372 self.value = addr, addr 373 374 def LoadAttr(self): 375 context, ref = self.value 376 # Retrieved context should already be appropriate for the instance. 377 # NOTE: Adding 1 to skip any header. 378 self.value = self.load(ref + self.operand + 1) 379 380 def StoreAttr(self): 381 context, ref = self.value 382 # Target should already be an instance. 383 # NOTE: Adding 1 to skip any header. 384 self.save(ref + self.operand + 1, self.source) 385 386 def LoadAttrIndex(self): 387 context, ref = self.value 388 data = self.load(ref) 389 element = self.objlist[data.classcode + self.operand] 390 attr_index, class_attr, offset = element 391 if attr_index == self.operand: 392 if class_attr: 393 self.value = self.load(offset) # offset is address of class attribute 394 else: 395 self.value = self.load(ref + offset) 396 else: 397 self.exception = self.attr_error 398 return self.RaiseException() 399 400 def LoadAttrIndexContext(self): 401 context, ref = self.value 402 data = self.load(ref) 403 element = self.objlist[data.classcode + self.operand] 404 attr_index, class_attr, offset = element 405 if attr_index == self.operand: 406 loaded_context, loaded_ref = self.load(offset) # offset is address of class attribute 407 self.value = ref, loaded_ref 408 else: 409 self.exception = self.attr_error 410 return self.RaiseException() 411 412 def LoadAttrIndexContextCond(self): 413 context, ref = self.value 414 data = self.load(ref) 415 element = self.objlist[data.classcode + self.operand] 416 attr_index, class_attr, offset = element 417 if attr_index == self.operand: 418 if class_attr: 419 loaded_context, loaded_ref = self.load(offset) # offset is address of class attribute 420 self.value = self._LoadAddressContextCond(loaded_context, loaded_ref, context, ref) 421 else: 422 self.value = self.load(ref + offset) 423 else: 424 self.exception = self.attr_error 425 return self.RaiseException() 426 427 # NOTE: LoadAttrIndexContextCond will test context compatibility. 428 429 def StoreAttrIndex(self): 430 context, ref = self.value 431 data = self.load(ref) 432 element = self.objlist[data.classcode + self.operand] 433 attr_index, class_attr, offset = element 434 if attr_index == self.operand: 435 if class_attr: 436 self.exception = self.type_error 437 return self.RaiseException() 438 else: 439 self.save(ref + offset, self.source) 440 else: 441 self.exception = self.attr_error 442 return self.RaiseException() 443 444 # NOTE: LoadAttrIndexContext is a possibility if a particular attribute can always be overridden. 445 446 def MakeFrame(self): 447 self.invocation_sp_stack.append(len(self.frame_stack)) 448 self.frame_stack.extend([None] * self.operand) 449 450 def DropFrame(self): 451 self.local_sp_stack.pop() 452 frame = self.invocation_sp_stack.pop() 453 self.frame_stack = self.frame_stack[:frame] # reset stack before call 454 455 def RecoverFrame(self): 456 self.local_sp_stack.pop() 457 458 def StoreFrame(self): 459 frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame 460 self.frame_stack[frame + self.operand] = self.value 461 462 def StoreFrameIndex(self): 463 context, ref = self.value 464 frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame 465 data = self.load(ref) 466 element = self.paramlist[data.funccode + self.operand] 467 # NOTE: Need to ensure correct positioning where a context has been generated. 468 param_index, offset = element 469 if param_index == self.operand: 470 self.frame_stack[frame + offset + 1] = self.source # add 1 to skip the context always generated 471 else: 472 self.exception = self.type_error 473 return self.RaiseException() 474 475 def LoadCallable(self): 476 context, ref = self.value 477 data = self.load(ref) 478 self.callable = data.codeaddr, data.codedetails 479 480 def StoreCallable(self): 481 context, ref = self.value 482 # NOTE: Should improve the representation and permit direct saving. 483 data = self.load(ref) 484 self.save(ref, (data.classcode, data.attrcode) + self.callable) 485 486 def LoadContext(self): 487 context, ref = self.value 488 self.value = None, context # context of context is not interesting 489 490 def CheckFrame(self): 491 operand = self.operand 492 frame = self.invocation_sp_stack[-1] 493 context, ref = self.value 494 data = self.load(ref) 495 496 # Support sliding of the frame to exclude any inappropriate context. 497 498 if context is None: 499 self.invocation_sp_stack[-1] += 1 500 operand -= 1 501 else: 502 context_data = self.load(context) 503 if context_data.attrcode is None: # absent attrcode == class 504 self.invocation_sp_stack[-1] += 1 505 operand -= 1 506 507 # Test the frame size. 508 509 nargs, ndefaults = data.codedetails 510 if not ((nargs - ndefaults) <= operand <= nargs): 511 raise Exception, "CheckFrame %r (%r <= %r <= %r)" % (self.operand, nargs - ndefaults, operand, nargs) 512 513 # Support population of defaults. 514 # This involves copying the "attributes" of a function into the frame. 515 516 default = operand - (nargs - ndefaults) 517 self.frame_stack.extend([None] * (nargs - operand)) 518 pos = self.operand 519 520 while operand < nargs: 521 self.frame_stack[frame + pos] = self.load(ref + default + 1) # skip header 522 default += 1 523 pos += 1 524 operand += 1 525 526 def CheckSelf(self): 527 context, ref = self.value 528 target_context, target_ref = self.source 529 530 # Check the details of the proposed context and the target's context. 531 532 self.status = self._CheckInstance(ref, target_context) 533 534 def JumpWithFrame(self): 535 codeaddr, codedetails = self.callable 536 self.local_sp_stack.append(self.invocation_sp_stack[-1]) # adopt the invocation frame 537 return self.jump(codeaddr, self.pc + 1) # return to the instruction after this one 538 539 def ExtendFrame(self): 540 self.frame_stack.extend([None] * self.operand) 541 542 def AdjustFrame(self): 543 if self.operand > 0: 544 self.frame_stack.append([None] * self.operand) 545 elif self.operand == -1: 546 self.invocation_sp_stack[-1] -= 1 547 else: 548 raise Exception, "AdjustFrame %r" % self.operand 549 550 def Return(self): 551 return self.pull_pc() 552 553 def LoadResult(self): 554 self.value = self.result 555 556 def StoreResult(self): 557 self.result = self.value 558 559 def Jump(self): 560 return self.operand 561 562 def JumpIfTrue(self): 563 if self.status: 564 return self.operand 565 566 def JumpIfFalse(self): 567 if not self.status: 568 return self.operand 569 570 def LoadException(self): 571 self.value = self.exception, self.exception 572 573 def StoreException(self): 574 self.exception = self.value[1] 575 576 def RaiseException(self): 577 return self.handler_stack[-1] 578 579 def PushHandler(self): 580 self.handler_stack.append(self.operand) 581 self.handler_local_sp_stack.append(len(self.local_sp_stack)) 582 self.handler_pc_stack.append(len(self.pc_stack)) 583 584 def PopHandler(self): 585 # Reduce the local frame pointer stack to refer to the handler's frame. 586 self.local_sp_stack = self.local_sp_stack[:self.handler_local_sp_stack.pop()] 587 # Reduce the PC stack to discard all superfluous return addresses. 588 self.pc_stack = self.pc_stack[:self.handler_pc_stack.pop()] 589 self.handler_stack.pop() 590 591 def CheckException(self): 592 self.status = self.exception is not None and self._CheckInstance(self.exception, self.value[1]) 593 594 def TestIdentity(self): 595 self.status = self.value[1] == self.source[1] 596 597 def TestIdentityAddress(self): 598 self.status = self.value[1] == self.operand 599 600 # LoadBoolean is implemented in the generated code. 601 # StoreBoolean is implemented by testing against the True value. 602 603 def InvertBoolean(self): 604 self.status = not self.status 605 606 # Common implementation details. 607 608 def _CheckInstance(self, ref, cls): 609 data = self.load(ref) 610 target_data = self.load(cls) 611 612 # Insist on a class. 613 614 if target_data.attrcode is not None: # present attrcode == instance 615 return 0 616 617 # Find the table entry for the descendant. 618 619 element = self.objlist[target_data.classcode + data.attrcode] 620 attr_index, class_attr, offset = element 621 return attr_index == data.attrcode 622 623 def _MakeObject(self, size, ref): 624 # Load the template. 625 data = self.load(ref) 626 addr = self.new(size) 627 # Save the header, overriding the size. 628 self.save(addr, data.with_size(size)) 629 return addr 630 631 def _LoadAddressContextCond(self, context, ref, inst_context, inst_ref): 632 633 # Check the instance context against the target's context. 634 635 if self._CheckInstance(inst_ref, context): 636 # Replace the context with the instance. 637 return inst_ref, ref 638 else: 639 return context, ref 640 641 # Native function implementations. 642 643 def builtins_int_add(self): 644 frame = self.local_sp_stack[-1] 645 646 # Get operands addresses. 647 648 left_context, left = self.frame_stack[frame] 649 right_context, right = self.frame_stack[frame + 1] 650 651 # Test operand suitability. 652 653 if not self._CheckInstance(left, self.int_class_location) and self._CheckInstance(right, self.int_class_location): 654 self.exception = self.type_error 655 return self.RaiseException() 656 657 # NOTE: Assume single location for data. 658 659 left_data = left + 1 660 right_data = right + 1 661 662 # Make a new object. 663 664 addr = self._MakeObject(2, self.int_instance_location) 665 666 # Store the result. 667 # NOTE: The data is considered ready to use. 668 669 self.save(addr + 1, self.load(left_data) + self.load(right_data)) 670 671 # Return the new object. 672 # Introduce object as context for the new object. 673 674 self.result = addr, addr 675 676 def builtins_int_bool(self): 677 frame = self.local_sp_stack[-1] 678 679 # Get operands addresses. 680 681 left_context, left = self.frame_stack[frame] 682 683 # Test operand suitability. 684 685 if not self._CheckInstance(left, self.int_class_location): 686 self.exception = self.type_error 687 return self.RaiseException() 688 689 # NOTE: Assume single location for data. 690 691 left_data = left + 1 692 693 # Test the data. 694 # NOTE: The data is considered ready to use. 695 696 if self.load(left_data) != 0: 697 self.result = self.true_constant, self.true_constant 698 else: 699 self.result = self.false_constant, self.false_constant 700 701 def builtins_bool_bool(self): 702 frame = self.local_sp_stack[-1] 703 704 # Get operands addresses. 705 706 left_context, left = self.frame_stack[frame] 707 self.result = left, left 708 709 def builtins_list_new(self): 710 frame = self.local_sp_stack[-1] 711 712 # NOTE: Specific copying of tuples/lists. 713 714 args_context, args = self.frame_stack[frame] 715 header = self.load(args) 716 717 list = self._MakeObject(header.size, self.list_instance_location) 718 for i in range(1, header.size): 719 self.save(list + i, self.load(args + i)) 720 721 self.result = list, list 722 723 native_functions = { 724 "__builtins__.int.__add__" : builtins_int_add, 725 "__builtins__.int.__bool__" : builtins_int_bool, 726 "__builtins__.bool.__bool__" : builtins_bool_bool, 727 "__builtins__.list" : builtins_list_new, 728 } 729 730 # vim: tabstop=4 expandtab shiftwidth=4