1.1 --- a/micropython/rsvp.py Fri Feb 26 01:56:26 2010 +0100
1.2 +++ b/micropython/rsvp.py Wed Mar 03 00:26:00 2010 +0100
1.3 @@ -382,99 +382,256 @@
1.4
1.5 # Access to stored constant data.
1.6
1.7 -class LoadConst(Address): "Load the constant or module from the specified location."
1.8 -class LoadClass(Address): "Load the class from the specified location."
1.9 -class LoadFunction(Address): "Load the function from the specified location."
1.10 +class LoadConst(Address):
1.11 + "Load the constant or module from the specified location."
1.12 + cost = 1
1.13 +
1.14 +class LoadClass(Address):
1.15 + "Load the class from the specified location."
1.16 + cost = 1
1.17 +
1.18 +class LoadFunction(Address):
1.19 + "Load the function from the specified location."
1.20 + cost = 1
1.21
1.22 # Access within an invocation frame.
1.23
1.24 -class LoadName(FR): "Load the current value from the given local attribute/variable."
1.25 -class StoreName(FR): "Store the source value into the given local attribute/variable."
1.26 -class LoadTemp(Immediate): "Load the current value from the given temporary location."
1.27 -class StoreTemp(Immediate): "Store the current value into the given temporary location."
1.28 +class LoadName(FR):
1.29 + "Load the current value from the given local attribute/variable."
1.30 + cost = 2
1.31 +
1.32 +class StoreName(FR):
1.33 + "Store the source value into the given local attribute/variable."
1.34 + cost = 2
1.35 +
1.36 +class LoadTemp(Immediate):
1.37 + "Load the current value from the given temporary location."
1.38 + cost = 2
1.39 +
1.40 +class StoreTemp(Immediate):
1.41 + "Store the current value into the given temporary location."
1.42 + cost = 2
1.43
1.44 # Access to static data.
1.45
1.46 -class LoadAddress(Address): "Load the current value from the given fixed attribute address."
1.47 -class StoreAddress(Address): "Store the source value into the given fixed attribute address."
1.48 -class LoadAddressContext(Address): "Load the current value from the given fixed attribute address, using the current value as context."
1.49 -class StoreAddressContext(Address): "Store the current value into the given fixed attribute address, using the current value as context."
1.50 +class LoadAddress(Address):
1.51 + "Load the current value from the given fixed attribute address."
1.52 + cost = 1
1.53 +
1.54 +class StoreAddress(Address):
1.55 + "Store the source value into the given fixed attribute address."
1.56 + cost = 1
1.57 +
1.58 +class LoadAddressContext(Address):
1.59 + "Load the current value from the given fixed attribute address, using the current value as context."
1.60 + cost = 2
1.61 +
1.62 +class StoreAddressContext(Address):
1.63 + "Store the current value into the given fixed attribute address, using the current value as context."
1.64 + cost = 2
1.65 +
1.66 class LoadAddressContextCond(Address):
1.67 - """Load the current value from the given fixed attribute address, only using the current value as
1.68 - context if the attribute is compatible."""
1.69 -class MakeInstance(Immediate): "Make a new instance using the current value as a reference to a template."
1.70 -class MakeFragment(Immediate): "Make a new list fragment."
1.71 + """
1.72 + Load the current value from the given fixed attribute address, only using the current value as
1.73 + context if the attribute is compatible.
1.74 + """
1.75 + cost = 4
1.76 +
1.77 +class MakeInstance(Immediate):
1.78 + "Make a new instance using the current value as a reference to a template."
1.79 + cost = 5
1.80 +
1.81 +class MakeFragment(Immediate):
1.82 + "Make a new list fragment."
1.83 + cost = 5
1.84
1.85 # Access to address-relative data. (LoadAttrIndexContext not defined.)
1.86
1.87 -class LoadAttr(AR): "Load into the current value the given attribute of the object referenced by the current value."
1.88 -class StoreAttr(AR): "Store the source value into the given attribute of the object referenced by the current value."
1.89 -class LoadAttrIndex(Immediate): "Load into the current value the attribute of the current value with the given index."
1.90 -class StoreAttrIndex(Immediate): "Store the source value into the attribute of the current value with the given index."
1.91 +class LoadAttr(AR):
1.92 + "Load into the current value the given attribute of the object referenced by the current value."
1.93 + cost = 2
1.94 +
1.95 +class StoreAttr(AR):
1.96 + "Store the source value into the given attribute of the object referenced by the current value."
1.97 + cost = 2
1.98 +
1.99 +class LoadAttrIndex(Immediate):
1.100 + "Load into the current value the attribute of the current value with the given index."
1.101 + cost = 6
1.102 +
1.103 +class StoreAttrIndex(Immediate):
1.104 + "Store the source value into the attribute of the current value with the given index."
1.105 + cost = 6
1.106 +
1.107 class LoadAttrIndexContextCond(Immediate):
1.108 - """Load into the current value the attribute of the current value with the given index, only making the
1.109 - current value the context if the attribute is compatible."""
1.110 + """
1.111 + Load into the current value the attribute of the current value with the given index, only making the
1.112 + current value the context if the attribute is compatible.
1.113 + """
1.114 + cost = 8
1.115
1.116 # Access to object details.
1.117
1.118 -class LoadCallable(Instruction): "Load the target of an invocation."
1.119 -class StoreCallable(Instruction): "Store the source value into the object referenced by the current value."
1.120 +class LoadCallable(Instruction):
1.121 + "Load the target of an invocation."
1.122 + cost = 2
1.123 +
1.124 +class StoreCallable(Instruction):
1.125 + "Store the source value into the object referenced by the current value."
1.126 + cost = 3
1.127
1.128 # Access to invocation frames in preparation.
1.129
1.130 -class MakeFrame(Immediate): "Make a new invocation frame."
1.131 -class AdjustFrame(Immediate): "Adjust the current invocation frame for corrected invocations."
1.132 -class DropFrame(Instruction): "Drop an invocation frame."
1.133 -class StoreFrame(Immediate): "Store the current value as an argument for the parameter with the given position."
1.134 -class StoreFrameIndex(Immediate): "Store the source value as an argument of the current value for the parameter with the given index."
1.135 -class LoadContext(Instruction): "Load the context of an invocation."
1.136 +class MakeFrame(Immediate):
1.137 + "Make a new invocation frame."
1.138 + cost = 2
1.139 +
1.140 +class AdjustFrame(Immediate):
1.141 + "Adjust the current invocation frame for corrected invocations."
1.142 + cost = 2
1.143 +
1.144 +class DropFrame(Instruction):
1.145 + "Drop an invocation frame."
1.146 + cost = 2
1.147 +
1.148 +class StoreFrame(Immediate):
1.149 + "Store the current value as an argument for the parameter with the given position."
1.150 + cost = 2
1.151 +
1.152 +class StoreFrameIndex(Immediate):
1.153 + "Store the source value as an argument of the current value for the parameter with the given index."
1.154 + cost = 6
1.155 +
1.156 +class LoadContext(Instruction):
1.157 + "Load the context of an invocation."
1.158 + cost = 2
1.159
1.160 # Context-related tests.
1.161
1.162 -class CheckContext(Instruction): "Check to see if the context is valid."
1.163 -class CheckClass(Instruction): "Check the current value to determine whether it is a class."
1.164 -class CheckInstance(Instruction): """Check the current value as an instance of a class or its subclasses (used with 'self' in an
1.165 - invocation)."""
1.166 -class CheckType(Instruction): "Check the current value as an instance of a specific class only."
1.167 +class CheckContext(Instruction):
1.168 + "Check to see if the context is valid."
1.169 + cost = 2
1.170 +
1.171 +class CheckClass(Instruction):
1.172 + "Check the current value to determine whether it is a class."
1.173 + cost = 2
1.174 +
1.175 +class CheckInstance(Instruction):
1.176 + """
1.177 + Check the current value as an instance of a class or its subclasses (used with 'self' in an
1.178 + invocation).
1.179 + """
1.180 + cost = 6
1.181 +
1.182 +class CheckType(Instruction):
1.183 + "Check the current value as an instance of a specific class only."
1.184 + cost = 5
1.185
1.186 # Access to frames upon invocation.
1.187
1.188 -class CheckFrame(Immediate): "Check the frame for the correct number of arguments."
1.189 -class CheckExtra(Immediate): "Ensure that the frame can provide extra arguments."
1.190 -class FillDefaults(Immediate): "Fill frame positions with defaults, if appropriate."
1.191 -class ExtendFrame(Immediate): "Extend the current frame for temporary storage use."
1.192 -class CopyExtra(Immediate): "Copy extra arguments into a separate sequence, starting from the given position."
1.193 +class CheckFrame(Immediate):
1.194 + "Check the frame for the correct number of arguments."
1.195 + cost = 3
1.196 +
1.197 +class CheckExtra(Immediate):
1.198 + "Ensure that the frame can provide extra arguments."
1.199 + cost = 3
1.200 +
1.201 +class FillDefaults(Immediate):
1.202 + "Fill frame positions with defaults, if appropriate."
1.203 + cost = 8 # variable
1.204 +
1.205 +class ExtendFrame(Immediate):
1.206 + "Extend the current frame for temporary storage use."
1.207 + cost = 1
1.208 +
1.209 +class CopyExtra(Immediate):
1.210 + "Copy extra arguments into a separate sequence, starting from the given position."
1.211 + cost = 10
1.212
1.213 # Invocation-related instructions, using a special result "register".
1.214
1.215 -class JumpInFrame(Instruction): "Jump, using the current locals, to the current callable."
1.216 -class JumpWithFrame(Instruction): "Jump, adopting the invocation frame, to the current callable."
1.217 -class JumpWithFrameDirect(Target): "Jump to the specified address, adopting the invocation frame."
1.218 -class Return(Instruction): "Return from a subprogram."
1.219 -class LoadResult(Instruction): "Load into the current value a returned value."
1.220 -class StoreResult(Instruction): "Store the current value as a value to be returned."
1.221 +class JumpInFrame(Instruction):
1.222 + "Jump, using the current locals, to the current callable."
1.223 + cost = 2
1.224 +
1.225 +class JumpWithFrame(Instruction):
1.226 + "Jump, adopting the invocation frame, to the current callable."
1.227 + cost = 3
1.228 +
1.229 +class JumpWithFrameDirect(Target):
1.230 + "Jump to the specified address, adopting the invocation frame."
1.231 + cost = 3
1.232 +
1.233 +class Return(Instruction):
1.234 + "Return from a subprogram."
1.235 + cost = 2
1.236 +
1.237 +class LoadResult(Instruction):
1.238 + "Load into the current value a returned value."
1.239 + cost = 1
1.240 +
1.241 +class StoreResult(Instruction):
1.242 + "Store the current value as a value to be returned."
1.243 + cost = 1
1.244
1.245 # Branch-related instructions.
1.246
1.247 -class Jump(Address): "Jump unconditionally."
1.248 -class JumpIfFalse(Address): "Jump if the last evaluation gave a false result."
1.249 -class JumpIfTrue(Address): "Jump if the last evaluation gave a true result."
1.250 +class Jump(Address):
1.251 + "Jump unconditionally."
1.252 + cost = 1
1.253 +
1.254 +class JumpIfFalse(Address):
1.255 + "Jump if the last evaluation gave a false result."
1.256 + cost = 2
1.257 +
1.258 +class JumpIfTrue(Address):
1.259 + "Jump if the last evaluation gave a true result."
1.260 + cost = 2
1.261
1.262 # Exception-related instructions, using a special exception "register".
1.263
1.264 -class LoadException(Instruction): "Load the raised exception."
1.265 -class StoreException(Instruction): "Store the current object in the exception register."
1.266 -class ClearException(Instruction): "Reset the exception register."
1.267 -class RaiseException(Instruction): "Raise an exception, jumping to the active handler."
1.268 -class PushHandler(Address): "Push an exception handler onto the handler stack."
1.269 -class PopHandler(Instruction): "Pop an exception handler from the handler stack."
1.270 -class CheckException(Instruction): "Check the raised exception against another."
1.271 +class LoadException(Instruction):
1.272 + "Load the raised exception."
1.273 + cost = 1
1.274 +
1.275 +class StoreException(Instruction):
1.276 + "Store the current object in the exception register."
1.277 + cost = 1
1.278 +
1.279 +class ClearException(Instruction):
1.280 + "Reset the exception register."
1.281 + cost = 1
1.282 +
1.283 +class RaiseException(Instruction):
1.284 + "Raise an exception, jumping to the active handler."
1.285 + cost = 2
1.286 +
1.287 +class PushHandler(Address):
1.288 + "Push an exception handler onto the handler stack."
1.289 + cost = 3
1.290 +
1.291 +class PopHandler(Instruction):
1.292 + "Pop an exception handler from the handler stack."
1.293 + cost = 3
1.294 +
1.295 +class CheckException(Instruction):
1.296 + "Check the raised exception against another."
1.297 + cost = 6
1.298
1.299 # Test instructions, operating on the boolean status register.
1.300
1.301 -class TestIdentity(Instruction): "Test whether the current value is identical to the source value, setting the boolean status."
1.302 -class TestIdentityAddress(Address): "Test whether the current value is identical to the given address, setting the boolean status."
1.303 -class InvertBoolean(Instruction): "Invert the boolean status."
1.304 +class TestIdentity(Instruction):
1.305 + "Test whether the current value is identical to the source value, setting the boolean status."
1.306 + cost = 2
1.307 +
1.308 +class TestIdentityAddress(Address):
1.309 + "Test whether the current value is identical to the given address, setting the boolean status."
1.310 + cost = 2
1.311 +
1.312 +class InvertBoolean(Instruction):
1.313 + "Invert the boolean status."
1.314 + cost = 1
1.315
1.316 # Instructions which affect the current value. (LoadAttrIndexContext not defined.)
1.317
1.318 @@ -487,13 +644,12 @@
1.319 CopyExtra
1.320 )
1.321
1.322 -# Instructions which use the current value.
1.323 +# Instructions which use the current value. (LoadAttrIndexContext not defined.)
1.324
1.325 simple_input_user_instructions = (
1.326 StoreTemp, StoreFrame, StoreResult, StoreException, # as the value being stored
1.327 LoadAddressContext, LoadAddressContextCond, # as the object being referenced
1.328 - LoadAttr, LoadAttrIndex, # LoadAttrIndexContext, # as the object being referenced
1.329 - LoadAttrIndexContextCond, # as the object being referenced
1.330 + LoadAttr, LoadAttrIndex, LoadAttrIndexContextCond, # as the object being referenced
1.331 StoreAttr, StoreAttrIndex, StoreCallable, # as the object being referenced
1.332 StoreFrameIndex, # as the object being referenced
1.333 StoreAddressContext, # as the context
2.1 --- a/rsvp.py Fri Feb 26 01:56:26 2010 +0100
2.2 +++ b/rsvp.py Wed Mar 03 00:26:00 2010 +0100
2.3 @@ -97,7 +97,11 @@
2.4 self.pc = pc or 0
2.5 self.debug = debug
2.6 self.abort_upon_exception = abort_upon_exception
2.7 +
2.8 + # Profiling.
2.9 +
2.10 self.counter = 0
2.11 + self.cost = 0
2.12
2.13 # Stacks.
2.14
2.15 @@ -262,6 +266,7 @@
2.16 print "At address", self.pc
2.17 else:
2.18 print "successfully."
2.19 + print "After", self.counter, "instructions at cost", self.cost, "units."
2.20
2.21 def test(self, module):
2.22
2.23 @@ -333,11 +338,13 @@
2.24 raise IllegalInstruction, (self.pc, instruction_name)
2.25 return method
2.26
2.27 - def perform(self, instruction):
2.28 + def perform(self, instruction, is_input=0):
2.29
2.30 "Perform the 'instruction', returning the next PC value or None."
2.31
2.32 - self.counter += 1
2.33 + if not is_input:
2.34 + self.counter += 1
2.35 + self.cost += instruction.cost
2.36 self.operand = instruction.get_operand()
2.37 method = self.get_method(instruction)
2.38 return method()
2.39 @@ -352,11 +359,11 @@
2.40
2.41 value = self.value
2.42 if self.instruction.source is not None:
2.43 - self.perform(self.instruction.source)
2.44 + self.perform(self.instruction.source, 1)
2.45 self.source = self.value
2.46 self.value = value
2.47 if self.instruction.input is not None:
2.48 - self.perform(self.instruction.input)
2.49 + self.perform(self.instruction.input, 1)
2.50
2.51 def jump(self, addr, next):
2.52