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@117 | 35 | current exception |
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@118 | 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@117 | 89 | self.result = None |
paul@117 | 90 | self.exception = None |
paul@68 | 91 | |
paul@68 | 92 | def load(self, address): |
paul@68 | 93 | |
paul@68 | 94 | "Return the value at the given 'address'." |
paul@68 | 95 | |
paul@68 | 96 | try: |
paul@68 | 97 | return self.memory[address] |
paul@68 | 98 | except IndexError: |
paul@68 | 99 | raise IllegalAddress, address |
paul@68 | 100 | |
paul@68 | 101 | def save(self, address, value): |
paul@68 | 102 | |
paul@68 | 103 | "Save to the given 'address' the specified 'value'." |
paul@68 | 104 | |
paul@68 | 105 | try: |
paul@68 | 106 | self.memory[address] = value |
paul@68 | 107 | except IndexError: |
paul@68 | 108 | raise IllegalAddress, address |
paul@68 | 109 | |
paul@68 | 110 | def new(self, size): |
paul@68 | 111 | |
paul@68 | 112 | """ |
paul@68 | 113 | Allocate space of the given 'size', returning the address of the space. |
paul@68 | 114 | """ |
paul@68 | 115 | |
paul@68 | 116 | addr = len(self.memory) |
paul@68 | 117 | for i in range(0, size): |
paul@68 | 118 | self.memory.append(None) |
paul@68 | 119 | return addr |
paul@68 | 120 | |
paul@68 | 121 | def push_pc(self, data): |
paul@68 | 122 | |
paul@68 | 123 | "Push 'data' onto the PC stack." |
paul@68 | 124 | |
paul@68 | 125 | self.pc_stack.append(data) |
paul@68 | 126 | |
paul@68 | 127 | def pull_pc(self): |
paul@68 | 128 | |
paul@68 | 129 | "Pull a value from the PC stack and return it." |
paul@68 | 130 | |
paul@68 | 131 | try: |
paul@68 | 132 | return self.pc_stack.pop() |
paul@68 | 133 | except IndexError: |
paul@68 | 134 | raise EmptyPCStack |
paul@68 | 135 | |
paul@90 | 136 | def run(self): |
paul@68 | 137 | |
paul@68 | 138 | "Execute code in the memory, starting from the current PC address." |
paul@68 | 139 | |
paul@68 | 140 | try: |
paul@68 | 141 | while 1: |
paul@90 | 142 | self.execute() |
paul@68 | 143 | except EmptyPCStack: |
paul@68 | 144 | pass |
paul@68 | 145 | |
paul@90 | 146 | def execute(self): |
paul@90 | 147 | |
paul@90 | 148 | "Execute code in the memory at the current PC address." |
paul@90 | 149 | |
paul@119 | 150 | self.instruction = self.load(self.pc) |
paul@119 | 151 | self.operand = self.instruction.get_operand() |
paul@119 | 152 | |
paul@119 | 153 | instruction_name = self.instruction.__class__.__name__ |
paul@90 | 154 | if self.debug: |
paul@119 | 155 | print "%8d %s" % (self.pc, instruction_name) |
paul@118 | 156 | |
paul@119 | 157 | method = self.get_method(instruction_name) |
paul@118 | 158 | |
paul@118 | 159 | # Process any inputs of the instruction. |
paul@118 | 160 | |
paul@119 | 161 | self.process_inputs() |
paul@119 | 162 | next_pc = method() |
paul@118 | 163 | |
paul@119 | 164 | # Update the program counter. |
paul@118 | 165 | |
paul@119 | 166 | if next_pc is None: |
paul@119 | 167 | self.pc += 1 |
paul@119 | 168 | else: |
paul@119 | 169 | self.pc = next_pc |
paul@118 | 170 | |
paul@119 | 171 | def get_method(self, instruction_name): |
paul@119 | 172 | |
paul@119 | 173 | "Return the handler method for the given 'instruction_name'." |
paul@119 | 174 | |
paul@119 | 175 | method = getattr(self, instruction_name, None) |
paul@90 | 176 | if method is None: |
paul@119 | 177 | raise IllegalInstruction, (self.pc, instruction_name) |
paul@118 | 178 | return method |
paul@118 | 179 | |
paul@119 | 180 | def process_inputs(self): |
paul@118 | 181 | |
paul@118 | 182 | """ |
paul@119 | 183 | Process any inputs of the current instruction. This permits any directly |
paul@118 | 184 | connected sub-instructions to produce the effects that separate |
paul@118 | 185 | instructions would otherwise have. |
paul@118 | 186 | """ |
paul@118 | 187 | |
paul@119 | 188 | for input in (self.instruction.input, self.instruction.source): |
paul@118 | 189 | if input is not None: |
paul@118 | 190 | method = self.get_method(input) |
paul@118 | 191 | method() |
paul@90 | 192 | |
paul@68 | 193 | def jump(self, addr, next): |
paul@68 | 194 | |
paul@68 | 195 | """ |
paul@68 | 196 | Jump to the subroutine at (or identified by) 'addr'. If 'addr' |
paul@68 | 197 | identifies a library function then invoke the library function and set |
paul@68 | 198 | PC to 'next' afterwards; otherwise, set PC to 'addr'. |
paul@68 | 199 | """ |
paul@68 | 200 | |
paul@68 | 201 | if isinstance(addr, str): |
paul@68 | 202 | getattr(self, addr)() |
paul@119 | 203 | return next |
paul@68 | 204 | else: |
paul@68 | 205 | self.push_pc(self.pc + 2) |
paul@119 | 206 | return addr |
paul@68 | 207 | |
paul@68 | 208 | # Instructions. |
paul@68 | 209 | |
paul@98 | 210 | def LoadConst(self): |
paul@119 | 211 | self.value = None, self.operand |
paul@68 | 212 | |
paul@68 | 213 | def LoadName(self): |
paul@117 | 214 | frame = self.local_sp_stack[-1] |
paul@119 | 215 | self.value = self.frame_stack[frame + self.operand] |
paul@68 | 216 | |
paul@68 | 217 | def StoreName(self): |
paul@117 | 218 | frame = self.local_sp_stack[-1] |
paul@119 | 219 | self.frame_stack[frame + self.operand] = self.value |
paul@68 | 220 | |
paul@72 | 221 | LoadTemp = LoadName |
paul@72 | 222 | StoreTemp = StoreName |
paul@72 | 223 | |
paul@72 | 224 | def LoadAddress(self): |
paul@119 | 225 | # Preserve context (potentially null). |
paul@119 | 226 | self.value = self.load(self.operand) |
paul@72 | 227 | |
paul@117 | 228 | def LoadAddressContext(self): |
paul@119 | 229 | value = self.load(self.operand) |
paul@119 | 230 | # Replace the context with the current value. |
paul@119 | 231 | self.value = self.value[1], value[1] |
paul@72 | 232 | |
paul@72 | 233 | def StoreAddress(self): |
paul@119 | 234 | # Preserve context. |
paul@119 | 235 | self.save(self.operand, self.value) |
paul@72 | 236 | |
paul@117 | 237 | def MakeObject(self): |
paul@119 | 238 | # Introduce null context for new object. |
paul@119 | 239 | self.value = None, self.new(self.operand) |
paul@72 | 240 | |
paul@118 | 241 | def LoadAttr(self): |
paul@118 | 242 | context, ref = self.value |
paul@119 | 243 | loaded_context, loaded_ref = self.load(ref + self.operand) |
paul@119 | 244 | if loaded_context is None: |
paul@119 | 245 | # Override null context with owning instance. |
paul@119 | 246 | self.value = ref, loaded_ref |
paul@119 | 247 | else: |
paul@119 | 248 | self.value = loaded_context, loaded_ref |
paul@118 | 249 | |
paul@118 | 250 | def StoreAttr(self): |
paul@119 | 251 | context, ref = self.value |
paul@119 | 252 | self.save(ref + self.operand, self.source) |
paul@119 | 253 | |
paul@119 | 254 | def LoadAttrIndex(self): |
paul@118 | 255 | context, ref = self.value |
paul@119 | 256 | code = self.load(ref) # + 0 (the classcode) |
paul@119 | 257 | element = self.objtable[code + self.operand] |
paul@119 | 258 | found_code, found_attr = element |
paul@119 | 259 | if found_code == code: |
paul@119 | 260 | # NOTE: The found context should be tested against the object's context. |
paul@119 | 261 | # NOTE: Compatibility between contexts should be stored in the table, or |
paul@119 | 262 | # NOTE: an incompatible context should be preserved. |
paul@119 | 263 | self.value = ref, found_ref |
paul@119 | 264 | else: |
paul@119 | 265 | # NOTE: This should cause an attribute error. |
paul@119 | 266 | self.value = None |
paul@118 | 267 | |
paul@118 | 268 | def StoreAttrIndex(self): pass |
paul@118 | 269 | |
paul@98 | 270 | def MakeFrame(self): |
paul@117 | 271 | self.invocation_sp_stack.append(len(self.frame_stack)) |
paul@119 | 272 | self.frame_stack.extend([None] * self.operand) |
paul@98 | 273 | |
paul@117 | 274 | def JumpWithFrame(self): |
paul@117 | 275 | self.local_sp_stack.append(self.invocation_sp_stack[-1]) # adopt the invocation frame |
paul@117 | 276 | context, ref = self.value |
paul@119 | 277 | return self.jump(ref.code_location, self.pc + 1) # return to the instruction after this one |
paul@117 | 278 | |
paul@98 | 279 | def DropFrame(self): |
paul@98 | 280 | result = self.pull() |
paul@117 | 281 | self.local_sp_stack.pop() |
paul@117 | 282 | frame = self.invocation_sp_stack.pop() |
paul@117 | 283 | self.frame_stack = self.frame_stack[:frame] # reset stack before call |
paul@98 | 284 | |
paul@117 | 285 | def CheckFrame(self): pass |
paul@117 | 286 | |
paul@98 | 287 | def LoadCallable(self): pass |
paul@98 | 288 | |
paul@98 | 289 | def LoadContext(self): |
paul@117 | 290 | context, ref = self.value |
paul@117 | 291 | self.push((None, context)) |
paul@98 | 292 | |
paul@98 | 293 | def CheckSelf(self): pass |
paul@98 | 294 | |
paul@117 | 295 | def CheckContext(self): pass |
paul@98 | 296 | |
paul@68 | 297 | def Return(self): |
paul@68 | 298 | self.pc = self.pull_pc() |
paul@68 | 299 | |
paul@117 | 300 | def LoadResult(self): |
paul@117 | 301 | self.value = self.result |
paul@117 | 302 | |
paul@117 | 303 | def StoreResult(self): |
paul@117 | 304 | self.result = self.value |
paul@117 | 305 | |
paul@117 | 306 | def LoadException(self): |
paul@117 | 307 | self.value = self.exception |
paul@117 | 308 | |
paul@117 | 309 | def StoreException(self): |
paul@117 | 310 | self.exception = self.value |
paul@117 | 311 | |
paul@117 | 312 | def LoadBoolean(self): |
paul@117 | 313 | self.value = self.status |
paul@98 | 314 | |
paul@118 | 315 | def Jump(self): |
paul@119 | 316 | return self.operand |
paul@98 | 317 | |
paul@68 | 318 | def JumpIfTrue(self): |
paul@117 | 319 | if self.status: |
paul@119 | 320 | return self.operand |
paul@68 | 321 | |
paul@68 | 322 | def JumpIfFalse(self): |
paul@117 | 323 | if not self.status: |
paul@119 | 324 | return self.operand |
paul@68 | 325 | |
paul@68 | 326 | def StoreFrame(self): |
paul@117 | 327 | frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame |
paul@119 | 328 | self.frame_stack[frame + self.operand] = self.value |
paul@68 | 329 | |
paul@68 | 330 | # vim: tabstop=4 expandtab shiftwidth=4 |