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