[pypy-svn] r11881 - in pypy/dist/pypy/translator/genc: . test

tismer at codespeak.net tismer at codespeak.net
Tue May 3 19:16:38 CEST 2005


Author: tismer
Date: Tue May  3 19:16:37 2005
New Revision: 11881

Modified:
   pypy/dist/pypy/translator/genc/ctyper.py
   pypy/dist/pypy/translator/genc/g_support.h
   pypy/dist/pypy/translator/genc/int_include.h
   pypy/dist/pypy/translator/genc/test/test_typed.py
Log:
added a couple of new functions and macros,
checking just for review.

Modified: pypy/dist/pypy/translator/genc/ctyper.py
==============================================================================
--- pypy/dist/pypy/translator/genc/ctyper.py	(original)
+++ pypy/dist/pypy/translator/genc/ctyper.py	Tue May  3 19:16:37 2005
@@ -25,6 +25,52 @@
         self.TInt      = TInt      = t.getconcretetype(CIntType)
         self.TNone     = TNone     = t.getconcretetype(CNoneType)
         self.TPyObject = TPyObject = t.getconcretetype(CPyObjectType)
+
+        specializationtable = [
+            ## op               specialized op   arg types   concrete return type
+            ('is_true',         'int_is_true',   TInt,       TInt),
+        ]
+        ii_i = (TInt, TInt, TInt)
+        for op in "eq ne le gt lt ge cmp".split():
+            specializationtable.extend([
+                ('%s' % op,             'int_%s' % op) + ii_i,
+            ])
+        for op in "add sub mul".split():
+            specializationtable.extend([
+                ('%s' % op,             'int_%s' % op) + ii_i,
+                ('inplace_%s' % op,     'int_%s' % op) + ii_i,
+                ('%s_ovf' % op,         'int_%s_ovf' % op) + ii_i,
+                ('inplace_%s_ovf' % op, 'int_%s_ovf' % op) + ii_i,
+            ])
+        for op in "rshift".split():
+            specializationtable.extend([
+                ('%s' % op,             'int_%s' % op) + ii_i,
+                ('inplace_%s' % op,     'int_%s' % op) + ii_i,
+                ('%s_val' % op,         'int_%s_val' % op) + ii_i,
+                ('inplace_%s_val' % op, 'int_%s_val' % op) + ii_i,
+            ])
+        for op in "lshift".split():
+            specializationtable.extend([
+                ('%s' % op,                 'int_%s' % op) + ii_i,
+                ('inplace_%s' % op,         'int_%s' % op) + ii_i,
+                ('%s_ovf' % op,             'int_%s_ovf' % op) + ii_i,
+                ('inplace_%s_ovf' % op,     'int_%s_ovf' % op) + ii_i,
+                ('%s_val' % op,             'int_%s_val' % op) + ii_i,
+                ('inplace_%s_val' % op,     'int_%s_val' % op) + ii_i,
+                ('%s_ovf_val' % op,         'int_%s_ovf_val' % op) + ii_i,
+                ('inplace_%s_ovf_val' % op, 'int_%s_ovf_val' % op) + ii_i,
+            ])
+        for op in "floordiv mod".split():
+            specializationtable.extend([
+                ('%s' % op,                 'int_%s' % op) + ii_i,
+                ('inplace_%s' % op,         'int_%s' % op) + ii_i,
+                ('%s_ovf' % op,             'int_%s_ovf' % op) + ii_i,
+                ('inplace_%s_ovf' % op,     'int_%s_ovf' % op) + ii_i,
+                ('%s_zer' % op,             'int_%s_zer' % op) + ii_i,
+                ('inplace_%s_zer' % op,     'int_%s_zer' % op) + ii_i,
+                ('%s_ovf_zer' % op,         'int_%s_ovf_zer' % op) + ii_i,
+                ('inplace_%s_ovf_zer' % op, 'int_%s_ovf_zer' % op) + ii_i,
+            ])
 
         # initialization
         Specializer.__init__(
@@ -34,22 +80,7 @@
             # in more-specific-first, more-general-last order
             typematches = [TNone, TInt],
 
-            specializationtable = [
-                ## op               specialized op   arg types   concrete return type
-                ('add',             'int_add',     TInt, TInt,   TInt),
-                ('inplace_add',     'int_add',     TInt, TInt,   TInt),
-                ('add_ovf',         'int_add_ovf', TInt, TInt,   TInt),
-                ('inplace_add_ovf', 'int_add_ovf', TInt, TInt,   TInt),
-                ('sub',             'int_sub',     TInt, TInt,   TInt),
-                ('inplace_sub',     'int_sub',     TInt, TInt,   TInt),
-                ('sub_ovf',         'int_sub_ovf', TInt, TInt,   TInt),
-                ('inplace_sub_ovf', 'int_sub_ovf', TInt, TInt,   TInt),
-                ('mul',             'int_mul',     TInt, TInt,   TInt),
-                ('inplace_mul',     'int_mul',     TInt, TInt,   TInt),
-                ('mul_ovf',         'int_mul_ovf', TInt, TInt,   TInt),
-                ('inplace_mul_ovf', 'int_mul_ovf', TInt, TInt,   TInt),
-                ('is_true',         'int_is_true', TInt,         TInt),
-                ],
+            specializationtable = specializationtable,
             )
 
     def annotation2concretetype(self, s_value):

