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:n-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:n-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-fun-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 ;; 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"))
493 ;;;; NATURALIZE/DEPORT/EXTRACT/DEPOSIT magic
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."))
501 (let ((result (funcall compute-lambda (continuation-value type))))
502 (/noshow "in %COMPUTED-LAMBDA" (continuation-value type) result)
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)))
515 ;;;; a hack to clean up divisions
517 (defun count-low-order-zeros (thing)
520 (if (constant-continuation-p thing)
521 (count-low-order-zeros (continuation-value thing))
522 (count-low-order-zeros (continuation-use thing))))
524 (case (continuation-fun-name (combination-fun thing))
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)))
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)))
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))
554 (do ((result 0 (1+ result))
555 (num thing (ash num -1)))
556 ((logbitp 0 num) result))))
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)))))
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))
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-function-args value 'ash 2)
586 '(lambda (value amount1 amount2)
587 (ash value (+ amount1 amount2))))
589 ;;;; ALIEN-FUNCALL support
591 (deftransform alien-funcall ((function &rest args)
592 ((alien (* t)) &rest *) *
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))))
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))
610 "wrong number of arguments; expected ~D, got ~D"
613 (collect ((params) (deports))
614 (dolist (arg-type arg-types)
615 (let ((param (gensym)))
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)
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)))
627 (results `(naturalize ,temp ',type))))
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))
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."))
644 (compute-alien-rep-type
645 (alien-fun-type-result-type type)))))
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)
653 (annotate-ordinary-continuation arg ltn-policy)))
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))
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)
666 (let* ((arg (pop args))
669 #!-x86 (temp-tn (make-representation-tn (tn-primitive-type tn)
671 (move-arg-vops (svref (sc-move-arg-vops sc) scn)))
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
677 (first move-arg-vops)
678 (continuation-tn call block arg)
684 (continuation-tn call block arg)
686 (emit-move-arg-template call
688 (first move-arg-vops)
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))))