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 self.index_error = objlist.access("__builtins__", "IndexError").get_value().location 127 128 # Native class constants. 129 130 cls = objlist.access("__builtins__", "int") 131 self.int_class_location = cls and cls.get_value() and cls.get_value().location 132 self.int_instance_location = cls and cls.get_value() and cls.get_value().instance_template_location 133 cls = objlist.access("__builtins__", "list") 134 self.list_instance_location = cls and cls.get_value() and cls.get_value().instance_template_location 135 136 # Debugging attributes. 137 138 self.breakpoints = set() 139 140 # Debugging methods. 141 142 def dump(self): 143 print "PC", self.pc, "->", self.load(self.pc) 144 print "PC stack", self.pc_stack 145 print "Frame stack", self.frame_stack 146 print "Local stack pointers", self.local_sp_stack 147 print "Invocation stack pointers", self.invocation_sp_stack 148 print "Handler stack", self.handler_stack 149 print "Handler frame stack", self.handler_local_sp_stack 150 print "Handler PC stack", self.handler_pc_stack 151 print 152 print "Instruction", self.instruction 153 print "Operand", self.operand 154 print "Value", self.value 155 print "Status", self.status 156 print "Source", self.source 157 print "Callable", self.callable 158 print "Result", self.result 159 print "Exception", self.exception 160 161 def show(self): 162 self.show_memory(self.memory, 0) 163 164 def show_pc(self, run_in=10): 165 start = max(0, self.pc - run_in) 166 end = self.pc + run_in 167 memory = self.memory[start:end] 168 self.show_memory(memory, start) 169 170 def show_memory(self, memory, start): 171 for i, x in enumerate(memory): 172 location = start + i 173 if location == self.pc: 174 print "->", 175 else: 176 print " ", 177 print "%5d %r" % (location, x) 178 179 def step(self, dump=0): 180 self.execute() 181 self.show_pc() 182 if dump: 183 self.dump() 184 185 def set_break(self, location): 186 self.breakpoints.add(location) 187 188 # Internal operations. 189 190 def load(self, address): 191 192 "Return the value at the given 'address'." 193 194 try: 195 return self.memory[address] 196 except IndexError: 197 raise IllegalAddress(address) 198 except TypeError: 199 raise IllegalAddress(address) 200 201 def save(self, address, value): 202 203 "Save to the given 'address' the specified 'value'." 204 205 try: 206 self.memory[address] = value 207 except IndexError: 208 raise IllegalAddress(address) 209 except TypeError: 210 raise IllegalAddress(address) 211 212 def new(self, size): 213 214 """ 215 Allocate space of the given 'size', returning the address of the space. 216 """ 217 218 addr = len(self.memory) 219 for i in range(0, size): 220 self.memory.append(None) 221 return addr 222 223 def push_pc(self, data): 224 225 "Push 'data' onto the PC stack." 226 227 self.pc_stack.append(data) 228 229 def pull_pc(self): 230 231 "Pull a value from the PC stack and return it." 232 233 try: 234 return self.pc_stack.pop() 235 except IndexError: 236 raise EmptyPCStack 237 238 def run(self): 239 240 "Execute code in the memory, starting from the current PC address." 241 242 try: 243 while 1: 244 self.execute() 245 except EmptyPCStack: 246 pass 247 248 def execute(self): 249 250 "Execute code in the memory at the current PC address." 251 252 if self.pc in self.breakpoints: 253 self.breakpoints.remove(self.pc) 254 raise BreakpointReached 255 256 self.instruction = self.load(self.pc) 257 258 # Process any inputs of the instruction. 259 260 self.process_inputs() 261 262 # Perform the instruction itself. 263 264 next_pc = self.perform(self.instruction) 265 266 # Update the program counter. 267 268 if next_pc is None: 269 self.pc += 1 270 else: 271 self.pc = next_pc 272 273 def get_method(self, instruction): 274 275 "Return the handler method for the given 'instruction'." 276 277 instruction_name = instruction.__class__.__name__ 278 if self.debug: 279 print "%8d %s" % (self.pc, instruction_name) 280 method = getattr(self, instruction_name, None) 281 if method is None: 282 raise IllegalInstruction, (self.pc, instruction_name) 283 return method 284 285 def perform(self, instruction): 286 287 "Perform the 'instruction', returning the next PC value or None." 288 289 self.operand = instruction.get_operand() 290 method = self.get_method(instruction) 291 return method() 292 293 def process_inputs(self): 294 295 """ 296 Process any inputs of the current instruction. This permits any directly 297 connected sub-instructions to produce the effects that separate 298 instructions would otherwise have. 299 """ 300 301 value = self.value 302 if self.instruction.source is not None: 303 self.perform(self.instruction.source) 304 self.source = self.value 305 self.value = value 306 if self.instruction.input is not None: 307 self.perform(self.instruction.input) 308 309 def jump(self, addr, next): 310 311 """ 312 Jump to the subroutine at (or identified by) 'addr'. If 'addr' 313 identifies a library function then invoke the library function and set 314 PC to 'next' afterwards; otherwise, set PC to 'addr'. 315 """ 316 317 # Trap library functions introduced through the use of strings instead 318 # of proper locations. 319 320 if isinstance(addr, str): 321 handler = self.native_functions[addr](self) 322 if handler is None: 323 return next 324 else: 325 return handler 326 else: 327 self.push_pc(self.pc + 1) 328 return addr 329 330 # Instructions. 331 332 def LoadConst(self): 333 self.value = None, self.operand # context of constant is not interesting 334 335 def LoadName(self): 336 frame = self.local_sp_stack[-1] 337 self.value = self.frame_stack[frame + self.operand] 338 339 def StoreName(self): 340 frame = self.local_sp_stack[-1] 341 self.frame_stack[frame + self.operand] = self.source 342 343 LoadTemp = LoadName 344 345 def StoreTemp(self): 346 frame = self.local_sp_stack[-1] 347 self.frame_stack[frame + self.operand] = self.value 348 349 def LoadAddress(self): 350 # Preserve context (potentially null). 351 self.value = self.load(self.operand) 352 353 def LoadAddressContext(self): 354 context, ref = self.load(self.operand) 355 inst_context, inst_ref = self.value 356 self.value = inst_ref, ref 357 358 def LoadAddressContextCond(self): 359 context, ref = self.load(self.operand) 360 inst_context, inst_ref = self.value 361 self.value = self._LoadAddressContextCond(context, ref, inst_context, inst_ref) 362 363 def StoreAddress(self): 364 # Preserve context. 365 self.save(self.operand, self.source) 366 367 def MakeObject(self): 368 size = self.operand 369 context, ref = self.value 370 # NOTE: Referencing the instance template. 371 addr = self._MakeObject(size, ref - 1) 372 # Introduce object as context for the new object. 373 self.value = addr, addr 374 375 def LoadAttr(self): 376 context, ref = self.value 377 # Retrieved context should already be appropriate for the instance. 378 # NOTE: Adding 1 to skip any header. 379 self.value = self.load(ref + self.operand + 1) 380 381 def StoreAttr(self): 382 context, ref = self.value 383 # Target should already be an instance. 384 # NOTE: Adding 1 to skip any header. 385 self.save(ref + self.operand + 1, self.source) 386 387 def LoadAttrIndex(self): 388 context, ref = self.value 389 data = self.load(ref) 390 element = self.objlist[data.classcode + self.operand] 391 attr_index, class_attr, offset = element 392 if attr_index == self.operand: 393 if class_attr: 394 self.value = self.load(offset) # offset is address of class attribute 395 else: 396 self.value = self.load(ref + offset) 397 else: 398 self.exception = self.attr_error 399 return self.RaiseException() 400 401 def LoadAttrIndexContext(self): 402 context, ref = self.value 403 data = self.load(ref) 404 element = self.objlist[data.classcode + self.operand] 405 attr_index, class_attr, offset = element 406 if attr_index == self.operand: 407 loaded_context, loaded_ref = self.load(offset) # offset is address of class attribute 408 self.value = ref, loaded_ref 409 else: 410 self.exception = self.attr_error 411 return self.RaiseException() 412 413 def LoadAttrIndexContextCond(self): 414 context, ref = self.value 415 data = self.load(ref) 416 element = self.objlist[data.classcode + self.operand] 417 attr_index, class_attr, offset = element 418 if attr_index == self.operand: 419 if class_attr: 420 loaded_context, loaded_ref = self.load(offset) # offset is address of class attribute 421 self.value = self._LoadAddressContextCond(loaded_context, loaded_ref, context, ref) 422 else: 423 self.value = self.load(ref + offset) 424 else: 425 self.exception = self.attr_error 426 return self.RaiseException() 427 428 def StoreAttrIndex(self): 429 context, ref = self.value 430 data = self.load(ref) 431 element = self.objlist[data.classcode + self.operand] 432 attr_index, class_attr, offset = element 433 if attr_index == self.operand: 434 if class_attr: 435 self.exception = self.type_error 436 return self.RaiseException() 437 else: 438 self.save(ref + offset, self.source) 439 else: 440 self.exception = self.attr_error 441 return self.RaiseException() 442 443 # NOTE: LoadAttrIndexContext is a possibility if a particular attribute can always be overridden. 444 445 def MakeFrame(self): 446 self.invocation_sp_stack.append(len(self.frame_stack)) 447 self.frame_stack.extend([None] * self.operand) 448 449 def DropFrame(self): 450 self.local_sp_stack.pop() 451 frame = self.invocation_sp_stack.pop() 452 self.frame_stack = self.frame_stack[:frame] # reset stack before call 453 454 def RecoverFrame(self): 455 self.local_sp_stack.pop() 456 457 def StoreFrame(self): 458 frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame 459 self.frame_stack[frame + self.operand] = self.value 460 461 def StoreFrameIndex(self): 462 context, ref = self.value 463 frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame 464 data = self.load(ref) 465 element = self.paramlist[data.funccode + self.operand] 466 # NOTE: Need to ensure correct positioning where a context has been generated. 467 param_index, offset = element 468 if param_index == self.operand: 469 self.frame_stack[frame + offset + 1] = self.source # add 1 to skip the context always generated 470 else: 471 self.exception = self.type_error 472 return self.RaiseException() 473 474 def LoadCallable(self): 475 context, ref = self.value 476 data = self.load(ref) 477 self.callable = data.codeaddr, data.codedetails 478 479 def StoreCallable(self): 480 context, ref = self.value 481 # NOTE: Should improve the representation and permit direct saving. 482 data = self.load(ref) 483 self.save(ref, (data.classcode, data.attrcode) + self.callable) 484 485 def LoadContext(self): 486 context, ref = self.value 487 self.value = None, context # context of context is not interesting 488 489 def CheckFrame(self): 490 (nargs, ndefaults, has_star) = self.operand 491 492 # The frame is actually installed as the locals. 493 # Retrieve the context from the first local. 494 495 frame = self.local_sp_stack[-1] 496 context, ref = self.frame_stack[frame] # + 0 497 nlocals = len(self.frame_stack[frame:]) 498 499 # Support sliding of the frame to exclude any inappropriate context. 500 501 if context is None: 502 self.local_sp_stack[-1] += 1 503 nlocals -= 1 504 else: 505 context_data = self.load(context) 506 if context_data.attrcode is None: # absent attrcode == class 507 self.local_sp_stack[-1] += 1 508 nlocals -= 1 509 510 # Test the frame size. 511 # NOTE: Raise a proper exception here. 512 513 if not ((nargs - ndefaults) <= nlocals and (nlocals <= nargs or has_star)): 514 raise Exception, "CheckFrame %r (%r <= %r <= %r)" % (self.operand, nargs - ndefaults, nlocals, nargs) 515 516 def FillDefaults(self): 517 (nargs, ndefaults) = self.operand 518 519 # The frame is actually installed as the locals. 520 521 frame = self.local_sp_stack[-1] 522 nlocals = len(self.frame_stack[frame:]) 523 524 # Support population of defaults. 525 # This involves copying the "attributes" of a function into the frame. 526 527 default = nlocals - (nargs - ndefaults) 528 self.frame_stack.extend([None] * (nargs - nlocals)) 529 pos = nlocals 530 531 while pos < nargs: 532 self.frame_stack[frame + pos] = self.load(ref + default + 1) # skip header 533 default += 1 534 pos += 1 535 536 def CheckSelf(self): 537 context, ref = self.value 538 target_context, target_ref = self.source 539 540 # Check the details of the proposed context and the target's context. 541 542 self.status = self._CheckInstance(ref, target_context) 543 544 def JumpWithFrame(self): 545 codeaddr, codedetails = self.callable 546 self.local_sp_stack.append(self.invocation_sp_stack[-1]) # adopt the invocation frame 547 return self.jump(codeaddr, self.pc + 1) # return to the instruction after this one 548 549 def JumpWithFrameDirect(self): 550 operand = self.operand 551 self.local_sp_stack.append(self.invocation_sp_stack[-1]) # adopt the invocation frame 552 return self.jump(operand, self.pc + 1) # return to the instruction after this one 553 554 def ExtendFrame(self): 555 self.frame_stack.extend([None] * self.operand) 556 557 def AdjustFrame(self): 558 self.invocation_sp_stack[-1] += self.operand 559 560 def Return(self): 561 return self.pull_pc() 562 563 def LoadResult(self): 564 self.value = self.result 565 566 def StoreResult(self): 567 self.result = self.value 568 569 def Jump(self): 570 return self.operand 571 572 def JumpIfTrue(self): 573 if self.status: 574 return self.operand 575 576 def JumpIfFalse(self): 577 if not self.status: 578 return self.operand 579 580 def LoadException(self): 581 self.value = self.exception, self.exception 582 583 def StoreException(self): 584 self.exception = self.value[1] 585 586 def RaiseException(self): 587 return self.handler_stack[-1] 588 589 def PushHandler(self): 590 self.handler_stack.append(self.operand) 591 self.handler_local_sp_stack.append(len(self.local_sp_stack)) 592 self.handler_pc_stack.append(len(self.pc_stack)) 593 594 def PopHandler(self): 595 # Reduce the local frame pointer stack to refer to the handler's frame. 596 self.local_sp_stack = self.local_sp_stack[:self.handler_local_sp_stack.pop()] 597 # Reduce the PC stack to discard all superfluous return addresses. 598 self.pc_stack = self.pc_stack[:self.handler_pc_stack.pop()] 599 self.handler_stack.pop() 600 601 def CheckException(self): 602 self.status = self.exception is not None and self._CheckInstance(self.exception, self.value[1]) 603 604 def TestIdentity(self): 605 self.status = self.value[1] == self.source[1] 606 607 def TestIdentityAddress(self): 608 self.status = self.value[1] == self.operand 609 610 # LoadBoolean is implemented in the generated code. 611 # StoreBoolean is implemented by testing against the True value. 612 613 def InvertBoolean(self): 614 self.status = not self.status 615 616 # Common implementation details. 617 618 def _CheckInstance(self, ref, cls): 619 data = self.load(ref) 620 target_data = self.load(cls) 621 622 # Insist on a class. 623 624 if target_data.attrcode is not None: # present attrcode == instance 625 return 0 626 627 # Find the table entry for the descendant. 628 629 element = self.objlist[target_data.classcode + data.attrcode] 630 attr_index, class_attr, offset = element 631 return attr_index == data.attrcode 632 633 def _MakeObject(self, size, ref): 634 # Load the template. 635 data = self.load(ref) 636 addr = self.new(size) 637 # Save the header, overriding the size. 638 self.save(addr, data.with_size(size)) 639 return addr 640 641 def _LoadAddressContextCond(self, context, ref, inst_context, inst_ref): 642 643 # Check the instance context against the target's context. 644 645 if self._CheckInstance(inst_ref, context): 646 # Replace the context with the instance. 647 return inst_ref, ref 648 else: 649 return context, ref 650 651 # Native function implementations. 652 653 def builtins_int_add(self): 654 frame = self.local_sp_stack[-1] 655 656 # Get operands addresses. 657 658 left_context, left = self.frame_stack[frame] 659 right_context, right = self.frame_stack[frame + 1] 660 661 # Test operand suitability. 662 663 if not self._CheckInstance(left, self.int_class_location) and self._CheckInstance(right, self.int_class_location): 664 self.exception = self.type_error 665 return self.RaiseException() 666 667 # NOTE: Assume single location for data. 668 669 left_data = left + 1 670 right_data = right + 1 671 672 # Make a new object. 673 674 addr = self._MakeObject(2, self.int_instance_location) 675 676 # Store the result. 677 # NOTE: The data is considered ready to use. 678 679 self.save(addr + 1, self.load(left_data) + self.load(right_data)) 680 681 # Return the new object. 682 # Introduce object as context for the new object. 683 684 self.result = addr, addr 685 686 def builtins_int_bool(self): 687 frame = self.local_sp_stack[-1] 688 689 # Get operands addresses. 690 691 left_context, left = self.frame_stack[frame] 692 693 # Test operand suitability. 694 695 if not self._CheckInstance(left, self.int_class_location): 696 self.exception = self.type_error 697 return self.RaiseException() 698 699 # NOTE: Assume single location for data. 700 701 left_data = left + 1 702 703 # Test the data. 704 # NOTE: The data is considered ready to use. 705 706 if self.load(left_data) != 0: 707 self.result = self.true_constant, self.true_constant 708 else: 709 self.result = self.false_constant, self.false_constant 710 711 def builtins_int_neg(self): 712 frame = self.local_sp_stack[-1] 713 714 # Get operands addresses. 715 716 left_context, left = self.frame_stack[frame] 717 718 # Test operand suitability. 719 720 if not self._CheckInstance(left, self.int_class_location): 721 self.exception = self.type_error 722 return self.RaiseException() 723 724 # NOTE: Assume single location for data. 725 726 left_data = left + 1 727 728 # Make a new object. 729 730 addr = self._MakeObject(2, self.int_instance_location) 731 732 # Store the result. 733 # NOTE: The data is considered ready to use. 734 735 self.save(addr + 1, -self.load(left_data)) 736 737 # Return the new object. 738 # Introduce object as context for the new object. 739 740 self.result = addr, addr 741 742 def builtins_bool_bool(self): 743 frame = self.local_sp_stack[-1] 744 745 # Get operands addresses. 746 747 left_context, left = self.frame_stack[frame] 748 self.result = left, left 749 750 def builtins_list_new(self): 751 frame = self.local_sp_stack[-1] 752 753 # NOTE: Specific copying of tuples/lists. 754 755 args_context, args = self.frame_stack[frame] 756 header = self.load(args) 757 758 list = self._MakeObject(header.size, self.list_instance_location) 759 for i in range(1, header.size): 760 self.save(list + i, self.load(args + i)) 761 762 self.result = list, list 763 764 def builtins_list_getitem(self): 765 frame = self.local_sp_stack[-1] 766 767 # Get operands addresses. 768 769 obj_context, obj = self.frame_stack[frame] 770 item_context, item = self.frame_stack[frame + 1] 771 772 header = self.load(obj) 773 nelements = header.size - 1 774 775 # NOTE: Assume single location for data. 776 777 item_pos = self.load(item + 1) 778 if item_pos >= 0 and item_pos < nelements: 779 pass 780 elif item_pos < 0 and item_pos >= -nelements: 781 item_pos = nelements + item_pos 782 else: 783 self.exception = self.index_error 784 return self.RaiseException() 785 786 self.result = self.load(obj + 1 + item_pos) 787 788 def builtins_object_init(self): 789 pass 790 791 native_functions = { 792 "__builtins__.int.__add__" : builtins_int_add, 793 "__builtins__.int.__bool__" : builtins_int_bool, 794 "__builtins__.int.__neg__" : builtins_int_neg, 795 "__builtins__.bool.__bool__" : builtins_bool_bool, 796 "__builtins__.list" : builtins_list_new, 797 "__builtins__.list.__getitem__" : builtins_list_getitem, 798 "__builtins__.object.__init__" : builtins_object_init, 799 } 800 801 # vim: tabstop=4 expandtab shiftwidth=4