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