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