1 ;;;; implementation-dependent transforms
3 ;;;; This software is part of the SBCL system. See the README file for
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.
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))
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
27 (def-source-transform short-float-p (x) `(single-float-p ,x))
29 (def-source-transform long-float-p (x) `(double-float-p ,x))
31 (def-source-transform compiled-function-p (x)
34 (def-source-transform char-int (x)
37 (deftransform abs ((x) (rational))
38 '(if (< x 0) (- x) x))
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)))
46 ;;;; character support
48 ;;; In our implementation there are really only BASE-CHARs.
49 (def-source-transform characterp (obj)
52 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
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)
77 (declare (type (simple-array ,element-type-specifier 1)
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)))))
84 (deftransform data-vector-ref ((array index)
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)
96 `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
98 (%array-data-vector array))
101 (deftransform hairy-data-vector-set ((array index new-value)
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)
123 (declare (type (simple-array ,element-type-specifier 1)
125 (%check-bound array 0 index)
126 (values array index)))
127 (data-vector-set (truly-the (simple-array ,element-type-specifier 1)
132 (deftransform data-vector-set ((array index new-value)
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)
144 `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
146 (%array-data-vector array))
150 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
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?)
159 (let ((elements-per-word (truncate sb!vm:word-bits bits)))
161 (deftransform data-vector-ref ((vector index)
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)
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)))
177 '(byte ,bits (- sb!vm:word-bits
178 (* (1+ bit) ,bits)))))
179 (%raw-bits vector (+ word sb!vm:vector-data-offset)))
181 (frob simple-bit-vector 1)
182 (frob (simple-array (unsigned-byte 2) (*)) 2)
183 (frob (simple-array (unsigned-byte 4) (*)) 4))
185 ;;;; simple string transforms
187 (deftransform subseq ((string start &optional (end nil))
188 (simple-string t &optional t))
189 `(let* ((length (- (or end (length string))
191 (result (make-string length)))
192 (declare (optimize (safety 0)))
193 (bit-bash-copy string
195 (+ (the index (* start sb!vm:byte-bits))
196 ,vector-data-bit-offset))
198 ,vector-data-bit-offset
199 (the index (* length sb!vm:byte-bits)))
202 (deftransform copy-seq ((seq) (simple-string))
203 `(let* ((length (length seq))
204 (res (make-string length)))
205 (declare (optimize (safety 0)))
207 ,vector-data-bit-offset
209 ,vector-data-bit-offset
210 (the index (* length sb!vm:byte-bits)))
213 (deftransform replace ((string1 string2 &key (start1 0) (start2 0)
215 (simple-string simple-string &rest t))
216 `(locally (declare (optimize (safety 0)))
217 (bit-bash-copy string2
219 (+ (the index (* start2 sb!vm:byte-bits))
220 ,vector-data-bit-offset))
223 (+ (the index (* start1 sb!vm:byte-bits))
224 ,vector-data-bit-offset))
226 (* (min (the index (- (or end1 (length string1))
228 (the index (- (or end2 (length string2))
233 (deftransform concatenate ((rtype &rest sequences)
234 (t &rest simple-string)
240 (dolist (seq sequences)
241 (declare (ignore seq))
242 (let ((n-seq (gensym))
245 (lets `(,n-length (the index (* (length ,n-seq) sb!vm:byte-bits))))
246 (all-lengths n-length)
247 (forms `(bit-bash-copy ,n-seq ,vector-data-bit-offset
250 (forms `(setq start (+ start ,n-length)))))
251 `(lambda (rtype ,@(args))
252 (declare (ignore rtype))
254 (res (make-string (truncate (the index (+ ,@(all-lengths)))
256 (start ,vector-data-bit-offset))
257 (declare (type index start ,@(all-lengths)))
261 ;;;; bit vector hackery
263 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word loop that
264 ;;; does 32 bits at a time.
266 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should be a
267 ;;; function call instead. And do it with DEF-FROB instead of DOLIST.
268 (dolist (x '((bit-and 32bit-logical-and)
269 (bit-ior 32bit-logical-or)
270 (bit-xor 32bit-logical-xor)
271 (bit-eqv 32bit-logical-eqv)
272 (bit-nand 32bit-logical-nand)
273 (bit-nor 32bit-logical-nor)
274 (bit-andc1 32bit-logical-andc1)
275 (bit-andc2 32bit-logical-andc2)
276 (bit-orc1 32bit-logical-orc1)
277 (bit-orc2 32bit-logical-orc2)))
278 (destructuring-bind (bitfun wordfun) x
280 ((bit-array-1 bit-array-2 result-bit-array)
281 '(simple-bit-vector simple-bit-vector simple-bit-vector) '*
282 :eval-name t :node node :policy (>= speed space))
284 ,@(unless (policy node (zerop safety))
285 '((unless (= (length bit-array-1) (length bit-array-2)
286 (length result-bit-array))
287 (error "Argument and/or result bit arrays are not the same length:~
289 bit-array-1 bit-array-2 result-bit-array))))
290 (do ((index sb!vm:vector-data-offset (1+ index))
291 (end (+ sb!vm:vector-data-offset
293 (+ (length bit-array-1)
296 ((= index end) result-bit-array)
297 (declare (optimize (speed 3) (safety 0))
298 (type index index end))
299 (setf (%raw-bits result-bit-array index)
300 (,wordfun (%raw-bits bit-array-1 index)
301 (%raw-bits bit-array-2 index))))))))
303 (deftransform bit-not
304 ((bit-array result-bit-array)
305 (simple-bit-vector simple-bit-vector) *
306 :node node :policy (>= speed space))
308 ,@(unless (policy node (zerop safety))
309 '((unless (= (length bit-array)
310 (length result-bit-array))
311 (error "Argument and result bit arrays are not the same length:~
313 bit-array result-bit-array))))
314 (do ((index sb!vm:vector-data-offset (1+ index))
315 (end (+ sb!vm:vector-data-offset
317 (+ (length bit-array)
318 (1- sb!vm:word-bits)))
320 ((= index end) result-bit-array)
321 (declare (optimize (speed 3) (safety 0))
322 (type index index end))
323 (setf (%raw-bits result-bit-array index)
324 (32bit-logical-not (%raw-bits bit-array index))))))
326 ;;;; primitive translator for BYTE-BLT
328 (def-primitive-translator byte-blt (src src-start dst dst-start dst-end)
330 (src-start (* ,src-start sb!vm:byte-bits))
332 (dst-start (* ,dst-start sb!vm:byte-bits))
333 (dst-end (* ,dst-end sb!vm:byte-bits)))
334 (let ((length (- dst-end dst-start)))
339 (system-area-copy src src-start dst dst-start length))
340 ((simple-unboxed-array (*))
341 (copy-from-system-area src src-start
342 dst (+ dst-start ,vector-data-bit-offset)
344 ((simple-unboxed-array (*))
347 (copy-to-system-area src (+ src-start ,vector-data-bit-offset)
350 ((simple-unboxed-array (*))
351 (bit-bash-copy src (+ src-start ,vector-data-bit-offset)
352 dst (+ dst-start ,vector-data-bit-offset)
355 ;;;; transforms for EQL of floating point values
357 (deftransform eql ((x y) (single-float single-float))
358 '(= (single-float-bits x) (single-float-bits y)))
360 (deftransform eql ((x y) (double-float double-float))
361 '(and (= (double-float-low-bits x) (double-float-low-bits y))
362 (= (double-float-high-bits x) (double-float-high-bits y))))