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