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