[pypy-svn] r74048 - in pypy/branch/blackhole-improvement/pypy/jit/codewriter: . test

arigo at codespeak.net arigo at codespeak.net
Sun Apr 25 11:19:55 CEST 2010


Author: arigo
Date: Sun Apr 25 11:19:53 2010
New Revision: 74048

Modified:
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py
Log:
Not-too-big switches.  Integer only for now.


Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py	Sun Apr 25 11:19:53 2010
@@ -129,8 +129,9 @@
             link = block.exits[0]
             assert link.exitcase is None
             self.make_link(link)
-        else:
-            assert len(block.exits) == 2, "XXX"
+        elif len(block.exits) == 2 and (
+                isinstance(block.exitswitch, tuple) or
+                block.exitswitch.concretetype == lltype.Bool):
             linkfalse, linktrue = block.exits
             if linkfalse.llexitcase == True:
                 linkfalse, linktrue = linktrue, linkfalse
@@ -150,6 +151,31 @@
             # false path:
             self.emitline(Label(linkfalse))
             self.make_link(linkfalse)
+        else:
+            switches = [link for link in block.exits
+                        if link.exitcase != 'default']
+            if len(switches) >= 5 and isinstance(block.exitswitch.concretetype,
+                                                 lltype.Primitive):
+                XXX
+            else:
+                kind = getkind(block.exitswitch.concretetype)
+                assert kind == 'int'    # XXX
+                for switch in switches:
+                    # make the case described by 'switch'
+                    self.emitline('goto_if_not_int_eq',
+                                  TLabel(switch),
+                                  self.getcolor(block.exitswitch),
+                                  Constant(switch.llexitcase,
+                                           block.exitswitch.concretetype))
+                    # emit code for the "taken" path
+                    self.make_link(switch)
+                    # finally, emit the label for the "non-taken" path
+                    self.emitline(Label(switch))
+                #
+                if block.exits[-1].exitcase == 'default':
+                    self.make_link(block.exits[-1])
+                else:
+                    self.emitline('unreachable')
 
     def insert_renamings(self, link):
         renamings = {}

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py	Sun Apr 25 11:19:53 2010
@@ -157,3 +157,22 @@
         self.assert_format(flattener.ssarepr, """
             foobar hi_there!
         """)
+
+    def test_switch(self):
+        def f(n):
+            if n == -5:  return 12
+            elif n == 2: return 51
+            elif n == 7: return 1212
+            else:        return 42
+        self.encoding_test(f, [65], """
+            goto_if_not_int_eq L1, %i0, $-5
+            int_return $12
+            L1:
+            goto_if_not_int_eq L2, %i0, $2
+            int_return $51
+            L2:
+            goto_if_not_int_eq L3, %i0, $7
+            int_return $1212
+            L3:
+            int_return $42
+        """)



More information about the Pypy-commit mailing list