e28bc1328f9305dd2e76638f4fead1d5992c2d59
[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-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-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                   (do ((index sb!vm:vector-data-offset (1+ index))
200                        (end (+ sb!vm:vector-data-offset
201                                (truncate (the index
202                                            (+ (length bit-array-1)
203                                               sb!vm:n-word-bits -1))
204                                          sb!vm:n-word-bits))))
205                       ((= index end) result-bit-array)
206                     (declare (optimize (speed 3) (safety 0))
207                              (type index index end))
208                     (setf (%raw-bits result-bit-array index)
209                           (,',wordfun (%raw-bits bit-array-1 index)
210                                       (%raw-bits bit-array-2 index))))))))
211  (def bit-and 32bit-logical-and)
212  (def bit-ior 32bit-logical-or)
213  (def bit-xor 32bit-logical-xor)
214  (def bit-eqv 32bit-logical-eqv)
215  (def bit-nand 32bit-logical-nand)
216  (def bit-nor 32bit-logical-nor)
217  (def bit-andc1 32bit-logical-andc1)
218  (def bit-andc2 32bit-logical-andc2)
219  (def bit-orc1 32bit-logical-orc1)
220  (def bit-orc2 32bit-logical-orc2))
221
222 (deftransform bit-not
223               ((bit-array result-bit-array)
224                (simple-bit-vector simple-bit-vector) *
225                :node node :policy (>= speed space))
226   `(progn
227      ,@(unless (policy node (zerop safety))
228          '((unless (= (length bit-array)
229                       (length result-bit-array))
230              (error "Argument and result bit arrays are not the same length:~
231                      ~%  ~S~%  ~S"
232                     bit-array result-bit-array))))
233      (do ((index sb!vm:vector-data-offset (1+ index))
234           (end (+ sb!vm:vector-data-offset
235                   (truncate (the index
236                                  (+ (length bit-array)
237                                     (1- sb!vm:n-word-bits)))
238                             sb!vm:n-word-bits))))
239          ((= index end) result-bit-array)
240        (declare (optimize (speed 3) (safety 0))
241                 (type index index end))
242        (setf (%raw-bits result-bit-array index)
243              (32bit-logical-not (%raw-bits bit-array index))))))
244 \f
245 ;;;; %BYTE-BLT
246
247 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
248 ;;; stuff (with all the associated bit-index cruft and overflow
249 ;;; issues) even for byte moves. In SBCL, we're converting to byte
250 ;;; moves as problems are discovered with the old code, and this is
251 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
252 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
253 ;;; ideal interface, though, and it probably deserves some thought.
254 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
255                          ((or (simple-unboxed-array (*)) system-area-pointer)
256                           index
257                           (or (simple-unboxed-array (*)) system-area-pointer)
258                           index
259                           index))
260   ;; FIXME: CMU CL had a hairier implementation of this (back when it
261   ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
262   ;; that it didn't work for large (>16M) values of SRC-START or
263   ;; DST-START. However, it might have been more efficient. In
264   ;; particular, I don't really know how much the foreign function
265   ;; call costs us here. My guess is that if the overhead is
266   ;; acceptable for SQRT and COS, it's acceptable here, but this
267   ;; should probably be checked. -- WHN
268   '(flet ((sapify (thing)
269             (etypecase thing
270               (system-area-pointer thing)
271               ;; FIXME: The code here rather relies on the simple
272               ;; unboxed array here having byte-sized entries. That
273               ;; should be asserted explicitly, I just haven't found
274               ;; a concise way of doing it. (It would be nice to
275               ;; declare it in the DEFKNOWN too.)
276               ((simple-unboxed-array (*)) (vector-sap thing)))))
277      (declare (inline sapify))
278      (without-gcing
279       (memmove (sap+ (sapify dst) dst-start)
280                (sap+ (sapify src) src-start)
281                (- dst-end dst-start)))
282      nil))
283 \f
284 ;;;; transforms for EQL of floating point values
285
286 (deftransform eql ((x y) (single-float single-float))
287   '(= (single-float-bits x) (single-float-bits y)))
288
289 (deftransform eql ((x y) (double-float double-float))
290   '(and (= (double-float-low-bits x) (double-float-low-bits y))
291         (= (double-float-high-bits x) (double-float-high-bits y))))
292