1.1 --- a/micropython/ast.py Sun Apr 06 03:04:36 2008 +0200
1.2 +++ b/micropython/ast.py Sun Apr 06 21:26:06 2008 +0200
1.3 @@ -48,7 +48,7 @@
1.4
1.5 "A translated module."
1.6
1.7 - supported_optimisations = ["constant_storage", "known_target"]
1.8 + supported_optimisations = ["constant_storage", "known_target", "self_access"]
1.9
1.10 def __init__(self, module, objtable, paramtable, builtins=None, optimisations=None):
1.11
1.12 @@ -238,14 +238,11 @@
1.13 self.replace_op(AttrInstruction(pos))
1.14
1.15 # Where the last operation involves the special 'self' name, check to
1.16 - # see if the attribute is acceptably positioned.
1.17 + # see if the attribute is acceptably positioned and produce a direct
1.18 + # access to the attribute.
1.19
1.20 - elif isinstance(self.unit, micropython.inspect.Function) and \
1.21 - self.unit.is_method() and isinstance(last, LoadName) and \
1.22 - last.attr.name == "self" and not self.unit.is_relocated(attrname):
1.23 -
1.24 - attr = self.unit.parent.all_attributes()[attrname]
1.25 - self.new_op(AttrInstruction(attr))
1.26 + elif self._optimise_self_access(attrname, AttrInstruction):
1.27 + pass
1.28
1.29 # Otherwise, perform a normal operation.
1.30
1.31 @@ -471,6 +468,9 @@
1.32 def _should_optimise_known_target(self):
1.33 return "known_target" in self.optimisations
1.34
1.35 + def _should_optimise_self_access(self):
1.36 + return "self_access" in self.optimisations
1.37 +
1.38 def _have_constant_input(self, n):
1.39 last = self.last_ops(n+1)
1.40 return len(last) > n and (isinstance(last[n], LoadAttr) and last[n].attr.assignments == 1 or
1.41 @@ -479,17 +479,26 @@
1.42 def _have_known_target(self):
1.43 return self._have_constant_input(0)
1.44
1.45 + def _have_self_input(self):
1.46 + last = self.last_op()
1.47 + return isinstance(self.unit, micropython.inspect.Function) and \
1.48 + self.unit.is_method() and isinstance(last, LoadName) and \
1.49 + last.attr.name == "self"
1.50 +
1.51 # Optimisation methods. See the supported_optimisations class attribute.
1.52
1.53 - def _optimise_constant_storage(self, cls, n):
1.54 + def _optimise_constant_storage(self, instruction, n):
1.55
1.56 """
1.57 Where this operation should store a constant into a target which is
1.58 also constant, optimise away both operations.
1.59 """
1.60
1.61 - if self._should_optimise_constant_storage() and cls in (StoreAttr, StoreName) and \
1.62 - self._have_constant_input(n) and (n == 0 or self._have_constant_input(n-1)):
1.63 + if self._should_optimise_constant_storage() and \
1.64 + instruction in (StoreAttr, StoreName) and \
1.65 + self._have_constant_input(n) and \
1.66 + (n == 0 or self._have_constant_input(n-1)):
1.67 +
1.68 self.remove_ops(n+1)
1.69 return 1
1.70 else:
1.71 @@ -523,6 +532,23 @@
1.72
1.73 return target, context
1.74
1.75 + def _optimise_self_access(self, attrname, instruction):
1.76 +
1.77 + """
1.78 + Where the provided 'attrname' accesses an attribute which occupies the
1.79 + same position in all possible objects which can be accessed, generate an
1.80 + 'instruction' accessing the attribute directly.
1.81 + """
1.82 +
1.83 + if self._should_optimise_self_access() and self._have_self_input() and \
1.84 + not self.unit.is_relocated(attrname):
1.85 +
1.86 + attr = self.unit.parent.all_attributes()[attrname]
1.87 + self.new_op(instruction(attr))
1.88 + return 1
1.89 + else:
1.90 + return 0
1.91 +
1.92 # Visitor methods.
1.93
1.94 def default(self, node, *args):
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/tests/attributes.py Sun Apr 06 21:26:06 2008 +0200
2.3 @@ -0,0 +1,9 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +class C:
2.7 + clsattr = 123
2.8 +
2.9 + def __init__(self, value):
2.10 + self.instattr = value
2.11 +
2.12 +# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/tests/sealing.py Sun Apr 06 03:04:36 2008 +0200
3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
3.3 @@ -1,9 +0,0 @@
3.4 -#!/usr/bin/env python
3.5 -
3.6 -class C:
3.7 - clsattr = 123
3.8 -
3.9 - def __init__(self, value):
3.10 - self.instattr = value
3.11 -
3.12 -# vim: tabstop=4 expandtab shiftwidth=4