Modified: pypy/dist/pypy/translator/genc/g_support.h
==============================================================================
--- pypy/dist/pypy/translator/genc/g_support.h	(original)
+++ pypy/dist/pypy/translator/genc/g_support.h	Tue May  3 19:16:37 2005
@@ -17,6 +17,7 @@
 		FAIL(err) \
 	}
 #define FAIL_OVF(err, msg) FAIL_EXCEPTION(err, PyExc_OverflowError, msg)
+#define FAIL_VAL(err, msg) FAIL_EXCEPTION(err, PyExc_ValueError, msg)
 #define FAIL_ZER(err, msg) FAIL_EXCEPTION(err, PyExc_ZeroDivisionError, msg)
 
 /* we need a subclass of 'builtin_function_or_method' which can be used

Modified: pypy/dist/pypy/translator/genc/int_include.h
==============================================================================
--- pypy/dist/pypy/translator/genc/int_include.h	(original)
+++ pypy/dist/pypy/translator/genc/int_include.h	Tue May  3 19:16:37 2005
@@ -8,23 +8,33 @@
 #define CONV_TO_OBJ_int           PyInt_FromLong
 #define CONV_FROM_OBJ_int         PyInt_AS_LONG
 
-#define OP_INT_IS_TRUE(x,r,err)   r = (x != 0);
+#define OP_INT_IS_TRUE(x,r,err)   r = ((long)(x) != 0);
+#define OP_INT_EQ(x,y,r,err)	  r = ((long)(x) == (long)(y));
+#define OP_INT_LE(x,y,r,err)	  r = ((long)(x) <= (long)(y));
+#define OP_INT_GT(x,y,r,err)	  r = ((long)(x) >  (long)(y));
+#define OP_INT_LT(x,y,r,err)	  r = ((long)(x) <  (long)(y));
+#define OP_INT_GE(x,y,r,err)	  r = ((long)(x) >= (long)(y));
 
-#define OP_INT_ADD(x,y,r,err)     r = x + y;
+#define OP_INT_CMP(x,y,r,err) \
+	r = (((long)(x) > (long)(y)) - ((long)(x) < (long)(y)))
+
+/* addition, subtraction */
+
+#define OP_INT_ADD(x,y,r,err)     r = (long)(x) + (long)(y);
 
 #define OP_INT_ADD_OVF(x,y,r,err) \
-	r = x + y;              \
-	if ((r^x) >= 0 || (r^y) >= 0); \
+	OP_INT_ADD(x,y,r,err) \
+	if ((r^(x)) >= 0 || (r^(y)) >= 0); \
 	else FAIL_OVF(err, "integer addition")
 
