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