0.9.8.7:
[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 #!+win32
72 (defknown alien-funcall-stdcall (alien-value &rest *) *
73   (any recursive))
74 \f
75 ;;;; cosmetic transforms
76
77 (deftransform slot ((object slot)
78                     ((alien (* t)) symbol))
79   '(slot (deref object) slot))
80
81 (deftransform %set-slot ((object slot value)
82                          ((alien (* t)) symbol t))
83   '(%set-slot (deref object) slot value))
84
85 (deftransform %slot-addr ((object slot)
86                           ((alien (* t)) symbol))
87   '(%slot-addr (deref object) slot))
88 \f
89 ;;;; SLOT support
90
91 (defun find-slot-offset-and-type (alien slot)
92   (unless (constant-lvar-p slot)
93     (give-up-ir1-transform
94      "The slot is not constant, so access cannot be open coded."))
95   (let ((type (lvar-type alien)))
96     (unless (alien-type-type-p type)
97       (give-up-ir1-transform))
98     (let ((alien-type (alien-type-type-alien-type type)))
99       (unless (alien-record-type-p alien-type)
100         (give-up-ir1-transform))
101       (let* ((slot-name (lvar-value slot))
102              (field (find slot-name (alien-record-type-fields alien-type)
103                           :key #'alien-record-field-name)))
104         (unless field
105           (abort-ir1-transform "~S doesn't have a slot named ~S"
106                                alien
107                                slot-name))
108         (values (alien-record-field-offset field)
109                 (alien-record-field-type field))))))
110
111 #+nil ;; Shouldn't be necessary.
112 (defoptimizer (slot derive-type) ((alien slot))
113   (block nil
114     (catch 'give-up-ir1-transform
115       (multiple-value-bind (slot-offset slot-type)
116           (find-slot-offset-and-type alien slot)
117         (declare (ignore slot-offset))
118         (return (make-alien-type-type slot-type))))
119     *wild-type*))
120
121 (deftransform slot ((alien slot) * * :important t)
122   (multiple-value-bind (slot-offset slot-type)
123       (find-slot-offset-and-type alien slot)
124     `(extract-alien-value (alien-sap alien)
125                           ,slot-offset
126                           ',slot-type)))
127
128 #+nil ;; ### But what about coercions?
129 (defoptimizer (%set-slot derive-type) ((alien slot value))
130   (block nil
131     (catch 'give-up-ir1-transform
132       (multiple-value-bind (slot-offset slot-type)
133           (find-slot-offset-and-type alien slot)
134         (declare (ignore slot-offset))
135         (let ((type (make-alien-type-type slot-type)))
136           (assert-lvar-type value type)
137           (return type))))
138     *wild-type*))
139
140 (deftransform %set-slot ((alien slot value) * * :important t)
141   (multiple-value-bind (slot-offset slot-type)
142       (find-slot-offset-and-type alien slot)
143     `(deposit-alien-value (alien-sap alien)
144                           ,slot-offset
145                           ',slot-type
146                           value)))
147
148 (defoptimizer (%slot-addr derive-type) ((alien slot))
149   (block nil
150     (catch 'give-up-ir1-transform
151       (multiple-value-bind (slot-offset slot-type)
152           (find-slot-offset-and-type alien slot)
153         (declare (ignore slot-offset))
154         (return (make-alien-type-type
155                  (make-alien-pointer-type :to slot-type)))))
156     *wild-type*))
157
158 (deftransform %slot-addr ((alien slot) * * :important t)
159   (multiple-value-bind (slot-offset slot-type)
160       (find-slot-offset-and-type alien slot)
161     (/noshow "in DEFTRANSFORM %SLOT-ADDR, creating %SAP-ALIEN")
162     `(%sap-alien (sap+ (alien-sap alien) (/ ,slot-offset sb!vm:n-byte-bits))
163                  ',(make-alien-pointer-type :to slot-type))))
164 \f
165 ;;;; DEREF support
166
167 (defun find-deref-alien-type (alien)
168   (let ((alien-type (lvar-type alien)))
169     (unless (alien-type-type-p alien-type)
170       (give-up-ir1-transform))
171     (let ((alien-type (alien-type-type-alien-type alien-type)))
172       (if (alien-type-p alien-type)
173           alien-type
174           (give-up-ir1-transform)))))
175
176 (defun find-deref-element-type (alien)
177   (let ((alien-type (find-deref-alien-type alien)))
178     (typecase alien-type
179       (alien-pointer-type
180        (alien-pointer-type-to alien-type))
181       (alien-array-type
182        (alien-array-type-element-type alien-type))
183       (t
184        (give-up-ir1-transform)))))
185
186 (defun compute-deref-guts (alien indices)
187   (let ((alien-type (find-deref-alien-type alien)))
188     (typecase alien-type
189       (alien-pointer-type
190        (when (cdr indices)
191          (abort-ir1-transform "too many indices for pointer deref: ~W"
192                               (length indices)))
193        (let ((element-type (alien-pointer-type-to alien-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)))))
230       (t
231        (abort-ir1-transform "~S not either a pointer or array type."
232                             alien-type)))))
233
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*))
241
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        (extract-alien-value (alien-sap alien)
247                             ,offset-expr
248                             ',element-type))))
249
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*))
261
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        (deposit-alien-value (alien-sap alien)
267                             ,offset-expr
268                             ',element-type
269                             value))))
270
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*))
279
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)))))
287 \f
288 ;;;; support for aliens on the heap
289
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))))
296
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*))
305
306 (deftransform %heap-alien ((info) * * :important t)
307   (multiple-value-bind (sap type) (heap-alien-sap-and-type info)
308     `(extract-alien-value ,sap 0 ',type)))
309
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*))
320
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     `(deposit-alien-value ,sap 0 ',type value)))
324
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*))
332
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 ',type)))
337 \f
338 ;;;; support for local (stack or register) aliens
339
340 (deftransform make-local-alien ((info) * * :important t)
341   (unless (constant-lvar-p info)
342     (abort-ir1-transform "Local alien info isn't constant?"))
343   (let* ((info (lvar-value info))
344          (alien-type (local-alien-info-type info))
345          (bits (alien-type-bits alien-type)))
346     (unless bits
347       (abort-ir1-transform "unknown size: ~S" (unparse-alien-type alien-type)))
348     (/noshow "in DEFTRANSFORM MAKE-LOCAL-ALIEN" info)
349     (/noshow (local-alien-info-force-to-memory-p info))
350     (/noshow alien-type (unparse-alien-type alien-type) (alien-type-bits alien-type))
351     (if (local-alien-info-force-to-memory-p info)
352       #!+(or x86 x86-64) `(truly-the system-area-pointer
353                          (%primitive alloc-alien-stack-space
354                                      ,(ceiling (alien-type-bits alien-type)
355                                                sb!vm:n-byte-bits)))
356       #!-(or x86 x86-64) `(truly-the system-area-pointer
357                          (%primitive alloc-number-stack-space
358                                      ,(ceiling (alien-type-bits alien-type)
359                                                sb!vm:n-byte-bits)))
360       (let* ((alien-rep-type-spec (compute-alien-rep-type alien-type))
361              (alien-rep-type (specifier-type alien-rep-type-spec)))
362         (cond ((csubtypep (specifier-type 'system-area-pointer)
363                           alien-rep-type)
364                '(int-sap 0))
365               ((ctypep 0 alien-rep-type) 0)
366               ((ctypep 0.0f0 alien-rep-type) 0.0f0)
367               ((ctypep 0.0d0 alien-rep-type) 0.0d0)
368               (t
369                (compiler-error
370                 "Aliens of type ~S cannot be represented immediately."
371                 (unparse-alien-type alien-type))))))))
372
373 (deftransform note-local-alien-type ((info var) * * :important t)
374   ;; FIXME: This test and error occur about a zillion times. They
375   ;; could be factored into a function.
376   (unless (constant-lvar-p info)
377     (abort-ir1-transform "Local alien info isn't constant?"))
378   (let ((info (lvar-value info)))
379     (/noshow "in DEFTRANSFORM NOTE-LOCAL-ALIEN-TYPE" info)
380     (/noshow (local-alien-info-force-to-memory-p info))
381     (unless (local-alien-info-force-to-memory-p info)
382       (let ((var-node (lvar-uses var)))
383         (/noshow var-node (ref-p var-node))
384         (when (ref-p var-node)
385           (propagate-to-refs (ref-leaf var-node)
386                              (specifier-type
387                               (compute-alien-rep-type
388                                (local-alien-info-type info))))))))
389   nil)
390
391 (deftransform local-alien ((info var) * * :important t)
392   (unless (constant-lvar-p info)
393     (abort-ir1-transform "Local alien info isn't constant?"))
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         `(extract-alien-value var 0 ',alien-type)
400         `(naturalize var ',alien-type))))
401
402 (deftransform %local-alien-forced-to-memory-p ((info) * * :important t)
403   (unless (constant-lvar-p info)
404     (abort-ir1-transform "Local alien info isn't constant?"))
405   (let ((info (lvar-value info)))
406     (local-alien-info-force-to-memory-p info)))
407
408 (deftransform %set-local-alien ((info var value) * * :important t)
409   (unless (constant-lvar-p info)
410     (abort-ir1-transform "Local alien info isn't constant?"))
411   (let* ((info (lvar-value info))
412          (alien-type (local-alien-info-type info)))
413     (if (local-alien-info-force-to-memory-p info)
414         `(deposit-alien-value var 0 ',alien-type value)
415         '(error "This should be eliminated as dead code."))))
416
417 (defoptimizer (%local-alien-addr derive-type) ((info var))
418   (if (constant-lvar-p info)
419       (let* ((info (lvar-value info))
420              (alien-type (local-alien-info-type info)))
421         (make-alien-type-type (make-alien-pointer-type :to alien-type)))
422       *wild-type*))
423
424 (deftransform %local-alien-addr ((info var) * * :important t)
425   (unless (constant-lvar-p info)
426     (abort-ir1-transform "Local alien info isn't constant?"))
427   (let* ((info (lvar-value info))
428          (alien-type (local-alien-info-type info)))
429     (/noshow "in DEFTRANSFORM %LOCAL-ALIEN-ADDR, creating %SAP-ALIEN")
430     (if (local-alien-info-force-to-memory-p info)
431         `(%sap-alien var ',(make-alien-pointer-type :to alien-type))
432         (error "This shouldn't happen."))))
433
434 (deftransform dispose-local-alien ((info var) * * :important t)
435   (unless (constant-lvar-p info)
436     (abort-ir1-transform "Local alien info isn't constant?"))
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       #!+(or x86 x86-64) `(%primitive dealloc-alien-stack-space
441                           ,(ceiling (alien-type-bits alien-type)
442                                     sb!vm:n-byte-bits))
443       #!-(or x86 x86-64) `(%primitive dealloc-number-stack-space
444                           ,(ceiling (alien-type-bits alien-type)
445                                     sb!vm:n-byte-bits))
446       nil)))
447 \f
448 ;;;; %CAST
449
450 (defoptimizer (%cast derive-type) ((alien type))
451   (or (when (constant-lvar-p type)
452         (let ((alien-type (lvar-value type)))
453           (when (alien-type-p alien-type)
454             (make-alien-type-type alien-type))))
455       *wild-type*))
456
457 (deftransform %cast ((alien target-type) * * :important t)
458   (unless (constant-lvar-p target-type)
459     (give-up-ir1-transform
460      "The alien type is not constant, so access cannot be open coded."))
461   (let ((target-type (lvar-value target-type)))
462     (cond ((or (alien-pointer-type-p target-type)
463                (alien-array-type-p target-type)
464                (alien-fun-type-p target-type))
465            `(naturalize (alien-sap alien) ',target-type))
466           (t
467            (abort-ir1-transform "cannot cast to alien type ~S" target-type)))))
468 \f
469 ;;;; ALIEN-SAP, %SAP-ALIEN, %ADDR, etc.
470
471 (deftransform alien-sap ((alien) * * :important t)
472   (let ((alien-node (lvar-uses alien)))
473     (typecase alien-node
474       (combination
475        (extract-fun-args alien '%sap-alien 2)
476        '(lambda (sap type)
477           (declare (ignore type))
478           sap))
479       (t
480        (give-up-ir1-transform)))))
481
482 (defoptimizer (%sap-alien derive-type) ((sap type))
483   (declare (ignore sap))
484   (if (constant-lvar-p type)
485       (make-alien-type-type (lvar-value type))
486       *wild-type*))
487
488 (deftransform %sap-alien ((sap type) * * :important t)
489   (give-up-ir1-transform
490    ;; FIXME: The hardcoded newline here causes more-than-usually
491    ;; screwed-up formatting of the optimization note output.
492    "could not optimize away %SAP-ALIEN: forced to do runtime ~@
493     allocation of alien-value structure"))
494 \f
495 ;;;; NATURALIZE/DEPORT/EXTRACT/DEPOSIT magic
496
497 (flet ((%computed-lambda (compute-lambda type)
498          (declare (type function compute-lambda))
499          (unless (constant-lvar-p type)
500            (give-up-ir1-transform
501             "The type is not constant at compile time; can't open code."))
502          (handler-case
503              (let ((result (funcall compute-lambda (lvar-value type))))
504                (/noshow "in %COMPUTED-LAMBDA" (lvar-value type) result)
505                result)
506            (error (condition)
507                   (compiler-error "~A" condition)))))
508   (deftransform naturalize ((object type) * * :important t)
509     (%computed-lambda #'compute-naturalize-lambda type))
510   (deftransform deport ((alien type) * * :important t)
511     (%computed-lambda #'compute-deport-lambda type))
512   (deftransform extract-alien-value ((sap offset type) * * :important t)
513     (%computed-lambda #'compute-extract-lambda type))
514   (deftransform deposit-alien-value ((sap offset type value) * * :important t)
515     (%computed-lambda #'compute-deposit-lambda type)))
516 \f
517 ;;;; a hack to clean up divisions
518
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 :unsigned) 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)))))
535        (*
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))
549                          0)
550                     0))
551               0)))
552        (t
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)))
562     (t
563      0)))
564
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)))))
577
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 :unsigned)
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           (extract-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)))))))))
603 \f
604 ;;;; ALIEN-FUNCALL support
605
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))))
613
614 (deftransform alien-funcall ((function &rest args) * * :important t)
615   (let ((type (lvar-type function)))
616     (unless (alien-type-type-p type)
617       (give-up-ir1-transform "can't tell function type at compile time"))
618     (/noshow "entering second DEFTRANSFORM ALIEN-FUNCALL" function)
619     (let ((alien-type (alien-type-type-alien-type type)))
620       (unless (alien-fun-type-p alien-type)
621         (give-up-ir1-transform))
622       (let ((arg-types (alien-fun-type-arg-types alien-type)))
623         (unless (= (length args) (length arg-types))
624           (abort-ir1-transform
625            "wrong number of arguments; expected ~W, got ~W"
626            (length arg-types)
627            (length args)))
628         (collect ((params) (deports))
629           (dolist (arg-type arg-types)
630             (let ((param (gensym)))
631               (params param)
632               (deports `(deport ,param ',arg-type))))
633           (let ((return-type (alien-fun-type-result-type alien-type))
634                 (body `(%alien-funcall (deport function ',alien-type)
635                                        ',alien-type
636                                        ,@(deports))))
637             (if (alien-values-type-p return-type)
638                 (collect ((temps) (results))
639                   (dolist (type (alien-values-type-values return-type))
640                     (let ((temp (gensym)))
641                       (temps temp)
642                       (results `(naturalize ,temp ',type))))
643                   (setf body
644                         `(multiple-value-bind ,(temps) ,body
645                            (values ,@(results)))))
646                 (setf body `(naturalize ,body ',return-type)))
647             (/noshow "returning from DEFTRANSFORM ALIEN-FUNCALL" (params) body)
648             `(lambda (function ,@(params))
649                ,body)))))))
650
651 (defoptimizer (%alien-funcall derive-type) ((function type &rest args))
652   (declare (ignore function args))
653   (unless (constant-lvar-p type)
654     (error "Something is broken."))
655   (let ((type (lvar-value type)))
656     (unless (alien-fun-type-p type)
657       (error "Something is broken."))
658     (values-specifier-type
659      (compute-alien-rep-type
660       (alien-fun-type-result-type type)))))
661
662 (defoptimizer (%alien-funcall ltn-annotate)
663               ((function type &rest args) node ltn-policy)
664   (setf (basic-combination-info node) :funny)
665   (setf (node-tail-p node) nil)
666   (annotate-ordinary-lvar function)
667   (dolist (arg args)
668     (annotate-ordinary-lvar arg)))
669
670 (defoptimizer (%alien-funcall ir2-convert)
671               ((function type &rest args) call block)
672   (let ((type (if (constant-lvar-p type)
673                   (lvar-value type)
674                   (error "Something is broken.")))
675         (lvar (node-lvar call))
676         (args args))
677     (multiple-value-bind (nsp stack-frame-size arg-tns result-tns)
678         (make-call-out-tns type)
679       (vop alloc-number-stack-space call block stack-frame-size nsp)
680       (dolist (tn arg-tns)
681         (let* ((arg (pop args))
682                (sc (tn-sc tn))
683                (scn (sc-number sc))
684                #!-(or x86 x86-64) (temp-tn (make-representation-tn (tn-primitive-type tn)
685                                                        scn))
686                (move-arg-vops (svref (sc-move-arg-vops sc) scn)))
687           (aver arg)
688           (unless (= (length move-arg-vops) 1)
689             (error "no unique move-arg-vop for moves in SC ~S" (sc-name sc)))
690           #!+(or x86 x86-64) (emit-move-arg-template call
691                                          block
692                                          (first move-arg-vops)
693                                          (lvar-tn call block arg)
694                                          nsp
695                                          tn)
696           #!-(or x86 x86-64) (progn
697                    (emit-move call
698                               block
699                               (lvar-tn call block arg)
700                               temp-tn)
701                    (emit-move-arg-template call
702                                            block
703                                            (first move-arg-vops)
704                                            temp-tn
705                                            nsp
706                                            tn))))
707       (aver (null args))
708       (unless (listp result-tns)
709         (setf result-tns (list result-tns)))
710       (vop* call-out call block
711             ((lvar-tn call block function)
712              (reference-tn-list arg-tns nil))
713             ((reference-tn-list result-tns t)))
714       (vop dealloc-number-stack-space call block stack-frame-size)
715       (move-lvar-result call block result-tns lvar))))
716 \f
717 ;;;; ALIEN-FUNCALL-STDCALL support
718
719 #!+win32
720 (deftransform alien-funcall-stdcall ((function &rest args)
721                              ((alien (* t)) &rest *) *
722                              :important t)
723   (let ((names (make-gensym-list (length args))))
724     (/noshow "entering first DEFTRANSFORM ALIEN-FUNCALL-STDCALL" function args)
725     `(lambda (function ,@names)
726        (alien-funcall-stdcall (deref function) ,@names))))
727
728 #!+win32
729 (deftransform alien-funcall-stdcall ((function &rest args) * * :important t)
730   (let ((type (lvar-type function)))
731     (unless (alien-type-type-p type)
732       (give-up-ir1-transform "can't tell function type at compile time"))
733     (/noshow "entering second DEFTRANSFORM ALIEN-FUNCALL-STDCALL" function)
734     (let ((alien-type (alien-type-type-alien-type type)))
735       (unless (alien-fun-type-p alien-type)
736         (give-up-ir1-transform))
737       (let ((arg-types (alien-fun-type-arg-types alien-type)))
738         (unless (= (length args) (length arg-types))
739           (abort-ir1-transform
740            "wrong number of arguments; expected ~W, got ~W"
741            (length arg-types)
742            (length args)))
743         (collect ((params) (deports))
744           (dolist (arg-type arg-types)
745             (let ((param (gensym)))
746               (params param)
747               (deports `(deport ,param ',arg-type))))
748           (let ((return-type (alien-fun-type-result-type alien-type))
749                 (body `(%alien-funcall-stdcall (deport function ',alien-type)
750                                        ',alien-type
751                                        ,@(deports))))
752             (if (alien-values-type-p return-type)
753                 (collect ((temps) (results))
754                   (dolist (type (alien-values-type-values return-type))
755                     (let ((temp (gensym)))
756                       (temps temp)
757                       (results `(naturalize ,temp ',type))))
758                   (setf body
759                         `(multiple-value-bind ,(temps) ,body
760                            (values ,@(results)))))
761                 (setf body `(naturalize ,body ',return-type)))
762             (/noshow "returning from DEFTRANSFORM ALIEN-FUNCALL-STDCALL" (params) body)
763             `(lambda (function ,@(params))
764                ,body)))))))
765
766 #!+win32
767 (defoptimizer (%alien-funcall-stdcall derive-type) ((function type &rest args))
768   (declare (ignore function args))
769   (unless (constant-lvar-p type)
770     (error "Something is broken."))
771   (let ((type (lvar-value type)))
772     (unless (alien-fun-type-p type)
773       (error "Something is broken."))
774     (values-specifier-type
775      (compute-alien-rep-type
776       (alien-fun-type-result-type type)))))
777
778 #!+win32
779 (defoptimizer (%alien-funcall-stdcall ltn-annotate)
780               ((function type &rest args) node ltn-policy)
781   (setf (basic-combination-info node) :funny)
782   (setf (node-tail-p node) nil)
783   (annotate-ordinary-lvar function)
784   (dolist (arg args)
785     (annotate-ordinary-lvar arg)))
786
787 #!+win32
788 (defoptimizer (%alien-funcall-stdcall ir2-convert)
789               ((function type &rest args) call block)
790   (let ((type (if (constant-lvar-p type)
791                   (lvar-value type)
792                   (error "Something is broken.")))
793         (lvar (node-lvar call))
794         (args args))
795     (multiple-value-bind (nsp stack-frame-size arg-tns result-tns)
796         (make-call-out-tns type)
797       (vop alloc-number-stack-space call block stack-frame-size nsp)
798       (dolist (tn arg-tns)
799         (let* ((arg (pop args))
800                (sc (tn-sc tn))
801                (scn (sc-number sc))
802                #!-x86 (temp-tn (make-representation-tn (tn-primitive-type tn)
803                                                        scn))
804                (move-arg-vops (svref (sc-move-arg-vops sc) scn)))
805           (aver arg)
806           (unless (= (length move-arg-vops) 1)
807             (error "no unique move-arg-vop for moves in SC ~S" (sc-name sc)))
808           #!+x86 (emit-move-arg-template call
809                                          block
810                                          (first move-arg-vops)
811                                          (lvar-tn call block arg)
812                                          nsp
813                                          tn)
814           #!-x86 (progn
815                    (emit-move call
816                               block
817                               (lvar-tn call block arg)
818                               temp-tn)
819                    (emit-move-arg-template call
820                                            block
821                                            (first move-arg-vops)
822                                            temp-tn
823                                            nsp
824                                            tn))))
825       (aver (null args))
826       (unless (listp result-tns)
827         (setf result-tns (list result-tns)))
828       (vop* call-out call block
829             ((lvar-tn call block function)
830              (reference-tn-list arg-tns nil))
831             ((reference-tn-list result-tns t)))
832       ;; This is the stdcall magic: Callee clears args.
833       #+nil (vop dealloc-number-stack-space call block stack-frame-size)
834       (move-lvar-result call block result-tns lvar))))