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