[pypy-svn] r10425 - in pypy/dist/pypy/translator/llvm: . test

cfbolz at codespeak.net cfbolz at codespeak.net
Fri Apr 8 04:58:52 CEST 2005


Author: cfbolz
Date: Fri Apr  8 04:58:52 2005
New Revision: 10425

Modified:
   pypy/dist/pypy/translator/llvm/classrepr.py
   pypy/dist/pypy/translator/llvm/funcrepr.py
   pypy/dist/pypy/translator/llvm/test/llvmsnippet.py
   pypy/dist/pypy/translator/llvm/test/test_genllvm.py
Log:
Improved exception support: catching of the instance of the exception works now.

Modified: pypy/dist/pypy/translator/llvm/classrepr.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/classrepr.py	(original)
+++ pypy/dist/pypy/translator/llvm/classrepr.py	Fri Apr  8 04:58:52 2005
@@ -262,7 +262,7 @@
             self.l_base = None
 
     def typename(self):
-        return "%std.exception* "
+        return "%std.exception*"
 
     def llvmtype(self):
         return "%std.class* "
@@ -284,13 +284,34 @@
                            (self.l_base.llvmname(), l_tmp.llvmname()))
 
     def op_simple_call(self, l_target, args, lblock, l_func):
+        print "$" * 80
+        print l_target.typed_name(),
         lblock.malloc(l_target)
         l_args0 = self.gen.get_repr(args[0])
+        print l_args0.typed_name()
+        l_tmp = self.gen.get_local_tmp(PointerTypeRepr("%std.class*",
+                                                       self.gen), l_func)
+        l_func.dependencies.update([l_args0, l_tmp])
+        lblock.getelementptr(l_tmp, l_target, [0, 0])
+        lblock.store(l_args0, l_tmp)
+        if len(args) > 1:
+            l_args1 = self.gen.get_repr(args[1])
+        else:
+            l_args1 = self.gen.get_repr(Constant(None))
+        l_tmp1 = self.gen.get_local_tmp(PointerTypeRepr("%std.list.sbyte*",
+                                                        self.gen), l_func)
         l_cast = self.gen.get_local_tmp(PointerTypeRepr("%std.list.sbyte",
                                                         self.gen), l_func)
-        l_tmp = self.gen.get_local_tmp(PointerTypeRepr("%std.list.sbyte*",
-                                                       self.gen), l_func)
-        l_func.dependencies.update([l_args0, l_cast, l_tmp])
-        lblock.cast(l_cast, l_args0)
-        lblock.getelementptr(l_tmp, l_target, [0, 1])
-        lblock.store(l_cast, l_tmp)
+        l_func.dependencies.update([l_args1, l_tmp1])
+        lblock.getelementptr(l_tmp1, l_target, [0, 1])
+        lblock.cast(l_cast, l_args1)
+        lblock.store(l_cast, l_tmp1)
+
+    def t_op_type(self, l_target, args, lblock, l_func):
+        l_args0 = self.gen.get_repr(args[0])
+        l_func.dependencies.add(l_args0)
+        l_tmp = self.gen.get_local_tmp(
+            PointerTypeRepr("%std.class*", self.gen), l_func)
+        lblock.getelementptr(l_tmp, l_args0, [0, 0])
+        lblock.load(l_target, l_tmp)
+    

Modified: pypy/dist/pypy/translator/llvm/funcrepr.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/funcrepr.py	(original)
+++ pypy/dist/pypy/translator/llvm/funcrepr.py	Fri Apr  8 04:58:52 2005
@@ -23,7 +23,7 @@
                  "inplace_mod", "inplace_pow", "inplace_lshift",
                  "inplace_rshift", "inplace_and", "inplace_or", "inplace_xor",
                  "contains", "newlist", "newtuple", "alloc_and_set",
-                 "issubtype"]
+                 "issubtype", "type"]
 
 C_SIMPLE_TYPES = {annmodel.SomeChar: "char",
                   annmodel.SomeString: "char*",
@@ -304,12 +304,17 @@
         l_exitcases = [self.gen.get_repr(ex.exitcase)
                        for ex in self.pyblock.exits[1:]]
         self.l_func.dependencies.update(l_exitcases)
-        sw = [(str(abs(id(ex.exitcase))), "%" + l_l.toblock)
-              for ex, l_l in zip(self.pyblock.exits[1:], l_exits)]
-        lexcblock.switch(l_ui, "%" + self.lblock.label + ".unwind", sw)
-        lunwindblock = llvmbc.BasicBlock(self.lblock.label + ".unwind")
-        lunwindblock.unwind()
-        self.l_func.add_block(lunwindblock)
+        # XXX XXX XXX: For now we assume, that if there is only one exit
+        # and it's exitcase is Exception, this should match anything
+        if len(l_exits) == 1 and self.pyblock.exits[1].exitcase == Exception:
+            lexcblock.uncond_branch("%" + l_exits[0].toblock)
+        else:
+            sw = [(str(abs(id(ex.exitcase))), "%" + l_l.toblock)
+                  for ex, l_l in zip(self.pyblock.exits[1:], l_exits)]
+            lexcblock.switch(l_ui, "%" + self.lblock.label + ".unwind", sw)
+            lunwindblock = llvmbc.BasicBlock(self.lblock.label + ".unwind")
+            lunwindblock.unwind()
+            self.l_func.add_block(lunwindblock)
 
 class ExceptBlockRepr(BlockRepr):
     def create_space_ops(self):

Modified: pypy/dist/pypy/translator/llvm/test/llvmsnippet.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/test/llvmsnippet.py	(original)
+++ pypy/dist/pypy/translator/llvm/test/llvmsnippet.py	Fri Apr  8 04:58:52 2005
@@ -306,8 +306,6 @@
         return 2
     return 4
 
-
-#doesn't work yet!
 class MyException(Exception):
     def __init__(self, n):
         self.n = n
@@ -317,6 +315,8 @@
         raise MyException, 12
     if i == 4:
         raise IndexError
+    if i > 5:
+        raise MyException(i)
     return 1
 
 def catches(i):

Modified: pypy/dist/pypy/translator/llvm/test/test_genllvm.py
==============================================================================
--- pypy/dist/pypy/translator/llvm/test/test_genllvm.py	(original)
+++ pypy/dist/pypy/translator/llvm/test/test_genllvm.py	Fri Apr  8 04:58:52 2005
@@ -295,6 +295,16 @@
         for i in range(10, 20):
             assert f(i) == 2
 
+    def test_catch_instance(self):
+        f = compile_function(llvmsnippet.catches, [int])
+        assert f(1) == 1
+        assert f(2) == 1
+        assert f(3) == 12
+        py.test.raises(RuntimeError, "f(4)")
+        assert f(5) == 1
+        assert f(6) == 6
+        assert f(13) == 13
+        
 
 class TestSnippet(object):
     def setup_method(self, method):



More information about the Pypy-commit mailing list