1.1 --- a/rsvp.py Sun Jun 12 22:14:36 2011 +0200
1.2 +++ b/rsvp.py Mon Jun 13 00:18:13 2011 +0200
1.3 @@ -133,7 +133,6 @@
1.4 self.exception = None
1.5
1.6 self.instruction = None
1.7 - self.operand = None
1.8 self.status = None
1.9 self.callable = None
1.10
1.11 @@ -192,7 +191,6 @@
1.12 print "Exception", self.exception
1.13 print
1.14 print "Instruction", self.instruction
1.15 - print "Operand", self.operand
1.16 print "Status", self.status
1.17 print "Callable", self.callable
1.18
1.19 @@ -389,9 +387,9 @@
1.20 if not is_input:
1.21 self.counter += 1
1.22 self.cost += instruction.cost
1.23 - self.operand = instruction.get_operand()
1.24 + operand = instruction.get_operand()
1.25 method = self.get_method(instruction)
1.26 - return method()
1.27 + return method(operand)
1.28
1.29 def process_inputs(self):
1.30
1.31 @@ -438,97 +436,101 @@
1.32 def LoadContext(self, operand):
1.33 self.context = operand
1.34
1.35 + def LoadSourceValue(self, operand):
1.36 + self.source_value = operand
1.37 +
1.38 + def LoadSourceContext(self, operand):
1.39 + self.source_context = operand
1.40 +
1.41 # Instructions.
1.42
1.43 - def LoadConst(self):
1.44 - self.LoadContext(self.operand)
1.45 - self.LoadValue(self.operand)
1.46 + def LoadConst(self, operand):
1.47 + self.LoadContext(operand)
1.48 + self.LoadValue(operand)
1.49
1.50 - def LoadClass(self):
1.51 + def LoadClass(self, operand):
1.52 self.LoadContext(PlaceholderContext)
1.53 - self.LoadValue(self.operand)
1.54 + self.LoadValue(operand)
1.55
1.56 - def LoadFunction(self):
1.57 + def LoadFunction(self, operand):
1.58 self.LoadContext(ReplaceableContext)
1.59 - self.LoadValue(self.operand)
1.60 + self.LoadValue(operand)
1.61
1.62 - def LoadName(self):
1.63 + def LoadName(self, operand):
1.64 frame = self.local_sp_stack[-1]
1.65 - data = self.frame_stack[frame + self.operand]
1.66 + data = self.frame_stack[frame + operand]
1.67 self.LoadContext(data.context)
1.68 self.LoadValue(data.ref)
1.69
1.70 - def StoreName(self):
1.71 + def StoreName(self, operand):
1.72 frame = self.local_sp_stack[-1]
1.73 - self.frame_stack[frame + self.operand] = DataValue(self.source_context, self.source_value)
1.74 + self.frame_stack[frame + operand] = DataValue(self.source_context, self.source_value)
1.75
1.76 LoadTemp = LoadName
1.77
1.78 - def StoreTemp(self):
1.79 + def StoreTemp(self, operand):
1.80 frame = self.local_sp_stack[-1]
1.81 - self.frame_stack[frame + self.operand] = DataValue(self.context, self.value)
1.82 + self.frame_stack[frame + operand] = DataValue(self.context, self.value)
1.83
1.84 - def LoadAddress(self):
1.85 + def LoadAddress(self, operand):
1.86 # Preserve context (potentially null).
1.87 - data = self.load(self.operand)
1.88 + data = self.load(operand)
1.89 self.LoadContext(data.context)
1.90 self.LoadValue(data.ref)
1.91
1.92 - def LoadAddressContext(self):
1.93 + def LoadAddressContext(self, operand):
1.94 # Value becomes context.
1.95 - data = self.load(self.operand)
1.96 + data = self.load(operand)
1.97 self.LoadContext(self.value)
1.98 self.LoadValue(data.ref)
1.99
1.100 - def LoadAddressContextCond(self):
1.101 - data = self.load(self.operand)
1.102 + def LoadAddressContextCond(self, operand):
1.103 + data = self.load(operand)
1.104 data = self._LoadAddressContextCond(data.context, data.ref, self.value)
1.105 self.LoadContext(data.context)
1.106 self.LoadValue(data.ref)
1.107
1.108 - def StoreAddress(self):
1.109 + def StoreAddress(self, operand):
1.110 # Preserve context.
1.111 - self.save(self.operand, DataValue(self.source_context, self.source_value))
1.112 + self.save(operand, DataValue(self.source_context, self.source_value))
1.113
1.114 - def StoreAddressContext(self):
1.115 + def StoreAddressContext(self, operand):
1.116 # Overwrite context if null.
1.117 - self._StoreAddressContext(self.operand, self.context, self.value, self.source_context, self.source_value)
1.118 + self._StoreAddressContext(operand, self.context, self.value, self.source_context, self.source_value)
1.119
1.120 - def MakeInstance(self):
1.121 - size = self.operand
1.122 + def MakeInstance(self, size):
1.123 # NOTE: Referencing the instance template.
1.124 addr = self._MakeObject(size, self.value - Library.instance_template_size)
1.125 # Introduce object as context for the new object.
1.126 self.LoadContext(addr)
1.127 self.LoadValue(addr)
1.128
1.129 - def MakeFragment(self):
1.130 - size = self.operand
1.131 + def MakeFragment(self, size):
1.132 # Reserve twice the amount of space.
1.133 addr = self._MakeFragment(size, size * 2)
1.134 # NOTE: Context is not relevant for fragments.
1.135 self.LoadContext(None)
1.136 self.LoadValue(addr)
1.137
1.138 - def LoadAttr(self):
1.139 + def LoadAttr(self, pos):
1.140 # Retrieved context should already be appropriate for the instance.
1.141 # NOTE: Adding 1 to skip any header.
1.142 - data = self.load(self.value + self.operand + 1)
1.143 + data = self.load(self.value + pos + 1)
1.144 self.LoadContext(data.context)
1.145 self.LoadValue(data.ref)
1.146
1.147 - def StoreAttr(self):
1.148 + def StoreAttr(self, pos):
1.149 # Target should already be an instance.
1.150 # NOTE: Adding 1 to skip any header.
1.151 - self.save(self.value + self.operand + 1, DataValue(self.source_context, self.source_value))
1.152 + self.save(self.value + pos + 1, DataValue(self.source_context, self.source_value))
1.153
1.154 - def LoadAttrIndex(self):
1.155 + def LoadAttrIndex(self, index):
1.156 data = self.load(self.value)
1.157 - element = self.objlist[data.classcode + self.operand]
1.158 + element = self.objlist[data.classcode + index]
1.159
1.160 if element is not None:
1.161 attr_index, static_attr, offset = element
1.162 - if attr_index == self.operand:
1.163 + if attr_index == index:
1.164 if static_attr:
1.165 data = self.load(offset) # offset is address of class/module attribute
1.166 else:
1.167 @@ -542,13 +544,13 @@
1.168
1.169 # LoadAttrIndexContext not defined.
1.170
1.171 - def LoadAttrIndexContextCond(self):
1.172 + def LoadAttrIndexContextCond(self, index):
1.173 data = self.load(self.value)
1.174 - element = self.objlist[data.classcode + self.operand]
1.175 + element = self.objlist[data.classcode + index]
1.176
1.177 if element is not None:
1.178 attr_index, static_attr, offset = element
1.179 - if attr_index == self.operand:
1.180 + if attr_index == index:
1.181 if static_attr:
1.182 loaded_data = self.load(offset) # offset is address of class/module attribute
1.183 # Absent attrcode == class/module.
1.184 @@ -563,13 +565,13 @@
1.185 self.exception = self._MakeObject(Library.instance_size, self.attr_error_instance)
1.186 return self.RaiseException()
1.187
1.188 - def StoreAttrIndex(self):
1.189 + def StoreAttrIndex(self, index):
1.190 data = self.load(self.value)
1.191 - element = self.objlist[data.classcode + self.operand]
1.192 + element = self.objlist[data.classcode + index]
1.193
1.194 if element is not None:
1.195 attr_index, static_attr, offset = element
1.196 - if attr_index == self.operand:
1.197 + if attr_index == index:
1.198 if static_attr:
1.199 self._StoreAddressContext(offset, self.context, self.value, self.source_context, self.source_value)
1.200 return
1.201 @@ -582,52 +584,52 @@
1.202
1.203 # NOTE: LoadAttrIndexContext is a possibility if a particular attribute can always be overridden.
1.204
1.205 - def MakeFrame(self):
1.206 + def MakeFrame(self, size):
1.207 self.invocation_sp_stack.append(len(self.frame_stack))
1.208 - self.frame_stack.extend([None] * self.operand)
1.209 + self.frame_stack.extend([None] * size)
1.210
1.211 - def DropFrame(self):
1.212 + def DropFrame(self, operand=None):
1.213 self.local_sp_stack.pop()
1.214 frame = self.invocation_sp_stack.pop()
1.215 del self.frame_stack[frame:] # reset stack before call
1.216
1.217 - def StoreFrame(self):
1.218 + def StoreFrame(self, pos):
1.219 frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame
1.220 - self.frame_stack[frame + self.operand] = DataValue(self.context, self.value)
1.221 + self.frame_stack[frame + pos] = DataValue(self.context, self.value)
1.222
1.223 - def StoreFrameIndex(self):
1.224 + def StoreFrameIndex(self, index):
1.225 frame = self.invocation_sp_stack[-1] # different from the current frame after MakeFrame
1.226 data = self.load(self.value)
1.227 - element = self.paramlist[data.funccode + self.operand]
1.228 + element = self.paramlist[data.funccode + index]
1.229
1.230 if element is not None:
1.231 # NOTE: Need to ensure correct positioning where a context has been generated.
1.232 param_index, offset = element
1.233 - if param_index == self.operand:
1.234 + if param_index == index:
1.235 self.frame_stack[frame + offset] = DataValue(self.source_context, self.source_value)
1.236 return
1.237
1.238 self.exception = self._MakeObject(Library.instance_size, self.type_error_instance)
1.239 return self.RaiseException()
1.240
1.241 - def LoadCallable(self):
1.242 + def LoadCallable(self, operand=None):
1.243 data = self.load(self.value)
1.244 self.callable = data.codeaddr
1.245
1.246 - def StoreCallable(self):
1.247 + def StoreCallable(self, operand=None):
1.248 # NOTE: Should improve the representation and permit direct saving.
1.249 data = self.load(self.value)
1.250 self.save(self.value, data.with_callable(self.callable))
1.251
1.252 - def LoadContextIntoValue(self):
1.253 + def LoadContextIntoValue(self, operand=None):
1.254 # NOTE: Omission of the context of the context would make things like
1.255 # NOTE: self() inside methods impossible.
1.256 self.LoadValue(self.context)
1.257
1.258 - def CheckContext(self):
1.259 + def CheckContext(self, operand=None):
1.260 self.status = self.value is not ReplaceableContext
1.261
1.262 - def CheckClass(self):
1.263 + def CheckClass(self, operand=None):
1.264 if self.value in (ReplaceableContext, PlaceholderContext):
1.265 self.status = 0
1.266 return
1.267 @@ -641,8 +643,8 @@
1.268
1.269 self.status = data.attrcode is None # absent attrcode == class
1.270
1.271 - def CheckFrame(self):
1.272 - (nargs, ndefaults) = self.operand
1.273 + def CheckFrame(self, operand):
1.274 + (nargs, ndefaults) = operand
1.275
1.276 # The frame is actually installed as the locals.
1.277 # Retrieve the context from the first local.
1.278 @@ -651,12 +653,11 @@
1.279 nlocals = len(self.frame_stack[frame:])
1.280
1.281 if not ((nargs - ndefaults) <= nlocals):
1.282 - raise Exception, "CheckFrame %r (%r <= %r <= %r)" % (self.operand, nargs - ndefaults, nlocals, nargs)
1.283 + raise Exception, "CheckFrame %r (%r <= %r <= %r)" % (operand, nargs - ndefaults, nlocals, nargs)
1.284 self.exception = self._MakeObject(Library.instance_size, self.type_error_instance)
1.285 return self.RaiseException()
1.286
1.287 - def CheckExtra(self):
1.288 - nargs = self.operand
1.289 + def CheckExtra(self, nargs):
1.290
1.291 # The frame is actually installed as the locals.
1.292 # Retrieve the context from the first local.
1.293 @@ -669,8 +670,8 @@
1.294 if nlocals == nargs:
1.295 self.frame_stack.extend([None]) # ExtendFrame(1)
1.296
1.297 - def FillDefaults(self):
1.298 - (nargs, ndefaults) = self.operand
1.299 + def FillDefaults(self, operand):
1.300 + (nargs, ndefaults) = operand
1.301
1.302 # The frame is actually installed as the locals.
1.303
1.304 @@ -689,8 +690,7 @@
1.305 default += 1
1.306 pos += 1
1.307
1.308 - def CopyExtra(self):
1.309 - start = self.operand
1.310 + def CopyExtra(self, start):
1.311
1.312 # The frame is the source of the extra arguments.
1.313
1.314 @@ -712,64 +712,63 @@
1.315 self.LoadContext(ref)
1.316 self.LoadValue(ref)
1.317
1.318 - def CheckInstance(self):
1.319 + def CheckInstance(self, operand=None):
1.320 # For the 'self' parameter in an invoked function, the proposed context
1.321 # ('self') is checked against the target's context.
1.322 self.status = self._CheckInstance(self.value, self.source_value)
1.323
1.324 - def JumpInFrame(self):
1.325 + def JumpInFrame(self, operand=None):
1.326 codeaddr = self.callable
1.327 return self.jump(codeaddr, self.pc + 1) # return to the instruction after this one
1.328
1.329 - def JumpWithFrame(self):
1.330 + def JumpWithFrame(self, operand=None):
1.331 codeaddr = self.callable
1.332 self.local_sp_stack.append(self.invocation_sp_stack[-1]) # adopt the invocation frame
1.333 return self.jump(codeaddr, self.pc + 1) # return to the instruction after this one
1.334
1.335 - def JumpWithFrameDirect(self):
1.336 - operand = self.operand
1.337 + def JumpWithFrameDirect(self, addr):
1.338 self.local_sp_stack.append(self.invocation_sp_stack[-1]) # adopt the invocation frame
1.339 - return self.jump(operand, self.pc + 1) # return to the instruction after this one
1.340 + return self.jump(addr, self.pc + 1) # return to the instruction after this one
1.341
1.342 - def ExtendFrame(self):
1.343 - self.frame_stack.extend([None] * self.operand)
1.344 + def ExtendFrame(self, size):
1.345 + self.frame_stack.extend([None] * size)
1.346
1.347 - def AdjustFrame(self):
1.348 - self.invocation_sp_stack[-1] += self.operand
1.349 + def AdjustFrame(self, size):
1.350 + self.invocation_sp_stack[-1] += size
1.351
1.352 - def Return(self):
1.353 + def Return(self, operand=None):
1.354 return self.pull_pc()
1.355
1.356 - def LoadResult(self):
1.357 + def LoadResultIntoValue(self, operand=None):
1.358 self.LoadContext(self.result_context)
1.359 self.LoadValue(self.result_value)
1.360
1.361 - def StoreResult(self):
1.362 + def LoadValueIntoResult(self, operand=None):
1.363 self.result_context = self.context
1.364 self.result_value = self.value
1.365
1.366 - def Jump(self):
1.367 - return self.operand
1.368 + def Jump(self, addr):
1.369 + return addr
1.370
1.371 - def JumpIfTrue(self):
1.372 + def JumpIfTrue(self, addr):
1.373 if self.status:
1.374 - return self.operand
1.375 + return addr
1.376
1.377 - def JumpIfFalse(self):
1.378 + def JumpIfFalse(self, addr):
1.379 if not self.status:
1.380 - return self.operand
1.381 + return addr
1.382
1.383 - def LoadException(self):
1.384 + def LoadException(self, operand=None):
1.385 self.LoadContext(self.exception)
1.386 self.LoadValue(self.exception)
1.387
1.388 - def StoreException(self):
1.389 + def StoreException(self, operand=None):
1.390 self.exception = self.value
1.391
1.392 - def ClearException(self):
1.393 + def ClearException(self, operand=None):
1.394 self.exception = None
1.395
1.396 - def RaiseException(self):
1.397 + def RaiseException(self, operand=None):
1.398 # NOTE: Adding the program counter as the first attribute after __class__.
1.399 self.save(self.exception + Library.instance_data_offset, self.pc)
1.400 # Jumping to the current handler.
1.401 @@ -777,14 +776,13 @@
1.402 raise Exception
1.403 return self.handler_stack[-1]
1.404
1.405 - def PushHandler(self):
1.406 - self.handler_stack.append(self.operand)
1.407 + def PushHandler(self, addr):
1.408 + self.handler_stack.append(addr)
1.409 self.handler_local_sp_stack.append(len(self.local_sp_stack))
1.410 self.handler_invocation_sp_stack.append(len(self.invocation_sp_stack))
1.411 self.handler_pc_stack.append(len(self.pc_stack))
1.412
1.413 - def PopHandler(self):
1.414 - nframes = self.operand
1.415 + def PopHandler(self, nframes):
1.416 # Get the new local frame pointer and PC stack references.
1.417 local_sp_top = self.handler_local_sp_stack[-nframes]
1.418 invocation_sp_top = self.handler_invocation_sp_stack[-nframes]
1.419 @@ -801,19 +799,19 @@
1.420 del self.handler_invocation_sp_stack[-nframes:]
1.421 del self.handler_stack[-nframes:]
1.422
1.423 - def CheckException(self):
1.424 + def CheckException(self, operand=None):
1.425 self.status = self.exception is not None and self._CheckInstance(self.exception, self.value)
1.426
1.427 - def TestIdentity(self):
1.428 + def TestIdentity(self, operand=None):
1.429 self.status = self.value == self.source_value
1.430
1.431 - def TestIdentityAddress(self):
1.432 - self.status = self.value == self.operand
1.433 + def TestIdentityAddress(self, addr):
1.434 + self.status = self.value == addr
1.435
1.436 # LoadBoolean is implemented in the generated code.
1.437 # StoreBoolean is implemented by testing against the True value.
1.438
1.439 - def InvertBoolean(self):
1.440 + def InvertBoolean(self, operand=None):
1.441 self.status = not self.status
1.442
1.443 # Common implementation details.