Initial revision
[sbcl.git] / src / code / target-alieneval.lisp
1 ;;;; This file contains parts of the ALIEN implementation that
2 ;;;; are not part of the compiler.
3
4 ;;;; This software is part of the SBCL system. See the README file for
5 ;;;; more information.
6 ;;;;
7 ;;;; This software is derived from the CMU CL system, which was
8 ;;;; written at Carnegie Mellon University and released into the
9 ;;;; public domain. The software is in the public domain and is
10 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
11 ;;;; files for more information.
12
13 (in-package "SB!ALIEN")
14
15 (file-comment
16   "$Header$")
17 \f
18 ;;;; alien variables
19
20 ;;; Make a string out of the symbol, converting all uppercase letters to
21 ;;; lower case and hyphens into underscores.
22 (eval-when (:compile-toplevel :load-toplevel :execute)
23   (defun guess-alien-name-from-lisp-name (lisp-name)
24     (declare (type symbol lisp-name))
25     (nsubstitute #\_ #\- (string-downcase (symbol-name lisp-name)))))
26
27 ;;; The opposite of GUESS-ALIEN-NAME-FROM-LISP-NAME. Make a symbol out
28 ;;; of the string, converting all lowercase letters to uppercase and
29 ;;; underscores into hyphens.
30 (eval-when (:compile-toplevel :load-toplevel :execute)
31   (defun guess-lisp-name-from-alien-name (alien-name)
32     (declare (type simple-string alien-name))
33     (intern (nsubstitute #\- #\_ (string-upcase alien-name)))))
34
35 ;;; Extract the Lisp and alien names from NAME. If only one is given,
36 ;;; guess the other.
37 (eval-when (:compile-toplevel :load-toplevel :execute)
38   (defun pick-lisp-and-alien-names (name)
39     (etypecase name
40       (string
41        (values (guess-lisp-name-from-alien-name name) name))
42       (symbol
43        (values name (guess-alien-name-from-lisp-name name)))
44       (list
45        (unless (proper-list-of-length-p name 2)
46          (error "badly formed alien name"))
47        (values (cadr name) (car name))))))
48
49 (defmacro def-alien-variable (name type &environment env)
50   #!+sb-doc
51   "Define NAME as an external alien variable of type TYPE. NAME should be
52    a list of a string holding the alien name and a symbol to use as the Lisp
53    name. If NAME is just a symbol or string, then the other name is guessed
54    from the one supplied."
55   (multiple-value-bind (lisp-name alien-name) (pick-lisp-and-alien-names name)
56     (with-auxiliary-alien-types env
57       (let ((alien-type (parse-alien-type type env)))
58         `(eval-when (:compile-toplevel :load-toplevel :execute)
59            ,@(when *new-auxiliary-types*
60                `((%def-auxiliary-alien-types ',*new-auxiliary-types*)))
61            (%def-alien-variable ',lisp-name
62                                 ',alien-name
63                                 ',alien-type))))))
64
65 ;;; Do the actual work of DEF-ALIEN-VARIABLE.
66 (eval-when (:compile-toplevel :load-toplevel :execute)
67   (defun %def-alien-variable (lisp-name alien-name type)
68     (setf (info :variable :kind lisp-name) :alien)
69     (setf (info :variable :where-from lisp-name) :defined)
70     (clear-info :variable :constant-value lisp-name)
71     (setf (info :variable :alien-info lisp-name)
72           (make-heap-alien-info :type type
73                                 :sap-form `(foreign-symbol-address
74                                             ',alien-name)))))
75
76 (defmacro extern-alien (name type &environment env)
77   #!+sb-doc
78   "Access the alien variable named NAME, assuming it is of type TYPE. This
79    is SETFable."
80   (let ((alien-name (etypecase name
81                       (symbol (guess-alien-name-from-lisp-name name))
82                       (string name))))
83     `(%heap-alien ',(make-heap-alien-info
84                      :type (parse-alien-type type env)
85                      :sap-form `(foreign-symbol-address ',alien-name)))))
86
87 (defmacro with-alien (bindings &body body &environment env)
88   #!+sb-doc
89   "Establish some local alien variables. Each BINDING is of the form:
90      VAR TYPE [ ALLOCATION ] [ INITIAL-VALUE | EXTERNAL-NAME ]
91    ALLOCATION should be one of:
92      :LOCAL (the default)
93        The alien is allocated on the stack, and has dynamic extent.
94      :STATIC
95        The alien is allocated on the heap, and has infinite extent. The alien
96        is allocated at load time, so the same piece of memory is used each time
97        this form executes.
98      :EXTERN
99        No alien is allocated, but VAR is established as a local name for
100        the external alien given by EXTERNAL-NAME."
101   (with-auxiliary-alien-types env
102     (dolist (binding (reverse bindings))
103       (destructuring-bind
104           (symbol type &optional (opt1 nil opt1p) (opt2 nil opt2p))
105           binding
106         (let ((alien-type (parse-alien-type type env)))
107           (multiple-value-bind (allocation initial-value)
108               (if opt2p
109                   (values opt1 opt2)
110                   (case opt1
111                     (:extern
112                      (values opt1 (guess-alien-name-from-lisp-name symbol)))
113                     (:static
114                      (values opt1 nil))
115                     (t
116                      (values :local opt1))))
117             (setf body
118                   (ecase allocation
119                     #+nil
120                     (:static
121                      (let ((sap
122                             (make-symbol (concatenate 'string "SAP-FOR-"
123                                                       (symbol-name symbol)))))
124                        `((let ((,sap (load-time-value (%make-alien ...))))
125                            (declare (type system-area-pointer ,sap))
126                            (symbol-macrolet
127                             ((,symbol (sap-alien ,sap ,type)))
128                             ,@(when initial-value
129                                 `((setq ,symbol ,initial-value)))
130                             ,@body)))))
131                     (:extern
132                      (let ((info (make-heap-alien-info
133                                   :type alien-type
134                                   :sap-form `(foreign-symbol-address
135                                               ',initial-value))))
136                        `((symbol-macrolet
137                           ((,symbol (%heap-alien ',info)))
138                           ,@body))))
139                     (:local
140                      (let ((var (gensym))
141                            (initval (if initial-value (gensym)))
142                            (info (make-local-alien-info :type alien-type)))
143                        `((let ((,var (make-local-alien ',info))
144                                ,@(when initial-value
145                                    `((,initval ,initial-value))))
146                            (note-local-alien-type ',info ,var)
147                            (multiple-value-prog1
148                                (symbol-macrolet
149                                 ((,symbol (local-alien ',info ,var)))
150                                 ,@(when initial-value
151                                     `((setq ,symbol ,initval)))
152                                 ,@body)
153                                (dispose-local-alien ',info ,var))))))))))))
154     (verify-local-auxiliaries-okay)
155     `(symbol-macrolet ((&auxiliary-type-definitions&
156                         ,(append *new-auxiliary-types*
157                                  (auxiliary-type-definitions env))))
158        ,@body)))
159 \f
160 ;;;; runtime C values that don't correspond directly to Lisp types
161
162 ;;; ALIEN-VALUE
163 ;;;
164 ;;; Note: The DEFSTRUCT for ALIEN-VALUE lives in a separate file
165 ;;; 'cause it has to be real early in the cold-load order.
166 #!-sb-fluid (declaim (freeze-type alien-value))
167 (def!method print-object ((value alien-value) stream)
168   (print-unreadable-object (value stream)
169     (format stream
170             "~S :SAP #X~8,'0X"
171             'alien-value
172             (sap-int (alien-value-sap value)))))
173
174 #!-sb-fluid (declaim (inline null-alien))
175 (defun null-alien (x)
176   #!+sb-doc
177   "Return true if X (which must be an ALIEN pointer) is null, false otherwise."
178   (zerop (sap-int (alien-sap x))))
179
180 (defmacro sap-alien (sap type &environment env)
181   #!+sb-doc
182   "Convert the system area pointer SAP to an ALIEN of the specified TYPE (not
183    evaluated.) TYPE must be pointer-like."
184   (let ((alien-type (parse-alien-type type env)))
185     (if (eq (compute-alien-rep-type alien-type) 'system-area-pointer)
186         `(%sap-alien ,sap ',alien-type)
187         (error "cannot make aliens of type ~S out of SAPs" type))))
188
189 (defun %sap-alien (sap type)
190   (declare (type system-area-pointer sap)
191            (type alien-type type))
192   (make-alien-value :sap sap :type type))
193
194 (defun alien-sap (alien)
195   #!+sb-doc
196   "Return a System-Area-Pointer pointing to Alien's data."
197   (declare (type alien-value alien))
198   (alien-value-sap alien))
199 \f
200 ;;;; allocation/deallocation of heap aliens
201
202 (defmacro make-alien (type &optional size &environment env)
203   #!+sb-doc
204   "Allocate an alien of type TYPE and return an alien pointer to it. If SIZE
205    is supplied, how it is interpreted depends on TYPE. If TYPE is an array
206    type, SIZE is used as the first dimension for the allocated array. If TYPE
207    is not an array, then SIZE is the number of elements to allocate. The
208    memory is allocated using ``malloc'', so it can be passed to foreign
209    functions which use ``free''."
210   (let ((alien-type (if (alien-type-p type)
211                         type
212                         (parse-alien-type type env))))
213     (multiple-value-bind (size-expr element-type)
214         (if (alien-array-type-p alien-type)
215             (let ((dims (alien-array-type-dimensions alien-type)))
216               (cond
217                (size
218                 (unless dims
219                   (error
220                    "cannot override the size of zero-dimensional arrays"))
221                 (when (constantp size)
222                   (setf alien-type (copy-alien-array-type alien-type))
223                   (setf (alien-array-type-dimensions alien-type)
224                         (cons (eval size) (cdr dims)))))
225                (dims
226                 (setf size (car dims)))
227                (t
228                 (setf size 1)))
229               (values `(* ,size ,@(cdr dims))
230                       (alien-array-type-element-type alien-type)))
231             (values (or size 1) alien-type))
232       (let ((bits (alien-type-bits element-type))
233             (alignment (alien-type-alignment element-type)))
234         (unless bits
235           (error "The size of ~S is unknown."
236                  (unparse-alien-type element-type)))
237         (unless alignment
238           (error "The alignment of ~S is unknown."
239                  (unparse-alien-type element-type)))
240         `(%sap-alien (%make-alien (* ,(align-offset bits alignment)
241                                      ,size-expr))
242                      ',(make-alien-pointer-type :to alien-type))))))
243
244 ;;; Allocate a block of memory at least BITS bits long and return a
245 ;;; system area pointer to it.
246 #!-sb-fluid (declaim (inline %make-alien))
247 (defun %make-alien (bits)
248   (declare (type sb!kernel:index bits) (optimize-interface (safety 2)))
249   (alien-funcall (extern-alien "malloc" (function system-area-pointer unsigned))
250                  (ash (the sb!kernel:index (+ bits 7)) -3)))
251
252 #!-sb-fluid (declaim (inline free-alien))
253 (defun free-alien (alien)
254   #!+sb-doc
255   "Dispose of the storage pointed to by ALIEN. ALIEN must have been allocated
256    by MAKE-ALIEN or ``malloc''."
257   (alien-funcall (extern-alien "free" (function (values) system-area-pointer))
258                  (alien-sap alien))
259   nil)
260 \f
261 ;;;; the SLOT operator
262
263 ;;; Find the field named SLOT, or die trying.
264 (defun slot-or-lose (type slot)
265   (declare (type alien-record-type type)
266            (type symbol slot))
267   (or (find slot (alien-record-type-fields type)
268             :key #'alien-record-field-name)
269       (error "There is no slot named ~S in ~S" slot type)))
270
271 ;;; Extract the value from the named slot from the record ALIEN. If
272 ;;; ALIEN is actually a pointer, then DEREF it first.
273 (defun slot (alien slot)
274   #!+sb-doc
275   "Extract SLOT from the Alien STRUCT or UNION ALIEN. May be set with SETF."
276   (declare (type alien-value alien)
277            (type symbol slot)
278            (optimize (inhibit-warnings 3)))
279   (let ((type (alien-value-type alien)))
280     (etypecase type
281       (alien-pointer-type
282        (slot (deref alien) slot))
283       (alien-record-type
284        (let ((field (slot-or-lose type slot)))
285          (extract-alien-value (alien-value-sap alien)
286                               (alien-record-field-offset field)
287                               (alien-record-field-type field)))))))
288
289 ;;; Deposit the value in the specified slot of the record ALIEN. If
290 ;;; the ALIEN is really a pointer, DEREF it first. The compiler uses
291 ;;; this when it can't figure out anything better.
292 (defun %set-slot (alien slot value)
293   (declare (type alien-value alien)
294            (type symbol slot)
295            (optimize (inhibit-warnings 3)))
296   (let ((type (alien-value-type alien)))
297     (etypecase type
298       (alien-pointer-type
299        (%set-slot (deref alien) slot value))
300       (alien-record-type
301        (let ((field (slot-or-lose type slot)))
302          (deposit-alien-value (alien-value-sap alien)
303                               (alien-record-field-offset field)
304                               (alien-record-field-type field)
305                               value))))))
306
307 ;;; Compute the address of the specified slot and return a pointer to it.
308 (defun %slot-addr (alien slot)
309   (declare (type alien-value alien)
310            (type symbol slot)
311            (optimize (inhibit-warnings 3)))
312   (let ((type (alien-value-type alien)))
313     (etypecase type
314       (alien-pointer-type
315        (%slot-addr (deref alien) slot))
316       (alien-record-type
317        (let* ((field (slot-or-lose type slot))
318               (offset (alien-record-field-offset field))
319               (field-type (alien-record-field-type field)))
320          (%sap-alien (sap+ (alien-sap alien) (/ offset sb!vm:byte-bits))
321                      (make-alien-pointer-type :to field-type)))))))
322 \f
323 ;;;; the DEREF operator
324
325 ;;; Does most of the work of the different DEREF methods. Returns two values:
326 ;;; the type and the offset (in bits) of the refered to alien.
327 (defun deref-guts (alien indices)
328   (declare (type alien-value alien)
329            (type list indices)
330            (values alien-type integer))
331   (let ((type (alien-value-type alien)))
332     (etypecase type
333       (alien-pointer-type
334        (when (cdr indices)
335          (error "too many indices when derefing ~S: ~D"
336                 type
337                 (length indices)))
338        (let ((element-type (alien-pointer-type-to type)))
339          (values element-type
340                  (if indices
341                      (* (align-offset (alien-type-bits element-type)
342                                       (alien-type-alignment element-type))
343                         (car indices))
344                      0))))
345       (alien-array-type
346        (unless (= (length indices) (length (alien-array-type-dimensions type)))
347          (error "incorrect number of indices when derefing ~S: ~D"
348                 type (length indices)))
349        (labels ((frob (dims indices offset)
350                   (if (null dims)
351                       offset
352                       (frob (cdr dims) (cdr indices)
353                         (+ (if (zerop offset)
354                                0
355                                (* offset (car dims)))
356                            (car indices))))))
357          (let ((element-type (alien-array-type-element-type type)))
358            (values element-type
359                    (* (align-offset (alien-type-bits element-type)
360                                     (alien-type-alignment element-type))
361                       (frob (alien-array-type-dimensions type)
362                         indices 0)))))))))
363
364 ;;; Dereference the alien and return the results.
365 (defun deref (alien &rest indices)
366   #!+sb-doc
367   "De-reference an Alien pointer or array. If an array, the indices are used
368    as the indices of the array element to access. If a pointer, one index can
369    optionally be specified, giving the equivalent of C pointer arithmetic."
370   (declare (type alien-value alien)
371            (type list indices)
372            (optimize (inhibit-warnings 3)))
373   (multiple-value-bind (target-type offset) (deref-guts alien indices)
374     (extract-alien-value (alien-value-sap alien)
375                          offset
376                          target-type)))
377
378 (defun %set-deref (alien value &rest indices)
379   (declare (type alien-value alien)
380            (type list indices)
381            (optimize (inhibit-warnings 3)))
382   (multiple-value-bind (target-type offset) (deref-guts alien indices)
383     (deposit-alien-value (alien-value-sap alien)
384                          offset
385                          target-type
386                          value)))
387
388 (defun %deref-addr (alien &rest indices)
389   (declare (type alien-value alien)
390            (type list indices)
391            (optimize (inhibit-warnings 3)))
392   (multiple-value-bind (target-type offset) (deref-guts alien indices)
393     (%sap-alien (sap+ (alien-value-sap alien) (/ offset sb!vm:byte-bits))
394                 (make-alien-pointer-type :to target-type))))
395 \f
396 ;;;; accessing heap alien variables
397
398 (defun %heap-alien (info)
399   (declare (type heap-alien-info info)
400            (optimize (inhibit-warnings 3)))
401   (extract-alien-value (eval (heap-alien-info-sap-form info))
402                        0
403                        (heap-alien-info-type info)))
404
405 (defun %set-heap-alien (info value)
406   (declare (type heap-alien-info info)
407            (optimize (inhibit-warnings 3)))
408   (deposit-alien-value (eval (heap-alien-info-sap-form info))
409                        0
410                        (heap-alien-info-type info)
411                        value))
412
413 (defun %heap-alien-addr (info)
414   (declare (type heap-alien-info info)
415            (optimize (inhibit-warnings 3)))
416   (%sap-alien (eval (heap-alien-info-sap-form info))
417               (make-alien-pointer-type :to (heap-alien-info-type info))))
418 \f
419 ;;;; accessing local aliens
420
421 (defun make-local-alien (info)
422   (let* ((alien (eval `(make-alien ,(local-alien-info-type info))))
423          (alien-sap (alien-sap alien)))
424     (finalize
425      alien
426      #'(lambda ()
427          (alien-funcall
428           (extern-alien "free" (function (values) system-area-pointer))
429           alien-sap)))
430     alien))
431
432 (defun note-local-alien-type (info alien)
433   (declare (ignore info alien))
434   nil)
435
436 (defun local-alien (info alien)
437   (declare (ignore info))
438   (deref alien))
439
440 (defun %set-local-alien (info alien value)
441   (declare (ignore info))
442   (setf (deref alien) value))
443
444 (define-setf-expander local-alien (&whole whole info alien)
445   (let ((value (gensym))
446         (info (if (and (consp info)
447                        (eq (car info) 'quote))
448                   (second info)
449                   (error "Something is wrong; local-alien-info not found: ~S"
450                          whole))))
451     (values nil
452             nil
453             (list value)
454             (if sb!c:*converting-for-interpreter*
455                 `(%set-local-alien ',info ,alien ,value)
456                 `(if (%local-alien-forced-to-memory-p ',info)
457                      (%set-local-alien ',info ,alien ,value)
458                      (setf ,alien
459                            (deport ,value ',(local-alien-info-type info)))))
460             whole)))
461
462 (defun %local-alien-forced-to-memory-p (info)
463   (local-alien-info-force-to-memory-p info))
464
465 (defun %local-alien-addr (info alien)
466   (declare (type local-alien-info info))
467   (unless (local-alien-info-force-to-memory-p info)
468     (error "~S isn't forced to memory. Something went wrong." alien))
469   alien)
470
471 (defun dispose-local-alien (info alien)
472   (declare (ignore info))
473   (cancel-finalization alien)
474   (free-alien alien))
475 \f
476 ;;;; the CAST macro
477
478 (defmacro cast (alien type &environment env)
479   #!+sb-doc
480   "Convert ALIEN to an Alien of the specified TYPE (not evaluated.)  Both types
481    must be Alien array, pointer or function types."
482   `(%cast ,alien ',(parse-alien-type type env)))
483
484 (defun %cast (alien target-type)
485   (declare (type alien-value alien)
486            (type alien-type target-type)
487            (optimize-interface (safety 2))
488            (optimize (inhibit-warnings 3)))
489   (if (or (alien-pointer-type-p target-type)
490           (alien-array-type-p target-type)
491           (alien-function-type-p target-type))
492       (let ((alien-type (alien-value-type alien)))
493         (if (or (alien-pointer-type-p alien-type)
494                 (alien-array-type-p alien-type)
495                 (alien-function-type-p alien-type))
496             (naturalize (alien-value-sap alien) target-type)
497             (error "~S cannot be casted." alien)))
498       (error "cannot cast to alien type ~S" (unparse-alien-type target-type))))
499 \f
500 ;;;; the ALIEN-SIZE macro
501
502 (defmacro alien-size (type &optional (units :bits) &environment env)
503   #!+sb-doc
504   "Return the size of the alien type TYPE. UNITS specifies the units to
505    use and can be either :BITS, :BYTES, or :WORDS."
506   (let* ((alien-type (parse-alien-type type env))
507          (bits (alien-type-bits alien-type)))
508     (if bits
509         (values (ceiling bits
510                          (ecase units
511                            (:bits 1)
512                            (:bytes sb!vm:byte-bits)
513                            (:words sb!vm:word-bits))))
514         (error "unknown size for alien type ~S"
515                (unparse-alien-type alien-type)))))
516 \f
517 ;;;; NATURALIZE, DEPORT, EXTRACT-ALIEN-VALUE, DEPOSIT-ALIEN-VALUE
518
519 (defun naturalize (alien type)
520   (declare (type alien-type type))
521   (funcall (coerce (compute-naturalize-lambda type) 'function)
522            alien type))
523
524 (defun deport (value type)
525   (declare (type alien-type type))
526   (funcall (coerce (compute-deport-lambda type) 'function)
527            value type))
528
529 (defun extract-alien-value (sap offset type)
530   (declare (type system-area-pointer sap)
531            (type unsigned-byte offset)
532            (type alien-type type))
533   (funcall (coerce (compute-extract-lambda type) 'function)
534            sap offset type))
535
536 (defun deposit-alien-value (sap offset type value)
537   (declare (type system-area-pointer sap)
538            (type unsigned-byte offset)
539            (type alien-type type))
540   (funcall (coerce (compute-deposit-lambda type) 'function)
541            sap offset type value))
542 \f
543 ;;;; ALIEN-FUNCALL, DEF-ALIEN-ROUTINE
544
545 (defun alien-funcall (alien &rest args)
546   #!+sb-doc
547   "Call the foreign function ALIEN with the specified arguments. ALIEN's
548    type specifies the argument and result types."
549   (declare (type alien-value alien))
550   (let ((type (alien-value-type alien)))
551     (typecase type
552       (alien-pointer-type
553        (apply #'alien-funcall (deref alien) args))
554       (alien-function-type
555        (unless (= (length (alien-function-type-arg-types type))
556                   (length args))
557          (error "wrong number of arguments for ~S~%expected ~D, got ~D"
558                 type
559                 (length (alien-function-type-arg-types type))
560                 (length args)))
561        (let ((stub (alien-function-type-stub type)))
562          (unless stub
563            (setf stub
564                  (let ((fun (gensym))
565                        (parms (loop repeat (length args) collect (gensym))))
566                    (compile nil
567                             `(lambda (,fun ,@parms)
568                                (declare (type (alien ,type) ,fun))
569                                (alien-funcall ,fun ,@parms)))))
570            (setf (alien-function-type-stub type) stub))
571          (apply stub alien args)))
572       (t
573        (error "~S is not an alien function." alien)))))
574
575 (defmacro def-alien-routine (name result-type &rest args &environment env)
576   #!+sb-doc
577   "Def-C-Routine Name Result-Type
578                     {(Arg-Name Arg-Type [Style])}*
579
580   Define a foreign interface function for the routine with the specified Name,
581   which may be either a string, symbol or list of the form (string symbol).
582   Return-Type is the Alien type for the function return value. VOID may be
583   used to specify a function with no result.
584
585   The remaining forms specifiy individual arguments that are passed to the
586   routine. Arg-Name is a symbol that names the argument, primarily for
587   documentation. Arg-Type is the C-Type of the argument. Style specifies the
588   say that the argument is passed.
589
590   :IN
591         An :In argument is simply passed by value. The value to be passed is
592         obtained from argument(s) to the interface function. No values are
593         returned for :In arguments. This is the default mode.
594
595   :OUT
596         The specified argument type must be a pointer to a fixed sized object.
597         A pointer to a preallocated object is passed to the routine, and the
598         the object is accessed on return, with the value being returned from
599         the interface function. :OUT and :IN-OUT cannot be used with pointers
600         to arrays, records or functions.
601
602   :COPY
603         Similar to :IN, except that the argument values are stored in on
604         the stack, and a pointer to the object is passed instead of
605         the values themselves.
606
607   :IN-OUT
608         A combination of :OUT and :COPY. A pointer to the argument is passed,
609         with the object being initialized from the supplied argument and
610         the return value being determined by accessing the object on return."
611   (multiple-value-bind (lisp-name alien-name)
612       (pick-lisp-and-alien-names name)
613     (collect ((docs) (lisp-args) (arg-types) (alien-vars)
614               (alien-args) (results))
615       (dolist (arg args)
616         (if (stringp arg)
617             (docs arg)
618             (destructuring-bind (name type &optional (style :in)) arg
619               (unless (member style '(:in :copy :out :in-out))
620                 (error "bogus argument style ~S in ~S" style arg))
621               (unless (eq style :out)
622                 (lisp-args name))
623               (when (and (member style '(:out :in-out))
624                          (typep (parse-alien-type type env)
625                                 'alien-pointer-type))
626                 (error "can't use :OUT or :IN-OUT on pointer-like type:~%  ~S"
627                        type))
628               (cond ((eq style :in)
629                      (arg-types type)
630                      (alien-args name))
631                     (t
632                      (arg-types `(* ,type))
633                      (if (eq style :out)
634                          (alien-vars `(,name ,type))
635                          (alien-vars `(,name ,type ,name)))
636                      (alien-args `(addr ,name))))
637               (when (or (eq style :out) (eq style :in-out))
638                 (results name)))))
639       `(defun ,lisp-name ,(lisp-args)
640          ,@(docs)
641          (with-alien
642              ((,lisp-name (function ,result-type ,@(arg-types))
643                           :extern ,alien-name)
644               ,@(alien-vars))
645              ,(if (alien-values-type-p result-type)
646                   (let ((temps (loop
647                                  repeat (length (alien-values-type-values
648                                                  result-type))
649                                  collect (gensym))))
650                     `(multiple-value-bind ,temps
651                          (alien-funcall ,lisp-name ,@(alien-args))
652                        (values ,@temps ,@(results))))
653                   `(values (alien-funcall ,lisp-name ,@(alien-args))
654                            ,@(results))))))))
655 \f
656 (defun alien-typep (object type)
657   #!+sb-doc
658   "Return T iff OBJECT is an alien of type TYPE."
659   (let ((lisp-rep-type (compute-lisp-rep-type type)))
660     (if lisp-rep-type
661         (typep object lisp-rep-type)
662         (and (alien-value-p object)
663              (alien-subtype-p (alien-value-type object) type)))))