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@68 | 55 | class IllegalInstruction(Exception): |
paul@68 | 56 | pass |
paul@68 | 57 | |
paul@68 | 58 | class IllegalAddress(Exception): |
paul@180 | 59 | def __init__(self, address): |
paul@180 | 60 | self.address = address |
paul@180 | 61 | def __repr__(self): |
paul@180 | 62 | return "IllegalAddress(%r)" % self.address |
paul@180 | 63 | def __str__(self): |
paul@180 | 64 | return repr(self) |
paul@68 | 65 | |
paul@68 | 66 | class EmptyPCStack(Exception): |
paul@68 | 67 | pass |
paul@68 | 68 | |
paul@92 | 69 | class EmptyFrameStack(Exception): |
paul@68 | 70 | pass |
paul@68 | 71 | |
paul@68 | 72 | class RSVPMachine: |
paul@68 | 73 | |
paul@68 | 74 | "A really simple virtual processor." |
paul@68 | 75 | |
paul@146 | 76 | def __init__(self, memory, objlist, paramlist, attr_error, type_error, pc=None, debug=0): |
paul@68 | 77 | |
paul@68 | 78 | """ |
paul@68 | 79 | Initialise the processor with a 'memory' (a list of values containing |
paul@68 | 80 | instructions and data) and the optional program counter 'pc'. |
paul@68 | 81 | """ |
paul@68 | 82 | |
paul@68 | 83 | self.memory = memory |
paul@138 | 84 | self.objlist = objlist |
paul@138 | 85 | self.paramlist = paramlist |
paul@68 | 86 | self.pc = pc or 0 |
paul@68 | 87 | self.debug = debug |
paul@117 | 88 | |
paul@117 | 89 | # Stacks. |
paul@117 | 90 | |
paul@68 | 91 | self.pc_stack = [] |
paul@68 | 92 | self.frame_stack = [] |
paul@143 | 93 | self.local_sp_stack = [0] |
paul@117 | 94 | self.invocation_sp_stack = [] |
paul@145 | 95 | self.handler_stack = [len(self.memory) - 1] # final handler is the end of the code |
paul@145 | 96 | self.handler_local_sp_stack = [] |
paul@145 | 97 | self.handler_pc_stack = [] |
paul@117 | 98 | |
paul@117 | 99 | # Registers. |
paul@117 | 100 | |
paul@119 | 101 | self.instruction = None |
paul@119 | 102 | self.operand = None |
paul@117 | 103 | self.value = None |
paul@117 | 104 | self.status = None |
paul@117 | 105 | self.source = None |
paul@132 | 106 | self.callable = None |
paul@117 | 107 | self.result = None |
paul@117 | 108 | self.exception = None |
paul@68 | 109 | |
paul@146 | 110 | # Constants. |
paul@146 | 111 | |
paul@146 | 112 | self.attr_error = attr_error |
paul@146 | 113 | self.type_error = type_error |
paul@146 | 114 | |
paul@162 | 115 | # Debugging methods. |
paul@162 | 116 | |
paul@137 | 117 | def dump(self): |
paul@138 | 118 | print "PC", self.pc, "->", self.load(self.pc) |
paul@137 | 119 | print "PC stack", self.pc_stack |
paul@137 | 120 | print "Frame stack", self.frame_stack |
paul@137 | 121 | print "Local stack pointers", self.local_sp_stack |
paul@137 | 122 | print "Invocation stack pointers", self.invocation_sp_stack |
paul@137 | 123 | print "Handler stack", self.handler_stack |
paul@145 | 124 | print "Handler frame stack", self.handler_local_sp_stack |
paul@145 | 125 | print "Handler PC stack", self.handler_pc_stack |
paul@137 | 126 | print |
paul@137 | 127 | print "Instruction", self.instruction |
paul@137 | 128 | print "Operand", self.operand |
paul@137 | 129 | print "Value", self.value |
paul@137 | 130 | print "Status", self.status |
paul@137 | 131 | print "Source", self.source |
paul@137 | 132 | print "Callable", self.callable |
paul@137 | 133 | print "Result", self.result |
paul@137 | 134 | print "Exception", self.exception |
paul@137 | 135 | |
paul@162 | 136 | def show(self): |
paul@162 | 137 | self.show_memory(self.memory, 0) |
paul@162 | 138 | |
paul@162 | 139 | def show_pc(self, run_in=10): |
paul@162 | 140 | start = max(0, self.pc - run_in) |
paul@162 | 141 | end = self.pc + run_in |
paul@162 | 142 | memory = self.memory[start:end] |
paul@162 | 143 | self.show_memory(memory, start) |
paul@162 | 144 | |
paul@162 | 145 | def show_memory(self, memory, start): |
paul@162 | 146 | for i, x in enumerate(memory): |
paul@162 | 147 | location = start + i |
paul@162 | 148 | if location == self.pc: |
paul@162 | 149 | print "->", |
paul@162 | 150 | else: |
paul@162 | 151 | print " ", |
paul@162 | 152 | print "%5d %r" % (location, x) |
paul@162 | 153 | |
paul@162 | 154 | def step(self, dump=0): |
paul@138 | 155 | self.execute() |
paul@162 | 156 | self.show_pc() |
paul@162 | 157 | if dump: |
paul@162 | 158 | self.dump() |
paul@162 | 159 | |
paul@162 | 160 | # Internal operations. |
paul@138 | 161 | |
paul@68 | 162 | def load(self, address): |
paul@68 | 163 | |
paul@68 | 164 | "Return the value at the given 'address'." |
paul@68 | 165 | |
paul@68 | 166 | try: |
paul@68 | 167 | return self.memory[address] |
paul@68 | 168 | except IndexError: |
paul@180 | 169 | raise IllegalAddress(address) |
paul@180 | 170 | except TypeError: |
paul@180 | 171 | raise IllegalAddress(address) |
paul@68 | 172 | |
paul@68 | 173 | def save(self, address, value): |
paul@68 | 174 | |
paul@68 | 175 | "Save to the given 'address' the specified 'value'." |
paul@68 | 176 | |
paul@68 | 177 | try: |
paul@68 | 178 | self.memory[address] = value |
paul@68 | 179 | except IndexError: |
paul@180 | 180 | raise IllegalAddress(address) |
paul@180 | 181 | except TypeError: |
paul@180 | 182 | raise IllegalAddress(address) |
paul@68 | 183 | |
paul@68 | 184 | def new(self, size): |
paul@68 | 185 | |
paul@68 | 186 | """ |
paul@68 | 187 | Allocate space of the given 'size', returning the address of the space. |
paul@68 | 188 | """ |
paul@68 | 189 | |
paul@68 | 190 | addr = len(self.memory) |
paul@68 | 191 | for i in range(0, size): |
paul@68 | 192 | self.memory.append(None) |
paul@68 | 193 | return addr |
paul@68 | 194 | |
paul@68 | 195 | def push_pc(self, data): |
paul@68 | 196 | |
paul@68 | 197 | "Push 'data' onto the PC stack." |
paul@68 | 198 | |
paul@68 | 199 | self.pc_stack.append(data) |
paul@68 | 200 | |
paul@68 | 201 | def pull_pc(self): |
paul@68 | 202 | |
paul@68 | 203 | "Pull a value from the PC stack and return it." |
paul@68 | 204 | |
paul@68 | 205 | try: |
paul@68 | 206 | return self.pc_stack.pop() |
paul@68 | 207 | except IndexError: |
paul@68 | 208 | raise EmptyPCStack |
paul@68 | 209 | |
paul@90 | 210 | def run(self): |
paul@68 | 211 | |
paul@68 | 212 | "Execute code in the memory, starting from the current PC address." |
paul@68 | 213 | |
paul@68 | 214 | try: |
paul@68 | 215 | while 1: |
paul@90 | 216 | self.execute() |
paul@68 | 217 | except EmptyPCStack: |
paul@68 | 218 | pass |
paul@68 | 219 | |
paul@90 | 220 | def execute(self): |
paul@90 | 221 | |
paul@90 | 222 | "Execute code in the memory at the current PC address." |
paul@90 | 223 | |
paul@119 | 224 | self.instruction = self.load(self.pc) |
paul@118 | 225 | |
paul@118 | 226 | # Process any inputs of the instruction. |
paul@118 | 227 | |
paul@119 | 228 | self.process_inputs() |
paul@137 | 229 | |
paul@137 | 230 | # Perform the instruction itself. |
paul@137 | 231 | |
paul@137 | 232 | next_pc = self.perform(self.instruction) |
paul@118 | 233 | |
paul@119 | 234 | # Update the program counter. |
paul@118 | 235 | |
paul@119 | 236 | if next_pc is None: |
paul@119 | 237 | self.pc += 1 |
paul@119 | 238 | else: |
paul@119 | 239 | self.pc = next_pc |
paul@118 | 240 | |
paul@137 | 241 | def get_method(self, instruction): |
paul@137 | 242 | |
paul@137 | 243 | "Return the handler method for the given 'instruction'." |
paul@119 | 244 | |
paul@137 | 245 | instruction_name = instruction.__class__.__name__ |
paul@137 | 246 | if self.debug: |
paul@137 | 247 | print "%8d %s" % (self.pc, instruction_name) |
paul@119 | 248 | method = getattr(self, instruction_name, None) |
paul@90 | 249 | if method is None: |
paul@119 | 250 | raise IllegalInstruction, (self.pc, instruction_name) |
paul@118 | 251 | return method |
paul@118 | 252 | |
paul@137 | 253 | def perform(self, instruction): |
paul@137 | 254 | |
paul@137 | 255 | "Perform the 'instruction', returning the next PC value or None." |
paul@137 | 256 | |
paul@137 | 257 | self.operand = instruction.get_operand() |
paul@137 | 258 | method = self.get_method(instruction) |
paul@137 | 259 | return method() |
paul@137 | 260 | |
paul@119 | 261 | def process_inputs(self): |
paul@118 | 262 | |
paul@118 | 263 | """ |
paul@119 | 264 | Process any inputs of the current instruction. This permits any directly |
paul@118 | 265 | connected sub-instructions to produce the effects that separate |
paul@118 | 266 | instructions would otherwise have. |
paul@118 | 267 | """ |
paul@118 | 268 | |
paul@174 | 269 | if self.instruction.source is not None: |
paul@174 | 270 | self.perform(self.instruction.source) |
paul@174 | 271 | self.source = self.value |
paul@174 | 272 | if self.instruction.input is not None: |
paul@174 | 273 | self.perform(self.instruction.input) |
paul@90 | 274 | |
paul@68 | 275 | def jump(self, addr, next): |
paul@68 | 276 | |
paul@68 | 277 | """ |
paul@68 | 278 | Jump to the subroutine at (or identified by) 'addr'. If 'addr' |
paul@68 | 279 | identifies a library function then invoke the library function and set |
paul@68 | 280 | PC to 'next' afterwards; otherwise, set PC to 'addr'. |
paul@68 | 281 | """ |
paul@68 | 282 | |
paul@68 | 283 | if isinstance(addr, str): |
paul@68 | 284 | getattr(self, addr)() |
paul@119 | 285 | return next |
paul@68 | 286 | else: |
paul@138 | 287 | self.push_pc(self.pc + 1) |
paul@119 | 288 | return addr |
paul@68 | 289 | |
paul@68 | 290 | # Instructions. |
paul@68 | 291 | |
paul@98 | 292 | def LoadConst(self): |
paul@119 | 293 | self.value = None, self.operand |
paul@68 | 294 | |
paul@68 | 295 | def LoadName(self): |
paul@117 | 296 | frame = self.local_sp_stack[-1] |
paul@119 | 297 | self.value = self.frame_stack[frame + self.operand] |
paul@68 | 298 | |
paul@68 | 299 | def StoreName(self): |
paul@117 | 300 | frame = self.local_sp_stack[-1] |
paul@119 | 301 | self.frame_stack[frame + self.operand] = self.value |
paul@68 | 302 | |
paul@72 | 303 | LoadTemp = LoadName |
paul@72 | 304 | StoreTemp = StoreName |
paul@72 | 305 | |
paul@72 | 306 | def LoadAddress(self): |
paul@119 | 307 | # Preserve context (potentially null). |
paul@119 | 308 | self.value = self.load(self.operand) |
paul@72 | 309 | |
paul@117 | 310 | def LoadAddressContext(self): |
paul@119 | 311 | value = self.load(self.operand) |
paul@119 | 312 | # Replace the context with the current value. |
paul@119 | 313 | self.value = self.value[1], value[1] |
paul@72 | 314 | |
paul@72 | 315 | def StoreAddress(self): |
paul@119 | 316 | # Preserve context. |
paul@119 | 317 | self.save(self.operand, self.value) |
paul@72 | 318 | |
paul@117 | 319 | def MakeObject(self): |
paul@138 | 320 | size = self.operand |
paul@138 | 321 | context, ref = self.value |
paul@145 | 322 | classcode, attrcode, codeaddr, codedetails, instance = self.load(ref) |
paul@138 | 323 | addr = self.new(size) |
paul@138 | 324 | # Set the header to resemble the class. |
paul@145 | 325 | self.save(addr, (classcode, attrcode, None, None, 1)) # NOTE: __call__ method not yet provided. |
paul@119 | 326 | # Introduce null context for new object. |
paul@138 | 327 | self.value = None, addr |
paul@72 | 328 | |
paul@118 | 329 | def LoadAttr(self): |
paul@118 | 330 | context, ref = self.value |
paul@127 | 331 | # Retrieved context should already be appropriate for the instance. |
paul@174 | 332 | self.value = self.load(ref + self.operand + 1) |
paul@118 | 333 | |
paul@118 | 334 | def StoreAttr(self): |
paul@119 | 335 | context, ref = self.value |
paul@129 | 336 | # Target should already be an instance. |
paul@174 | 337 | self.save(ref + self.operand + 1, self.source) |
paul@119 | 338 | |
paul@119 | 339 | def LoadAttrIndex(self): |
paul@118 | 340 | context, ref = self.value |
paul@145 | 341 | classcode, attrcode, codeaddr, codedetails, instance = self.load(ref) |
paul@138 | 342 | element = self.objlist[classcode + self.operand] |
paul@135 | 343 | attr_index, class_attr, replace_context, offset = element |
paul@135 | 344 | if attr_index == self.operand: |
paul@127 | 345 | if class_attr: |
paul@127 | 346 | loaded_context, loaded_ref = self.load(offset) # offset is address of class attribute |
paul@127 | 347 | if replace_context: |
paul@127 | 348 | self.value = ref, loaded_ref # classes can also replace the context if compatible |
paul@127 | 349 | return |
paul@127 | 350 | self.value = loaded_context, loaded_ref |
paul@127 | 351 | else: |
paul@127 | 352 | self.value = self.load(ref + offset) |
paul@119 | 353 | else: |
paul@146 | 354 | self.exception = self.attr_error |
paul@118 | 355 | |
paul@129 | 356 | def StoreAttrIndex(self): |
paul@129 | 357 | context, ref = self.value |
paul@145 | 358 | classcode, attrcode, codeaddr, codedetails, instance = self.load(ref) |
paul@138 | 359 | element = self.objlist[classcode + self.operand] |
paul@135 | 360 | attr_index, class_attr, replace_context, offset = element |
paul@135 | 361 | if attr_index == self.operand: |
paul@129 | 362 | if class_attr: |
paul@146 | 363 | self.exception = self.type_error |
paul@129 | 364 | else: |
paul@129 | 365 | self.save(ref + offset, self.source) |
paul@129 | 366 | else: |
paul@146 | 367 | self.exception = self.attr_error |
paul@118 | 368 | |
paul@127 | 369 | # NOTE: LoadAttrIndexContext is a possibility if a particular attribute can always be overridden. |
paul@127 | 370 | |
paul@98 | 371 | def MakeFrame(self): |
paul@117 | 372 | self.invocation_sp_stack.append(len(self.frame_stack)) |
paul@119 | 373 | self.frame_stack.extend([None] * self.operand) |
paul@98 | 374 | |
paul@98 | 375 | def DropFrame(self): |
paul@117 | 376 | self.local_sp_stack.pop() |
paul@117 | 377 | frame = self.invocation_sp_stack.pop() |
paul@117 | 378 | self.frame_stack = self.frame_stack[:frame] # reset stack before call |
paul@98 | 379 | |
paul@137 | 380 | def RecoverFrame(self): |
paul@137 | 381 | self.local_sp_stack.pop() |
paul@137 | 382 | |
paul@129 | 383 | def StoreFrame(self): |
paul@129 | 384 | frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame |
paul@129 | 385 | self.frame_stack[frame + self.operand] = self.value |
paul@129 | 386 | |
paul@129 | 387 | def StoreFrameIndex(self): |
paul@129 | 388 | frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame |
paul@145 | 389 | classcode, attrcode, codeaddr, codedetails, instance = self.load(ref) |
paul@138 | 390 | element = self.objlist[classcode + self.operand] |
paul@135 | 391 | attr_index, offset = element |
paul@135 | 392 | if attr_index == self.operand: |
paul@129 | 393 | self.frame_stack[frame + offset] = self.value |
paul@129 | 394 | else: |
paul@146 | 395 | self.exception = self.type_error |
paul@129 | 396 | |
paul@132 | 397 | def LoadCallable(self): |
paul@132 | 398 | context, ref = self.value |
paul@145 | 399 | classcode, attrcode, codeaddr, codedetails, instance = self.load(ref) |
paul@134 | 400 | self.callable = codeaddr, codedetails |
paul@132 | 401 | |
paul@132 | 402 | def StoreCallable(self): |
paul@132 | 403 | context, ref = self.value |
paul@134 | 404 | # NOTE: Should improve the representation and permit direct saving. |
paul@145 | 405 | classcode, attrcode, codeaddr, codedetails, instance = self.load(ref) |
paul@145 | 406 | self.save(ref, (classcode, attrcode) + self.callable + (instance,)) |
paul@132 | 407 | |
paul@132 | 408 | def LoadContext(self): |
paul@132 | 409 | context, ref = self.value |
paul@132 | 410 | self.value = None, context |
paul@132 | 411 | |
paul@132 | 412 | def CheckFrame(self): |
paul@134 | 413 | operand = self.operand |
paul@134 | 414 | frame = self.invocation_sp_stack[-1] |
paul@132 | 415 | context, ref = self.value |
paul@145 | 416 | classcode, attrcode, codeaddr, codedetails, instance = self.load(ref) |
paul@134 | 417 | |
paul@134 | 418 | # Support sliding of the frame to exclude any inappropriate context. |
paul@134 | 419 | |
paul@134 | 420 | if context is None: |
paul@138 | 421 | self.invocation_sp_stack[-1] += 1 |
paul@134 | 422 | operand -= 1 |
paul@134 | 423 | else: |
paul@145 | 424 | context_classcode, context_attrcode, context_codeaddr, context_codedetails, context_instance = self.load(context) |
paul@145 | 425 | if not context_instance: |
paul@138 | 426 | self.invocation_sp_stack[-1] += 1 |
paul@134 | 427 | operand -= 1 |
paul@134 | 428 | |
paul@134 | 429 | nargs, ndefaults = codedetails |
paul@134 | 430 | if not (nargs - ndefaults <= operand <= nargs): |
paul@132 | 431 | raise Exception, "CheckFrame %r" % (nargs - ndefaults, self.operand, nargs) |
paul@134 | 432 | |
paul@132 | 433 | # NOTE: Support population of defaults. |
paul@117 | 434 | |
paul@134 | 435 | def CheckSelf(self): |
paul@134 | 436 | context, ref = self.value |
paul@134 | 437 | target_context, target_ref = self.source |
paul@135 | 438 | |
paul@145 | 439 | # Check the details of the proposed context and the target's context. |
paul@135 | 440 | |
paul@145 | 441 | self._CheckInstance(ref, target_context) |
paul@124 | 442 | |
paul@132 | 443 | def JumpWithFrame(self): |
paul@138 | 444 | codeaddr, codedetails = self.callable |
paul@132 | 445 | self.local_sp_stack.append(self.invocation_sp_stack[-1]) # adopt the invocation frame |
paul@138 | 446 | return self.jump(codeaddr, self.pc + 1) # return to the instruction after this one |
paul@98 | 447 | |
paul@132 | 448 | def ExtendFrame(self): |
paul@138 | 449 | self.frame_stack.extend([None] * self.operand) |
paul@98 | 450 | |
paul@137 | 451 | def AdjustFrame(self): |
paul@137 | 452 | if self.operand > 0: |
paul@138 | 453 | self.frame_stack.append([None] * self.operand) |
paul@137 | 454 | elif self.operand == -1: |
paul@137 | 455 | self.invocation_sp_stack[-1] -= 1 |
paul@137 | 456 | else: |
paul@137 | 457 | raise Exception, "AdjustFrame %r" % self.operand |
paul@137 | 458 | |
paul@68 | 459 | def Return(self): |
paul@138 | 460 | return self.pull_pc() |
paul@68 | 461 | |
paul@117 | 462 | def LoadResult(self): |
paul@117 | 463 | self.value = self.result |
paul@117 | 464 | |
paul@117 | 465 | def StoreResult(self): |
paul@117 | 466 | self.result = self.value |
paul@117 | 467 | |
paul@118 | 468 | def Jump(self): |
paul@119 | 469 | return self.operand |
paul@98 | 470 | |
paul@68 | 471 | def JumpIfTrue(self): |
paul@117 | 472 | if self.status: |
paul@119 | 473 | return self.operand |
paul@68 | 474 | |
paul@68 | 475 | def JumpIfFalse(self): |
paul@117 | 476 | if not self.status: |
paul@119 | 477 | return self.operand |
paul@68 | 478 | |
paul@129 | 479 | def LoadException(self): |
paul@145 | 480 | self.value = None, self.exception |
paul@129 | 481 | |
paul@129 | 482 | def StoreException(self): |
paul@145 | 483 | self.exception = self.value[1] |
paul@129 | 484 | |
paul@129 | 485 | def RaiseException(self): |
paul@145 | 486 | return self.handler_stack[-1] |
paul@129 | 487 | |
paul@129 | 488 | def PushHandler(self): |
paul@129 | 489 | self.handler_stack.append(self.operand) |
paul@145 | 490 | self.handler_local_sp_stack.append(len(self.local_sp_stack)) |
paul@145 | 491 | self.handler_pc_stack.append(len(self.pc_stack)) |
paul@129 | 492 | |
paul@129 | 493 | def PopHandler(self): |
paul@145 | 494 | # Reduce the local frame pointer stack to refer to the handler's frame. |
paul@145 | 495 | self.local_sp_stack = self.local_sp_stack[:self.handler_local_sp_stack.pop()] |
paul@145 | 496 | # Reduce the PC stack to discard all superfluous return addresses. |
paul@145 | 497 | self.pc_stack = self.pc_stack[:self.handler_pc_stack.pop()] |
paul@129 | 498 | self.handler_stack.pop() |
paul@129 | 499 | |
paul@129 | 500 | def CheckException(self): |
paul@145 | 501 | self._CheckInstance(self.exception, self.value[1]) |
paul@129 | 502 | |
paul@129 | 503 | def TestIdentity(self): |
paul@145 | 504 | self.status = self.value[1] == self.source[1] |
paul@129 | 505 | |
paul@129 | 506 | def TestIdentityAddress(self): |
paul@129 | 507 | self.status = self.value[1] == self.operand |
paul@129 | 508 | |
paul@129 | 509 | # LoadBoolean is implemented in the generated code. |
paul@129 | 510 | # StoreBoolean is implemented by testing against the True value. |
paul@68 | 511 | |
paul@132 | 512 | def InvertBoolean(self): |
paul@132 | 513 | self.status = not self.status |
paul@132 | 514 | |
paul@145 | 515 | # Common implementation details. |
paul@145 | 516 | |
paul@145 | 517 | def _CheckInstance(self, ref, cls): |
paul@145 | 518 | classcode, attrcode, codeaddr, codedetails, instance = self.load(ref) |
paul@145 | 519 | target_classcode, target_attrcode, target_codeaddr, target_codedetails, target_instance = self.load(cls) |
paul@145 | 520 | |
paul@145 | 521 | # Find the table entry for the descendant. |
paul@145 | 522 | |
paul@145 | 523 | element = self.objlist[target_classcode + attrcode] |
paul@145 | 524 | attr_index, class_attr, replace_context, offset = element |
paul@145 | 525 | if attr_index == attrcode: |
paul@145 | 526 | self.status = 1 |
paul@145 | 527 | else: |
paul@145 | 528 | self.status = 0 |
paul@145 | 529 | |
paul@68 | 530 | # vim: tabstop=4 expandtab shiftwidth=4 |