summaryrefslogtreecommitdiffstats
path: root/share/txr/stdlib/ffi.tl
blob: e171cb472b6aff87fe8986af59c7394a9acc2181 (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
;; Copyright 2017-2021
;; 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 ((keep-var (gensym "lib-")))
    ^(prog1
       (defvarl ,keep-var (sys:dlib-expr ,lib))
       (symacrolet ((sys:ffi-lib ,keep-var))
         ,*body))))

(defun sys:with-dyn-lib-check (f e ref)
  (when (eq (macroexpand 'sys:ffi-lib 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)
  (let ((p (posq : argtypes)))
    (when p
      (if (zerop p)
        (compile-error form "variadic with zero fixed arguments not allowed")
        (del [argtypes p])))
    (list* (length argtypes) p argtypes)))


(defmacro deffi (:form f :env e name fun-expr rettype argtypes)
  (let ((fun-ref (sys:expand-sym-ref f e fun-expr))
        (ret-type-sym (gensym "ret-type-"))
        (arg-types-sym (gensym "arg-types-"))
        (call-desc-sym (gensym "call-desc-"))
        (fun-sym (gensym "ffi-fun-")))
    (tree-bind (nargs nvariadic . argtypes) (sys:analyze-argtypes f argtypes)
      (let ((arg-syms (take nargs (gun (gensym)))))
        ^(progn
           (defvarl ,ret-type-sym (ffi-type-compile ',rettype))
           (defvarl ,arg-types-sym [mapcar ffi-type-compile ',argtypes])
           (defvarl ,call-desc-sym (ffi-make-call-desc ,nargs ,nvariadic
                                                       ,ret-type-sym
                                                       ,arg-types-sym))
           (defvarl ,fun-sym ,fun-ref)
           (defun ,name ,arg-syms
             (ffi-call ,fun-sym ,call-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 ((ret-type-sym (gensym "ret-type-"))
        (arg-types-sym (gensym "arg-types-"))
        (call-desc-sym (gensym "call-desc-"))
        (fun-sym (gensym "fun-")))
    (tree-bind (nargs nvariadic . argtypes) (sys:analyze-argtypes f argtypes)
      ^(progn
         (defvarl ,ret-type-sym (ffi-type-compile ',rettype))
         (defvarl ,arg-types-sym [mapcar ffi-type-compile ',argtypes])
         (defvarl ,call-desc-sym (ffi-make-call-desc ,nargs ,nvariadic
                                                     ,ret-type-sym
                                                     ,arg-types-sym))
         (defun ,name (,fun-sym)
           [ffi-make-closure ,fun-sym ,call-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 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)
  ^(ffi-type-compile ',type))

(define-accessor carray-ref carray-refset)

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

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

(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)))