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@182 | 55 | from micropython.common import DataObject # for creating "nice" new objects |
paul@182 | 56 | |
paul@68 | 57 | class IllegalInstruction(Exception): |
paul@68 | 58 | pass |
paul@68 | 59 | |
paul@68 | 60 | class IllegalAddress(Exception): |
paul@180 | 61 | def __init__(self, address): |
paul@180 | 62 | self.address = address |
paul@180 | 63 | def __repr__(self): |
paul@180 | 64 | return "IllegalAddress(%r)" % self.address |
paul@180 | 65 | def __str__(self): |
paul@180 | 66 | return repr(self) |
paul@68 | 67 | |
paul@68 | 68 | class EmptyPCStack(Exception): |
paul@68 | 69 | pass |
paul@68 | 70 | |
paul@92 | 71 | class EmptyFrameStack(Exception): |
paul@68 | 72 | pass |
paul@68 | 73 | |
paul@194 | 74 | class BreakpointReached(Exception): |
paul@194 | 75 | pass |
paul@194 | 76 | |
paul@68 | 77 | class RSVPMachine: |
paul@68 | 78 | |
paul@68 | 79 | "A really simple virtual processor." |
paul@68 | 80 | |
paul@196 | 81 | def __init__(self, memory, objlist, paramlist, true_constant, false_constant, pc=None, debug=0): |
paul@68 | 82 | |
paul@68 | 83 | """ |
paul@68 | 84 | Initialise the processor with a 'memory' (a list of values containing |
paul@196 | 85 | instructions and data), the object and parameter lists 'objlist' and |
paul@196 | 86 | 'paramlist', the addresses 'true_constant' and 'false_constant', and the |
paul@196 | 87 | optional program counter 'pc'. |
paul@68 | 88 | """ |
paul@68 | 89 | |
paul@68 | 90 | self.memory = memory |
paul@181 | 91 | self.objlist = objlist.as_raw() |
paul@181 | 92 | self.paramlist = paramlist.as_raw() |
paul@196 | 93 | self.true_constant = true_constant |
paul@196 | 94 | self.false_constant = false_constant |
paul@196 | 95 | |
paul@68 | 96 | self.pc = pc or 0 |
paul@68 | 97 | self.debug = debug |
paul@117 | 98 | |
paul@117 | 99 | # Stacks. |
paul@117 | 100 | |
paul@68 | 101 | self.pc_stack = [] |
paul@68 | 102 | self.frame_stack = [] |
paul@143 | 103 | self.local_sp_stack = [0] |
paul@117 | 104 | self.invocation_sp_stack = [] |
paul@145 | 105 | self.handler_stack = [len(self.memory) - 1] # final handler is the end of the code |
paul@145 | 106 | self.handler_local_sp_stack = [] |
paul@145 | 107 | self.handler_pc_stack = [] |
paul@117 | 108 | |
paul@117 | 109 | # Registers. |
paul@117 | 110 | |
paul@119 | 111 | self.instruction = None |
paul@119 | 112 | self.operand = None |
paul@117 | 113 | self.value = None |
paul@117 | 114 | self.status = None |
paul@117 | 115 | self.source = None |
paul@132 | 116 | self.callable = None |
paul@117 | 117 | self.result = None |
paul@117 | 118 | self.exception = None |
paul@68 | 119 | |
paul@146 | 120 | # Constants. |
paul@146 | 121 | |
paul@192 | 122 | self.attr_error = objlist.access("__builtins__", "AttributeError").get_value().location |
paul@192 | 123 | self.type_error = objlist.access("__builtins__", "TypeError").get_value().location |
paul@181 | 124 | |
paul@181 | 125 | # Native class constants. |
paul@181 | 126 | |
paul@182 | 127 | cls = objlist.access("__builtins__", "int") |
paul@196 | 128 | self.int_class_location = cls and cls.get_value() and cls.get_value().location |
paul@192 | 129 | self.int_instance_location = cls and cls.get_value() and cls.get_value().instance_template_location |
paul@146 | 130 | |
paul@194 | 131 | # Debugging attributes. |
paul@194 | 132 | |
paul@194 | 133 | self.breakpoints = set() |
paul@194 | 134 | |
paul@162 | 135 | # Debugging methods. |
paul@162 | 136 | |
paul@137 | 137 | def dump(self): |
paul@138 | 138 | print "PC", self.pc, "->", self.load(self.pc) |
paul@137 | 139 | print "PC stack", self.pc_stack |
paul@137 | 140 | print "Frame stack", self.frame_stack |
paul@137 | 141 | print "Local stack pointers", self.local_sp_stack |
paul@137 | 142 | print "Invocation stack pointers", self.invocation_sp_stack |
paul@137 | 143 | print "Handler stack", self.handler_stack |
paul@145 | 144 | print "Handler frame stack", self.handler_local_sp_stack |
paul@145 | 145 | print "Handler PC stack", self.handler_pc_stack |
paul@137 | 146 | print |
paul@137 | 147 | print "Instruction", self.instruction |
paul@137 | 148 | print "Operand", self.operand |
paul@137 | 149 | print "Value", self.value |
paul@137 | 150 | print "Status", self.status |
paul@137 | 151 | print "Source", self.source |
paul@137 | 152 | print "Callable", self.callable |
paul@137 | 153 | print "Result", self.result |
paul@137 | 154 | print "Exception", self.exception |
paul@137 | 155 | |
paul@162 | 156 | def show(self): |
paul@162 | 157 | self.show_memory(self.memory, 0) |
paul@162 | 158 | |
paul@162 | 159 | def show_pc(self, run_in=10): |
paul@162 | 160 | start = max(0, self.pc - run_in) |
paul@162 | 161 | end = self.pc + run_in |
paul@162 | 162 | memory = self.memory[start:end] |
paul@162 | 163 | self.show_memory(memory, start) |
paul@162 | 164 | |
paul@162 | 165 | def show_memory(self, memory, start): |
paul@162 | 166 | for i, x in enumerate(memory): |
paul@162 | 167 | location = start + i |
paul@162 | 168 | if location == self.pc: |
paul@162 | 169 | print "->", |
paul@162 | 170 | else: |
paul@162 | 171 | print " ", |
paul@162 | 172 | print "%5d %r" % (location, x) |
paul@162 | 173 | |
paul@162 | 174 | def step(self, dump=0): |
paul@138 | 175 | self.execute() |
paul@162 | 176 | self.show_pc() |
paul@162 | 177 | if dump: |
paul@162 | 178 | self.dump() |
paul@162 | 179 | |
paul@194 | 180 | def set_break(self, location): |
paul@194 | 181 | self.breakpoints.add(location) |
paul@194 | 182 | |
paul@162 | 183 | # Internal operations. |
paul@138 | 184 | |
paul@68 | 185 | def load(self, address): |
paul@68 | 186 | |
paul@68 | 187 | "Return the value at the given 'address'." |
paul@68 | 188 | |
paul@68 | 189 | try: |
paul@68 | 190 | return self.memory[address] |
paul@68 | 191 | except IndexError: |
paul@180 | 192 | raise IllegalAddress(address) |
paul@180 | 193 | except TypeError: |
paul@180 | 194 | raise IllegalAddress(address) |
paul@68 | 195 | |
paul@68 | 196 | def save(self, address, value): |
paul@68 | 197 | |
paul@68 | 198 | "Save to the given 'address' the specified 'value'." |
paul@68 | 199 | |
paul@68 | 200 | try: |
paul@68 | 201 | self.memory[address] = value |
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 new(self, size): |
paul@68 | 208 | |
paul@68 | 209 | """ |
paul@68 | 210 | Allocate space of the given 'size', returning the address of the space. |
paul@68 | 211 | """ |
paul@68 | 212 | |
paul@68 | 213 | addr = len(self.memory) |
paul@68 | 214 | for i in range(0, size): |
paul@68 | 215 | self.memory.append(None) |
paul@68 | 216 | return addr |
paul@68 | 217 | |
paul@68 | 218 | def push_pc(self, data): |
paul@68 | 219 | |
paul@68 | 220 | "Push 'data' onto the PC stack." |
paul@68 | 221 | |
paul@68 | 222 | self.pc_stack.append(data) |
paul@68 | 223 | |
paul@68 | 224 | def pull_pc(self): |
paul@68 | 225 | |
paul@68 | 226 | "Pull a value from the PC stack and return it." |
paul@68 | 227 | |
paul@68 | 228 | try: |
paul@68 | 229 | return self.pc_stack.pop() |
paul@68 | 230 | except IndexError: |
paul@68 | 231 | raise EmptyPCStack |
paul@68 | 232 | |
paul@90 | 233 | def run(self): |
paul@68 | 234 | |
paul@68 | 235 | "Execute code in the memory, starting from the current PC address." |
paul@68 | 236 | |
paul@68 | 237 | try: |
paul@68 | 238 | while 1: |
paul@90 | 239 | self.execute() |
paul@68 | 240 | except EmptyPCStack: |
paul@68 | 241 | pass |
paul@68 | 242 | |
paul@90 | 243 | def execute(self): |
paul@90 | 244 | |
paul@90 | 245 | "Execute code in the memory at the current PC address." |
paul@90 | 246 | |
paul@194 | 247 | if self.pc in self.breakpoints: |
paul@194 | 248 | self.breakpoints.remove(self.pc) |
paul@194 | 249 | raise BreakpointReached |
paul@194 | 250 | |
paul@119 | 251 | self.instruction = self.load(self.pc) |
paul@118 | 252 | |
paul@118 | 253 | # Process any inputs of the instruction. |
paul@118 | 254 | |
paul@119 | 255 | self.process_inputs() |
paul@137 | 256 | |
paul@137 | 257 | # Perform the instruction itself. |
paul@137 | 258 | |
paul@137 | 259 | next_pc = self.perform(self.instruction) |
paul@118 | 260 | |
paul@119 | 261 | # Update the program counter. |
paul@118 | 262 | |
paul@119 | 263 | if next_pc is None: |
paul@119 | 264 | self.pc += 1 |
paul@119 | 265 | else: |
paul@119 | 266 | self.pc = next_pc |
paul@118 | 267 | |
paul@137 | 268 | def get_method(self, instruction): |
paul@137 | 269 | |
paul@137 | 270 | "Return the handler method for the given 'instruction'." |
paul@119 | 271 | |
paul@137 | 272 | instruction_name = instruction.__class__.__name__ |
paul@137 | 273 | if self.debug: |
paul@137 | 274 | print "%8d %s" % (self.pc, instruction_name) |
paul@119 | 275 | method = getattr(self, instruction_name, None) |
paul@90 | 276 | if method is None: |
paul@119 | 277 | raise IllegalInstruction, (self.pc, instruction_name) |
paul@118 | 278 | return method |
paul@118 | 279 | |
paul@137 | 280 | def perform(self, instruction): |
paul@137 | 281 | |
paul@137 | 282 | "Perform the 'instruction', returning the next PC value or None." |
paul@137 | 283 | |
paul@137 | 284 | self.operand = instruction.get_operand() |
paul@137 | 285 | method = self.get_method(instruction) |
paul@137 | 286 | return method() |
paul@137 | 287 | |
paul@119 | 288 | def process_inputs(self): |
paul@118 | 289 | |
paul@118 | 290 | """ |
paul@119 | 291 | Process any inputs of the current instruction. This permits any directly |
paul@118 | 292 | connected sub-instructions to produce the effects that separate |
paul@118 | 293 | instructions would otherwise have. |
paul@118 | 294 | """ |
paul@118 | 295 | |
paul@190 | 296 | value = self.value |
paul@174 | 297 | if self.instruction.source is not None: |
paul@174 | 298 | self.perform(self.instruction.source) |
paul@174 | 299 | self.source = self.value |
paul@190 | 300 | self.value = value |
paul@174 | 301 | if self.instruction.input is not None: |
paul@174 | 302 | self.perform(self.instruction.input) |
paul@90 | 303 | |
paul@68 | 304 | def jump(self, addr, next): |
paul@68 | 305 | |
paul@68 | 306 | """ |
paul@68 | 307 | Jump to the subroutine at (or identified by) 'addr'. If 'addr' |
paul@68 | 308 | identifies a library function then invoke the library function and set |
paul@68 | 309 | PC to 'next' afterwards; otherwise, set PC to 'addr'. |
paul@68 | 310 | """ |
paul@68 | 311 | |
paul@181 | 312 | # Trap library functions introduced through the use of strings instead |
paul@181 | 313 | # of proper locations. |
paul@181 | 314 | |
paul@68 | 315 | if isinstance(addr, str): |
paul@196 | 316 | handler = self.native_functions[addr](self) |
paul@196 | 317 | if handler is None: |
paul@196 | 318 | return next |
paul@196 | 319 | else: |
paul@196 | 320 | return handler |
paul@68 | 321 | else: |
paul@138 | 322 | self.push_pc(self.pc + 1) |
paul@119 | 323 | return addr |
paul@68 | 324 | |
paul@68 | 325 | # Instructions. |
paul@68 | 326 | |
paul@98 | 327 | def LoadConst(self): |
paul@198 | 328 | self.value = None, self.operand # context of constant is not interesting |
paul@68 | 329 | |
paul@68 | 330 | def LoadName(self): |
paul@117 | 331 | frame = self.local_sp_stack[-1] |
paul@119 | 332 | self.value = self.frame_stack[frame + self.operand] |
paul@68 | 333 | |
paul@68 | 334 | def StoreName(self): |
paul@117 | 335 | frame = self.local_sp_stack[-1] |
paul@119 | 336 | self.frame_stack[frame + self.operand] = self.value |
paul@68 | 337 | |
paul@72 | 338 | LoadTemp = LoadName |
paul@72 | 339 | StoreTemp = StoreName |
paul@72 | 340 | |
paul@72 | 341 | def LoadAddress(self): |
paul@119 | 342 | # Preserve context (potentially null). |
paul@119 | 343 | self.value = self.load(self.operand) |
paul@72 | 344 | |
paul@117 | 345 | def LoadAddressContext(self): |
paul@191 | 346 | context, ref = self.load(self.operand) |
paul@191 | 347 | inst_context, inst_ref = self.value |
paul@194 | 348 | self.value = inst_ref, ref |
paul@194 | 349 | |
paul@194 | 350 | def LoadAddressContextCond(self): |
paul@194 | 351 | context, ref = self.load(self.operand) |
paul@194 | 352 | inst_context, inst_ref = self.value |
paul@194 | 353 | self.value = self._LoadAddressContextCond(context, ref, inst_context, inst_ref) |
paul@72 | 354 | |
paul@72 | 355 | def StoreAddress(self): |
paul@119 | 356 | # Preserve context. |
paul@190 | 357 | self.save(self.operand, self.source) |
paul@72 | 358 | |
paul@117 | 359 | def MakeObject(self): |
paul@138 | 360 | size = self.operand |
paul@138 | 361 | context, ref = self.value |
paul@184 | 362 | # NOTE: Referencing the instance template. |
paul@184 | 363 | addr = self._MakeObject(size, ref - 1) |
paul@184 | 364 | # Introduce object as context for the new object. |
paul@185 | 365 | self.value = addr, addr |
paul@72 | 366 | |
paul@118 | 367 | def LoadAttr(self): |
paul@118 | 368 | context, ref = self.value |
paul@127 | 369 | # Retrieved context should already be appropriate for the instance. |
paul@184 | 370 | # NOTE: Adding 1 to skip any header. |
paul@174 | 371 | self.value = self.load(ref + self.operand + 1) |
paul@118 | 372 | |
paul@118 | 373 | def StoreAttr(self): |
paul@119 | 374 | context, ref = self.value |
paul@129 | 375 | # Target should already be an instance. |
paul@184 | 376 | # NOTE: Adding 1 to skip any header. |
paul@174 | 377 | self.save(ref + self.operand + 1, self.source) |
paul@119 | 378 | |
paul@119 | 379 | def LoadAttrIndex(self): |
paul@118 | 380 | context, ref = self.value |
paul@182 | 381 | data = self.load(ref) |
paul@182 | 382 | element = self.objlist[data.classcode + self.operand] |
paul@194 | 383 | attr_index, class_attr, offset = element |
paul@135 | 384 | if attr_index == self.operand: |
paul@127 | 385 | if class_attr: |
paul@194 | 386 | self.value = self.load(offset) # offset is address of class attribute |
paul@127 | 387 | else: |
paul@127 | 388 | self.value = self.load(ref + offset) |
paul@119 | 389 | else: |
paul@146 | 390 | self.exception = self.attr_error |
paul@190 | 391 | return self.RaiseException() |
paul@118 | 392 | |
paul@194 | 393 | def LoadAttrIndexContext(self): |
paul@194 | 394 | context, ref = self.value |
paul@194 | 395 | data = self.load(ref) |
paul@194 | 396 | element = self.objlist[data.classcode + self.operand] |
paul@194 | 397 | attr_index, class_attr, offset = element |
paul@194 | 398 | if attr_index == self.operand: |
paul@194 | 399 | loaded_context, loaded_ref = self.load(offset) # offset is address of class attribute |
paul@194 | 400 | self.value = ref, loaded_ref |
paul@194 | 401 | else: |
paul@194 | 402 | self.exception = self.attr_error |
paul@194 | 403 | return self.RaiseException() |
paul@194 | 404 | |
paul@194 | 405 | def LoadAttrIndexContextCond(self): |
paul@194 | 406 | context, ref = self.value |
paul@194 | 407 | data = self.load(ref) |
paul@194 | 408 | element = self.objlist[data.classcode + self.operand] |
paul@194 | 409 | attr_index, class_attr, offset = element |
paul@194 | 410 | if attr_index == self.operand: |
paul@194 | 411 | if class_attr: |
paul@194 | 412 | loaded_context, loaded_ref = self.load(offset) # offset is address of class attribute |
paul@194 | 413 | self.value = self._LoadAddressContextCond(loaded_context, loaded_ref, context, ref) |
paul@194 | 414 | else: |
paul@194 | 415 | self.value = self.load(ref + offset) |
paul@194 | 416 | else: |
paul@194 | 417 | self.exception = self.attr_error |
paul@194 | 418 | return self.RaiseException() |
paul@194 | 419 | |
paul@194 | 420 | # NOTE: LoadAttrIndexContextCond will test context compatibility. |
paul@194 | 421 | |
paul@129 | 422 | def StoreAttrIndex(self): |
paul@129 | 423 | context, ref = self.value |
paul@182 | 424 | data = self.load(ref) |
paul@182 | 425 | element = self.objlist[data.classcode + self.operand] |
paul@194 | 426 | attr_index, class_attr, offset = element |
paul@135 | 427 | if attr_index == self.operand: |
paul@129 | 428 | if class_attr: |
paul@146 | 429 | self.exception = self.type_error |
paul@190 | 430 | return self.RaiseException() |
paul@129 | 431 | else: |
paul@129 | 432 | self.save(ref + offset, self.source) |
paul@129 | 433 | else: |
paul@146 | 434 | self.exception = self.attr_error |
paul@190 | 435 | return self.RaiseException() |
paul@118 | 436 | |
paul@127 | 437 | # NOTE: LoadAttrIndexContext is a possibility if a particular attribute can always be overridden. |
paul@127 | 438 | |
paul@98 | 439 | def MakeFrame(self): |
paul@117 | 440 | self.invocation_sp_stack.append(len(self.frame_stack)) |
paul@119 | 441 | self.frame_stack.extend([None] * self.operand) |
paul@98 | 442 | |
paul@98 | 443 | def DropFrame(self): |
paul@117 | 444 | self.local_sp_stack.pop() |
paul@117 | 445 | frame = self.invocation_sp_stack.pop() |
paul@117 | 446 | self.frame_stack = self.frame_stack[:frame] # reset stack before call |
paul@98 | 447 | |
paul@137 | 448 | def RecoverFrame(self): |
paul@137 | 449 | self.local_sp_stack.pop() |
paul@137 | 450 | |
paul@129 | 451 | def StoreFrame(self): |
paul@129 | 452 | frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame |
paul@129 | 453 | self.frame_stack[frame + self.operand] = self.value |
paul@129 | 454 | |
paul@129 | 455 | def StoreFrameIndex(self): |
paul@184 | 456 | context, ref = self.value |
paul@129 | 457 | frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame |
paul@182 | 458 | data = self.load(ref) |
paul@190 | 459 | element = self.paramlist[data.funccode + self.operand] |
paul@190 | 460 | # NOTE: Need to ensure correct positioning where a context has been generated. |
paul@190 | 461 | param_index, offset = element |
paul@190 | 462 | if param_index == self.operand: |
paul@190 | 463 | self.frame_stack[frame + offset + 1] = self.source # add 1 to skip the context always generated |
paul@129 | 464 | else: |
paul@146 | 465 | self.exception = self.type_error |
paul@190 | 466 | return self.RaiseException() |
paul@129 | 467 | |
paul@132 | 468 | def LoadCallable(self): |
paul@132 | 469 | context, ref = self.value |
paul@182 | 470 | data = self.load(ref) |
paul@182 | 471 | self.callable = data.codeaddr, data.codedetails |
paul@132 | 472 | |
paul@132 | 473 | def StoreCallable(self): |
paul@132 | 474 | context, ref = self.value |
paul@134 | 475 | # NOTE: Should improve the representation and permit direct saving. |
paul@182 | 476 | data = self.load(ref) |
paul@182 | 477 | self.save(ref, (data.classcode, data.attrcode) + self.callable + (data.instance,)) |
paul@132 | 478 | |
paul@132 | 479 | def LoadContext(self): |
paul@132 | 480 | context, ref = self.value |
paul@198 | 481 | self.value = None, context # context of context is not interesting |
paul@132 | 482 | |
paul@132 | 483 | def CheckFrame(self): |
paul@134 | 484 | operand = self.operand |
paul@134 | 485 | frame = self.invocation_sp_stack[-1] |
paul@132 | 486 | context, ref = self.value |
paul@182 | 487 | data = self.load(ref) |
paul@134 | 488 | |
paul@134 | 489 | # Support sliding of the frame to exclude any inappropriate context. |
paul@134 | 490 | |
paul@134 | 491 | if context is None: |
paul@138 | 492 | self.invocation_sp_stack[-1] += 1 |
paul@134 | 493 | operand -= 1 |
paul@134 | 494 | else: |
paul@182 | 495 | context_data = self.load(context) |
paul@182 | 496 | if not context_data.instance: |
paul@138 | 497 | self.invocation_sp_stack[-1] += 1 |
paul@134 | 498 | operand -= 1 |
paul@134 | 499 | |
paul@190 | 500 | # Test the frame size. |
paul@190 | 501 | |
paul@182 | 502 | nargs, ndefaults = data.codedetails |
paul@184 | 503 | if not ((nargs - ndefaults) <= operand <= nargs): |
paul@184 | 504 | raise Exception, "CheckFrame %r (%r <= %r <= %r)" % (self.operand, nargs - ndefaults, operand, nargs) |
paul@134 | 505 | |
paul@190 | 506 | # Support population of defaults. |
paul@190 | 507 | # This involves copying the "attributes" of a function into the frame. |
paul@190 | 508 | |
paul@190 | 509 | default = operand - (nargs - ndefaults) |
paul@190 | 510 | self.frame_stack.extend([None] * (nargs - operand)) |
paul@190 | 511 | pos = self.operand |
paul@190 | 512 | |
paul@190 | 513 | while operand < nargs: |
paul@190 | 514 | self.frame_stack[frame + pos] = self.load(ref + default + 1) # skip header |
paul@190 | 515 | default += 1 |
paul@190 | 516 | pos += 1 |
paul@190 | 517 | operand += 1 |
paul@117 | 518 | |
paul@134 | 519 | def CheckSelf(self): |
paul@134 | 520 | context, ref = self.value |
paul@134 | 521 | target_context, target_ref = self.source |
paul@135 | 522 | |
paul@145 | 523 | # Check the details of the proposed context and the target's context. |
paul@135 | 524 | |
paul@181 | 525 | self.status = self._CheckInstance(ref, target_context) |
paul@124 | 526 | |
paul@132 | 527 | def JumpWithFrame(self): |
paul@138 | 528 | codeaddr, codedetails = self.callable |
paul@132 | 529 | self.local_sp_stack.append(self.invocation_sp_stack[-1]) # adopt the invocation frame |
paul@138 | 530 | return self.jump(codeaddr, self.pc + 1) # return to the instruction after this one |
paul@98 | 531 | |
paul@132 | 532 | def ExtendFrame(self): |
paul@138 | 533 | self.frame_stack.extend([None] * self.operand) |
paul@98 | 534 | |
paul@137 | 535 | def AdjustFrame(self): |
paul@137 | 536 | if self.operand > 0: |
paul@138 | 537 | self.frame_stack.append([None] * self.operand) |
paul@137 | 538 | elif self.operand == -1: |
paul@137 | 539 | self.invocation_sp_stack[-1] -= 1 |
paul@137 | 540 | else: |
paul@137 | 541 | raise Exception, "AdjustFrame %r" % self.operand |
paul@137 | 542 | |
paul@68 | 543 | def Return(self): |
paul@138 | 544 | return self.pull_pc() |
paul@68 | 545 | |
paul@117 | 546 | def LoadResult(self): |
paul@117 | 547 | self.value = self.result |
paul@117 | 548 | |
paul@117 | 549 | def StoreResult(self): |
paul@117 | 550 | self.result = self.value |
paul@117 | 551 | |
paul@118 | 552 | def Jump(self): |
paul@119 | 553 | return self.operand |
paul@98 | 554 | |
paul@68 | 555 | def JumpIfTrue(self): |
paul@117 | 556 | if self.status: |
paul@119 | 557 | return self.operand |
paul@68 | 558 | |
paul@68 | 559 | def JumpIfFalse(self): |
paul@117 | 560 | if not self.status: |
paul@119 | 561 | return self.operand |
paul@68 | 562 | |
paul@129 | 563 | def LoadException(self): |
paul@197 | 564 | self.value = self.exception, self.exception |
paul@129 | 565 | |
paul@129 | 566 | def StoreException(self): |
paul@145 | 567 | self.exception = self.value[1] |
paul@129 | 568 | |
paul@129 | 569 | def RaiseException(self): |
paul@145 | 570 | return self.handler_stack[-1] |
paul@129 | 571 | |
paul@129 | 572 | def PushHandler(self): |
paul@129 | 573 | self.handler_stack.append(self.operand) |
paul@145 | 574 | self.handler_local_sp_stack.append(len(self.local_sp_stack)) |
paul@145 | 575 | self.handler_pc_stack.append(len(self.pc_stack)) |
paul@129 | 576 | |
paul@129 | 577 | def PopHandler(self): |
paul@145 | 578 | # Reduce the local frame pointer stack to refer to the handler's frame. |
paul@145 | 579 | self.local_sp_stack = self.local_sp_stack[:self.handler_local_sp_stack.pop()] |
paul@145 | 580 | # Reduce the PC stack to discard all superfluous return addresses. |
paul@145 | 581 | self.pc_stack = self.pc_stack[:self.handler_pc_stack.pop()] |
paul@129 | 582 | self.handler_stack.pop() |
paul@129 | 583 | |
paul@129 | 584 | def CheckException(self): |
paul@181 | 585 | self.status = self.exception is not None and self._CheckInstance(self.exception, self.value[1]) |
paul@129 | 586 | |
paul@129 | 587 | def TestIdentity(self): |
paul@145 | 588 | self.status = self.value[1] == self.source[1] |
paul@129 | 589 | |
paul@129 | 590 | def TestIdentityAddress(self): |
paul@129 | 591 | self.status = self.value[1] == self.operand |
paul@129 | 592 | |
paul@129 | 593 | # LoadBoolean is implemented in the generated code. |
paul@129 | 594 | # StoreBoolean is implemented by testing against the True value. |
paul@68 | 595 | |
paul@132 | 596 | def InvertBoolean(self): |
paul@132 | 597 | self.status = not self.status |
paul@132 | 598 | |
paul@145 | 599 | # Common implementation details. |
paul@145 | 600 | |
paul@145 | 601 | def _CheckInstance(self, ref, cls): |
paul@182 | 602 | data = self.load(ref) |
paul@182 | 603 | target_data = self.load(cls) |
paul@145 | 604 | |
paul@191 | 605 | # Insist on a class. |
paul@191 | 606 | |
paul@191 | 607 | if target_data.instance: |
paul@191 | 608 | return 0 |
paul@191 | 609 | |
paul@145 | 610 | # Find the table entry for the descendant. |
paul@145 | 611 | |
paul@182 | 612 | element = self.objlist[target_data.classcode + data.attrcode] |
paul@194 | 613 | attr_index, class_attr, offset = element |
paul@182 | 614 | return attr_index == data.attrcode |
paul@181 | 615 | |
paul@181 | 616 | def _MakeObject(self, size, ref): |
paul@182 | 617 | data = self.load(ref) |
paul@181 | 618 | addr = self.new(size) |
paul@181 | 619 | # Set the header to resemble the class. |
paul@184 | 620 | self.save(addr, data) |
paul@181 | 621 | return addr |
paul@181 | 622 | |
paul@194 | 623 | def _LoadAddressContextCond(self, context, ref, inst_context, inst_ref): |
paul@191 | 624 | |
paul@191 | 625 | # Check the instance context against the target's context. |
paul@191 | 626 | |
paul@191 | 627 | if self._CheckInstance(inst_ref, context): |
paul@191 | 628 | # Replace the context with the instance. |
paul@191 | 629 | return inst_ref, ref |
paul@191 | 630 | else: |
paul@191 | 631 | return context, ref |
paul@191 | 632 | |
paul@181 | 633 | # Native function implementations. |
paul@181 | 634 | |
paul@181 | 635 | def builtins_object_init(self): |
paul@181 | 636 | pass |
paul@181 | 637 | |
paul@181 | 638 | def builtins_int_init(self): |
paul@181 | 639 | pass |
paul@181 | 640 | |
paul@181 | 641 | def builtins_int_add(self): |
paul@181 | 642 | frame = self.local_sp_stack[-1] |
paul@181 | 643 | |
paul@181 | 644 | # Get operands addresses. |
paul@181 | 645 | |
paul@181 | 646 | left_context, left = self.frame_stack[frame] |
paul@181 | 647 | right_context, right = self.frame_stack[frame + 1] |
paul@181 | 648 | |
paul@181 | 649 | # Test operand suitability. |
paul@181 | 650 | |
paul@196 | 651 | if not self._CheckInstance(left, self.int_class_location) and self._CheckInstance(right, self.int_class_location): |
paul@181 | 652 | self.exception = self.type_error |
paul@181 | 653 | return self.RaiseException() |
paul@181 | 654 | |
paul@181 | 655 | # NOTE: Assume single location for data. |
paul@181 | 656 | |
paul@181 | 657 | left_data = left + 1 |
paul@181 | 658 | right_data = right + 1 |
paul@181 | 659 | |
paul@181 | 660 | # Make a new object. |
paul@181 | 661 | |
paul@184 | 662 | addr = self._MakeObject(2, self.int_instance_location) |
paul@196 | 663 | |
paul@196 | 664 | # Store the result. |
paul@196 | 665 | # NOTE: The data is considered ready to use. |
paul@196 | 666 | |
paul@181 | 667 | self.save(addr + 1, self.load(left_data) + self.load(right_data)) |
paul@181 | 668 | |
paul@187 | 669 | # Return the new object. |
paul@187 | 670 | # Introduce object as context for the new object. |
paul@181 | 671 | |
paul@187 | 672 | self.result = addr, addr |
paul@181 | 673 | |
paul@196 | 674 | def builtins_int_bool(self): |
paul@196 | 675 | frame = self.local_sp_stack[-1] |
paul@196 | 676 | |
paul@196 | 677 | # Get operands addresses. |
paul@196 | 678 | |
paul@196 | 679 | left_context, left = self.frame_stack[frame] |
paul@196 | 680 | |
paul@196 | 681 | # Test operand suitability. |
paul@196 | 682 | |
paul@196 | 683 | if not self._CheckInstance(left, self.int_class_location): |
paul@196 | 684 | self.exception = self.type_error |
paul@196 | 685 | return self.RaiseException() |
paul@196 | 686 | |
paul@196 | 687 | # NOTE: Assume single location for data. |
paul@196 | 688 | |
paul@196 | 689 | left_data = left + 1 |
paul@196 | 690 | |
paul@196 | 691 | # Test the data. |
paul@196 | 692 | # NOTE: The data is considered ready to use. |
paul@196 | 693 | |
paul@196 | 694 | if self.load(left_data) != 0: |
paul@196 | 695 | self.result = self.true_constant, self.true_constant |
paul@196 | 696 | else: |
paul@196 | 697 | self.result = self.false_constant, self.false_constant |
paul@196 | 698 | |
paul@197 | 699 | def builtins_bool_bool(self): |
paul@197 | 700 | frame = self.local_sp_stack[-1] |
paul@197 | 701 | |
paul@197 | 702 | # Get operands addresses. |
paul@197 | 703 | |
paul@197 | 704 | left_context, left = self.frame_stack[frame] |
paul@197 | 705 | self.result = left, left |
paul@197 | 706 | |
paul@181 | 707 | native_functions = { |
paul@181 | 708 | "__builtins__.object.__init__" : builtins_object_init, |
paul@181 | 709 | "__builtins__.int.__init__" : builtins_int_init, |
paul@181 | 710 | "__builtins__.int.__add__" : builtins_int_add, |
paul@196 | 711 | "__builtins__.int.__bool__" : builtins_int_bool, |
paul@197 | 712 | "__builtins__.bool.__bool__" : builtins_bool_bool, |
paul@181 | 713 | } |
paul@145 | 714 | |
paul@68 | 715 | # vim: tabstop=4 expandtab shiftwidth=4 |