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@68 | 29 | * Value stack |
paul@68 | 30 | * Frame stack (containing pointers to the value stack) |
paul@68 | 31 | * Current frame and arguments pointers |
paul@68 | 32 | |
paul@68 | 33 | The memory contains constants, global variable references and program code. |
paul@68 | 34 | |
paul@68 | 35 | The PC stack contains the return address associated with each function |
paul@68 | 36 | invocation. |
paul@68 | 37 | |
paul@68 | 38 | The value stack contains references to objects that are being manipulated, along |
paul@68 | 39 | with pointers to previous stack frames. |
paul@68 | 40 | |
paul@68 | 41 | The frame stack tracks the position of stack frames within the value stack. |
paul@68 | 42 | """ |
paul@68 | 43 | |
paul@68 | 44 | class IllegalInstruction(Exception): |
paul@68 | 45 | pass |
paul@68 | 46 | |
paul@68 | 47 | class IllegalAddress(Exception): |
paul@68 | 48 | pass |
paul@68 | 49 | |
paul@68 | 50 | class EmptyPCStack(Exception): |
paul@68 | 51 | pass |
paul@68 | 52 | |
paul@92 | 53 | class EmptyFrameStack(Exception): |
paul@68 | 54 | pass |
paul@68 | 55 | |
paul@68 | 56 | class RSVPMachine: |
paul@68 | 57 | |
paul@68 | 58 | "A really simple virtual processor." |
paul@68 | 59 | |
paul@68 | 60 | def __init__(self, memory, pc=None, debug=0): |
paul@68 | 61 | |
paul@68 | 62 | """ |
paul@68 | 63 | Initialise the processor with a 'memory' (a list of values containing |
paul@68 | 64 | instructions and data) and the optional program counter 'pc'. |
paul@68 | 65 | """ |
paul@68 | 66 | |
paul@68 | 67 | self.memory = memory |
paul@68 | 68 | self.pc = pc or 0 |
paul@68 | 69 | self.debug = debug |
paul@68 | 70 | self.pc_stack = [] |
paul@68 | 71 | self.value_stack = [] |
paul@68 | 72 | self.frame_stack = [] |
paul@68 | 73 | self.frame_sp = 0 |
paul@68 | 74 | |
paul@68 | 75 | def load(self, address): |
paul@68 | 76 | |
paul@68 | 77 | "Return the value at the given 'address'." |
paul@68 | 78 | |
paul@68 | 79 | try: |
paul@68 | 80 | return self.memory[address] |
paul@68 | 81 | except IndexError: |
paul@68 | 82 | raise IllegalAddress, address |
paul@68 | 83 | |
paul@68 | 84 | def save(self, address, value): |
paul@68 | 85 | |
paul@68 | 86 | "Save to the given 'address' the specified 'value'." |
paul@68 | 87 | |
paul@68 | 88 | try: |
paul@68 | 89 | self.memory[address] = value |
paul@68 | 90 | except IndexError: |
paul@68 | 91 | raise IllegalAddress, address |
paul@68 | 92 | |
paul@68 | 93 | def new(self, size): |
paul@68 | 94 | |
paul@68 | 95 | """ |
paul@68 | 96 | Allocate space of the given 'size', returning the address of the space. |
paul@68 | 97 | """ |
paul@68 | 98 | |
paul@68 | 99 | addr = len(self.memory) |
paul@68 | 100 | for i in range(0, size): |
paul@68 | 101 | self.memory.append(None) |
paul@68 | 102 | return addr |
paul@68 | 103 | |
paul@98 | 104 | def stack_save(self, n, data): |
paul@68 | 105 | |
paul@98 | 106 | "Save to location 'n' on the value stack the given 'data'." |
paul@68 | 107 | |
paul@68 | 108 | self.value_stack.append(data) |
paul@68 | 109 | |
paul@98 | 110 | def stack_load(self, n): |
paul@68 | 111 | |
paul@98 | 112 | "Load a value from the value stack at location 'n'." |
paul@68 | 113 | |
paul@68 | 114 | return self.value_stack.pop() |
paul@68 | 115 | |
paul@68 | 116 | def push_pc(self, data): |
paul@68 | 117 | |
paul@68 | 118 | "Push 'data' onto the PC stack." |
paul@68 | 119 | |
paul@68 | 120 | self.pc_stack.append(data) |
paul@68 | 121 | |
paul@68 | 122 | def pull_pc(self): |
paul@68 | 123 | |
paul@68 | 124 | "Pull a value from the PC stack and return it." |
paul@68 | 125 | |
paul@68 | 126 | try: |
paul@68 | 127 | return self.pc_stack.pop() |
paul@68 | 128 | except IndexError: |
paul@68 | 129 | raise EmptyPCStack |
paul@68 | 130 | |
paul@68 | 131 | def add_frame(self, data): |
paul@68 | 132 | |
paul@68 | 133 | "Add 'data' to the frame stack without updating the stack pointer." |
paul@68 | 134 | |
paul@68 | 135 | self.frame_stack.append(data) |
paul@68 | 136 | |
paul@68 | 137 | def push_frame(self, data): |
paul@68 | 138 | |
paul@68 | 139 | "Push 'data' onto the frame stack." |
paul@68 | 140 | |
paul@68 | 141 | self.frame_stack.append(data) |
paul@68 | 142 | self.frame_sp += 1 |
paul@68 | 143 | |
paul@68 | 144 | def pull_frame(self): |
paul@68 | 145 | |
paul@68 | 146 | "Pull a value from the frame stack and return it." |
paul@68 | 147 | |
paul@68 | 148 | try: |
paul@68 | 149 | self.frame_sp -= 1 |
paul@68 | 150 | return self.frame_stack.pop() |
paul@68 | 151 | except IndexError: |
paul@92 | 152 | raise EmptyFrameStack |
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@90 | 168 | instruction = self.load(self.pc).__class__.__name__ |
paul@90 | 169 | if self.debug: |
paul@90 | 170 | print "%8d %s" % (self.pc, instruction) |
paul@90 | 171 | method = getattr(self, instruction, None) |
paul@90 | 172 | if method is None: |
paul@92 | 173 | raise IllegalInstruction, (self.pc, instruction) |
paul@90 | 174 | else: |
paul@90 | 175 | method() |
paul@90 | 176 | |
paul@68 | 177 | def jump(self, addr, next): |
paul@68 | 178 | |
paul@68 | 179 | """ |
paul@68 | 180 | Jump to the subroutine at (or identified by) 'addr'. If 'addr' |
paul@68 | 181 | identifies a library function then invoke the library function and set |
paul@68 | 182 | PC to 'next' afterwards; otherwise, set PC to 'addr'. |
paul@68 | 183 | """ |
paul@68 | 184 | |
paul@68 | 185 | if isinstance(addr, str): |
paul@68 | 186 | getattr(self, addr)() |
paul@68 | 187 | self.pc = next |
paul@68 | 188 | else: |
paul@68 | 189 | self.push_pc(self.pc + 2) |
paul@68 | 190 | self.pc = addr |
paul@68 | 191 | |
paul@68 | 192 | # Instructions. |
paul@68 | 193 | |
paul@98 | 194 | def LoadConst(self): |
paul@92 | 195 | |
paul@92 | 196 | """ |
paul@98 | 197 | LoadConst addr |
paul@98 | 198 | Load the reference to memory: get the address addr, push the value onto |
paul@98 | 199 | the stack as a value without context. |
paul@98 | 200 | |
paul@98 | 201 | This is useful for loading constants. |
paul@98 | 202 | NOTE: This assumes that constants are encoded with context. |
paul@92 | 203 | """ |
paul@92 | 204 | |
paul@98 | 205 | op = self.load(self.pc) |
paul@98 | 206 | addr = op.get_operand() |
paul@98 | 207 | value = (addr, None) |
paul@98 | 208 | for result in op.results: |
paul@98 | 209 | self.stack_save(result.level, value) |
paul@92 | 210 | self.pc += 1 |
paul@68 | 211 | |
paul@68 | 212 | def LoadName(self): |
paul@68 | 213 | |
paul@68 | 214 | """ |
paul@68 | 215 | LoadName #n |
paul@68 | 216 | Load from position n in frame: get position n from the current stack |
paul@68 | 217 | frame, push the retrieved value onto the stack. |
paul@68 | 218 | """ |
paul@68 | 219 | |
paul@90 | 220 | n = self.load(self.pc).get_operand() |
paul@68 | 221 | frame = self.frame_stack[self.frame_sp] |
paul@68 | 222 | self.push(self.value_stack[frame + n]) |
paul@90 | 223 | self.pc += 1 |
paul@68 | 224 | |
paul@68 | 225 | def StoreName(self): |
paul@68 | 226 | |
paul@68 | 227 | """ |
paul@68 | 228 | StoreName #n |
paul@68 | 229 | Save to position n in frame: pull a value from the stack and set it as |
paul@68 | 230 | position n in the current stack frame. |
paul@68 | 231 | """ |
paul@68 | 232 | |
paul@90 | 233 | n = self.load(self.pc).get_operand() |
paul@68 | 234 | frame = self.frame_stack[self.frame_sp] |
paul@68 | 235 | self.value_stack[frame + n] = self.pull() |
paul@90 | 236 | self.pc += 1 |
paul@68 | 237 | |
paul@72 | 238 | LoadTemp = LoadName |
paul@72 | 239 | StoreTemp = StoreName |
paul@72 | 240 | |
paul@68 | 241 | def LoadAttr(self): |
paul@68 | 242 | |
paul@68 | 243 | """ |
paul@68 | 244 | LoadAttr #n |
paul@68 | 245 | Load from position n in reference: get the contents of position n in the |
paul@68 | 246 | memory referenced by the value on the top of the stack, replacing the |
paul@68 | 247 | value on the top of the stack with the retrieved value. |
paul@68 | 248 | """ |
paul@68 | 249 | |
paul@90 | 250 | n = self.load(self.pc).get_operand() |
paul@90 | 251 | context, ref = self.pull() |
paul@90 | 252 | value = self.load(ref + n) |
paul@90 | 253 | self.push(value) |
paul@90 | 254 | self.pc += 1 |
paul@68 | 255 | |
paul@68 | 256 | def StoreAttr(self): |
paul@68 | 257 | |
paul@68 | 258 | """ |
paul@68 | 259 | StoreAttr #n |
paul@68 | 260 | Save to position n in reference: pull a value from the stack and save it |
paul@68 | 261 | to position n in the memory referenced by the next value on the stack, |
paul@68 | 262 | also removing the next value on the stack. |
paul@68 | 263 | """ |
paul@68 | 264 | |
paul@90 | 265 | n = self.load(self.pc).get_operand() |
paul@68 | 266 | value = self.pull() |
paul@90 | 267 | context, ref = self.pull() |
paul@90 | 268 | self.save(ref + n, value) |
paul@90 | 269 | self.pc += 1 |
paul@68 | 270 | |
paul@98 | 271 | def LoadAttrIndex(self): pass |
paul@98 | 272 | |
paul@98 | 273 | def StoreAttrIndex(self): pass |
paul@98 | 274 | |
paul@72 | 275 | def LoadAddress(self): |
paul@72 | 276 | |
paul@72 | 277 | """ |
paul@90 | 278 | LoadAddress addr |
paul@90 | 279 | Load from addr: get the contents of the location in memory referenced by |
paul@90 | 280 | addr, adding the retrieved value to the top of the stack. |
paul@72 | 281 | """ |
paul@72 | 282 | |
paul@90 | 283 | addr = self.load(self.pc).get_operand() |
paul@90 | 284 | value = self.load(addr) |
paul@90 | 285 | self.push(value) |
paul@90 | 286 | self.pc += 1 |
paul@72 | 287 | |
paul@72 | 288 | def StoreAddress(self): |
paul@72 | 289 | |
paul@72 | 290 | """ |
paul@90 | 291 | StoreAddress addr |
paul@90 | 292 | Save to addr: pull a value from the stack and save it to the location in |
paul@90 | 293 | memory referenced by addr, also removing the value on the top of the |
paul@90 | 294 | stack. |
paul@72 | 295 | """ |
paul@72 | 296 | |
paul@90 | 297 | addr = self.load(self.pc).get_operand() |
paul@72 | 298 | value = self.pull() |
paul@90 | 299 | self.save(addr, value) |
paul@90 | 300 | self.pc += 1 |
paul@72 | 301 | |
paul@98 | 302 | def MakeFrame(self): |
paul@98 | 303 | n = self.load(self.pc).get_operand() |
paul@98 | 304 | top = len(self.value_stack) |
paul@98 | 305 | self.add_frame(top) |
paul@98 | 306 | while n > 0: |
paul@98 | 307 | self.push(None) |
paul@98 | 308 | n -= 1 |
paul@98 | 309 | self.pc += 1 |
paul@98 | 310 | |
paul@98 | 311 | def DropFrame(self): |
paul@98 | 312 | result = self.pull() |
paul@98 | 313 | frame = self.pull_frame() |
paul@98 | 314 | self.value_stack = self.value_stack[:frame] # reset stack before call |
paul@98 | 315 | self.push(result) |
paul@98 | 316 | self.pc += 1 |
paul@98 | 317 | |
paul@98 | 318 | def LoadCallable(self): pass |
paul@98 | 319 | |
paul@98 | 320 | def LoadContext(self): |
paul@98 | 321 | |
paul@98 | 322 | """ |
paul@98 | 323 | LoadContext |
paul@98 | 324 | Load context from top of stack: get the context from the value on the |
paul@98 | 325 | top of the stack, pushing it onto the stack. |
paul@98 | 326 | """ |
paul@98 | 327 | |
paul@98 | 328 | attr = self.value_stack[-1] |
paul@98 | 329 | self.push((attr[1], None)) |
paul@98 | 330 | self.pc += 1 |
paul@98 | 331 | |
paul@98 | 332 | def CheckFrame(self): pass |
paul@98 | 333 | |
paul@98 | 334 | def CheckSelf(self): pass |
paul@98 | 335 | |
paul@98 | 336 | def CheckContext(self): |
paul@98 | 337 | |
paul@98 | 338 | """ |
paul@98 | 339 | CheckContext |
paul@98 | 340 | Check the context: check the context on the top of the stack |
paul@98 | 341 | """ |
paul@98 | 342 | |
paul@98 | 343 | attr = self.value_stack[-1] |
paul@98 | 344 | # NOTE: To be written. |
paul@98 | 345 | self.pc += 1 |
paul@98 | 346 | |
paul@98 | 347 | def JumpWithFrame(self): |
paul@98 | 348 | attr = self.pull() |
paul@98 | 349 | self.frame_sp += 1 # adopt the added frame |
paul@98 | 350 | target_location = attr[0] |
paul@98 | 351 | target = self.load(target_location) |
paul@98 | 352 | self.jump(target.code_location, self.pc + 1) # return to the instruction after this one |
paul@98 | 353 | |
paul@68 | 354 | def Return(self): |
paul@68 | 355 | |
paul@68 | 356 | """ |
paul@68 | 357 | Return |
paul@68 | 358 | Return to the address of the caller: pull a value from the PC stack and |
paul@68 | 359 | set it in the PC. |
paul@68 | 360 | """ |
paul@68 | 361 | |
paul@68 | 362 | self.pc = self.pull_pc() |
paul@68 | 363 | |
paul@98 | 364 | def LoadResult(self): pass |
paul@98 | 365 | |
paul@98 | 366 | def Jump(self): pass |
paul@98 | 367 | |
paul@68 | 368 | def JumpIfTrue(self): |
paul@68 | 369 | |
paul@68 | 370 | """ |
paul@68 | 371 | JumpIfTrue addr |
paul@68 | 372 | Jump to address addr if true: pull a value from the stack and if it |
paul@68 | 373 | represents a true value, jump to address addr. |
paul@68 | 374 | """ |
paul@68 | 375 | |
paul@90 | 376 | addr = self.load(self.pc).get_operand() |
paul@68 | 377 | value = self.pull() |
paul@68 | 378 | if value: |
paul@68 | 379 | self.pc = addr |
paul@68 | 380 | else: |
paul@90 | 381 | self.pc += 1 |
paul@68 | 382 | |
paul@68 | 383 | def JumpIfFalse(self): |
paul@68 | 384 | |
paul@68 | 385 | """ |
paul@68 | 386 | JumpIfFalse addr |
paul@68 | 387 | Jump to address addr if false: pull a value from the stack and if it |
paul@68 | 388 | represents a false value, jump to address addr. |
paul@68 | 389 | """ |
paul@68 | 390 | |
paul@90 | 391 | addr = self.load(self.pc).get_operand() |
paul@68 | 392 | value = self.pull() |
paul@68 | 393 | if not value: |
paul@68 | 394 | self.pc = addr |
paul@68 | 395 | else: |
paul@90 | 396 | self.pc += 1 |
paul@68 | 397 | |
paul@68 | 398 | def StoreFrame(self): |
paul@68 | 399 | |
paul@68 | 400 | """ |
paul@68 | 401 | StoreFrame #n |
paul@68 | 402 | Move the value from the top of the stack to the argument in position n: |
paul@68 | 403 | pull a value from the stack and store it in the argument frame at the |
paul@68 | 404 | given position. |
paul@68 | 405 | """ |
paul@68 | 406 | |
paul@90 | 407 | pos = self.load(self.pc).get_operand() |
paul@68 | 408 | value = self.pull() |
paul@68 | 409 | frame = self.frame_stack[-1] # different from the current frame after MakeFrame |
paul@68 | 410 | self.value_stack[frame + pos] = value |
paul@90 | 411 | self.pc += 1 |
paul@68 | 412 | |
paul@68 | 413 | # Library functions. |
paul@68 | 414 | |
paul@68 | 415 | def rsvp___printnl(self): |
paul@68 | 416 | print self.load(self.value_stack[-1]) |
paul@68 | 417 | |
paul@68 | 418 | def rsvp___print(self): |
paul@68 | 419 | print self.load(self.value_stack[-1]), |
paul@68 | 420 | |
paul@68 | 421 | def int_____gt__(self): |
paul@68 | 422 | self.push(self.load(self.value_stack[-2]) > self.load(self.value_stack[-1])) |
paul@68 | 423 | |
paul@68 | 424 | def int_____sub__(self): |
paul@68 | 425 | result = self.load(self.value_stack[-2]) - self.load(self.value_stack[-1]) |
paul@68 | 426 | addr = self.new(1) |
paul@68 | 427 | self.push(addr) |
paul@68 | 428 | self.save(addr, result) |
paul@68 | 429 | |
paul@68 | 430 | def int_____mul__(self): |
paul@68 | 431 | result = self.load(self.value_stack[-2]) * self.load(self.value_stack[-1]) |
paul@68 | 432 | addr = self.new(1) |
paul@68 | 433 | self.push(addr) |
paul@68 | 434 | self.save(addr, result) |
paul@68 | 435 | |
paul@68 | 436 | # vim: tabstop=4 expandtab shiftwidth=4 |