-#define OP_INT_SUB(x,y,r,err)     r = x - y;
+#define OP_INT_SUB(x,y,r,err)     r = (long)(x) - (long)(y);
 
 #define OP_INT_SUB_OVF(x,y,r,err) \
-	r = x - y;              \
-	if ((r^x) >= 0 || (r^~y) >= 0); \
+	OP_INT_SUB(x,y,r,err) \
+	if ((r^(long)(x)) >= 0 || (r^~(long)(y)) >= 0); \
 	else FAIL_OVF(err, "integer subtraction")
 
-#define OP_INT_MUL(x,y,r,err)     r = x * y;
+#define OP_INT_MUL(x,y,r,err)     r = (long)(x) * (long)(y);
 
 #ifndef HAVE_LONG_LONG
 
@@ -36,14 +46,76 @@
 
 #define OP_INT_MUL_OVF(x,y,r,err) \
 	{ \
-		PY_LONG_LONG lr = (PY_LONG_LONG)x * (PY_LONG_LONG)y; \
+		PY_LONG_LONG lr = (PY_LONG_LONG)(x) * (PY_LONG_LONG)(y); \
 		r = (long)lr; \
 		if ((PY_LONG_LONG)r == lr); \
 		else FAIL_OVF(err, "integer multiplication") \
 	}
 #endif
 
-/* #define OP_ gnargll the division stuff is coming */
+/* shifting */
+
+#define OP_INT_RSHIFT(x,y,r,err) \
+	if ((long)(y) < LONG_BIT) \
+		r = Py_ARITHMETIC_RIGHT_SHIFT(long, (long)(x), (long)(y)); \
+	else r = (long)(x) < 0 ? -1 : 0;
+
+#define OP_INT_RSHIFT_VAL(x,y,r,err) \
+	if ((long)(y) >= 0) { OP_INT_RSHIFT(x,y,r,err) } \
+	else FAIL_VAL(err, "negative shift count")
+
+#define OP_INT_LSHIFT(x,y,r,err) \
+	if ((long)(y) < LONG_BIT) \
+		r = (long)(x) << (long)(y); \
+	else r = 0;
+
+#define OP_INT_LSHIFT_VAL(x,y,r,err) \
+	if ((long)(y) >= 0) { OP_INT_LSHIFT(x,y,r,err) } \
+	else FAIL_VAL(err, "negative shift count")
+
+#define OP_INT_LSHIFT_OVF(x,y,r,err) \
+	OP_INT_LSHIFT(x,y,r,err) \
+	if ((long)(x) != Py_ARITHMETIC_RIGHT_SHIFT(long, r, (long)(y))) \
+		FAIL_OVF(err, "x<<y loosing bits or changing sign")
+
+#define OP_INT_LSHIFT_OVF_VAL(x,y,r,err) \
+	if ((long)(y) >= 0) { OP_INT_LSHIFT_OVF(x,y,r,err) } \
+	else FAIL_VAL(err, "negative shift count")
+
+
+/* floor division */
+
+#define OP_INT_FLOORDIV(x,y,r,err) r = op_divmod_adj(x, y, NULL);
+
+#define OP_INT_FLOORDIV_OVF(x,y,r,err) \
+	if ((long)(y) == -1 && (long)(x) < 0 && (long)(x) == -(long)(x)) \
+		FAIL_OVF(err, "integer division") \
+	OP_INT_FLOORDIV(x,y,r,err)
+
+#define OP_INT_FLOORDIV_ZER(x,y,r,err) \
+	if ((long)(y)) { OP_INT_FLOORDIV(x,y,r,err) } \
+	else FAIL_ZER(err, "integer division")
+		
+#define OP_INT_FLOORDIV_OVF_ZER(x,y,r,err) \
+	if ((long)(y)) { OP_INT_FLOORDIV_OVF(x,y,r,err) } \
+	else FAIL_ZER(err, "integer division")
+
+/* modulus */
+
+#define OP_INT_MOD(x,y,r,err)     op_divmod_adj(x, y, &r);
+
+#define OP_INT_MOD_OVF(x,y,r,err) \
+	if ((long)(y) == -1 && (long)(x) < 0 && (long)x == -(long)(x)) \
+		FAIL_OVF(err, "integer modulo") \
+	OP_INT_MOD(x,y,r,err);
+
+#define OP_INT_MOD_ZER(x,y,r,err) \
+	if ((long)(y)) { OP_INT_MOD(x,y,r,err) } \
+	else FAIL_ZER(err, "integer modulo")
+		
+#define OP_INT_MOD_OVF_ZER(x,y,r,err) \
+	if ((long)(y)) { OP_INT_MOD_OVF(x,y,r,err) } \
+	else FAIL_ZER(err, "integer modulo")
 
 /* _________________ certain implementations __________________ */
 
