[pypy-svn] rev 1738 - in pypy/trunk/src/pypy/translator: . test

sanxiyn at codespeak.net sanxiyn at codespeak.net
Sat Oct 11 21:22:30 CEST 2003


Author: sanxiyn
Date: Sat Oct 11 21:22:30 2003
New Revision: 1738

Modified:
   pypy/trunk/src/pypy/translator/gencl.py
   pypy/trunk/src/pypy/translator/test/test_cltrans.py
Log:
Integrated annotator. Proper truth testing.


Modified: pypy/trunk/src/pypy/translator/gencl.py
==============================================================================
--- pypy/trunk/src/pypy/translator/gencl.py	(original)
+++ pypy/trunk/src/pypy/translator/gencl.py	Sat Oct 11 21:22:30 2003
@@ -1,5 +1,6 @@
 import autopath
 from pypy.translator.flowmodel import *
+from pypy.translator.annotation import Annotator
 
 class Op:
     def __init__(self, gen, op):
@@ -28,20 +29,31 @@
     def __init__(self, fun):
         self.fun = fun
         self.blockref = {}
+        self.annotate([])
+    def annotate(self, input_arg_types):
+        ann = Annotator(self.fun)
+        ann.build_types(input_arg_types)
+        ann.simplify()
+        self.ann = ann
     def str(self, obj):
         if isinstance(obj, Variable):
             return obj.pseudoname
         elif isinstance(obj, Constant):
             return self.conv(obj.value)
         else:
-            return "#<" # unreadable
+            return "#<"
     def conv(self, val):
         if val is None:
             return "nil"
+        elif isinstance(val, bool): # should precedes int
+            if val:
+                return "t"
+            else:
+                return "nil"
         elif isinstance(val, int):
             return str(val)
         else:
-            return "#<" # unreadable
+            return "#<"
     def emitcode(self):
         import sys
         from cStringIO import StringIO
@@ -69,6 +81,7 @@
             print "(go", "tag" + str(tag), ")"
             print ")" # close tagbody
             return
+        self.cur_block = block
         print "tag" + str(tag)
         for op in block.operations:
             emit_op = Op(self, op)
@@ -88,7 +101,7 @@
         if branch.target.has_renaming:
             source = branch.args
             target = branch.target.input_args
-            print "(psetq",   # parallel assignment
+            print "(psetq", # parallel assignment
             for item in zip(source, target):
                 init, var = map(self.str, item)
                 print var, init,
@@ -104,5 +117,20 @@
         retval = self.str(branch.returnvalue)
         print "(return", retval, ")"
     def emit_truth_test(self, obj):
-        # XXX: Fix this
-        print "(not (zerop", self.str(obj), "))"
+        annset = self.ann.annotated[self.cur_block]
+        tp = annset.get_type(obj)
+        s = self.str
+        if tp is bool:
+            print s(obj)
+        elif tp is int:
+            print "(not (zerop", s(obj), "))"
+        else:
+            print self.template(s(obj), [
+                "(typecase %",
+                "(boolean %)",
+                "(fixnum (not (zerop %)))",
+                ")"])
+    def template(self, sub, seq):
+        def _(x):
+            return x.replace("%", sub)
+        return "\n".join(map(_, seq))

Modified: pypy/trunk/src/pypy/translator/test/test_cltrans.py
==============================================================================
--- pypy/trunk/src/pypy/translator/test/test_cltrans.py	(original)
+++ pypy/trunk/src/pypy/translator/test/test_cltrans.py	Sat Oct 11 21:22:30 2003
@@ -14,6 +14,17 @@
             raise test.TestSkip
 
     #___________________________________
+    def if_then_else(cond, x, y):
+        if cond:
+            return x
+        else:
+            return y
+    def test_if(self):
+        cl_if = make_cl_func(self.if_then_else, self.cl, udir)
+        self.assertEquals(cl_if(True, 50, 100), 50)
+        self.assertEquals(cl_if(False, 50, 100), 100)
+
+    #___________________________________
     def my_gcd(a, b):
         r = a % b
         while r:


More information about the Pypy-commit mailing list