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