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