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