0.6.7.19: added stop-compiler-crash patch from Martin Atzmueller
[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 (file-comment
15   "$Header$")
16
17 ;;; FIXME: It would be good to implement SB!XC:DEFCONSTANT, and use
18 ;;; use that here, so that the compiler is born knowing this value.
19 ;;; FIXME: Add a comment telling whether this holds for all vectors
20 ;;; or only for vectors based on simple arrays (non-adjustable, etc.).
21 (defconstant vector-data-bit-offset
22   (* sb!vm:vector-data-offset sb!vm:word-bits))
23
24 ;;; We need to define these predicates, since the TYPEP source transform picks
25 ;;; whichever predicate was defined last when there are multiple predicates for
26 ;;; equivalent types.
27 (def-source-transform short-float-p (x) `(single-float-p ,x))
28 #!-long-float
29 (def-source-transform long-float-p (x) `(double-float-p ,x))
30
31 (def-source-transform compiled-function-p (x)
32   `(functionp ,x))
33
34 (def-source-transform char-int (x)
35   `(char-code ,x))
36
37 (deftransform abs ((x) (rational))
38   '(if (< x 0) (- x) x))
39
40 ;;; The layout is stored in slot 0.
41 (def-source-transform %instance-layout (x)
42   `(truly-the layout (%instance-ref ,x 0)))
43 (def-source-transform %set-instance-layout (x val)
44   `(%instance-set ,x 0 (the layout ,val)))
45 \f
46 ;;;; character support
47
48 ;;; In our implementation there are really only BASE-CHARs.
49 (def-source-transform characterp (obj)
50   `(base-char-p ,obj))
51 \f
52 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
53
54 (deftransform hairy-data-vector-ref ((array index) (array t) * :important t)
55   "avoid runtime dispatch on array element type"
56   (let ((element-ctype (extract-upgraded-element-type array)))
57     (declare (type ctype element-ctype))
58     (when (eq *wild-type* element-ctype)
59       (give-up-ir1-transform
60        "Upgraded element type of array is not known at compile time."))
61     ;; (The expansion here is basically a degenerate case of
62     ;; WITH-ARRAY-DATA. Since WITH-ARRAY-DATA is implemented as a
63     ;; macro, and macros aren't expanded in transform output, we have
64     ;; to hand-expand it ourselves.)
65     (let ((element-type-specifier (type-specifier element-ctype)))
66       `(multiple-value-bind (array index)
67            ;; FIXME: All this noise should move into a
68            ;; %DATA-VECTOR-AND-INDEX function, and there should be
69            ;; DEFTRANSFORMs for %DATA-VECTOR-AND-INDEX to optimize the
70            ;; function call away when the array is known to be simple,
71            ;; and to specialize to
72            ;; %DATA-VECTOR-AND-INDEX-IN-VECTOR-CASE when the array is
73            ;; known to have only one dimension.
74            (if (array-header-p array)
75                (%with-array-data array index nil)
76                (let ((array array))
77                  (declare (type (simple-array ,element-type-specifier 1)
78                                 array))
79                  (%check-bound array 0 index)
80                  (values array index)))
81          (declare (type (simple-array ,element-type-specifier 1) array))
82          (data-vector-ref array index)))))
83
84 (deftransform data-vector-ref ((array index)
85                                (simple-array t))
86   (let ((array-type (continuation-type array)))
87     (unless (array-type-p array-type)
88       (give-up-ir1-transform))
89     (let ((dims (array-type-dimensions array-type)))
90       (when (or (atom dims) (= (length dims) 1))
91         (give-up-ir1-transform))
92       (let ((el-type (array-type-element-type array-type))
93             (total-size (if (member '* dims)
94                             '*
95                             (reduce #'* dims))))
96         `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
97                                                    (,total-size))
98                                      (%array-data-vector array))
99                           index)))))
100
101 (deftransform hairy-data-vector-set ((array index new-value)
102                                      (array t t)
103                                      *
104                                      :important t)
105   "avoid runtime dispatch on array element type"
106   (let ((element-ctype (extract-upgraded-element-type array)))
107     (declare (type ctype element-ctype))
108     (when (eq *wild-type* element-ctype)
109       (give-up-ir1-transform
110        "Upgraded element type of array is not known at compile time."))
111     (let ((element-type-specifier (type-specifier element-ctype)))
112       `(multiple-value-bind (array index)
113            ;; FIXME: All this noise should move into a
114            ;; %DATA-VECTOR-AND-INDEX function, and there should be
115            ;; DEFTRANSFORMs for %DATA-VECTOR-AND-INDEX to optimize the
116            ;; function call away when the array is known to be simple,
117            ;; and to specialize to
118            ;; %DATA-VECTOR-AND-INDEX-IN-VECTOR-CASE when the array is
119            ;; known to have only one dimension.
120            (if (array-header-p array)
121                (%with-array-data array index nil)
122                (let ((array array))
123                  (declare (type (simple-array ,element-type-specifier 1)
124                                 array))
125                  (%check-bound array 0 index)
126                  (values array index)))
127          (data-vector-set (truly-the (simple-array ,element-type-specifier 1)
128                                      array)
129                           index
130                           new-value)))))
131
132 (deftransform data-vector-set ((array index new-value)
133                                (simple-array t t))
134   (let ((array-type (continuation-type array)))
135     (unless (array-type-p array-type)
136       (give-up-ir1-transform))
137     (let ((dims (array-type-dimensions array-type)))
138       (when (or (atom dims) (= (length dims) 1))
139         (give-up-ir1-transform))
140       (let ((el-type (array-type-element-type array-type))
141             (total-size (if (member '* dims)
142                             '*
143                             (reduce #'* dims))))
144         `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
145                                                    (,total-size))
146                                      (%array-data-vector array))
147                           index
148                           new-value)))))
149
150 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
151 ;;;
152 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
153 ;;; we fix them or should we delete them? (Perhaps these definitions
154 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
155 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
156 #+nil
157 (macrolet
158     ((frob (type bits)
159        (let ((elements-per-word (truncate sb!vm:word-bits bits)))
160          `(progn
161             (deftransform data-vector-ref ((vector index)
162                                            (,type *))
163               `(multiple-value-bind (word bit)
164                    (floor index ,',elements-per-word)
165                  (ldb ,(ecase sb!vm:target-byte-order
166                          (:little-endian '(byte ,bits (* bit ,bits)))
167                          (:big-endian '(byte ,bits (- sb!vm:word-bits
168                                                       (* (1+ bit) ,bits)))))
169                       (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
170             (deftransform data-vector-set ((vector index new-value)
171                                            (,type * *))
172               `(multiple-value-bind (word bit)
173                    (floor index ,',elements-per-word)
174                  (setf (ldb ,(ecase sb!vm:target-byte-order
175                                (:little-endian '(byte ,bits (* bit ,bits)))
176                                (:big-endian
177                                 '(byte ,bits (- sb!vm:word-bits
178                                                 (* (1+ bit) ,bits)))))
179                             (%raw-bits vector (+ word sb!vm:vector-data-offset)))
180                        new-value)))))))
181   (frob simple-bit-vector 1)
182   (frob (simple-array (unsigned-byte 2) (*)) 2)
183   (frob (simple-array (unsigned-byte 4) (*)) 4))
184 \f
185 ;;;; simple string transforms
186 ;;;;
187 ;;;; Note: CMU CL had more of these, including transforms for
188 ;;;; functions which cons. In SBCL, we've gotten rid of the transforms
189 ;;;; for functions which cons, since our GC overhead is sufficiently
190 ;;;; large that it doesn't seem worth it to try to economize on
191 ;;;; function call overhead or on the overhead of runtime type
192 ;;;; dispatch in AREF.
193
194 (deftransform subseq ((string start &optional (end nil))
195                       (simple-string t &optional t))
196   `(let* ((length (- (or end (length string))
197                      start))
198           (result (make-string length)))
199      (declare (optimize (safety 0)))
200      (bit-bash-copy string
201                     (the index
202                          (+ (the index (* start sb!vm:byte-bits))
203                             ,vector-data-bit-offset))
204                     result
205                     ,vector-data-bit-offset
206                     (the index (* length sb!vm:byte-bits)))
207      result))
208
209 (deftransform copy-seq ((seq) (simple-string))
210   `(let* ((length (length seq))
211           (res (make-string length)))
212      (declare (optimize (safety 0)))
213      (bit-bash-copy seq
214                     ,vector-data-bit-offset
215                     res
216                     ,vector-data-bit-offset
217                     (the index (* length sb!vm:byte-bits)))
218      res))
219
220 (deftransform replace ((string1 string2 &key (start1 0) (start2 0)
221                                 end1 end2)
222                        (simple-string simple-string &rest t))
223   `(locally (declare (optimize (safety 0)))
224      (bit-bash-copy string2
225                     (the index
226                          (+ (the index (* start2 sb!vm:byte-bits))
227                             ,vector-data-bit-offset))
228                     string1
229                     (the index
230                          (+ (the index (* start1 sb!vm:byte-bits))
231                             ,vector-data-bit-offset))
232                     (the index
233                          (* (min (the index (- (or end1 (length string1))
234                                                start1))
235                                  (the index (- (or end2 (length string2))
236                                                start2)))
237                             sb!vm:byte-bits)))
238      string1))
239
240 (deftransform concatenate ((rtype &rest sequences)
241                            (t &rest simple-string)
242                            simple-string)
243   (collect ((lets)
244             (forms)
245             (all-lengths)
246             (args))
247     (dolist (seq sequences)
248       (declare (ignore seq))
249       (let ((n-seq (gensym))
250             (n-length (gensym)))
251         (args n-seq)
252         (lets `(,n-length (the index (* (length ,n-seq) sb!vm:byte-bits))))
253         (all-lengths n-length)
254         (forms `(bit-bash-copy ,n-seq ,vector-data-bit-offset
255                                res start
256                                ,n-length))
257         (forms `(setq start (+ start ,n-length)))))
258     `(lambda (rtype ,@(args))
259        (declare (ignore rtype))
260        (let* (,@(lets)
261               (res (make-string (truncate (the index (+ ,@(all-lengths)))
262                                           sb!vm:byte-bits)))
263               (start ,vector-data-bit-offset))
264          (declare (type index start ,@(all-lengths)))
265          ,@(forms)
266          res))))
267 \f
268 ;;;; bit vector hackery
269
270 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word loop that
271 ;;; does 32 bits at a time.
272 ;;;
273 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should be a
274 ;;; function call instead. And do it with DEF-FROB instead of DOLIST.
275 (dolist (x '((bit-and 32bit-logical-and)
276              (bit-ior 32bit-logical-or)
277              (bit-xor 32bit-logical-xor)
278              (bit-eqv 32bit-logical-eqv)
279              (bit-nand 32bit-logical-nand)
280              (bit-nor 32bit-logical-nor)
281              (bit-andc1 32bit-logical-andc1)
282              (bit-andc2 32bit-logical-andc2)
283              (bit-orc1 32bit-logical-orc1)
284              (bit-orc2 32bit-logical-orc2)))
285   (destructuring-bind (bitfun wordfun) x
286     (deftransform bitfun
287                   ((bit-array-1 bit-array-2 result-bit-array)
288                    '(simple-bit-vector simple-bit-vector simple-bit-vector) '*
289                    :eval-name t :node node :policy (>= speed space))
290       `(progn
291          ,@(unless (policy node (zerop safety))
292              '((unless (= (length bit-array-1) (length bit-array-2)
293                           (length result-bit-array))
294                  (error "Argument and/or result bit arrays are not the same length:~
295                          ~%  ~S~%  ~S  ~%  ~S"
296                         bit-array-1 bit-array-2 result-bit-array))))
297          (do ((index sb!vm:vector-data-offset (1+ index))
298               (end (+ sb!vm:vector-data-offset
299                       (truncate (the index
300                                      (+ (length bit-array-1)
301                                         sb!vm:word-bits -1))
302                                 sb!vm:word-bits))))
303              ((= index end) result-bit-array)
304            (declare (optimize (speed 3) (safety 0))
305                     (type index index end))
306            (setf (%raw-bits result-bit-array index)
307                  (,wordfun (%raw-bits bit-array-1 index)
308                            (%raw-bits bit-array-2 index))))))))
309
310 (deftransform bit-not
311               ((bit-array result-bit-array)
312                (simple-bit-vector simple-bit-vector) *
313                :node node :policy (>= speed space))
314   `(progn
315      ,@(unless (policy node (zerop safety))
316          '((unless (= (length bit-array)
317                       (length result-bit-array))
318              (error "Argument and result bit arrays are not the same length:~
319                      ~%  ~S~%  ~S"
320                     bit-array result-bit-array))))
321      (do ((index sb!vm:vector-data-offset (1+ index))
322           (end (+ sb!vm:vector-data-offset
323                   (truncate (the index
324                                  (+ (length bit-array)
325                                     (1- sb!vm:word-bits)))
326                             sb!vm:word-bits))))
327          ((= index end) result-bit-array)
328        (declare (optimize (speed 3) (safety 0))
329                 (type index index end))
330        (setf (%raw-bits result-bit-array index)
331              (32bit-logical-not (%raw-bits bit-array index))))))
332 \f
333 ;;;; primitive translator for BYTE-BLT
334
335 (def-primitive-translator byte-blt (src src-start dst dst-start dst-end)
336   `(let ((src ,src)
337          (src-start (* ,src-start sb!vm:byte-bits))
338          (dst ,dst)
339          (dst-start (* ,dst-start sb!vm:byte-bits))
340          (dst-end (* ,dst-end sb!vm:byte-bits)))
341      (let ((length (- dst-end dst-start)))
342        (etypecase src
343          (system-area-pointer
344           (etypecase dst
345             (system-area-pointer
346              (system-area-copy src src-start dst dst-start length))
347             ((simple-unboxed-array (*))
348              (copy-from-system-area src src-start
349                                     dst (+ dst-start ,vector-data-bit-offset)
350                                     length))))
351          ((simple-unboxed-array (*))
352           (etypecase dst
353             (system-area-pointer
354              (copy-to-system-area src (+ src-start ,vector-data-bit-offset)
355                                   dst dst-start
356                                   length))
357             ((simple-unboxed-array (*))
358              (bit-bash-copy src (+ src-start ,vector-data-bit-offset)
359                             dst (+ dst-start ,vector-data-bit-offset)
360                             length))))))))
361 \f
362 ;;;; transforms for EQL of floating point values
363
364 (deftransform eql ((x y) (single-float single-float))
365   '(= (single-float-bits x) (single-float-bits y)))
366
367 (deftransform eql ((x y) (double-float double-float))
368   '(and (= (double-float-low-bits x) (double-float-low-bits y))
369         (= (double-float-high-bits x) (double-float-high-bits y))))