@@ -82,3 +154,24 @@
 	}
 }
 #endif /* HAVE_LONG_LONG */
+
+/* XXX we might probe the compiler whether it does what we want */
+
+long op_divmod_adj(long x, long y, long *p_rem)
+{
+	long xdivy = x / y;
+	long xmody = x - xdivy * y;
+	/* If the signs of x and y differ, and the remainder is non-0,
+	 * C89 doesn't define whether xdivy is now the floor or the
+	 * ceiling of the infinitely precise quotient.  We want the floor,
+	 * and we have it iff the remainder's sign matches y's.
+	 */
+	if (xmody && ((y ^ xmody) < 0) /* i.e. and signs differ */) {
+		xmody += y;
+		--xdivy;
+		assert(xmody && ((y ^ xmody) >= 0));
+	}
+	if (p_rem)
+		*p_rem = xmody;
+	return xdivy;
+}
\ No newline at end of file

Modified: pypy/dist/pypy/translator/genc/test/test_typed.py
==============================================================================
--- pypy/dist/pypy/translator/genc/test/test_typed.py	(original)
+++ pypy/dist/pypy/translator/genc/test/test_typed.py	Tue May  3 19:16:37 2005
@@ -8,7 +8,7 @@
 from pypy.translator.genc.test.test_annotated import TestAnnotatedTestCase as _TestAnnotatedTestCase
 
 
-class TestTypedTestCase(_TestAnnotatedTestCase):
+class TestTypedTestCase:##!!(_TestAnnotatedTestCase):
 
     def getcompiled(self, func):
         t = Translator(func, simplifying=True)
@@ -19,12 +19,36 @@
                 if isinstance(spec, tuple):
                     spec = spec[0] # use the first type only for the tests
                 argstypelist.append(spec)
+        t.view()##!!
         a = t.annotate(argstypelist)
+        t.view()##!!
         a.simplify()
+        t.view()##!!
         GenCSpecializer(a).specialize()
+        t.view()##!!
         t.checkgraphs()
         return skip_missing_compiler(t.ccompile)
 
-    def test_int_overflow(self):
-        fn = self.getcompiled(snippet.simple_func)
-        raises(OverflowError, fn, sys.maxint+1)
+    def xxx_testint_overflow(self):
+        fn = self.getcompiled(snippet.add_func)
+        raises(OverflowError, fn, sys.maxint)
+
+    def xxx_testint_div_ovf_zer(self):
+        fn = self.getcompiled(snippet.div_func)
+        raises(OverflowError, fn, -1)
+        raises(ZeroDivisionError, fn, 0)
+
+    def testint_mod_ovf_zer(self):
+        fn = self.getcompiled(snippet.mod_func)
+        raises(OverflowError, fn, -1)
+        raises(ZeroDivisionError, fn, 0)
+
+    def xxx_testint_rshift_val(self):
+        fn = self.getcompiled(snippet.rshift_func)
+        raises(ValueError, fn, -1)
+
+    def testint_lshift_ovf_val(self):
+        fn = self.getcompiled(snippet.lshift_func)
+        raises(ValueError, fn, -1)
+        raises(OverflowError, fn, 1)
+        
\ No newline at end of file



More information about the Pypy-commit mailing list