paul@68 | 1 | #!/usr/bin/env python |
paul@68 | 2 | |
paul@68 | 3 | """ |
paul@68 | 4 | A really simple virtual processor employing a simple set of instructions which |
paul@68 | 5 | ignore low-level operations and merely concentrate on variable access, structure |
paul@68 | 6 | access, structure allocation and function invocations. |
paul@68 | 7 | |
paul@394 | 8 | Copyright (C) 2007, 2008, 2009, 2010, 2011 Paul Boddie <paul@boddie.org.uk> |
paul@68 | 9 | |
paul@68 | 10 | This program is free software; you can redistribute it and/or modify it under |
paul@68 | 11 | the terms of the GNU General Public License as published by the Free Software |
paul@68 | 12 | Foundation; either version 3 of the License, or (at your option) any later |
paul@68 | 13 | version. |
paul@68 | 14 | |
paul@68 | 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@68 | 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@68 | 17 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@68 | 18 | details. |
paul@68 | 19 | |
paul@68 | 20 | You should have received a copy of the GNU General Public License along with |
paul@68 | 21 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@68 | 22 | |
paul@68 | 23 | -------- |
paul@68 | 24 | |
paul@68 | 25 | The execution model of the virtual processor involves the following things: |
paul@68 | 26 | |
paul@145 | 27 | * Memory contains constants, global variable |
paul@145 | 28 | references and program code |
paul@145 | 29 | |
paul@145 | 30 | * PC (program counter) stack contains the return address associated |
paul@145 | 31 | with each function invocation |
paul@145 | 32 | |
paul@145 | 33 | * Frame stack contains invocation frames in use and in |
paul@145 | 34 | preparation plus temporary storage |
paul@145 | 35 | |
paul@145 | 36 | * Local frame pointer stack refers to the frames in the frame stack |
paul@145 | 37 | |
paul@117 | 38 | * Invocation frame pointer stack |
paul@145 | 39 | |
paul@117 | 40 | * Exception handler stack |
paul@68 | 41 | |
paul@145 | 42 | * Exception handler locals stack refers to the state of the local frame |
paul@145 | 43 | pointer stack |
paul@145 | 44 | |
paul@145 | 45 | * Exception handler PC stack refers to the state of the PC stack |
paul@68 | 46 | |
paul@450 | 47 | * Registers: working context/value, |
paul@450 | 48 | assignment source context/value, |
paul@429 | 49 | current exception value, |
paul@460 | 50 | boolean status value, |
paul@460 | 51 | object list start (constant), |
paul@460 | 52 | parameter list start (constant) |
paul@68 | 53 | """ |
paul@68 | 54 | |
paul@264 | 55 | from micropython.program import DataValue, ReplaceableContext, PlaceholderContext, FragmentObject |
paul@261 | 56 | from rsvplib import Library |
paul@182 | 57 | |
paul@68 | 58 | class IllegalInstruction(Exception): |
paul@68 | 59 | pass |
paul@68 | 60 | |
paul@68 | 61 | class IllegalAddress(Exception): |
paul@180 | 62 | def __init__(self, address): |
paul@180 | 63 | self.address = address |
paul@180 | 64 | def __repr__(self): |
paul@180 | 65 | return "IllegalAddress(%r)" % self.address |
paul@180 | 66 | def __str__(self): |
paul@180 | 67 | return repr(self) |
paul@68 | 68 | |
paul@68 | 69 | class EmptyPCStack(Exception): |
paul@68 | 70 | pass |
paul@68 | 71 | |
paul@92 | 72 | class EmptyFrameStack(Exception): |
paul@68 | 73 | pass |
paul@68 | 74 | |
paul@194 | 75 | class BreakpointReached(Exception): |
paul@194 | 76 | pass |
paul@194 | 77 | |
paul@68 | 78 | class RSVPMachine: |
paul@68 | 79 | |
paul@68 | 80 | "A really simple virtual processor." |
paul@68 | 81 | |
paul@248 | 82 | def __init__(self, memory, objlist, paramlist, pc=None, debug=0, abort_upon_exception=0): |
paul@68 | 83 | |
paul@68 | 84 | """ |
paul@68 | 85 | Initialise the processor with a 'memory' (a list of values containing |
paul@196 | 86 | instructions and data), the object and parameter lists 'objlist' and |
paul@248 | 87 | 'paramlist', and the optional program counter 'pc'. |
paul@68 | 88 | """ |
paul@68 | 89 | |
paul@68 | 90 | self.memory = memory |
paul@202 | 91 | self._objlist = objlist |
paul@202 | 92 | self._paramlist = paramlist |
paul@459 | 93 | end_of_code = len(self.memory) |
paul@459 | 94 | |
paul@459 | 95 | # Add the object list. |
paul@459 | 96 | |
paul@460 | 97 | objlist_start = len(self.memory) |
paul@459 | 98 | self.memory += objlist.as_raw() |
paul@459 | 99 | |
paul@459 | 100 | # Add the parameter list. |
paul@459 | 101 | |
paul@460 | 102 | paramlist_start = len(self.memory) |
paul@459 | 103 | self.memory += paramlist.as_raw() |
paul@459 | 104 | |
paul@459 | 105 | # A reference to the native library. |
paul@459 | 106 | |
paul@248 | 107 | self.library = None |
paul@196 | 108 | |
paul@459 | 109 | # Program counter and machine configuration. |
paul@459 | 110 | |
paul@68 | 111 | self.pc = pc or 0 |
paul@68 | 112 | self.debug = debug |
paul@246 | 113 | self.abort_upon_exception = abort_upon_exception |
paul@313 | 114 | |
paul@313 | 115 | # Profiling. |
paul@313 | 116 | |
paul@459 | 117 | self.coverage = [0] * end_of_code |
paul@285 | 118 | self.counter = 0 |
paul@313 | 119 | self.cost = 0 |
paul@117 | 120 | |
paul@117 | 121 | # Stacks. |
paul@117 | 122 | |
paul@68 | 123 | self.pc_stack = [] |
paul@68 | 124 | self.frame_stack = [] |
paul@143 | 125 | self.local_sp_stack = [0] |
paul@117 | 126 | self.invocation_sp_stack = [] |
paul@398 | 127 | |
paul@398 | 128 | # Exception handler stacks are used to reset the state of the main |
paul@398 | 129 | # stacks. |
paul@398 | 130 | |
paul@459 | 131 | self.handler_stack = [end_of_code - 1] # final handler is the end of the code |
paul@145 | 132 | self.handler_local_sp_stack = [] |
paul@398 | 133 | self.handler_invocation_sp_stack = [] |
paul@145 | 134 | self.handler_pc_stack = [] |
paul@117 | 135 | |
paul@117 | 136 | # Registers. |
paul@117 | 137 | |
paul@450 | 138 | self.register_names = ( |
paul@450 | 139 | "working", |
paul@450 | 140 | "working_context", |
paul@450 | 141 | "source", |
paul@450 | 142 | "source_context", |
paul@450 | 143 | "exception", |
paul@460 | 144 | "status", |
paul@460 | 145 | "objlist", |
paul@460 | 146 | "paramlist" |
paul@450 | 147 | ) |
paul@450 | 148 | self.registers = {} |
paul@429 | 149 | |
paul@450 | 150 | for name in self.register_names: |
paul@450 | 151 | self.registers[name] = None |
paul@429 | 152 | |
paul@460 | 153 | self.registers["objlist"] = objlist_start |
paul@460 | 154 | self.registers["paramlist"] = paramlist_start |
paul@460 | 155 | |
paul@119 | 156 | self.instruction = None |
paul@68 | 157 | |
paul@146 | 158 | # Constants. |
paul@450 | 159 | # NOTE: These should eventually be removed once the code using them has |
paul@450 | 160 | # NOTE: been migrated to the native code library. |
paul@146 | 161 | |
paul@221 | 162 | cls = self._get_class("__builtins__", "AttributeError") |
paul@221 | 163 | self.attr_error = cls.location |
paul@221 | 164 | self.attr_error_instance = cls.instance_template_location |
paul@221 | 165 | cls = self._get_class("__builtins__", "TypeError") |
paul@221 | 166 | self.type_error = cls.location |
paul@221 | 167 | self.type_error_instance = cls.instance_template_location |
paul@256 | 168 | cls = self._get_class("__builtins__", "tuple") |
paul@255 | 169 | self.tuple_class = cls.location |
paul@255 | 170 | self.tuple_instance = cls.instance_template_location |
paul@181 | 171 | |
paul@194 | 172 | # Debugging attributes. |
paul@194 | 173 | |
paul@194 | 174 | self.breakpoints = set() |
paul@194 | 175 | |
paul@221 | 176 | def _get_class(self, module, name): |
paul@346 | 177 | attr = self._objlist.access(module, name) |
paul@346 | 178 | if attr is not None: |
paul@346 | 179 | return attr.get_value() |
paul@346 | 180 | else: |
paul@346 | 181 | return None |
paul@221 | 182 | |
paul@162 | 183 | # Debugging methods. |
paul@162 | 184 | |
paul@137 | 185 | def dump(self): |
paul@138 | 186 | print "PC", self.pc, "->", self.load(self.pc) |
paul@137 | 187 | print "PC stack", self.pc_stack |
paul@401 | 188 | print "Frame stack:" |
paul@401 | 189 | if self.local_sp_stack: |
paul@401 | 190 | start = self.local_sp_stack[0] |
paul@401 | 191 | for end in self.local_sp_stack[1:]: |
paul@401 | 192 | print " %2d" % start, self.frame_stack[start:end] |
paul@401 | 193 | start = end |
paul@401 | 194 | else: |
paul@401 | 195 | print " %2d" % start, self.frame_stack[start:] |
paul@401 | 196 | print |
paul@137 | 197 | print "Local stack pointers", self.local_sp_stack |
paul@137 | 198 | print "Invocation stack pointers", self.invocation_sp_stack |
paul@137 | 199 | print "Handler stack", self.handler_stack |
paul@145 | 200 | print "Handler frame stack", self.handler_local_sp_stack |
paul@145 | 201 | print "Handler PC stack", self.handler_pc_stack |
paul@137 | 202 | print |
paul@450 | 203 | print "Registers:" |
paul@429 | 204 | print |
paul@450 | 205 | for name in self.register_names: |
paul@450 | 206 | print "%s: %s" % (name, self.registers[name]) |
paul@429 | 207 | print |
paul@137 | 208 | print "Instruction", self.instruction |
paul@137 | 209 | |
paul@399 | 210 | def show(self, start=None, end=None): |
paul@399 | 211 | self.show_memory(self.memory[start:end], self.coverage[start:end], start or 0) |
paul@162 | 212 | |
paul@162 | 213 | def show_pc(self, run_in=10): |
paul@162 | 214 | start = max(0, self.pc - run_in) |
paul@162 | 215 | end = self.pc + run_in |
paul@162 | 216 | memory = self.memory[start:end] |
paul@389 | 217 | coverage = self.coverage[start:end] |
paul@389 | 218 | self.show_memory(memory, coverage, start) |
paul@162 | 219 | |
paul@389 | 220 | def show_memory(self, memory, coverage, start): |
paul@389 | 221 | for i, (c, x) in enumerate(map(None, coverage, memory)): |
paul@162 | 222 | location = start + i |
paul@162 | 223 | if location == self.pc: |
paul@162 | 224 | print "->", |
paul@381 | 225 | elif location in self.pc_stack: |
paul@381 | 226 | print "..", |
paul@162 | 227 | else: |
paul@162 | 228 | print " ", |
paul@389 | 229 | print "%-5s %5d %r" % (c or "", location, x) |
paul@162 | 230 | |
paul@162 | 231 | def step(self, dump=0): |
paul@138 | 232 | self.execute() |
paul@162 | 233 | self.show_pc() |
paul@162 | 234 | if dump: |
paul@162 | 235 | self.dump() |
paul@162 | 236 | |
paul@194 | 237 | def set_break(self, location): |
paul@194 | 238 | self.breakpoints.add(location) |
paul@194 | 239 | |
paul@399 | 240 | def up(self): |
paul@399 | 241 | retaddr = self.pc_stack[-1] |
paul@401 | 242 | self.set_break(retaddr) |
paul@399 | 243 | self.run() |
paul@399 | 244 | |
paul@162 | 245 | # Internal operations. |
paul@138 | 246 | |
paul@68 | 247 | def load(self, address): |
paul@68 | 248 | |
paul@68 | 249 | "Return the value at the given 'address'." |
paul@68 | 250 | |
paul@68 | 251 | try: |
paul@68 | 252 | return self.memory[address] |
paul@68 | 253 | except IndexError: |
paul@180 | 254 | raise IllegalAddress(address) |
paul@180 | 255 | except TypeError: |
paul@180 | 256 | raise IllegalAddress(address) |
paul@68 | 257 | |
paul@68 | 258 | def save(self, address, value): |
paul@68 | 259 | |
paul@68 | 260 | "Save to the given 'address' the specified 'value'." |
paul@68 | 261 | |
paul@68 | 262 | try: |
paul@68 | 263 | self.memory[address] = value |
paul@68 | 264 | except IndexError: |
paul@180 | 265 | raise IllegalAddress(address) |
paul@180 | 266 | except TypeError: |
paul@180 | 267 | raise IllegalAddress(address) |
paul@68 | 268 | |
paul@68 | 269 | def new(self, size): |
paul@68 | 270 | |
paul@68 | 271 | """ |
paul@68 | 272 | Allocate space of the given 'size', returning the address of the space. |
paul@68 | 273 | """ |
paul@68 | 274 | |
paul@68 | 275 | addr = len(self.memory) |
paul@68 | 276 | for i in range(0, size): |
paul@68 | 277 | self.memory.append(None) |
paul@68 | 278 | return addr |
paul@68 | 279 | |
paul@68 | 280 | def push_pc(self, data): |
paul@68 | 281 | |
paul@68 | 282 | "Push 'data' onto the PC stack." |
paul@68 | 283 | |
paul@68 | 284 | self.pc_stack.append(data) |
paul@68 | 285 | |
paul@68 | 286 | def pull_pc(self): |
paul@68 | 287 | |
paul@68 | 288 | "Pull a value from the PC stack and return it." |
paul@68 | 289 | |
paul@68 | 290 | try: |
paul@68 | 291 | return self.pc_stack.pop() |
paul@68 | 292 | except IndexError: |
paul@68 | 293 | raise EmptyPCStack |
paul@68 | 294 | |
paul@90 | 295 | def run(self): |
paul@68 | 296 | |
paul@68 | 297 | "Execute code in the memory, starting from the current PC address." |
paul@68 | 298 | |
paul@228 | 299 | breakpoint = 0 |
paul@228 | 300 | |
paul@68 | 301 | try: |
paul@68 | 302 | while 1: |
paul@90 | 303 | self.execute() |
paul@68 | 304 | except EmptyPCStack: |
paul@68 | 305 | pass |
paul@228 | 306 | except BreakpointReached: |
paul@228 | 307 | breakpoint = 1 |
paul@68 | 308 | |
paul@221 | 309 | print "Execution terminated", |
paul@450 | 310 | if breakpoint: |
paul@450 | 311 | print "with breakpoint." |
paul@450 | 312 | print "At address", self.pc |
paul@450 | 313 | elif self.registers["exception"] is not None: |
paul@450 | 314 | ref = self.registers["exception"] |
paul@399 | 315 | addr = self.load(ref + Library.instance_data_offset) |
paul@221 | 316 | print "with exception:", self.load(ref) |
paul@222 | 317 | print "At address %d: %r" % (addr, self.load(addr)) |
paul@221 | 318 | else: |
paul@221 | 319 | print "successfully." |
paul@313 | 320 | print "After", self.counter, "instructions at cost", self.cost, "units." |
paul@221 | 321 | |
paul@228 | 322 | def test(self, module): |
paul@228 | 323 | |
paul@228 | 324 | """ |
paul@228 | 325 | Test the code in the memory by running the code and investigating the |
paul@228 | 326 | contents of variables. Use 'module' to identify result variables. |
paul@228 | 327 | """ |
paul@228 | 328 | |
paul@228 | 329 | self.run() |
paul@228 | 330 | success = 1 |
paul@228 | 331 | |
paul@450 | 332 | if self.registers["exception"] is None: |
paul@229 | 333 | for name in module.keys(): |
paul@229 | 334 | if name.startswith("result"): |
paul@229 | 335 | label, expected = name.split("_") |
paul@229 | 336 | attr = module[name] |
paul@228 | 337 | |
paul@450 | 338 | # Need to skip the header to get at the members. |
paul@228 | 339 | |
paul@450 | 340 | attr_location = module.location + Library.instance_data_offset + attr.position |
paul@264 | 341 | value = self.load(attr_location) |
paul@228 | 342 | |
paul@264 | 343 | if value is not None: |
paul@395 | 344 | content = self.load(value.ref + Library.instance_data_offset) |
paul@243 | 345 | print label, expected, content |
paul@243 | 346 | success = success and (int(expected) == content) |
paul@243 | 347 | else: |
paul@243 | 348 | print label, expected, "missing" |
paul@243 | 349 | success = 0 |
paul@228 | 350 | |
paul@229 | 351 | return success |
paul@229 | 352 | else: |
paul@229 | 353 | return 0 |
paul@228 | 354 | |
paul@90 | 355 | def execute(self): |
paul@90 | 356 | |
paul@90 | 357 | "Execute code in the memory at the current PC address." |
paul@90 | 358 | |
paul@194 | 359 | if self.pc in self.breakpoints: |
paul@194 | 360 | self.breakpoints.remove(self.pc) |
paul@194 | 361 | raise BreakpointReached |
paul@194 | 362 | |
paul@119 | 363 | self.instruction = self.load(self.pc) |
paul@118 | 364 | |
paul@137 | 365 | # Perform the instruction itself. |
paul@137 | 366 | |
paul@137 | 367 | next_pc = self.perform(self.instruction) |
paul@118 | 368 | |
paul@389 | 369 | # Update the coverage. |
paul@389 | 370 | |
paul@389 | 371 | self.coverage[self.pc] += 1 |
paul@389 | 372 | |
paul@119 | 373 | # Update the program counter. |
paul@118 | 374 | |
paul@119 | 375 | if next_pc is None: |
paul@119 | 376 | self.pc += 1 |
paul@119 | 377 | else: |
paul@119 | 378 | self.pc = next_pc |
paul@118 | 379 | |
paul@137 | 380 | def get_method(self, instruction): |
paul@137 | 381 | |
paul@137 | 382 | "Return the handler method for the given 'instruction'." |
paul@119 | 383 | |
paul@137 | 384 | instruction_name = instruction.__class__.__name__ |
paul@137 | 385 | if self.debug: |
paul@137 | 386 | print "%8d %s" % (self.pc, instruction_name) |
paul@119 | 387 | method = getattr(self, instruction_name, None) |
paul@90 | 388 | if method is None: |
paul@119 | 389 | raise IllegalInstruction, (self.pc, instruction_name) |
paul@118 | 390 | return method |
paul@118 | 391 | |
paul@450 | 392 | def perform(self, instruction): |
paul@137 | 393 | |
paul@137 | 394 | "Perform the 'instruction', returning the next PC value or None." |
paul@137 | 395 | |
paul@450 | 396 | self.counter += 1 |
paul@450 | 397 | self.cost += instruction.cost |
paul@430 | 398 | operand = instruction.get_operand() |
paul@137 | 399 | method = self.get_method(instruction) |
paul@450 | 400 | return method(operand, instruction) |
paul@90 | 401 | |
paul@68 | 402 | def jump(self, addr, next): |
paul@68 | 403 | |
paul@68 | 404 | """ |
paul@68 | 405 | Jump to the subroutine at (or identified by) 'addr'. If 'addr' |
paul@68 | 406 | identifies a library function then invoke the library function and set |
paul@68 | 407 | PC to 'next' afterwards; otherwise, set PC to 'addr'. |
paul@68 | 408 | """ |
paul@68 | 409 | |
paul@181 | 410 | # Trap library functions introduced through the use of strings instead |
paul@181 | 411 | # of proper locations. |
paul@181 | 412 | |
paul@68 | 413 | if isinstance(addr, str): |
paul@248 | 414 | handler = self.library and self.library.native_functions[addr](self.library) |
paul@196 | 415 | if handler is None: |
paul@196 | 416 | return next |
paul@196 | 417 | else: |
paul@196 | 418 | return handler |
paul@68 | 419 | else: |
paul@138 | 420 | self.push_pc(self.pc + 1) |
paul@119 | 421 | return addr |
paul@68 | 422 | |
paul@450 | 423 | # Helper methods. |
paul@450 | 424 | |
paul@450 | 425 | def context_of(self, register): |
paul@450 | 426 | return "%s_context" % register |
paul@450 | 427 | |
paul@450 | 428 | def load_from_frame(self, operand): |
paul@450 | 429 | frame = self.local_sp_stack[-1] |
paul@450 | 430 | return self.frame_stack[frame + operand] |
paul@450 | 431 | |
paul@429 | 432 | # Low-level instructions. |
paul@429 | 433 | |
paul@450 | 434 | def LoadImmediate(self, operand, target): |
paul@450 | 435 | self.registers[target] = operand |
paul@429 | 436 | |
paul@450 | 437 | def LoadMemory(self, operand, target, source): |
paul@450 | 438 | self.registers[target] = self.load( |
paul@450 | 439 | self.registers[source] + (operand is not None and operand or 0) |
paul@450 | 440 | ) |
paul@430 | 441 | |
paul@68 | 442 | # Instructions. |
paul@68 | 443 | |
paul@450 | 444 | def Transfer(self, operand, instruction): |
paul@450 | 445 | self.registers[instruction.target] = self.registers[instruction.source] |
paul@223 | 446 | |
paul@450 | 447 | def LoadConst(self, operand, instruction): |
paul@450 | 448 | self.LoadImmediate(operand, self.context_of(instruction.target)) |
paul@450 | 449 | self.LoadImmediate(operand, instruction.target) |
paul@450 | 450 | |
paul@450 | 451 | def LoadClass(self, operand, instruction): |
paul@450 | 452 | self.LoadImmediate(PlaceholderContext, self.context_of(instruction.target)) |
paul@450 | 453 | self.LoadImmediate(operand, instruction.target) |
paul@237 | 454 | |
paul@450 | 455 | def LoadFunction(self, operand, instruction): |
paul@450 | 456 | self.LoadImmediate(ReplaceableContext, self.context_of(instruction.target)) |
paul@450 | 457 | self.LoadImmediate(operand, instruction.target) |
paul@68 | 458 | |
paul@450 | 459 | def LoadName(self, operand, instruction): |
paul@450 | 460 | data = self.load_from_frame(operand) |
paul@450 | 461 | self.LoadImmediate(data.context, self.context_of(instruction.target)) |
paul@450 | 462 | self.LoadImmediate(data.ref, instruction.target) |
paul@450 | 463 | |
paul@450 | 464 | def StoreName(self, operand, instruction): |
paul@117 | 465 | frame = self.local_sp_stack[-1] |
paul@450 | 466 | self.frame_stack[frame + operand] = DataValue( |
paul@450 | 467 | self.registers[self.context_of(instruction.source)], |
paul@450 | 468 | self.registers[instruction.source]) |
paul@68 | 469 | |
paul@72 | 470 | LoadTemp = LoadName |
paul@203 | 471 | |
paul@450 | 472 | def StoreTemp(self, operand, instruction): |
paul@203 | 473 | frame = self.local_sp_stack[-1] |
paul@450 | 474 | self.frame_stack[frame + operand] = DataValue( |
paul@450 | 475 | self.registers[self.context_of(instruction.working)], |
paul@450 | 476 | self.registers[instruction.working]) |
paul@72 | 477 | |
paul@450 | 478 | def LoadAddress(self, operand, instruction): |
paul@119 | 479 | # Preserve context (potentially null). |
paul@430 | 480 | data = self.load(operand) |
paul@450 | 481 | self.LoadImmediate(data.context, self.context_of(instruction.target)) |
paul@450 | 482 | self.LoadImmediate(data.ref, instruction.target) |
paul@72 | 483 | |
paul@450 | 484 | def LoadAddressContext(self, operand, instruction): |
paul@429 | 485 | # Value becomes context. |
paul@430 | 486 | data = self.load(operand) |
paul@450 | 487 | self.LoadImmediate(self.registers[instruction.working], self.context_of(instruction.target)) |
paul@450 | 488 | self.LoadImmediate(data.ref, instruction.target) |
paul@194 | 489 | |
paul@450 | 490 | def LoadAddressContextCond(self, operand, instruction): |
paul@430 | 491 | data = self.load(operand) |
paul@450 | 492 | data = self._LoadAddressContextCond(data.context, data.ref, self.registers[instruction.working]) |
paul@450 | 493 | self.LoadImmediate(data.context, self.context_of(instruction.target)) |
paul@450 | 494 | self.LoadImmediate(data.ref, instruction.target) |
paul@72 | 495 | |
paul@450 | 496 | def StoreAddress(self, operand, instruction): |
paul@119 | 497 | # Preserve context. |
paul@450 | 498 | self.save(operand, DataValue( |
paul@450 | 499 | self.registers[self.context_of(instruction.source)], |
paul@450 | 500 | self.registers[instruction.source])) |
paul@72 | 501 | |
paul@450 | 502 | def StoreAddressContext(self, operand, instruction): |
paul@223 | 503 | # Overwrite context if null. |
paul@450 | 504 | self._StoreAddressContext(operand, |
paul@450 | 505 | self.registers[self.context_of(instruction.working)], |
paul@450 | 506 | self.registers[instruction.working], |
paul@450 | 507 | self.registers[self.context_of(instruction.source)], |
paul@450 | 508 | self.registers[instruction.source]) |
paul@223 | 509 | |
paul@450 | 510 | def MakeInstance(self, size, instruction): |
paul@184 | 511 | # NOTE: Referencing the instance template. |
paul@450 | 512 | addr = self._MakeObject(size, self.registers[instruction.working] - Library.instance_template_size) |
paul@184 | 513 | # Introduce object as context for the new object. |
paul@450 | 514 | self.LoadImmediate(addr, self.context_of(instruction.target)) |
paul@450 | 515 | self.LoadImmediate(addr, instruction.target) |
paul@72 | 516 | |
paul@450 | 517 | def MakeFragment(self, size, instruction): |
paul@336 | 518 | # Reserve twice the amount of space. |
paul@336 | 519 | addr = self._MakeFragment(size, size * 2) |
paul@246 | 520 | # NOTE: Context is not relevant for fragments. |
paul@450 | 521 | self.LoadImmediate(None, self.context_of(instruction.target)) |
paul@450 | 522 | self.LoadImmediate(addr, instruction.target) |
paul@246 | 523 | |
paul@450 | 524 | def LoadAttr(self, pos, instruction): |
paul@127 | 525 | # Retrieved context should already be appropriate for the instance. |
paul@450 | 526 | # Skip any header. |
paul@450 | 527 | data = self.load(self.registers[instruction.working] + Library.instance_data_offset + pos) |
paul@450 | 528 | self.LoadImmediate(data.context, self.context_of(instruction.target)) |
paul@450 | 529 | self.LoadImmediate(data.ref, instruction.target) |
paul@118 | 530 | |
paul@450 | 531 | def StoreAttr(self, pos, instruction): |
paul@129 | 532 | # Target should already be an instance. |
paul@450 | 533 | # Skip any header. |
paul@450 | 534 | self.save(self.registers[instruction.working] + Library.instance_data_offset + pos , DataValue( |
paul@450 | 535 | self.registers[self.context_of(instruction.source)], |
paul@450 | 536 | self.registers[instruction.source])) |
paul@119 | 537 | |
paul@450 | 538 | def LoadAttrIndex(self, index, instruction): |
paul@450 | 539 | data = self.load(self.registers[instruction.working]) |
paul@460 | 540 | element = self.load(self.registers["objlist"] + data.classcode + index) |
paul@238 | 541 | |
paul@238 | 542 | if element is not None: |
paul@242 | 543 | attr_index, static_attr, offset = element |
paul@430 | 544 | if attr_index == index: |
paul@242 | 545 | if static_attr: |
paul@429 | 546 | data = self.load(offset) # offset is address of class/module attribute |
paul@238 | 547 | else: |
paul@450 | 548 | data = self.load(self.registers[instruction.working] + offset) |
paul@450 | 549 | self.LoadImmediate(data.context, self.context_of(instruction.target)) |
paul@450 | 550 | self.LoadImmediate(data.ref, instruction.target) |
paul@238 | 551 | return |
paul@238 | 552 | |
paul@450 | 553 | self.registers["exception"] = self._MakeObject(Library.instance_size, self.attr_error_instance) |
paul@238 | 554 | return self.RaiseException() |
paul@118 | 555 | |
paul@226 | 556 | # LoadAttrIndexContext not defined. |
paul@194 | 557 | |
paul@450 | 558 | def LoadAttrIndexContextCond(self, index, instruction): |
paul@450 | 559 | data = self.load(self.registers[instruction.working]) |
paul@460 | 560 | element = self.load(self.registers["objlist"] + data.classcode + index) |
paul@238 | 561 | |
paul@238 | 562 | if element is not None: |
paul@242 | 563 | attr_index, static_attr, offset = element |
paul@430 | 564 | if attr_index == index: |
paul@242 | 565 | if static_attr: |
paul@429 | 566 | loaded_data = self.load(offset) # offset is address of class/module attribute |
paul@429 | 567 | # Absent attrcode == class/module. |
paul@429 | 568 | if data.attrcode is not None: |
paul@450 | 569 | loaded_data = self._LoadAddressContextCond( |
paul@450 | 570 | loaded_data.context, loaded_data.ref, |
paul@450 | 571 | self.registers[instruction.working]) |
paul@223 | 572 | else: |
paul@450 | 573 | loaded_data = self.load(self.registers[instruction.working] + offset) |
paul@450 | 574 | self.LoadImmediate(loaded_data.context, self.context_of(instruction.target)) |
paul@450 | 575 | self.LoadImmediate(loaded_data.ref, instruction.target) |
paul@238 | 576 | return |
paul@238 | 577 | |
paul@450 | 578 | self.registers["exception"] = self._MakeObject(Library.instance_size, self.attr_error_instance) |
paul@238 | 579 | return self.RaiseException() |
paul@194 | 580 | |
paul@450 | 581 | def StoreAttrIndex(self, index, instruction): |
paul@450 | 582 | data = self.load(self.registers[instruction.working]) |
paul@460 | 583 | element = self.load(self.registers["objlist"] + data.classcode + index) |
paul@238 | 584 | |
paul@238 | 585 | if element is not None: |
paul@242 | 586 | attr_index, static_attr, offset = element |
paul@430 | 587 | if attr_index == index: |
paul@242 | 588 | if static_attr: |
paul@450 | 589 | self._StoreAddressContext(offset, |
paul@450 | 590 | self.registers[self.context_of(instruction.working)], |
paul@450 | 591 | self.registers[instruction.working], |
paul@450 | 592 | self.registers[self.context_of(instruction.source)], |
paul@450 | 593 | self.registers[instruction.source]) |
paul@405 | 594 | return |
paul@238 | 595 | else: |
paul@450 | 596 | self.save(self.registers[instruction.working] + offset, DataValue( |
paul@450 | 597 | self.registers[self.context_of(instruction.source)], |
paul@450 | 598 | self.registers[instruction.source])) |
paul@238 | 599 | return |
paul@238 | 600 | |
paul@450 | 601 | self.registers["exception"] = self._MakeObject(Library.instance_size, self.attr_error_instance) |
paul@238 | 602 | return self.RaiseException() |
paul@118 | 603 | |
paul@127 | 604 | # NOTE: LoadAttrIndexContext is a possibility if a particular attribute can always be overridden. |
paul@127 | 605 | |
paul@450 | 606 | def MakeFrame(self, size, instruction): |
paul@117 | 607 | self.invocation_sp_stack.append(len(self.frame_stack)) |
paul@430 | 608 | self.frame_stack.extend([None] * size) |
paul@98 | 609 | |
paul@450 | 610 | def DropFrame(self, operand, instruction): |
paul@117 | 611 | self.local_sp_stack.pop() |
paul@117 | 612 | frame = self.invocation_sp_stack.pop() |
paul@248 | 613 | del self.frame_stack[frame:] # reset stack before call |
paul@98 | 614 | |
paul@450 | 615 | def StoreFrame(self, pos, instruction): |
paul@129 | 616 | frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame |
paul@450 | 617 | self.frame_stack[frame + pos] = DataValue( |
paul@450 | 618 | self.registers[self.context_of(instruction.working)], |
paul@450 | 619 | self.registers[instruction.working]) |
paul@129 | 620 | |
paul@450 | 621 | def StoreFrameIndex(self, index, instruction): |
paul@129 | 622 | frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame |
paul@450 | 623 | data = self.load(self.registers[instruction.working]) |
paul@460 | 624 | element = self.load(self.registers["paramlist"] + data.funccode + index) |
paul@238 | 625 | |
paul@238 | 626 | if element is not None: |
paul@238 | 627 | # NOTE: Need to ensure correct positioning where a context has been generated. |
paul@238 | 628 | param_index, offset = element |
paul@430 | 629 | if param_index == index: |
paul@450 | 630 | self.frame_stack[frame + offset] = DataValue( |
paul@450 | 631 | self.registers[self.context_of(instruction.source)], |
paul@450 | 632 | self.registers[instruction.source]) |
paul@238 | 633 | return |
paul@238 | 634 | |
paul@450 | 635 | self.registers["exception"] = self._MakeObject(Library.instance_size, self.type_error_instance) |
paul@238 | 636 | return self.RaiseException() |
paul@129 | 637 | |
paul@450 | 638 | def LoadCallable(self, operand, instruction): |
paul@450 | 639 | data = self.load(self.registers[instruction.working]) |
paul@450 | 640 | self.registers[instruction.target] = data.codeaddr |
paul@132 | 641 | |
paul@450 | 642 | def StoreCallable(self, operand, instruction): |
paul@450 | 643 | # NOTE: Should improve the representation and permit direct saving. |
paul@450 | 644 | data = self.load(self.registers[instruction.working]) |
paul@450 | 645 | self.save(self.registers[instruction.working], data.with_callable(self.registers[instruction.source])) |
paul@132 | 646 | |
paul@450 | 647 | def CheckContext(self, operand, instruction): |
paul@450 | 648 | self.registers[instruction.target] = self.registers[instruction.working] is not ReplaceableContext |
paul@222 | 649 | |
paul@450 | 650 | def CheckClass(self, operand, instruction): |
paul@450 | 651 | if self.registers[instruction.working] in (ReplaceableContext, PlaceholderContext): |
paul@450 | 652 | self.registers[instruction.target] = 0 |
paul@237 | 653 | return |
paul@237 | 654 | |
paul@450 | 655 | data = self.load(self.registers[instruction.working]) |
paul@222 | 656 | |
paul@222 | 657 | # Classes are not themselves usable as the self argument. |
paul@222 | 658 | # NOTE: This may change at some point. |
paul@222 | 659 | # However, where classes appear as the context, instance |
paul@222 | 660 | # compatibility is required in the first argument. |
paul@222 | 661 | |
paul@450 | 662 | self.registers[instruction.target] = data.attrcode is None # absent attrcode == class |
paul@222 | 663 | |
paul@450 | 664 | def CheckFrame(self, operand, instruction): |
paul@430 | 665 | (nargs, ndefaults) = operand |
paul@215 | 666 | |
paul@215 | 667 | # The frame is actually installed as the locals. |
paul@215 | 668 | # Retrieve the context from the first local. |
paul@215 | 669 | |
paul@215 | 670 | frame = self.local_sp_stack[-1] |
paul@215 | 671 | nlocals = len(self.frame_stack[frame:]) |
paul@134 | 672 | |
paul@234 | 673 | if not ((nargs - ndefaults) <= nlocals): |
paul@430 | 674 | raise Exception, "CheckFrame %r (%r <= %r <= %r)" % (operand, nargs - ndefaults, nlocals, nargs) |
paul@450 | 675 | self.registers["exception"] = self._MakeObject(Library.instance_size, self.type_error_instance) |
paul@221 | 676 | return self.RaiseException() |
paul@215 | 677 | |
paul@450 | 678 | def CheckExtra(self, nargs, instruction): |
paul@256 | 679 | |
paul@256 | 680 | # The frame is actually installed as the locals. |
paul@256 | 681 | # Retrieve the context from the first local. |
paul@256 | 682 | |
paul@256 | 683 | frame = self.local_sp_stack[-1] |
paul@256 | 684 | nlocals = len(self.frame_stack[frame:]) |
paul@256 | 685 | |
paul@256 | 686 | # Provide the extra star parameter if necessary. |
paul@256 | 687 | |
paul@256 | 688 | if nlocals == nargs: |
paul@256 | 689 | self.frame_stack.extend([None]) # ExtendFrame(1) |
paul@256 | 690 | |
paul@450 | 691 | def FillDefaults(self, operand, instruction): |
paul@430 | 692 | (nargs, ndefaults) = operand |
paul@215 | 693 | |
paul@215 | 694 | # The frame is actually installed as the locals. |
paul@215 | 695 | |
paul@215 | 696 | frame = self.local_sp_stack[-1] |
paul@215 | 697 | nlocals = len(self.frame_stack[frame:]) |
paul@134 | 698 | |
paul@190 | 699 | # Support population of defaults. |
paul@190 | 700 | # This involves copying the "attributes" of a function into the frame. |
paul@190 | 701 | |
paul@215 | 702 | default = nlocals - (nargs - ndefaults) |
paul@215 | 703 | self.frame_stack.extend([None] * (nargs - nlocals)) |
paul@215 | 704 | pos = nlocals |
paul@190 | 705 | |
paul@215 | 706 | while pos < nargs: |
paul@450 | 707 | |
paul@450 | 708 | # Skip any header. |
paul@450 | 709 | |
paul@450 | 710 | self.frame_stack[frame + pos] = self.load(self.registers[instruction.working] + Library.instance_data_offset + default) |
paul@190 | 711 | default += 1 |
paul@190 | 712 | pos += 1 |
paul@117 | 713 | |
paul@450 | 714 | def CopyExtra(self, start, instruction): |
paul@255 | 715 | |
paul@255 | 716 | # The frame is the source of the extra arguments. |
paul@255 | 717 | |
paul@255 | 718 | frame = self.local_sp_stack[-1] |
paul@255 | 719 | nlocals = len(self.frame_stack[frame:]) |
paul@255 | 720 | |
paul@255 | 721 | # Make a tuple to hold the arguments. |
paul@255 | 722 | |
paul@255 | 723 | ref = self._MakeObject(nlocals - start + 1, self.tuple_instance) |
paul@255 | 724 | |
paul@255 | 725 | extra = 0 |
paul@255 | 726 | pos = start |
paul@255 | 727 | |
paul@255 | 728 | while pos < nlocals: |
paul@450 | 729 | |
paul@450 | 730 | # Skip any header. |
paul@450 | 731 | |
paul@450 | 732 | self.save(ref + Library.instance_data_offset + extra, self.frame_stack[frame + pos]) |
paul@255 | 733 | extra += 1 |
paul@255 | 734 | pos += 1 |
paul@255 | 735 | |
paul@450 | 736 | self.LoadImmediate(ref, self.context_of(instruction.working)) |
paul@450 | 737 | self.LoadImmediate(ref, instruction.working) |
paul@255 | 738 | |
paul@450 | 739 | def CheckInstance(self, operand, instruction): |
paul@285 | 740 | # For the 'self' parameter in an invoked function, the proposed context |
paul@285 | 741 | # ('self') is checked against the target's context. |
paul@450 | 742 | self.registers[instruction.target] = self._CheckInstance( |
paul@450 | 743 | self.registers[instruction.working], |
paul@450 | 744 | self.registers[instruction.source]) |
paul@308 | 745 | |
paul@450 | 746 | def JumpInFrame(self, operand, instruction): |
paul@450 | 747 | codeaddr = self.registers[instruction.working] |
paul@230 | 748 | return self.jump(codeaddr, self.pc + 1) # return to the instruction after this one |
paul@230 | 749 | |
paul@450 | 750 | def JumpWithFrame(self, operand, instruction): |
paul@450 | 751 | codeaddr = self.registers[instruction.working] |
paul@132 | 752 | self.local_sp_stack.append(self.invocation_sp_stack[-1]) # adopt the invocation frame |
paul@138 | 753 | return self.jump(codeaddr, self.pc + 1) # return to the instruction after this one |
paul@98 | 754 | |
paul@450 | 755 | def JumpWithFrameDirect(self, addr, instruction): |
paul@215 | 756 | self.local_sp_stack.append(self.invocation_sp_stack[-1]) # adopt the invocation frame |
paul@430 | 757 | return self.jump(addr, self.pc + 1) # return to the instruction after this one |
paul@215 | 758 | |
paul@450 | 759 | def ExtendFrame(self, size, instruction): |
paul@430 | 760 | self.frame_stack.extend([None] * size) |
paul@98 | 761 | |
paul@450 | 762 | def AdjustFrame(self, size, instruction): |
paul@430 | 763 | self.invocation_sp_stack[-1] += size |
paul@137 | 764 | |
paul@450 | 765 | def Return(self, operand, instruction): |
paul@138 | 766 | return self.pull_pc() |
paul@68 | 767 | |
paul@450 | 768 | def Jump(self, addr, instruction): |
paul@430 | 769 | return addr |
paul@98 | 770 | |
paul@450 | 771 | def JumpIfTrue(self, addr, instruction): |
paul@450 | 772 | if self.registers[instruction.working]: |
paul@430 | 773 | return addr |
paul@68 | 774 | |
paul@450 | 775 | def JumpIfFalse(self, addr, instruction): |
paul@450 | 776 | if not self.registers[instruction.working]: |
paul@450 | 777 | return addr |
paul@129 | 778 | |
paul@450 | 779 | def ClearException(self, operand, instruction): |
paul@450 | 780 | self.LoadImmediate(None, instruction.target) |
paul@232 | 781 | |
paul@450 | 782 | def RaiseException(self, operand=None, instruction=None): |
paul@399 | 783 | # NOTE: Adding the program counter as the first attribute after __class__. |
paul@450 | 784 | self.save(self.registers["exception"] + Library.instance_data_offset, self.pc) |
paul@221 | 785 | # Jumping to the current handler. |
paul@246 | 786 | if self.abort_upon_exception: |
paul@246 | 787 | raise Exception |
paul@145 | 788 | return self.handler_stack[-1] |
paul@129 | 789 | |
paul@450 | 790 | def PushHandler(self, addr, instruction): |
paul@430 | 791 | self.handler_stack.append(addr) |
paul@145 | 792 | self.handler_local_sp_stack.append(len(self.local_sp_stack)) |
paul@398 | 793 | self.handler_invocation_sp_stack.append(len(self.invocation_sp_stack)) |
paul@145 | 794 | self.handler_pc_stack.append(len(self.pc_stack)) |
paul@129 | 795 | |
paul@450 | 796 | def PopHandler(self, nframes, instruction): |
paul@398 | 797 | # Get the new local frame pointer and PC stack references. |
paul@398 | 798 | local_sp_top = self.handler_local_sp_stack[-nframes] |
paul@398 | 799 | invocation_sp_top = self.handler_invocation_sp_stack[-nframes] |
paul@398 | 800 | pc_top = self.handler_pc_stack[-nframes] |
paul@145 | 801 | # Reduce the local frame pointer stack to refer to the handler's frame. |
paul@398 | 802 | del self.local_sp_stack[local_sp_top:] |
paul@398 | 803 | # Reduce the invocation frame pointer stack to refer to an outer frame. |
paul@398 | 804 | del self.invocation_sp_stack[invocation_sp_top:] |
paul@145 | 805 | # Reduce the PC stack to discard all superfluous return addresses. |
paul@398 | 806 | del self.pc_stack[pc_top:] |
paul@398 | 807 | # Remove elements from the handler stacks. |
paul@398 | 808 | del self.handler_pc_stack[-nframes:] |
paul@398 | 809 | del self.handler_local_sp_stack[-nframes:] |
paul@398 | 810 | del self.handler_invocation_sp_stack[-nframes:] |
paul@398 | 811 | del self.handler_stack[-nframes:] |
paul@129 | 812 | |
paul@450 | 813 | def CheckException(self, operand, instruction): |
paul@450 | 814 | self.registers[instruction.target] = self.registers["exception"] is not None and \ |
paul@450 | 815 | self._CheckInstance(self.registers["exception"], self.registers[instruction.working]) |
paul@129 | 816 | |
paul@450 | 817 | def TestIdentity(self, operand, instruction): |
paul@450 | 818 | self.registers[instruction.target] = self.registers[instruction.working] == self.registers[instruction.source] |
paul@129 | 819 | |
paul@450 | 820 | def TestIdentityAddress(self, addr, instruction): |
paul@450 | 821 | self.registers[instruction.target] = self.registers[instruction.working] == addr |
paul@68 | 822 | |
paul@450 | 823 | def InvertBoolean(self, operand, instruction): |
paul@450 | 824 | self.registers[instruction.target] = not self.registers[instruction.source] |
paul@132 | 825 | |
paul@145 | 826 | # Common implementation details. |
paul@145 | 827 | |
paul@145 | 828 | def _CheckInstance(self, ref, cls): |
paul@182 | 829 | data = self.load(ref) |
paul@182 | 830 | target_data = self.load(cls) |
paul@145 | 831 | |
paul@223 | 832 | # Insist on instance vs. class. |
paul@223 | 833 | |
paul@242 | 834 | if data.attrcode is None: # absent attrcode == class/module |
paul@223 | 835 | return 0 |
paul@191 | 836 | |
paul@207 | 837 | if target_data.attrcode is not None: # present attrcode == instance |
paul@191 | 838 | return 0 |
paul@191 | 839 | |
paul@145 | 840 | # Find the table entry for the descendant. |
paul@145 | 841 | |
paul@460 | 842 | element = self.load(self.registers["objlist"] + target_data.classcode + data.attrcode) |
paul@238 | 843 | |
paul@220 | 844 | if element is not None: |
paul@242 | 845 | attr_index, static_attr, offset = element |
paul@220 | 846 | return attr_index == data.attrcode |
paul@220 | 847 | else: |
paul@220 | 848 | return 0 |
paul@181 | 849 | |
paul@181 | 850 | def _MakeObject(self, size, ref): |
paul@203 | 851 | # Load the template. |
paul@182 | 852 | data = self.load(ref) |
paul@181 | 853 | addr = self.new(size) |
paul@203 | 854 | # Save the header, overriding the size. |
paul@203 | 855 | self.save(addr, data.with_size(size)) |
paul@181 | 856 | return addr |
paul@181 | 857 | |
paul@336 | 858 | def _MakeFragment(self, occupied, size): |
paul@336 | 859 | addr = self.new(size) |
paul@246 | 860 | # Save the header, overriding the size. |
paul@336 | 861 | self.save(addr, FragmentObject(occupied, size)) |
paul@246 | 862 | return addr |
paul@246 | 863 | |
paul@429 | 864 | def _LoadAddressContextCond(self, context, value, inst_value): |
paul@191 | 865 | # Check the instance context against the target's context. |
paul@230 | 866 | # This provides the context overriding for methods. |
paul@429 | 867 | if context is ReplaceableContext or context is not PlaceholderContext and self._CheckInstance(inst_value, context): |
paul@191 | 868 | # Replace the context with the instance. |
paul@429 | 869 | return DataValue(inst_value, value) |
paul@191 | 870 | else: |
paul@429 | 871 | return DataValue(context, value) |
paul@191 | 872 | |
paul@429 | 873 | def _StoreAddressContext(self, location, context, value, source_context, source_value): |
paul@429 | 874 | if source_context is ReplaceableContext: |
paul@429 | 875 | context = value |
paul@405 | 876 | else: |
paul@429 | 877 | context = source_context |
paul@429 | 878 | self.save(location, DataValue(context, source_value)) |
paul@405 | 879 | |
paul@228 | 880 | # Convenience functions. |
paul@228 | 881 | |
paul@246 | 882 | def machine(program, with_builtins=0, debug=0, abort_upon_exception=0): |
paul@228 | 883 | print "Making the image..." |
paul@228 | 884 | code = program.get_image(with_builtins) |
paul@228 | 885 | print "Getting raw structures..." |
paul@228 | 886 | ot = program.get_object_table() |
paul@228 | 887 | pt = program.get_parameter_table() |
paul@228 | 888 | objlist = ot.as_list() |
paul@228 | 889 | paramlist = pt.as_list() |
paul@228 | 890 | print "Getting raw image..." |
paul@228 | 891 | rc = program.get_raw_image() |
paul@228 | 892 | print "Initialising the machine..." |
paul@228 | 893 | importer = program.get_importer() |
paul@354 | 894 | constants = {} |
paul@354 | 895 | for x in (True, False, NotImplemented): |
paul@354 | 896 | constants[x] = importer.get_constant(x).location |
paul@248 | 897 | rm = RSVPMachine(rc, objlist, paramlist, debug=debug, abort_upon_exception=abort_upon_exception) |
paul@354 | 898 | library = Library(rm, constants) |
paul@248 | 899 | rm.library = library |
paul@228 | 900 | rm.pc = program.code_location |
paul@241 | 901 | print "Returning program occupying %d locations." % len(rm.memory) |
paul@228 | 902 | return rm |
paul@228 | 903 | |
paul@68 | 904 | # vim: tabstop=4 expandtab shiftwidth=4 |