0.6.7.22: removed CVS dollar-Header-dollar tags from sources
[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 (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: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: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: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 ;;;; simple string transforms
183 ;;;;
184 ;;;; Note: CMU CL had more of these, including transforms for
185 ;;;; functions which cons. In SBCL, we've gotten rid of the transforms
186 ;;;; for functions which cons, since our GC overhead is sufficiently
187 ;;;; large that it doesn't seem worth it to try to economize on
188 ;;;; function call overhead or on the overhead of runtime type
189 ;;;; dispatch in AREF.
190
191 ;;; FIXME: Shouldn't we be testing for legality of
192 ;;;   * START1, START2, END1, and END2 indices?
193 ;;;   * size of copied string relative to destination string?
194 ;;; (Either there should be tests conditional on SAFETY>=SPEED, or
195 ;;; the transform should be conditional on SPEED>SAFETY.)
196 ;;;
197 ;;; FIXME: Also, the transform should probably be dependent on
198 ;;; SPEED>SPACE.
199 (deftransform replace ((string1 string2 &key (start1 0) (start2 0)
200                                 end1 end2)
201                        (simple-string simple-string &rest t))
202   `(locally
203      (declare (optimize (safety 0)))
204      (bit-bash-copy string2
205                     (the index
206                          (+ (the index (* start2 sb!vm:byte-bits))
207                             ,vector-data-bit-offset))
208                     string1
209                     (the index
210                          (+ (the index (* start1 sb!vm:byte-bits))
211                             ,vector-data-bit-offset))
212                     (the index
213                          (* (min (the index (- (or end1 (length string1))
214                                                start1))
215                                  (the index (- (or end2 (length string2))
216                                                start2)))
217                             sb!vm:byte-bits)))
218      string1))
219 \f
220 ;;;; bit vector hackery
221
222 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word loop that
223 ;;; does 32 bits at a time.
224 ;;;
225 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should be a
226 ;;; function call instead. And do it with DEF-FROB instead of DOLIST.
227 (dolist (x '((bit-and 32bit-logical-and)
228              (bit-ior 32bit-logical-or)
229              (bit-xor 32bit-logical-xor)
230              (bit-eqv 32bit-logical-eqv)
231              (bit-nand 32bit-logical-nand)
232              (bit-nor 32bit-logical-nor)
233              (bit-andc1 32bit-logical-andc1)
234              (bit-andc2 32bit-logical-andc2)
235              (bit-orc1 32bit-logical-orc1)
236              (bit-orc2 32bit-logical-orc2)))
237   (destructuring-bind (bitfun wordfun) x
238     (deftransform bitfun
239                   ((bit-array-1 bit-array-2 result-bit-array)
240                    '(simple-bit-vector simple-bit-vector simple-bit-vector) '*
241                    :eval-name t :node node :policy (>= speed space))
242       `(progn
243          ,@(unless (policy node (zerop safety))
244              '((unless (= (length bit-array-1) (length bit-array-2)
245                           (length result-bit-array))
246                  (error "Argument and/or result bit arrays are not the same length:~
247                          ~%  ~S~%  ~S  ~%  ~S"
248                         bit-array-1 bit-array-2 result-bit-array))))
249          (do ((index sb!vm:vector-data-offset (1+ index))
250               (end (+ sb!vm:vector-data-offset
251                       (truncate (the index
252                                      (+ (length bit-array-1)
253                                         sb!vm:word-bits -1))
254                                 sb!vm:word-bits))))
255              ((= index end) result-bit-array)
256            (declare (optimize (speed 3) (safety 0))
257                     (type index index end))
258            (setf (%raw-bits result-bit-array index)
259                  (,wordfun (%raw-bits bit-array-1 index)
260                            (%raw-bits bit-array-2 index))))))))
261
262 (deftransform bit-not
263               ((bit-array result-bit-array)
264                (simple-bit-vector simple-bit-vector) *
265                :node node :policy (>= speed space))
266   `(progn
267      ,@(unless (policy node (zerop safety))
268          '((unless (= (length bit-array)
269                       (length result-bit-array))
270              (error "Argument and result bit arrays are not the same length:~
271                      ~%  ~S~%  ~S"
272                     bit-array result-bit-array))))
273      (do ((index sb!vm:vector-data-offset (1+ index))
274           (end (+ sb!vm:vector-data-offset
275                   (truncate (the index
276                                  (+ (length bit-array)
277                                     (1- sb!vm:word-bits)))
278                             sb!vm:word-bits))))
279          ((= index end) result-bit-array)
280        (declare (optimize (speed 3) (safety 0))
281                 (type index index end))
282        (setf (%raw-bits result-bit-array index)
283              (32bit-logical-not (%raw-bits bit-array index))))))
284 \f
285 ;;;; primitive translator for BYTE-BLT
286
287 (def-primitive-translator byte-blt (src src-start dst dst-start dst-end)
288   `(let ((src ,src)
289          (src-start (* ,src-start sb!vm:byte-bits))
290          (dst ,dst)
291          (dst-start (* ,dst-start sb!vm:byte-bits))
292          (dst-end (* ,dst-end sb!vm:byte-bits)))
293      (let ((length (- dst-end dst-start)))
294        (etypecase src
295          (system-area-pointer
296           (etypecase dst
297             (system-area-pointer
298              (system-area-copy src src-start dst dst-start length))
299             ((simple-unboxed-array (*))
300              (copy-from-system-area src src-start
301                                     dst (+ dst-start ,vector-data-bit-offset)
302                                     length))))
303          ((simple-unboxed-array (*))
304           (etypecase dst
305             (system-area-pointer
306              (copy-to-system-area src (+ src-start ,vector-data-bit-offset)
307                                   dst dst-start
308                                   length))
309             ((simple-unboxed-array (*))
310              (bit-bash-copy src (+ src-start ,vector-data-bit-offset)
311                             dst (+ dst-start ,vector-data-bit-offset)
312                             length))))))))
313 \f
314 ;;;; transforms for EQL of floating point values
315
316 (deftransform eql ((x y) (single-float single-float))
317   '(= (single-float-bits x) (single-float-bits y)))
318
319 (deftransform eql ((x y) (double-float double-float))
320   '(and (= (double-float-low-bits x) (double-float-low-bits y))
321         (= (double-float-high-bits x) (double-float-high-bits y))))