-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathffi.py
More file actions
933 lines (693 loc) · 27.8 KB
/
ffi.py
File metadata and controls
933 lines (693 loc) · 27.8 KB
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
py_object = object
import rpython.rlib.rdynload as dynload
import pixie.vm2.object as object
from pixie.vm2.object import runtime_error
from pixie.vm2.keyword import Keyword
import pixie.vm2.stdlib as proto
from pixie.vm2.code import as_var, affirm, extend_var, wrap_fn, intern_var
import pixie.vm2.rt as rt
from rpython.rtyper.lltypesystem import rffi, lltype, llmemory
from pixie.vm2.primitives import nil, true, false
from pixie.vm2.numbers import Integer, Float
from pixie.vm2.string import String
from pixie.vm2.keyword import Keyword
from rpython.rlib import clibffi
from rpython.rlib.jit_libffi import jit_ffi_call, CIF_DESCRIPTION, CIF_DESCRIPTION_P, \
FFI_TYPE_P, FFI_TYPE_PP, SIZE_OF_FFI_ARG
import rpython.rlib.jit_libffi as jit_libffi
from rpython.rlib.objectmodel import keepalive_until_here, we_are_translated
import rpython.rlib.jit as jit
from rpython.rlib.rarithmetic import intmask
"""
FFI interface for pixie.
This code gets a bit interesting. We use the RPython rlib module jit_libffi to do the interfacing, you can find
good docs in that module.
"""
class CType(object.Type):
def __init__(self, name):
object.Type.__init__(self, name)
load_paths = intern_var(u"pixie.stdlib", u"load-paths")
class ExternalLib(object.Object):
_type = object.Type(u"pixie.stdlib.ExternalLib")
def type(self):
return ExternalLib._type
def __init__(self, nm):
assert isinstance(nm, unicode)
self._name = nm
self._is_inited = False
self.load_lib()
def load_lib(self):
if not self._is_inited:
s = rffi.str2charp(str(self._name))
try:
self._dyn_lib = dynload.dlopen(s)
self._is_inited = True
finally:
rffi.free_charp(s)
def get_fn_ptr(self, nm):
assert isinstance(nm, unicode)
s = rffi.str2charp(str(nm))
sym = dynload.dlsym(self._dyn_lib, s)
rffi.free_charp(s)
return sym
@as_var("-ffi-library")
def _ffi_library(ns):
try:
return ExternalLib(ns.get_name())
except dynload.DLOpenError as ex:
return nil
class FFIFn(object.Object):
_type = object.Type(u"pixie.stdlib.FFIFn")
_immutable_fields_ = ["_is_inited", "_lib", "_name", "_arg_types[*]", "_arity", "_ret_type", "_is_variadic", \
"_transfer_size", "_arg0_offset", "_ret_offset", "_cd"]
def type(self):
return FFIFn._type
def __init__(self, lib, name, arg_types, ret_type, is_variadic):
self._rev = 0
self._name = name
self._lib = lib
self._arg_types = arg_types
self._arity = len(arg_types)
self._ret_type = ret_type
self._is_variadic = is_variadic
self._f_ptr = self._lib.get_fn_ptr(self._name)
self._cd = CifDescrBuilder(self._arg_types, self._ret_type).rawallocate()
@jit.unroll_safe
def prep_exb(self, args):
size = jit.promote(self._cd.exchange_size)
exb = rffi.cast(rffi.VOIDP, lltype.malloc(rffi.CCHARP.TO, size, flavor="raw"))
tokens = [None] * len(args)
for i, tp in enumerate(self._arg_types):
offset_p = rffi.ptradd(exb, jit.promote(self._cd.exchange_args[i]))
tokens[i] = tp.ffi_set_value(offset_p, args[i])
return exb, tokens
def get_ret_val_from_buffer(self, exb):
offset_p = rffi.ptradd(exb, jit.promote(self._cd.exchange_result))
ret_val = self._ret_type.ffi_get_value(offset_p)
return ret_val
@jit.unroll_safe
def _invoke(self, args):
arity = len(args)
if self._is_variadic:
if arity < self._arity:
runtime_error(u"Wrong number of args to fn: got " + unicode(str(arity)) +
u", expected at least " + unicode(str(self._arity)))
else:
if arity != self._arity:
runtime_error(u"Wrong number of args to fn: got " + unicode(str(arity)) +
u", expected " + unicode(str(self._arity)))
exb, tokens = self.prep_exb(args)
cd = jit.promote(self._cd)
#fp = jit.promote(self._f_ptr)
jit_ffi_call(cd,
self._f_ptr,
exb)
ret_val = self.get_ret_val_from_buffer(exb)
for x in range(len(args)):
t = tokens[x]
if t is not None:
t.finalize_token()
lltype.free(exb, flavor="raw")
keepalive_until_here(args)
return ret_val
def invoke_k(self, args, stack):
self = jit.promote(self)
return self._invoke(args), stack
@as_var("ffi-fn")
def _ffi_fn__args(args):
from pixie.vm2.array import Array
affirm(len(args) >= 4, u"ffi-fn requires at least 4 arguments")
lib, nm, arg_types, ret_type = args[:4]
affirm(isinstance(lib, ExternalLib), u"First argument must be an ExternalLib")
affirm(isinstance(ret_type, object.Type), u"Ret type must be a type")
affirm(nm.get_ns() is None, u"Name must not be namespaced")
affirm(isinstance(arg_types, Array), u"Arguments must be in an array")
assert isinstance(arg_types, Array)
new_args = arg_types._list
kwargs = args[4:]
affirm(len(kwargs) & 0x1 == 0, u"ffi-fn requires even number of options")
is_variadic = False
for i in range(0, len(kwargs)/2, 2):
key = kwargs[i]
val = kwargs[i+1]
affirm(isinstance(key, Keyword), u"ffi-fn options should be keyword/bool pairs")
affirm(val is true or val is false, u"ffi-fn options should be keyword/bool pairs")
k = key.get_name()
if k == u"variadic?":
is_variadic = True if val is true else False
else:
affirm(False, u"unknown ffi-fn option: :" + k)
f = FFIFn(lib, nm.get_name(), new_args, ret_type, is_variadic)
return f
@as_var("ffi-voidp")
def _ffi_voidp(lib, nm):
affirm(isinstance(lib, ExternalLib), u"First argument to ffi-voidp should be an external library")
name = nm.get_name()
return VoidP(lib.get_fn_ptr(name))
class Buffer(object.Object):
""" Defines a byte buffer with non-gc'd (therefore non-movable) contents
"""
_type = object.Type(u"pixie.stdlib.Buffer")
def type(self):
return Buffer._type
def __init__(self, size):
self._size = size
self._used_size = 0
self._buffer = lltype.malloc(rffi.CCHARP.TO, size, flavor="raw")
def __del__(self):
#lltype.free(self._buffer, flavor="raw")
pass
def set_used_size(self, size):
self._used_size = size
def buffer(self):
return self._buffer
def raw_data(self):
return rffi.cast(rffi.VOIDP, self._buffer)
def count(self):
return self._used_size
def nth_char(self, idx):
assert isinstance(idx, int)
return self._buffer[idx]
def capacity(self):
return self._size
def free_data(self):
lltype.free(self._buffer, flavor="raw")
#
# @extend(proto._dispose_BANG_, Buffer)
# def _dispose_voidp(self):
# self.free_data()
#
#
# @extend(proto._nth, Buffer)
# def _nth(self, idx):
# return rt.wrap(ord(self.nth_char(idx.int_val())))
#
# @extend(proto._nth_not_found, Buffer)
# def _nth_not_found(self, idx, not_found):
# return rt.wrap(ord(self.nth_char(idx.int_val())))
#
#
@extend_var("pixie.stdlib", "-count", Buffer)
def _count(self):
return rt.wrap(intmask(self.count()))
@as_var("buffer")
def buffer(size):
return Buffer(size.int_val())
@as_var("buffer-capacity")
def buffer_capacity(buffer):
return rt.wrap(intmask(buffer.capacity()))
@as_var("set-buffer-count!")
def set_buffer_size(self, size):
self.set_used_size(size.int_val())
return self
def make_itype(name, ctype, llt):
lltp = lltype.Ptr(lltype.Array(llt, hints={'nolength': True}))
class GenericCInt(CType):
def __init__(self):
CType.__init__(self, name)
def ffi_get_value(self, ptr):
casted = rffi.cast(lltp, ptr)
return Integer(rffi.cast(rffi.LONG, casted[0]))
def ffi_set_value(self, ptr, val):
casted = rffi.cast(lltp, ptr)
casted[0] = rffi.cast(llt, val.int_val())
def ffi_size(self):
return rffi.sizeof(llt)
def ffi_type(self):
return ctype
return GenericCInt()
from rpython.rlib.rarithmetic import build_int
for x in [8, 16, 32, 64]:
for s in [True, False]:
nm = "C" + ("" if s else "U") + "Int" + str(x)
int_tp = lltype.build_number(None, build_int(nm, s, x))
ctype = clibffi.cast_type_to_ffitype(int_tp)
make_itype(unicode("pixie.stdlib." + nm), ctype, int_tp)
class Token(py_object):
""" Tokens are returned by ffi_set_value and are called when ffi is ready to clean up resources
"""
def finalize_token(self):
pass
class CInt(CType):
def __init__(self):
CType.__init__(self, u"pixie.stdlib.CInt")
def ffi_get_value(self, ptr):
casted = rffi.cast(rffi.INTP, ptr)
return Integer(rffi.cast(rffi.LONG, casted[0]))
def ffi_set_value(self, ptr, val):
casted = rffi.cast(rffi.INTP, ptr)
casted[0] = rffi.cast(rffi.INT, val.int_val())
def ffi_size(self):
return rffi.sizeof(rffi.INT)
def ffi_type(self):
return clibffi.cast_type_to_ffitype(rffi.INT)
CInt()
class CDouble(CType):
def __init__(self):
CType.__init__(self, u"pixie.stdlib.CDouble")
def ffi_get_value(self, ptr):
casted = rffi.cast(rffi.DOUBLEP, ptr)
return Float(casted[0])
def ffi_set_value(self, ptr, val):
casted = rffi.cast(rffi.DOUBLEP, ptr)
casted[0] = rffi.cast(rffi.DOUBLE, val.float_val())
def ffi_size(self):
return rffi.sizeof(rffi.DOUBLE)
def ffi_type(self):
return clibffi.cast_type_to_ffitype(rffi.DOUBLE)
CDouble()
class CCharP(CType):
def __init__(self):
CType.__init__(self, u"pixie.stdlib.CCharP")
def ffi_get_value(self, ptr):
casted = rffi.cast(rffi.CCHARPP, ptr)
if casted[0] == lltype.nullptr(rffi.CCHARP.TO):
return nil
else:
return String(unicode(rffi.charp2str(casted[0])))
def ffi_set_value(self, ptr, val):
if isinstance(val, String):
pnt = rffi.cast(rffi.CCHARPP, ptr)
utf8 = unicode_to_utf8(val.get_name())
raw = rffi.str2charp(utf8)
pnt[0] = raw
return CCharPToken(raw)
elif isinstance(val, Buffer):
vpnt = rffi.cast(rffi.VOIDPP, ptr)
vpnt[0] = val.buffer()
elif isinstance(val, VoidP):
vpnt = rffi.cast(rffi.VOIDPP, ptr)
vpnt[0] = val.raw_data()
elif val is nil:
vpnt = rffi.cast(rffi.VOIDPP, ptr)
vpnt[0] = rffi.cast(rffi.VOIDP, 0)
elif isinstance(val, CStruct):
vpnt = rffi.cast(rffi.VOIDPP, ptr)
vpnt[0] = rffi.cast(rffi.VOIDP, val.raw_data())
else:
frm_name = val.to_repr()
to_name = self.to_repr()
affirm(False, u"Cannot encode " + frm_name + u" as " + to_name)
def ffi_size(self):
return rffi.sizeof(rffi.CCHARP)
def ffi_type(self):
return clibffi.ffi_type_pointer
CCharP()
class CCharPToken(Token):
def __init__(self, raw):
self._raw = raw
def finalize_token(self):
rffi.free_charp(self._raw)
class CVoid(CType):
def __init__(self):
CType.__init__(self, u"pixie.stdlib.CVoid")
def ffi_get_value(self, ptr):
return nil
def ffi_set_value(self, ptr, val):
runtime_error(u"Can't encode a Void")
def ffi_size(self):
return rffi.sizeof(rffi.VOIDP)
def ffi_type(self):
return clibffi.ffi_type_pointer
cvoid = CVoid()
class CVoidP(CType):
def __init__(self):
CType.__init__(self, u"pixie.stdlib.CVoidP")
def ffi_get_value(self, ptr):
casted = rffi.cast(rffi.VOIDPP, ptr)
if casted[0] == lltype.nullptr(rffi.VOIDP.TO):
return nil
else:
return VoidP(casted[0])
def ffi_set_value(self, ptr, val):
pnt = rffi.cast(rffi.VOIDPP, ptr)
if isinstance(val, String):
pnt = rffi.cast(rffi.CCHARPP, ptr)
utf8 = unicode_to_utf8(val.get_name())
raw = rffi.str2charp(utf8)
pnt[0] = raw
return CCharPToken(raw)
elif isinstance(val, Buffer):
pnt[0] = val.buffer()
elif isinstance(val, VoidP):
pnt[0] = val.raw_data()
elif val is nil:
pnt[0] = rffi.cast(rffi.VOIDP, 0)
elif isinstance(val, CStruct):
pnt[0] = rffi.cast(rffi.VOIDP, val.raw_data())
else:
frm_name = val.to_repr()
to_name = self.to_repr()
affirm(False, u"Cannot encode " + frm_name + u" as " + to_name)
def ffi_size(self):
return rffi.sizeof(rffi.VOIDP)
def ffi_type(self):
return clibffi.ffi_type_pointer
cvoidp = CVoidP()
class VoidP(object.Object):
def type(self):
return cvoidp
def __init__(self, raw_data):
self._raw_data = raw_data
def raw_data(self):
return rffi.cast(rffi.VOIDP, self._raw_data)
def free_data(self):
lltype.free(self._raw_data, flavor="raw")
# @extend(proto._dispose_BANG_, cvoidp)
# def _dispose_voidp(self):
# self.free_data()
@as_var(u"pixie.ffi", u"prep-string")
def prep_string(s):
"""Takes a Pixie string and returns a VoidP to that string. The string should be freed via dispose!, otherwise
memory leaks could result."""
affirm(isinstance(s, String), u"Can only prep strings with prep-string")
utf8 = unicode_to_utf8(s.get_name())
raw = rffi.str2charp(utf8)
return VoidP(rffi.cast(rffi.VOIDP, raw))
@as_var(u"pixie.ffi", u"unpack")
def unpack(ptr, offset, tp):
"""(unpack ptr offset tp)
Reads a value of type tp from offset of ptr."""
affirm(isinstance(ptr, VoidP) or isinstance(ptr, Buffer) or isinstance(ptr, CStruct), u"Type is not unpackable")
affirm(isinstance(tp, CType), u"Packing type must be a CType")
ptr = rffi.ptradd(ptr.raw_data(), offset.int_val())
return tp.ffi_get_value(ptr)
@as_var(u"pixie.ffi", u"pack!")
def pack(ptr, offset, tp, val):
"""(pack! ptr offset tp val)
Writes val at offset of ptr with the format tp"""
affirm(isinstance(ptr, VoidP) or isinstance(ptr, Buffer) or isinstance(ptr, CStruct), u"Type is not unpackable")
affirm(isinstance(tp, CType), u"Packing type must be a CType")
ptr = rffi.ptradd(ptr.raw_data(), offset.int_val())
tp.ffi_set_value(ptr, val)
return nil
@as_var(u"pixie.ffi", u"ptr-add")
def pack(ptr, offset):
affirm(isinstance(ptr, VoidP) or isinstance(ptr, Buffer) or isinstance(ptr, CStruct), u"Type is not unpackable")
ptr = rffi.ptradd(ptr.raw_data(), offset.int_val())
return VoidP(ptr)
class CStructType(object.Type):
base_type = object.Type(u"pixie.ffi.CStruct")
_immutable_fields_ = ["_desc", "_size"]
def __init__(self, name, size, desc):
object.Type.__init__(self, name, CStructType.base_type)
self._desc = desc
self._size = size
#offsets is a dict of {nm, (type, offset)}
def get_offset(self, nm):
(tp, offset) = self._desc.get(nm, (None, 0))
assert tp is not None
return offset
def get_type(self, nm):
(tp, offset) = self._desc.get(nm, (None, 0))
assert tp is not None
return tp
def get_size(self):
return self._size
def cast_to(self, frm):
return CStruct(self, frm.raw_data())
@jit.elidable_promote()
def get_desc(self, nm):
return self._desc.get(nm, (None, 0))
def invoke(self, args):
return CStruct(self, rffi.cast(rffi.VOIDP, lltype.malloc(rffi.CCHARP.TO, self._size, flavor="raw")))
registered_callbacks = {}
class CCallback(object.Object):
_type = object.Type(u"pixie.ffi.CCallback")
def __init__(self, cft, raw_closure, id, fn):
self._fn = fn
self._cft = cft
self._raw_closure = raw_closure
self._is_invoked = False
self._unique_id = id
def type(self):
return CCallback._type
def get_raw_closure(self):
return self._raw_closure
def ll_invoke(self, llargs, llres):
cft = self._cft
assert isinstance(cft, CFunctionType)
args = [None] * len(cft._arg_types)
for i, tp in enumerate(cft._arg_types):
args[i] = tp.ffi_get_value(llargs[i])
self._is_invoked = True
retval = self._fn.invoke(args)
if cft._ret_type is not cvoid:
cft._ret_type.ffi_set_value(llres, retval)
def cleanup(self):
del registered_callbacks[self._unique_id]
clibffi.closureHeap.free(self._raw_closure)
# @extend(proto._dispose_BANG_, CCallback)
# def _dispose(self):
# self.cleanup()
@as_var(u"pixie.ffi", u"-ffi-callback")
def ffi_callback(args, ret_type):
"""(ffi-callback args ret-type)
Creates a ffi callback type. Args is a vector of CType args. Ret-type is the CType return
type of the callback. Returns a ffi callback type that can be used with ffi-prep-callback."""
from pixie.vm2.array import Array
assert isinstance(args, Array)
args_w = args._list
return CFunctionType(args_w, ret_type)
@as_var(u"pixie.ffi", u"ffi-prep-callback")
def ffi_prep_callback(tp, f):
"""(ffi-prep-callback callback-tp fn)
Prepares a Pixie function for use as a c callback. callback-tp is a ffi callback type,
fn is a pixie function (can be a closure, native fn or any object that implements -invoke.
Returns a function pointer that can be passed to c and invoked as a callback."""
affirm(isinstance(tp, CFunctionType), u"First argument to ffi-prep-callback must be a CFunctionType")
raw_closure = rffi.cast(rffi.VOIDP, clibffi.closureHeap.alloc())
unique_id = rffi.cast(lltype.Signed, raw_closure)
res = clibffi.c_ffi_prep_closure(rffi.cast(clibffi.FFI_CLOSUREP, raw_closure), tp.get_cd().cif,
invoke_callback,
rffi.cast(rffi.VOIDP, unique_id))
if rffi.cast(lltype.Signed, res) != clibffi.FFI_OK:
registered_callbacks[unique_id] = None
runtime_error(u"libffi failed to build this callback")
cb = CCallback(tp, raw_closure, unique_id, f)
registered_callbacks[unique_id] = cb
return cb
# Callback Code
@jit.jit_callback("CFFI")
def invoke_callback(ffi_cif, ll_res, ll_args, ll_userdata):
cb = registered_callbacks[rffi.cast(rffi.INT_real, ll_userdata)]
cb.ll_invoke(ll_args, ll_res)
class FunctionTypeNameGenerator(py_object):
def __init__(self):
self._idx = 0
def next(self):
self._idx += 1
return unicode("CFunctionType" + str(self._idx))
name_gen = FunctionTypeNameGenerator()
class CFunctionType(object.Type):
base_type = object.Type(u"pixie.ffi.CType")
_immutable_fields_ = ["_arg_types", "_ret-type", "_cd"]
def __init__(self, arg_types, ret_type):
object.Type.__init__(self, name_gen.next(), CStructType.base_type)
self._arg_types = arg_types
self._ret_type = ret_type
self._cd = CifDescrBuilder(self._arg_types, self._ret_type).rawallocate()
def ffi_get_value(self, ptr):
runtime_error(u"Cannot get a callback value via FFI")
def ffi_set_value(self, ptr, val):
affirm(isinstance(val, CCallback), u"Can only encode CCallbacks as function pointers")
casted = rffi.cast(rffi.VOIDPP, ptr)
casted[0] = val.get_raw_closure()
return None
def get_cd(self):
return self._cd
def ffi_size(self):
return rffi.sizeof(rffi.VOIDP)
def ffi_type(self):
return clibffi.ffi_type_pointer
class CStruct(object.Object):
_immutable_fields_ = ["_type", "_buffer"]
def __init__(self, tp, buffer):
self._type = tp
self._buffer = buffer
def type(self):
return self._type
def raw_data(self):
return rffi.cast(rffi.VOIDP, self._buffer)
def val_at(self, k, not_found):
(tp, offset) = self._type.get_desc(k)
if tp is None:
return not_found
offset = rffi.ptradd(self._buffer, offset)
return tp.ffi_get_value(rffi.cast(rffi.VOIDP, offset))
def set_val(self, k, v):
(tp, offset) = self._type.get_desc(k)
if tp is None:
runtime_error(u"Invalid field name: " + k.to_repr())
offset = rffi.ptradd(self._buffer, offset)
tp.ffi_set_value(rffi.cast(rffi.VOIDP, offset), v)
return nil
def free_data(self):
lltype.free(self._buffer, flavor="raw")
@wrap_fn
def _dispose_cstruct(self):
self.free_data()
@as_var("pixie.ffi", "c-struct")
def c_struct(name, size, spec):
"""(c-struct name size spec)
Creates a CStruct named name, of size size, with the given spec. Spec is a vector
of vectors. Each row of the format [field-name type offset]"""
from pixie.vm2.array import Array
from pixie.vm2.code import intern_var
d = {}
assert isinstance(spec, Array)
spec_lst = spec._list
for x in range(len(spec_lst)):
row = spec_lst[x]
assert isinstance(row, Array)
row_lst = row._list
nm = row_lst[0]
tp = row_lst[1]
offset = row_lst[2]
affirm(isinstance(nm, Keyword), u"c-struct field names must be keywords")
if not isinstance(tp, CType):
runtime_error(u"c-struct field types must be c types, got: " + tp.to_repr())
d[nm] = (tp, offset.int_val())
tp = CStructType(name.get_name(), size.int_val(), d)
dispose_var = intern_var(u"pixie.stdlib", u"-dispose!")
dispose_var.deref().extend(tp, _dispose_cstruct)
return tp
@as_var("pixie.ffi", "cast")
def c_cast(frm, to):
"""(cast from to)
Converts a VoidP to a CStruct. From is either a VoidP or a CStruct, to is a CStruct type."""
if not isinstance(to, CStructType):
runtime_error(u"Expected a CStruct type to cast to, got " + to.to_repr())
if not isinstance(frm, CStruct) and not isinstance(frm, VoidP):
runtime_error(u"From must be a CVoidP or a CStruct, got " + to.to_repr())
return to.cast_to(frm)
@as_var("pixie.ffi", "struct-size")
def struct_size(tp):
"""(struct-size tp)
Gives the size of the given CStruct type tp, in bytes."""
if not isinstance(tp, CStructType):
runtime_error(u"Expected a CStruct type to get the size of, got " + tp.to_repr())
return rt.wrap(tp.get_size())
# @extend(proto._val_at, CStructType.base_type)
# def val_at(self, k, not_found):
# return self.val_at(k, not_found)
@as_var("pixie.ffi", "set!")
def set_(self, k, val):
"""(set! ptr k val)
Sets a field k of struct ptr to value val"""
return self.set_val(k, val)
@as_var("pixie.ffi", "prep-ffi-call")
def prep_ffi_call__args(args):
fn = args[0]
affirm(isinstance(fn, CFunctionType), u"First arg must be a FFI function")
import sys
USE_C_LIBFFI_MSVC = getattr(clibffi, 'USE_C_LIBFFI_MSVC', False)
class CifDescrBuilder(py_object):
rawmem = lltype.nullptr(rffi.CCHARP.TO)
def __init__(self, fargs, fresult):
self.fargs = fargs
self.fresult = fresult
def fb_alloc(self, size):
size = llmemory.raw_malloc_usage(size)
if not self.bufferp:
self.nb_bytes += size
return lltype.nullptr(rffi.CCHARP.TO)
else:
result = self.bufferp
self.bufferp = rffi.ptradd(result, size)
return result
def fb_fill_type(self, ctype, is_result_type):
return ctype.ffi_type()
def fb_build(self):
# Build a CIF_DESCRIPTION. Actually this computes the size and
# allocates a larger amount of data. It starts with a
# CIF_DESCRIPTION and continues with data needed for the CIF:
#
# - the argument types, as an array of 'ffi_type *'.
#
# - optionally, the result's and the arguments' ffi type data
# (this is used only for 'struct' ffi types; in other cases the
# 'ffi_type *' just points to static data like 'ffi_type_sint32').
#
nargs = len(self.fargs)
# start with a cif_description (cif and exchange_* fields)
self.fb_alloc(llmemory.sizeof(CIF_DESCRIPTION, nargs))
# next comes an array of 'ffi_type*', one per argument
atypes = self.fb_alloc(rffi.sizeof(FFI_TYPE_P) * nargs)
self.atypes = rffi.cast(FFI_TYPE_PP, atypes)
# next comes the result type data
self.rtype = self.fb_fill_type(self.fresult, True)
# next comes each argument's type data
for i, farg in enumerate(self.fargs):
atype = self.fb_fill_type(farg, False)
if self.atypes:
self.atypes[i] = atype
def align_arg(self, n):
return (n + 7) & ~7
def fb_build_exchange(self, cif_descr):
nargs = len(self.fargs)
# first, enough room for an array of 'nargs' pointers
exchange_offset = rffi.sizeof(rffi.VOIDP) * nargs
exchange_offset = self.align_arg(exchange_offset)
cif_descr.exchange_result = exchange_offset
# then enough room for the result, rounded up to sizeof(ffi_arg)
exchange_offset += max(rffi.getintfield(self.rtype, 'c_size'),
SIZE_OF_FFI_ARG)
# loop over args
for i, farg in enumerate(self.fargs):
#if isinstance(farg, W_CTypePointer):
# exchange_offset += 1 # for the "must free" flag
exchange_offset = self.align_arg(exchange_offset)
cif_descr.exchange_args[i] = exchange_offset
exchange_offset += rffi.getintfield(self.atypes[i], 'c_size')
# store the exchange data size
cif_descr.exchange_size = exchange_offset
def fb_extra_fields(self, cif_descr):
cif_descr.abi = clibffi.FFI_DEFAULT_ABI # XXX
cif_descr.nargs = len(self.fargs)
cif_descr.rtype = self.rtype
cif_descr.atypes = self.atypes
@jit.dont_look_inside
def rawallocate(self):
# compute the total size needed in the CIF_DESCRIPTION buffer
self.nb_bytes = 0
self.bufferp = lltype.nullptr(rffi.CCHARP.TO)
self.fb_build()
# allocate the buffer
if we_are_translated():
rawmem = lltype.malloc(rffi.CCHARP.TO, self.nb_bytes,
flavor='raw')
rawmem = rffi.cast(CIF_DESCRIPTION_P, rawmem)
else:
# gross overestimation of the length below, but too bad
rawmem = lltype.malloc(CIF_DESCRIPTION_P.TO, self.nb_bytes,
flavor='raw')
# the buffer is automatically managed from the W_CTypeFunc instance
# ctypefunc._cd = rawmem
# call again fb_build() to really build the libffi data structures
self.bufferp = rffi.cast(rffi.CCHARP, rawmem)
self.fb_build()
assert self.bufferp == rffi.ptradd(rffi.cast(rffi.CCHARP, rawmem),
self.nb_bytes)
# fill in the 'exchange_*' fields
self.fb_build_exchange(rawmem)
# fill in the extra fields
self.fb_extra_fields(rawmem)
# call libffi's ffi_prep_cif() function
res = jit_libffi.jit_ffi_prep_cif(rawmem)
if res != clibffi.FFI_OK:
runtime_error(u"libffi failed to build function type")
return rawmem
def get_item(self, nm):
(tp, offset) = self._type.get_desc(nm)
ptr = rffi.ptradd(self._buffer, offset)
return tp.ffi_load_from(ptr)
# unicode utils
from rpython.rlib.runicode import str_decode_utf_8, unicode_encode_utf_8
def unicode_from_utf8(s):
"""Converts a `str` value to a `unicode` value assuming it's encoded in UTF8."""
res, _ = str_decode_utf_8(s, len(s), 'strict')
return res
def unicode_to_utf8(s):
"""Converts a `unicode` value to a UTF8 encoded `str` value."""
return unicode_encode_utf_8(s, len(s), 'strict')