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 ;;; MNA: open-coded-simple-array patch
82 (deftransform data-vector-ref ((array index)
84 (let ((array-type (continuation-type array)))
85 (unless (array-type-p array-type)
86 (give-up-ir1-transform))
87 (let ((dims (array-type-dimensions array-type)))
88 (when (and (consp dims) (= (length dims) 1))
89 (give-up-ir1-transform))
90 (let* ((el-type (array-type-element-type array-type))
91 (total-size (if (or (atom dims) (member '* dims))
94 (type-sp `(simple-array ,(type-specifier el-type)
97 `(let ((a (truly-the ,type-sp (%array-simp array))))
98 (data-vector-ref a index))
99 `(let ((a (truly-the ,type-sp (%array-data-vector array))))
100 (data-vector-ref a index)))))))
102 (deftransform hairy-data-vector-set ((array index new-value)
106 "avoid runtime dispatch on array element type"
107 (let ((element-ctype (extract-upgraded-element-type array)))
108 (declare (type ctype element-ctype))
109 (when (eq *wild-type* element-ctype)
110 (give-up-ir1-transform
111 "Upgraded element type of array is not known at compile time."))
112 (let ((element-type-specifier (type-specifier element-ctype)))
113 `(multiple-value-bind (array index)
114 ;; FIXME: All this noise should move into a
115 ;; %DATA-VECTOR-AND-INDEX function, and there should be
116 ;; DEFTRANSFORMs for %DATA-VECTOR-AND-INDEX to optimize the
117 ;; function call away when the array is known to be simple,
118 ;; and to specialize to
119 ;; %DATA-VECTOR-AND-INDEX-IN-VECTOR-CASE when the array is
120 ;; known to have only one dimension.
121 (if (array-header-p array)
122 (%with-array-data array index nil)
124 (declare (type (simple-array ,element-type-specifier 1)
126 (%check-bound array 0 index)
127 (values array index)))
128 (data-vector-set (truly-the (simple-array ,element-type-specifier 1)
133 ;;; MNA: open-coded-simple-array patch
134 (deftransform data-vector-set ((array index new-value)
136 (let ((array-type (continuation-type array)))
137 (unless (array-type-p array-type)
138 (give-up-ir1-transform))
139 (let ((dims (array-type-dimensions array-type)))
140 (when (and (consp dims) (= (length dims) 1))
141 (give-up-ir1-transform))
142 (let* ((el-type (array-type-element-type array-type))
143 (total-size (if (or (atom dims) (member '* dims))
146 (type-sp `(simple-array ,(type-specifier el-type)
149 `(let ((a (truly-the ,type-sp (%array-simp array))))
150 (data-vector-set a index new-value))
151 `(let ((a (truly-the ,type-sp (%array-data-vector array))))
152 (data-vector-set a index new-value)))))))
154 ;;; transforms for getting at simple arrays of (UNSIGNED-BYTE N) when (< N 8)
156 ;;; FIXME: In CMU CL, these were commented out with #+NIL. Why? Should
157 ;;; we fix them or should we delete them? (Perhaps these definitions
158 ;;; predate the various DATA-VECTOR-REF-FOO VOPs which have
159 ;;; (:TRANSLATE DATA-VECTOR-REF), and are redundant now?)
163 (let ((elements-per-word (truncate sb!vm:word-bits bits)))
165 (deftransform data-vector-ref ((vector index)
167 `(multiple-value-bind (word bit)
168 (floor index ,',elements-per-word)
169 (ldb ,(ecase sb!vm:target-byte-order
170 (:little-endian '(byte ,bits (* bit ,bits)))
171 (:big-endian '(byte ,bits (- sb!vm:word-bits
172 (* (1+ bit) ,bits)))))
173 (%raw-bits vector (+ word sb!vm:vector-data-offset)))))
174 (deftransform data-vector-set ((vector index new-value)
176 `(multiple-value-bind (word bit)
177 (floor index ,',elements-per-word)
178 (setf (ldb ,(ecase sb!vm:target-byte-order
179 (:little-endian '(byte ,bits (* bit ,bits)))
181 '(byte ,bits (- sb!vm:word-bits
182 (* (1+ bit) ,bits)))))
183 (%raw-bits vector (+ word sb!vm:vector-data-offset)))
185 (frob simple-bit-vector 1)
186 (frob (simple-array (unsigned-byte 2) (*)) 2)
187 (frob (simple-array (unsigned-byte 4) (*)) 4))
189 ;;;; bit vector hackery
191 ;;; SIMPLE-BIT-VECTOR bit-array operations are transformed to a word loop that
192 ;;; does 32 bits at a time.
194 ;;; FIXME: This is a lot of repeatedly macroexpanded code. It should be a
195 ;;; function call instead. And do it with DEF-FROB instead of DOLIST.
196 (dolist (x '((bit-and 32bit-logical-and)
197 (bit-ior 32bit-logical-or)
198 (bit-xor 32bit-logical-xor)
199 (bit-eqv 32bit-logical-eqv)
200 (bit-nand 32bit-logical-nand)
201 (bit-nor 32bit-logical-nor)
202 (bit-andc1 32bit-logical-andc1)
203 (bit-andc2 32bit-logical-andc2)
204 (bit-orc1 32bit-logical-orc1)
205 (bit-orc2 32bit-logical-orc2)))
206 (destructuring-bind (bitfun wordfun) x
208 ((bit-array-1 bit-array-2 result-bit-array)
209 '(simple-bit-vector simple-bit-vector simple-bit-vector) '*
210 :eval-name t :node node :policy (>= speed space))
212 ,@(unless (policy node (zerop safety))
213 '((unless (= (length bit-array-1) (length bit-array-2)
214 (length result-bit-array))
215 (error "Argument and/or result bit arrays are not the same length:~
217 bit-array-1 bit-array-2 result-bit-array))))
218 (do ((index sb!vm:vector-data-offset (1+ index))
219 (end (+ sb!vm:vector-data-offset
221 (+ (length bit-array-1)
224 ((= index end) result-bit-array)
225 (declare (optimize (speed 3) (safety 0))
226 (type index index end))
227 (setf (%raw-bits result-bit-array index)
228 (,wordfun (%raw-bits bit-array-1 index)
229 (%raw-bits bit-array-2 index))))))))
231 (deftransform bit-not
232 ((bit-array result-bit-array)
233 (simple-bit-vector simple-bit-vector) *
234 :node node :policy (>= speed space))
236 ,@(unless (policy node (zerop safety))
237 '((unless (= (length bit-array)
238 (length result-bit-array))
239 (error "Argument and result bit arrays are not the same length:~
241 bit-array result-bit-array))))
242 (do ((index sb!vm:vector-data-offset (1+ index))
243 (end (+ sb!vm:vector-data-offset
245 (+ (length bit-array)
246 (1- sb!vm:word-bits)))
248 ((= index end) result-bit-array)
249 (declare (optimize (speed 3) (safety 0))
250 (type index index end))
251 (setf (%raw-bits result-bit-array index)
252 (32bit-logical-not (%raw-bits bit-array index))))))
254 ;;;; primitive translator for BYTE-BLT
256 (def-primitive-translator byte-blt (src src-start dst dst-start dst-end)
258 (src-start (* ,src-start sb!vm:byte-bits))
260 (dst-start (* ,dst-start sb!vm:byte-bits))
261 (dst-end (* ,dst-end sb!vm:byte-bits)))
262 (let ((length (- dst-end dst-start)))
267 (system-area-copy src src-start dst dst-start length))
268 ((simple-unboxed-array (*))
269 (copy-from-system-area src src-start
270 dst (+ dst-start ,vector-data-bit-offset)
272 ((simple-unboxed-array (*))
275 (copy-to-system-area src (+ src-start ,vector-data-bit-offset)
278 ((simple-unboxed-array (*))
279 (bit-bash-copy src (+ src-start ,vector-data-bit-offset)
280 dst (+ dst-start ,vector-data-bit-offset)
283 ;;;; transforms for EQL of floating point values
285 (deftransform eql ((x y) (single-float single-float))
286 '(= (single-float-bits x) (single-float-bits y)))
288 (deftransform eql ((x y) (double-float double-float))
289 '(and (= (double-float-low-bits x) (double-float-low-bits y))
290 (= (double-float-high-bits x) (double-float-high-bits y))))