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