Source

pypy / pypy / rlib / libffi.py

The branch 'kill-more-multimethods' does not exist.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
from __future__ import with_statement

from pypy.rpython.lltypesystem import rffi, lltype
from pypy.rlib.objectmodel import specialize, enforceargs, we_are_translated
from pypy.rlib.rarithmetic import intmask, r_uint, r_singlefloat, r_longlong
from pypy.rlib import jit
from pypy.rlib import clibffi
from pypy.rlib.clibffi import get_libc_name, FUNCFLAG_CDECL, AbstractFuncPtr, \
    push_arg_as_ffiptr, c_ffi_call, FFI_TYPE_STRUCT
from pypy.rlib.rdynload import dlopen, dlclose, dlsym, dlsym_byordinal
from pypy.rlib.rdynload import DLLHANDLE

class types(object):
    """
    This namespace contains the primitive types you can use to declare the
    signatures of the ffi functions.

    In general, the name of the types are closely related to the ones of the
    C-level ffi_type_*: e.g, instead of ffi_type_sint you should use
    libffi.types.sint.

    However, you should not rely on a perfect correspondence: in particular,
    the exact meaning of ffi_type_{slong,ulong} changes a lot between libffi
    versions, so types.slong could be different than ffi_type_slong.
    """

    @classmethod
    def _import(cls):
        prefix = 'ffi_type_'
        for key, value in clibffi.__dict__.iteritems():
            if key.startswith(prefix):
                name = key[len(prefix):]
                setattr(cls, name, value)
        cls.slong = clibffi.cast_type_to_ffitype(rffi.LONG)
        cls.ulong = clibffi.cast_type_to_ffitype(rffi.ULONG)
        cls.slonglong = clibffi.cast_type_to_ffitype(rffi.LONGLONG)
        cls.ulonglong = clibffi.cast_type_to_ffitype(rffi.ULONGLONG)
        cls.wchar_t = clibffi.cast_type_to_ffitype(lltype.UniChar)
        del cls._import

    @staticmethod
    @jit.elidable
    def getkind(ffi_type):
        """Returns 'v' for void, 'f' for float, 'i' for signed integer,
        and 'u' for unsigned integer.
        """
        if   ffi_type is types.void:    return 'v'
        elif ffi_type is types.double:  return 'f'
        elif ffi_type is types.float:   return 's'
        elif ffi_type is types.pointer: return 'u'
        #
        elif ffi_type is types.schar:   return 'i'
        elif ffi_type is types.uchar:   return 'u'
        elif ffi_type is types.sshort:  return 'i'
        elif ffi_type is types.ushort:  return 'u'
        elif ffi_type is types.sint:    return 'i'
        elif ffi_type is types.uint:    return 'u'
        elif ffi_type is types.slong:   return 'i'
        elif ffi_type is types.ulong:   return 'u'
        #
        elif ffi_type is types.sint8:   return 'i'
        elif ffi_type is types.uint8:   return 'u'
        elif ffi_type is types.sint16:  return 'i'
        elif ffi_type is types.uint16:  return 'u'
        elif ffi_type is types.sint32:  return 'i'
        elif ffi_type is types.uint32:  return 'u'
        ## (note that on 64-bit platforms, types.sint64 is types.slong and the
        ## case is caught above)
        elif ffi_type is types.sint64:  return 'I'
        elif ffi_type is types.uint64:  return 'U'
        #
        elif types.is_struct(ffi_type): return 'S'
        raise KeyError

    @staticmethod
    @jit.elidable
    def is_struct(ffi_type):
        return intmask(ffi_type.c_type) == FFI_TYPE_STRUCT

types._import()

@specialize.arg(0)
def _fits_into_long(TYPE):
    if isinstance(TYPE, lltype.Ptr):
        return True # pointers always fits into longs
    if not isinstance(TYPE, lltype.Primitive):
        return False
    if TYPE is lltype.Void or TYPE is rffi.FLOAT or TYPE is rffi.DOUBLE:
        return False
    sz = rffi.sizeof(TYPE)
    return sz <= rffi.sizeof(rffi.LONG)


