summaryrefslogtreecommitdiffstats
path: root/stdlib/ffi.tl
blob: 04b5fe09aeac3d492481621cd214a1c4fa26a67b (plain)
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
;; Copyright 2017-2024
;; Kaz Kylheku <kaz@kylheku.com>
;; Vancouver, Canada
;; All rights reserved.
;;
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions are met:
;;
;; 1. Redistributions of source code must retain the above copyright notice,
;;    this list of conditions and the following disclaimer.
;;
;; 2. Redistributions in binary form must reproduce the above copyright notice,
;;    this list of conditions and the following disclaimer in the documentation
;;    and/or other materials provided with the distribution.
;;
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
;; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;; ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
;; CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
;; ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
;; POSSIBILITY OF SUCH DAMAGE.

(defmacro sys:dlib-expr (spec)
  (typecase spec
    (null ^(dlopen))
    (str ^(dlopen ,spec rtld-now))
    (t spec)))

(defmacro with-dyn-lib (lib . body)
  ^(let ((sys:ffi-lib (sys:dlib-expr ,lib)))
     ,*body))

(defun sys:with-dyn-lib-check (f e ref)
  (unless (lexical-var-p e 'sys:ffi-lib)
    (compile-warning f "simple ref ~s requires ~s"
                     ref 'with-dyn-lib)))

(defun sys:expand-sym-ref (f e exp)
  (cond
    ((stringp exp)
       (sys:with-dyn-lib-check f e exp)
       ^(dlsym-checked sys:ffi-lib ,exp))
    ((and (consp exp) (stringp (car exp)))
       (mac-param-bind f (sym ver) exp
         (sys:with-dyn-lib-check f e exp)
         ^(dlvsym-checked sys:ffi-lib ,sym ,ver)))
    (t exp)))

(defun sys:analyze-argtypes (form argtypes)
  (tree-bind (: ftypes vtypes) (split* argtypes (op where (op eq :)))
    (when vtypes
      (when (null ftypes)
        (compile-error form "variadic with zero fixed arguments not allowed"))
      (set vtypes
           (collect-each ((vt vtypes))
             (caseq vt
               ((float) 'double)
               ((be-float le-float)
                (compile-error form "variadic argument cannot be of type ~s"
                               vt))
               (t vt)))))
    (list* (+ (len ftypes) (len vtypes)) (len ftypes) (append ftypes vtypes))))


(defmacro deffi (:form f :env e name fun-expr rettype argtypes)
  (let ((fun-ref (sys:expand-sym-ref f e fun-expr))
        (fun-sym (gensym "fun-"))
        (desc-sym (gensym "desc-")))
    (tree-bind (nargs nfixed . argtypes) (sys:analyze-argtypes f argtypes)
      (let ((arg-syms (take nargs (gun (gensym)))))
        ^(let ((,fun-sym ,fun-ref)
               (,desc-sym (ffi-make-call-desc ,nargs ,nfixed
                                              (ffi-type-compile ',rettype)
                                              [mapcar ffi-type-compile
                                                      ',argtypes]
                                              ',name)))
           (defun ,name ,arg-syms
             (ffi-call ,fun-sym ,desc-sym ,*arg-syms)))))))

(defmacro deffi-type (name type-expr)
  ^(ffi-typedef ',name (ffi-type-compile ',type-expr)))

(defmacro typedef (name type-expr)
  ^(ffi-typedef ',name (ffi-type-compile ',type-expr)))

(defun sys:deffi-cb-expander (f name rettype argtypes safe-p abort-retval)
  (let ((fun-sym (gensym "fun-"))
        (desc-sym (gensym "desc-")))
    (tree-bind (nargs nvariadic . argtypes) (sys:analyze-argtypes f argtypes)
      ^(let ((,desc-sym (ffi-make-call-desc ,nargs ,nvariadic
                                            (ffi-type-compile ',rettype)
                                            [mapcar ffi-type-compile
                                                    ',argtypes]
                                            ',name)))
         (defun ,name (,fun-sym)
           [ffi-make-closure ,fun-sym ,desc-sym
                             ,safe-p ,abort-retval])))))

(defmacro deffi-cb (:form f name rettype argtypes : abort-retval)
  (sys:deffi-cb-expander f name rettype argtypes t abort-retval))

(defmacro deffi-cb-unsafe (:form f name rettype argtypes)
  (sys:deffi-cb-expander f name rettype argtypes nil nil))

(defmacro deffi-sym (:form f :env e name var-expr : type-sym)
  (let ((var-ref (sys:expand-sym-ref f e var-expr)))
    ^(defparml ,name ,(if type-sym
                        ^(cptr-cast ',type-sym ,var-ref)
                        var-ref))))

(defmacro deffi-var (:form f :env e name var-expr type)
  (let ((var-ref (sys:expand-sym-ref f e var-expr))
        (type-sym (gensym "type-"))
        (var-sym (gensym "var-")))
    ^(progn
       (defvarl ,type-sym (ffi ,type))
       (defvarl ,var-sym (carray-cptr ,var-ref ,type-sym 1))
       (defsymacro ,name (carray-ref ,var-sym 0)))))

(defmacro deffi-struct (name . body)
  ^(typedef ,name (struct ,name ,*body)))

(defmacro deffi-union (name . body)
  ^(typedef ,name (union ,name ,*body)))

(defmacro sizeof (type : (obj nil obj-p) :env menv)
  (if obj-p
    (if (constantp obj menv)
      (sys:dyn-size (ffi-type-compile type) obj)
      ^(sys:dyn-size (load-time (ffi-type-compile ',type)) ,obj))
    (ffi-size (ffi-type-compile type))))

(defmacro alignof (type)
  (ffi-alignof (ffi-type-compile type)))

(defmacro offsetof (struct memb)
  (ffi-offsetof (ffi-type-compile struct) memb))

(defmacro arraysize (arr)
  (ffi-arraysize (ffi-type-compile arr)))

(defmacro elemtype (type)
  ^(ffi-elemtype (ffi-type-compile ',type)))

(defmacro elemsize (type)
  (ffi-elemsize (ffi-type-compile type)))

(defmacro ffi (type)
  ^(load-time (ffi-type-compile ',type)))

(define-accessor carray-ref carray-refset)

(defset carray-sub (carray : (from 0) (to t)) items
  ^(progn (carray-replace ,carray ,items ,from ,to) ,items))

(defset sub-buf (buf : (from 0) (to t)) items
  ^(progn (replace-buf ,buf ,items ,from ,to) ,items))

(defmacro znew (type . pairs)
  (if (oddp (length pairs))
    (throwf 'eval-error "~s: slot initform arguments must occur pairwise"
            'znew))
  (let ((qpairs (mappend (aret ^(',@1 ,@2)) (tuples 2 pairs))))
    ^(make-zstruct (ffi ,type) ,*qpairs)))

(defmacro setjmp (:form f jmp-buf longjmp-var try-expr . longjmp-exprs)
  (unless (bindable longjmp-var)
    (compile-error f "~s is not a bindable symbol" longjmp-var))
  ^(sys:rt-setjmp ,jmp-buf
                  (lambda () ,try-expr)
                  (lambda (,longjmp-var) ,*longjmp-exprs)))