# HG changeset patch # User Paul Boddie # Date 1207509966 -7200 # Node ID 24df65a47aa8ef4220e7fe70697e972661dd0898 # Parent 0ed30aeb2ef92a70830b17ad7f3a02d67b26acc7 Made optimised attribute access via self optional. Renamed an example. diff -r 0ed30aeb2ef9 -r 24df65a47aa8 micropython/ast.py --- a/micropython/ast.py Sun Apr 06 03:04:36 2008 +0200 +++ b/micropython/ast.py Sun Apr 06 21:26:06 2008 +0200 @@ -48,7 +48,7 @@ "A translated module." - supported_optimisations = ["constant_storage", "known_target"] + supported_optimisations = ["constant_storage", "known_target", "self_access"] def __init__(self, module, objtable, paramtable, builtins=None, optimisations=None): @@ -238,14 +238,11 @@ self.replace_op(AttrInstruction(pos)) # Where the last operation involves the special 'self' name, check to - # see if the attribute is acceptably positioned. + # see if the attribute is acceptably positioned and produce a direct + # access to the attribute. - elif isinstance(self.unit, micropython.inspect.Function) and \ - self.unit.is_method() and isinstance(last, LoadName) and \ - last.attr.name == "self" and not self.unit.is_relocated(attrname): - - attr = self.unit.parent.all_attributes()[attrname] - self.new_op(AttrInstruction(attr)) + elif self._optimise_self_access(attrname, AttrInstruction): + pass # Otherwise, perform a normal operation. @@ -471,6 +468,9 @@ def _should_optimise_known_target(self): return "known_target" in self.optimisations + def _should_optimise_self_access(self): + return "self_access" in self.optimisations + def _have_constant_input(self, n): last = self.last_ops(n+1) return len(last) > n and (isinstance(last[n], LoadAttr) and last[n].attr.assignments == 1 or @@ -479,17 +479,26 @@ def _have_known_target(self): return self._have_constant_input(0) + def _have_self_input(self): + last = self.last_op() + return isinstance(self.unit, micropython.inspect.Function) and \ + self.unit.is_method() and isinstance(last, LoadName) and \ + last.attr.name == "self" + # Optimisation methods. See the supported_optimisations class attribute. - def _optimise_constant_storage(self, cls, n): + def _optimise_constant_storage(self, instruction, n): """ Where this operation should store a constant into a target which is also constant, optimise away both operations. """ - if self._should_optimise_constant_storage() and cls in (StoreAttr, StoreName) and \ - self._have_constant_input(n) and (n == 0 or self._have_constant_input(n-1)): + if self._should_optimise_constant_storage() and \ + instruction in (StoreAttr, StoreName) and \ + self._have_constant_input(n) and \ + (n == 0 or self._have_constant_input(n-1)): + self.remove_ops(n+1) return 1 else: @@ -523,6 +532,23 @@ return target, context + def _optimise_self_access(self, attrname, instruction): + + """ + Where the provided 'attrname' accesses an attribute which occupies the + same position in all possible objects which can be accessed, generate an + 'instruction' accessing the attribute directly. + """ + + if self._should_optimise_self_access() and self._have_self_input() and \ + not self.unit.is_relocated(attrname): + + attr = self.unit.parent.all_attributes()[attrname] + self.new_op(instruction(attr)) + return 1 + else: + return 0 + # Visitor methods. def default(self, node, *args): diff -r 0ed30aeb2ef9 -r 24df65a47aa8 tests/attributes.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/attributes.py Sun Apr 06 21:26:06 2008 +0200 @@ -0,0 +1,9 @@ +#!/usr/bin/env python + +class C: + clsattr = 123 + + def __init__(self, value): + self.instattr = value + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 0ed30aeb2ef9 -r 24df65a47aa8 tests/sealing.py --- a/tests/sealing.py Sun Apr 06 03:04:36 2008 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,9 +0,0 @@ -#!/usr/bin/env python - -class C: - clsattr = 123 - - def __init__(self, value): - self.instattr = value - -# vim: tabstop=4 expandtab shiftwidth=4