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