# ======================================================================

IS_32_BIT = (r_uint.BITS == 32)

@specialize.memo()
def _check_type(TYPE):
    if isinstance(TYPE, lltype.Ptr):
        if TYPE.TO._gckind != 'raw':
            raise TypeError, "Can only push raw values to C, not 'gc'"
        # XXX probably we should recursively check for struct fields here,
        # lets just ignore that for now
        if isinstance(TYPE.TO, lltype.Array) and 'nolength' not in TYPE.TO._hints:
            raise TypeError, "Can only push to C arrays without length info"


class ArgChain(object):
    first = None
    last = None
    numargs = 0

    @specialize.argtype(1)
    def arg(self, val):
        TYPE = lltype.typeOf(val)
        _check_type(TYPE)
        if _fits_into_long(TYPE):
            cls = IntArg
            val = rffi.cast(rffi.LONG, val)
        elif TYPE is rffi.DOUBLE:
            cls = FloatArg
        elif TYPE is rffi.LONGLONG or TYPE is rffi.ULONGLONG:
            cls = LongLongArg
            val = rffi.cast(rffi.LONGLONG, val)
        elif TYPE is rffi.FLOAT:
            cls = SingleFloatArg
        else:
            raise TypeError, 'Unsupported argument type: %s' % TYPE
        self._append(cls(val))
        return self

    def arg_raw(self, val):
        self._append(RawArg(val))

    def _append(self, arg):
        if self.first is None:
            self.first = self.last = arg
        else:
            self.last.next = arg
            self.last = arg
        self.numargs += 1


class AbstractArg(object):
    next = None

class IntArg(AbstractArg):
    """ An argument holding an integer
    """

    def __init__(self, intval):
        self.intval = intval

    def push(self, func, ll_args, i):
        func._push_int(self.intval, ll_args, i)


class FloatArg(AbstractArg):
    """ An argument holding a python float (i.e. a C double)
    """

    def __init__(self, floatval):
        self.floatval = floatval

    def push(self, func, ll_args, i):
        func._push_float(self.floatval, ll_args, i)

class RawArg(AbstractArg):
    """ An argument holding a raw pointer to put inside ll_args
    """

    def __init__(self, ptrval):
        self.ptrval = ptrval

    def push(self, func, ll_args, i):
        func._push_raw(self.ptrval, ll_args, i)

class SingleFloatArg(AbstractArg):
    """ An argument representing a C float
    """

    def __init__(self, singlefloatval):
        self.singlefloatval = singlefloatval

    def push(self, func, ll_args, i):
        func._push_singlefloat(self.singlefloatval, ll_args, i)


class LongLongArg(AbstractArg):
    """ An argument representing a C long long
    """

    def __init__(self, longlongval):
        self.longlongval = longlongval

    def push(self, func, ll_args, i):
        func._push_longlong(self.longlongval, ll_args, i)


# ======================================================================


