[pypy-svn] r11751 - in pypy/dist: goal pypy/translator/genc
tismer at codespeak.net
tismer at codespeak.net
Mon May 2 01:42:29 CEST 2005
Author: tismer
Date: Mon May 2 01:42:29 2005
New Revision: 11751
Modified:
pypy/dist/goal/targetpypy1.py
pypy/dist/pypy/translator/genc/ctyper.py
pypy/dist/pypy/translator/genc/g_support.h
pypy/dist/pypy/translator/genc/int_include.h
Log:
added exception macros to g_support.h
added new specializations to ctyper.py
implemented them in int_include.h
added operations to targetpypy1.py
will continue with this
Modified: pypy/dist/goal/targetpypy1.py
==============================================================================
--- pypy/dist/goal/targetpypy1.py (original)
+++ pypy/dist/goal/targetpypy1.py Mon May 2 01:42:29 2005
@@ -6,13 +6,16 @@
# __________ Entry point __________
def entry_point():
- w_a = W_IntObject(space, -6)
- w_b = W_IntObject(space, -7)
- return mmentrypoint(space, w_a, w_b)
+ w_a = W_IntObject(space, -7)
+ w_b = W_IntObject(space, -6)
+ ret_mul = mmentrypoints["mul"](space, w_a, w_b)
+ ret_add = mmentrypoints["add"](space, w_a, w_b)
+ ret_sub = mmentrypoints["sub"](space, w_a, w_b)
+ return ret_mul, ret_add, ret_sub
# _____ Define and setup target _____
def target():
- global space, mmentrypoint
+ global space, mmentrypoints
# disable translation of the whole of classobjinterp.py
StdObjSpace.setup_old_style_classes = lambda self: None
space = StdObjSpace()
@@ -20,14 +23,15 @@
buildcache2.buildcache(space)
# ------------------------------------------------------------
- name = 'mul'
- mm = space.MM.mul
- exprargs, expr, miniglobals, fallback = (
- mm.install_not_sliced(space.model.typeorder, baked_perform_call=False))
- func = stdtypedef.make_perform_trampoline('__mm_'+name,
- exprargs, expr, miniglobals,
- mm)
- mmentrypoint = func
+ mmentrypoints = {}
+ for name in "mul add sub".split():
+ mm = getattr(space.MM, name)
+ exprargs, expr, miniglobals, fallback = (
+ mm.install_not_sliced(space.model.typeorder, baked_perform_call=False))
+ func = stdtypedef.make_perform_trampoline('__mm_'+name,
+ exprargs, expr, miniglobals,
+ mm)
+ mmentrypoints[name] = func
# ------------------------------------------------------------
# further call the entry_point once to trigger building remaining
@@ -39,7 +43,8 @@
# _____ Run translated _____
def run(c_entry_point):
- w_result = c_entry_point()
- print w_result
- print w_result.intval
- assert w_result.intval == 42
+ res_w = c_entry_point()
+ res = tuple([each.intval for each in res_w])
+ print res
+ assert res == (-7 * -6, -7 + -6, -7 - -6)
+
\ No newline at end of file
Modified: pypy/dist/pypy/translator/genc/ctyper.py
==============================================================================
--- pypy/dist/pypy/translator/genc/ctyper.py (original)
+++ pypy/dist/pypy/translator/genc/ctyper.py Mon May 2 01:42:29 2005
@@ -35,12 +35,20 @@
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),
- ('sub', 'int_sub', TInt, TInt, TInt),
- ('inplace_sub', 'int_sub', TInt, TInt, TInt),
- ('is_true', 'int_is_true', TInt, TInt),
+ ## 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),
],
)
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 Mon May 2 01:42:29 2005
@@ -11,6 +11,13 @@
#define MOVE(x, y) y = x;
+#define FAIL_EXCEPTION(err, exc, msg) \
+ { \
+ PyErr_SetString(exc, msg); \
+ FAIL(err) \
+ }
+#define FAIL_OVF(err, msg) FAIL_EXCEPTION(err, PyExc_OverflowError, 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
as methods: builtin function objects that can be bound on instances */
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 Mon May 2 01:42:29 2005
@@ -6,9 +6,79 @@
#define OP_INCREF_int(x) /* nothing */
#define OP_DECREF_int(x) /* nothing */
#define CONV_TO_OBJ_int PyInt_FromLong
-#define CONV_FROM_OBJ_int PyInt_AsLong
+#define CONV_FROM_OBJ_int PyInt_AS_LONG
#define OP_INT_IS_TRUE(x,r,err) r = (x != 0);
#define OP_INT_ADD(x,y,r,err) r = x + y;
+
+#define OP_INT_ADD_OVF(x,y,r,err) \
+ r = x + y; \
+ 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_OVF(x,y,r,err) \
+ r = x - y; \
+ if ((r^x) >= 0 || (r^~y) >= 0); \
+ else FAIL_OVF(err, "integer subtraction")
+
+#define OP_INT_MUL(x,y,r,err) r = x * y;
+
+#ifndef HAVE_LONG_LONG
+
+#define OP_INT_MUL_OVF(x,y,r,err) \
+ if (op_int_mul_ovf(x,y,&r)); \
+ else FAIL_OVF(err, "integer multiplication")
+
+#else
+
+#define OP_INT_MUL_OVF(x,y,r,err) \
+ { \
+ 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 */
+
+/* _________________ certain implementations __________________ */
+
+#ifndef HAVE_LONG_LONG
+/* adjusted from intobject.c, Python 2.3.3 */
+int
+op_int_mul_ovf(long a, long b, long *longprod)
+{
+ double doubled_longprod; /* (double)longprod */
+ double doubleprod; /* (double)a * (double)b */
+
+ *longprod = a * b;
+ doubleprod = (double)a * (double)b;
+ doubled_longprod = (double)*longprod;
+
+ /* Fast path for normal case: small multiplicands, and no info
+ is lost in either method. */
+ if (doubled_longprod == doubleprod)
+ return 1;
+
+ /* Somebody somewhere lost info. Close enough, or way off? Note
+ that a != 0 and b != 0 (else doubled_longprod == doubleprod == 0).
+ The difference either is or isn't significant compared to the
+ true value (of which doubleprod is a good approximation).
+ */
+ {
+ const double diff = doubled_longprod - doubleprod;
+ const double absdiff = diff >= 0.0 ? diff : -diff;
+ const double absprod = doubleprod >= 0.0 ? doubleprod :
+ -doubleprod;
+ /* absdiff/absprod <= 1/32 iff
+ 32 * absdiff <= absprod -- 5 good bits is "close enough" */
+ if (32.0 * absdiff <= absprod)
+ return 1;
+ return 0;
+ }
+}
+#endif /* HAVE_LONG_LONG */
More information about the Pypy-commit
mailing list