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