class Func(AbstractFuncPtr):

    _immutable_fields_ = ['funcsym']
    argtypes = []
    restype = lltype.nullptr(clibffi.FFI_TYPE_P.TO)
    flags = 0
    funcsym = lltype.nullptr(rffi.VOIDP.TO)

    def __init__(self, name, argtypes, restype, funcsym, flags=FUNCFLAG_CDECL,
                 keepalive=None):
        AbstractFuncPtr.__init__(self, name, argtypes, restype, flags)
        self.keepalive = keepalive
        self.funcsym = funcsym

    # ========================================================================
    # PUBLIC INTERFACE
    # ========================================================================

    @jit.unroll_safe
    @specialize.arg(2, 3)
    def call(self, argchain, RESULT, is_struct=False):
        # WARNING!  This code is written carefully in a way that the JIT
        # optimizer will see a sequence of calls like the following:
        #
        #    libffi_prepare_call
        #    libffi_push_arg
        #    libffi_push_arg
        #    ...
        #    libffi_call
        #
        # It is important that there is no other operation in the middle, else
        # the optimizer will fail to recognize the pattern and won't turn it
        # into a fast CALL.  Note that "arg = arg.next" is optimized away,
        # assuming that argchain is completely virtual.
        self = jit.promote(self)
        if argchain.numargs != len(self.argtypes):
            raise TypeError, 'Wrong number of arguments: %d expected, got %d' %\
                (argchain.numargs, len(self.argtypes))
        ll_args = self._prepare()
        i = 0
        arg = argchain.first
        while arg:
            arg.push(self, ll_args, i)
            i += 1
            arg = arg.next
        #
        if is_struct:
            assert types.is_struct(self.restype)
            res = self._do_call_raw(self.funcsym, ll_args)
        elif _fits_into_long(RESULT):
            assert not types.is_struct(self.restype)
            res = self._do_call_int(self.funcsym, ll_args)
        elif RESULT is rffi.DOUBLE:
            return self._do_call_float(self.funcsym, ll_args)
        elif RESULT is rffi.FLOAT:
            return self._do_call_singlefloat(self.funcsym, ll_args)
        elif RESULT is rffi.LONGLONG or RESULT is rffi.ULONGLONG:
            assert IS_32_BIT
            res = self._do_call_longlong(self.funcsym, ll_args)
        elif RESULT is lltype.Void:
            return self._do_call_void(self.funcsym, ll_args)
        else:
            raise TypeError, 'Unsupported result type: %s' % RESULT
        #
        return rffi.cast(RESULT, res)

    # END OF THE PUBLIC INTERFACE
    # ------------------------------------------------------------------------

    # JIT friendly interface
    # the following methods are supposed to be seen opaquely by the optimizer

    @jit.oopspec('libffi_prepare_call(self)')
    def _prepare(self):
        ll_args = lltype.malloc(rffi.VOIDPP.TO, len(self.argtypes), flavor='raw')
        return ll_args


    # _push_* and _do_call_* in theory could be automatically specialize()d by
    # the annotator.  However, specialization doesn't work well with oopspec,
    # so we specialize them by hand

    @jit.oopspec('libffi_push_int(self, value, ll_args, i)')
    @enforceargs( None, int,   None,    int) # fix the annotation for tests
    def _push_int(self, value, ll_args, i):
        self._push_arg(value, ll_args, i)

    @jit.dont_look_inside
    def _push_raw(self, value, ll_args, i):
        ll_args[i] = value

    @jit.oopspec('libffi_push_float(self, value, ll_args, i)')
    @enforceargs(   None, float, None,    int) # fix the annotation for tests
    def _push_float(self, value, ll_args, i):
        self._push_arg(value, ll_args, i)

    @jit.oopspec('libffi_push_singlefloat(self, value, ll_args, i)')
    @enforceargs(None, r_singlefloat, None, int) # fix the annotation for tests
    def _push_singlefloat(self, value, ll_args, i):
        self._push_arg(value, ll_args, i)

    @jit.oopspec('libffi_push_longlong(self, value, ll_args, i)')
    @enforceargs(None, r_longlong, None, int) # fix the annotation for tests
    def _push_longlong(self, value, ll_args, i):
        self._push_arg(value, ll_args, i)

    @jit.oopspec('libffi_call_int(self, funcsym, ll_args)')
    def _do_call_int(self, funcsym, ll_args):
        return self._do_call(funcsym, ll_args, rffi.LONG)

    @jit.oopspec('libffi_call_float(self, funcsym, ll_args)')
    def _do_call_float(self, funcsym, ll_args):
        return self._do_call(funcsym, ll_args, rffi.DOUBLE)

    @jit.oopspec('libffi_call_singlefloat(self, funcsym, ll_args)')
    def _do_call_singlefloat(self, funcsym, ll_args):
        return self._do_call(funcsym, ll_args, rffi.FLOAT)

    @jit.dont_look_inside
    def _do_call_raw(self, funcsym, ll_args):
        # same as _do_call_int, but marked as jit.dont_look_inside
        return self._do_call(funcsym, ll_args, rffi.LONG)

    @jit.oopspec('libffi_call_longlong(self, funcsym, ll_args)')
    def _do_call_longlong(self, funcsym, ll_args):
        return self._do_call(funcsym, ll_args, rffi.LONGLONG)

    @jit.oopspec('libffi_call_void(self, funcsym, ll_args)')
    def _do_call_void(self, funcsym, ll_args):
        return self._do_call(funcsym, ll_args, lltype.Void)

    # ------------------------------------------------------------------------
    # private methods

    @specialize.argtype(1)
    def _push_arg(self, value, ll_args, i):
        # XXX: check the type is not translated?
        argtype = self.argtypes[i]
        c_size = intmask(argtype.c_size)
        ll_buf = lltype.malloc(rffi.CCHARP.TO, c_size, flavor='raw')
        push_arg_as_ffiptr(argtype, value, ll_buf)
        ll_args[i] = ll_buf

    @specialize.arg(3)
    def _do_call(self, funcsym, ll_args, RESULT):
        # XXX: check len(args)?
        ll_result = lltype.nullptr(rffi.CCHARP.TO)
        if self.restype != types.void:
            ll_result = lltype.malloc(rffi.CCHARP.TO,
                                      intmask(self.restype.c_size),
                                      flavor='raw')
        ffires = c_ffi_call(self.ll_cif,
                            self.funcsym,
                            rffi.cast(rffi.VOIDP, ll_result),
                            rffi.cast(rffi.VOIDPP, ll_args))
        if RESULT is not lltype.Void:
            TP = lltype.Ptr(rffi.CArray(RESULT))
            buf = rffi.cast(TP, ll_result)
            if types.is_struct(self.restype):
                assert RESULT == rffi.LONG
                # for structs, we directly return the buffer and transfer the
                # ownership
                res = rffi.cast(RESULT, buf)
            else:
                res = buf[0]
        else:
            res = None
        self._free_buffers(ll_result, ll_args)
        #check_fficall_result(ffires, self.flags)
        return res

    def _free_buffers(self, ll_result, ll_args):
        if ll_result:
            self._free_buffer_maybe(rffi.cast(rffi.VOIDP, ll_result), self.restype)
        for i in range(len(self.argtypes)):
            argtype = self.argtypes[i]
            self._free_buffer_maybe(ll_args[i], argtype)
        lltype.free(ll_args, flavor='raw')

    def _free_buffer_maybe(self, buf, ffitype):
        # if it's a struct, the buffer is not freed and the ownership is
        # already of the caller (in case of ll_args buffers) or transferred to
        # it (in case of ll_result buffer)
        if not types.is_struct(ffitype):
            lltype.free(buf, flavor='raw')


# ======================================================================


# XXX: it partially duplicate the code in clibffi.py
class CDLL(object):
    def __init__(self, libname, mode=-1):
        """Load the library, or raises DLOpenError."""
        self.lib = rffi.cast(DLLHANDLE, 0)
        with rffi.scoped_str2charp(libname) as ll_libname:
            self.lib = dlopen(ll_libname, mode)

    def __del__(self):
        if self.lib:
            dlclose(self.lib)
            self.lib = rffi.cast(DLLHANDLE, 0)

    def getpointer(self, name, argtypes, restype, flags=FUNCFLAG_CDECL):
        return Func(name, argtypes, restype, dlsym(self.lib, name),
                    flags=flags, keepalive=self)

    def getaddressindll(self, name):
        return dlsym(self.lib, name)

# These specialize.call_location's should really be specialize.arg(0), however
# you can't hash a pointer obj, which the specialize machinery wants to do.
# Given the present usage of these functions, it's good enough.
@specialize.call_location()
@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

@specialize.call_location()
@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
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.