d2a98d5bbfa8ea37f4efa9d1fa9ecf09f6319884
[sbcl.git] / src / compiler / aliencomp.lisp
1 ;;;; transforms and other stuff used to compile ALIEN operations
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!C")
13 \f
14 ;;;; DEFKNOWNs
15
16 (defknown %sap-alien (system-area-pointer alien-type) alien-value
17   (flushable movable))
18 (defknown alien-sap (alien-value) system-area-pointer
19   (flushable movable))
20
21 (defknown slot (alien-value symbol) t
22   (flushable recursive))
23 (defknown %set-slot (alien-value symbol t) t
24   (recursive))
25 (defknown %slot-addr (alien-value symbol) (alien (* t))
26   (flushable movable recursive))
27
28 (defknown deref (alien-value &rest index) t
29   (flushable))
30 (defknown %set-deref (alien-value t &rest index) t
31   ())
32 (defknown %deref-addr (alien-value &rest index) (alien (* t))
33   (flushable movable))
34
35 (defknown %heap-alien (heap-alien-info) t
36   (flushable))
37 (defknown %set-heap-alien (heap-alien-info t) t
38   ())
39 (defknown %heap-alien-addr (heap-alien-info) (alien (* t))
40   (flushable movable))
41
42 (defknown make-local-alien (local-alien-info) t
43   ())
44 (defknown note-local-alien-type (local-alien-info t) null
45   ())
46 (defknown local-alien (local-alien-info t) t
47   (flushable))
48 (defknown %local-alien-forced-to-memory-p (local-alien-info) (member t nil)
49   (movable))
50 (defknown %set-local-alien (local-alien-info t t) t
51   ())
52 (defknown %local-alien-addr (local-alien-info t) (alien (* t))
53   (flushable movable))
54 (defknown dispose-local-alien (local-alien-info t) t
55   ())
56
57 (defknown %cast (alien-value alien-type) alien
58   (flushable movable))
59
60 (defknown naturalize (t alien-type) alien
61   (flushable movable))
62 (defknown deport (alien alien-type) t
63   (flushable movable))
64 (defknown extract-alien-value (system-area-pointer index alien-type) t
65   (flushable))
66 (defknown deposit-alien-value (system-area-pointer index alien-type t) t
67   ())
68
69 (defknown alien-funcall (alien-value &rest *) *
70   (any recursive))
71 (defknown %alien-funcall (system-area-pointer alien-type &rest *) *)
72 \f
73 ;;;; cosmetic transforms
74
75 (deftransform slot ((object slot)
76                     ((alien (* t)) symbol))
77   '(slot (deref object) slot))
78
79 (deftransform %set-slot ((object slot value)
80                          ((alien (* t)) symbol t))
81   '(%set-slot (deref object) slot value))
82
83 (deftransform %slot-addr ((object slot)
84                           ((alien (* t)) symbol))
85   '(%slot-addr (deref object) slot))
86 \f
87 ;;;; SLOT support
88
89 (defun find-slot-offset-and-type (alien slot)
90   (unless (constant-continuation-p slot)
91     (give-up-ir1-transform
92      "The slot is not constant, so access cannot be open coded."))
93   (let ((type (continuation-type alien)))
94     (unless (alien-type-type-p type)
95       (give-up-ir1-transform))
96     (let ((alien-type (alien-type-type-alien-type type)))
97       (unless (alien-record-type-p alien-type)
98         (give-up-ir1-transform))
99       (let* ((slot-name (continuation-value slot))
100              (field (find slot-name (alien-record-type-fields alien-type)
101                           :key #'alien-record-field-name)))
102         (unless field
103           (abort-ir1-transform "~S doesn't have a slot named ~S"
104                                alien
105                                slot-name))
106         (values (alien-record-field-offset field)
107                 (alien-record-field-type field))))))
108
109 #+nil ;; Shouldn't be necessary.
110 (defoptimizer (slot derive-type) ((alien slot))
111   (block nil
112     (catch 'give-up-ir1-transform
113       (multiple-value-bind (slot-offset slot-type)
114           (find-slot-offset-and-type alien slot)
115         (declare (ignore slot-offset))
116         (return (make-alien-type-type slot-type))))
117     *wild-type*))
118
119 (deftransform slot ((alien slot) * * :important t)
120   (multiple-value-bind (slot-offset slot-type)
121       (find-slot-offset-and-type alien slot)
122     `(extract-alien-value (alien-sap alien)
123                           ,slot-offset
124                           ',slot-type)))
125
126 #+nil ;; ### But what about coercions?
127 (defoptimizer (%set-slot derive-type) ((alien slot value))
128   (block nil
129     (catch 'give-up-ir1-transform
130       (multiple-value-bind (slot-offset slot-type)
131           (find-slot-offset-and-type alien slot)
132         (declare (ignore slot-offset))
133         (let ((type (make-alien-type-type slot-type)))
134           (assert-continuation-type value type)
135           (return type))))
136     *wild-type*))
137
138 (deftransform %set-slot ((alien slot value) * * :important t)
139   (multiple-value-bind (slot-offset slot-type)
140       (find-slot-offset-and-type alien slot)
141     `(deposit-alien-value (alien-sap alien)
142                           ,slot-offset
143                           ',slot-type
144                           value)))
145
146 (defoptimizer (%slot-addr derive-type) ((alien slot))
147   (block nil
148     (catch 'give-up-ir1-transform
149       (multiple-value-bind (slot-offset slot-type)
150           (find-slot-offset-and-type alien slot)
151         (declare (ignore slot-offset))
152         (return (make-alien-type-type
153                  (make-alien-pointer-type :to slot-type)))))
154     *wild-type*))
155
156 (deftransform %slot-addr ((alien slot) * * :important t)
157   (multiple-value-bind (slot-offset slot-type)
158       (find-slot-offset-and-type alien slot)
159     (/noshow "in DEFTRANSFORM %SLOT-ADDR, creating %SAP-ALIEN")
160     `(%sap-alien (sap+ (alien-sap alien) (/ ,slot-offset sb!vm:n-byte-bits))
161                  ',(make-alien-pointer-type :to slot-type))))
162 \f
163 ;;;; DEREF support
164
165 (defun find-deref-alien-type (alien)
166   (let ((alien-type (continuation-type alien)))
167     (unless (alien-type-type-p alien-type)
168       (give-up-ir1-transform))
169     (let ((alien-type (alien-type-type-alien-type alien-type)))
170       (if (alien-type-p alien-type)
171           alien-type
172           (give-up-ir1-transform)))))
173
174 (defun find-deref-element-type (alien)
175   (let ((alien-type (find-deref-alien-type alien)))
176     (typecase alien-type
177       (alien-pointer-type
178        (alien-pointer-type-to alien-type))
179       (alien-array-type
180        (alien-array-type-element-type alien-type))
181       (t
182        (give-up-ir1-transform)))))
183
184 (defun compute-deref-guts (alien indices)
185   (let ((alien-type (find-deref-alien-type alien)))
186     (typecase alien-type
187       (alien-pointer-type
188        (when (cdr indices)
189          (abort-ir1-transform "too many indices for pointer deref: ~W"
190                               (length indices)))
191        (let ((element-type (alien-pointer-type-to alien-type)))
192          (if indices
193              (let ((bits (alien-type-bits element-type))
194                    (alignment (alien-type-alignment element-type)))
195                (unless bits
196                  (abort-ir1-transform "unknown element size"))
197                (unless alignment
198                  (abort-ir1-transform "unknown element alignment"))
199                (values '(offset)
200                        `(* offset
201                            ,(align-offset bits alignment))
202                        element-type))
203              (values nil 0 element-type))))
204       (alien-array-type
205        (let* ((element-type (alien-array-type-element-type alien-type))
206               (bits (alien-type-bits element-type))
207               (alignment (alien-type-alignment element-type))
208               (dims (alien-array-type-dimensions alien-type)))
209          (unless (= (length indices) (length dims))
210            (give-up-ir1-transform "incorrect number of indices"))
211          (unless bits
212            (give-up-ir1-transform "Element size is unknown."))
213          (unless alignment
214            (give-up-ir1-transform "Element alignment is unknown."))
215          (if (null dims)
216              (values nil 0 element-type)
217              (let* ((arg (gensym))
218                     (args (list arg))
219                     (offsetexpr arg))
220                (dolist (dim (cdr dims))
221                  (let ((arg (gensym)))
222                    (push arg args)
223                    (setf offsetexpr `(+ (* ,offsetexpr ,dim) ,arg))))
224                (values (reverse args)
225                        `(* ,offsetexpr
226                            ,(align-offset bits alignment))
227                        element-type)))))
228       (t
229        (abort-ir1-transform "~S not either a pointer or array type."
230                             alien-type)))))
231
232 #+nil ;; Shouldn't be necessary.
233 (defoptimizer (deref derive-type) ((alien &rest noise))
234   (declare (ignore noise))
235   (block nil
236     (catch 'give-up-ir1-transform
237       (return (make-alien-type-type (find-deref-element-type alien))))
238     *wild-type*))
239
240 (deftransform deref ((alien &rest indices) * * :important t)
241   (multiple-value-bind (indices-args offset-expr element-type)
242       (compute-deref-guts alien indices)
243     `(lambda (alien ,@indices-args)
244        (extract-alien-value (alien-sap alien)
245                             ,offset-expr
246                             ',element-type))))
247
248 #+nil ;; ### Again, the value might be coerced.
249 (defoptimizer (%set-deref derive-type) ((alien value &rest noise))
250   (declare (ignore noise))
251   (block nil
252     (catch 'give-up-ir1-transform
253       (let ((type (make-alien-type-type
254                    (make-alien-pointer-type
255                     :to (find-deref-element-type alien)))))
256         (assert-continuation-type value type)
257         (return type)))
258     *wild-type*))
259
260 (deftransform %set-deref ((alien value &rest indices) * * :important t)
261   (multiple-value-bind (indices-args offset-expr element-type)
262       (compute-deref-guts alien indices)
263     `(lambda (alien value ,@indices-args)
264        (deposit-alien-value (alien-sap alien)
265                             ,offset-expr
266                             ',element-type
267                             value))))
268
269 (defoptimizer (%deref-addr derive-type) ((alien &rest noise))
270   (declare (ignore noise))
271   (block nil
272     (catch 'give-up-ir1-transform
273       (return (make-alien-type-type
274                (make-alien-pointer-type
275                 :to (find-deref-element-type alien)))))
276     *wild-type*))
277
278 (deftransform %deref-addr ((alien &rest indices) * * :important t)
279   (multiple-value-bind (indices-args offset-expr element-type)
280       (compute-deref-guts alien indices)
281     (/noshow "in DEFTRANSFORM %DEREF-ADDR, creating (LAMBDA .. %SAP-ALIEN)")
282     `(lambda (alien ,@indices-args)
283        (%sap-alien (sap+ (alien-sap alien) (/ ,offset-expr sb!vm:n-byte-bits))
284                    ',(make-alien-pointer-type :to element-type)))))
285 \f
286 ;;;; support for aliens on the heap
287
288 (defun heap-alien-sap-and-type (info)
289   (unless (constant-continuation-p info)
290     (give-up-ir1-transform "info not constant; can't open code"))
291   (let ((info (continuation-value info)))
292     (values (heap-alien-info-sap-form info)
293             (heap-alien-info-type info))))
294
295 #+nil ; shouldn't be necessary
296 (defoptimizer (%heap-alien derive-type) ((info))
297   (block nil
298     (catch 'give-up
299       (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
300         (declare (ignore sap))
301         (return (make-alien-type-type type))))
302     *wild-type*))
303
304 (deftransform %heap-alien ((info) * * :important t)
305   (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
306     `(extract-alien-value ,sap 0 ',type)))
307
308 #+nil ;; ### Again, deposit value might change the type.
309 (defoptimizer (%set-heap-alien derive-type) ((info value))
310   (block nil
311     (catch 'give-up-ir1-transform
312       (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
313         (declare (ignore sap))
314         (let ((type (make-alien-type-type type)))
315           (assert-continuation-type value type)
316           (return type))))
317     *wild-type*))
318
319 (deftransform %set-heap-alien ((info value) (heap-alien-info *) * :important t)
320   (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
321     `(deposit-alien-value ,sap 0 ',type value)))
322
323 (defoptimizer (%heap-alien-addr derive-type) ((info))
324   (block nil
325     (catch 'give-up-ir1-transform
326       (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
327         (declare (ignore sap))
328         (return (make-alien-type-type (make-alien-pointer-type :to type)))))
329     *wild-type*))
330
331 (deftransform %heap-alien-addr ((info) * * :important t)
332   (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
333     (/noshow "in DEFTRANSFORM %HEAP-ALIEN-ADDR, creating %SAP-ALIEN")
334     `(%sap-alien ,sap ',type)))
335 \f
336 ;;;; support for local (stack or register) aliens
337
338 (deftransform make-local-alien ((info) * * :important t)
339   (unless (constant-continuation-p info)
340     (abort-ir1-transform "Local alien info isn't constant?"))
341   (let* ((info (continuation-value info))
342          (alien-type (local-alien-info-type info))
343          (bits (alien-type-bits alien-type)))
344     (unless bits
345       (abort-ir1-transform "unknown size: ~S" (unparse-alien-type alien-type)))
346     (/noshow "in DEFTRANSFORM MAKE-LOCAL-ALIEN" info)
347     (/noshow (local-alien-info-force-to-memory-p info))
348     (/noshow alien-type (unparse-alien-type alien-type) (alien-type-bits alien-type))
349     (if (local-alien-info-force-to-memory-p info)
350       #!+x86 `(truly-the system-area-pointer
351                          (%primitive alloc-alien-stack-space
352                                      ,(ceiling (alien-type-bits alien-type)
353                                                sb!vm:n-byte-bits)))
354       #!-x86 `(truly-the system-area-pointer
355                          (%primitive alloc-number-stack-space
356                                      ,(ceiling (alien-type-bits alien-type)
357                                                sb!vm:n-byte-bits)))
358       (let* ((alien-rep-type-spec (compute-alien-rep-type alien-type))
359              (alien-rep-type (specifier-type alien-rep-type-spec)))
360         (cond ((csubtypep (specifier-type 'system-area-pointer)
361                           alien-rep-type)
362                '(int-sap 0))
363               ((ctypep 0 alien-rep-type) 0)
364               ((ctypep 0.0f0 alien-rep-type) 0.0f0)
365               ((ctypep 0.0d0 alien-rep-type) 0.0d0)
366               (t
367                (compiler-error
368                 "Aliens of type ~S cannot be represented immediately."
369                 (unparse-alien-type alien-type))))))))
370
371 (deftransform note-local-alien-type ((info var) * * :important t)
372   ;; FIXME: This test and error occur about a zillion times. They
373   ;; could be factored into a function.
374   (unless (constant-continuation-p info)
375     (abort-ir1-transform "Local alien info isn't constant?"))
376   (let ((info (continuation-value info)))
377     (/noshow "in DEFTRANSFORM NOTE-LOCAL-ALIEN-TYPE" info)
378     (/noshow (local-alien-info-force-to-memory-p info))
379     (unless (local-alien-info-force-to-memory-p info)
380       (let ((var-node (continuation-use var)))
381         (/noshow var-node (ref-p var-node))
382         (when (ref-p var-node)
383           (propagate-to-refs (ref-leaf var-node)
384                              (specifier-type
385                               (compute-alien-rep-type
386                                (local-alien-info-type info))))))))
387   nil)
388
389 (deftransform local-alien ((info var) * * :important t)
390   (unless (constant-continuation-p info)
391     (abort-ir1-transform "Local alien info isn't constant?"))
392   (let* ((info (continuation-value info))
393          (alien-type (local-alien-info-type info)))
394     (/noshow "in DEFTRANSFORM LOCAL-ALIEN" info alien-type)
395     (/noshow (local-alien-info-force-to-memory-p info))
396     (if (local-alien-info-force-to-memory-p info)
397         `(extract-alien-value var 0 ',alien-type)
398         `(naturalize var ',alien-type))))
399
400 (deftransform %local-alien-forced-to-memory-p ((info) * * :important t)
401   (unless (constant-continuation-p info)
402     (abort-ir1-transform "Local alien info isn't constant?"))
403   (let ((info (continuation-value info)))
404     (local-alien-info-force-to-memory-p info)))
405
406 (deftransform %set-local-alien ((info var value) * * :important t)
407   (unless (constant-continuation-p info)
408     (abort-ir1-transform "Local alien info isn't constant?"))
409   (let* ((info (continuation-value info))
410          (alien-type (local-alien-info-type info)))
411     (if (local-alien-info-force-to-memory-p info)
412         `(deposit-alien-value var 0 ',alien-type value)
413         '(error "This should be eliminated as dead code."))))
414
415 (defoptimizer (%local-alien-addr derive-type) ((info var))
416   (if (constant-continuation-p info)
417       (let* ((info (continuation-value info))
418              (alien-type (local-alien-info-type info)))
419         (make-alien-type-type (make-alien-pointer-type :to alien-type)))
420       *wild-type*))
421
422 (deftransform %local-alien-addr ((info var) * * :important t)
423   (unless (constant-continuation-p info)
424     (abort-ir1-transform "Local alien info isn't constant?"))
425   (let* ((info (continuation-value info))
426          (alien-type (local-alien-info-type info)))
427     (/noshow "in DEFTRANSFORM %LOCAL-ALIEN-ADDR, creating %SAP-ALIEN")
428     (if (local-alien-info-force-to-memory-p info)
429         `(%sap-alien var ',(make-alien-pointer-type :to alien-type))
430         (error "This shouldn't happen."))))
431
432 (deftransform dispose-local-alien ((info var) * * :important t)
433   (unless (constant-continuation-p info)
434     (abort-ir1-transform "Local alien info isn't constant?"))
435   (let* ((info (continuation-value info))
436          (alien-type (local-alien-info-type info)))
437     (if (local-alien-info-force-to-memory-p info)
438       #!+x86 `(%primitive dealloc-alien-stack-space
439                           ,(ceiling (alien-type-bits alien-type)
440                                     sb!vm:n-byte-bits))
441       #!-x86 `(%primitive dealloc-number-stack-space
442                           ,(ceiling (alien-type-bits alien-type)
443                                     sb!vm:n-byte-bits))
444       nil)))
445 \f
446 ;;;; %CAST
447
448 (defoptimizer (%cast derive-type) ((alien type))
449   (or (when (constant-continuation-p type)
450         (let ((alien-type (continuation-value type)))
451           (when (alien-type-p alien-type)
452             (make-alien-type-type alien-type))))
453       *wild-type*))
454
455 (deftransform %cast ((alien target-type) * * :important t)
456   (unless (constant-continuation-p target-type)
457     (give-up-ir1-transform
458      "The alien type is not constant, so access cannot be open coded."))
459   (let ((target-type (continuation-value target-type)))
460     (cond ((or (alien-pointer-type-p target-type)
461                (alien-array-type-p target-type)
462                (alien-fun-type-p target-type))
463            `(naturalize (alien-sap alien) ',target-type))
464           (t
465            (abort-ir1-transform "cannot cast to alien type ~S" target-type)))))
466 \f
467 ;;;; ALIEN-SAP, %SAP-ALIEN, %ADDR, etc.
468
469 (deftransform alien-sap ((alien) * * :important t)
470   (let ((alien-node (continuation-use alien)))
471     (typecase alien-node
472       (combination
473        (extract-fun-args alien '%sap-alien 2)
474        '(lambda (sap type)
475           (declare (ignore type))
476           sap))
477       (t
478        (give-up-ir1-transform)))))
479
480 (defoptimizer (%sap-alien derive-type) ((sap type))
481   (declare (ignore sap))
482   (if (constant-continuation-p type)
483       (make-alien-type-type (continuation-value type))
484       *wild-type*))
485
486 (deftransform %sap-alien ((sap type) * * :important t)
487   (give-up-ir1-transform
488    ;; FIXME: The hardcoded newline here causes more-than-usually
489    ;; screwed-up formatting of the optimization note output.
490    "could not optimize away %SAP-ALIEN: forced to do runtime ~@
491     allocation of alien-value structure"))
492 \f
493 ;;;; NATURALIZE/DEPORT/EXTRACT/DEPOSIT magic
494
495 (flet ((%computed-lambda (compute-lambda type)
496          (declare (type function compute-lambda))
497          (unless (constant-continuation-p type)
498            (give-up-ir1-transform
499             "The type is not constant at compile time; can't open code."))
500          (handler-case
501              (let ((result (funcall compute-lambda (continuation-value type))))
502                (/noshow "in %COMPUTED-LAMBDA" (continuation-value type) result)
503                result)
504            (error (condition)
505                   (compiler-error "~A" condition)))))
506   (deftransform naturalize ((object type) * * :important t)
507     (%computed-lambda #'compute-naturalize-lambda type))
508   (deftransform deport ((alien type) * * :important t)
509     (%computed-lambda #'compute-deport-lambda type))
510   (deftransform extract-alien-value ((sap offset type) * * :important t)
511     (%computed-lambda #'compute-extract-lambda type))
512   (deftransform deposit-alien-value ((sap offset type value) * * :important t)
513     (%computed-lambda #'compute-deposit-lambda type)))
514 \f
515 ;;;; a hack to clean up divisions
516
517 (defun count-low-order-zeros (thing)
518   (typecase thing
519     (continuation
520      (if (constant-continuation-p thing)
521          (count-low-order-zeros (continuation-value thing))
522          (count-low-order-zeros (continuation-use thing))))
523     (combination
524      (case (continuation-fun-name (combination-fun thing))
525        ((+ -)
526         (let ((min most-positive-fixnum)
527               (itype (specifier-type 'integer)))
528           (dolist (arg (combination-args thing) min)
529             (if (csubtypep (continuation-type arg) itype)
530                 (setf min (min min (count-low-order-zeros arg)))
531                 (return 0)))))
532        (*
533         (let ((result 0)
534               (itype (specifier-type 'integer)))
535           (dolist (arg (combination-args thing) result)
536             (if (csubtypep (continuation-type arg) itype)
537                 (setf result (+ result (count-low-order-zeros arg)))
538                 (return 0)))))
539        (ash
540         (let ((args (combination-args thing)))
541           (if (= (length args) 2)
542               (let ((amount (second args)))
543                 (if (constant-continuation-p amount)
544                     (max (+ (count-low-order-zeros (first args))
545                             (continuation-value amount))
546                          0)
547                     0))
548               0)))
549        (t
550         0)))
551     (integer
552      (if (zerop thing)
553          most-positive-fixnum
554          (do ((result 0 (1+ result))
555               (num thing (ash num -1)))
556              ((logbitp 0 num) result))))
557     (t
558      0)))
559
560 (deftransform / ((numerator denominator) (integer integer))
561   (unless (constant-continuation-p denominator)
562     (give-up-ir1-transform))
563   (let* ((denominator (continuation-value denominator))
564          (bits (1- (integer-length denominator))))
565     (unless (= (ash 1 bits) denominator)
566       (give-up-ir1-transform))
567     (let ((alignment (count-low-order-zeros numerator)))
568       (unless (>= alignment bits)
569         (give-up-ir1-transform))
570       `(ash numerator ,(- bits)))))
571
572 (deftransform ash ((value amount))
573   (let ((value-node (continuation-use value)))
574     (unless (and (combination-p value-node)
575                  (eq (continuation-fun-name (combination-fun value-node))
576                      'ash))
577       (give-up-ir1-transform))
578     (let ((inside-args (combination-args value-node)))
579       (unless (= (length inside-args) 2)
580         (give-up-ir1-transform))
581       (let ((inside-amount (second inside-args)))
582         (unless (and (constant-continuation-p inside-amount)
583                      (not (minusp (continuation-value inside-amount))))
584           (give-up-ir1-transform)))))
585   (extract-fun-args value 'ash 2)
586   '(lambda (value amount1 amount2)
587      (ash value (+ amount1 amount2))))
588 \f
589 ;;;; ALIEN-FUNCALL support
590
591 (deftransform alien-funcall ((function &rest args)
592                              ((alien (* t)) &rest *) *
593                              :important t)
594   (let ((names (make-gensym-list (length args))))
595     (/noshow "entering first DEFTRANSFORM ALIEN-FUNCALL" function args)
596     `(lambda (function ,@names)
597        (alien-funcall (deref function) ,@names))))
598
599 (deftransform alien-funcall ((function &rest args) * * :important t)
600   (let ((type (continuation-type function)))
601     (unless (alien-type-type-p type)
602       (give-up-ir1-transform "can't tell function type at compile time"))
603     (/noshow "entering second DEFTRANSFORM ALIEN-FUNCALL" function)
604     (let ((alien-type (alien-type-type-alien-type type)))
605       (unless (alien-fun-type-p alien-type)
606         (give-up-ir1-transform))
607       (let ((arg-types (alien-fun-type-arg-types alien-type)))
608         (unless (= (length args) (length arg-types))
609           (abort-ir1-transform
610            "wrong number of arguments; expected ~W, got ~W"
611            (length arg-types)
612            (length args)))
613         (collect ((params) (deports))
614           (dolist (arg-type arg-types)
615             (let ((param (gensym)))
616               (params param)
617               (deports `(deport ,param ',arg-type))))
618           (let ((return-type (alien-fun-type-result-type alien-type))
619                 (body `(%alien-funcall (deport function ',alien-type)
620                                        ',alien-type
621                                        ,@(deports))))
622             (if (alien-values-type-p return-type)
623                 (collect ((temps) (results))
624                   (dolist (type (alien-values-type-values return-type))
625                     (let ((temp (gensym)))
626                       (temps temp)
627                       (results `(naturalize ,temp ',type))))
628                   (setf body
629                         `(multiple-value-bind ,(temps) ,body
630                            (values ,@(results)))))
631                 (setf body `(naturalize ,body ',return-type)))
632             (/noshow "returning from DEFTRANSFORM ALIEN-FUNCALL" (params) body)
633             `(lambda (function ,@(params))
634                ,body)))))))
635
636 (defoptimizer (%alien-funcall derive-type) ((function type &rest args))
637   (declare (ignore function args))
638   (unless (constant-continuation-p type)
639     (error "Something is broken."))
640   (let ((type (continuation-value type)))
641     (unless (alien-fun-type-p type)
642       (error "Something is broken."))
643     (specifier-type
644      (compute-alien-rep-type
645       (alien-fun-type-result-type type)))))
646
647 (defoptimizer (%alien-funcall ltn-annotate)
648               ((function type &rest args) node ltn-policy)
649   (setf (basic-combination-info node) :funny)
650   (setf (node-tail-p node) nil)
651   (annotate-ordinary-continuation function ltn-policy)
652   (dolist (arg args)
653     (annotate-ordinary-continuation arg ltn-policy)))
654
655 (defoptimizer (%alien-funcall ir2-convert)
656               ((function type &rest args) call block)
657   (let ((type (if (constant-continuation-p type)
658                   (continuation-value type)
659                   (error "Something is broken.")))
660         (cont (node-cont call))
661         (args args))
662     (multiple-value-bind (nsp stack-frame-size arg-tns result-tns)
663         (make-call-out-tns type)
664       (vop alloc-number-stack-space call block stack-frame-size nsp)
665       (dolist (tn arg-tns)
666         (let* ((arg (pop args))
667                (sc (tn-sc tn))
668                (scn (sc-number sc))
669                #!-x86 (temp-tn (make-representation-tn (tn-primitive-type tn)
670                                                        scn))
671                (move-arg-vops (svref (sc-move-arg-vops sc) scn)))
672           (aver arg)
673           (unless (= (length move-arg-vops) 1)
674             (error "no unique move-arg-vop for moves in SC ~S" (sc-name sc)))
675           #!+x86 (emit-move-arg-template call
676                                          block
677                                          (first move-arg-vops)
678                                          (continuation-tn call block arg)
679                                          nsp
680                                          tn)
681           #!-x86 (progn
682                    (emit-move call
683                               block
684                               (continuation-tn call block arg)
685                               temp-tn)
686                    (emit-move-arg-template call
687                                            block
688                                            (first move-arg-vops)
689                                            temp-tn
690                                            nsp
691                                            tn))))
692       (aver (null args))
693       (unless (listp result-tns)
694         (setf result-tns (list result-tns)))
695       (vop* call-out call block
696             ((continuation-tn call block function)
697              (reference-tn-list arg-tns nil))
698             ((reference-tn-list result-tns t)))
699       (vop dealloc-number-stack-space call block stack-frame-size)
700       (move-continuation-result call block result-tns cont))))