simplify

Changeset

38:cc6abe59ef05
2006-08-03 paulb raw files shortlog changelog graph Fixed temporary value manipulation in annotate. Improved invocation support, adding star, dstar and missing args attributes to Invoke nodes.
annotate.py (file) simplify.py (file)
     1.1 --- a/annotate.py	Thu Aug 03 01:00:40 2006 +0200
     1.2 +++ b/annotate.py	Thu Aug 03 01:02:47 2006 +0200
     1.3 @@ -162,8 +162,6 @@
     1.4      def __init__(self):
     1.5          Visitor.__init__(self)
     1.6          self.system = system
     1.7 -        self.types = None
     1.8 -        self.temp = {}
     1.9  
    1.10          # Satisfy visitor issues.
    1.11  
    1.12 @@ -207,6 +205,8 @@
    1.13  
    1.14          self.returns = []
    1.15          self.return_locals = []
    1.16 +        self.types = None
    1.17 +        self.temp = {}
    1.18  
    1.19          # Add namespace details to any structure involved.
    1.20  
    1.21 @@ -298,19 +298,21 @@
    1.22  
    1.23      def visitLoadTemp(self, loadtemp):
    1.24          index = getattr(loadtemp, "index", None)
    1.25 -        self.types = self.temp[index]
    1.26 +        self.types = self.temp[index][-1]
    1.27          self.annotate(loadtemp)
    1.28          return loadtemp
    1.29  
    1.30      def visitStoreTemp(self, storetemp):
    1.31          storetemp.expr = self.dispatch(storetemp.expr)
    1.32          index = getattr(storetemp, "index", None)
    1.33 -        self.temp[index] = self.types
    1.34 +        if not self.temp.has_key(index):
    1.35 +            self.temp[index] = []
    1.36 +        self.temp[index].append(self.types)
    1.37          return storetemp
    1.38  
    1.39      def visitReleaseTemp(self, releasetemp):
    1.40          index = getattr(releasetemp, "index", None)
    1.41 -        del self.temp[index]
    1.42 +        self.temp[index].pop()
    1.43          return releasetemp
    1.44  
    1.45      def visitLoadAttr(self, loadattr):
    1.46 @@ -343,12 +345,16 @@
    1.47          self.namespace = Namespace()
    1.48          self.namespace.merge_namespace(saved_namespace)
    1.49          conditional.body = self.dispatches(conditional.body)
    1.50 +        body_namespace = self.namespace
    1.51  
    1.52          self.namespace = Namespace()
    1.53          self.namespace.merge_namespace(saved_namespace)
    1.54          conditional.else_ = self.dispatches(conditional.else_)
    1.55 +        else_namespace = self.namespace
    1.56  
    1.57 -        self.namespace = saved_namespace
    1.58 +        self.namespace = Namespace()
    1.59 +        self.namespace.merge_namespace(body_namespace)
    1.60 +        self.namespace.merge_namespace(else_namespace)
    1.61          return conditional
    1.62  
    1.63      def visitReturn(self, return_):
    1.64 @@ -388,13 +394,12 @@
    1.65              types.append(default.types)
    1.66  
    1.67          invoke.args = args
    1.68 -        invoke.types = expr
    1.69  
    1.70          # Now locate and invoke the subprogram. This can be complicated because
    1.71          # the target may be a class or object, and there may be many different
    1.72          # related subprograms.
    1.73  
    1.74 -        invocations = []
    1.75 +        invocations = {}
    1.76  
    1.77          # Visit each callable in turn
    1.78  
    1.79 @@ -420,7 +425,10 @@
    1.80  
    1.81              for subprogram in subprograms:
    1.82                  if subprogram is not None:
    1.83 -                    invocations.append(self.invoke_subprogram(invoke, subprogram))
    1.84 +                    self.invoke_subprogram(invoke, subprogram)
    1.85 +                    invocations[callable] = subprogram
    1.86 +
    1.87 +        invoke.invocations = invocations
    1.88  
    1.89          return invoke
    1.90  
     2.1 --- a/simplify.py	Thu Aug 03 01:00:40 2006 +0200
     2.2 +++ b/simplify.py	Thu Aug 03 01:02:47 2006 +0200
     2.3 @@ -161,8 +161,7 @@
     2.4          return result
     2.5  
     2.6      def _visitBuiltin(self, builtin, name):
     2.7 -        result = Invoke(builtin, expr=LoadName(name=name))
     2.8 -        result.args = self.dispatches(builtin.nodes)
     2.9 +        result = Invoke(builtin, expr=LoadName(name=name), args=self.dispatches(builtin.nodes), star=None, dstar=None)
    2.10          return result
    2.11  
    2.12      def visitTuple(self, tuple):
    2.13 @@ -172,7 +171,7 @@
    2.14          return self._visitBuiltin(list, "list")
    2.15  
    2.16      def visitDict(self, dict):
    2.17 -        result = Invoke(dict, expr=LoadName(name="dict"))
    2.18 +        result = Invoke(dict, expr=LoadName(name="dict"), star=None, dstar=None)
    2.19          args = []
    2.20          for key, value in dict.items:
    2.21              tuple = Invoke(expr=LoadName(name="tuple"), star=None, dstar=None)
    2.22 @@ -210,8 +209,7 @@
    2.23          for compare, stmt in if_.tests:
    2.24              # Produce something like...
    2.25              # expr.__true__() ? body
    2.26 -            test = Conditional(test=Invoke(expr=LoadAttr(expr=self.dispatch(compare), name="__true__"),
    2.27 -                args=[], star=None, dstar=None))
    2.28 +            test = Conditional(test=Invoke(expr=LoadAttr(expr=self.dispatch(compare), name="__true__"), args=[], star=None, dstar=None))
    2.29              test.body = self.dispatch(stmt)
    2.30              nodes.append(test)
    2.31              nodes = test.else_ = []
    2.32 @@ -485,20 +483,19 @@
    2.33          return result
    2.34  
    2.35      def visitNot(self, not_):
    2.36 -        result = Not(not_, expr=Invoke(expr=LoadAttr(expr=self.dispatch(not_.expr), name="__true__"),
    2.37 -            args=[], star=None, dstar=None))
    2.38 +        result = Not(not_, expr=Invoke(expr=LoadAttr(expr=self.dispatch(not_.expr), name="__true__"), args=[], star=None, dstar=None))
    2.39          return result
    2.40  
    2.41      # Operators.
    2.42  
    2.43      def visitUnaryAdd(self, unaryadd):
    2.44 -        return Invoke(unaryadd, expr=LoadAttr(expr=self.dispatch(unaryadd.expr), name="__pos__"), args=[])
    2.45 +        return Invoke(unaryadd, expr=LoadAttr(expr=self.dispatch(unaryadd.expr), name="__pos__"), args=[], star=None, dstar=None)
    2.46  
    2.47      def visitUnarySub(self, unarysub):
    2.48 -        return Invoke(unarysub, expr=LoadAttr(expr=self.dispatch(unarysub.expr), name="__neg__"), args=[])
    2.49 +        return Invoke(unarysub, expr=LoadAttr(expr=self.dispatch(unarysub.expr), name="__neg__"), args=[], star=None, dstar=None)
    2.50  
    2.51      def visitInvert(self, invert):
    2.52 -        return Invoke(invert, expr=LoadAttr(expr=self.dispatch(invert.expr), name="__invert__"), args=[])
    2.53 +        return Invoke(invert, expr=LoadAttr(expr=self.dispatch(invert.expr), name="__invert__"), args=[], star=None, dstar=None)
    2.54  
    2.55      def visitAdd(self, add):
    2.56  
    2.57 @@ -506,8 +503,8 @@
    2.58  
    2.59          result = Choice(add)
    2.60          result.choices = [
    2.61 -            Invoke(expr=LoadAttr(expr=self.dispatch(add.left), name="__add__"), args=[self.dispatch(add.right)]),
    2.62 -            Invoke(expr=LoadAttr(expr=self.dispatch(add.right), name="__radd__"), args=[self.dispatch(add.left)])
    2.63 +            Invoke(expr=LoadAttr(expr=self.dispatch(add.left), name="__add__"), args=[self.dispatch(add.right)], star=None, dstar=None),
    2.64 +            Invoke(expr=LoadAttr(expr=self.dispatch(add.right), name="__radd__"), args=[self.dispatch(add.left)], star=None, dstar=None)
    2.65              ]
    2.66          return result
    2.67  
    2.68 @@ -529,7 +526,7 @@
    2.69              name = augassign.node
    2.70              node = self.dispatch(name)
    2.71              get_incremented = StoreTemp(
    2.72 -                expr=Invoke(expr=LoadAttr(expr=node, name=self.augassign_methods[augassign.op]), args=[expr])
    2.73 +                expr=Invoke(expr=LoadAttr(expr=node, name=self.augassign_methods[augassign.op]), args=[expr], star=None, dstar=None)
    2.74                  )
    2.75              store = StoreName(expr=LoadTemp(), name=name.name)
    2.76              result.code = [get_incremented, store, ReleaseTemp()]
    2.77 @@ -544,7 +541,7 @@
    2.78              store_expr = StoreTemp(index="expr", expr=self.dispatch(getattr.expr))
    2.79              node_attr = LoadAttr(expr=LoadTemp(index="expr"), name=getattr.attrname)
    2.80              get_incremented = StoreTemp(
    2.81 -                expr=Invoke(expr=LoadAttr(expr=node_attr, name=self.augassign_methods[augassign.op]), args=[expr])
    2.82 +                expr=Invoke(expr=LoadAttr(expr=node_attr, name=self.augassign_methods[augassign.op]), args=[expr], star=None, dstar=None)
    2.83                  )
    2.84              store = StoreAttr(expr=LoadTemp(), lvalue=LoadTemp(index="expr"), name=getattr.attrname)
    2.85              result.code = [store_expr, get_incremented, store, ReleaseTemp(index="expr"), ReleaseTemp()]
    2.86 @@ -561,7 +558,7 @@
    2.87              store_upper = StoreTemp(index="upper", expr=self.dispatch_or_none(slice.upper))
    2.88              node_slice = self._visitSlice(slice, LoadTemp(index="expr"), LoadTemp(index="lower"), LoadTemp(index="upper"), "OP_APPLY")
    2.89              get_incremented = StoreTemp(
    2.90 -                expr=Invoke(expr=LoadAttr(expr=node_slice, name=self.augassign_methods[augassign.op]), args=[expr])
    2.91 +                expr=Invoke(expr=LoadAttr(expr=node_slice, name=self.augassign_methods[augassign.op]), args=[expr], star=None, dstar=None)
    2.92                  )
    2.93              store = self._visitSlice(slice, LoadTemp(index="expr"), LoadTemp(index="lower"), LoadTemp(index="upper"), "OP_ASSIGN", LoadTemp())
    2.94              result.code = [store_expr, store_lower, store_upper, get_incremented, store,
    2.95 @@ -579,7 +576,7 @@
    2.96              store_subs = StoreTemp(index="subs", expr=subs)
    2.97              node_subscript = self._visitSubscript(subscript, LoadTemp(index="expr"), LoadTemp(index="subs"), "OP_APPLY")
    2.98              get_incremented = StoreTemp(
    2.99 -                expr=Invoke(expr=LoadAttr(expr=node_subscript, name=self.augassign_methods[augassign.op]), args=[expr])
   2.100 +                expr=Invoke(expr=LoadAttr(expr=node_subscript, name=self.augassign_methods[augassign.op]), args=[expr], star=None, dstar=None)
   2.101                  )
   2.102              store = self._visitSubscript(subscript, LoadTemp(index="expr"), LoadTemp(index="subs"), "OP_ASSIGN", LoadTemp())
   2.103              result.code = [store_expr, store_subs, get_incremented, store, ReleaseTemp(index="expr"), ReleaseTemp(index="subs"), ReleaseTemp()]
   2.104 @@ -600,9 +597,9 @@
   2.105          if not in_sequence:
   2.106              expr = LoadTemp()
   2.107          else:
   2.108 -            expr = Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"))
   2.109 +            expr = Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"), star=None, dstar=None, args=[])
   2.110          result = Assign(asslist)
   2.111 -        store = StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=expr)))
   2.112 +        store = StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=expr), star=None, dstar=None, args=[]))
   2.113          release = ReleaseTemp()
   2.114          result.code = [store] + self.dispatches(asslist.nodes, 1) + [release]
   2.115          return result
   2.116 @@ -613,7 +610,7 @@
   2.117          if not in_sequence:
   2.118              return LoadTemp()
   2.119          else:
   2.120 -            return Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"))
   2.121 +            return Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"), star=None, dstar=None, args=[])
   2.122  
   2.123      def visitAssName(self, assname, in_sequence=0):
   2.124          expr = self._visitAssNameOrAttr(assname, in_sequence)
   2.125 @@ -629,13 +626,13 @@
   2.126      def _visitSlice(self, slice, expr, lower, upper, flags, value=None):
   2.127          if flags == "OP_ASSIGN":
   2.128              args = [value]
   2.129 -            result = Invoke(expr=LoadAttr(expr=expr, name="__setslice__"))
   2.130 +            result = Invoke(expr=LoadAttr(expr=expr, name="__setslice__"), star=None, dstar=None, args=[])
   2.131          elif flags == "OP_APPLY":
   2.132              args = []
   2.133 -            result = Invoke(expr=LoadAttr(expr=expr, name="__getslice__"))
   2.134 +            result = Invoke(expr=LoadAttr(expr=expr, name="__getslice__"), star=None, dstar=None, args=[])
   2.135          elif flags == "OP_DELETE":
   2.136              args = []
   2.137 -            result = Invoke(expr=LoadAttr(expr=expr, name="__delslice__"))
   2.138 +            result = Invoke(expr=LoadAttr(expr=expr, name="__delslice__"), star=None, dstar=None, args=[])
   2.139          else:
   2.140              raise NotImplementedError, flags
   2.141  
   2.142 @@ -656,13 +653,13 @@
   2.143      def _visitSubscript(self, subscript, expr, subs, flags, value=None):
   2.144          if flags == "OP_ASSIGN":
   2.145              args = [value]
   2.146 -            result = Invoke(expr=LoadAttr(expr=expr, name="__setitem__"))
   2.147 +            result = Invoke(expr=LoadAttr(expr=expr, name="__setitem__"), star=None, dstar=None, args=[])
   2.148          elif flags == "OP_APPLY":
   2.149              args = []
   2.150 -            result = Invoke(expr=LoadAttr(expr=expr, name="__getitem__"))
   2.151 +            result = Invoke(expr=LoadAttr(expr=expr, name="__getitem__"), star=None, dstar=None, args=[])
   2.152          elif flags == "OP_DELETE":
   2.153              args = []
   2.154 -            result = Invoke(expr=LoadAttr(expr=expr, name="__delitem__"))
   2.155 +            result = Invoke(expr=LoadAttr(expr=expr, name="__delitem__"), star=None, dstar=None, args=[])
   2.156          else:
   2.157              raise NotImplementedError, flags
   2.158  
   2.159 @@ -678,7 +675,7 @@
   2.160          if len(subs) == 1:
   2.161              return self.dispatch(subs[0])
   2.162          else:
   2.163 -            return Invoke(expr=LoadName(name="tuple"), args=self.dispatches(subs))
   2.164 +            return Invoke(expr=LoadName(name="tuple"), args=self.dispatches(subs), star=None, dstar=None)
   2.165  
   2.166      def visitSubscript(self, subscript, in_sequence=0):
   2.167          value = self._visitAssNameOrAttr(subscript, in_sequence)
   2.168 @@ -811,8 +808,7 @@
   2.169          # Include a conditional statement in the subprogram.
   2.170  
   2.171          test = Conditional(else_=[])
   2.172 -        test.test = Invoke(expr=LoadAttr(expr=self.dispatch(while_.test), name="__true__"),
   2.173 -            args=[], star=None, dstar=None)
   2.174 +        test.test = Invoke(expr=LoadAttr(expr=self.dispatch(while_.test), name="__true__"), args=[], star=None, dstar=None)
   2.175  
   2.176          # Inside the conditional, add a recursive invocation to the subprogram
   2.177          # if the test condition was satisfied.
   2.178 @@ -876,17 +872,14 @@
   2.179  
   2.180          try_except = Try(body=[], else_=[], finally_=[])
   2.181          test = Conditional(
   2.182 -            test=Invoke(
   2.183 -                expr=LoadName(name="isinstance"),
   2.184 -                args=[LoadExc(), LoadName(name="StopIteration")], star=None, dstar=None
   2.185 -                ),
   2.186 +            test=Invoke(expr=LoadName(name="isinstance"), args=[LoadExc(), LoadName(name="StopIteration")], star=None, dstar=None),
   2.187              body=else_stmt,
   2.188              else_=[Raise(expr=LoadExc())])
   2.189          try_except.handler = [test]
   2.190  
   2.191          assign = Assign()
   2.192          assign.code = [
   2.193 -            StoreTemp(expr=Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"))),
   2.194 +            StoreTemp(expr=Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"), args=[], star=None, dstar=None)),
   2.195              self.dispatch(for_.assign),
   2.196              ReleaseTemp()
   2.197              ]
   2.198 @@ -909,7 +902,7 @@
   2.199  
   2.200          result = Assign(for_)
   2.201          result.code = [
   2.202 -            StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=self.dispatch(for_.list)))),
   2.203 +            StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=self.dispatch(for_.list)), args=[], star=None, dstar=None)),
   2.204              Invoke(expr=LoadRef(ref=subprogram), same_frame=1, produces_result=0, star=None, dstar=None, args=[]),
   2.205              ReleaseTemp()
   2.206              ]