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