0.7.3.8:
[sbcl.git] / src / compiler / generic / vm-tran.lisp
1 ;;;; implementation-dependent transforms
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
14 ;;; We need to define these predicates, since the TYPEP source
15 ;;; transform picks whichever predicate was defined last when there
16 ;;; are multiple predicates for equivalent types.
17 (define-source-transform short-float-p (x) `(single-float-p ,x))
18 #!-long-float
19 (define-source-transform long-float-p (x) `(double-float-p ,x))
20
21 (define-source-transform compiled-function-p (x)
22   `(functionp ,x))
23
24 (define-source-transform char-int (x)
25   `(char-code ,x))
26
27 (deftransform abs ((x) (rational))
28   '(if (< x 0) (- x) x))
29
30 ;;; The layout is stored in slot 0.
31 (define-source-transform %instance-layout (x)
32   `(truly-the layout (%instance-ref ,x 0)))
33 (define-source-transform %set-instance-layout (x val)
34   `(%instance-set ,x 0 (the layout ,val)))
35 \f
36 ;;;; character support
37
38 ;;; In our implementation there are really only BASE-CHARs.
39 (define-source-transform characterp (obj)
40   `(base-char-p ,obj))
41 \f
42 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
43
44 (deftransform hairy-data-vector-ref ((array index) (array t) * :important t)
45   "avoid runtime dispatch on array element type"
46   (let ((element-ctype (extract-upgraded-element-type array)))
47     (declare (type ctype element-ctype))
48     (when (eq *wild-type* element-ctype)
49       (give-up-ir1-transform
50        "Upgraded element type of array is not known at compile time."))
51     ;; (The expansion here is basically a degenerate case of
52     ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
53     ;; macro, and macros aren't expanded in transform output, we have
54     ;; to hand-expand it ourselves.)
55     (let ((element-type-specifier (type-specifier element-ctype)))
56       `(multiple-value-bind (array index)
57            ;; FIXME: All this noise should move into a
58            ;; %DATA-VECTOR-AND-INDEX function, and there should be
59            ;; DEFTRANSFORMs for %DATA-VECTOR-AND-INDEX to optimize the
60            ;; function call away when the array is known to be simple,
61            ;; and to specialize to
62            ;; %DATA-VECTOR-AND-INDEX-IN-VECTOR-CASE when the array is
63            ;; known to have only one dimension.
64            (if (array-header-p array)
65                (%with-array-data array index nil)
66                (let ((array array))
67                  (declare (type (simple-array ,element-type-specifier 1)
68                                 array))
69                  (%check-bound array 0 index)
70                  (values array index)))
71          (declare (type (simple-array ,element-type-specifier 1) array))
72          (data-vector-ref array index)))))
73
74 (deftransform data-vector-ref ((array index)
75                                (simple-array t))
76   (let ((array-type (continuation-type array)))
77     (unless (array-type-p array-type)
78       (give-up-ir1-transform))
79     (let ((dims (array-type-dimensions array-type)))
80       (when (or (atom dims) (= (length dims) 1))
81         (give-up-ir1-transform))
82       (let ((el-type (array-type-specialized-element-type array-type))
83             (total-size (if (member '* dims)
84                             '*
85                             (reduce #'* dims))))
86         `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
87                                                    (,total-size))
88                                      (%array-data-vector array))
89                           index)))))
90
91 (deftransform hairy-data-vector-set ((array index new-value)
92                                      (array t t)
93                                      *
94                                      :important t)
95   "avoid runtime dispatch on array element type"
96   (let ((element-ctype (extract-upgraded-element-type array)))
97     (declare (type ctype element-ctype))
98     (when (eq *wild-type* element-ctype)
99       (give-up-ir1-transform
100        "Upgraded element type of array is not known at compile time."))
101     (let ((element-type-specifier (type-specifier element-ctype)))
102       `(multiple-value-bind (array index)
103            ;; FIXME: All this noise should move into a
104            ;; %DATA-VECTOR-AND-INDEX function, and there should be
105            ;; DEFTRANSFORMs for %DATA-VECTOR-AND-INDEX to optimize the
106            ;; function call away when the array is known to be simple,
107            ;; and to specialize to
108            ;; %DATA-VECTOR-AND-INDEX-IN-VECTOR-CASE when the array is
109            ;; known to have only one dimension.
110            (if (array-header-p array)
111                (%with-array-data array index nil)
112                (let ((array array))
113                  (declare (type (simple-array ,element-type-specifier 1)
114                                 array))
115                  (%check-bound array 0 index)
116                  (values array index)))
117          (data-vector-set (truly-the (simple-array ,element-type-specifier 1)
118                                      array)
119                           index
120                           new-value)))))
121
122 (deftransform data-vector-set ((array index new-value)
123                                (simple-array t t))
124   (let ((array-type (continuation-type array)))
125     (unless (array-type-p array-type)
126       (give-up-ir1-transform))
127     (let ((dims (array-type-dimensions array-type)))
128       (when (or (atom dims) (= (length dims) 1))
129         (give-up-ir1-transform))
130       (let ((el-type (array-type-specialized-element-type array-type))
131             (total-size (if (member '* dims)
132                             '*
133                             (reduce #'* dims))))
134         `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
135                                                    (,total-size))
136                                      (%array-data-vector array))
137                           index
138                           new-value)))))
139
140 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
141 ;;;
142 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
143 ;;; we fix them or should we delete them? (Perhaps these definitions
144 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
145 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
146 #+nil
147 (macrolet
148     ((frob (type bits)
149        (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
150          `(progn
151             (deftransform data-vector-ref ((vector index)
152                                            (,type *))
153               `(multiple-value-bind (word bit)
154                    (floor index ,',elements-per-word)
155                  (ldb ,(ecase sb!vm:target-byte-order
156                          (:little-endian '(byte ,bits (* bit ,bits)))
157                          (:big-endian '(byte ,bits (- sb!vm:n-word-bits
158                                                       (* (1+ bit) ,bits)))))
159                       (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
160             (deftransform data-vector-set ((vector index new-value)
161                                            (,type * *))
162               `(multiple-value-bind (word bit)
163                    (floor index ,',elements-per-word)
164                  (setf (ldb ,(ecase sb!vm:target-byte-order
165                                (:little-endian '(byte ,bits (* bit ,bits)))
166                                (:big-endian
167                                 '(byte ,bits (- sb!vm:n-word-bits
168                                                 (* (1+ bit) ,bits)))))
169                             (%raw-bits vector (+ word sb!vm:vector-data-offset)))
170                        new-value)))))))
171   (frob simple-bit-vector 1)
172   (frob (simple-array (unsigned-byte 2) (*)) 2)
173   (frob (simple-array (unsigned-byte 4) (*)) 4))
174 \f
175 ;;;; BIT-VECTOR hackery
176
177 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
178 ;;; loop that does 32 bits at a time.
179 ;;;
180 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
181 ;;; be a function call instead.
182 (macrolet ((def (bitfun wordfun)
183              `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
184                                      (simple-bit-vector
185                                       simple-bit-vector
186                                       simple-bit-vector)
187                                      *
188                                      :node node :policy (>= speed space))
189                 `(progn
190                    ,@(unless (policy node (zerop safety))
191                              '((unless (= (length bit-array-1)
192                                           (length bit-array-2)
193                                           (length result-bit-array))
194                                  (error "Argument and/or result bit arrays are not the same length:~
195                          ~%  ~S~%  ~S  ~%  ~S"
196                                         bit-array-1
197                                         bit-array-2
198                                         result-bit-array))))
199                   (let ((length (length result-bit-array)))
200                     (if (= length 0)
201                         ;; We avoid doing anything to 0-length
202                         ;; bit-vectors, or rather, the memory that
203                         ;; follows them. Other divisible-by-32 cases
204                         ;; are handled by the (1- length), below.
205                         ;; CSR, 2002-04-24
206                         result-bit-array
207                         (do ((index sb!vm:vector-data-offset (1+ index))
208                              (end-1 (+ sb!vm:vector-data-offset
209                                        ;; bit-vectors of length 1-32
210                                        ;; need precisely one (SETF
211                                        ;; %RAW-BITS), done here in the
212                                        ;; epilogue. - CSR, 2002-04-24
213                                        (truncate (truly-the index (1- length))
214                                                  sb!vm:n-word-bits))))
215                             ((= index end-1)
216                              (setf (%raw-bits result-bit-array index)
217                                    (,',wordfun (%raw-bits bit-array-1 index)
218                                                (%raw-bits bit-array-2 index)))
219                              result-bit-array)
220                           (declare (optimize (speed 3) (safety 0))
221                                    (type index index end-1))
222                           (setf (%raw-bits result-bit-array index)
223                                 (,',wordfun (%raw-bits bit-array-1 index)
224                                             (%raw-bits bit-array-2 index))))))))))
225  (def bit-and 32bit-logical-and)
226  (def bit-ior 32bit-logical-or)
227  (def bit-xor 32bit-logical-xor)
228  (def bit-eqv 32bit-logical-eqv)
229  (def bit-nand 32bit-logical-nand)
230  (def bit-nor 32bit-logical-nor)
231  (def bit-andc1 32bit-logical-andc1)
232  (def bit-andc2 32bit-logical-andc2)
233  (def bit-orc1 32bit-logical-orc1)
234  (def bit-orc2 32bit-logical-orc2))
235
236 (deftransform bit-not
237               ((bit-array result-bit-array)
238                (simple-bit-vector simple-bit-vector) *
239                :node node :policy (>= speed space))
240   `(progn
241      ,@(unless (policy node (zerop safety))
242          '((unless (= (length bit-array)
243                       (length result-bit-array))
244              (error "Argument and result bit arrays are not the same length:~
245                      ~%  ~S~%  ~S"
246                     bit-array result-bit-array))))
247     (let ((length (length result-bit-array)))
248       (if (= length 0)
249           ;; We avoid doing anything to 0-length bit-vectors, or
250           ;; rather, the memory that follows them. Other
251           ;; divisible-by-32 cases are handled by the (1- length),
252           ;; below.  CSR, 2002-04-24
253           result-bit-array
254           (do ((index sb!vm:vector-data-offset (1+ index))
255                (end-1 (+ sb!vm:vector-data-offset
256                          ;; bit-vectors of length 1-32 need precisely
257                          ;; one (SETF %RAW-BITS), done here in the
258                          ;; epilogue. - CSR, 2002-04-24
259                          (truncate (truly-the index (1- length))
260                                    sb!vm:n-word-bits))))
261               ((= index end-1)
262                (setf (%raw-bits result-bit-array index)
263                      (32bit-logical-not (%raw-bits bit-array index)))
264                result-bit-array)
265             (declare (optimize (speed 3) (safety 0))
266                      (type index index end-1))
267             (setf (%raw-bits result-bit-array index)
268                   (32bit-logical-not (%raw-bits bit-array index))))))))
269 \f
270 ;;;; %BYTE-BLT
271
272 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
273 ;;; stuff (with all the associated bit-index cruft and overflow
274 ;;; issues) even for byte moves. In SBCL, we're converting to byte
275 ;;; moves as problems are discovered with the old code, and this is
276 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
277 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
278 ;;; ideal interface, though, and it probably deserves some thought.
279 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
280                          ((or (simple-unboxed-array (*)) system-area-pointer)
281                           index
282                           (or (simple-unboxed-array (*)) system-area-pointer)
283                           index
284                           index))
285   ;; FIXME: CMU CL had a hairier implementation of this (back when it
286   ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
287   ;; that it didn't work for large (>16M) values of SRC-START or
288   ;; DST-START. However, it might have been more efficient. In
289   ;; particular, I don't really know how much the foreign function
290   ;; call costs us here. My guess is that if the overhead is
291   ;; acceptable for SQRT and COS, it's acceptable here, but this
292   ;; should probably be checked. -- WHN
293   '(flet ((sapify (thing)
294             (etypecase thing
295               (system-area-pointer thing)
296               ;; FIXME: The code here rather relies on the simple
297               ;; unboxed array here having byte-sized entries. That
298               ;; should be asserted explicitly, I just haven't found
299               ;; a concise way of doing it. (It would be nice to
300               ;; declare it in the DEFKNOWN too.)
301               ((simple-unboxed-array (*)) (vector-sap thing)))))
302      (declare (inline sapify))
303      (without-gcing
304       (memmove (sap+ (sapify dst) dst-start)
305                (sap+ (sapify src) src-start)
306                (- dst-end dst-start)))
307      nil))
308 \f
309 ;;;; transforms for EQL of floating point values
310
311 (deftransform eql ((x y) (single-float single-float))
312   '(= (single-float-bits x) (single-float-bits y)))
313
314 (deftransform eql ((x y) (double-float double-float))
315   '(and (= (double-float-low-bits x) (double-float-low-bits y))
316         (= (double-float-high-bits x) (double-float-high-bits y))))
317