0.8.0.78.vector-nil-string.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 ((string index) (simple-string t))
45   (let ((ctype (continuation-type string)))
46     (if (array-type-p ctype)
47         ;; the other transform will kick in, so that's OK
48         (give-up-ir1-transform)
49         `(typecase string
50           ((simple-array character (*)) (data-vector-ref string index))
51           ((simple-array nil (*)) (data-vector-ref string index))))))
52
53 (deftransform hairy-data-vector-ref ((array index) (array t) * :important t)
54   "avoid runtime dispatch on array element type"
55   (let ((element-ctype (extract-upgraded-element-type array)))
56     (declare (type ctype element-ctype))
57     (when (eq *wild-type* element-ctype)
58       (give-up-ir1-transform
59        "Upgraded element type of array is not known at compile time."))
60     ;; (The expansion here is basically a degenerate case of
61     ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
62     ;; macro, and macros aren't expanded in transform output, we have
63     ;; to hand-expand it ourselves.)
64     (let ((element-type-specifier (type-specifier element-ctype)))
65       `(multiple-value-bind (array index)
66            (%data-vector-and-index array index)
67          (declare (type (simple-array ,element-type-specifier 1) array))
68          (data-vector-ref array index)))))
69
70 (deftransform data-vector-ref ((array index)
71                                (simple-array t))
72   (let ((array-type (continuation-type array)))
73     (unless (array-type-p array-type)
74       (give-up-ir1-transform))
75     (let ((dims (array-type-dimensions array-type)))
76       (when (or (atom dims) (= (length dims) 1))
77         (give-up-ir1-transform))
78       (let ((el-type (array-type-specialized-element-type array-type))
79             (total-size (if (member '* dims)
80                             '*
81                             (reduce #'* dims))))
82         `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
83                                                    (,total-size))
84                                      (%array-data-vector array))
85                           index)))))
86
87 (deftransform hairy-data-vector-set ((array index new-value)
88                                      (array t t)
89                                      *
90                                      :important t)
91   "avoid runtime dispatch on array element type"
92   (let ((element-ctype (extract-upgraded-element-type array)))
93     (declare (type ctype element-ctype))
94     (when (eq *wild-type* element-ctype)
95       (give-up-ir1-transform
96        "Upgraded element type of array is not known at compile time."))
97     (let ((element-type-specifier (type-specifier element-ctype)))
98       `(multiple-value-bind (array index)
99            (%data-vector-and-index array index)
100          (declare (type (simple-array ,element-type-specifier 1) array)
101                   (type ,element-type-specifier new-value))
102          (data-vector-set array
103                           index
104                           new-value)))))
105
106 (deftransform hairy-data-vector-set ((string index new-value)
107                                      (simple-string t t))
108   (let ((ctype (continuation-type string)))
109     (if (array-type-p ctype)
110         ;; the other transform will kick in, so that's OK
111         (give-up-ir1-transform)
112         `(typecase string
113           ((simple-array character (*))
114            (data-vector-set string index new-value))
115           ((simple-array nil (*))
116            (data-vector-set string index new-value))))))
117
118 (deftransform data-vector-set ((array index new-value)
119                                (simple-array t t))
120   (let ((array-type (continuation-type array)))
121     (unless (array-type-p array-type)
122       (give-up-ir1-transform))
123     (let ((dims (array-type-dimensions array-type)))
124       (when (or (atom dims) (= (length dims) 1))
125         (give-up-ir1-transform))
126       (let ((el-type (array-type-specialized-element-type array-type))
127             (total-size (if (member '* dims)
128                             '*
129                             (reduce #'* dims))))
130         `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
131                                                    (,total-size))
132                                      (%array-data-vector array))
133                           index
134                           new-value)))))
135
136 (defoptimizer (%data-vector-and-index derive-type) ((array index))
137   (let ((atype (continuation-type array)))
138     (when (array-type-p atype)
139       (values-specifier-type
140        `(values (simple-array ,(type-specifier
141                                 (array-type-specialized-element-type atype))
142                               (*))
143                 index)))))
144
145 (deftransform %data-vector-and-index ((%array %index)
146                                       (simple-array t)
147                                       *
148                                       :important t)
149   ;; KLUDGE: why the percent signs?  Well, ARRAY and INDEX are
150   ;; respectively exported from the CL and SB!INT packages, which
151   ;; means that they're visible to all sorts of things.  If the
152   ;; compiler can prove that the call to ARRAY-HEADER-P, below, either
153   ;; returns T or NIL, it will delete the irrelevant branch.  However,
154   ;; user code might have got here with a variable named CL:ARRAY, and
155   ;; quite often compiler code with a variable named SB!INT:INDEX, so
156   ;; this can generate code deletion notes for innocuous user code:
157   ;; (DEFUN F (ARRAY I) (DECLARE (SIMPLE-VECTOR ARRAY)) (AREF ARRAY I))
158   ;; -- CSR, 2003-04-01
159
160   ;; We do this solely for the -OR-GIVE-UP side effect, since we want
161   ;; to know that the type can be figured out in the end before we
162   ;; proceed, but we don't care yet what the type will turn out to be.
163   (upgraded-element-type-specifier-or-give-up %array)
164
165   '(if (array-header-p %array)
166        (values (%array-data-vector %array) %index)
167        (values %array %index)))
168
169 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
170 ;;;
171 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
172 ;;; we fix them or should we delete them? (Perhaps these definitions
173 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
174 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
175 #+nil
176 (macrolet
177     ((frob (type bits)
178        (let ((elements-per-word (truncate sb!vm:n-word-bits bits)))
179          `(progn
180             (deftransform data-vector-ref ((vector index)
181                                            (,type *))
182               `(multiple-value-bind (word bit)
183                    (floor index ,',elements-per-word)
184                  (ldb ,(ecase sb!vm:target-byte-order
185                          (:little-endian '(byte ,bits (* bit ,bits)))
186                          (:big-endian '(byte ,bits (- sb!vm:n-word-bits
187                                                       (* (1+ bit) ,bits)))))
188                       (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
189             (deftransform data-vector-set ((vector index new-value)
190                                            (,type * *))
191               `(multiple-value-bind (word bit)
192                    (floor index ,',elements-per-word)
193                  (setf (ldb ,(ecase sb!vm:target-byte-order
194                                (:little-endian '(byte ,bits (* bit ,bits)))
195                                (:big-endian
196                                 '(byte ,bits (- sb!vm:n-word-bits
197                                                 (* (1+ bit) ,bits)))))
198                             (%raw-bits vector (+ word sb!vm:vector-data-offset)))
199                        new-value)))))))
200   (frob simple-bit-vector 1)
201   (frob (simple-array (unsigned-byte 2) (*)) 2)
202   (frob (simple-array (unsigned-byte 4) (*)) 4))
203 \f
204 ;;;; BIT-VECTOR hackery
205
206 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word
207 ;;; loop that does 32 bits at a time.
208 ;;;
209 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should
210 ;;; be a function call instead.
211 (macrolet ((def (bitfun wordfun)
212              `(deftransform ,bitfun ((bit-array-1 bit-array-2 result-bit-array)
213                                      (simple-bit-vector
214                                       simple-bit-vector
215                                       simple-bit-vector)
216                                      *
217                                      :node node :policy (>= speed space))
218                 `(progn
219                    ,@(unless (policy node (zerop safety))
220                              '((unless (= (length bit-array-1)
221                                           (length bit-array-2)
222                                           (length result-bit-array))
223                                  (error "Argument and/or result bit arrays are not the same length:~
224                          ~%  ~S~%  ~S  ~%  ~S"
225                                         bit-array-1
226                                         bit-array-2
227                                         result-bit-array))))
228                   (let ((length (length result-bit-array)))
229                     (if (= length 0)
230                         ;; We avoid doing anything to 0-length
231                         ;; bit-vectors, or rather, the memory that
232                         ;; follows them. Other divisible-by-32 cases
233                         ;; are handled by the (1- length), below.
234                         ;; CSR, 2002-04-24
235                         result-bit-array
236                         (do ((index sb!vm:vector-data-offset (1+ index))
237                              (end-1 (+ sb!vm:vector-data-offset
238                                        ;; bit-vectors of length 1-32
239                                        ;; need precisely one (SETF
240                                        ;; %RAW-BITS), done here in the
241                                        ;; epilogue. - CSR, 2002-04-24
242                                        (truncate (truly-the index (1- length))
243                                                  sb!vm:n-word-bits))))
244                             ((= index end-1)
245                              (setf (%raw-bits result-bit-array index)
246                                    (,',wordfun (%raw-bits bit-array-1 index)
247                                                (%raw-bits bit-array-2 index)))
248                              result-bit-array)
249                           (declare (optimize (speed 3) (safety 0))
250                                    (type index index end-1))
251                           (setf (%raw-bits result-bit-array index)
252                                 (,',wordfun (%raw-bits bit-array-1 index)
253                                             (%raw-bits bit-array-2 index))))))))))
254  (def bit-and 32bit-logical-and)
255  (def bit-ior 32bit-logical-or)
256  (def bit-xor 32bit-logical-xor)
257  (def bit-eqv 32bit-logical-eqv)
258  (def bit-nand 32bit-logical-nand)
259  (def bit-nor 32bit-logical-nor)
260  (def bit-andc1 32bit-logical-andc1)
261  (def bit-andc2 32bit-logical-andc2)
262  (def bit-orc1 32bit-logical-orc1)
263  (def bit-orc2 32bit-logical-orc2))
264
265 (deftransform bit-not
266               ((bit-array result-bit-array)
267                (simple-bit-vector simple-bit-vector) *
268                :node node :policy (>= speed space))
269   `(progn
270      ,@(unless (policy node (zerop safety))
271          '((unless (= (length bit-array)
272                       (length result-bit-array))
273              (error "Argument and result bit arrays are not the same length:~
274                      ~%  ~S~%  ~S"
275                     bit-array result-bit-array))))
276     (let ((length (length result-bit-array)))
277       (if (= length 0)
278           ;; We avoid doing anything to 0-length bit-vectors, or
279           ;; rather, the memory that follows them. Other
280           ;; divisible-by-32 cases are handled by the (1- length),
281           ;; below.  CSR, 2002-04-24
282           result-bit-array
283           (do ((index sb!vm:vector-data-offset (1+ index))
284                (end-1 (+ sb!vm:vector-data-offset
285                          ;; bit-vectors of length 1-32 need precisely
286                          ;; one (SETF %RAW-BITS), done here in the
287                          ;; epilogue. - CSR, 2002-04-24
288                          (truncate (truly-the index (1- length))
289                                    sb!vm:n-word-bits))))
290               ((= index end-1)
291                (setf (%raw-bits result-bit-array index)
292                      (32bit-logical-not (%raw-bits bit-array index)))
293                result-bit-array)
294             (declare (optimize (speed 3) (safety 0))
295                      (type index index end-1))
296             (setf (%raw-bits result-bit-array index)
297                   (32bit-logical-not (%raw-bits bit-array index))))))))
298
299 (deftransform bit-vector-= ((x y) (simple-bit-vector simple-bit-vector))
300   `(and (= (length x) (length y))
301         (let ((length (length x)))
302           (or (= length 0)
303               (do* ((i sb!vm:vector-data-offset (+ i 1))
304                     (end-1 (+ sb!vm:vector-data-offset
305                               (floor (1- length) sb!vm:n-word-bits))))
306                    ((= i end-1)
307                     (let* ((extra (mod length sb!vm:n-word-bits))
308                            (mask (1- (ash 1 extra)))
309                            (numx
310                             (logand
311                              (ash mask
312                                   ,(ecase sb!c:*backend-byte-order*
313                                      (:little-endian 0)
314                                      (:big-endian
315                                       '(- sb!vm:n-word-bits extra))))
316                              (%raw-bits x i)))
317                            (numy
318                             (logand
319                              (ash mask
320                                   ,(ecase sb!c:*backend-byte-order*
321                                      (:little-endian 0)
322                                      (:big-endian
323                                       '(- sb!vm:n-word-bits extra))))
324                              (%raw-bits y i))))
325                       (declare (type (integer 0 31) extra)
326                                (type (unsigned-byte 32) mask numx numy))
327                       (= numx numy)))
328                 (declare (type index i end-1))
329                 (let ((numx (%raw-bits x i))
330                       (numy (%raw-bits y i)))
331                   (declare (type (unsigned-byte 32) numx numy))
332                   (unless (= numx numy)
333                     (return nil))))))))
334 \f
335 ;;;; %BYTE-BLT
336
337 ;;; FIXME: The old CMU CL code used various COPY-TO/FROM-SYSTEM-AREA
338 ;;; stuff (with all the associated bit-index cruft and overflow
339 ;;; issues) even for byte moves. In SBCL, we're converting to byte
340 ;;; moves as problems are discovered with the old code, and this is
341 ;;; currently (ca. sbcl-0.6.12.30) the main interface for code in
342 ;;; SB!KERNEL and SB!SYS (e.g. i/o code). It's not clear that it's the
343 ;;; ideal interface, though, and it probably deserves some thought.
344 (deftransform %byte-blt ((src src-start dst dst-start dst-end)
345                          ((or (simple-unboxed-array (*)) system-area-pointer)
346                           index
347                           (or (simple-unboxed-array (*)) system-area-pointer)
348                           index
349                           index))
350   ;; FIXME: CMU CL had a hairier implementation of this (back when it
351   ;; was still called (%PRIMITIVE BYTE-BLT). It had the small problem
352   ;; that it didn't work for large (>16M) values of SRC-START or
353   ;; DST-START. However, it might have been more efficient. In
354   ;; particular, I don't really know how much the foreign function
355   ;; call costs us here. My guess is that if the overhead is
356   ;; acceptable for SQRT and COS, it's acceptable here, but this
357   ;; should probably be checked. -- WHN
358   '(flet ((sapify (thing)
359             (etypecase thing
360               (system-area-pointer thing)
361               ;; FIXME: The code here rather relies on the simple
362               ;; unboxed array here having byte-sized entries. That
363               ;; should be asserted explicitly, I just haven't found
364               ;; a concise way of doing it. (It would be nice to
365               ;; declare it in the DEFKNOWN too.)
366               ((simple-unboxed-array (*)) (vector-sap thing)))))
367      (declare (inline sapify))
368      (without-gcing
369       (memmove (sap+ (sapify dst) dst-start)
370                (sap+ (sapify src) src-start)
371                (- dst-end dst-start)))
372      (values)))
373 \f
374 ;;;; transforms for EQL of floating point values
375
376 (deftransform eql ((x y) (single-float single-float))
377   '(= (single-float-bits x) (single-float-bits y)))
378
379 (deftransform eql ((x y) (double-float double-float))
380   '(and (= (double-float-low-bits x) (double-float-low-bits y))
381         (= (double-float-high-bits x) (double-float-high-bits y))))
382