[pypy-commit] pypy default: Merged jit-dynamic-getarrayitem. Added support for creating custom getarrayitems at jit-compile time. Steals some stuff from anto's ffistruct.
alex_gaynor
noreply at buildbot.pypy.org
Tue Nov 15 23:01:18 CET 2011
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch:
Changeset: r49447:bd871afa3feb
Date: 2011-11-15 17:00 -0500
http://bitbucket.org/pypy/pypy/changeset/bd871afa3feb/
Log: Merged jit-dynamic-getarrayitem. Added support for creating custom
getarrayitems at jit-compile time. Steals some stuff from anto's
ffistruct.
diff --git a/pypy/jit/backend/llgraph/llimpl.py b/pypy/jit/backend/llgraph/llimpl.py
--- a/pypy/jit/backend/llgraph/llimpl.py
+++ b/pypy/jit/backend/llgraph/llimpl.py
@@ -20,6 +20,7 @@
from pypy.jit.backend.llgraph import symbolic
from pypy.jit.codewriter import longlong
+from pypy.rlib import libffi
from pypy.rlib.objectmodel import ComputedIntSymbolic, we_are_translated
from pypy.rlib.rarithmetic import ovfcheck
from pypy.rlib.rarithmetic import r_longlong, r_ulonglong, r_uint
@@ -325,12 +326,12 @@
loop = _from_opaque(loop)
loop.operations.append(Operation(opnum))
-def compile_add_descr(loop, ofs, type, arg_types):
+def compile_add_descr(loop, ofs, type, arg_types, extrainfo, width):
from pypy.jit.backend.llgraph.runner import Descr
loop = _from_opaque(loop)
op = loop.operations[-1]
assert isinstance(type, str) and len(type) == 1
- op.descr = Descr(ofs, type, arg_types=arg_types)
+ op.descr = Descr(ofs, type, arg_types=arg_types, extrainfo=extrainfo, width=width)
def compile_add_descr_arg(loop, ofs, type, arg_types):
from pypy.jit.backend.llgraph.runner import Descr
@@ -825,6 +826,16 @@
else:
raise NotImplementedError
+ def op_getinteriorfield_raw(self, descr, array, index):
+ if descr.typeinfo == REF:
+ return do_getinteriorfield_raw_ptr(array, index, descr.width, descr.ofs)
+ elif descr.typeinfo == INT:
+ return do_getinteriorfield_raw_int(array, index, descr.width, descr.ofs)
+ elif descr.typeinfo == FLOAT:
+ return do_getinteriorfield_raw_float(array, index, descr.width, descr.ofs)
+ else:
+ raise NotImplementedError
+
def op_setinteriorfield_gc(self, descr, array, index, newvalue):
if descr.typeinfo == REF:
return do_setinteriorfield_gc_ptr(array, index, descr.ofs,
@@ -838,6 +849,16 @@
else:
raise NotImplementedError
+ def op_setinteriorfield_raw(self, descr, array, index, newvalue):
+ if descr.typeinfo == REF:
+ return do_setinteriorfield_raw_ptr(array, index, newvalue, descr.width, descr.ofs)
+ elif descr.typeinfo == INT:
+ return do_setinteriorfield_raw_int(array, index, newvalue, descr.width, descr.ofs)
+ elif descr.typeinfo == FLOAT:
+ return do_setinteriorfield_raw_float(array, index, newvalue, descr.width, descr.ofs)
+ else:
+ raise NotImplementedError
+
def op_setfield_gc(self, fielddescr, struct, newvalue):
if fielddescr.typeinfo == REF:
do_setfield_gc_ptr(struct, fielddescr.ofs, newvalue)
@@ -1403,6 +1424,14 @@
struct = array._obj.container.getitem(index)
return cast_to_ptr(_getinteriorfield_gc(struct, fieldnum))
+def _getinteriorfield_raw(ffitype, array, index, width, ofs):
+ addr = rffi.cast(rffi.VOIDP, array)
+ return libffi.array_getitem(ffitype, width, addr, index, ofs)
+
+def do_getinteriorfield_raw_int(array, index, width, ofs):
+ res = _getinteriorfield_raw(libffi.types.slong, array, index, width, ofs)
+ return res
+
def _getfield_raw(struct, fieldnum):
STRUCT, fieldname = symbolic.TokenToField[fieldnum]
ptr = cast_from_int(lltype.Ptr(STRUCT), struct)
@@ -1479,7 +1508,14 @@
return do_setinteriorfield_gc
do_setinteriorfield_gc_int = new_setinteriorfield_gc(cast_from_int)
do_setinteriorfield_gc_float = new_setinteriorfield_gc(cast_from_floatstorage)
-do_setinteriorfield_gc_ptr = new_setinteriorfield_gc(cast_from_ptr)
+do_setinteriorfield_gc_ptr = new_setinteriorfield_gc(cast_from_ptr)
+
+def new_setinteriorfield_raw(ffitype):
+ def do_setinteriorfield_raw(array, index, newvalue, width, ofs):
+ addr = rffi.cast(rffi.VOIDP, array)
+ return libffi.array_setitem(ffitype, width, addr, index, ofs, newvalue)
+ return do_setinteriorfield_raw
+do_setinteriorfield_raw_int = new_setinteriorfield_raw(libffi.types.slong)
def do_setfield_raw_int(struct, fieldnum, newvalue):
STRUCT, fieldname = symbolic.TokenToField[fieldnum]
diff --git a/pypy/jit/backend/llgraph/runner.py b/pypy/jit/backend/llgraph/runner.py
--- a/pypy/jit/backend/llgraph/runner.py
+++ b/pypy/jit/backend/llgraph/runner.py
@@ -23,8 +23,10 @@
class Descr(history.AbstractDescr):
def __init__(self, ofs, typeinfo, extrainfo=None, name=None,
- arg_types=None, count_fields_if_immut=-1, ffi_flags=0):
+ arg_types=None, count_fields_if_immut=-1, ffi_flags=0, width=-1):
+
self.ofs = ofs
+ self.width = width
self.typeinfo = typeinfo
self.extrainfo = extrainfo
self.name = name
@@ -119,14 +121,14 @@
return False
def getdescr(self, ofs, typeinfo='?', extrainfo=None, name=None,
- arg_types=None, count_fields_if_immut=-1, ffi_flags=0):
+ arg_types=None, count_fields_if_immut=-1, ffi_flags=0, width=-1):
key = (ofs, typeinfo, extrainfo, name, arg_types,
- count_fields_if_immut, ffi_flags)
+ count_fields_if_immut, ffi_flags, width)
try:
return self._descrs[key]
except KeyError:
descr = Descr(ofs, typeinfo, extrainfo, name, arg_types,
- count_fields_if_immut, ffi_flags)
+ count_fields_if_immut, ffi_flags, width)
self._descrs[key] = descr
return descr
@@ -179,7 +181,8 @@
descr = op.getdescr()
if isinstance(descr, Descr):
llimpl.compile_add_descr(c, descr.ofs, descr.typeinfo,
- descr.arg_types)
+ descr.arg_types, descr.extrainfo,
+ descr.width)
if (isinstance(descr, history.LoopToken) and
op.getopnum() != rop.JUMP):
llimpl.compile_add_loop_token(c, descr)
@@ -324,10 +327,22 @@
def interiorfielddescrof(self, A, fieldname):
S = A.OF
- ofs2 = symbolic.get_size(A)
+ width = symbolic.get_size(A)
ofs, size = symbolic.get_field_token(S, fieldname)
token = history.getkind(getattr(S, fieldname))
- return self.getdescr(ofs, token[0], name=fieldname, extrainfo=ofs2)
+ return self.getdescr(ofs, token[0], name=fieldname, width=width)
+
+ def interiorfielddescrof_dynamic(self, offset, width, fieldsize,
+ is_pointer, is_float, is_signed):
+
+ if is_pointer:
+ typeinfo = REF
+ elif is_float:
+ typeinfo = FLOAT
+ else:
+ typeinfo = INT
+ # we abuse the arg_types field to distinguish dynamic and static descrs
+ return Descr(offset, typeinfo, arg_types='dynamic', name='<dynamic interior field>', width=width)
def calldescrof(self, FUNC, ARGS, RESULT, extrainfo):
arg_types = []
diff --git a/pypy/jit/backend/llsupport/descr.py b/pypy/jit/backend/llsupport/descr.py
--- a/pypy/jit/backend/llsupport/descr.py
+++ b/pypy/jit/backend/llsupport/descr.py
@@ -111,6 +111,16 @@
def repr_of_descr(self):
return '<%s %s %s>' % (self._clsname, self.name, self.offset)
+class DynamicFieldDescr(BaseFieldDescr):
+ def __init__(self, offset, fieldsize, is_pointer, is_float, is_signed):
+ self.offset = offset
+ self._fieldsize = fieldsize
+ self._is_pointer_field = is_pointer
+ self._is_float_field = is_float
+ self._is_field_signed = is_signed
+
+ def get_field_size(self, translate_support_code):
+ return self._fieldsize
class NonGcPtrFieldDescr(BaseFieldDescr):
_clsname = 'NonGcPtrFieldDescr'
@@ -182,6 +192,7 @@
def repr_of_descr(self):
return '<%s>' % self._clsname
+
class NonGcPtrArrayDescr(BaseArrayDescr):
_clsname = 'NonGcPtrArrayDescr'
def get_item_size(self, translate_support_code):
@@ -211,6 +222,13 @@
def get_ofs_length(self, translate_support_code):
return -1
+class DynamicArrayNoLengthDescr(BaseArrayNoLengthDescr):
+ def __init__(self, itemsize):
+ self.itemsize = itemsize
+
+ def get_item_size(self, translate_support_code):
+ return self.itemsize
+
class NonGcPtrArrayNoLengthDescr(BaseArrayNoLengthDescr):
_clsname = 'NonGcPtrArrayNoLengthDescr'
def get_item_size(self, translate_support_code):
diff --git a/pypy/jit/backend/llsupport/llmodel.py b/pypy/jit/backend/llsupport/llmodel.py
--- a/pypy/jit/backend/llsupport/llmodel.py
+++ b/pypy/jit/backend/llsupport/llmodel.py
@@ -9,9 +9,10 @@
from pypy.jit.backend.llsupport import symbolic
from pypy.jit.backend.llsupport.symbolic import WORD, unroll_basic_sizes
from pypy.jit.backend.llsupport.descr import (get_size_descr,
- get_field_descr, BaseFieldDescr, get_array_descr, BaseArrayDescr,
- get_call_descr, BaseIntCallDescr, GcPtrCallDescr, FloatCallDescr,
- VoidCallDescr, InteriorFieldDescr, get_interiorfield_descr)
+ get_field_descr, BaseFieldDescr, DynamicFieldDescr, get_array_descr,
+ BaseArrayDescr, DynamicArrayNoLengthDescr, get_call_descr,
+ BaseIntCallDescr, GcPtrCallDescr, FloatCallDescr, VoidCallDescr,
+ InteriorFieldDescr, get_interiorfield_descr)
from pypy.jit.backend.llsupport.asmmemmgr import AsmMemoryManager
@@ -238,6 +239,12 @@
def interiorfielddescrof(self, A, fieldname):
return get_interiorfield_descr(self.gc_ll_descr, A, A.OF, fieldname)
+ def interiorfielddescrof_dynamic(self, offset, width, fieldsize,
+ is_pointer, is_float, is_signed):
+ arraydescr = DynamicArrayNoLengthDescr(width)
+ fielddescr = DynamicFieldDescr(offset, fieldsize, is_pointer, is_float, is_signed)
+ return InteriorFieldDescr(arraydescr, fielddescr)
+
def unpack_arraydescr(self, arraydescr):
assert isinstance(arraydescr, BaseArrayDescr)
return arraydescr.get_base_size(self.translate_support_code)
diff --git a/pypy/jit/backend/model.py b/pypy/jit/backend/model.py
--- a/pypy/jit/backend/model.py
+++ b/pypy/jit/backend/model.py
@@ -183,38 +183,35 @@
lst[n] = None
self.fail_descr_free_list.extend(faildescr_indices)
- @staticmethod
- def sizeof(S):
+ def sizeof(self, S):
raise NotImplementedError
- @staticmethod
- def fielddescrof(S, fieldname):
+ def fielddescrof(self, S, fieldname):
"""Return the Descr corresponding to field 'fieldname' on the
structure 'S'. It is important that this function (at least)
caches the results."""
raise NotImplementedError
- @staticmethod
- def arraydescrof(A):
+ def interiorfielddescrof(self, A, fieldname):
raise NotImplementedError
- @staticmethod
- def calldescrof(FUNC, ARGS, RESULT):
+ def interiorfielddescrof_dynamic(self, offset, width, fieldsize, is_pointer,
+ is_float, is_signed):
+ raise NotImplementedError
+
+ def arraydescrof(self, A):
+ raise NotImplementedError
+
+ def calldescrof(self, FUNC, ARGS, RESULT):
# FUNC is the original function type, but ARGS is a list of types
# with Voids removed
raise NotImplementedError
- @staticmethod
- def methdescrof(SELFTYPE, methname):
+ def methdescrof(self, SELFTYPE, methname):
# must return a subclass of history.AbstractMethDescr
raise NotImplementedError
- @staticmethod
- def typedescrof(TYPE):
- raise NotImplementedError
-
- @staticmethod
- def interiorfielddescrof(A, fieldname):
+ def typedescrof(self, TYPE):
raise NotImplementedError
# ---------- the backend-dependent operations ----------
diff --git a/pypy/jit/backend/x86/assembler.py b/pypy/jit/backend/x86/assembler.py
--- a/pypy/jit/backend/x86/assembler.py
+++ b/pypy/jit/backend/x86/assembler.py
@@ -8,8 +8,8 @@
from pypy.rpython.lltypesystem.lloperation import llop
from pypy.rpython.annlowlevel import llhelper
from pypy.jit.backend.model import CompiledLoopToken
-from pypy.jit.backend.x86.regalloc import (RegAlloc, get_ebp_ofs,
- _get_scale, gpr_reg_mgr_cls)
+from pypy.jit.backend.x86.regalloc import (RegAlloc, get_ebp_ofs, _get_scale,
+ gpr_reg_mgr_cls, _valid_addressing_size)
from pypy.jit.backend.x86.arch import (FRAME_FIXED_SIZE, FORCE_INDEX_OFS, WORD,
IS_X86_32, IS_X86_64)
@@ -1601,8 +1601,10 @@
assert isinstance(itemsize_loc, ImmedLoc)
if isinstance(index_loc, ImmedLoc):
temp_loc = imm(index_loc.value * itemsize_loc.value)
+ elif _valid_addressing_size(itemsize_loc.value):
+ return AddressLoc(base_loc, index_loc, _get_scale(itemsize_loc.value), ofs_loc.value)
else:
- # XXX should not use IMUL in most cases
+ # XXX should not use IMUL in more cases, it can use a clever LEA
assert isinstance(temp_loc, RegLoc)
assert isinstance(index_loc, RegLoc)
assert not temp_loc.is_xmm
@@ -1619,6 +1621,8 @@
ofs_loc)
self.load_from_mem(resloc, src_addr, fieldsize_loc, sign_loc)
+ genop_getinteriorfield_raw = genop_getinteriorfield_gc
+
def genop_discard_setfield_gc(self, op, arglocs):
base_loc, ofs_loc, size_loc, value_loc = arglocs
@@ -1634,6 +1638,8 @@
ofs_loc)
self.save_into_mem(dest_addr, value_loc, fieldsize_loc)
+ genop_discard_setinteriorfield_raw = genop_discard_setinteriorfield_gc
+
def genop_discard_setarrayitem_gc(self, op, arglocs):
base_loc, ofs_loc, value_loc, size_loc, baseofs = arglocs
assert isinstance(baseofs, ImmedLoc)
diff --git a/pypy/jit/backend/x86/regalloc.py b/pypy/jit/backend/x86/regalloc.py
--- a/pypy/jit/backend/x86/regalloc.py
+++ b/pypy/jit/backend/x86/regalloc.py
@@ -1067,6 +1067,8 @@
self.PerformDiscard(op, [base_loc, ofs, itemsize, fieldsize,
index_loc, temp_loc, value_loc])
+ consider_setinteriorfield_raw = consider_setinteriorfield_gc
+
def consider_strsetitem(self, op):
args = op.getarglist()
base_loc = self.rm.make_sure_var_in_reg(op.getarg(0), args)
@@ -1158,6 +1160,8 @@
self.Perform(op, [base_loc, ofs, itemsize, fieldsize,
index_loc, temp_loc, sign_loc], result_loc)
+ consider_getinteriorfield_raw = consider_getinteriorfield_gc
+
def consider_int_is_true(self, op, guard_op):
# doesn't need arg to be in a register
argloc = self.loc(op.getarg(0))
@@ -1430,8 +1434,11 @@
# i.e. the n'th word beyond the fixed frame size.
return -WORD * (FRAME_FIXED_SIZE + position)
+def _valid_addressing_size(size):
+ return size == 1 or size == 2 or size == 4 or size == 8
+
def _get_scale(size):
- assert size == 1 or size == 2 or size == 4 or size == 8
+ assert _valid_addressing_size(size)
if size < 4:
return size - 1 # 1, 2 => 0, 1
else:
diff --git a/pypy/jit/backend/x86/test/test_fficall.py b/pypy/jit/backend/x86/test/test_fficall.py
new file mode 100644
--- /dev/null
+++ b/pypy/jit/backend/x86/test/test_fficall.py
@@ -0,0 +1,8 @@
+import py
+from pypy.jit.metainterp.test import test_fficall
+from pypy.jit.backend.x86.test.test_basic import Jit386Mixin
+
+class TestFfiLookups(Jit386Mixin, test_fficall.FfiLookupTests):
+ # for the individual tests see
+ # ====> ../../../metainterp/test/test_fficall.py
+ supports_all = True
diff --git a/pypy/jit/codewriter/effectinfo.py b/pypy/jit/codewriter/effectinfo.py
--- a/pypy/jit/codewriter/effectinfo.py
+++ b/pypy/jit/codewriter/effectinfo.py
@@ -48,6 +48,8 @@
OS_LIBFFI_PREPARE = 60
OS_LIBFFI_PUSH_ARG = 61
OS_LIBFFI_CALL = 62
+ OS_LIBFFI_GETARRAYITEM = 63
+ OS_LIBFFI_SETARRAYITEM = 64
#
OS_LLONG_INVERT = 69
OS_LLONG_ADD = 70
diff --git a/pypy/jit/codewriter/jtransform.py b/pypy/jit/codewriter/jtransform.py
--- a/pypy/jit/codewriter/jtransform.py
+++ b/pypy/jit/codewriter/jtransform.py
@@ -1615,6 +1615,12 @@
elif oopspec_name.startswith('libffi_call_'):
oopspecindex = EffectInfo.OS_LIBFFI_CALL
extraeffect = EffectInfo.EF_RANDOM_EFFECTS
+ elif oopspec_name == 'libffi_array_getitem':
+ oopspecindex = EffectInfo.OS_LIBFFI_GETARRAYITEM
+ extraeffect = EffectInfo.EF_CANNOT_RAISE
+ elif oopspec_name == 'libffi_array_setitem':
+ oopspecindex = EffectInfo.OS_LIBFFI_SETARRAYITEM
+ extraeffect = EffectInfo.EF_CANNOT_RAISE
else:
assert False, 'unsupported oopspec: %s' % oopspec_name
return self._handle_oopspec_call(op, args, oopspecindex, extraeffect)
diff --git a/pypy/jit/metainterp/executor.py b/pypy/jit/metainterp/executor.py
--- a/pypy/jit/metainterp/executor.py
+++ b/pypy/jit/metainterp/executor.py
@@ -340,6 +340,8 @@
rop.DEBUG_MERGE_POINT,
rop.JIT_DEBUG,
rop.SETARRAYITEM_RAW,
+ rop.GETINTERIORFIELD_RAW,
+ rop.SETINTERIORFIELD_RAW,
rop.CALL_RELEASE_GIL,
rop.QUASIIMMUT_FIELD,
): # list of opcodes never executed by pyjitpl
diff --git a/pypy/jit/metainterp/optimizeopt/fficall.py b/pypy/jit/metainterp/optimizeopt/fficall.py
--- a/pypy/jit/metainterp/optimizeopt/fficall.py
+++ b/pypy/jit/metainterp/optimizeopt/fficall.py
@@ -1,11 +1,13 @@
+from pypy.jit.codewriter.effectinfo import EffectInfo
+from pypy.jit.metainterp.optimizeopt.optimizer import Optimization
+from pypy.jit.metainterp.optimizeopt.util import make_dispatcher_method
+from pypy.jit.metainterp.resoperation import rop, ResOperation
+from pypy.rlib import clibffi, libffi
+from pypy.rlib.debug import debug_print
+from pypy.rlib.libffi import Func
+from pypy.rlib.objectmodel import we_are_translated
from pypy.rpython.annlowlevel import cast_base_ptr_to_instance
-from pypy.rlib.objectmodel import we_are_translated
-from pypy.rlib.libffi import Func
-from pypy.rlib.debug import debug_print
-from pypy.jit.codewriter.effectinfo import EffectInfo
-from pypy.jit.metainterp.resoperation import rop, ResOperation
-from pypy.jit.metainterp.optimizeopt.util import make_dispatcher_method
-from pypy.jit.metainterp.optimizeopt.optimizer import Optimization
+from pypy.rpython.lltypesystem import llmemory
class FuncInfo(object):
@@ -78,7 +80,7 @@
def new(self):
return OptFfiCall()
-
+
def begin_optimization(self, funcval, op):
self.rollback_maybe('begin_optimization', op)
self.funcinfo = FuncInfo(funcval, self.optimizer.cpu, op)
@@ -116,6 +118,9 @@
ops = self.do_push_arg(op)
elif oopspec == EffectInfo.OS_LIBFFI_CALL:
ops = self.do_call(op)
+ elif (oopspec == EffectInfo.OS_LIBFFI_GETARRAYITEM or
+ oopspec == EffectInfo.OS_LIBFFI_SETARRAYITEM):
+ ops = self.do_getsetarrayitem(op, oopspec)
#
for op in ops:
self.emit_operation(op)
@@ -190,6 +195,53 @@
ops.append(newop)
return ops
+ def do_getsetarrayitem(self, op, oopspec):
+ ffitypeval = self.getvalue(op.getarg(1))
+ widthval = self.getvalue(op.getarg(2))
+ offsetval = self.getvalue(op.getarg(5))
+ if not ffitypeval.is_constant() or not widthval.is_constant() or not offsetval.is_constant():
+ return [op]
+
+ ffitypeaddr = ffitypeval.box.getaddr()
+ ffitype = llmemory.cast_adr_to_ptr(ffitypeaddr, clibffi.FFI_TYPE_P)
+ offset = offsetval.box.getint()
+ width = widthval.box.getint()
+ descr = self._get_interior_descr(ffitype, width, offset)
+
+ arglist = [
+ self.getvalue(op.getarg(3)).force_box(self.optimizer),
+ self.getvalue(op.getarg(4)).force_box(self.optimizer),
+ ]
+ if oopspec == EffectInfo.OS_LIBFFI_GETARRAYITEM:
+ opnum = rop.GETINTERIORFIELD_RAW
+ elif oopspec == EffectInfo.OS_LIBFFI_SETARRAYITEM:
+ opnum = rop.SETINTERIORFIELD_RAW
+ arglist.append(self.getvalue(op.getarg(6)).force_box(self.optimizer))
+ else:
+ assert False
+ return [
+ ResOperation(opnum, arglist, op.result, descr=descr),
+ ]
+
+ def _get_interior_descr(self, ffitype, width, offset):
+ kind = libffi.types.getkind(ffitype)
+ is_pointer = is_float = is_signed = False
+ if ffitype is libffi.types.pointer:
+ is_pointer = True
+ elif kind == 'i':
+ is_signed = True
+ elif kind == 'f' or kind == 'I' or kind == 'U':
+ # longlongs are treated as floats, see
+ # e.g. llsupport/descr.py:getDescrClass
+ is_float = True
+ else:
+ assert False, "unsupported ffitype or kind"
+ #
+ fieldsize = ffitype.c_size
+ return self.optimizer.cpu.interiorfielddescrof_dynamic(
+ offset, width, fieldsize, is_pointer, is_float, is_signed
+ )
+
def propagate_forward(self, op):
if self.logops is not None:
debug_print(self.logops.repr_of_resop(op))
diff --git a/pypy/jit/metainterp/resoperation.py b/pypy/jit/metainterp/resoperation.py
--- a/pypy/jit/metainterp/resoperation.py
+++ b/pypy/jit/metainterp/resoperation.py
@@ -461,6 +461,7 @@
'GETARRAYITEM_GC/2d',
'GETARRAYITEM_RAW/2d',
'GETINTERIORFIELD_GC/2d',
+ 'GETINTERIORFIELD_RAW/2d',
'GETFIELD_GC/1d',
'GETFIELD_RAW/1d',
'_MALLOC_FIRST',
@@ -479,6 +480,7 @@
'SETARRAYITEM_GC/3d',
'SETARRAYITEM_RAW/3d',
'SETINTERIORFIELD_GC/3d',
+ 'SETINTERIORFIELD_RAW/3d',
'SETFIELD_GC/2d',
'SETFIELD_RAW/2d',
'STRSETITEM/3',
diff --git a/pypy/jit/metainterp/test/test_fficall.py b/pypy/jit/metainterp/test/test_fficall.py
--- a/pypy/jit/metainterp/test/test_fficall.py
+++ b/pypy/jit/metainterp/test/test_fficall.py
@@ -1,19 +1,18 @@
+import py
-import py
+from pypy.jit.metainterp.test.support import LLJitMixin
+from pypy.rlib.jit import JitDriver, promote, dont_look_inside
+from pypy.rlib.libffi import (ArgChain, IS_32_BIT, array_getitem, array_setitem,
+ types)
+from pypy.rlib.objectmodel import specialize
from pypy.rlib.rarithmetic import r_singlefloat, r_longlong, r_ulonglong
-from pypy.rlib.jit import JitDriver, promote, dont_look_inside
+from pypy.rlib.test.test_libffi import TestLibffiCall as _TestLibffiCall
from pypy.rlib.unroll import unrolling_iterable
-from pypy.rlib.libffi import ArgChain
-from pypy.rlib.libffi import IS_32_BIT
-from pypy.rlib.test.test_libffi import TestLibffiCall as _TestLibffiCall
from pypy.rpython.lltypesystem import lltype, rffi
-from pypy.rlib.objectmodel import specialize
from pypy.tool.sourcetools import func_with_new_name
-from pypy.jit.metainterp.test.support import LLJitMixin
-class TestFfiCall(LLJitMixin, _TestLibffiCall):
- supports_all = False # supports_{floats,longlong,singlefloats}
+class FfiCallTests(_TestLibffiCall):
# ===> ../../../rlib/test/test_libffi.py
def call(self, funcspec, args, RESULT, is_struct=False, jitif=[]):
@@ -92,6 +91,69 @@
test_byval_result.__doc__ = _TestLibffiCall.test_byval_result.__doc__
test_byval_result.dont_track_allocations = True
+class FfiLookupTests(object):
+ def test_array_fields(self):
+ myjitdriver = JitDriver(
+ greens = [],
+ reds = ["n", "i", "points", "result_point"],
+ )
-class TestFfiCallSupportAll(TestFfiCall):
+ POINT = lltype.Struct("POINT",
+ ("x", lltype.Signed),
+ ("y", lltype.Signed),
+ )
+ def f(points, result_point, n):
+ i = 0
+ while i < n:
+ myjitdriver.jit_merge_point(i=i, points=points, n=n,
+ result_point=result_point)
+ x = array_getitem(
+ types.slong, rffi.sizeof(lltype.Signed) * 2, points, i, 0
+ )
+ y = array_getitem(
+ types.slong, rffi.sizeof(lltype.Signed) * 2, points, i, rffi.sizeof(lltype.Signed)
+ )
+
+ cur_x = array_getitem(
+ types.slong, rffi.sizeof(lltype.Signed) * 2, result_point, 0, 0
+ )
+ cur_y = array_getitem(
+ types.slong, rffi.sizeof(lltype.Signed) * 2, result_point, 0, rffi.sizeof(lltype.Signed)
+ )
+
+ array_setitem(
+ types.slong, rffi.sizeof(lltype.Signed) * 2, result_point, 0, 0, cur_x + x
+ )
+ array_setitem(
+ types.slong, rffi.sizeof(lltype.Signed) * 2, result_point, 0, rffi.sizeof(lltype.Signed), cur_y + y
+ )
+ i += 1
+
+ def main(n):
+ with lltype.scoped_alloc(rffi.CArray(POINT), n) as points:
+ with lltype.scoped_alloc(rffi.CArray(POINT), 1) as result_point:
+ for i in xrange(n):
+ points[i].x = i * 2
+ points[i].y = i * 2 + 1
+ points = rffi.cast(rffi.CArrayPtr(lltype.Char), points)
+ result_point[0].x = 0
+ result_point[0].y = 0
+ result_point = rffi.cast(rffi.CArrayPtr(lltype.Char), result_point)
+ f(points, result_point, n)
+ result_point = rffi.cast(rffi.CArrayPtr(POINT), result_point)
+ return result_point[0].x * result_point[0].y
+
+ assert self.meta_interp(main, [10]) == main(10) == 9000
+ self.check_loops({"int_add": 3, "jump": 1, "int_lt": 1, "guard_true": 1,
+ "getinteriorfield_raw": 4, "setinteriorfield_raw": 2
+ })
+
+
+class TestFfiCall(FfiCallTests, LLJitMixin):
+ supports_all = False
+
+class TestFfiCallSupportAll(FfiCallTests, LLJitMixin):
supports_all = True # supports_{floats,longlong,singlefloats}
+
+class TestFfiLookup(FfiLookupTests, LLJitMixin):
+ pass
\ No newline at end of file
diff --git a/pypy/rlib/clibffi.py b/pypy/rlib/clibffi.py
--- a/pypy/rlib/clibffi.py
+++ b/pypy/rlib/clibffi.py
@@ -30,9 +30,6 @@
_MAC_OS = platform.name == "darwin"
_FREEBSD_7 = platform.name == "freebsd7"
-_LITTLE_ENDIAN = sys.byteorder == 'little'
-_BIG_ENDIAN = sys.byteorder == 'big'
-
if _WIN32:
from pypy.rlib import rwin32
@@ -213,26 +210,48 @@
elif sz == 8: return ffi_type_uint64
else: raise ValueError("unsupported type size for %r" % (TYPE,))
-TYPE_MAP = {
- rffi.DOUBLE : ffi_type_double,
- rffi.FLOAT : ffi_type_float,
- rffi.LONGDOUBLE : ffi_type_longdouble,
- rffi.UCHAR : ffi_type_uchar,
- rffi.CHAR : ffi_type_schar,
- rffi.SHORT : ffi_type_sshort,
- rffi.USHORT : ffi_type_ushort,
- rffi.UINT : ffi_type_uint,
- rffi.INT : ffi_type_sint,
+__int_type_map = [
+ (rffi.UCHAR, ffi_type_uchar),
+ (rffi.SIGNEDCHAR, ffi_type_schar),
+ (rffi.SHORT, ffi_type_sshort),
+ (rffi.USHORT, ffi_type_ushort),
+ (rffi.UINT, ffi_type_uint),
+ (rffi.INT, ffi_type_sint),
# xxx don't use ffi_type_slong and ffi_type_ulong - their meaning
# changes from a libffi version to another :-((
- rffi.ULONG : _unsigned_type_for(rffi.ULONG),
- rffi.LONG : _signed_type_for(rffi.LONG),
- rffi.ULONGLONG : _unsigned_type_for(rffi.ULONGLONG),
- rffi.LONGLONG : _signed_type_for(rffi.LONGLONG),
- lltype.Void : ffi_type_void,
- lltype.UniChar : _unsigned_type_for(lltype.UniChar),
- lltype.Bool : _unsigned_type_for(lltype.Bool),
- }
+ (rffi.ULONG, _unsigned_type_for(rffi.ULONG)),
+ (rffi.LONG, _signed_type_for(rffi.LONG)),
+ (rffi.ULONGLONG, _unsigned_type_for(rffi.ULONGLONG)),
+ (rffi.LONGLONG, _signed_type_for(rffi.LONGLONG)),
+ (lltype.UniChar, _unsigned_type_for(lltype.UniChar)),
+ (lltype.Bool, _unsigned_type_for(lltype.Bool)),
+ ]
+
+__float_type_map = [
+ (rffi.DOUBLE, ffi_type_double),
+ (rffi.FLOAT, ffi_type_float),
+ (rffi.LONGDOUBLE, ffi_type_longdouble),
+ ]
+
+__ptr_type_map = [
+ (rffi.VOIDP, ffi_type_pointer),
+ ]
+
+__type_map = __int_type_map + __float_type_map + [
+ (lltype.Void, ffi_type_void)
+ ]
+
+TYPE_MAP_INT = dict(__int_type_map)
+TYPE_MAP_FLOAT = dict(__float_type_map)
+TYPE_MAP = dict(__type_map)
+
+ffitype_map_int = unrolling_iterable(__int_type_map)
+ffitype_map_int_or_ptr = unrolling_iterable(__int_type_map + __ptr_type_map)
+ffitype_map_float = unrolling_iterable(__float_type_map)
+ffitype_map = unrolling_iterable(__type_map)
+
+del __int_type_map, __float_type_map, __ptr_type_map, __type_map
+
def external(name, args, result, **kwds):
return rffi.llexternal(name, args, result, compilation_info=eci, **kwds)
@@ -341,38 +360,15 @@
cast_type_to_ffitype._annspecialcase_ = 'specialize:memo'
def push_arg_as_ffiptr(ffitp, arg, ll_buf):
- # This is for primitive types. Note that the exact type of 'arg' may be
- # different from the expected 'c_size'. To cope with that, we fall back
- # to a byte-by-byte copy.
+ # this is for primitive types. For structures and arrays
+ # would be something different (more dynamic)
TP = lltype.typeOf(arg)
TP_P = lltype.Ptr(rffi.CArray(TP))
- TP_size = rffi.sizeof(TP)
- c_size = intmask(ffitp.c_size)
- # if both types have the same size, we can directly write the
- # value to the buffer
- if c_size == TP_size:
- buf = rffi.cast(TP_P, ll_buf)
- buf[0] = arg
- else:
- # needs byte-by-byte copying. Make sure 'arg' is an integer type.
- # Note that this won't work for rffi.FLOAT/rffi.DOUBLE.
- assert TP is not rffi.FLOAT and TP is not rffi.DOUBLE
- if TP_size <= rffi.sizeof(lltype.Signed):
- arg = rffi.cast(lltype.Unsigned, arg)
- else:
- arg = rffi.cast(lltype.UnsignedLongLong, arg)
- if _LITTLE_ENDIAN:
- for i in range(c_size):
- ll_buf[i] = chr(arg & 0xFF)
- arg >>= 8
- elif _BIG_ENDIAN:
- for i in range(c_size-1, -1, -1):
- ll_buf[i] = chr(arg & 0xFF)
- arg >>= 8
- else:
- raise AssertionError
+ buf = rffi.cast(TP_P, ll_buf)
+ buf[0] = arg
push_arg_as_ffiptr._annspecialcase_ = 'specialize:argtype(1)'
+
# type defs for callback and closure userdata
USERDATA_P = lltype.Ptr(lltype.ForwardReference())
CALLBACK_TP = lltype.Ptr(lltype.FuncType([rffi.VOIDPP, rffi.VOIDP, USERDATA_P],
diff --git a/pypy/rlib/libffi.py b/pypy/rlib/libffi.py
--- a/pypy/rlib/libffi.py
+++ b/pypy/rlib/libffi.py
@@ -140,7 +140,7 @@
self.last.next = arg
self.last = arg
self.numargs += 1
-
+
class AbstractArg(object):
next = None
@@ -410,3 +410,22 @@
def getaddressindll(self, name):
return dlsym(self.lib, name)
+
+ at jit.oopspec("libffi_array_getitem(ffitype, width, addr, index, offset)")
+def array_getitem(ffitype, width, addr, index, offset):
+ for TYPE, ffitype2 in clibffi.ffitype_map:
+ if ffitype is ffitype2:
+ addr = rffi.ptradd(addr, index * width)
+ addr = rffi.ptradd(addr, offset)
+ return rffi.cast(rffi.CArrayPtr(TYPE), addr)[0]
+ assert False
+
+ at jit.oopspec("libffi_array_setitem(ffitype, width, addr, index, offset, value)")
+def array_setitem(ffitype, width, addr, index, offset, value):
+ for TYPE, ffitype2 in clibffi.ffitype_map:
+ if ffitype is ffitype2:
+ addr = rffi.ptradd(addr, index * width)
+ addr = rffi.ptradd(addr, offset)
+ rffi.cast(rffi.CArrayPtr(TYPE), addr)[0] = value
+ return
+ assert False
\ No newline at end of file
diff --git a/pypy/rlib/test/test_libffi.py b/pypy/rlib/test/test_libffi.py
--- a/pypy/rlib/test/test_libffi.py
+++ b/pypy/rlib/test/test_libffi.py
@@ -1,11 +1,13 @@
+import sys
+
import py
-import sys
+
+from pypy.rlib.libffi import (CDLL, Func, get_libc_name, ArgChain, types,
+ IS_32_BIT, array_getitem, array_setitem)
+from pypy.rlib.rarithmetic import r_singlefloat, r_longlong, r_ulonglong
+from pypy.rlib.test.test_clibffi import BaseFfiTest, get_libm_name, make_struct_ffitype_e
from pypy.rpython.lltypesystem import rffi, lltype
from pypy.rpython.lltypesystem.ll2ctypes import ALLOCATED
-from pypy.rlib.rarithmetic import r_singlefloat, r_longlong, r_ulonglong
-from pypy.rlib.test.test_clibffi import BaseFfiTest, get_libm_name, make_struct_ffitype_e
-from pypy.rlib.libffi import CDLL, Func, get_libc_name, ArgChain, types
-from pypy.rlib.libffi import IS_32_BIT
class TestLibffiMisc(BaseFfiTest):
@@ -52,6 +54,34 @@
del lib
assert not ALLOCATED
+ def test_array_fields(self):
+ POINT = lltype.Struct("POINT",
+ ("x", lltype.Float),
+ ("y", lltype.Float),
+ )
+ points = lltype.malloc(rffi.CArray(POINT), 2, flavor="raw")
+ points[0].x = 1.0
+ points[0].y = 2.0
+ points[1].x = 3.0
+ points[1].y = 4.0
+ points = rffi.cast(rffi.CArrayPtr(lltype.Char), points)
+ assert array_getitem(types.double, 16, points, 0, 0) == 1.0
+ assert array_getitem(types.double, 16, points, 0, 8) == 2.0
+ assert array_getitem(types.double, 16, points, 1, 0) == 3.0
+ assert array_getitem(types.double, 16, points, 1, 8) == 4.0
+
+ array_setitem(types.double, 16, points, 0, 0, 10.0)
+ array_setitem(types.double, 16, points, 0, 8, 20.0)
+ array_setitem(types.double, 16, points, 1, 0, 30.0)
+ array_setitem(types.double, 16, points, 1, 8, 40.0)
+
+ assert array_getitem(types.double, 16, points, 0, 0) == 10.0
+ assert array_getitem(types.double, 16, points, 0, 8) == 20.0
+ assert array_getitem(types.double, 16, points, 1, 0) == 30.0
+ assert array_getitem(types.double, 16, points, 1, 8) == 40.0
+
+ lltype.free(points, flavor="raw")
+
class TestLibffiCall(BaseFfiTest):
"""
Test various kind of calls through libffi.
@@ -109,7 +139,7 @@
This method is overridden by metainterp/test/test_fficall.py in
order to do the call in a loop and JIT it. The optional arguments are
used only by that overridden method.
-
+
"""
lib, name, argtypes, restype = funcspec
func = lib.getpointer(name, argtypes, restype)
@@ -132,7 +162,7 @@
return x - y;
}
"""
- libfoo = self.get_libfoo()
+ libfoo = self.get_libfoo()
func = (libfoo, 'diff_xy', [types.sint, types.slong], types.sint)
res = self.call(func, [50, 8], lltype.Signed)
assert res == 42
@@ -144,7 +174,7 @@
return (x + (int)y);
}
"""
- libfoo = self.get_libfoo()
+ libfoo = self.get_libfoo()
func = (libfoo, 'sum_xy', [types.sint, types.double], types.sint)
res = self.call(func, [38, 4.2], lltype.Signed, jitif=["floats"])
assert res == 42
@@ -249,7 +279,7 @@
};
struct pair my_static_pair = {10, 20};
-
+
long* get_pointer_to_b()
{
return &my_static_pair.b;
@@ -340,7 +370,7 @@
def test_wrong_number_of_arguments(self):
from pypy.rpython.llinterp import LLException
- libfoo = self.get_libfoo()
+ libfoo = self.get_libfoo()
func = (libfoo, 'sum_xy', [types.sint, types.double], types.sint)
glob = globals()
More information about the pypy-commit
mailing list