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