# HG changeset patch # User Paul Boddie # Date 1209861723 -7200 # Node ID 259153134864cf36e15f4269f94aa94c736e52ce # Parent d06c78713711df12518645c47523f98dde4b3d93 Changed the _optimise_known_target method to return None where the target and context are not identified, thus producing a false value for testing. Introduced optimisation of known targets when obtaining binary operator methods. Changed constant attribute access to use LoadAddress (StoreAddress should not apply). Exposed code produced by the show function in the test program as a global. diff -r d06c78713711 -r 259153134864 micropython/ast.py --- a/micropython/ast.py Fri May 02 01:47:19 2008 +0200 +++ b/micropython/ast.py Sun May 04 02:42:03 2008 +0200 @@ -279,10 +279,7 @@ # Produce a suitable instruction. - if isinstance(target, micropython.inspect.Const): - self.replace_op(AttrInstruction(pos)) - else: - self.replace_op(AddressInstruction(pos)) + self.replace_op(AddressInstruction(pos)) # Where the last operation involves the special 'self' name, check to # see if the attribute is acceptably positioned and produce a direct @@ -312,7 +309,11 @@ # NOTE: Only simple cases are used for optimisations. - target, context = self._optimise_known_target() + t = self._optimise_known_target() + if t: + target, context = t + else: + target, context = None, None # Where a target is known and has a known context, avoid generating any # first argument. Instance methods do not have a known target since they @@ -650,11 +651,9 @@ # A special context is chosen to avoid generating unnecessary # context loading and checking instructions. + return target, context else: - target = None - context = None - - return target, context + return None def _optimise_self_access(self, attrname, instruction): @@ -730,9 +729,11 @@ # Get left method on temp1. self._generateAttr(node, left_method, (LoadAddress, LoadAttr, LoadAttrIndex)) - self.dispatch(compiler.ast.Name("AttributeError")) - self.new_op(CheckException()) - self.new_op(JumpIfTrue(end_left_label)) + + if not self._optimise_known_target(): + self.dispatch(compiler.ast.Name("AttributeError")) + self.new_op(CheckException()) + self.new_op(JumpIfTrue(end_left_label)) # Add arguments. @@ -763,9 +764,11 @@ # Get right method on temp2. self._generateAttr(node, right_method, (LoadAddress, LoadAttr, LoadAttrIndex)) - self.dispatch(compiler.ast.Name("AttributeError")) - self.new_op(CheckException()) - self.new_op(JumpIfTrue(type_error_label)) + + if not self._optimise_known_target(): + self.dispatch(compiler.ast.Name("AttributeError")) + self.new_op(CheckException()) + self.new_op(JumpIfTrue(type_error_label)) # Add arguments. diff -r d06c78713711 -r 259153134864 test.py --- a/test.py Fri May 02 01:47:19 2008 +0200 +++ b/test.py Sun May 04 02:42:03 2008 +0200 @@ -3,9 +3,13 @@ import micropython import sys +code = None + def show(importer, with_builtins=0, optimisations=None): optimisations = optimisations or requested_optimisations - for i, x in enumerate(importer.get_image(with_builtins, optimisations)): + global code + code = importer.get_image(with_builtins, optimisations) + for i, x in enumerate(code): print i, x def attrs(obj):