| 1 |
""" |
| 2 |
This whole file is DEPRECATED. Use jit_libffi.py instead. |
| 3 |
""" |
| 4 |
from __future__ import with_statement |
| 5 |
|
| 6 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 7 |
from rpython.rlib.unroll import unrolling_iterable |
| 8 |
from rpython.rlib.objectmodel import specialize, enforceargs |
| 9 |
from rpython.rlib.rarithmetic import intmask, r_uint, r_singlefloat, r_longlong |
| 10 |
from rpython.rlib import jit |
| 11 |
from rpython.rlib import clibffi |
| 12 |
from rpython.rlib.clibffi import FUNCFLAG_CDECL, FUNCFLAG_STDCALL, \ |
| 13 |
AbstractFuncPtr, push_arg_as_ffiptr, c_ffi_call, FFI_TYPE_STRUCT, \ |
| 14 |
adjust_return_size |
| 15 |
from rpython.rlib.rdynload import dlopen, dlclose, dlsym, dlsym_byordinal |
| 16 |
from rpython.rlib.rdynload import DLLHANDLE |
| 17 |
|
| 18 |
import os |
| 19 |
import sys |
| 20 |
|
| 21 |
_BIG_ENDIAN = sys.byteorder == 'big' |
| 22 |
|
| 23 |
class types(object): |
| 24 |
""" |
| 25 |
This namespace contains the primitive types you can use to declare the |
| 26 |
signatures of the ffi functions. |
| 27 |
|
| 28 |
In general, the name of the types are closely related to the ones of the |
| 29 |
C-level ffi_type_*: e.g, instead of ffi_type_sint you should use |
| 30 |
libffi.types.sint. |
| 31 |
|
| 32 |
However, you should not rely on a perfect correspondence: in particular, |
| 33 |
the exact meaning of ffi_type_{slong,ulong} changes a lot between libffi |
| 34 |
versions, so types.slong could be different than ffi_type_slong. |
| 35 |
""" |
| 36 |
|
| 37 |
@classmethod |
| 38 |
def _import(cls): |
| 39 |
prefix = 'ffi_type_' |
| 40 |
for key, value in clibffi.__dict__.iteritems(): |
| 41 |
if key.startswith(prefix): |
| 42 |
name = key[len(prefix):] |
| 43 |
setattr(cls, name, value) |
| 44 |
cls.slong = clibffi.cast_type_to_ffitype(rffi.LONG) |
| 45 |
cls.ulong = clibffi.cast_type_to_ffitype(rffi.ULONG) |
| 46 |
cls.slonglong = clibffi.cast_type_to_ffitype(rffi.LONGLONG) |
| 47 |
cls.ulonglong = clibffi.cast_type_to_ffitype(rffi.ULONGLONG) |
| 48 |
cls.signed = clibffi.cast_type_to_ffitype(rffi.SIGNED) |
| 49 |
cls.unsigned = clibffi.cast_type_to_ffitype(rffi.UNSIGNED) |
| 50 |
cls.wchar_t = clibffi.cast_type_to_ffitype(lltype.UniChar) |
| 51 |
|
| 52 |
|
| 53 |
del cls._import |
| 54 |
|
| 55 |
@staticmethod |
| 56 |
@jit.elidable |
| 57 |
def getkind(ffi_type): |
| 58 |
"""Returns 'v' for void, 'f' for float, 'i' for signed integer, |
| 59 |
and 'u' for unsigned integer. |
| 60 |
""" |
| 61 |
if ffi_type is types.void: return 'v' |
| 62 |
elif ffi_type is types.double: return 'f' |
| 63 |
elif ffi_type is types.float: return 's' |
| 64 |
elif ffi_type is types.pointer: return 'u' |
| 65 |
|
| 66 |
elif ffi_type is types.schar: return 'i' |
| 67 |
elif ffi_type is types.uchar: return 'u' |
| 68 |
elif ffi_type is types.sshort: return 'i' |
| 69 |
elif ffi_type is types.ushort: return 'u' |
| 70 |
elif ffi_type is types.sint: return 'i' |
| 71 |
elif ffi_type is types.uint: return 'u' |
| 72 |
elif ffi_type is types.slong: return 'i' |
| 73 |
elif ffi_type is types.ulong: return 'u' |
| 74 |
|
| 75 |
elif ffi_type is types.sint8: return 'i' |
| 76 |
elif ffi_type is types.uint8: return 'u' |
| 77 |
elif ffi_type is types.sint16: return 'i' |
| 78 |
elif ffi_type is types.uint16: return 'u' |
| 79 |
elif ffi_type is types.sint32: return 'i' |
| 80 |
elif ffi_type is types.uint32: return 'u' |
| 81 |
|
| 82 |
elif ffi_type is types.signed: return 'i' |
| 83 |
elif ffi_type is types.unsigned:return 'u' |
| 84 |
|
| 85 |
|
| 86 |
elif ffi_type is types.sint64: return 'I' |
| 87 |
elif ffi_type is types.uint64: return 'U' |
| 88 |
|
| 89 |
elif types.is_struct(ffi_type): return 'S' |
| 90 |
raise KeyError |
| 91 |
|
| 92 |
@staticmethod |
| 93 |
@jit.elidable |
| 94 |
def is_struct(ffi_type): |
| 95 |
return intmask(ffi_type.c_type) == FFI_TYPE_STRUCT |
| 96 |
|
| 97 |
types._import() |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
@specialize.arg(0) |
| 104 |
def _fits_into_signed(TYPE): |
| 105 |
if isinstance(TYPE, lltype.Ptr): |
| 106 |
return True |
| 107 |
if not isinstance(TYPE, lltype.Primitive): |
| 108 |
return False |
| 109 |
if TYPE is lltype.Void or TYPE is rffi.FLOAT or TYPE is rffi.DOUBLE: |
| 110 |
return False |
| 111 |
sz = rffi.sizeof(TYPE) |
| 112 |
return sz <= rffi.sizeof(rffi.SIGNED) |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
IS_32_BIT = (r_uint.BITS == 32) |
| 118 |
IS_WIN64 = os.name == 'nt' and (r_uint.BITS == 64) |
| 119 |
|
| 120 |
@specialize.memo() |
| 121 |
def _check_type(TYPE): |
| 122 |
if isinstance(TYPE, lltype.Ptr): |
| 123 |
if TYPE.TO._gckind != 'raw': |
| 124 |
raise TypeError("Can only push raw values to C, not 'gc'") |
| 125 |
|
| 126 |
|
| 127 |
if isinstance(TYPE.TO, lltype.Array) and 'nolength' not in TYPE.TO._hints: |
| 128 |
raise TypeError("Can only push to C arrays without length info") |
| 129 |
|
| 130 |
|
| 131 |
class ArgChain(object): |
| 132 |
first = None |
| 133 |
last = None |
| 134 |
numargs = 0 |
| 135 |
|
| 136 |
@specialize.argtype(1) |
| 137 |
def arg(self, val): |
| 138 |
TYPE = lltype.typeOf(val) |
| 139 |
_check_type(TYPE) |
| 140 |
if _fits_into_signed(TYPE): |
| 141 |
cls = IntArg |
| 142 |
val = rffi.cast(rffi.SIGNED, val) |
| 143 |
elif TYPE is rffi.DOUBLE: |
| 144 |
cls = FloatArg |
| 145 |
elif TYPE is rffi.LONGLONG or TYPE is rffi.ULONGLONG: |
| 146 |
cls = LongLongArg |
| 147 |
val = rffi.cast(rffi.LONGLONG, val) |
| 148 |
elif TYPE is rffi.FLOAT: |
| 149 |
cls = SingleFloatArg |
| 150 |
else: |
| 151 |
raise TypeError('Unsupported argument type: %s' % TYPE) |
| 152 |
self._append(cls(val)) |
| 153 |
return self |
| 154 |
|
| 155 |
def arg_raw(self, val): |
| 156 |
self._append(RawArg(val)) |
| 157 |
|
| 158 |
def _append(self, arg): |
| 159 |
if self.first is None: |
| 160 |
self.first = self.last = arg |
| 161 |
else: |
| 162 |
self.last.next = arg |
| 163 |
self.last = arg |
| 164 |
self.numargs += 1 |
| 165 |
|
| 166 |
|
| 167 |
class AbstractArg(object): |
| 168 |
next = None |
| 169 |
|
| 170 |
class IntArg(AbstractArg): |
| 171 |
""" An argument holding an integer |
| 172 |
""" |
| 173 |
|
| 174 |
def __init__(self, intval): |
| 175 |
self.intval = intval |
| 176 |
|
| 177 |
def push(self, func, ll_args, i): |
| 178 |
func._push_int(self.intval, ll_args, i) |
| 179 |
|
| 180 |
|
| 181 |
class FloatArg(AbstractArg): |
| 182 |
""" An argument holding a python float (i.e. a C double) |
| 183 |
""" |
| 184 |
|
| 185 |
def __init__(self, floatval): |
| 186 |
self.floatval = floatval |
| 187 |
|
| 188 |
def push(self, func, ll_args, i): |
| 189 |
func._push_float(self.floatval, ll_args, i) |
| 190 |
|
| 191 |
class RawArg(AbstractArg): |
| 192 |
""" An argument holding a raw pointer to put inside ll_args |
| 193 |
""" |
| 194 |
|
| 195 |
def __init__(self, ptrval): |
| 196 |
self.ptrval = ptrval |
| 197 |
|
| 198 |
def push(self, func, ll_args, i): |
| 199 |
func._push_raw(self.ptrval, ll_args, i) |
| 200 |
|
| 201 |
class SingleFloatArg(AbstractArg): |
| 202 |
""" An argument representing a C float |
| 203 |
""" |
| 204 |
|
| 205 |
def __init__(self, singlefloatval): |
| 206 |
self.singlefloatval = singlefloatval |
| 207 |
|
| 208 |
def push(self, func, ll_args, i): |
| 209 |
func._push_singlefloat(self.singlefloatval, ll_args, i) |
| 210 |
|
| 211 |
|
| 212 |
class LongLongArg(AbstractArg): |
| 213 |
""" An argument representing a C long long |
| 214 |
""" |
| 215 |
|
| 216 |
def __init__(self, longlongval): |
| 217 |
self.longlongval = longlongval |
| 218 |
|
| 219 |
def push(self, func, ll_args, i): |
| 220 |
func._push_longlong(self.longlongval, ll_args, i) |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
NARROW_INTEGER_TYPES = unrolling_iterable([rffi.CHAR, rffi.SIGNEDCHAR, |
| 226 |
rffi.UCHAR, rffi.SHORT, rffi.USHORT, rffi.INT, rffi.UINT]) |
| 227 |
|
| 228 |
class Func(AbstractFuncPtr): |
| 229 |
|
| 230 |
_immutable_fields_ = ['funcsym'] |
| 231 |
argtypes = [] |
| 232 |
restype = clibffi.FFI_TYPE_NULL |
| 233 |
flags = 0 |
| 234 |
funcsym = lltype.nullptr(rffi.VOIDP.TO) |
| 235 |
|
| 236 |
def __init__(self, name, argtypes, restype, funcsym, flags=FUNCFLAG_CDECL, |
| 237 |
keepalive=None, variadic_args=0): |
| 238 |
AbstractFuncPtr.__init__(self, name, argtypes, restype, flags, variadic_args) |
| 239 |
self.keepalive = keepalive |
| 240 |
self.funcsym = funcsym |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
@jit.unroll_safe |
| 247 |
@specialize.arg(2, 3) |
| 248 |
def call(self, argchain, RESULT, is_struct=False): |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
self = jit.promote(self) |
| 263 |
if argchain.numargs != len(self.argtypes): |
| 264 |
raise TypeError('Wrong number of arguments: %d expected, got %d' % |
| 265 |
(len(self.argtypes), argchain.numargs)) |
| 266 |
ll_args = self._prepare() |
| 267 |
i = 0 |
| 268 |
arg = argchain.first |
| 269 |
while arg: |
| 270 |
arg.push(self, ll_args, i) |
| 271 |
i += 1 |
| 272 |
arg = arg.next |
| 273 |
|
| 274 |
if is_struct: |
| 275 |
assert types.is_struct(self.restype) |
| 276 |
res = self._do_call_raw(self.funcsym, ll_args) |
| 277 |
elif _fits_into_signed(RESULT): |
| 278 |
assert not types.is_struct(self.restype) |
| 279 |
for res in NARROW_INTEGER_TYPES: |
| 280 |
if RESULT is res: |
| 281 |
res = self._do_call_int(self.funcsym, ll_args, RESULT) |
| 282 |
break |
| 283 |
else: |
| 284 |
res = self._do_call_int(self.funcsym, ll_args, rffi.SIGNED) |
| 285 |
elif RESULT is rffi.DOUBLE: |
| 286 |
return self._do_call_float(self.funcsym, ll_args) |
| 287 |
elif RESULT is rffi.FLOAT: |
| 288 |
return self._do_call_singlefloat(self.funcsym, ll_args) |
| 289 |
elif RESULT is rffi.LONGLONG or RESULT is rffi.ULONGLONG: |
| 290 |
assert IS_32_BIT |
| 291 |
res = self._do_call_longlong(self.funcsym, ll_args) |
| 292 |
elif RESULT is lltype.Void: |
| 293 |
return self._do_call_void(self.funcsym, ll_args) |
| 294 |
else: |
| 295 |
raise TypeError('Unsupported result type: %s' % RESULT) |
| 296 |
|
| 297 |
return rffi.cast(RESULT, res) |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
@jit.dont_look_inside |
| 307 |
def _prepare(self): |
| 308 |
ll_args = lltype.malloc(rffi.VOIDPP.TO, len(self.argtypes), flavor='raw') |
| 309 |
return ll_args |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
@jit.dont_look_inside |
| 318 |
@enforceargs( None, int, None, int) |
| 319 |
def _push_int(self, value, ll_args, i): |
| 320 |
self._push_arg(value, ll_args, i) |
| 321 |
|
| 322 |
@jit.dont_look_inside |
| 323 |
def _push_raw(self, value, ll_args, i): |
| 324 |
ll_args[i] = value |
| 325 |
|
| 326 |
|
| 327 |
@jit.dont_look_inside |
| 328 |
@enforceargs( None, float, None, int) |
| 329 |
def _push_float(self, value, ll_args, i): |
| 330 |
self._push_arg(value, ll_args, i) |
| 331 |
|
| 332 |
|
| 333 |
@jit.dont_look_inside |
| 334 |
@enforceargs(None, r_singlefloat, None, int) |
| 335 |
def _push_singlefloat(self, value, ll_args, i): |
| 336 |
self._push_arg(value, ll_args, i) |
| 337 |
|
| 338 |
|
| 339 |
@jit.dont_look_inside |
| 340 |
@enforceargs(None, r_longlong, None, int) |
| 341 |
def _push_longlong(self, value, ll_args, i): |
| 342 |
self._push_arg(value, ll_args, i) |
| 343 |
|
| 344 |
|
| 345 |
@jit.dont_look_inside |
| 346 |
@specialize.arg(3) |
| 347 |
def _do_call_int(self, funcsym, ll_args, TP): |
| 348 |
return rffi.cast(rffi.SIGNED, self._do_call(funcsym, ll_args, TP)) |
| 349 |
|
| 350 |
|
| 351 |
@jit.dont_look_inside |
| 352 |
def _do_call_float(self, funcsym, ll_args): |
| 353 |
return self._do_call(funcsym, ll_args, rffi.DOUBLE) |
| 354 |
|
| 355 |
|
| 356 |
@jit.dont_look_inside |
| 357 |
def _do_call_singlefloat(self, funcsym, ll_args): |
| 358 |
return self._do_call(funcsym, ll_args, rffi.FLOAT) |
| 359 |
|
| 360 |
@jit.dont_look_inside |
| 361 |
def _do_call_raw(self, funcsym, ll_args): |
| 362 |
|
| 363 |
return self._do_call(funcsym, ll_args, rffi.SIGNED) |
| 364 |
|
| 365 |
|
| 366 |
@jit.dont_look_inside |
| 367 |
def _do_call_longlong(self, funcsym, ll_args): |
| 368 |
return self._do_call(funcsym, ll_args, rffi.LONGLONG) |
| 369 |
|
| 370 |
|
| 371 |
@jit.dont_look_inside |
| 372 |
def _do_call_void(self, funcsym, ll_args): |
| 373 |
return self._do_call(funcsym, ll_args, lltype.Void) |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
@specialize.argtype(1) |
| 379 |
def _push_arg(self, value, ll_args, i): |
| 380 |
|
| 381 |
argtype = self.argtypes[i] |
| 382 |
c_size = intmask(argtype.c_size) |
| 383 |
ll_buf = lltype.malloc(rffi.CCHARP.TO, c_size, flavor='raw') |
| 384 |
push_arg_as_ffiptr(argtype, value, ll_buf) |
| 385 |
ll_args[i] = ll_buf |
| 386 |
|
| 387 |
@specialize.arg(3) |
| 388 |
def _do_call(self, funcsym, ll_args, RESULT): |
| 389 |
|
| 390 |
ll_result = lltype.nullptr(rffi.VOIDP.TO) |
| 391 |
if self.restype != types.void: |
| 392 |
size = adjust_return_size(intmask(self.restype.c_size)) |
| 393 |
ll_result = lltype.malloc(rffi.VOIDP.TO, size, |
| 394 |
flavor='raw') |
| 395 |
ffires = c_ffi_call(self.ll_cif, |
| 396 |
self.funcsym, |
| 397 |
rffi.cast(rffi.VOIDP, ll_result), |
| 398 |
rffi.cast(rffi.VOIDPP, ll_args)) |
| 399 |
if RESULT is not lltype.Void: |
| 400 |
TP = lltype.Ptr(rffi.CArray(RESULT)) |
| 401 |
if types.is_struct(self.restype): |
| 402 |
assert RESULT == rffi.SIGNED |
| 403 |
|
| 404 |
|
| 405 |
buf = rffi.cast(TP, ll_result) |
| 406 |
res = rffi.cast(RESULT, buf) |
| 407 |
else: |
| 408 |
if _BIG_ENDIAN and types.getkind(self.restype) in ('i','u'): |
| 409 |
ptr = ll_result |
| 410 |
n = rffi.sizeof(lltype.Signed) - self.restype.c_size |
| 411 |
ptr = rffi.ptradd(ptr, n) |
| 412 |
res = rffi.cast(TP, ptr)[0] |
| 413 |
else: |
| 414 |
res = rffi.cast(TP, ll_result)[0] |
| 415 |
else: |
| 416 |
res = None |
| 417 |
self._free_buffers(ll_result, ll_args) |
| 418 |
clibffi.check_fficall_result(ffires, self.flags) |
| 419 |
return res |
| 420 |
|
| 421 |
def _free_buffers(self, ll_result, ll_args): |
| 422 |
if ll_result: |
| 423 |
self._free_buffer_maybe(rffi.cast(rffi.VOIDP, ll_result), self.restype) |
| 424 |
for i in range(len(self.argtypes)): |
| 425 |
argtype = self.argtypes[i] |
| 426 |
self._free_buffer_maybe(ll_args[i], argtype) |
| 427 |
lltype.free(ll_args, flavor='raw') |
| 428 |
|
| 429 |
def _free_buffer_maybe(self, buf, ffitype): |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
if not types.is_struct(ffitype): |
| 434 |
lltype.free(buf, flavor='raw') |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
class CDLL(object): |
| 442 |
def __init__(self, libname, mode=-1, lib=0): |
| 443 |
"""Load the library, or raises DLOpenError.""" |
| 444 |
self.lib = rffi.cast(DLLHANDLE, lib) |
| 445 |
if lib == 0: |
| 446 |
self.lib = dlopen(libname, mode) |
| 447 |
|
| 448 |
def __del__(self): |
| 449 |
if self.lib: |
| 450 |
dlclose(self.lib) |
| 451 |
self.lib = rffi.cast(DLLHANDLE, 0) |
| 452 |
|
| 453 |
def getpointer(self, name, argtypes, restype, flags=FUNCFLAG_CDECL, variadic_args=0): |
| 454 |
return Func(name, argtypes, restype, dlsym(self.lib, name), |
| 455 |
flags=flags, keepalive=self, variadic_args=variadic_args) |
| 456 |
|
| 457 |
def getpointer_by_ordinal(self, name, argtypes, restype, |
| 458 |
flags=FUNCFLAG_CDECL, variadic_args=0): |
| 459 |
return Func('by_ordinal', argtypes, restype, |
| 460 |
dlsym_byordinal(self.lib, name), |
| 461 |
flags=flags, keepalive=self, variadic_args=variadic_args) |
| 462 |
def getaddressindll(self, name): |
| 463 |
return dlsym(self.lib, name) |
| 464 |
|
| 465 |
def getidentifier(self): |
| 466 |
return rffi.cast(lltype.Unsigned, self.lib) |
| 467 |
|
| 468 |
if os.name == 'nt': |
| 469 |
class WinDLL(CDLL): |
| 470 |
def getpointer(self, name, argtypes, restype, flags=FUNCFLAG_STDCALL, variadic_args=0): |
| 471 |
return Func(name, argtypes, restype, dlsym(self.lib, name), |
| 472 |
flags=flags, keepalive=self, variadic_args=variadic_args) |
| 473 |
def getpointer_by_ordinal(self, name, argtypes, restype, |
| 474 |
flags=FUNCFLAG_STDCALL, variadic_args=0): |
| 475 |
return Func(name, argtypes, restype, dlsym_byordinal(self.lib, name), |
| 476 |
flags=flags, keepalive=self, variadic_args=variadic_args) |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
@jit.dont_look_inside |
| 482 |
def struct_getfield_int(ffitype, addr, offset): |
| 483 |
""" |
| 484 |
Return the field of type ``ffitype`` at ``addr+offset``, widened to |
| 485 |
lltype.Signed. |
| 486 |
""" |
| 487 |
for TYPE, ffitype2 in clibffi.ffitype_map_int_or_ptr: |
| 488 |
if ffitype is ffitype2: |
| 489 |
value = _struct_getfield(TYPE, addr, offset) |
| 490 |
return rffi.cast(lltype.Signed, value) |
| 491 |
assert False, "cannot find the given ffitype" |
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
@jit.dont_look_inside |
| 496 |
def struct_setfield_int(ffitype, addr, offset, value): |
| 497 |
""" |
| 498 |
Set the field of type ``ffitype`` at ``addr+offset``. ``value`` is of |
| 499 |
type lltype.Signed, and it's automatically converted to the right type. |
| 500 |
""" |
| 501 |
for TYPE, ffitype2 in clibffi.ffitype_map_int_or_ptr: |
| 502 |
if ffitype is ffitype2: |
| 503 |
value = rffi.cast(TYPE, value) |
| 504 |
_struct_setfield(TYPE, addr, offset, value) |
| 505 |
return |
| 506 |
assert False, "cannot find the given ffitype" |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
@jit.dont_look_inside |
| 511 |
def struct_getfield_longlong(ffitype, addr, offset): |
| 512 |
""" |
| 513 |
Return the field of type ``ffitype`` at ``addr+offset``, casted to |
| 514 |
lltype.LongLong. |
| 515 |
""" |
| 516 |
value = _struct_getfield(lltype.SignedLongLong, addr, offset) |
| 517 |
return value |
| 518 |
|
| 519 |
|
| 520 |
@jit.dont_look_inside |
| 521 |
def struct_setfield_longlong(ffitype, addr, offset, value): |
| 522 |
""" |
| 523 |
Set the field of type ``ffitype`` at ``addr+offset``. ``value`` is of |
| 524 |
type lltype.LongLong |
| 525 |
""" |
| 526 |
_struct_setfield(lltype.SignedLongLong, addr, offset, value) |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
@jit.dont_look_inside |
| 531 |
def struct_getfield_float(ffitype, addr, offset): |
| 532 |
value = _struct_getfield(lltype.Float, addr, offset) |
| 533 |
return value |
| 534 |
|
| 535 |
|
| 536 |
@jit.dont_look_inside |
| 537 |
def struct_setfield_float(ffitype, addr, offset, value): |
| 538 |
_struct_setfield(lltype.Float, addr, offset, value) |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
@jit.dont_look_inside |
| 543 |
def struct_getfield_singlefloat(ffitype, addr, offset): |
| 544 |
value = _struct_getfield(lltype.SingleFloat, addr, offset) |
| 545 |
return value |
| 546 |
|
| 547 |
|
| 548 |
@jit.dont_look_inside |
| 549 |
def struct_setfield_singlefloat(ffitype, addr, offset, value): |
| 550 |
_struct_setfield(lltype.SingleFloat, addr, offset, value) |
| 551 |
|
| 552 |
|
| 553 |
@specialize.arg(0) |
| 554 |
def _struct_getfield(TYPE, addr, offset): |
| 555 |
""" |
| 556 |
Read the field of type TYPE at addr+offset. |
| 557 |
addr is of type rffi.VOIDP, offset is an int. |
| 558 |
""" |
| 559 |
addr = rffi.ptradd(addr, offset) |
| 560 |
PTR_FIELD = lltype.Ptr(rffi.CArray(TYPE)) |
| 561 |
return rffi.cast(PTR_FIELD, addr)[0] |
| 562 |
|
| 563 |
|
| 564 |
@specialize.arg(0) |
| 565 |
def _struct_setfield(TYPE, addr, offset, value): |
| 566 |
""" |
| 567 |
Write the field of type TYPE at addr+offset. |
| 568 |
addr is of type rffi.VOIDP, offset is an int. |
| 569 |
""" |
| 570 |
addr = rffi.ptradd(addr, offset) |
| 571 |
PTR_FIELD = lltype.Ptr(rffi.CArray(TYPE)) |
| 572 |
rffi.cast(PTR_FIELD, addr)[0] = value |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
@specialize.call_location() |
| 580 |
|
| 581 |
@jit.dont_look_inside |
| 582 |
def array_getitem(ffitype, width, addr, index, offset): |
| 583 |
for TYPE, ffitype2 in clibffi.ffitype_map: |
| 584 |
if ffitype is ffitype2: |
| 585 |
addr = rffi.ptradd(addr, index * width) |
| 586 |
addr = rffi.ptradd(addr, offset) |
| 587 |
return rffi.cast(rffi.CArrayPtr(TYPE), addr)[0] |
| 588 |
assert False |
| 589 |
|
| 590 |
def array_getitem_T(TYPE, width, addr, index, offset): |
| 591 |
addr = rffi.ptradd(addr, index * width) |
| 592 |
addr = rffi.ptradd(addr, offset) |
| 593 |
return rffi.cast(rffi.CArrayPtr(TYPE), addr)[0] |
| 594 |
|
| 595 |
@specialize.call_location() |
| 596 |
|
| 597 |
@jit.dont_look_inside |
| 598 |
def array_setitem(ffitype, width, addr, index, offset, value): |
| 599 |
for TYPE, ffitype2 in clibffi.ffitype_map: |
| 600 |
if ffitype is ffitype2: |
| 601 |
addr = rffi.ptradd(addr, index * width) |
| 602 |
addr = rffi.ptradd(addr, offset) |
| 603 |
rffi.cast(rffi.CArrayPtr(TYPE), addr)[0] = value |
| 604 |
return |
| 605 |
assert False |
| 606 |
|
| 607 |
def array_setitem_T(TYPE, width, addr, index, offset, value): |
| 608 |
addr = rffi.ptradd(addr, index * width) |
| 609 |
addr = rffi.ptradd(addr, offset) |
| 610 |
rffi.cast(rffi.CArrayPtr(TYPE), addr)[0] = value |