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 def StoreAttrIndex(self): 428 context, ref = self.value 429 data = self.load(ref) 430 element = self.objlist[data.classcode + self.operand] 431 attr_index, class_attr, offset = element 432 if attr_index == self.operand: 433 if class_attr: 434 self.exception = self.type_error 435 return self.RaiseException() 436 else: 437 self.save(ref + offset, self.source) 438 else: 439 self.exception = self.attr_error 440 return self.RaiseException() 441 442 # NOTE: LoadAttrIndexContext is a possibility if a particular attribute can always be overridden. 443 444 def MakeFrame(self): 445 self.invocation_sp_stack.append(len(self.frame_stack)) 446 self.frame_stack.extend([None] * self.operand) 447 448 def DropFrame(self): 449 self.local_sp_stack.pop() 450 frame = self.invocation_sp_stack.pop() 451 self.frame_stack = self.frame_stack[:frame] # reset stack before call 452 453 def RecoverFrame(self): 454 self.local_sp_stack.pop() 455 456 def StoreFrame(self): 457 frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame 458 self.frame_stack[frame + self.operand] = self.value 459 460 def StoreFrameIndex(self): 461 context, ref = self.value 462 frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame 463 data = self.load(ref) 464 element = self.paramlist[data.funccode + self.operand] 465 # NOTE: Need to ensure correct positioning where a context has been generated. 466 param_index, offset = element 467 if param_index == self.operand: 468 self.frame_stack[frame + offset + 1] = self.source # add 1 to skip the context always generated 469 else: 470 self.exception = self.type_error 471 return self.RaiseException() 472 473 def LoadCallable(self): 474 context, ref = self.value 475 data = self.load(ref) 476 self.callable = data.codeaddr, data.codedetails 477 478 def StoreCallable(self): 479 context, ref = self.value 480 # NOTE: Should improve the representation and permit direct saving. 481 data = self.load(ref) 482 self.save(ref, (data.classcode, data.attrcode) + self.callable) 483 484 def LoadContext(self): 485 context, ref = self.value 486 self.value = None, context # context of context is not interesting 487 488 def CheckFrame(self): 489 operand = self.operand 490 frame = self.invocation_sp_stack[-1] 491 context, ref = self.value 492 data = self.load(ref) 493 494 # Support sliding of the frame to exclude any inappropriate context. 495 496 if context is None: 497 self.invocation_sp_stack[-1] += 1 498 operand -= 1 499 else: 500 context_data = self.load(context) 501 if context_data.attrcode is None: # absent attrcode == class 502 self.invocation_sp_stack[-1] += 1 503 operand -= 1 504 505 # Test the frame size. 506 507 nargs, ndefaults = data.codedetails 508 if not ((nargs - ndefaults) <= operand <= nargs): 509 raise Exception, "CheckFrame %r (%r <= %r <= %r)" % (self.operand, nargs - ndefaults, operand, nargs) 510 511 # Support population of defaults. 512 # This involves copying the "attributes" of a function into the frame. 513 514 default = operand - (nargs - ndefaults) 515 self.frame_stack.extend([None] * (nargs - operand)) 516 pos = self.operand 517 518 while operand < nargs: 519 self.frame_stack[frame + pos] = self.load(ref + default + 1) # skip header 520 default += 1 521 pos += 1 522 operand += 1 523 524 def CheckSelf(self): 525 context, ref = self.value 526 target_context, target_ref = self.source 527 528 # Check the details of the proposed context and the target's context. 529 530 self.status = self._CheckInstance(ref, target_context) 531 532 def JumpWithFrame(self): 533 codeaddr, codedetails = self.callable 534 self.local_sp_stack.append(self.invocation_sp_stack[-1]) # adopt the invocation frame 535 return self.jump(codeaddr, self.pc + 1) # return to the instruction after this one 536 537 def ExtendFrame(self): 538 self.frame_stack.extend([None] * self.operand) 539 540 def AdjustFrame(self): 541 self.invocation_sp_stack[-1] += self.operand 542 543 def Return(self): 544 return self.pull_pc() 545 546 def LoadResult(self): 547 self.value = self.result 548 549 def StoreResult(self): 550 self.result = self.value 551 552 def Jump(self): 553 return self.operand 554 555 def JumpIfTrue(self): 556 if self.status: 557 return self.operand 558 559 def JumpIfFalse(self): 560 if not self.status: 561 return self.operand 562 563 def LoadException(self): 564 self.value = self.exception, self.exception 565 566 def StoreException(self): 567 self.exception = self.value[1] 568 569 def RaiseException(self): 570 return self.handler_stack[-1] 571 572 def PushHandler(self): 573 self.handler_stack.append(self.operand) 574 self.handler_local_sp_stack.append(len(self.local_sp_stack)) 575 self.handler_pc_stack.append(len(self.pc_stack)) 576 577 def PopHandler(self): 578 # Reduce the local frame pointer stack to refer to the handler's frame. 579 self.local_sp_stack = self.local_sp_stack[:self.handler_local_sp_stack.pop()] 580 # Reduce the PC stack to discard all superfluous return addresses. 581 self.pc_stack = self.pc_stack[:self.handler_pc_stack.pop()] 582 self.handler_stack.pop() 583 584 def CheckException(self): 585 self.status = self.exception is not None and self._CheckInstance(self.exception, self.value[1]) 586 587 def TestIdentity(self): 588 self.status = self.value[1] == self.source[1] 589 590 def TestIdentityAddress(self): 591 self.status = self.value[1] == self.operand 592 593 # LoadBoolean is implemented in the generated code. 594 # StoreBoolean is implemented by testing against the True value. 595 596 def InvertBoolean(self): 597 self.status = not self.status 598 599 # Common implementation details. 600 601 def _CheckInstance(self, ref, cls): 602 data = self.load(ref) 603 target_data = self.load(cls) 604 605 # Insist on a class. 606 607 if target_data.attrcode is not None: # present attrcode == instance 608 return 0 609 610 # Find the table entry for the descendant. 611 612 element = self.objlist[target_data.classcode + data.attrcode] 613 attr_index, class_attr, offset = element 614 return attr_index == data.attrcode 615 616 def _MakeObject(self, size, ref): 617 # Load the template. 618 data = self.load(ref) 619 addr = self.new(size) 620 # Save the header, overriding the size. 621 self.save(addr, data.with_size(size)) 622 return addr 623 624 def _LoadAddressContextCond(self, context, ref, inst_context, inst_ref): 625 626 # Check the instance context against the target's context. 627 628 if self._CheckInstance(inst_ref, context): 629 # Replace the context with the instance. 630 return inst_ref, ref 631 else: 632 return context, ref 633 634 # Native function implementations. 635 636 def builtins_int_add(self): 637 frame = self.local_sp_stack[-1] 638 639 # Get operands addresses. 640 641 left_context, left = self.frame_stack[frame] 642 right_context, right = self.frame_stack[frame + 1] 643 644 # Test operand suitability. 645 646 if not self._CheckInstance(left, self.int_class_location) and self._CheckInstance(right, self.int_class_location): 647 self.exception = self.type_error 648 return self.RaiseException() 649 650 # NOTE: Assume single location for data. 651 652 left_data = left + 1 653 right_data = right + 1 654 655 # Make a new object. 656 657 addr = self._MakeObject(2, self.int_instance_location) 658 659 # Store the result. 660 # NOTE: The data is considered ready to use. 661 662 self.save(addr + 1, self.load(left_data) + self.load(right_data)) 663 664 # Return the new object. 665 # Introduce object as context for the new object. 666 667 self.result = addr, addr 668 669 def builtins_int_bool(self): 670 frame = self.local_sp_stack[-1] 671 672 # Get operands addresses. 673 674 left_context, left = self.frame_stack[frame] 675 676 # Test operand suitability. 677 678 if not self._CheckInstance(left, self.int_class_location): 679 self.exception = self.type_error 680 return self.RaiseException() 681 682 # NOTE: Assume single location for data. 683 684 left_data = left + 1 685 686 # Test the data. 687 # NOTE: The data is considered ready to use. 688 689 if self.load(left_data) != 0: 690 self.result = self.true_constant, self.true_constant 691 else: 692 self.result = self.false_constant, self.false_constant 693 694 def builtins_bool_bool(self): 695 frame = self.local_sp_stack[-1] 696 697 # Get operands addresses. 698 699 left_context, left = self.frame_stack[frame] 700 self.result = left, left 701 702 def builtins_list_new(self): 703 frame = self.local_sp_stack[-1] 704 705 # NOTE: Specific copying of tuples/lists. 706 707 args_context, args = self.frame_stack[frame] 708 header = self.load(args) 709 710 list = self._MakeObject(header.size, self.list_instance_location) 711 for i in range(1, header.size): 712 self.save(list + i, self.load(args + i)) 713 714 self.result = list, list 715 716 def builtins_object_init(self): 717 pass 718 719 native_functions = { 720 "__builtins__.int.__add__" : builtins_int_add, 721 "__builtins__.int.__bool__" : builtins_int_bool, 722 "__builtins__.bool.__bool__" : builtins_bool_bool, 723 "__builtins__.list" : builtins_list_new, 724 "__builtins__.object.__init__" : builtins_object_init, 725 } 726 727 # vim: tabstop=4 expandtab shiftwidth=4