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