1 ;;;; transforms and other stuff used to compile ALIEN operations
3 ;;;; This software is part of the SBCL system. See the README file for
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.
16 (defknown %sap-alien (system-area-pointer alien-type) alien-value
18 (defknown alien-sap (alien-value) system-area-pointer
21 (defknown slot (alien-value symbol) t
22 (flushable recursive))
23 (defknown %set-slot (alien-value symbol t) t
25 (defknown %slot-addr (alien-value symbol) (alien (* t))
26 (flushable movable recursive))
28 (defknown deref (alien-value &rest index) t
30 (defknown %set-deref (alien-value t &rest index) t
32 (defknown %deref-addr (alien-value &rest index) (alien (* t))
35 (defknown %heap-alien (heap-alien-info) t
37 (defknown %set-heap-alien (heap-alien-info t) t
39 (defknown %heap-alien-addr (heap-alien-info) (alien (* t))
42 (defknown make-local-alien (local-alien-info) t
44 (defknown note-local-alien-type (local-alien-info t) null
46 (defknown local-alien (local-alien-info t) t
48 (defknown %local-alien-forced-to-memory-p (local-alien-info) (member t nil)
50 (defknown %set-local-alien (local-alien-info t t) t
52 (defknown %local-alien-addr (local-alien-info t) (alien (* t))
54 (defknown dispose-local-alien (local-alien-info t) t
57 (defknown %cast (alien-value alien-type) alien
60 (defknown naturalize (t alien-type) alien
62 (defknown deport (alien alien-type) t
64 (defknown extract-alien-value (system-area-pointer index alien-type) t
66 (defknown deposit-alien-value (system-area-pointer index alien-type t) t
69 (defknown alien-funcall (alien-value &rest *) *
71 (defknown %alien-funcall (system-area-pointer alien-type &rest *) *)
73 ;;;; cosmetic transforms
75 (deftransform slot ((object slot)
76 ((alien (* t)) symbol))
77 '(slot (deref object) slot))
79 (deftransform %set-slot ((object slot value)
80 ((alien (* t)) symbol t))
81 '(%set-slot (deref object) slot value))
83 (deftransform %slot-addr ((object slot)
84 ((alien (* t)) symbol))
85 '(%slot-addr (deref object) slot))
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)))
103 (abort-ir1-transform "~S doesn't have a slot named ~S"
106 (values (alien-record-field-offset field)
107 (alien-record-field-type field))))))
109 #+nil ;; Shouldn't be necessary.
110 (defoptimizer (slot derive-type) ((alien slot))
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))))
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)
126 #+nil ;; ### But what about coercions?
127 (defoptimizer (%set-slot derive-type) ((alien slot value))
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)
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)
146 (defoptimizer (%slot-addr derive-type) ((alien slot))
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)))))
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:byte-bits))
161 ',(make-alien-pointer-type :to slot-type))))
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)
172 (give-up-ir1-transform)))))
174 (defun find-deref-element-type (alien)
175 (let ((alien-type (find-deref-alien-type alien)))
178 (alien-pointer-type-to alien-type))
180 (alien-array-type-element-type alien-type))
182 (give-up-ir1-transform)))))
184 (defun compute-deref-guts (alien indices)
185 (let ((alien-type (find-deref-alien-type alien)))
189 (abort-ir1-transform "too many indices for pointer deref: ~D"
191 (let ((element-type (alien-pointer-type-to alien-type)))
193 (let ((bits (alien-type-bits element-type))
194 (alignment (alien-type-alignment element-type)))
196 (abort-ir1-transform "unknown element size"))
198 (abort-ir1-transform "unknown element alignment"))
201 ,(align-offset bits alignment))
203 (values nil 0 element-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"))
212 (give-up-ir1-transform "Element size is unknown."))
214 (give-up-ir1-transform "Element alignment is unknown."))
216 (values nil 0 element-type)
217 (let* ((arg (gensym))
220 (dolist (dim (cdr dims))
221 (let ((arg (gensym)))
223 (setf offsetexpr `(+ (* ,offsetexpr ,dim) ,arg))))
224 (values (reverse args)
226 ,(align-offset bits alignment))
229 (abort-ir1-transform "~S not either a pointer or array type."
232 #+nil ;; Shouldn't be necessary.
233 (defoptimizer (deref derive-type) ((alien &rest noise))
234 (declare (ignore noise))
236 (catch 'give-up-ir1-transform
237 (return (make-alien-type-type (find-deref-element-type alien))))
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)
248 #+nil ;; ### Again, the value might be coerced.
249 (defoptimizer (%set-deref derive-type) ((alien value &rest noise))
250 (declare (ignore noise))
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)
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)
269 (defoptimizer (%deref-addr derive-type) ((alien &rest noise))
270 (declare (ignore noise))
272 (catch 'give-up-ir1-transform
273 (return (make-alien-type-type
274 (make-alien-pointer-type
275 :to (find-deref-element-type alien)))))
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:byte-bits))
284 ',(make-alien-pointer-type :to element-type)))))
286 ;;;; support for aliens on the heap
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))))
295 #+nil ; shouldn't be necessary
296 (defoptimizer (%heap-alien derive-type) ((info))
299 (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
300 (declare (ignore sap))
301 (return (make-alien-type-type type))))
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)))
308 #+nil ;; ### Again, deposit value might change the type.
309 (defoptimizer (%set-heap-alien derive-type) ((info value))
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)
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)))
323 (defoptimizer (%heap-alien-addr derive-type) ((info))
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)))))
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)))
336 ;;;; support for local (stack or register) aliens
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)))
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)
354 #!-x86 `(truly-the system-area-pointer
355 (%primitive alloc-number-stack-space
356 ,(ceiling (alien-type-bits alien-type)
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)
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)
368 "Aliens of type ~S cannot be represented immediately."
369 (unparse-alien-type alien-type))))))))
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)
385 (compute-alien-rep-type
386 (local-alien-info-type info))))))))
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))))
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)))
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."))))
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)))
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."))))
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)
441 #!-x86 `(%primitive dealloc-number-stack-space
442 ,(ceiling (alien-type-bits alien-type)
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))))
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-function-type-p target-type))
463 `(naturalize (alien-sap alien) ',target-type))
465 (abort-ir1-transform "cannot cast to alien type ~S" target-type)))))
467 ;;;; ALIEN-SAP, %SAP-ALIEN, %ADDR, etc.
469 (deftransform alien-sap ((alien) * * :important t)
470 (let ((alien-node (continuation-use alien)))
473 (extract-function-args alien '%sap-alien 2)
475 (declare (ignore type))
478 (give-up-ir1-transform)))))
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))
486 (deftransform %sap-alien ((sap type) * * :important t)
487 (give-up-ir1-transform
488 "could not optimize away %SAP-ALIEN: forced to do runtime ~@
489 allocation of alien-value structure"))
491 ;;;; NATURALIZE/DEPORT/EXTRACT/DEPOSIT magic
493 (flet ((%computed-lambda (compute-lambda type)
494 (declare (type function compute-lambda))
495 (unless (constant-continuation-p type)
496 (give-up-ir1-transform
497 "The type is not constant at compile time; can't open code."))
499 (let ((result (funcall compute-lambda (continuation-value type))))
500 (/noshow "in %COMPUTED-LAMBDA" (continuation-value type) result)
503 (compiler-error "~A" condition)))))
504 (deftransform naturalize ((object type) * * :important t)
505 (%computed-lambda #'compute-naturalize-lambda type))
506 (deftransform deport ((alien type) * * :important t)
507 (%computed-lambda #'compute-deport-lambda type))
508 (deftransform extract-alien-value ((sap offset type) * * :important t)
509 (%computed-lambda #'compute-extract-lambda type))
510 (deftransform deposit-alien-value ((sap offset type value) * * :important t)
511 (%computed-lambda #'compute-deposit-lambda type)))
513 ;;;; a hack to clean up divisions
515 (defun count-low-order-zeros (thing)
518 (if (constant-continuation-p thing)
519 (count-low-order-zeros (continuation-value thing))
520 (count-low-order-zeros (continuation-use thing))))
522 (case (continuation-function-name (combination-fun thing))
524 (let ((min most-positive-fixnum)
525 (itype (specifier-type 'integer)))
526 (dolist (arg (combination-args thing) min)
527 (if (csubtypep (continuation-type arg) itype)
528 (setf min (min min (count-low-order-zeros arg)))
532 (itype (specifier-type 'integer)))
533 (dolist (arg (combination-args thing) result)
534 (if (csubtypep (continuation-type arg) itype)
535 (setf result (+ result (count-low-order-zeros arg)))
538 (let ((args (combination-args thing)))
539 (if (= (length args) 2)
540 (let ((amount (second args)))
541 (if (constant-continuation-p amount)
542 (max (+ (count-low-order-zeros (first args))
543 (continuation-value amount))
552 (do ((result 0 (1+ result))
553 (num thing (ash num -1)))
554 ((logbitp 0 num) result))))
558 (deftransform / ((numerator denominator) (integer integer))
559 (unless (constant-continuation-p denominator)
560 (give-up-ir1-transform))
561 (let* ((denominator (continuation-value denominator))
562 (bits (1- (integer-length denominator))))
563 (unless (= (ash 1 bits) denominator)
564 (give-up-ir1-transform))
565 (let ((alignment (count-low-order-zeros numerator)))
566 (unless (>= alignment bits)
567 (give-up-ir1-transform))
568 `(ash numerator ,(- bits)))))
570 (deftransform ash ((value amount))
571 (let ((value-node (continuation-use value)))
572 (unless (and (combination-p value-node)
573 (eq (continuation-function-name (combination-fun value-node))
575 (give-up-ir1-transform))
576 (let ((inside-args (combination-args value-node)))
577 (unless (= (length inside-args) 2)
578 (give-up-ir1-transform))
579 (let ((inside-amount (second inside-args)))
580 (unless (and (constant-continuation-p inside-amount)
581 (not (minusp (continuation-value inside-amount))))
582 (give-up-ir1-transform)))))
583 (extract-function-args value 'ash 2)
584 '(lambda (value amount1 amount2)
585 (ash value (+ amount1 amount2))))
587 ;;;; ALIEN-FUNCALL support
589 (deftransform alien-funcall ((function &rest args)
590 ((alien (* t)) &rest *) *
592 (let ((names (make-gensym-list (length args))))
593 (/noshow "entering first DEFTRANSFORM ALIEN-FUNCALL" function args)
594 `(lambda (function ,@names)
595 (alien-funcall (deref function) ,@names))))
597 (deftransform alien-funcall ((function &rest args) * * :important t)
598 (let ((type (continuation-type function)))
599 (unless (alien-type-type-p type)
600 (give-up-ir1-transform "can't tell function type at compile time"))
601 (/noshow "entering second DEFTRANSFORM ALIEN-FUNCALL" function)
602 (let ((alien-type (alien-type-type-alien-type type)))
603 (unless (alien-function-type-p alien-type)
604 (give-up-ir1-transform))
605 (let ((arg-types (alien-function-type-arg-types alien-type)))
606 (unless (= (length args) (length arg-types))
608 "wrong number of arguments; expected ~D, got ~D"
611 (collect ((params) (deports))
612 (dolist (arg-type arg-types)
613 (let ((param (gensym)))
615 (deports `(deport ,param ',arg-type))))
616 (let ((return-type (alien-function-type-result-type alien-type))
617 (body `(%alien-funcall (deport function ',alien-type)
620 (if (alien-values-type-p return-type)
621 (collect ((temps) (results))
622 (dolist (type (alien-values-type-values return-type))
623 (let ((temp (gensym)))
625 (results `(naturalize ,temp ',type))))
627 `(multiple-value-bind ,(temps) ,body
628 (values ,@(results)))))
629 (setf body `(naturalize ,body ',return-type)))
630 (/noshow "returning from DEFTRANSFORM ALIEN-FUNCALL" (params) body)
631 `(lambda (function ,@(params))
634 (defoptimizer (%alien-funcall derive-type) ((function type &rest args))
635 (declare (ignore function args))
636 (unless (constant-continuation-p type)
637 (error "Something is broken."))
638 (let ((type (continuation-value type)))
639 (unless (alien-function-type-p type)
640 (error "Something is broken."))
642 (compute-alien-rep-type
643 (alien-function-type-result-type type)))))
645 (defoptimizer (%alien-funcall ltn-annotate)
646 ((function type &rest args) node ltn-policy)
647 (setf (basic-combination-info node) :funny)
648 (setf (node-tail-p node) nil)
649 (annotate-ordinary-continuation function ltn-policy)
651 (annotate-ordinary-continuation arg ltn-policy)))
653 (defoptimizer (%alien-funcall ir2-convert)
654 ((function type &rest args) call block)
655 (let ((type (if (constant-continuation-p type)
656 (continuation-value type)
657 (error "Something is broken.")))
658 (cont (node-cont call))
660 (multiple-value-bind (nsp stack-frame-size arg-tns result-tns)
661 (make-call-out-tns type)
662 (vop alloc-number-stack-space call block stack-frame-size nsp)
664 (let* ((arg (pop args))
667 #!-x86 (temp-tn (make-representation-tn (tn-primitive-type tn)
669 (move-arg-vops (svref (sc-move-arg-vops sc) scn)))
671 (assert (= (length move-arg-vops) 1) ()
672 "no unique move-arg-vop for moves in SC ~S"
674 #!+x86 (emit-move-arg-template call
676 (first move-arg-vops)
677 (continuation-tn call block arg)
683 (continuation-tn call block arg)
685 (emit-move-arg-template call
687 (first move-arg-vops)
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))))