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