Fix comment about *code-coverage-info*.
[sbcl.git] / src / compiler / aliencomp.lisp
blob667692537a7b31c14c1d911fa04c5d938635c818
1 ;;;; transforms and other stuff used to compile ALIEN operations
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.
12 (in-package "SB!C")
14 ;;;; DEFKNOWNs
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))
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))
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))
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))
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 ())
57 (defknown %cast (alien-value alien-type) alien
58 (flushable movable))
60 (defknown naturalize (t alien-type) alien
61 (flushable movable))
62 (defknown deport (alien alien-type) t
63 (flushable movable))
64 (defknown deport-alloc (alien alien-type) t
65 (flushable movable))
66 (defknown %alien-value (system-area-pointer unsigned-byte alien-type) t
67 (flushable))
68 (defknown (setf %alien-value) (t system-area-pointer unsigned-byte alien-type) t
69 ())
71 (defknown alien-funcall (alien-value &rest *) *
72 (any recursive))
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))
88 ;;;; SLOT support
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)))
103 (unless field
104 (abort-ir1-transform "~S~% doesn't have a slot named ~S"
105 type slot-name))
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))
111 (block nil
112 (catch 'give-up-ir1-transform
113 (multiple-value-bind (slot-offset slot-type)
114 (find-slot-offset-and-type alien slot)
115 (declare (ignore slot-offset))
116 (return (make-alien-type-type slot-type))))
117 *wild-type*))
119 (deftransform slot ((alien slot) * * :important t)
120 (multiple-value-bind (slot-offset slot-type)
121 (find-slot-offset-and-type alien slot)
122 `(%alien-value (alien-sap alien)
123 ,slot-offset
124 ',slot-type)))
126 #+nil ;; ### But what about coercions?
127 (defoptimizer (%set-slot derive-type) ((alien slot value))
128 (block nil
129 (catch 'give-up-ir1-transform
130 (multiple-value-bind (slot-offset slot-type)
131 (find-slot-offset-and-type alien slot)
132 (declare (ignore slot-offset))
133 (let ((type (make-alien-type-type slot-type)))
134 (assert-lvar-type value type)
135 (return type))))
136 *wild-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 `(setf (%alien-value (alien-sap alien)
142 ,slot-offset
143 ',slot-type)
144 value)))
146 (defoptimizer (%slot-addr derive-type) ((alien slot))
147 (block nil
148 (catch 'give-up-ir1-transform
149 (multiple-value-bind (slot-offset slot-type)
150 (find-slot-offset-and-type alien slot)
151 (declare (ignore slot-offset))
152 (return (make-alien-type-type
153 (make-alien-pointer-type :to slot-type)))))
154 *wild-type*))
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))))
163 ;;;; DEREF support
165 (defun find-deref-alien-type (alien)
166 (let ((alien-type (lvar-type alien)))
167 (unless (alien-type-type-p alien-type)
168 (give-up-ir1-transform))
169 (let ((alien-type (alien-type-type-alien-type alien-type)))
170 (if (alien-type-p alien-type)
171 alien-type
172 (give-up-ir1-transform)))))
174 (defun find-deref-element-type (alien)
175 (let ((alien-type (find-deref-alien-type alien)))
176 (typecase alien-type
177 (alien-pointer-type
178 (alien-pointer-type-to alien-type))
179 (alien-array-type
180 (alien-array-type-element-type alien-type))
182 (give-up-ir1-transform)))))
184 (defun compute-deref-guts (alien indices)
185 (let ((alien-type (find-deref-alien-type alien)))
186 (typecase alien-type
187 (alien-pointer-type
188 (when (cdr indices)
189 (abort-ir1-transform "too many indices for pointer deref: ~W"
190 (length indices)))
191 (let ((element-type (alien-pointer-type-to alien-type)))
192 (unless element-type
193 (give-up-ir1-transform "unable to open code deref of wild pointer type"))
194 (if indices
195 (let ((bits (alien-type-bits element-type))
196 (alignment (alien-type-alignment element-type)))
197 (unless bits
198 (abort-ir1-transform "unknown element size"))
199 (unless alignment
200 (abort-ir1-transform "unknown element alignment"))
201 (values '(offset)
202 `(* offset
203 ,(align-offset bits alignment))
204 element-type))
205 (values nil 0 element-type))))
206 (alien-array-type
207 (let* ((element-type (alien-array-type-element-type alien-type))
208 (bits (alien-type-bits element-type))
209 (alignment (alien-type-alignment element-type))
210 (dims (alien-array-type-dimensions alien-type)))
211 (unless (= (length indices) (length dims))
212 (give-up-ir1-transform "incorrect number of indices"))
213 (unless bits
214 (give-up-ir1-transform "Element size is unknown."))
215 (unless alignment
216 (give-up-ir1-transform "Element alignment is unknown."))
217 (if (null dims)
218 (values nil 0 element-type)
219 (let* ((arg (gensym))
220 (args (list arg))
221 (offsetexpr arg))
222 (dolist (dim (cdr dims))
223 (let ((arg (gensym)))
224 (push arg args)
225 (setf offsetexpr `(+ (* ,offsetexpr ,dim) ,arg))))
226 (values (reverse args)
227 `(* ,offsetexpr
228 ,(align-offset bits alignment))
229 element-type)))))
231 (abort-ir1-transform "~S not either a pointer or array type."
232 alien-type)))))
234 #+nil ;; Shouldn't be necessary.
235 (defoptimizer (deref derive-type) ((alien &rest noise))
236 (declare (ignore noise))
237 (block nil
238 (catch 'give-up-ir1-transform
239 (return (make-alien-type-type (find-deref-element-type alien))))
240 *wild-type*))
242 (deftransform deref ((alien &rest indices) * * :important t)
243 (multiple-value-bind (indices-args offset-expr element-type)
244 (compute-deref-guts alien indices)
245 `(lambda (alien ,@indices-args)
246 (%alien-value (alien-sap alien)
247 ,offset-expr
248 ',element-type))))
250 #+nil ;; ### Again, the value might be coerced.
251 (defoptimizer (%set-deref derive-type) ((alien value &rest noise))
252 (declare (ignore noise))
253 (block nil
254 (catch 'give-up-ir1-transform
255 (let ((type (make-alien-type-type
256 (make-alien-pointer-type
257 :to (find-deref-element-type alien)))))
258 (assert-lvar-type value type)
259 (return type)))
260 *wild-type*))
262 (deftransform %set-deref ((alien value &rest indices) * * :important t)
263 (multiple-value-bind (indices-args offset-expr element-type)
264 (compute-deref-guts alien indices)
265 `(lambda (alien value ,@indices-args)
266 (setf (%alien-value (alien-sap alien)
267 ,offset-expr
268 ',element-type)
269 value))))
271 (defoptimizer (%deref-addr derive-type) ((alien &rest noise))
272 (declare (ignore noise))
273 (block nil
274 (catch 'give-up-ir1-transform
275 (return (make-alien-type-type
276 (make-alien-pointer-type
277 :to (find-deref-element-type alien)))))
278 *wild-type*))
280 (deftransform %deref-addr ((alien &rest indices) * * :important t)
281 (multiple-value-bind (indices-args offset-expr element-type)
282 (compute-deref-guts alien indices)
283 (/noshow "in DEFTRANSFORM %DEREF-ADDR, creating (LAMBDA .. %SAP-ALIEN)")
284 `(lambda (alien ,@indices-args)
285 (%sap-alien (sap+ (alien-sap alien) (/ ,offset-expr sb!vm:n-byte-bits))
286 ',(make-alien-pointer-type :to element-type)))))
288 ;;;; support for aliens on the heap
290 (defun heap-alien-sap-and-type (info)
291 (unless (constant-lvar-p info)
292 (give-up-ir1-transform "info not constant; can't open code"))
293 (let ((info (lvar-value info)))
294 (values (heap-alien-info-sap-form info)
295 (heap-alien-info-type info))))
297 #+nil ; shouldn't be necessary
298 (defoptimizer (%heap-alien derive-type) ((info))
299 (block nil
300 (catch 'give-up
301 (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
302 (declare (ignore sap))
303 (return (make-alien-type-type type))))
304 *wild-type*))
306 (deftransform %heap-alien ((info) ((constant-arg heap-alien-info)) * :important t)
307 (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
308 `(%alien-value ,sap 0 ',type)))
310 #+nil ;; ### Again, deposit value might change the type.
311 (defoptimizer (%set-heap-alien derive-type) ((info value))
312 (block nil
313 (catch 'give-up-ir1-transform
314 (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
315 (declare (ignore sap))
316 (let ((type (make-alien-type-type type)))
317 (assert-lvar-type value type)
318 (return type))))
319 *wild-type*))
321 (deftransform %set-heap-alien ((info value) (heap-alien-info *) * :important t)
322 (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
323 `(setf (%alien-value ,sap 0 ',type) value)))
325 (defoptimizer (%heap-alien-addr derive-type) ((info))
326 (block nil
327 (catch 'give-up-ir1-transform
328 (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
329 (declare (ignore sap))
330 (return (make-alien-type-type (make-alien-pointer-type :to type)))))
331 *wild-type*))
333 (deftransform %heap-alien-addr ((info) * * :important t)
334 (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
335 (/noshow "in DEFTRANSFORM %HEAP-ALIEN-ADDR, creating %SAP-ALIEN")
336 `(%sap-alien ,sap ',(make-alien-pointer-type :to type))))
339 ;;;; support for local (stack or register) aliens
341 (defun alien-info-constant-or-abort (info)
342 (unless (constant-lvar-p info)
343 (abort-ir1-transform "Local alien info isn't constant?")))
345 (deftransform make-local-alien ((info) * * :important t)
346 (alien-info-constant-or-abort info)
347 (let* ((info (lvar-value info))
348 (alien-type (local-alien-info-type info))
349 (bits (alien-type-bits alien-type)))
350 (unless bits
351 (abort-ir1-transform "unknown size: ~S" (unparse-alien-type alien-type)))
352 (/noshow "in DEFTRANSFORM MAKE-LOCAL-ALIEN" info)
353 (/noshow (local-alien-info-force-to-memory-p info))
354 (/noshow alien-type (unparse-alien-type alien-type) (alien-type-bits alien-type))
355 (if (local-alien-info-force-to-memory-p info)
356 #!+(or x86 x86-64)
357 `(%primitive alloc-alien-stack-space
358 ,(ceiling (alien-type-bits alien-type)
359 sb!vm:n-byte-bits))
360 #!-(or x86 x86-64)
361 `(%primitive alloc-number-stack-space
362 ,(ceiling (alien-type-bits alien-type)
363 sb!vm:n-byte-bits))
364 (let* ((alien-rep-type-spec (compute-alien-rep-type alien-type))
365 (alien-rep-type (specifier-type alien-rep-type-spec)))
366 (cond ((csubtypep (specifier-type 'system-area-pointer)
367 alien-rep-type)
368 '(int-sap 0))
369 ((ctypep 0 alien-rep-type) 0)
370 ((ctypep 0.0f0 alien-rep-type) 0.0f0)
371 ((ctypep 0.0d0 alien-rep-type) 0.0d0)
373 (compiler-error
374 "Aliens of type ~S cannot be represented immediately."
375 (unparse-alien-type alien-type))))))))
377 (deftransform note-local-alien-type ((info var) * * :important t)
378 (alien-info-constant-or-abort info)
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)
387 (specifier-type
388 (compute-alien-rep-type
389 (local-alien-info-type info))))))))
390 nil)
392 (deftransform local-alien ((info var) * * :important t)
393 (alien-info-constant-or-abort info)
394 (let* ((info (lvar-value info))
395 (alien-type (local-alien-info-type info)))
396 (/noshow "in DEFTRANSFORM LOCAL-ALIEN" info alien-type)
397 (/noshow (local-alien-info-force-to-memory-p info))
398 (if (local-alien-info-force-to-memory-p info)
399 `(%alien-value var 0 ',alien-type)
400 `(naturalize var ',alien-type))))
402 (deftransform %local-alien-forced-to-memory-p ((info) * * :important t)
403 (alien-info-constant-or-abort info)
404 (let ((info (lvar-value info)))
405 (local-alien-info-force-to-memory-p info)))
407 (deftransform %set-local-alien ((info var value) * * :important t)
408 (alien-info-constant-or-abort info)
409 (let* ((info (lvar-value info))
410 (alien-type (local-alien-info-type info)))
411 (if (local-alien-info-force-to-memory-p info)
412 `(setf (%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 (declare (ignore var))
417 (if (constant-lvar-p info)
418 (let* ((info (lvar-value info))
419 (alien-type (local-alien-info-type info)))
420 (make-alien-type-type (make-alien-pointer-type :to alien-type)))
421 *wild-type*))
423 (deftransform %local-alien-addr ((info var) * * :important t)
424 (alien-info-constant-or-abort info)
425 (let* ((info (lvar-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 ;; DISPOSE-LOCAL-ALIEN can't happens on x86[-64].
433 ;; A transform of it is just misdirection.
434 #!-(or x86 x86-64)
435 (deftransform dispose-local-alien ((info var) * * :important t)
436 (alien-info-constant-or-abort info)
437 (let* ((info (lvar-value info))
438 (alien-type (local-alien-info-type info)))
439 (if (local-alien-info-force-to-memory-p info)
440 `(%primitive dealloc-number-stack-space
441 ,(ceiling (alien-type-bits alien-type)
442 sb!vm:n-byte-bits))
443 nil)))
445 ;;;; %CAST
447 (defoptimizer (%cast derive-type) ((alien type))
448 (declare (ignore alien))
449 (or (when (constant-lvar-p type)
450 (let ((alien-type (lvar-value type)))
451 (when (alien-type-p alien-type)
452 (make-alien-type-type alien-type))))
453 *wild-type*))
455 (deftransform %cast ((alien target-type) * * :important t)
456 (unless (constant-lvar-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 (lvar-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 (lvar-uses alien)))
471 (typecase alien-node
472 (combination
473 (splice-fun-args alien '%sap-alien 2)
474 '(lambda (sap type)
475 (declare (ignore type))
476 sap))
478 (give-up-ir1-transform)))))
480 (defoptimizer (%sap-alien derive-type) ((sap type))
481 (declare (ignore sap))
482 (if (constant-lvar-p type)
483 (make-alien-type-type (lvar-value type))
484 *wild-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-lvar-p type)
498 (give-up-ir1-transform
499 "The type is not constant at compile time; can't open code."))
500 (handler-case
501 (let ((result (funcall compute-lambda (lvar-value type))))
502 (/noshow "in %COMPUTED-LAMBDA" (lvar-value type) result)
503 result)
504 (error (condition)
505 (compiler-error "~A" condition)))))
506 (deftransform naturalize ((object type) * * :important t)
507 (%computed-lambda #'compute-naturalize-lambda type))
508 (deftransform deport ((alien type) * * :important t)
509 (%computed-lambda #'compute-deport-lambda type))
510 (deftransform deport-alloc ((alien type) * * :important t)
511 (%computed-lambda #'compute-deport-alloc-lambda type))
512 (deftransform %alien-value ((sap offset type) * * :important t)
513 (%computed-lambda #'compute-extract-lambda type))
514 (deftransform (setf %alien-value) ((value sap offset type) * * :important t)
515 (%computed-lambda #'compute-deposit-lambda type)))
517 ;;;; a hack to clean up divisions
519 (defun count-low-order-zeros (thing)
520 (typecase thing
521 (lvar
522 (if (constant-lvar-p thing)
523 (count-low-order-zeros (lvar-value thing))
524 (count-low-order-zeros (lvar-uses thing))))
525 (combination
526 (case (let ((name (lvar-fun-name (combination-fun thing))))
527 (or (modular-version-info name :untagged nil) name))
528 ((+ -)
529 (let ((min most-positive-fixnum)
530 (itype (specifier-type 'integer)))
531 (dolist (arg (combination-args thing) min)
532 (if (csubtypep (lvar-type arg) itype)
533 (setf min (min min (count-low-order-zeros arg)))
534 (return 0)))))
536 (let ((result 0)
537 (itype (specifier-type 'integer)))
538 (dolist (arg (combination-args thing) result)
539 (if (csubtypep (lvar-type arg) itype)
540 (setf result (+ result (count-low-order-zeros arg)))
541 (return 0)))))
542 (ash
543 (let ((args (combination-args thing)))
544 (if (= (length args) 2)
545 (let ((amount (second args)))
546 (if (constant-lvar-p amount)
547 (max (+ (count-low-order-zeros (first args))
548 (lvar-value amount))
551 0)))
553 0)))
554 (integer
555 (if (zerop thing)
556 most-positive-fixnum
557 (do ((result 0 (1+ result))
558 (num thing (ash num -1)))
559 ((logbitp 0 num) result))))
560 (cast
561 (count-low-order-zeros (cast-value thing)))
563 0)))
565 (deftransform / ((numerator denominator) (integer integer))
566 "convert x/2^k to shift"
567 (unless (constant-lvar-p denominator)
568 (give-up-ir1-transform))
569 (let* ((denominator (lvar-value denominator))
570 (bits (1- (integer-length denominator))))
571 (unless (and (> denominator 0) (= (ash 1 bits) denominator))
572 (give-up-ir1-transform))
573 (let ((alignment (count-low-order-zeros numerator)))
574 (unless (>= alignment bits)
575 (give-up-ir1-transform))
576 `(ash numerator ,(- bits)))))
578 (deftransform ash ((value amount))
579 (let ((value-node (lvar-uses value)))
580 (unless (combination-p value-node)
581 (give-up-ir1-transform))
582 (let ((inside-fun-name (lvar-fun-name (combination-fun value-node))))
583 (multiple-value-bind (prototype width)
584 (modular-version-info inside-fun-name :untagged nil)
585 (unless (eq (or prototype inside-fun-name) 'ash)
586 (give-up-ir1-transform))
587 (when (and width (not (constant-lvar-p amount)))
588 (give-up-ir1-transform))
589 (let ((inside-args (combination-args value-node)))
590 (unless (= (length inside-args) 2)
591 (give-up-ir1-transform))
592 (let ((inside-amount (second inside-args)))
593 (unless (and (constant-lvar-p inside-amount)
594 (not (minusp (lvar-value inside-amount))))
595 (give-up-ir1-transform)))
596 (splice-fun-args value inside-fun-name 2)
597 (if width
598 `(lambda (value amount1 amount2)
599 (logand (ash value (+ amount1 amount2))
600 ,(1- (ash 1 (+ width (lvar-value amount))))))
601 `(lambda (value amount1 amount2)
602 (ash value (+ amount1 amount2)))))))))
604 ;;;; ALIEN-FUNCALL support
606 (deftransform alien-funcall ((function &rest args)
607 ((alien (* t)) &rest *) *
608 :important t)
609 (let ((names (make-gensym-list (length args))))
610 (/noshow "entering first DEFTRANSFORM ALIEN-FUNCALL" function args)
611 `(lambda (function ,@names)
612 (alien-funcall (deref function) ,@names))))
614 #-sb-xc-host
615 (defun find-saved-fp-and-pc (fp)
616 (dolist (x *saved-fp-and-pcs*)
617 (declare (type (simple-array word (2)) x))
618 (when (#!+stack-grows-downward-not-upward
619 sap>
620 #!-stack-grows-downward-not-upward
621 sap<
622 (int-sap (aref x 0)) fp)
623 (return (values (int-sap (aref x 0)) (int-sap (aref x 1)))))))
625 (deftransform alien-funcall ((function &rest args) * * :node node :important t)
626 (let ((type (lvar-type function)))
627 (unless (alien-type-type-p type)
628 (give-up-ir1-transform "can't tell function type at compile time"))
629 (/noshow "entering second DEFTRANSFORM ALIEN-FUNCALL" function)
630 (let ((alien-type (alien-type-type-alien-type type)))
631 (unless (alien-fun-type-p alien-type)
632 (give-up-ir1-transform))
633 (let ((arg-types (alien-fun-type-arg-types alien-type)))
634 (unless (= (length args) (length arg-types))
635 (abort-ir1-transform
636 "wrong number of arguments; expected ~W, got ~W"
637 (length arg-types)
638 (length args)))
639 (collect ((params) (deports))
640 (dolist (arg-type arg-types)
641 (let ((param (gensym)))
642 (params param)
643 (deports `(deport ,param ',arg-type))))
644 ;; Build BODY from the inside out.
645 (let ((return-type (alien-fun-type-result-type alien-type))
646 ;; Innermost, we DEPORT the parameters (e.g. by taking SAPs
647 ;; to them) and do the call.
648 (body `(%alien-funcall (deport function ',alien-type)
649 ',alien-type
650 ,@(deports))))
651 ;; Wrap that in a WITH-PINNED-OBJECTS to ensure the values
652 ;; the SAPs are taken for won't be moved by the GC. (If
653 ;; needed: some alien types won't need it).
654 (setf body `(maybe-with-pinned-objects ,(params) ,arg-types
655 ,body))
656 ;; Around that handle any memory allocation that's needed.
657 ;; Mostly the DEPORT-ALLOC alien-type-methods are just an
658 ;; identity operation, but for example for deporting a
659 ;; Unicode string we need to convert the string into an
660 ;; octet array. This step needs to be done before the pinning
661 ;; to ensure we pin the right objects, so it can't be combined
662 ;; with the deporting.
663 ;; -- JES 2006-03-16
664 (loop for param in (params)
665 for arg-type in arg-types
666 do (setf body
667 `(let ((,param (deport-alloc ,param ',arg-type)))
668 ,body)))
669 (if (alien-values-type-p return-type)
670 (collect ((temps) (results))
671 (dolist (type (alien-values-type-values return-type))
672 (let ((temp (gensym)))
673 (temps temp)
674 (results `(naturalize ,temp ',type))))
675 (setf body
676 `(multiple-value-bind ,(temps) ,body
677 (values ,@(results)))))
678 (setf body `(naturalize ,body ',return-type)))
679 ;; Remember this frame to make sure that we can get back
680 ;; to it later regardless of how the foreign stack looks
681 ;; like.
682 #!+c-stack-is-control-stack
683 (when (policy node (= 3 alien-funcall-saves-fp-and-pc))
684 (setf body `(invoke-with-saved-fp-and-pc (lambda () ,body))))
685 (/noshow "returning from DEFTRANSFORM ALIEN-FUNCALL" (params) body)
686 `(lambda (function ,@(params))
687 (declare (optimize (let-conversion 3)))
688 ,body)))))))
690 (defoptimizer (%alien-funcall derive-type) ((function type &rest args))
691 (declare (ignore function args))
692 (unless (constant-lvar-p type)
693 (error "Something is broken."))
694 (let ((type (lvar-value type)))
695 (unless (alien-fun-type-p type)
696 (error "Something is broken."))
697 (values-specifier-type
698 (compute-alien-rep-type
699 (alien-fun-type-result-type type)
700 :result))))
702 (defoptimizer (%alien-funcall ltn-annotate)
703 ((function type &rest args) node ltn-policy)
704 (declare (ignore type ltn-policy))
705 (setf (basic-combination-info node) :funny)
706 (setf (node-tail-p node) nil)
707 (annotate-ordinary-lvar function)
708 (dolist (arg args)
709 (annotate-ordinary-lvar arg)))
711 ;;; We support both the stdcall and cdecl calling conventions on win32 by
712 ;;; resetting ESP after the foreign function returns. This way it works
713 ;;; correctly whether the party that is supposed to pop arguments from
714 ;;; the stack is the caller (cdecl) or the callee (stdcall).
715 (defoptimizer (%alien-funcall ir2-convert)
716 ((function type &rest args) call block)
717 (let ((type (if (constant-lvar-p type)
718 (lvar-value type)
719 (error "Something is broken.")))
720 (lvar (node-lvar call))
721 ;; KLUDGE: On ARM systems our register pressure is so high
722 ;; that if we process register args before stack args we end
723 ;; up with all of our non-descriptor regs either
724 ;; component-live (NFP) or wired (everything else) and can't
725 ;; actually process the stack args. Processing the arguments
726 ;; in reverse order here doesn't change the semantics, but we
727 ;; deal with all of the stack arguments before the wired
728 ;; register arguments become live.
729 (args #!-arm args #!+arm (reverse args))
730 #!+c-stack-is-control-stack
731 (stack-pointer (make-stack-pointer-tn)))
732 (multiple-value-bind (nsp stack-frame-size arg-tns result-tns)
733 (make-call-out-tns type)
734 #!+x86
735 (vop set-fpu-word-for-c call block)
736 ;; Save the stack pointer, it will get aligned and subtracting
737 ;; the size will not restore the original value, and some
738 ;; things, like SB-C::CALL-VARIABLE, use the stack pointer to
739 ;; calculate the number of saved values.
740 ;; See alien.impure.lisp/:stack-misalignment
741 #!+c-stack-is-control-stack
742 (vop current-stack-pointer call block stack-pointer)
743 (vop alloc-number-stack-space call block stack-frame-size nsp)
744 ;; KLUDGE: This is where the second half of the ARM
745 ;; register-pressure change lives (see above).
746 (dolist (tn #!-arm arg-tns #!+arm (reverse arg-tns))
747 ;; On PPC, TN might be a list. This is used to indicate
748 ;; something special needs to happen. See below.
750 ;; FIXME: We should implement something better than this.
751 (let* ((first-tn (if (listp tn) (car tn) tn))
752 (arg (pop args))
753 (sc (tn-sc first-tn))
754 (scn (sc-number sc))
755 (move-arg-vops (svref (sc-move-arg-vops sc) scn)))
756 (aver arg)
757 (unless (= (length move-arg-vops) 1)
758 (error "no unique move-arg-vop for moves in SC ~S" (sc-name sc)))
759 #!+(or x86 x86-64) (emit-move-arg-template call
760 block
761 (first move-arg-vops)
762 (lvar-tn call block arg)
764 first-tn)
765 #!-(or x86 x86-64)
766 (cond
767 #!+arm-softfp
768 ((and (proper-list-of-length-p tn 3)
769 (symbolp (third tn)))
770 (emit-template call block
771 (template-or-lose (third tn))
772 (reference-tn (lvar-tn call block arg) nil)
773 (reference-tn-list (butlast tn) t)))
775 (let ((temp-tn (make-representation-tn
776 (tn-primitive-type first-tn) scn)))
777 (emit-move call
778 block
779 (lvar-tn call block arg)
780 temp-tn)
781 (emit-move-arg-template call
782 block
783 (first move-arg-vops)
784 temp-tn
786 first-tn))))
787 #!+(and ppc darwin)
788 (when (listp tn)
789 ;; This means that we have a float arg that we need to
790 ;; also copy to some int regs. The list contains the TN
791 ;; for the float as well as the TNs to use for the int
792 ;; arg.
793 (destructuring-bind (float-tn i1-tn &optional i2-tn)
795 (if i2-tn
796 (vop sb!vm::move-double-to-int-arg call block
797 float-tn i1-tn i2-tn)
798 (vop sb!vm::move-single-to-int-arg call block
799 float-tn i1-tn))))))
800 (aver (null args))
801 (let ((result-tns (ensure-list result-tns)))
802 (vop* call-out call block
803 ((lvar-tn call block function)
804 (reference-tn-list (remove-if-not #'tn-p (flatten-list arg-tns)) nil))
805 ((reference-tn-list (remove-if-not #'tn-p result-tns) t)))
806 #!-c-stack-is-control-stack
807 (vop dealloc-number-stack-space call block stack-frame-size)
808 #!+c-stack-is-control-stack
809 (vop reset-stack-pointer call block stack-pointer)
810 #!+x86
811 (vop set-fpu-word-for-lisp call block)
812 (cond
813 #!+arm-softfp
814 ((and lvar
815 (proper-list-of-length-p result-tns 3)
816 (symbolp (third result-tns)))
817 (emit-template call block
818 (template-or-lose (third result-tns))
819 (reference-tn-list (butlast result-tns) nil)
820 (reference-tn (car (ir2-lvar-locs (lvar-info lvar))) t)))
822 (move-lvar-result call block result-tns lvar)))))))