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