1.1 --- a/simplify.py Sun Jul 30 16:15:57 2006 +0200
1.2 +++ b/simplify.py Mon Jul 31 01:01:34 2006 +0200
1.3 @@ -44,13 +44,14 @@
1.4 Sub, Yield.
1.5 """
1.6
1.7 - def __init__(self):
1.8 + def __init__(self, builtins=0):
1.9 Visitor.__init__(self)
1.10 self.result = None # The resulting tree.
1.11 self.subprograms = [] # Subprograms outside the tree.
1.12 self.structures = [] # Structures/classes.
1.13 self.constants = {} # Constants.
1.14 self.current_subprograms = [] # Current subprograms being processed.
1.15 + self.builtins = builtins # Whether the builtins are being processed.
1.16
1.17 def process(self, node):
1.18 self.visitor = self
1.19 @@ -77,7 +78,21 @@
1.20
1.21 def visitModule(self, module):
1.22 self.result = Module(module)
1.23 - self.result.code = self.dispatch(module.node)
1.24 + module_code = self.dispatch(module.node)
1.25 +
1.26 + # NOTE: Constant initialisation necessary for annotation but perhaps
1.27 + # NOTE: redundant in the program.
1.28 +
1.29 + init_code = []
1.30 + for value, constant in self.constants.items():
1.31 + init_code.append(StoreAttr(lvalue=LoadRef(ref=constant), name="__class__", expr=LoadName(name=constant.typename)))
1.32 +
1.33 + # NOTE: Hack to ensure correct initialisation of constants.
1.34 +
1.35 + if self.builtins:
1.36 + self.result.code = module_code + init_code
1.37 + else:
1.38 + self.result.code = init_code + module_code
1.39 return self.result
1.40
1.41 def visitGetattr(self, getattr):
1.42 @@ -205,6 +220,8 @@
1.43 self.current_subprograms.pop()
1.44 self.subprograms.append(body_subprogram)
1.45
1.46 + # Always return from conditional sections.
1.47 +
1.48 test.body = [Invoke(stmt, expr=LoadRef(ref=body_subprogram), same_frame=1, star=None, dstar=None, args=[]), Return()]
1.49 nodes.append(test)
1.50
1.51 @@ -219,7 +236,10 @@
1.52 self.current_subprograms.pop()
1.53 self.subprograms.append(else_subprogram)
1.54
1.55 + # Always return from conditional subprograms.
1.56 +
1.57 nodes.append(Invoke(stmt, expr=LoadRef(ref=else_subprogram), same_frame=1, star=None, dstar=None, args=[]))
1.58 + nodes.append(Return())
1.59
1.60 subprogram.code = nodes
1.61
1.62 @@ -258,6 +278,9 @@
1.63 test = Conditional(body=[], else_=[], test=Invoke(expr=LoadName(name="isinstance"), args=[LoadExc(), new_spec], star=None, dstar=None))
1.64 if assign is not None:
1.65 test.body.append(Assign(code=[StoreTemp(expr=LoadExc()), self.dispatch(assign), ReleaseTemp()]))
1.66 +
1.67 + # Always return from conditional sections.
1.68 +
1.69 test.body += self.dispatch(stmt) + [Return()]
1.70 nodes.append(test)
1.71
1.72 @@ -316,6 +339,9 @@
1.73 else:
1.74 raise NotImplementedError, op_name
1.75 nodes.append(StoreTemp(expr=invocation))
1.76 +
1.77 + # Always return from conditional sections/subprograms.
1.78 +
1.79 if op is not last:
1.80 nodes.append(Conditional(test=Not(expr=LoadTemp()), body=[Return(expr=LoadTemp())]))
1.81 nodes.append(ReleaseTemp())
1.82 @@ -348,6 +374,9 @@
1.83 last = and_.nodes[-1]
1.84 for node in and_.nodes:
1.85 expr = self.dispatch(node)
1.86 +
1.87 + # Always return from conditional sections/subprograms.
1.88 +
1.89 if node is not last:
1.90 nodes.append(StoreTemp(expr=expr))
1.91 invocation = Invoke(expr=LoadAttr(expr=LoadTemp(), name="__true__"), args=[], star=None, dstar=None)
1.92 @@ -381,6 +410,9 @@
1.93 last = or_.nodes[-1]
1.94 for node in or_.nodes:
1.95 expr = self.dispatch(node)
1.96 +
1.97 + # Always return from conditional sections/subprograms.
1.98 +
1.99 if node is not last:
1.100 nodes.append(StoreTemp(expr=expr))
1.101 invocation = Invoke(expr=LoadAttr(expr=LoadTemp(), name="__true__"), args=[], star=None, dstar=None)
1.102 @@ -608,12 +640,12 @@
1.103
1.104 # Make a subprogram which initialises the class structure.
1.105
1.106 - subprogram = Subprogram(name=None, structure=structure, params=[], star=None, dstar=None)
1.107 + subprogram = Subprogram(name=None, acquire_locals=1, structure=structure, params=[], star=None, dstar=None)
1.108 self.current_subprograms.append(subprogram)
1.109
1.110 # The class is initialised using the code found inside.
1.111
1.112 - subprogram.code = self.dispatch(class_.code)
1.113 + subprogram.code = self.dispatch(class_.code) + [Return()]
1.114
1.115 self.current_subprograms.pop()
1.116 self.subprograms.append(subprogram)
1.117 @@ -668,9 +700,9 @@
1.118 # Make a subprogram for the function and record it outside the main
1.119 # tree.
1.120
1.121 - subprogram = Subprogram(name=function.name, returns_value=1, star=None, dstar=None)
1.122 + subprogram = Subprogram(name=function.name, acquire_locals=0, returns_value=1, star=None, dstar=None)
1.123 self.current_subprograms.append(subprogram)
1.124 - subprogram.code = self.dispatch(function.code)
1.125 + subprogram.code = self.dispatch(function.code) + [Return()]
1.126 self.current_subprograms.pop()
1.127 self._visitFunction(function, subprogram)
1.128
1.129 @@ -684,7 +716,7 @@
1.130 # Make a subprogram for the function and record it outside the main
1.131 # tree.
1.132
1.133 - subprogram = Subprogram(name=None, returns_value=1, star=None, dstar=None)
1.134 + subprogram = Subprogram(name=None, acquire_locals=0, returns_value=1, star=None, dstar=None)
1.135 self.current_subprograms.append(subprogram)
1.136 subprogram.code = [Return(expr=self.dispatch(lambda_.code))]
1.137 self.current_subprograms.pop()
1.138 @@ -722,10 +754,13 @@
1.139
1.140 continuation = Invoke(same_frame=1, star=None, dstar=None, args=[])
1.141 continuation.expr = LoadRef(ref=subprogram)
1.142 - test.body = self.dispatch(while_.body) + [continuation]
1.143 +
1.144 + # Always return from conditional sections/subprograms.
1.145 +
1.146 + test.body = self.dispatch(while_.body) + [continuation, Return()]
1.147 if while_.else_ is not None:
1.148 test.else_ = self.dispatch(while_.else_)
1.149 - subprogram.code = [test]
1.150 + subprogram.code = [test, Return()]
1.151
1.152 self.current_subprograms.pop()
1.153 self.subprograms.append(subprogram)
1.154 @@ -743,10 +778,12 @@
1.155 subprogram = Subprogram(name=None, acquire_locals=1, returns_value=0, params=[], star=None, dstar=None)
1.156 self.current_subprograms.append(subprogram)
1.157
1.158 + # Always return from conditional sections/subprograms.
1.159 +
1.160 if for_.else_ is not None:
1.161 - else_stmt = self.dispatch(for_.else_)
1.162 + else_stmt = self.dispatch(for_.else_) + [Return()]
1.163 else:
1.164 - else_stmt = []
1.165 + else_stmt = [Return()]
1.166
1.167 # Wrap the assignment in a try...except statement.
1.168
1.169 @@ -768,7 +805,7 @@
1.170 continuation = Invoke(same_frame=1, produces_result=0, star=None, dstar=None, args=[])
1.171 continuation.expr = LoadRef(ref=subprogram)
1.172 try_except.body = [assign] + self.dispatch(for_.body) + [continuation]
1.173 - subprogram.code = [try_except]
1.174 + subprogram.code = [try_except, Return()]
1.175
1.176 self.subprograms.append(subprogram)
1.177 self.current_subprograms.pop()