[pypy-svn] r75845 - in pypy/branch/fast-ctypes/pypy: module/jitffi module/jitffi/test rlib rlib/test
getxsick at codespeak.net
getxsick at codespeak.net
Mon Jul 5 19:02:56 CEST 2010
Author: getxsick
Date: Mon Jul 5 19:02:54 2010
New Revision: 75845
Modified:
pypy/branch/fast-ctypes/pypy/module/jitffi/interp_jitffi.py
pypy/branch/fast-ctypes/pypy/module/jitffi/test/test_jitffi.py
pypy/branch/fast-ctypes/pypy/rlib/rjitffi.py
pypy/branch/fast-ctypes/pypy/rlib/test/test_rjitffi.py
Log:
functions with various type of arguments can be called now
limitations:
- can't "recall" function (stack has to be cleaned up)
- rjitffi.call doesn't accept args (only module.jitffi or by using push_*())
- tests for rjitffi fails because of the above
Modified: pypy/branch/fast-ctypes/pypy/module/jitffi/interp_jitffi.py
==============================================================================
--- pypy/branch/fast-ctypes/pypy/module/jitffi/interp_jitffi.py (original)
+++ pypy/branch/fast-ctypes/pypy/module/jitffi/interp_jitffi.py Mon Jul 5 19:02:54 2010
@@ -34,17 +34,27 @@
if space.is_w(w_args, space.w_None):
return space.wrap(self.call())
else:
- if self.args_type[0] == 'int':
- args_w = [ space.int_w(w_x) for w_x in space.listview(w_args) ]
- elif self.args_type[0] == 'float':
- args_w = [ space.float_w(w_x) for w_x in space.listview(w_args) ]
- else:
- raise OperationError(
- space.w_TypeError,
- space.wrap('Unsupported type of argument: %s'
- % self.args_type[0]))
-
- return space.wrap(self.call(args_w))
+ i = 0
+ w_iterator = space.iter(w_args)
+ while True:
+ try:
+ w_arg = space.next(w_iterator)
+ except OperationError, e:
+ if not e.match(space, space.w_StopIteration):
+ raise
+ break # done
+
+ if self.args_type[i] == 'int':
+ self.push_int(space.int_w(w_arg))
+ elif self.args_type[i] == 'float':
+ self.push_float(space.float_w(w_arg))
+ else:
+ raise OperationError(
+ space.w_TypeError,
+ space.wrap('Unsupported type of argument: %s'
+ % self.args_type[0]))
+ i += 1
+ return space.wrap(self.call())
def W_Get___new__(space, w_type, cpu, lib, func, args_type, res_type):
try:
Modified: pypy/branch/fast-ctypes/pypy/module/jitffi/test/test_jitffi.py
==============================================================================
--- pypy/branch/fast-ctypes/pypy/module/jitffi/test/test_jitffi.py (original)
+++ pypy/branch/fast-ctypes/pypy/module/jitffi/test/test_jitffi.py Mon Jul 5 19:02:54 2010
@@ -20,6 +20,12 @@
return a+b;
}
+ int add_intfloat(int a, double b)
+ {
+ int rb = (int)b;
+ return a+rb;
+ }
+
double return_float(int a, int b)
{
return a+b;
@@ -46,8 +52,8 @@
'''
))
- symbols = ['add_integers', 'add_floats', 'return_float',
- 'max3', 'fvoid', 'return_void']
+ symbols = ['add_integers', 'add_floats', 'add_intfloat',
+ 'return_float', 'max3', 'fvoid', 'return_void']
eci = ExternalCompilationInfo(export_symbols=symbols)
return str(platform.compile([c_file], eci, 'x', standalone=False))
@@ -90,6 +96,13 @@
func = lib.get('return_void', ['int', 'int'])
assert func.call([1, 2]) is None
+ def test_various_type_args(self):
+ import jitffi
+ lib = jitffi.CDLL(self.lib_name)
+
+ func = lib.get('add_intfloat', ['int', 'float'], 'int')
+ assert func.call([1, 2.9]) == 3
+
def test_undefined_func(self):
import jitffi
lib = jitffi.CDLL(self.lib_name)
Modified: pypy/branch/fast-ctypes/pypy/rlib/rjitffi.py
==============================================================================
--- pypy/branch/fast-ctypes/pypy/rlib/rjitffi.py (original)
+++ pypy/branch/fast-ctypes/pypy/rlib/rjitffi.py Mon Jul 5 19:02:54 2010
@@ -33,6 +33,7 @@
self.res_type = res_type
self.cpu = cpu
self.lib = lib.handler
+ self.esp = 1 # 0 is a func addr
if self.res_type == 'int':
self.bres = BoxInt()
@@ -70,19 +71,10 @@
FUNC = deref(FPTR)
self.calldescr = self.cpu.calldescrof(FUNC, FUNC.ARGS, FUNC.RESULT)
- def call(self, func_args=None):
- if func_args is None:
- func_args = []
-
- bargs = []
- for tp, value in zip(self.args_type, func_args):
- if tp == 'int':
- bargs.append(BoxInt(value))
- elif tp == 'float':
- bargs.append(BoxFloat(value))
- elif tp == 'ref':
- bargs.append(BoxPtr(value))
- inputargs = [self.bfuncaddr] + bargs
+ self.bargs = []
+
+ def call(self):
+ inputargs = [self.bfuncaddr] + self.bargs
oplist = [ResOperation(rop.CALL, inputargs, self.bres,
descr=self.calldescr),
@@ -90,16 +82,7 @@
descr=BasicFailDescr(0))]
looptoken = LoopToken()
self.cpu.compile_loop(inputargs, oplist, looptoken)
-
- for i, box in enumerate(inputargs):
- if i == 0: # func address
- self.cpu.set_future_value_int(i, box.getint())
- elif self.args_type[i-1] == 'int':
- self.cpu.set_future_value_int(i, box.getint())
- elif self.args_type[i-1] == 'float':
- self.cpu.set_future_value_float(i, box.getfloat())
- elif self.args_type[i-1] == 'ref':
- self.cpu.set_future_value_ref(i, box.getref())
+ self.cpu.set_future_value_int(0, self.bfuncaddr.getint())
res = self.cpu.execute_token(looptoken)
if res is oplist[-1].descr:
@@ -118,3 +101,13 @@
else:
raise ValueError(self.res_type)
return r
+
+ def push_int(self, value):
+ self.cpu.set_future_value_int(self.esp, value)
+ self.bargs.append(BoxInt(value))
+ self.esp += 1
+
+ def push_float(self, value):
+ self.cpu.set_future_value_float(self.esp, value)
+ self.bargs.append(BoxFloat(value))
+ self.esp += 1
Modified: pypy/branch/fast-ctypes/pypy/rlib/test/test_rjitffi.py
==============================================================================
--- pypy/branch/fast-ctypes/pypy/rlib/test/test_rjitffi.py (original)
+++ pypy/branch/fast-ctypes/pypy/rlib/test/test_rjitffi.py Mon Jul 5 19:02:54 2010
@@ -21,6 +21,12 @@
return a+b;
}
+ int add_intfloat(int a, double b)
+ {
+ int rb = (int)b;
+ return a+rb;
+ }
+
double return_float(int a, int b)
{
return a+b;
@@ -47,8 +53,8 @@
'''
))
- symbols = ['add_integers', 'add_floats', 'return_float',
- 'max3', 'fvoid', 'return_void']
+ symbols = ['add_integers', 'add_floats', 'add_intfloat',
+ 'return_float', 'max3', 'fvoid', 'return_void']
eci = ExternalCompilationInfo(export_symbols=symbols)
return str(platform.compile([c_file], eci, 'x', standalone=False))
@@ -86,6 +92,12 @@
func = lib.get('return_void', ['int', 'int'])
assert func.call([1, 2]) is None
+ def test_various_type_args(self):
+ lib = rjitffi.CDLL(self.lib_name)
+
+ func = lib.get('add_intfloat', ['int', 'float'], 'int')
+ assert func.call([1, 2.9]) == 3
+
def test_undefined_func(self):
lib = rjitffi.CDLL(self.lib_name)
# xxxfoo888baryyy - not existed function
More information about the Pypy-commit
mailing list