1.1 --- a/rsvp.py Mon Sep 01 01:32:32 2008 +0200
1.2 +++ b/rsvp.py Thu Sep 04 19:44:39 2008 +0200
1.3 @@ -58,7 +58,7 @@
1.4
1.5 "A really simple virtual processor."
1.6
1.7 - def __init__(self, memory, objtable, paramtable, pc=None, debug=0):
1.8 + def __init__(self, memory, objlist, paramlist, pc=None, debug=0):
1.9
1.10 """
1.11 Initialise the processor with a 'memory' (a list of values containing
1.12 @@ -66,8 +66,8 @@
1.13 """
1.14
1.15 self.memory = memory
1.16 - self.objtable = objtable
1.17 - self.paramtable = paramtable
1.18 + self.objlist = objlist
1.19 + self.paramlist = paramlist
1.20 self.pc = pc or 0
1.21 self.debug = debug
1.22
1.23 @@ -91,7 +91,7 @@
1.24 self.exception = None
1.25
1.26 def dump(self):
1.27 - print "PC", self.pc
1.28 + print "PC", self.pc, "->", self.load(self.pc)
1.29 print "PC stack", self.pc_stack
1.30 print "Frame stack", self.frame_stack
1.31 print "Local stack pointers", self.local_sp_stack
1.32 @@ -107,6 +107,10 @@
1.33 print "Result", self.result
1.34 print "Exception", self.exception
1.35
1.36 + def step(self):
1.37 + self.execute()
1.38 + self.dump()
1.39 +
1.40 def load(self, address):
1.41
1.42 "Return the value at the given 'address'."
1.43 @@ -226,7 +230,7 @@
1.44 getattr(self, addr)()
1.45 return next
1.46 else:
1.47 - self.push_pc(self.pc + 2)
1.48 + self.push_pc(self.pc + 1)
1.49 return addr
1.50
1.51 # Instructions.
1.52 @@ -259,8 +263,14 @@
1.53 self.save(self.operand, self.value)
1.54
1.55 def MakeObject(self):
1.56 + size = self.operand
1.57 + context, ref = self.value
1.58 + classcode, attrcode, codeaddr, codedetails = self.load(ref)
1.59 + addr = self.new(size)
1.60 + # Set the header to resemble the class.
1.61 + self.save(addr, (classcode, attrcode, None, None)) # NOTE: __call__ method not yet provided.
1.62 # Introduce null context for new object.
1.63 - self.value = None, self.new(self.operand)
1.64 + self.value = None, addr
1.65
1.66 def LoadAttr(self):
1.67 context, ref = self.value
1.68 @@ -275,7 +285,7 @@
1.69 def LoadAttrIndex(self):
1.70 context, ref = self.value
1.71 classcode, attrcode, codeaddr, codedetails = self.load(ref)
1.72 - element = self.objtable[classcode + self.operand]
1.73 + element = self.objlist[classcode + self.operand]
1.74 attr_index, class_attr, replace_context, offset = element
1.75 if attr_index == self.operand:
1.76 if class_attr:
1.77 @@ -293,7 +303,7 @@
1.78 def StoreAttrIndex(self):
1.79 context, ref = self.value
1.80 classcode, attrcode, codeaddr, codedetails = self.load(ref)
1.81 - element = self.objtable[classcode + self.operand]
1.82 + element = self.objlist[classcode + self.operand]
1.83 attr_index, class_attr, replace_context, offset = element
1.84 if attr_index == self.operand:
1.85 if class_attr:
1.86 @@ -327,7 +337,7 @@
1.87 def StoreFrameIndex(self):
1.88 frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame
1.89 classcode, attrcode, codeaddr, codedetails = self.load(ref)
1.90 - element = self.objtable[classcode + self.operand]
1.91 + element = self.objlist[classcode + self.operand]
1.92 attr_index, offset = element
1.93 if attr_index == self.operand:
1.94 self.frame_stack[frame + offset] = self.value
1.95 @@ -359,11 +369,11 @@
1.96 # Support sliding of the frame to exclude any inappropriate context.
1.97
1.98 if context is None:
1.99 - frame = frame[1:]
1.100 + self.invocation_sp_stack[-1] += 1
1.101 operand -= 1
1.102 else:
1.103 if contexttype == self.typetype:
1.104 - frame = frame[1:]
1.105 + self.invocation_sp_stack[-1] += 1
1.106 operand -= 1
1.107
1.108 nargs, ndefaults = codedetails
1.109 @@ -383,7 +393,7 @@
1.110
1.111 # Find the table entry for the descendant.
1.112
1.113 - element = self.objtable[target_classcode + attrcode]
1.114 + element = self.objlist[target_classcode + attrcode]
1.115 attr_index, class_attr, replace_context, offset = element
1.116 if attr_index == attrcode:
1.117 self.status = 1
1.118 @@ -391,23 +401,23 @@
1.119 self.status = 0
1.120
1.121 def JumpWithFrame(self):
1.122 + codeaddr, codedetails = self.callable
1.123 self.local_sp_stack.append(self.invocation_sp_stack[-1]) # adopt the invocation frame
1.124 - return self.jump(self.callable, self.pc + 1) # return to the instruction after this one
1.125 + return self.jump(codeaddr, self.pc + 1) # return to the instruction after this one
1.126
1.127 def ExtendFrame(self):
1.128 - frame = self.local_sp_stack[-1]
1.129 - frame.extend([None] * self.operand)
1.130 + self.frame_stack.extend([None] * self.operand)
1.131
1.132 def AdjustFrame(self):
1.133 if self.operand > 0:
1.134 - frame.append([None] * self.operand)
1.135 + self.frame_stack.append([None] * self.operand)
1.136 elif self.operand == -1:
1.137 self.invocation_sp_stack[-1] -= 1
1.138 else:
1.139 raise Exception, "AdjustFrame %r" % self.operand
1.140
1.141 def Return(self):
1.142 - self.pc = self.pull_pc()
1.143 + return self.pull_pc()
1.144
1.145 def LoadResult(self):
1.146 self.value = self.result