| 1 |
import ctypes |
| 2 |
|
| 3 |
import py |
| 4 |
|
| 5 |
def primitive_pointer_repr(tp_s): |
| 6 |
return 'lltype.Ptr(lltype.FixedSizeArray(%s, 1))' % tp_s |
| 7 |
|
| 8 |
|
| 9 |
SIMPLE_TYPE_MAPPING = { |
| 10 |
ctypes.c_ubyte : 'rffi.UCHAR', |
| 11 |
ctypes.c_byte : 'rffi.CHAR', |
| 12 |
ctypes.c_char : 'rffi.CHAR', |
| 13 |
ctypes.c_int8 : 'rffi.CHAR', |
| 14 |
ctypes.c_ushort : 'rffi.USHORT', |
| 15 |
ctypes.c_short : 'rffi.SHORT', |
| 16 |
ctypes.c_uint16 : 'rffi.USHORT', |
| 17 |
ctypes.c_int16 : 'rffi.SHORT', |
| 18 |
ctypes.c_int : 'rffi.INT', |
| 19 |
ctypes.c_uint : 'rffi.UINT', |
| 20 |
ctypes.c_int32 : 'rffi.INT_real', |
| 21 |
ctypes.c_uint32 : 'rffi.UINT_real', |
| 22 |
ctypes.c_longlong : 'rffi.LONGLONG', |
| 23 |
ctypes.c_ulonglong : 'rffi.ULONGLONG', |
| 24 |
ctypes.c_int64 : 'rffi.LONGLONG', |
| 25 |
ctypes.c_uint64 : 'rffi.ULONGLONG', |
| 26 |
ctypes.c_voidp : 'rffi.VOIDP', |
| 27 |
None : 'rffi.lltype.Void', |
| 28 |
ctypes.c_char_p : 'rffi.CCHARP', |
| 29 |
ctypes.c_double : 'rffi.lltype.Float', |
| 30 |
} |
| 31 |
|
| 32 |
class RffiSource(object): |
| 33 |
def __init__(self, structs=None, source=None, includes=[], libraries=[], |
| 34 |
include_dirs=[]): |
| 35 |
|
| 36 |
if structs is None: |
| 37 |
self.structs = set() |
| 38 |
else: |
| 39 |
self.structs = structs |
| 40 |
if source is None: |
| 41 |
self.source = py.code.Source() |
| 42 |
else: |
| 43 |
self.source = source |
| 44 |
includes = includes and "includes=%s, " % repr(tuple(includes)) or '' |
| 45 |
libraries = libraries and "libraries=%s, " % repr(tuple(libraries)) or '' |
| 46 |
include_dirs = include_dirs and \ |
| 47 |
"include_dirs=%s, " % repr(tuple(include_dirs)) or '' |
| 48 |
self.extra_args = includes+libraries+include_dirs |
| 49 |
self.seen = {} |
| 50 |
self.forward_refs = 0 |
| 51 |
self.forward_refs_to_consider = {} |
| 52 |
|
| 53 |
def next_forward_reference(self): |
| 54 |
try: |
| 55 |
return "forward_ref%d" % self.forward_refs |
| 56 |
finally: |
| 57 |
self.forward_refs += 1 |
| 58 |
|
| 59 |
def __str__(self): |
| 60 |
return str(self.source) |
| 61 |
|
| 62 |
def __add__(self, other): |
| 63 |
structs = self.structs.copy() |
| 64 |
structs.update(other.structs) |
| 65 |
source = py.code.Source(self.source, other.source) |
| 66 |
return RffiSource(structs, source) |
| 67 |
|
| 68 |
def __iadd__(self, other): |
| 69 |
self.structs.update(other.structs) |
| 70 |
self.source = py.code.Source(self.source, other.source) |
| 71 |
return self |
| 72 |
|
| 73 |
def proc_struct(self, tp): |
| 74 |
name = tp.__name__ |
| 75 |
if tp not in self.structs: |
| 76 |
fields = ["('%s', %s), " % (name_, self.proc_tp(field_tp)) |
| 77 |
for name_, field_tp in tp._fields_] |
| 78 |
fields_repr = ''.join(fields) |
| 79 |
self.structs.add(tp) |
| 80 |
src = py.code.Source( |
| 81 |
"%s = lltype.Struct('%s', %s hints={'external':'C'})"%( |
| 82 |
name, name, fields_repr)) |
| 83 |
forward_ref = self.forward_refs_to_consider.get(tp, None) |
| 84 |
l = [self.source, src] |
| 85 |
if forward_ref: |
| 86 |
l.append(py.code.Source("\n%s.become(%s)\n" % (forward_ref, name))) |
| 87 |
self.source = py.code.Source(*l) |
| 88 |
return name |
| 89 |
|
| 90 |
def proc_forward_ref(self, tp): |
| 91 |
name = self.next_forward_reference() |
| 92 |
src = py.code.Source(""" |
| 93 |
%s = lltype.ForwardReference() |
| 94 |
""" % (name,) ) |
| 95 |
self.source = py.code.Source(self.source, src) |
| 96 |
self.forward_refs_to_consider[tp] = name |
| 97 |
return name |
| 98 |
|
| 99 |
def proc_tp(self, tp): |
| 100 |
try: |
| 101 |
return SIMPLE_TYPE_MAPPING[tp] |
| 102 |
except KeyError: |
| 103 |
pass |
| 104 |
if issubclass(tp, ctypes._Pointer): |
| 105 |
if issubclass(tp._type_, ctypes._SimpleCData): |
| 106 |
return "lltype.Ptr(lltype.Array(%s, hints={'nolength': True}))"\ |
| 107 |
% self.proc_tp(tp._type_) |
| 108 |
return "lltype.Ptr(%s)" % self.proc_tp(tp._type_) |
| 109 |
elif issubclass(tp, ctypes.Structure): |
| 110 |
if tp in self.seen: |
| 111 |
|
| 112 |
return self.proc_forward_ref(tp) |
| 113 |
self.seen[tp] = True |
| 114 |
return self.proc_struct(tp) |
| 115 |
elif issubclass(tp, ctypes.Array): |
| 116 |
return "lltype.Ptr(lltype.Array(%s, hints={'nolength': True}))" % \ |
| 117 |
self.proc_tp(tp._type_) |
| 118 |
raise NotImplementedError("Not implemented mapping for %s" % tp) |
| 119 |
|
| 120 |
def proc_func(self, func): |
| 121 |
name = func.__name__ |
| 122 |
if not self.extra_args: |
| 123 |
extra_args = "" |
| 124 |
else: |
| 125 |
extra_args = ", " + self.extra_args |
| 126 |
src = py.code.Source(""" |
| 127 |
%s = rffi.llexternal('%s', [%s], %s%s) |
| 128 |
"""%(name, name, ", ".join([self.proc_tp(arg) for arg in func.argtypes]), |
| 129 |
self.proc_tp(func.restype), extra_args)) |
| 130 |
self.source = py.code.Source(self.source, src) |
| 131 |
|
| 132 |
def proc_namespace(self, ns): |
| 133 |
exempt = set(id(value) for value in ctypes.__dict__.values()) |
| 134 |
for key, value in ns.items(): |
| 135 |
if id(value) in exempt: |
| 136 |
continue |
| 137 |
if isinstance(value, ctypes._CFuncPtr): |
| 138 |
self.proc_func(value) |
| 139 |
|
| 140 |
|
| 141 |
def compiled(self): |
| 142 |
|
| 143 |
globs = {} |
| 144 |
src = py.code.Source(""" |
| 145 |
from rpython.rtyper.lltypesystem import lltype |
| 146 |
from rpython.rtyper.lltypesystem import rffi |
| 147 |
""", self.source) |
| 148 |
exec(src.compile(), globs) |
| 149 |
return globs |