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
22 ;;; transform picks whichever predicate was defined last when there
23 ;;; are multiple predicates for equivalent types.
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 ;;; MNA: open-coded-simple-array patch
82 (deftransform data-vector-ref ((array index)
84 (let ((array-type (continuation-type array)))
85 ;; FIXME: How could this happen? Doesn't the limitation to arg
86 ;; type SIMPLE-ARRAY guarantee that ARRAY-TYPE is an ARRAY-TYPE?
87 (unless (array-type-p array-type)
88 (give-up-ir1-transform))
89 (let ((dims (array-type-dimensions array-type)))
90 (when (and (consp dims) (= (length dims) 1))
91 (give-up-ir1-transform))
92 (let* ((el-type (array-type-element-type array-type))
93 (total-size (if (or (atom dims) (member '* dims))
96 (type-sp `(simple-array ,(type-specifier el-type)
99 `(let ((a (truly-the ,type-sp (%array-simp array))))
100 (data-vector-ref a index))
101 `(let ((a (truly-the ,type-sp (%array-data-vector array))))
102 (data-vector-ref a index)))))))
104 (deftransform hairy-data-vector-set ((array index new-value)
108 "avoid runtime dispatch on array element type"
109 (let ((element-ctype (extract-upgraded-element-type array)))
110 (declare (type ctype element-ctype))
111 (when (eq *wild-type* element-ctype)
112 (give-up-ir1-transform
113 "Upgraded element type of array is not known at compile time."))
114 (let ((element-type-specifier (type-specifier element-ctype)))
115 `(multiple-value-bind (array index)
116 ;; FIXME: All this noise should move into a
117 ;; %DATA-VECTOR-AND-INDEX function, and there should be
118 ;; DEFTRANSFORMs for %DATA-VECTOR-AND-INDEX to optimize the
119 ;; function call away when the array is known to be simple,
120 ;; and to specialize to
121 ;; %DATA-VECTOR-AND-INDEX-IN-VECTOR-CASE when the array is
122 ;; known to have only one dimension.
123 (if (array-header-p array)
124 (%with-array-data array index nil)
126 (declare (type (simple-array ,element-type-specifier 1)
128 (%check-bound array 0 index)
129 (values array index)))
130 (data-vector-set (truly-the (simple-array ,element-type-specifier 1)
135 ;;; MNA: open-coded-simple-array patch
136 (deftransform data-vector-set ((array index new-value)
138 (let ((array-type (continuation-type array)))
139 ;; FIXME: How could this happen? Doesn't the limitation to arg
140 ;; type SIMPLE-ARRAY guarantee that ARRAY-TYPE is an ARRAY-TYPE?
141 (unless (array-type-p array-type)
142 (give-up-ir1-transform))
143 (let ((dims (array-type-dimensions array-type)))
144 (when (and (consp dims) (= (length dims) 1))
145 (give-up-ir1-transform))
146 (let* ((el-type (array-type-element-type array-type))
147 (total-size (if (or (atom dims) (member '* dims))
150 (type-sp `(simple-array ,(type-specifier el-type)
153 `(let ((a (truly-the ,type-sp (%array-simp array))))
154 (data-vector-set a index new-value))
155 `(let ((a (truly-the ,type-sp (%array-data-vector array))))
156 (data-vector-set a index new-value)))))))
158 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
160 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
161 ;;; we fix them or should we delete them? (Perhaps these definitions
162 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
163 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
167 (let ((elements-per-word (truncate sb!vm:word-bits bits)))
169 (deftransform data-vector-ref ((vector index)
171 `(multiple-value-bind (word bit)
172 (floor index ,',elements-per-word)
173 (ldb ,(ecase sb!vm:target-byte-order
174 (:little-endian '(byte ,bits (* bit ,bits)))
175 (:big-endian '(byte ,bits (- sb!vm:word-bits
176 (* (1+ bit) ,bits)))))
177 (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
178 (deftransform data-vector-set ((vector index new-value)
180 `(multiple-value-bind (word bit)
181 (floor index ,',elements-per-word)
182 (setf (ldb ,(ecase sb!vm:target-byte-order
183 (:little-endian '(byte ,bits (* bit ,bits)))
185 '(byte ,bits (- sb!vm:word-bits
186 (* (1+ bit) ,bits)))))
187 (%raw-bits vector (+ word sb!vm:vector-data-offset)))
189 (frob simple-bit-vector 1)
190 (frob (simple-array (unsigned-byte 2) (*)) 2)
191 (frob (simple-array (unsigned-byte 4) (*)) 4))
193 ;;;; bit vector hackery
195 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word loop that
196 ;;; does 32 bits at a time.
198 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should be a
199 ;;; function call instead. And do it with DEF-FROB instead of DOLIST.
200 (dolist (x '((bit-and 32bit-logical-and)
201 (bit-ior 32bit-logical-or)
202 (bit-xor 32bit-logical-xor)
203 (bit-eqv 32bit-logical-eqv)
204 (bit-nand 32bit-logical-nand)
205 (bit-nor 32bit-logical-nor)
206 (bit-andc1 32bit-logical-andc1)
207 (bit-andc2 32bit-logical-andc2)
208 (bit-orc1 32bit-logical-orc1)
209 (bit-orc2 32bit-logical-orc2)))
210 (destructuring-bind (bitfun wordfun) x
212 ((bit-array-1 bit-array-2 result-bit-array)
213 '(simple-bit-vector simple-bit-vector simple-bit-vector) '*
214 :eval-name t :node node :policy (>= speed space))
216 ,@(unless (policy node (zerop safety))
217 '((unless (= (length bit-array-1) (length bit-array-2)
218 (length result-bit-array))
219 (error "Argument and/or result bit arrays are not the same length:~
221 bit-array-1 bit-array-2 result-bit-array))))
222 (do ((index sb!vm:vector-data-offset (1+ index))
223 (end (+ sb!vm:vector-data-offset
225 (+ (length bit-array-1)
228 ((= index end) result-bit-array)
229 (declare (optimize (speed 3) (safety 0))
230 (type index index end))
231 (setf (%raw-bits result-bit-array index)
232 (,wordfun (%raw-bits bit-array-1 index)
233 (%raw-bits bit-array-2 index))))))))
235 (deftransform bit-not
236 ((bit-array result-bit-array)
237 (simple-bit-vector simple-bit-vector) *
238 :node node :policy (>= speed space))
240 ,@(unless (policy node (zerop safety))
241 '((unless (= (length bit-array)
242 (length result-bit-array))
243 (error "Argument and result bit arrays are not the same length:~
245 bit-array result-bit-array))))
246 (do ((index sb!vm:vector-data-offset (1+ index))
247 (end (+ sb!vm:vector-data-offset
249 (+ (length bit-array)
250 (1- sb!vm:word-bits)))
252 ((= index end) result-bit-array)
253 (declare (optimize (speed 3) (safety 0))
254 (type index index end))
255 (setf (%raw-bits result-bit-array index)
256 (32bit-logical-not (%raw-bits bit-array index))))))
258 ;;;; primitive translator for BYTE-BLT
260 (def-primitive-translator byte-blt (src src-start dst dst-start dst-end)
262 (src-start (* ,src-start sb!vm:byte-bits))
264 (dst-start (* ,dst-start sb!vm:byte-bits))
265 (dst-end (* ,dst-end sb!vm:byte-bits)))
266 (let ((length (- dst-end dst-start)))
271 (system-area-copy src src-start dst dst-start length))
272 ((simple-unboxed-array (*))
273 (copy-from-system-area src src-start
274 dst (+ dst-start ,vector-data-bit-offset)
276 ((simple-unboxed-array (*))
279 (copy-to-system-area src (+ src-start ,vector-data-bit-offset)
282 ((simple-unboxed-array (*))
283 (bit-bash-copy src (+ src-start ,vector-data-bit-offset)
284 dst (+ dst-start ,vector-data-bit-offset)
287 ;;;; transforms for EQL of floating point values
289 (deftransform eql ((x y) (single-float single-float))
290 '(= (single-float-bits x) (single-float-bits y)))
292 (deftransform eql ((x y) (double-float double-float))
293 '(and (= (double-float-low-bits x) (double-float-low-bits y))
294 (= (double-float-high-bits x) (double-float-high-bits y))))