ad74c20e18706db86d9eef41d925aa92c973e2f2
[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:word-bits))
20
21 ;;; We need to define these predicates, since the TYPEP source transform picks
22 ;;; whichever predicate was defined last when there are multiple predicates for
23 ;;; equivalent types.
24 (def-source-transform short-float-p (x) `(single-float-p ,x))
25 #!-long-float
26 (def-source-transform long-float-p (x) `(double-float-p ,x))
27
28 (def-source-transform compiled-function-p (x)
29   `(functionp ,x))
30
31 (def-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 (def-source-transform %instance-layout (x)
39   `(truly-the layout (%instance-ref ,x 0)))
40 (def-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 (def-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 ;;; MNA: open-coded-simple-array patch
82 (deftransform data-vector-ref ((array index)
83                                (simple-array t))
84   (let ((array-type (continuation-type array)))
85     (unless (array-type-p array-type)
86       (give-up-ir1-transform))
87     (let ((dims (array-type-dimensions array-type)))
88       (when (and (consp dims) (= (length dims) 1))
89         (give-up-ir1-transform))
90       (let* ((el-type (array-type-element-type array-type))
91              (total-size (if (or (atom dims) (member '* dims))
92                             '*
93                            (reduce #'* dims)))
94              (type-sp `(simple-array ,(type-specifier el-type)
95                         (,total-size))))
96         (if (atom dims)
97           `(let ((a (truly-the ,type-sp (%array-simp array))))
98             (data-vector-ref a index))
99           `(let ((a (truly-the ,type-sp (%array-data-vector array))))
100             (data-vector-ref a index)))))))
101
102 (deftransform hairy-data-vector-set ((array index new-value)
103                                      (array t t)
104                                      *
105                                      :important t)
106   "avoid runtime dispatch on array element type"
107   (let ((element-ctype (extract-upgraded-element-type array)))
108     (declare (type ctype element-ctype))
109     (when (eq *wild-type* element-ctype)
110       (give-up-ir1-transform
111        "Upgraded element type of array is not known at compile time."))
112     (let ((element-type-specifier (type-specifier element-ctype)))
113       `(multiple-value-bind (array index)
114            ;; FIXME: All this noise should move into a
115            ;; %DATA-VECTOR-AND-INDEX function, and there should be
116            ;; DEFTRANSFORMs for %DATA-VECTOR-AND-INDEX to optimize the
117            ;; function call away when the array is known to be simple,
118            ;; and to specialize to
119            ;; %DATA-VECTOR-AND-INDEX-IN-VECTOR-CASE when the array is
120            ;; known to have only one dimension.
121            (if (array-header-p array)
122                (%with-array-data array index nil)
123                (let ((array array))
124                  (declare (type (simple-array ,element-type-specifier 1)
125                                 array))
126                  (%check-bound array 0 index)
127                  (values array index)))
128          (data-vector-set (truly-the (simple-array ,element-type-specifier 1)
129                                      array)
130                           index
131                           new-value)))))
132
133 ;;; MNA: open-coded-simple-array patch
134 (deftransform data-vector-set ((array index new-value)
135                                (simple-array t t))
136   (let ((array-type (continuation-type array)))
137     (unless (array-type-p array-type)
138       (give-up-ir1-transform))
139     (let ((dims (array-type-dimensions array-type)))
140       (when (and (consp dims) (= (length dims) 1))
141         (give-up-ir1-transform))
142       (let* ((el-type (array-type-element-type array-type))
143              (total-size (if (or (atom dims) (member '* dims))
144                             '*
145                            (reduce #'* dims)))
146              (type-sp `(simple-array ,(type-specifier el-type)
147                         (,total-size))))
148                 (if (atom dims)
149             `(let ((a (truly-the ,type-sp (%array-simp array))))
150                (data-vector-set a index new-value))
151             `(let ((a (truly-the ,type-sp (%array-data-vector array))))
152                (data-vector-set a index new-value)))))))
153
154 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
155 ;;;
156 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
157 ;;; we fix them or should we delete them? (Perhaps these definitions
158 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
159 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
160 #+nil
161 (macrolet
162     ((frob (type bits)
163        (let ((elements-per-word (truncate sb!vm:word-bits bits)))
164          `(progn
165             (deftransform data-vector-ref ((vector index)
166                                            (,type *))
167               `(multiple-value-bind (word bit)
168                    (floor index ,',elements-per-word)
169                  (ldb ,(ecase sb!vm:target-byte-order
170                          (:little-endian '(byte ,bits (* bit ,bits)))
171                          (:big-endian '(byte ,bits (- sb!vm:word-bits
172                                                       (* (1+ bit) ,bits)))))
173                       (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
174             (deftransform data-vector-set ((vector index new-value)
175                                            (,type * *))
176               `(multiple-value-bind (word bit)
177                    (floor index ,',elements-per-word)
178                  (setf (ldb ,(ecase sb!vm:target-byte-order
179                                (:little-endian '(byte ,bits (* bit ,bits)))
180                                (:big-endian
181                                 '(byte ,bits (- sb!vm:word-bits
182                                                 (* (1+ bit) ,bits)))))
183                             (%raw-bits vector (+ word sb!vm:vector-data-offset)))
184                        new-value)))))))
185   (frob simple-bit-vector 1)
186   (frob (simple-array (unsigned-byte 2) (*)) 2)
187   (frob (simple-array (unsigned-byte 4) (*)) 4))
188 \f
189 ;;;; bit vector hackery
190
191 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word loop that
192 ;;; does 32 bits at a time.
193 ;;;
194 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should be a
195 ;;; function call instead. And do it with DEF-FROB instead of DOLIST.
196 (dolist (x '((bit-and 32bit-logical-and)
197              (bit-ior 32bit-logical-or)
198              (bit-xor 32bit-logical-xor)
199              (bit-eqv 32bit-logical-eqv)
200              (bit-nand 32bit-logical-nand)
201              (bit-nor 32bit-logical-nor)
202              (bit-andc1 32bit-logical-andc1)
203              (bit-andc2 32bit-logical-andc2)
204              (bit-orc1 32bit-logical-orc1)
205              (bit-orc2 32bit-logical-orc2)))
206   (destructuring-bind (bitfun wordfun) x
207     (deftransform bitfun
208                   ((bit-array-1 bit-array-2 result-bit-array)
209                    '(simple-bit-vector simple-bit-vector simple-bit-vector) '*
210                    :eval-name t :node node :policy (>= speed space))
211       `(progn
212          ,@(unless (policy node (zerop safety))
213              '((unless (= (length bit-array-1) (length bit-array-2)
214                           (length result-bit-array))
215                  (error "Argument and/or result bit arrays are not the same length:~
216                          ~%  ~S~%  ~S  ~%  ~S"
217                         bit-array-1 bit-array-2 result-bit-array))))
218          (do ((index sb!vm:vector-data-offset (1+ index))
219               (end (+ sb!vm:vector-data-offset
220                       (truncate (the index
221                                      (+ (length bit-array-1)
222                                         sb!vm:word-bits -1))
223                                 sb!vm:word-bits))))
224              ((= index end) result-bit-array)
225            (declare (optimize (speed 3) (safety 0))
226                     (type index index end))
227            (setf (%raw-bits result-bit-array index)
228                  (,wordfun (%raw-bits bit-array-1 index)
229                            (%raw-bits bit-array-2 index))))))))
230
231 (deftransform bit-not
232               ((bit-array result-bit-array)
233                (simple-bit-vector simple-bit-vector) *
234                :node node :policy (>= speed space))
235   `(progn
236      ,@(unless (policy node (zerop safety))
237          '((unless (= (length bit-array)
238                       (length result-bit-array))
239              (error "Argument and result bit arrays are not the same length:~
240                      ~%  ~S~%  ~S"
241                     bit-array result-bit-array))))
242      (do ((index sb!vm:vector-data-offset (1+ index))
243           (end (+ sb!vm:vector-data-offset
244                   (truncate (the index
245                                  (+ (length bit-array)
246                                     (1- sb!vm:word-bits)))
247                             sb!vm:word-bits))))
248          ((= index end) result-bit-array)
249        (declare (optimize (speed 3) (safety 0))
250                 (type index index end))
251        (setf (%raw-bits result-bit-array index)
252              (32bit-logical-not (%raw-bits bit-array index))))))
253 \f
254 ;;;; primitive translator for BYTE-BLT
255
256 (def-primitive-translator byte-blt (src src-start dst dst-start dst-end)
257   `(let ((src ,src)
258          (src-start (* ,src-start sb!vm:byte-bits))
259          (dst ,dst)
260          (dst-start (* ,dst-start sb!vm:byte-bits))
261          (dst-end (* ,dst-end sb!vm:byte-bits)))
262      (let ((length (- dst-end dst-start)))
263        (etypecase src
264          (system-area-pointer
265           (etypecase dst
266             (system-area-pointer
267              (system-area-copy src src-start dst dst-start length))
268             ((simple-unboxed-array (*))
269              (copy-from-system-area src src-start
270                                     dst (+ dst-start ,vector-data-bit-offset)
271                                     length))))
272          ((simple-unboxed-array (*))
273           (etypecase dst
274             (system-area-pointer
275              (copy-to-system-area src (+ src-start ,vector-data-bit-offset)
276                                   dst dst-start
277                                   length))
278             ((simple-unboxed-array (*))
279              (bit-bash-copy src (+ src-start ,vector-data-bit-offset)
280                             dst (+ dst-start ,vector-data-bit-offset)
281                             length))))))))
282 \f
283 ;;;; transforms for EQL of floating point values
284
285 (deftransform eql ((x y) (single-float single-float))
286   '(= (single-float-bits x) (single-float-bits y)))
287
288 (deftransform eql ((x y) (double-float double-float))
289   '(and (= (double-float-low-bits x) (double-float-low-bits y))
290         (= (double-float-high-bits x) (double-float-high-bits y))))
291