Debian Codegraph @

src/cmp/cmppass1-ffi.lsp

raw
1 ;;;;
2 ;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya
3 ;;;; Copyright (c) 1990, Giuseppe Attardi
4 ;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll
5 ;;;; Copyright (c) 2021, Daniel Kochmański
6 ;;;;
7 ;;;; See file 'LICENSE' for the copyright details.
8 ;;;;
9
10 (in-package #:compiler)
11
12 ;;; FIXME provide the AST form for clines. Currently c1clines is defined in
13 ;;; cmppass2-ffi and pushes directly to a backend-specific variable.
14 #+ (or)
15 (defun c1clines (args)
16 (make-c1form* 'ffi:clines :args args))
17
18 (defun c1c-inline (args)
19 ;; We are on the safe side by assuming that the form has side effects
20 (destructuring-bind (arguments arg-types output-type c-expression
21 &rest rest
22 &key (side-effects t) one-liner
23 &aux output-host-type)
24 args
25 (unless (= (length arguments) (length arg-types))
26 (cmperr "In a C-INLINE form the number of declare arguments and the number of supplied ones do not match:~%~S"
27 `(ffi:c-inline ,@args)))
28 ;; We cannot handle :cstrings as input arguments. :cstrings are
29 ;; null-terminated strings, but not all of our lisp strings will
30 ;; be null terminated. In particular, those with a fill pointer
31 ;; will not.
32 (ext:when-let ((ndx (position :cstring arg-types)))
33 (let* ((var (gensym))
34 (arguments (copy-list arguments))
35 (value (elt arguments ndx)))
36 (setf (elt arguments ndx) var
37 (elt arg-types ndx) :char*)
38 (return-from c1c-inline
39 (c1expr
40 `(ffi::with-cstring (,var ,value)
41 (ffi:c-inline ,arguments ,arg-types ,output-type ,c-expression
42 ,@rest))))))
43 ;; Find out the output types of the inline form. The syntax is rather relaxed
44 ;; output-type = lisp-type | c-type | (values {lisp-type | c-type}*)
45 (flet ((produce-type-pair (type)
46 (if (lisp-type-p type)
47 (cons type (lisp-type->host-type type))
48 (cons (host-type->lisp-type type) type))))
49 (cond ((eq output-type ':void)
50 (setf output-host-type '()
51 output-type 'NIL))
52 ((equal output-type '(VALUES &REST t))
53 (setf output-host-type '((VALUES &REST t))))
54 ((and (consp output-type) (eql (first output-type) 'VALUES))
55 (let ((x (mapcar #'produce-type-pair (rest output-type))))
56 (setf output-host-type (mapcar #'cdr x)
57 output-type `(VALUES ,@(mapcar #'car x)))))
58 (t
59 (let ((x (produce-type-pair output-type)))
60 (setf output-type (car x)
61 output-host-type (list (cdr x)))))))
62 (unless (and (listp arguments)
63 (listp arg-types)
64 (stringp c-expression))
65 (cmperr "C-INLINE: syntax error in ~S"
66 (list* 'ffi:c-inline args)))
67 (unless (= (length arguments)
68 (length arg-types))
69 (cmperr "C-INLINE: wrong number of arguments in ~S"
70 (list* 'ffi:c-inline args)))
71 (let* ((arguments (mapcar #'c1expr arguments))
72 (form (make-c1form* 'ffi:c-inline :type output-type
73 :side-effects side-effects
74 :args arguments arg-types
75 output-host-type
76 c-expression
77 side-effects
78 one-liner)))
79 (loop for form in arguments
80 when (eq (c1form-name form) 'VARIABLE)
81 do (let ((var (c1form-arg 0 form)))
82 (add-to-set-nodes var form)))
83 form)))
84
85 (defun c1c-progn (arguments)
86 (let* ((variables (mapcar #'c1vref (pop arguments)))
87 (statements (loop for form in arguments
88 collect (if (stringp form)
89 form
90 (c1expr form))))
91 (form (make-c1form* 'FFI:C-PROGN :type NIL
92 :side-effects t
93 :args variables statements)))
94 (add-to-set-nodes-of-var-list variables form)
95 form))
96
97 ;;;;
98 ;;;; CMPCBK -- Callbacks: lisp functions that can be called from the C world
99
100 (defconstant +foreign-elt-type-codes+
101 '( (:char . "ECL_FFI_CHAR")
102 (:unsigned-char . "ECL_FFI_UNSIGNED_CHAR")
103 (:byte . "ECL_FFI_BYTE")
104 (:unsigned-byte . "ECL_FFI_UNSIGNED_BYTE")
105 (:short . "ECL_FFI_SHORT")
106 (:unsigned-short . "ECL_FFI_UNSIGNED_SHORT")
107 (:int . "ECL_FFI_INT")
108 (:unsigned-int . "ECL_FFI_UNSIGNED_INT")
109 (:long . "ECL_FFI_LONG")
110 (:unsigned-long . "ECL_FFI_UNSIGNED_LONG")
111 #+:uint16-t (:int16-t . "ECL_FFI_INT16_T")
112 #+:uint16-t (:uint16-t . "ECL_FFI_UINT16_T")
113 #+:uint32-t (:int32-t . "ECL_FFI_INT32_T")
114 #+:uint32-t (:uint32-t . "ECL_FFI_UINT32_T")
115 #+:uint64-t (:int64-t . "ECL_FFI_INT64_T")
116 #+:uint64-t (:uint64-t . "ECL_FFI_UINT64_T")
117 #+:long-long (:long-long . "ECL_FFI_LONG_LONG")
118 #+:long-long (:unsigned-long-long . "ECL_FFI_UNSIGNED_LONG_LONG")
119 (:pointer-void . "ECL_FFI_POINTER_VOID")
120 (:cstring . "ECL_FFI_CSTRING")
121 (:object . "ECL_FFI_OBJECT")
122 (:float . "ECL_FFI_FLOAT")
123 (:double . "ECL_FFI_DOUBLE")
124 (:long-double . "ECL_FFI_LONG_DOUBLE")
125 #+complex-float (:csfloat . "ECL_FFI_CSFLOAT")
126 #+complex-float (:cdfloat . "ECL_FFI_CDFLOAT")
127 #+complex-float (:clfloat . "ECL_FFI_CLFLOAT")
128 (:void . "ECL_FFI_VOID")))
129
130 (defun foreign-elt-type-code (type)
131 (ext:if-let ((x (assoc type +foreign-elt-type-codes+)))
132 (cdr x)
133 (cmperr "DEFCALLBACK: ~a is not a valid elementary FFI type." type)))
134
135 ;;; We could have made FFI:DEFCALLBACK to accept any ffi type defined
136 ;;; for the current machine (see cmpc-machine.lisp), but it wouldn't
137 ;;; be useful because it only extends FFI types with ECL-specific
138 ;;; types like :fixnum or :sse2. Another argument against such
139 ;;; approach is semantic equivalence between interpreted and compiled
140 ;;; versions of the special form. -- jd 2019-11-27
141 (defun c1-defcallback (args)
142 (destructuring-bind (name return-type arg-list &rest body)
143 args
144 (cond ((eql return-type nil)
145 (setf return-type :void))
146 ((and (consp return-type)
147 (member (first return-type) '(* array)))
148 (setf return-type :pointer-void)))
149 (let ((arg-types '())
150 (arg-type-constants '())
151 (arg-variables '())
152 (c-name (format nil "ecl_callback_~d" (length *callbacks*)))
153 (name (if (consp name) (first name) name))
154 (call-type (if (consp name) (second name) :cdecl)))
155 (dolist (i arg-list)
156 (unless (consp i)
157 (cmperr "Syntax error in CALLBACK form: C type is missing in argument ~A "i))
158 (push (first i) arg-variables)
159 (let ((type (second i)))
160 (push type arg-types)
161 (push (foreign-elt-type-code type) arg-type-constants)))
162 (push (list name c-name (add-object name)
163 return-type
164 (foreign-elt-type-code return-type)
165 (reverse arg-types)
166 (reverse arg-type-constants)
167 call-type)
168 *callbacks*)
169 (c1expr
170 `(progn
171 (defun ,name ,(reverse arg-variables) ,@body)
172 (si:put-sysprop ',name :callback
173 (ffi:c-inline (:pointer-void) (:object) :object
174 ,(format nil "ecl_make_foreign_data(#0,0,(void*)~a)" c-name)
175 :one-liner t)))))))