# HG changeset patch # User Paul Boddie # Date 1220746625 -7200 # Node ID 2548cd0c4008938221fa410d3a105fa2be86fd58 # Parent 4cb6ccaaf758bf4f5eeeaa9b12dc016c7a69b9f6 Removed the source storage optimisation, since it can effectively be replaced by a suitably chosen temporary storage substitute under the appropriate conditions. Made temporary storage substitution possible for assignment expressions and other source values when used immediately. Added missing unused result optimisation test. Added frame extension at the module level. Added temporary storage erasure after each statement. Made each AST Module node refer to the inspected module object. Added an initial frame to the RSVP machine. diff -r 4cb6ccaaf758 -r 2548cd0c4008 docs/optimisations.txt --- a/docs/optimisations.txt Sun Sep 07 00:47:20 2008 +0200 +++ b/docs/optimisations.txt Sun Sep 07 02:17:05 2008 +0200 @@ -6,10 +6,6 @@ remove both instructions (currently a single merged instruction) -source_storage source instruction is a simple input operation; -(elimination) source instruction is the last instruction; - remove the source instruction - known_target value instruction references a constant; (guidance) target and context are provided (no instructions removed) diff -r 4cb6ccaaf758 -r 2548cd0c4008 micropython/ast.py --- a/micropython/ast.py Sun Sep 07 00:47:20 2008 +0200 +++ b/micropython/ast.py Sun Sep 07 02:17:05 2008 +0200 @@ -30,8 +30,9 @@ "A code optimiser." supported_optimisations = [ - "constant_storage", "source_storage", "known_target", "self_access", - "temp_storage", "load_operations", "no_operations", "unused_results" + "constant_storage", "known_target", "self_access", + "temp_storage", "load_operations", "no_operations", + "unused_results" ] def __init__(self, translation, optimisations=None): @@ -50,9 +51,6 @@ def should_optimise_constant_storage(self): return "constant_storage" in self.optimisations - def should_optimise_source_storage(self): - return "source_storage" in self.optimisations - def should_optimise_known_target(self): return "known_target" in self.optimisations @@ -221,25 +219,6 @@ else: return 0 - def optimise_source_storage(self): - - """ - Where the source value in an assignment can be inserted into the - eventual target without intermediate storage, optimise away the storage - instruction. - """ - - if self.should_optimise_source_storage() and \ - self.translation.active_source is not None and \ - self.translation.active_source.source is None and \ - self.translation.active_source.input is None and \ - self.translation.active_source is self.translation.active: - - self.translation.remove_op() - return 1 - else: - return 0 - def optimise_known_target(self): """ @@ -365,7 +344,7 @@ "Discard results which will not be used." - if self.have_simple_input(): + if self.should_optimise_unused_results() and self.have_simple_input(): self.translation.remove_active_value() # Program visitors. @@ -610,13 +589,15 @@ # Assignment expression values. - def record_value(self): - self.expr_temp.append(self.get_temp()) - self.active_source = self.active + def record_value(self, immediate=1): + if immediate: + temp = self.optimiser.optimise_temp_storage() + else: + temp = self.get_temp() + self.expr_temp.append(temp) def discard_value(self): self.discard_temp(self.expr_temp.pop()) - self.active_source = None def set_source(self): if self.active is not None: @@ -725,7 +706,6 @@ self.active = None self.active_value = None - self.active_source = None # Visitor methods. @@ -1438,7 +1418,7 @@ "Make a sequence of 'sequence_type' for the given program 'node'." - self.make_object(self.get_builtin(sequence_type, node), len(node.nodes)) + self.make_object(self.get_builtin_class(sequence_type, node), len(node.nodes)) temp = self.get_temp() for i, n in enumerate(node.nodes): @@ -1530,18 +1510,18 @@ def visitAssign(self, node): self.dispatch(node.expr) - self.record_value() + self.record_value(0) for n in node.nodes: - self.dispatch(n, 1) + self.dispatch(n) self.discard_value() - def visitAssAttr(self, node, top_level=0): + def visitAssAttr(self, node): self._visitAttr(node, self.attribute_store_instructions) self.set_source() - def visitAssList(self, node, top_level=1): + def visitAssList(self, node): for i, n in enumerate(node.nodes): self._startCallFunc() self.new_op(self.expr_temp[-1]) @@ -1552,19 +1532,13 @@ # Provide a different source value. - self.record_value() - self.dispatch(n, 0) + self.record_value(0) + self.dispatch(n) self.discard_value() - def visitAssName(self, node, top_level=1): - - # Optimise away intermediate source storage. - - if top_level: - no_source = self.optimiser.optimise_source_storage() + def visitAssName(self, node): self._visitName(node, self.name_store_instructions) - if not top_level or not no_source: - self.set_source() + self.set_source() visitAssTuple = visitAssList @@ -1936,7 +1910,10 @@ self._visitBinary(node, "__mod__", "__rmod__") def visitModule(self, node): + extend = ExtendFrame() + self.new_op(extend) self.dispatch(node.node) + self.set_frame_usage(node, extend) def visitMul(self, node): self._visitBinary(node, "__mul__", "__rmul__") @@ -2034,6 +2011,9 @@ def visitStmt(self, node): for n in node.nodes: self.dispatch(n) + if self.temp_positions: + #print "Had temp", self.temp_positions + self.temp_positions = set() def visitSub(self, node): self._visitBinary(node, "__sub__", "__rsub__") diff -r 4cb6ccaaf758 -r 2548cd0c4008 micropython/inspect.py --- a/micropython/inspect.py Sun Sep 07 00:47:20 2008 +0200 +++ b/micropython/inspect.py Sun Sep 07 02:17:05 2008 +0200 @@ -540,6 +540,10 @@ visitMod = OP def visitModule(self, node): + + # Make a back reference from the node for code generation. + + node.unit = self return self.dispatch(node.node) visitMul = OP diff -r 4cb6ccaaf758 -r 2548cd0c4008 rsvp.py --- a/rsvp.py Sun Sep 07 00:47:20 2008 +0200 +++ b/rsvp.py Sun Sep 07 02:17:05 2008 +0200 @@ -75,7 +75,7 @@ self.pc_stack = [] self.frame_stack = [] - self.local_sp_stack = [] + self.local_sp_stack = [0] self.invocation_sp_stack = [] self.handler_stack = []