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@192 | 114 | self.attr_error = objlist.access("__builtins__", "AttributeError").get_value().location |
paul@192 | 115 | self.type_error = objlist.access("__builtins__", "TypeError").get_value().location |
paul@181 | 116 | |
paul@181 | 117 | # Native class constants. |
paul@181 | 118 | |
paul@182 | 119 | cls = objlist.access("__builtins__", "int") |
paul@192 | 120 | self.int_instance_location = cls and cls.get_value() and cls.get_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@190 | 276 | value = self.value |
paul@174 | 277 | if self.instruction.source is not None: |
paul@174 | 278 | self.perform(self.instruction.source) |
paul@174 | 279 | self.source = self.value |
paul@190 | 280 | self.value = value |
paul@174 | 281 | if self.instruction.input is not None: |
paul@174 | 282 | self.perform(self.instruction.input) |
paul@90 | 283 | |
paul@68 | 284 | def jump(self, addr, next): |
paul@68 | 285 | |
paul@68 | 286 | """ |
paul@68 | 287 | Jump to the subroutine at (or identified by) 'addr'. If 'addr' |
paul@68 | 288 | identifies a library function then invoke the library function and set |
paul@68 | 289 | PC to 'next' afterwards; otherwise, set PC to 'addr'. |
paul@68 | 290 | """ |
paul@68 | 291 | |
paul@181 | 292 | # Trap library functions introduced through the use of strings instead |
paul@181 | 293 | # of proper locations. |
paul@181 | 294 | |
paul@68 | 295 | if isinstance(addr, str): |
paul@181 | 296 | self.native_functions[addr](self) |
paul@119 | 297 | return next |
paul@68 | 298 | else: |
paul@138 | 299 | self.push_pc(self.pc + 1) |
paul@119 | 300 | return addr |
paul@68 | 301 | |
paul@68 | 302 | # Instructions. |
paul@68 | 303 | |
paul@98 | 304 | def LoadConst(self): |
paul@119 | 305 | self.value = None, self.operand |
paul@68 | 306 | |
paul@68 | 307 | def LoadName(self): |
paul@117 | 308 | frame = self.local_sp_stack[-1] |
paul@119 | 309 | self.value = self.frame_stack[frame + self.operand] |
paul@68 | 310 | |
paul@68 | 311 | def StoreName(self): |
paul@117 | 312 | frame = self.local_sp_stack[-1] |
paul@119 | 313 | self.frame_stack[frame + self.operand] = self.value |
paul@68 | 314 | |
paul@72 | 315 | LoadTemp = LoadName |
paul@72 | 316 | StoreTemp = StoreName |
paul@72 | 317 | |
paul@72 | 318 | def LoadAddress(self): |
paul@119 | 319 | # Preserve context (potentially null). |
paul@119 | 320 | self.value = self.load(self.operand) |
paul@72 | 321 | |
paul@117 | 322 | def LoadAddressContext(self): |
paul@191 | 323 | context, ref = self.load(self.operand) |
paul@191 | 324 | inst_context, inst_ref = self.value |
paul@191 | 325 | self.value = self._LoadAddressContext(context, ref, inst_context, inst_ref) |
paul@72 | 326 | |
paul@72 | 327 | def StoreAddress(self): |
paul@119 | 328 | # Preserve context. |
paul@190 | 329 | self.save(self.operand, self.source) |
paul@72 | 330 | |
paul@117 | 331 | def MakeObject(self): |
paul@138 | 332 | size = self.operand |
paul@138 | 333 | context, ref = self.value |
paul@184 | 334 | # NOTE: Referencing the instance template. |
paul@184 | 335 | addr = self._MakeObject(size, ref - 1) |
paul@184 | 336 | # Introduce object as context for the new object. |
paul@185 | 337 | self.value = addr, addr |
paul@72 | 338 | |
paul@118 | 339 | def LoadAttr(self): |
paul@118 | 340 | context, ref = self.value |
paul@127 | 341 | # Retrieved context should already be appropriate for the instance. |
paul@184 | 342 | # NOTE: Adding 1 to skip any header. |
paul@174 | 343 | self.value = self.load(ref + self.operand + 1) |
paul@118 | 344 | |
paul@118 | 345 | def StoreAttr(self): |
paul@119 | 346 | context, ref = self.value |
paul@129 | 347 | # Target should already be an instance. |
paul@184 | 348 | # NOTE: Adding 1 to skip any header. |
paul@174 | 349 | self.save(ref + self.operand + 1, self.source) |
paul@119 | 350 | |
paul@119 | 351 | def LoadAttrIndex(self): |
paul@118 | 352 | context, ref = self.value |
paul@182 | 353 | data = self.load(ref) |
paul@182 | 354 | element = self.objlist[data.classcode + self.operand] |
paul@135 | 355 | attr_index, class_attr, replace_context, offset = element |
paul@135 | 356 | if attr_index == self.operand: |
paul@127 | 357 | if class_attr: |
paul@127 | 358 | loaded_context, loaded_ref = self.load(offset) # offset is address of class attribute |
paul@191 | 359 | self.value = self._LoadAddressContext(loaded_context, loaded_ref, context, ref) |
paul@127 | 360 | else: |
paul@127 | 361 | self.value = self.load(ref + offset) |
paul@119 | 362 | else: |
paul@146 | 363 | self.exception = self.attr_error |
paul@190 | 364 | return self.RaiseException() |
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@190 | 374 | return self.RaiseException() |
paul@129 | 375 | else: |
paul@129 | 376 | self.save(ref + offset, self.source) |
paul@129 | 377 | else: |
paul@146 | 378 | self.exception = self.attr_error |
paul@190 | 379 | return self.RaiseException() |
paul@118 | 380 | |
paul@127 | 381 | # NOTE: LoadAttrIndexContext is a possibility if a particular attribute can always be overridden. |
paul@127 | 382 | |
paul@98 | 383 | def MakeFrame(self): |
paul@117 | 384 | self.invocation_sp_stack.append(len(self.frame_stack)) |
paul@119 | 385 | self.frame_stack.extend([None] * self.operand) |
paul@98 | 386 | |
paul@98 | 387 | def DropFrame(self): |
paul@117 | 388 | self.local_sp_stack.pop() |
paul@117 | 389 | frame = self.invocation_sp_stack.pop() |
paul@117 | 390 | self.frame_stack = self.frame_stack[:frame] # reset stack before call |
paul@98 | 391 | |
paul@137 | 392 | def RecoverFrame(self): |
paul@137 | 393 | self.local_sp_stack.pop() |
paul@137 | 394 | |
paul@129 | 395 | def StoreFrame(self): |
paul@129 | 396 | frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame |
paul@129 | 397 | self.frame_stack[frame + self.operand] = self.value |
paul@129 | 398 | |
paul@129 | 399 | def StoreFrameIndex(self): |
paul@184 | 400 | context, ref = self.value |
paul@129 | 401 | frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame |
paul@182 | 402 | data = self.load(ref) |
paul@190 | 403 | element = self.paramlist[data.funccode + self.operand] |
paul@190 | 404 | # NOTE: Need to ensure correct positioning where a context has been generated. |
paul@190 | 405 | param_index, offset = element |
paul@190 | 406 | if param_index == self.operand: |
paul@190 | 407 | self.frame_stack[frame + offset + 1] = self.source # add 1 to skip the context always generated |
paul@129 | 408 | else: |
paul@146 | 409 | self.exception = self.type_error |
paul@190 | 410 | return self.RaiseException() |
paul@129 | 411 | |
paul@132 | 412 | def LoadCallable(self): |
paul@132 | 413 | context, ref = self.value |
paul@182 | 414 | data = self.load(ref) |
paul@182 | 415 | self.callable = data.codeaddr, data.codedetails |
paul@132 | 416 | |
paul@132 | 417 | def StoreCallable(self): |
paul@132 | 418 | context, ref = self.value |
paul@134 | 419 | # NOTE: Should improve the representation and permit direct saving. |
paul@182 | 420 | data = self.load(ref) |
paul@182 | 421 | self.save(ref, (data.classcode, data.attrcode) + self.callable + (data.instance,)) |
paul@132 | 422 | |
paul@132 | 423 | def LoadContext(self): |
paul@132 | 424 | context, ref = self.value |
paul@132 | 425 | self.value = None, context |
paul@132 | 426 | |
paul@132 | 427 | def CheckFrame(self): |
paul@134 | 428 | operand = self.operand |
paul@134 | 429 | frame = self.invocation_sp_stack[-1] |
paul@132 | 430 | context, ref = self.value |
paul@182 | 431 | data = self.load(ref) |
paul@134 | 432 | |
paul@134 | 433 | # Support sliding of the frame to exclude any inappropriate context. |
paul@134 | 434 | |
paul@134 | 435 | if context is None: |
paul@138 | 436 | self.invocation_sp_stack[-1] += 1 |
paul@134 | 437 | operand -= 1 |
paul@134 | 438 | else: |
paul@182 | 439 | context_data = self.load(context) |
paul@182 | 440 | if not context_data.instance: |
paul@138 | 441 | self.invocation_sp_stack[-1] += 1 |
paul@134 | 442 | operand -= 1 |
paul@134 | 443 | |
paul@190 | 444 | # Test the frame size. |
paul@190 | 445 | |
paul@182 | 446 | nargs, ndefaults = data.codedetails |
paul@184 | 447 | if not ((nargs - ndefaults) <= operand <= nargs): |
paul@184 | 448 | raise Exception, "CheckFrame %r (%r <= %r <= %r)" % (self.operand, nargs - ndefaults, operand, nargs) |
paul@134 | 449 | |
paul@190 | 450 | # Support population of defaults. |
paul@190 | 451 | # This involves copying the "attributes" of a function into the frame. |
paul@190 | 452 | |
paul@190 | 453 | default = operand - (nargs - ndefaults) |
paul@190 | 454 | self.frame_stack.extend([None] * (nargs - operand)) |
paul@190 | 455 | pos = self.operand |
paul@190 | 456 | |
paul@190 | 457 | while operand < nargs: |
paul@190 | 458 | self.frame_stack[frame + pos] = self.load(ref + default + 1) # skip header |
paul@190 | 459 | default += 1 |
paul@190 | 460 | pos += 1 |
paul@190 | 461 | operand += 1 |
paul@117 | 462 | |
paul@134 | 463 | def CheckSelf(self): |
paul@134 | 464 | context, ref = self.value |
paul@134 | 465 | target_context, target_ref = self.source |
paul@135 | 466 | |
paul@145 | 467 | # Check the details of the proposed context and the target's context. |
paul@135 | 468 | |
paul@181 | 469 | self.status = self._CheckInstance(ref, target_context) |
paul@124 | 470 | |
paul@132 | 471 | def JumpWithFrame(self): |
paul@138 | 472 | codeaddr, codedetails = self.callable |
paul@132 | 473 | self.local_sp_stack.append(self.invocation_sp_stack[-1]) # adopt the invocation frame |
paul@138 | 474 | return self.jump(codeaddr, self.pc + 1) # return to the instruction after this one |
paul@98 | 475 | |
paul@132 | 476 | def ExtendFrame(self): |
paul@138 | 477 | self.frame_stack.extend([None] * self.operand) |
paul@98 | 478 | |
paul@137 | 479 | def AdjustFrame(self): |
paul@137 | 480 | if self.operand > 0: |
paul@138 | 481 | self.frame_stack.append([None] * self.operand) |
paul@137 | 482 | elif self.operand == -1: |
paul@137 | 483 | self.invocation_sp_stack[-1] -= 1 |
paul@137 | 484 | else: |
paul@137 | 485 | raise Exception, "AdjustFrame %r" % self.operand |
paul@137 | 486 | |
paul@68 | 487 | def Return(self): |
paul@138 | 488 | return self.pull_pc() |
paul@68 | 489 | |
paul@117 | 490 | def LoadResult(self): |
paul@117 | 491 | self.value = self.result |
paul@117 | 492 | |
paul@117 | 493 | def StoreResult(self): |
paul@117 | 494 | self.result = self.value |
paul@117 | 495 | |
paul@118 | 496 | def Jump(self): |
paul@119 | 497 | return self.operand |
paul@98 | 498 | |
paul@68 | 499 | def JumpIfTrue(self): |
paul@117 | 500 | if self.status: |
paul@119 | 501 | return self.operand |
paul@68 | 502 | |
paul@68 | 503 | def JumpIfFalse(self): |
paul@117 | 504 | if not self.status: |
paul@119 | 505 | return self.operand |
paul@68 | 506 | |
paul@129 | 507 | def LoadException(self): |
paul@145 | 508 | self.value = None, self.exception |
paul@129 | 509 | |
paul@129 | 510 | def StoreException(self): |
paul@145 | 511 | self.exception = self.value[1] |
paul@129 | 512 | |
paul@129 | 513 | def RaiseException(self): |
paul@145 | 514 | return self.handler_stack[-1] |
paul@129 | 515 | |
paul@129 | 516 | def PushHandler(self): |
paul@129 | 517 | self.handler_stack.append(self.operand) |
paul@145 | 518 | self.handler_local_sp_stack.append(len(self.local_sp_stack)) |
paul@145 | 519 | self.handler_pc_stack.append(len(self.pc_stack)) |
paul@129 | 520 | |
paul@129 | 521 | def PopHandler(self): |
paul@145 | 522 | # Reduce the local frame pointer stack to refer to the handler's frame. |
paul@145 | 523 | self.local_sp_stack = self.local_sp_stack[:self.handler_local_sp_stack.pop()] |
paul@145 | 524 | # Reduce the PC stack to discard all superfluous return addresses. |
paul@145 | 525 | self.pc_stack = self.pc_stack[:self.handler_pc_stack.pop()] |
paul@129 | 526 | self.handler_stack.pop() |
paul@129 | 527 | |
paul@129 | 528 | def CheckException(self): |
paul@181 | 529 | self.status = self.exception is not None and self._CheckInstance(self.exception, self.value[1]) |
paul@129 | 530 | |
paul@129 | 531 | def TestIdentity(self): |
paul@145 | 532 | self.status = self.value[1] == self.source[1] |
paul@129 | 533 | |
paul@129 | 534 | def TestIdentityAddress(self): |
paul@129 | 535 | self.status = self.value[1] == self.operand |
paul@129 | 536 | |
paul@129 | 537 | # LoadBoolean is implemented in the generated code. |
paul@129 | 538 | # StoreBoolean is implemented by testing against the True value. |
paul@68 | 539 | |
paul@132 | 540 | def InvertBoolean(self): |
paul@132 | 541 | self.status = not self.status |
paul@132 | 542 | |
paul@145 | 543 | # Common implementation details. |
paul@145 | 544 | |
paul@145 | 545 | def _CheckInstance(self, ref, cls): |
paul@182 | 546 | data = self.load(ref) |
paul@182 | 547 | target_data = self.load(cls) |
paul@145 | 548 | |
paul@191 | 549 | # Insist on a class. |
paul@191 | 550 | |
paul@191 | 551 | if target_data.instance: |
paul@191 | 552 | return 0 |
paul@191 | 553 | |
paul@145 | 554 | # Find the table entry for the descendant. |
paul@145 | 555 | |
paul@182 | 556 | element = self.objlist[target_data.classcode + data.attrcode] |
paul@145 | 557 | attr_index, class_attr, replace_context, offset = element |
paul@182 | 558 | return attr_index == data.attrcode |
paul@181 | 559 | |
paul@181 | 560 | def _MakeObject(self, size, ref): |
paul@182 | 561 | data = self.load(ref) |
paul@181 | 562 | addr = self.new(size) |
paul@181 | 563 | # Set the header to resemble the class. |
paul@184 | 564 | self.save(addr, data) |
paul@181 | 565 | return addr |
paul@181 | 566 | |
paul@191 | 567 | def _LoadAddressContext(self, context, ref, inst_context, inst_ref): |
paul@191 | 568 | |
paul@191 | 569 | # Check the instance context against the target's context. |
paul@191 | 570 | |
paul@191 | 571 | if self._CheckInstance(inst_ref, context): |
paul@191 | 572 | # Replace the context with the instance. |
paul@191 | 573 | return inst_ref, ref |
paul@191 | 574 | else: |
paul@191 | 575 | return context, ref |
paul@191 | 576 | |
paul@181 | 577 | # Native function implementations. |
paul@181 | 578 | |
paul@181 | 579 | def builtins_object_init(self): |
paul@181 | 580 | pass |
paul@181 | 581 | |
paul@181 | 582 | def builtins_int_init(self): |
paul@181 | 583 | pass |
paul@181 | 584 | |
paul@181 | 585 | def builtins_int_add(self): |
paul@181 | 586 | frame = self.local_sp_stack[-1] |
paul@181 | 587 | |
paul@181 | 588 | # Get operands addresses. |
paul@181 | 589 | |
paul@181 | 590 | left_context, left = self.frame_stack[frame] |
paul@181 | 591 | right_context, right = self.frame_stack[frame + 1] |
paul@181 | 592 | |
paul@181 | 593 | # Test operand suitability. |
paul@181 | 594 | |
paul@187 | 595 | if not self._CheckInstance(left, self.int_instance_location) and self._CheckInstance(right, self.int_instance_location): |
paul@181 | 596 | self.exception = self.type_error |
paul@181 | 597 | return self.RaiseException() |
paul@181 | 598 | |
paul@181 | 599 | # NOTE: Assume single location for data. |
paul@181 | 600 | |
paul@181 | 601 | left_data = left + 1 |
paul@181 | 602 | right_data = right + 1 |
paul@181 | 603 | |
paul@181 | 604 | # Make a new object. |
paul@181 | 605 | |
paul@184 | 606 | addr = self._MakeObject(2, self.int_instance_location) |
paul@181 | 607 | self.save(addr + 1, self.load(left_data) + self.load(right_data)) |
paul@181 | 608 | |
paul@187 | 609 | # Return the new object. |
paul@187 | 610 | # Introduce object as context for the new object. |
paul@181 | 611 | |
paul@187 | 612 | self.result = addr, addr |
paul@181 | 613 | |
paul@181 | 614 | native_functions = { |
paul@181 | 615 | "__builtins__.object.__init__" : builtins_object_init, |
paul@181 | 616 | "__builtins__.int.__init__" : builtins_int_init, |
paul@181 | 617 | "__builtins__.int.__add__" : builtins_int_add, |
paul@181 | 618 | } |
paul@145 | 619 | |
paul@68 | 620 | # vim: tabstop=4 expandtab shiftwidth=4 |