1.1 --- a/micropython/opt.py Sat May 30 20:30:28 2009 +0200
1.2 +++ b/micropython/opt.py Sun May 31 18:44:40 2009 +0200
1.3 @@ -31,6 +31,7 @@
1.4 # Code generation optimisations:
1.5 "constant_storage", "constant_accessor", "known_target", "self_access",
1.6 "temp_storage", "load_operations", "no_operations", "unused_results",
1.7 + "unused_handlers",
1.8 # Inspection optimisations:
1.9 "unused_objects"
1.10 ]
1.11 @@ -142,6 +143,9 @@
1.12 def should_optimise_unused_results(self):
1.13 return "unused_results" in self.optimisations
1.14
1.15 + def should_optimise_unused_handlers(self):
1.16 + return "unused_handlers" in self.optimisations
1.17 +
1.18 # Simple tests.
1.19
1.20 def is_constant_input(self, instruction):
1.21 @@ -286,6 +290,15 @@
1.22
1.23 return 0
1.24
1.25 + def have_empty_handler(self, instruction):
1.26 +
1.27 + """
1.28 + Return whether the active instruction defined a handler for exceptions
1.29 + which is then discarded by the given 'instruction'.
1.30 + """
1.31 +
1.32 + return isinstance(self.translation.last_op(), PushHandler) and isinstance(instruction, PopHandler)
1.33 +
1.34 # Optimisation methods. See the supported_optimisations class attribute.
1.35
1.36 def optimise_constant_storage(self):
1.37 @@ -419,4 +432,14 @@
1.38 if self.should_optimise_unused_results() and self.have_simple_input():
1.39 self.remove_active_value()
1.40
1.41 + def optimise_unused_handlers(self, instruction):
1.42 +
1.43 + "Discard handlers which will not be used."
1.44 +
1.45 + if self.should_optimise_unused_handlers() and self.have_empty_handler(instruction):
1.46 + self.translation.remove_op()
1.47 + return 1
1.48 + else:
1.49 + return 0
1.50 +
1.51 # vim: tabstop=4 expandtab shiftwidth=4