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.
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))
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
24 (def-source-transform short-float-p (x) `(single-float-p ,x))
26 (def-source-transform long-float-p (x) `(double-float-p ,x))
28 (def-source-transform compiled-function-p (x)
31 (def-source-transform char-int (x)
34 (deftransform abs ((x) (rational))
35 '(if (< x 0) (- x) x))
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)))
43 ;;;; character support
45 ;;; In our implementation there are really only BASE-CHARs.
46 (def-source-transform characterp (obj)
49 ;;;; simplifying HAIRY-DATA-VECTOR-REF and HAIRY-DATA-VECTOR-SET
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)
74 (declare (type (simple-array ,element-type-specifier 1)
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)))))
81 (deftransform data-vector-ref ((array index)
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)
93 `(data-vector-ref (truly-the (simple-array ,(type-specifier el-type)
95 (%array-data-vector array))
98 (deftransform hairy-data-vector-set ((array index new-value)
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)
120 (declare (type (simple-array ,element-type-specifier 1)
122 (%check-bound array 0 index)
123 (values array index)))
124 (data-vector-set (truly-the (simple-array ,element-type-specifier 1)
129 (deftransform data-vector-set ((array index new-value)
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)
141 `(data-vector-set (truly-the (simple-array ,(type-specifier el-type)
143 (%array-data-vector array))
147 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
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?)
156 (let ((elements-per-word (truncate sb!vm:word-bits bits)))
158 (deftransform data-vector-ref ((vector index)
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)
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)))
174 '(byte ,bits (- sb!vm:word-bits
175 (* (1+ bit) ,bits)))))
176 (%raw-bits vector (+ word sb!vm:vector-data-offset)))
178 (frob simple-bit-vector 1)
179 (frob (simple-array (unsigned-byte 2) (*)) 2)
180 (frob (simple-array (unsigned-byte 4) (*)) 4))
182 ;;;; simple string transforms
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.
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.)
197 ;;; FIXME: Also, the transform should probably be dependent on
199 (deftransform replace ((string1 string2 &key (start1 0) (start2 0)
201 (simple-string simple-string &rest t))
203 (declare (optimize (safety 0)))
204 (bit-bash-copy string2
206 (+ (the index (* start2 sb!vm:byte-bits))
207 ,vector-data-bit-offset))
210 (+ (the index (* start1 sb!vm:byte-bits))
211 ,vector-data-bit-offset))
213 (* (min (the index (- (or end1 (length string1))
215 (the index (- (or end2 (length string2))
220 ;;;; bit vector hackery
222 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word loop that
223 ;;; does 32 bits at a time.
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
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))
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:~
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
252 (+ (length bit-array-1)
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))))))))
262 (deftransform bit-not
263 ((bit-array result-bit-array)
264 (simple-bit-vector simple-bit-vector) *
265 :node node :policy (>= speed space))
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:~
272 bit-array result-bit-array))))
273 (do ((index sb!vm:vector-data-offset (1+ index))
274 (end (+ sb!vm:vector-data-offset
276 (+ (length bit-array)
277 (1- 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))))))
285 ;;;; primitive translator for BYTE-BLT
287 (def-primitive-translator byte-blt (src src-start dst dst-start dst-end)
289 (src-start (* ,src-start sb!vm:byte-bits))
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)))
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)
303 ((simple-unboxed-array (*))
306 (copy-to-system-area src (+ src-start ,vector-data-bit-offset)
309 ((simple-unboxed-array (*))
310 (bit-bash-copy src (+ src-start ,vector-data-bit-offset)
311 dst (+ dst-start ,vector-data-bit-offset)
314 ;;;; transforms for EQL of floating point values
316 (deftransform eql ((x y) (single-float single-float))
317 '(= (single-float-bits x) (single-float-bits y)))
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))))