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