1.1 --- a/micropython/opt.py Sun Sep 28 19:59:32 2008 +0200
1.2 +++ b/micropython/opt.py Sun Sep 28 21:27:01 2008 +0200
1.3 @@ -82,7 +82,10 @@
1.4
1.5 def remove_active_value(self):
1.6
1.7 - "Remove the value-providing active instruction if appropriate."
1.8 + """
1.9 + Remove the value-providing active instruction from the generated code,
1.10 + if appropriate, but keep a record of the active instruction itself.
1.11 + """
1.12
1.13 if self.active_value is self.active:
1.14 removed = self.active
1.15 @@ -275,17 +278,12 @@
1.16
1.17 """
1.18 Where the last operation stores a constant into a target which is also
1.19 - constant, optimise away both operations.
1.20 + constant, indicate that both operations should be optimised away.
1.21 """
1.22
1.23 - if self.should_optimise_constant_storage() and \
1.24 + return self.should_optimise_constant_storage() and \
1.25 self.have_constant_target() and \
1.26 - self.have_constant_source():
1.27 -
1.28 - self.translation.remove_op()
1.29 - return 1
1.30 - else:
1.31 - return 0
1.32 + self.have_constant_source()
1.33
1.34 def optimise_constant_accessor(self):
1.35
1.36 @@ -331,57 +329,16 @@
1.37 else:
1.38 return None
1.39
1.40 - def optimise_self_access(self, attrname, classes, node):
1.41 + def optimise_self_access(self, unit, attrname):
1.42
1.43 """
1.44 - Where the provided 'attrname' accesses an attribute which occupies the
1.45 - same position in all possible objects which can be accessed, generate an
1.46 - instruction using one of the given 'classes', accessing the attribute
1.47 - directly.
1.48 + Return whether code in the given 'unit' is able to access the given
1.49 + 'attrname' through the same position in all possible objects which might
1.50 + be accessed.
1.51 """
1.52
1.53 - AddressInstruction, AddressContextInstruction, AttrInstruction = classes
1.54 -
1.55 - if self.should_optimise_self_access() and self.have_self_input() and \
1.56 - not self.translation.unit.is_relocated(attrname):
1.57 -
1.58 - # Either generate an instruction operating on an instance attribute.
1.59 -
1.60 - try:
1.61 - attr = self.translation.unit.parent.instance_attributes()[attrname]
1.62 - self.translation.new_op(AttrInstruction(attr))
1.63 -
1.64 - # Or generate an instruction operating on a class attribute.
1.65 -
1.66 - except KeyError:
1.67 - attr = self.translation.unit.parent.all_attributes()[attrname]
1.68 -
1.69 - # Switch the context if the class attribute is compatible with
1.70 - # the instance.
1.71 -
1.72 - if attr.defined_within_hierarchy():
1.73 -
1.74 - # Only permit loading (not storing) of class attributes via self.
1.75 -
1.76 - if AddressContextInstruction is not None:
1.77 - self.translation.new_op(AddressContextInstruction(attr))
1.78 - else:
1.79 - raise TranslateError(self.translation.module.full_name(), node,
1.80 - "Storing of class attribute %r via self not permitted." % attrname)
1.81 -
1.82 - # Preserve the context if the class attribute comes from an
1.83 - # incompatible class.
1.84 -
1.85 - else:
1.86 - if AddressInstruction is not None:
1.87 - self.translation.new_op(AddressInstruction(attr))
1.88 - else:
1.89 - raise TranslateError(self.translation.module.full_name(), node,
1.90 - "Storing of class attribute %r via self not permitted." % attrname)
1.91 -
1.92 - return 1
1.93 - else:
1.94 - return 0
1.95 + return self.should_optimise_self_access() and \
1.96 + self.have_self_input() and not unit.is_relocated(attrname)
1.97
1.98 def optimise_temp_storage(self):
1.99
1.100 @@ -433,12 +390,8 @@
1.101 the inputs originally came from.
1.102 """
1.103
1.104 - if self.should_optimise_away_no_operations() and \
1.105 - self.is_resultant_no_operation(instruction):
1.106 -
1.107 - return 1
1.108 - else:
1.109 - return 0
1.110 + return self.should_optimise_away_no_operations() and \
1.111 + self.is_resultant_no_operation(instruction)
1.112
1.113 def optimise_unused_results(self):
1.114