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 deport-alloc
(alien alien-type
) t
66 (defknown extract-alien-value
(system-area-pointer unsigned-byte alien-type
) t
68 (defknown deposit-alien-value
(system-area-pointer unsigned-byte alien-type t
) t
71 (defknown alien-funcall
(alien-value &rest
*) *
74 ;;;; cosmetic transforms
76 (deftransform slot
((object slot
)
77 ((alien (* t
)) symbol
))
78 '(slot (deref object
) slot
))
80 (deftransform %set-slot
((object slot value
)
81 ((alien (* t
)) symbol t
))
82 '(%set-slot
(deref object
) slot value
))
84 (deftransform %slot-addr
((object slot
)
85 ((alien (* t
)) symbol
))
86 '(%slot-addr
(deref object
) slot
))
90 (defun find-slot-offset-and-type (alien slot
)
91 (unless (constant-lvar-p slot
)
92 (give-up-ir1-transform
93 "The slot is not constant, so access cannot be open coded."))
94 (let ((type (lvar-type alien
)))
95 (unless (alien-type-type-p type
)
96 (give-up-ir1-transform))
97 (let ((alien-type (alien-type-type-alien-type type
)))
98 (unless (alien-record-type-p alien-type
)
99 (give-up-ir1-transform))
100 (let* ((slot-name (lvar-value slot
))
101 (field (find slot-name
(alien-record-type-fields alien-type
)
102 :key
#'alien-record-field-name
)))
104 (abort-ir1-transform "~S doesn't have a slot named ~S"
107 (values (alien-record-field-offset field
)
108 (alien-record-field-type field
))))))
110 #+nil
;; Shouldn't be necessary.
111 (defoptimizer (slot derive-type
) ((alien slot
))
113 (catch 'give-up-ir1-transform
114 (multiple-value-bind (slot-offset slot-type
)
115 (find-slot-offset-and-type alien slot
)
116 (declare (ignore slot-offset
))
117 (return (make-alien-type-type slot-type
))))
120 (deftransform slot
((alien slot
) * * :important t
)
121 (multiple-value-bind (slot-offset slot-type
)
122 (find-slot-offset-and-type alien slot
)
123 `(extract-alien-value (alien-sap alien
)
127 #+nil
;; ### But what about coercions?
128 (defoptimizer (%set-slot derive-type
) ((alien slot value
))
130 (catch 'give-up-ir1-transform
131 (multiple-value-bind (slot-offset slot-type
)
132 (find-slot-offset-and-type alien slot
)
133 (declare (ignore slot-offset
))
134 (let ((type (make-alien-type-type slot-type
)))
135 (assert-lvar-type value type
)
139 (deftransform %set-slot
((alien slot value
) * * :important t
)
140 (multiple-value-bind (slot-offset slot-type
)
141 (find-slot-offset-and-type alien slot
)
142 `(deposit-alien-value (alien-sap alien
)
147 (defoptimizer (%slot-addr derive-type
) ((alien slot
))
149 (catch 'give-up-ir1-transform
150 (multiple-value-bind (slot-offset slot-type
)
151 (find-slot-offset-and-type alien slot
)
152 (declare (ignore slot-offset
))
153 (return (make-alien-type-type
154 (make-alien-pointer-type :to slot-type
)))))
157 (deftransform %slot-addr
((alien slot
) * * :important t
)
158 (multiple-value-bind (slot-offset slot-type
)
159 (find-slot-offset-and-type alien slot
)
160 (/noshow
"in DEFTRANSFORM %SLOT-ADDR, creating %SAP-ALIEN")
161 `(%sap-alien
(sap+ (alien-sap alien
) (/ ,slot-offset sb
!vm
:n-byte-bits
))
162 ',(make-alien-pointer-type :to slot-type
))))
166 (defun find-deref-alien-type (alien)
167 (let ((alien-type (lvar-type alien
)))
168 (unless (alien-type-type-p alien-type
)
169 (give-up-ir1-transform))
170 (let ((alien-type (alien-type-type-alien-type alien-type
)))
171 (if (alien-type-p alien-type
)
173 (give-up-ir1-transform)))))
175 (defun find-deref-element-type (alien)
176 (let ((alien-type (find-deref-alien-type alien
)))
179 (alien-pointer-type-to alien-type
))
181 (alien-array-type-element-type alien-type
))
183 (give-up-ir1-transform)))))
185 (defun compute-deref-guts (alien indices
)
186 (let ((alien-type (find-deref-alien-type alien
)))
190 (abort-ir1-transform "too many indices for pointer deref: ~W"
192 (let ((element-type (alien-pointer-type-to alien-type
)))
194 (let ((bits (alien-type-bits element-type
))
195 (alignment (alien-type-alignment element-type
)))
197 (abort-ir1-transform "unknown element size"))
199 (abort-ir1-transform "unknown element alignment"))
202 ,(align-offset bits alignment
))
204 (values nil
0 element-type
))))
206 (let* ((element-type (alien-array-type-element-type alien-type
))
207 (bits (alien-type-bits element-type
))
208 (alignment (alien-type-alignment element-type
))
209 (dims (alien-array-type-dimensions alien-type
)))
210 (unless (= (length indices
) (length dims
))
211 (give-up-ir1-transform "incorrect number of indices"))
213 (give-up-ir1-transform "Element size is unknown."))
215 (give-up-ir1-transform "Element alignment is unknown."))
217 (values nil
0 element-type
)
218 (let* ((arg (gensym))
221 (dolist (dim (cdr dims
))
222 (let ((arg (gensym)))
224 (setf offsetexpr
`(+ (* ,offsetexpr
,dim
) ,arg
))))
225 (values (reverse args
)
227 ,(align-offset bits alignment
))
230 (abort-ir1-transform "~S not either a pointer or array type."
233 #+nil
;; Shouldn't be necessary.
234 (defoptimizer (deref derive-type
) ((alien &rest noise
))
235 (declare (ignore noise
))
237 (catch 'give-up-ir1-transform
238 (return (make-alien-type-type (find-deref-element-type alien
))))
241 (deftransform deref
((alien &rest indices
) * * :important t
)
242 (multiple-value-bind (indices-args offset-expr element-type
)
243 (compute-deref-guts alien indices
)
244 `(lambda (alien ,@indices-args
)
245 (extract-alien-value (alien-sap alien
)
249 #+nil
;; ### Again, the value might be coerced.
250 (defoptimizer (%set-deref derive-type
) ((alien value
&rest noise
))
251 (declare (ignore noise
))
253 (catch 'give-up-ir1-transform
254 (let ((type (make-alien-type-type
255 (make-alien-pointer-type
256 :to
(find-deref-element-type alien
)))))
257 (assert-lvar-type value type
)
261 (deftransform %set-deref
((alien value
&rest indices
) * * :important t
)
262 (multiple-value-bind (indices-args offset-expr element-type
)
263 (compute-deref-guts alien indices
)
264 `(lambda (alien value
,@indices-args
)
265 (deposit-alien-value (alien-sap alien
)
270 (defoptimizer (%deref-addr derive-type
) ((alien &rest noise
))
271 (declare (ignore noise
))
273 (catch 'give-up-ir1-transform
274 (return (make-alien-type-type
275 (make-alien-pointer-type
276 :to
(find-deref-element-type alien
)))))
279 (deftransform %deref-addr
((alien &rest indices
) * * :important t
)
280 (multiple-value-bind (indices-args offset-expr element-type
)
281 (compute-deref-guts alien indices
)
282 (/noshow
"in DEFTRANSFORM %DEREF-ADDR, creating (LAMBDA .. %SAP-ALIEN)")
283 `(lambda (alien ,@indices-args
)
284 (%sap-alien
(sap+ (alien-sap alien
) (/ ,offset-expr sb
!vm
:n-byte-bits
))
285 ',(make-alien-pointer-type :to element-type
)))))
287 ;;;; support for aliens on the heap
289 (defun heap-alien-sap-and-type (info)
290 (unless (constant-lvar-p info
)
291 (give-up-ir1-transform "info not constant; can't open code"))
292 (let ((info (lvar-value info
)))
293 (values (heap-alien-info-sap-form info
)
294 (heap-alien-info-type info
))))
296 #+nil
; shouldn't be necessary
297 (defoptimizer (%heap-alien derive-type
) ((info))
300 (multiple-value-bind (sap type
) (heap-alien-sap-and-type info
)
301 (declare (ignore sap
))
302 (return (make-alien-type-type type
))))
305 (deftransform %heap-alien
((info) * * :important t
)
306 (multiple-value-bind (sap type
) (heap-alien-sap-and-type info
)
307 `(extract-alien-value ,sap
0 ',type
)))
309 #+nil
;; ### Again, deposit value might change the type.
310 (defoptimizer (%set-heap-alien derive-type
) ((info value
))
312 (catch 'give-up-ir1-transform
313 (multiple-value-bind (sap type
) (heap-alien-sap-and-type info
)
314 (declare (ignore sap
))
315 (let ((type (make-alien-type-type type
)))
316 (assert-lvar-type value type
)
320 (deftransform %set-heap-alien
((info value
) (heap-alien-info *) * :important t
)
321 (multiple-value-bind (sap type
) (heap-alien-sap-and-type info
)
322 `(deposit-alien-value ,sap
0 ',type value
)))
324 (defoptimizer (%heap-alien-addr derive-type
) ((info))
326 (catch 'give-up-ir1-transform
327 (multiple-value-bind (sap type
) (heap-alien-sap-and-type info
)
328 (declare (ignore sap
))
329 (return (make-alien-type-type (make-alien-pointer-type :to type
)))))
332 (deftransform %heap-alien-addr
((info) * * :important t
)
333 (multiple-value-bind (sap type
) (heap-alien-sap-and-type info
)
334 (/noshow
"in DEFTRANSFORM %HEAP-ALIEN-ADDR, creating %SAP-ALIEN")
335 `(%sap-alien
,sap
',type
)))
337 ;;;; support for local (stack or register) aliens
339 (deftransform make-local-alien
((info) * * :important t
)
340 (unless (constant-lvar-p info
)
341 (abort-ir1-transform "Local alien info isn't constant?"))
342 (let* ((info (lvar-value info
))
343 (alien-type (local-alien-info-type info
))
344 (bits (alien-type-bits alien-type
)))
346 (abort-ir1-transform "unknown size: ~S" (unparse-alien-type alien-type
)))
347 (/noshow
"in DEFTRANSFORM MAKE-LOCAL-ALIEN" info
)
348 (/noshow
(local-alien-info-force-to-memory-p info
))
349 (/noshow alien-type
(unparse-alien-type alien-type
) (alien-type-bits alien-type
))
350 (if (local-alien-info-force-to-memory-p info
)
352 `(truly-the system-area-pointer
353 (%primitive alloc-alien-stack-space
354 ,(ceiling (alien-type-bits alien-type
)
357 `(truly-the system-area-pointer
358 (%primitive alloc-number-stack-space
359 ,(ceiling (alien-type-bits alien-type
)
361 (let* ((alien-rep-type-spec (compute-alien-rep-type alien-type
))
362 (alien-rep-type (specifier-type alien-rep-type-spec
)))
363 (cond ((csubtypep (specifier-type 'system-area-pointer
)
366 ((ctypep 0 alien-rep-type
) 0)
367 ((ctypep 0.0f0 alien-rep-type
) 0.0f0
)
368 ((ctypep 0.0d0 alien-rep-type
) 0.0d0
)
371 "Aliens of type ~S cannot be represented immediately."
372 (unparse-alien-type alien-type
))))))))
374 (deftransform note-local-alien-type
((info var
) * * :important t
)
375 ;; FIXME: This test and error occur about a zillion times. They
376 ;; could be factored into a function.
377 (unless (constant-lvar-p info
)
378 (abort-ir1-transform "Local alien info isn't constant?"))
379 (let ((info (lvar-value info
)))
380 (/noshow
"in DEFTRANSFORM NOTE-LOCAL-ALIEN-TYPE" info
)
381 (/noshow
(local-alien-info-force-to-memory-p info
))
382 (unless (local-alien-info-force-to-memory-p info
)
383 (let ((var-node (lvar-uses var
)))
384 (/noshow var-node
(ref-p var-node
))
385 (when (ref-p var-node
)
386 (propagate-to-refs (ref-leaf var-node
)
388 (compute-alien-rep-type
389 (local-alien-info-type info
))))))))
392 (deftransform local-alien
((info var
) * * :important t
)
393 (unless (constant-lvar-p info
)
394 (abort-ir1-transform "Local alien info isn't constant?"))
395 (let* ((info (lvar-value info
))
396 (alien-type (local-alien-info-type info
)))
397 (/noshow
"in DEFTRANSFORM LOCAL-ALIEN" info alien-type
)
398 (/noshow
(local-alien-info-force-to-memory-p info
))
399 (if (local-alien-info-force-to-memory-p info
)
400 `(extract-alien-value var
0 ',alien-type
)
401 `(naturalize var
',alien-type
))))
403 (deftransform %local-alien-forced-to-memory-p
((info) * * :important t
)
404 (unless (constant-lvar-p info
)
405 (abort-ir1-transform "Local alien info isn't constant?"))
406 (let ((info (lvar-value info
)))
407 (local-alien-info-force-to-memory-p info
)))
409 (deftransform %set-local-alien
((info var value
) * * :important t
)
410 (unless (constant-lvar-p info
)
411 (abort-ir1-transform "Local alien info isn't constant?"))
412 (let* ((info (lvar-value info
))
413 (alien-type (local-alien-info-type info
)))
414 (if (local-alien-info-force-to-memory-p info
)
415 `(deposit-alien-value var
0 ',alien-type value
)
416 '(error "This should be eliminated as dead code."))))
418 (defoptimizer (%local-alien-addr derive-type
) ((info var
))
419 (if (constant-lvar-p info
)
420 (let* ((info (lvar-value info
))
421 (alien-type (local-alien-info-type info
)))
422 (make-alien-type-type (make-alien-pointer-type :to alien-type
)))
425 (deftransform %local-alien-addr
((info var
) * * :important t
)
426 (unless (constant-lvar-p info
)
427 (abort-ir1-transform "Local alien info isn't constant?"))
428 (let* ((info (lvar-value info
))
429 (alien-type (local-alien-info-type info
)))
430 (/noshow
"in DEFTRANSFORM %LOCAL-ALIEN-ADDR, creating %SAP-ALIEN")
431 (if (local-alien-info-force-to-memory-p info
)
432 `(%sap-alien var
',(make-alien-pointer-type :to alien-type
))
433 (error "This shouldn't happen."))))
435 (deftransform dispose-local-alien
((info var
) * * :important t
)
436 (unless (constant-lvar-p info
)
437 (abort-ir1-transform "Local alien info isn't constant?"))
438 (let* ((info (lvar-value info
))
439 (alien-type (local-alien-info-type info
)))
440 (if (local-alien-info-force-to-memory-p info
)
441 #!+(or x86 x86-64
) `(%primitive dealloc-alien-stack-space
442 ,(ceiling (alien-type-bits alien-type
)
444 #!-
(or x86 x86-64
) `(%primitive dealloc-number-stack-space
445 ,(ceiling (alien-type-bits alien-type
)
451 (defoptimizer (%cast derive-type
) ((alien type
))
452 (or (when (constant-lvar-p type
)
453 (let ((alien-type (lvar-value type
)))
454 (when (alien-type-p alien-type
)
455 (make-alien-type-type alien-type
))))
458 (deftransform %cast
((alien target-type
) * * :important t
)
459 (unless (constant-lvar-p target-type
)
460 (give-up-ir1-transform
461 "The alien type is not constant, so access cannot be open coded."))
462 (let ((target-type (lvar-value target-type
)))
463 (cond ((or (alien-pointer-type-p target-type
)
464 (alien-array-type-p target-type
)
465 (alien-fun-type-p target-type
))
466 `(naturalize (alien-sap alien
) ',target-type
))
468 (abort-ir1-transform "cannot cast to alien type ~S" target-type
)))))
470 ;;;; ALIEN-SAP, %SAP-ALIEN, %ADDR, etc.
472 (deftransform alien-sap
((alien) * * :important t
)
473 (let ((alien-node (lvar-uses alien
)))
476 (splice-fun-args alien
'%sap-alien
2)
478 (declare (ignore type
))
481 (give-up-ir1-transform)))))
483 (defoptimizer (%sap-alien derive-type
) ((sap type
))
484 (declare (ignore sap
))
485 (if (constant-lvar-p type
)
486 (make-alien-type-type (lvar-value type
))
489 (deftransform %sap-alien
((sap type
) * * :important t
)
490 (give-up-ir1-transform
491 ;; FIXME: The hardcoded newline here causes more-than-usually
492 ;; screwed-up formatting of the optimization note output.
493 "could not optimize away %SAP-ALIEN: forced to do runtime ~@
494 allocation of alien-value structure"))
496 ;;;; NATURALIZE/DEPORT/EXTRACT/DEPOSIT magic
498 (flet ((%computed-lambda
(compute-lambda type
)
499 (declare (type function compute-lambda
))
500 (unless (constant-lvar-p type
)
501 (give-up-ir1-transform
502 "The type is not constant at compile time; can't open code."))
504 (let ((result (funcall compute-lambda
(lvar-value type
))))
505 (/noshow
"in %COMPUTED-LAMBDA" (lvar-value type
) result
)
508 (compiler-error "~A" condition
)))))
509 (deftransform naturalize
((object type
) * * :important t
)
510 (%computed-lambda
#'compute-naturalize-lambda type
))
511 (deftransform deport
((alien type
) * * :important t
)
512 (%computed-lambda
#'compute-deport-lambda type
))
513 (deftransform deport-alloc
((alien type
) * * :important t
)
514 (%computed-lambda
#'compute-deport-alloc-lambda type
))
515 (deftransform extract-alien-value
((sap offset type
) * * :important t
)
516 (%computed-lambda
#'compute-extract-lambda type
))
517 (deftransform deposit-alien-value
((sap offset type value
) * * :important t
)
518 (%computed-lambda
#'compute-deposit-lambda type
)))
520 ;;;; a hack to clean up divisions
522 (defun count-low-order-zeros (thing)
525 (if (constant-lvar-p thing
)
526 (count-low-order-zeros (lvar-value thing
))
527 (count-low-order-zeros (lvar-uses thing
))))
529 (case (let ((name (lvar-fun-name (combination-fun thing
))))
530 (or (modular-version-info name
:unsigned
) name
))
532 (let ((min most-positive-fixnum
)
533 (itype (specifier-type 'integer
)))
534 (dolist (arg (combination-args thing
) min
)
535 (if (csubtypep (lvar-type arg
) itype
)
536 (setf min
(min min
(count-low-order-zeros arg
)))
540 (itype (specifier-type 'integer
)))
541 (dolist (arg (combination-args thing
) result
)
542 (if (csubtypep (lvar-type arg
) itype
)
543 (setf result
(+ result
(count-low-order-zeros arg
)))
546 (let ((args (combination-args thing
)))
547 (if (= (length args
) 2)
548 (let ((amount (second args
)))
549 (if (constant-lvar-p amount
)
550 (max (+ (count-low-order-zeros (first args
))
560 (do ((result 0 (1+ result
))
561 (num thing
(ash num -
1)))
562 ((logbitp 0 num
) result
))))
564 (count-low-order-zeros (cast-value thing
)))
568 (deftransform / ((numerator denominator
) (integer integer
))
569 "convert x/2^k to shift"
570 (unless (constant-lvar-p denominator
)
571 (give-up-ir1-transform))
572 (let* ((denominator (lvar-value denominator
))
573 (bits (1- (integer-length denominator
))))
574 (unless (and (> denominator
0) (= (ash 1 bits
) denominator
))
575 (give-up-ir1-transform))
576 (let ((alignment (count-low-order-zeros numerator
)))
577 (unless (>= alignment bits
)
578 (give-up-ir1-transform))
579 `(ash numerator
,(- bits
)))))
581 (deftransform ash
((value amount
))
582 (let ((value-node (lvar-uses value
)))
583 (unless (combination-p value-node
)
584 (give-up-ir1-transform))
585 (let ((inside-fun-name (lvar-fun-name (combination-fun value-node
))))
586 (multiple-value-bind (prototype width
)
587 (modular-version-info inside-fun-name
:unsigned
)
588 (unless (eq (or prototype inside-fun-name
) 'ash
)
589 (give-up-ir1-transform))
590 (when (and width
(not (constant-lvar-p amount
)))
591 (give-up-ir1-transform))
592 (let ((inside-args (combination-args value-node
)))
593 (unless (= (length inside-args
) 2)
594 (give-up-ir1-transform))
595 (let ((inside-amount (second inside-args
)))
596 (unless (and (constant-lvar-p inside-amount
)
597 (not (minusp (lvar-value inside-amount
))))
598 (give-up-ir1-transform)))
599 (splice-fun-args value inside-fun-name
2)
601 `(lambda (value amount1 amount2
)
602 (logand (ash value
(+ amount1 amount2
))
603 ,(1- (ash 1 (+ width
(lvar-value amount
))))))
604 `(lambda (value amount1 amount2
)
605 (ash value
(+ amount1 amount2
)))))))))
607 ;;;; ALIEN-FUNCALL support
609 (deftransform alien-funcall
((function &rest args
)
610 ((alien (* t
)) &rest
*) *
612 (let ((names (make-gensym-list (length args
))))
613 (/noshow
"entering first DEFTRANSFORM ALIEN-FUNCALL" function args
)
614 `(lambda (function ,@names
)
615 (alien-funcall (deref function
) ,@names
))))
617 (deftransform alien-funcall
((function &rest args
) * * :important t
)
618 (let ((type (lvar-type function
)))
619 (unless (alien-type-type-p type
)
620 (give-up-ir1-transform "can't tell function type at compile time"))
621 (/noshow
"entering second DEFTRANSFORM ALIEN-FUNCALL" function
)
622 (let ((alien-type (alien-type-type-alien-type type
)))
623 (unless (alien-fun-type-p alien-type
)
624 (give-up-ir1-transform))
625 (let ((arg-types (alien-fun-type-arg-types alien-type
)))
626 (unless (= (length args
) (length arg-types
))
628 "wrong number of arguments; expected ~W, got ~W"
631 (collect ((params) (deports))
632 (dolist (arg-type arg-types
)
633 (let ((param (gensym)))
635 (deports `(deport ,param
',arg-type
))))
636 ;; Build BODY from the inside out.
637 (let ((return-type (alien-fun-type-result-type alien-type
))
638 ;; Innermost, we DEPORT the parameters (e.g. by taking SAPs
639 ;; to them) and do the call.
640 (body `(%alien-funcall
(deport function
',alien-type
)
643 ;; Wrap that in a WITH-PINNED-OBJECTS to ensure the values
644 ;; the SAPs are taken for won't be moved by the GC. (If
645 ;; needed: some alien types won't need it).
646 (setf body
`(maybe-with-pinned-objects ,(params) ,arg-types
648 ;; Around that handle any memory allocation that's needed.
649 ;; Mostly the DEPORT-ALLOC alien-type-methods are just an
650 ;; identity operation, but for example for deporting a
651 ;; Unicode string we need to convert the string into an
652 ;; octet array. This step needs to be done before the pinning
653 ;; to ensure we pin the right objects, so it can't be combined
654 ;; with the deporting.
656 (loop for param in
(params)
657 for arg-type in arg-types
659 `(let ((,param
(deport-alloc ,param
',arg-type
)))
661 (if (alien-values-type-p return-type
)
662 (collect ((temps) (results))
663 (dolist (type (alien-values-type-values return-type
))
664 (let ((temp (gensym)))
666 (results `(naturalize ,temp
',type
))))
668 `(multiple-value-bind ,(temps) ,body
669 (values ,@(results)))))
670 (setf body
`(naturalize ,body
',return-type
)))
671 (/noshow
"returning from DEFTRANSFORM ALIEN-FUNCALL" (params) body
)
672 `(lambda (function ,@(params))
675 (defoptimizer (%alien-funcall derive-type
) ((function type
&rest args
))
676 (declare (ignore function args
))
677 (unless (constant-lvar-p type
)
678 (error "Something is broken."))
679 (let ((type (lvar-value type
)))
680 (unless (alien-fun-type-p type
)
681 (error "Something is broken."))
682 (values-specifier-type
683 (compute-alien-rep-type
684 (alien-fun-type-result-type type
)))))
686 (defoptimizer (%alien-funcall ltn-annotate
)
687 ((function type
&rest args
) node ltn-policy
)
688 (setf (basic-combination-info node
) :funny
)
689 (setf (node-tail-p node
) nil
)
690 (annotate-ordinary-lvar function
)
692 (annotate-ordinary-lvar arg
)))
694 ;;; We support both the stdcall and cdecl calling conventions on win32 by
695 ;;; resetting ESP after the foreign function returns. This way it works
696 ;;; correctly whether the party that is supposed to pop arguments from
697 ;;; the stack is the caller (cdecl) or the callee (stdcall).
698 (defoptimizer (%alien-funcall ir2-convert
)
699 ((function type
&rest args
) call block
)
700 (let ((type (if (constant-lvar-p type
)
702 (error "Something is broken.")))
703 (lvar (node-lvar call
))
706 (stack-pointer (make-stack-pointer-tn)))
707 (multiple-value-bind (nsp stack-frame-size arg-tns result-tns
)
708 (make-call-out-tns type
)
711 (vop set-fpu-word-for-c call block
)
712 (vop current-stack-pointer call block stack-pointer
))
713 (vop alloc-number-stack-space call block stack-frame-size nsp
)
715 ;; On PPC, TN might be a list. This is used to indicate
716 ;; something special needs to happen. See below.
718 ;; FIXME: We should implement something better than this.
719 (let* ((first-tn (if (listp tn
) (car tn
) tn
))
721 (sc (tn-sc first-tn
))
723 #!-
(or x86 x86-64
) (temp-tn (make-representation-tn
724 (tn-primitive-type first-tn
) scn
))
725 (move-arg-vops (svref (sc-move-arg-vops sc
) scn
)))
727 (unless (= (length move-arg-vops
) 1)
728 (error "no unique move-arg-vop for moves in SC ~S" (sc-name sc
)))
729 #!+(or x86 x86-64
) (emit-move-arg-template call
731 (first move-arg-vops
)
732 (lvar-tn call block arg
)
735 #!-
(or x86 x86-64
) (progn
738 (lvar-tn call block arg
)
740 (emit-move-arg-template call
742 (first move-arg-vops
)
748 ;; This means that we have a float arg that we need to
749 ;; also copy to some int regs. The list contains the TN
750 ;; for the float as well as the TNs to use for the int
752 (destructuring-bind (float-tn i1-tn
&optional i2-tn
)
755 (vop sb
!vm
::move-double-to-int-arg call block
756 float-tn i1-tn i2-tn
)
757 (vop sb
!vm
::move-single-to-int-arg call block
760 (unless (listp result-tns
)
761 (setf result-tns
(list result-tns
)))
762 (let ((arg-tns (flatten-list arg-tns
)))
763 (vop* call-out call block
764 ((lvar-tn call block function
)
765 (reference-tn-list arg-tns nil
))
766 ((reference-tn-list result-tns t
))))
768 (vop dealloc-number-stack-space call block stack-frame-size
)
771 (vop reset-stack-pointer call block stack-pointer
)
772 (vop set-fpu-word-for-lisp call block
))
773 (move-lvar-result call block result-tns lvar
))))