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