/[cmucl]/src/compiler/array-tran.lisp
ViewVC logotype

Contents of /src/compiler/array-tran.lisp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.45 - (show annotations)
Tue Apr 20 17:57:46 2010 UTC (3 years, 11 months ago) by rtoy
Branch: MAIN
CVS Tags: sparc-tramp-assem-base, release-20b-pre1, release-20b-pre2, sparc-tramp-assem-2010-07-19, GIT-CONVERSION, cross-sol-x86-merged, RELEASE_20b, cross-sol-x86-base, snapshot-2010-12, snapshot-2010-11, snapshot-2011-09, snapshot-2011-06, snapshot-2011-07, snapshot-2011-04, snapshot-2011-02, snapshot-2011-03, snapshot-2011-01, snapshot-2010-05, snapshot-2010-07, snapshot-2010-06, snapshot-2010-08, cross-sol-x86-2010-12-20, cross-sparc-branch-base, HEAD
Branch point for: cross-sparc-branch, RELEASE-20B-BRANCH, sparc-tramp-assem-branch, cross-sol-x86-branch
Changes since 1.44: +15 -15 lines
Change uses of _"foo" to (intl:gettext "foo").  This is because slime
may get confused with source locations if the reader macros are
installed.
1 ;;; -*- Package: C; Log: C.Log -*-
2 ;;;
3 ;;; **********************************************************************
4 ;;; This code was written as part of the CMU Common Lisp project at
5 ;;; Carnegie Mellon University, and has been placed in the public domain.
6 ;;;
7 (ext:file-comment
8 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/array-tran.lisp,v 1.45 2010/04/20 17:57:46 rtoy Rel $")
9 ;;;
10 ;;; **********************************************************************
11 ;;;
12 ;;; This file contains array specific optimizers and transforms.
13 ;;;
14 ;;; Extracted from srctran and extended by William Lott.
15 ;;;
16 (in-package "C")
17 (intl:textdomain "cmucl")
18
19
20 ;;;; Derive-Type Optimizers
21
22 ;;; ASSERT-ARRAY-RANK -- internal
23 ;;;
24 ;;; Array operations that use a specific number of indices implicitly assert
25 ;;; that the array is of that rank.
26 ;;;
27 (defun assert-array-rank (array rank)
28 (assert-continuation-type
29 array
30 (specifier-type `(array * ,(make-list rank :initial-element '*)))))
31
32 ;;; EXTRACT-ELEMENT-TYPE -- internal
33 ;;;
34 ;;; Array access functions return an object from the array, hence it's
35 ;;; type will be asserted to be array element type.
36 ;;;
37 (defun extract-element-type (array)
38 (let ((type (continuation-type array)))
39 (if (array-type-p type)
40 (array-type-element-type type)
41 *universal-type*)))
42
43 ;;; EXTRACT-UPGRADED-ELEMENT-TYPE -- internal
44 ;;;
45 ;;; Array access functions return an object from the array, hence it's
46 ;;; type is going to be the array upgraded element type.
47 ;;;
48 (defun extract-upgraded-element-type (array)
49 (let ((type (continuation-type array)))
50 (if (array-type-p type)
51 (array-type-specialized-element-type type)
52 *universal-type*)))
53
54 ;;; ASSERT-NEW-VALUE-TYPE -- internal
55 ;;;
56 ;;; The ``new-value'' for array setters must fit in the array, and the
57 ;;; return type is going to be the same as the new-value for setf functions.
58 ;;;
59 (defun assert-new-value-type (new-value array)
60 (let ((type (continuation-type array)))
61 (when (array-type-p type)
62 (assert-continuation-optional-type new-value
63 (array-type-specialized-element-type type))))
64 (continuation-type new-value))
65
66 ;;; Unsupplied-Or-NIL -- Internal
67 ;;;
68 ;;; Return true if Arg is NIL, or is a constant-continuation whose value is
69 ;;; NIL, false otherwise.
70 ;;;
71 (defun unsupplied-or-nil (arg)
72 (declare (type (or continuation null) arg))
73 (or (not arg)
74 (and (constant-continuation-p arg)
75 (not (continuation-value arg)))))
76
77
78 ;;; ARRAY-IN-BOUNDS-P -- derive-type optimizer.
79 ;;;
80 (defoptimizer (array-in-bounds-p derive-type) ((array &rest indices))
81 (assert-array-rank array (length indices))
82 *universal-type*)
83
84 ;;; AREF -- derive-type optimizer.
85 ;;;
86 (defoptimizer (aref derive-type) ((array &rest indices) node)
87 (assert-array-rank array (length indices))
88 ;; If the node continuation has a single use then assert its type.
89 ;;
90 ;; Let's not do this. As reported by Lynn Quam on cmucl-imp on
91 ;; 2004-03-30, the compiler generates bogus code for
92 ;;
93 ;; (defun foo (f d)
94 ;; (declare (type (simple-array single-float (*)) f)
95 ;; (type (simple-array double-float (*)) d))
96 ;; (setf (aref f 0) (aref d 0)))
97 ;;
98 ;; and doesn't even warn about the type mismatch. This might be a
99 ;; symptom of other compiler bugs, but removing this at least gives
100 ;; us back the warning. I (RLT) do not know what impact removing
101 ;; this has on other user code.
102 #+(or)
103 (let ((cont (node-cont node)))
104 (when (= (length (find-uses cont)) 1)
105 (assert-continuation-type cont (extract-upgraded-element-type array))))
106 (extract-upgraded-element-type array))
107
108 ;;; %ASET -- derive-type optimizer.
109 ;;;
110 (defoptimizer (%aset derive-type) ((array &rest stuff))
111 (assert-array-rank array (1- (length stuff)))
112 (assert-new-value-type (car (last stuff)) array)
113 ;; Without the following, this function
114 ;;
115 ;; (defun fn-492 (r p1)
116 ;; (declare (optimize speed (safety 1))
117 ;; (type (simple-array (signed-byte 8) nil) r) (type (integer * 22050378) p1))
118 ;; (setf (aref r) (lognand (the (integer 19464371) p1) 2257))
119 ;; (values))
120 ;; (defun tst-492 ()
121 ;; (let ((r (make-array nil :element-type '(signed-byte 8))))
122 ;; (fn-492 r 19469591)
123 ;; (aref r)))
124 ;;
125 ;; causes the compiler to delete (values) as unreachable and in so
126 ;; doing, deletes the return, so we just run off the end. I (rtoy)
127 ;; think this is caused by some confusion in type-derivation. The
128 ;; derived result of the lognand is bigger than a (signed-byte 8).
129 ;;
130 ;; I think doing this also causes some loss, because we return the
131 ;; element-type of the array, even though the result of the aset is
132 ;; the new value. Well, almost. The element type is returned only
133 ;; if the array continuation is really an array type. Otherwise, we
134 ;; do return the type of the new value.
135 ;;
136 ;; FIXME: This needs something better, but I (rtoy) am not smart
137 ;; enough to know what to do about it.
138 (let ((atype (continuation-type array)))
139 (if (array-type-p atype)
140 (array-type-specialized-element-type atype)
141 (continuation-type (car (last stuff))))))
142
143 ;;; DATA-VECTOR-REF -- derive-type optimizer.
144 ;;;
145 (defoptimizer (data-vector-ref derive-type) ((array index))
146 (extract-upgraded-element-type array))
147
148 ;;; DATA-VECTOR-SET -- derive-type optimizer.
149 ;;;
150 (defoptimizer (data-vector-set derive-type) ((array index new-value))
151 (assert-new-value-type new-value array))
152
153 ;;; %WITH-ARRAY-DATA -- derive-type optimizer.
154 ;;;
155 ;;; Figure out the type of the data vector if we know the argument element
156 ;;; type.
157 ;;;
158 (defoptimizer (%with-array-data derive-type) ((array start end))
159 (let ((atype (continuation-type array)))
160 (when (array-type-p atype)
161 (values-specifier-type
162 `(values (simple-array ,(type-specifier
163 (array-type-element-type atype))
164 (*))
165 index index index)))))
166
167
168 ;;; ARRAY-ROW-MAJOR-INDEX -- derive-type optimizer.
169 ;;;
170 (defoptimizer (array-row-major-index derive-type) ((array &rest indices))
171 (assert-array-rank array (length indices))
172 *universal-type*)
173
174 ;;; ROW-MAJOR-AREF -- derive-type optimizer.
175 ;;;
176 (defoptimizer (row-major-aref derive-type) ((array index))
177 (extract-upgraded-element-type array))
178
179 ;;; %SET-ROW-MAJOR-AREF -- derive-type optimizer.
180 ;;;
181 (defoptimizer (%set-row-major-aref derive-type) ((array index new-value))
182 (assert-new-value-type new-value array))
183
184 ;;; MAKE-ARRAY -- derive-type optimizer.
185 ;;;
186 (defoptimizer (make-array derive-type)
187 ((dims &key initial-element element-type initial-contents
188 adjustable fill-pointer displaced-index-offset displaced-to))
189 (let ((simple (and (unsupplied-or-nil adjustable)
190 (unsupplied-or-nil displaced-to)
191 (unsupplied-or-nil fill-pointer))))
192 (specifier-type
193 `(,(if simple 'simple-array 'array)
194 ,(cond ((not element-type) 't)
195 ((constant-continuation-p element-type)
196 (continuation-value element-type))
197 (t
198 '*))
199 ,(cond ((not simple)
200 ;; Can't derive the actual dimensions lest someone do
201 ;; an ADJUST-ARRAY, but we can try to get the rank
202 ;; correct.
203 (cond ((constant-continuation-p dims)
204 (let ((val (continuation-value dims)))
205 (if (listp val)
206 (make-list (length val) :initial-element '*)
207 '(*))))
208 ((csubtypep (continuation-type dims)
209 (specifier-type 'integer))
210 '(*))
211 (t
212 '*)))
213 ((constant-continuation-p dims)
214 (let ((val (continuation-value dims)))
215 (if (listp val) val (list val))))
216 ((csubtypep (continuation-type dims)
217 (specifier-type 'integer))
218 '(*))
219 (t
220 '*))))))
221
222
223 ;;;; Constructors.
224
225 ;;; VECTOR -- source-transform.
226 ;;;
227 ;;; Convert VECTOR into a make-array followed by setfs of all the elements.
228 ;;;
229 (def-source-transform vector (&rest elements)
230 (if (byte-compiling)
231 (values nil t)
232 (let ((len (length elements))
233 (n -1))
234 (once-only ((n-vec `(make-array ,len)))
235 `(progn
236 ,@(mapcar #'(lambda (el)
237 (once-only ((n-val el))
238 `(locally (declare (optimize (safety 0)))
239 (setf (svref ,n-vec ,(incf n))
240 ,n-val))))
241 elements)
242 ,n-vec)))))
243
244
245 ;;; MAKE-STRING -- source-transform.
246 ;;;
247 ;;; Just convert it into a make-array.
248 ;;;
249 (deftransform make-string ((length &key (element-type 'base-char)
250 (initial-element #\NULL)))
251 `(make-array (the (values index &rest t) length)
252 :element-type element-type
253 :initial-element initial-element))
254
255 (defconstant array-info
256 '((base-char #\NULL #.vm:char-bits vm:simple-string-type)
257 (single-float 0.0f0 32 vm:simple-array-single-float-type)
258 (double-float 0.0d0 64 vm:simple-array-double-float-type)
259 #+long-float (long-float 0.0l0 #+x86 96 #+sparc 128
260 vm:simple-array-long-float-type)
261 #+double-double
262 (double-double-float 0w0 128
263 vm::simple-array-double-double-float-type)
264 (bit 0 1 vm:simple-bit-vector-type)
265 ((unsigned-byte 2) 0 2 vm:simple-array-unsigned-byte-2-type)
266 ((unsigned-byte 4) 0 4 vm:simple-array-unsigned-byte-4-type)
267 ((unsigned-byte 8) 0 8 vm:simple-array-unsigned-byte-8-type)
268 ((unsigned-byte 16) 0 16 vm:simple-array-unsigned-byte-16-type)
269 ((unsigned-byte 32) 0 32 vm:simple-array-unsigned-byte-32-type)
270 ((signed-byte 8) 0 8 vm:simple-array-signed-byte-8-type)
271 ((signed-byte 16) 0 16 vm:simple-array-signed-byte-16-type)
272 ((signed-byte 30) 0 32 vm:simple-array-signed-byte-30-type)
273 ((signed-byte 32) 0 32 vm:simple-array-signed-byte-32-type)
274 ((complex single-float) #C(0.0f0 0.0f0) 64
275 vm:simple-array-complex-single-float-type)
276 ((complex double-float) #C(0.0d0 0.0d0) 128
277 vm:simple-array-complex-double-float-type)
278 #+long-float
279 ((complex long-float) #C(0.0l0 0.0l0) #+x86 192 #+sparc 256
280 vm:simple-array-complex-long-float-type)
281 #+double-double
282 ((complex double-double-float) #C(0.0w0 0.0w0) 256
283 vm::simple-array-complex-double-double-float-type)
284 (t 0 32 vm:simple-vector-type)))
285
286 ;;; MAKE-ARRAY -- source-transform.
287 ;;;
288 ;;; The integer type restriction on the length assures that it will be a
289 ;;; vector. The lack of adjustable, fill-pointer, and displaced-to keywords
290 ;;; assures that it will be simple.
291 ;;;
292 (deftransform make-array ((length &key initial-element element-type)
293 (integer &rest *))
294 (let* ((eltype (cond ((not element-type) t)
295 ((not (constant-continuation-p element-type))
296 (give-up (intl:gettext "Element-Type is not constant.")))
297 (t
298 (continuation-value element-type))))
299 (len (if (constant-continuation-p length)
300 (continuation-value length)
301 '*))
302 (spec `(simple-array ,eltype (,len)))
303 (eltype-type (specifier-type eltype)))
304 (multiple-value-bind
305 (default-initial-element element-size typecode)
306 (dolist (info array-info
307 (give-up (intl:gettext "Cannot open-code creation of ~S") spec))
308 (when (csubtypep eltype-type (specifier-type (car info)))
309 (return (values-list (cdr info)))))
310 (let* ((nwords-form
311 (if (>= element-size vm:word-bits)
312 `(* length ,(/ element-size vm:word-bits))
313 (let ((elements-per-word (/ 32 element-size)))
314 `(truncate (+ length
315 ,(if (eq 'vm:simple-string-type typecode)
316 elements-per-word
317 (1- elements-per-word)))
318 ,elements-per-word))))
319 (constructor
320 `(truly-the ,spec
321 (allocate-vector ,typecode length ,nwords-form))))
322 (values
323 (cond ((and default-initial-element
324 (or (null initial-element)
325 (and (constant-continuation-p initial-element)
326 (eql (continuation-value initial-element)
327 default-initial-element))))
328 (unless (csubtypep (ctype-of default-initial-element)
329 eltype-type)
330 (compiler-note _N"Default initial element ~s is not a ~s."
331 default-initial-element eltype))
332 constructor)
333 (t
334 `(truly-the ,spec (fill ,constructor initial-element))))
335 '((declare (type index length))))))))
336
337 ;;; MAKE-ARRAY -- transform.
338 ;;;
339 ;;; The list type restriction does not assure that the result will be a
340 ;;; multi-dimensional array. But the lack of
341 ;;;
342 (deftransform make-array ((dims &key initial-element element-type)
343 (list &rest *))
344 (unless (or (null element-type) (constant-continuation-p element-type))
345 (give-up (intl:gettext "Element-type not constant; cannot open code array creation")))
346 (unless (constant-continuation-p dims)
347 (give-up (intl:gettext "Dimension list not constant; cannot open code array creation")))
348 (let ((dims (continuation-value dims)))
349 (unless (every #'integerp dims)
350 (give-up (intl:gettext "Dimension list contains something other than an integer: ~S")
351 dims))
352 (if (= (length dims) 1)
353 `(make-array ',(car dims)
354 ,@(when initial-element
355 '(:initial-element initial-element))
356 ,@(when element-type
357 '(:element-type element-type)))
358 (let* ((total-size (reduce #'* dims))
359 (rank (length dims))
360 (spec `(simple-array
361 ,(cond ((null element-type) t)
362 ((constant-continuation-p element-type)
363 (continuation-value element-type))
364 (t '*))
365 ,(make-list rank :initial-element '*))))
366 `(let ((header (make-array-header vm:simple-array-type ,rank)))
367 (setf (%array-fill-pointer header) ,total-size)
368 (setf (%array-fill-pointer-p header) nil)
369 (setf (%array-available-elements header) ,total-size)
370 (setf (%array-data-vector header)
371 (make-array ,total-size
372 ,@(when element-type
373 '(:element-type element-type))
374 ,@(when initial-element
375 '(:initial-element initial-element))))
376 (setf (%array-displaced-p header) nil)
377 ,@(let ((axis -1))
378 (mapcar #'(lambda (dim)
379 `(setf (%array-dimension header ,(incf axis))
380 ,dim))
381 dims))
382 (truly-the ,spec header))))))
383
384
385 ;;;; Random properties of arrays.
386
387 ;;; Transforms for various random array properties. If the property is know
388 ;;; at compile time because of a type spec, use that constant value.
389
390 ;;; ARRAY-RANK -- transform.
391 ;;;
392 ;;; If we can tell the rank from the type info, use it instead.
393 ;;;
394 (deftransform array-rank ((array))
395 (let ((array-type (continuation-type array)))
396 (unless (array-type-p array-type)
397 (give-up))
398 (let ((dims (array-type-dimensions array-type)))
399 (if (not (listp dims))
400 (give-up (intl:gettext "Array rank not known at compile time: ~S") dims)
401 (length dims)))))
402
403 ;;; ARRAY-DIMENSION -- transform.
404 ;;;
405 ;;; If we know the dimensions at compile time, just use it. Otherwise, if
406 ;;; we can tell that the axis is in bounds, convert to %array-dimension
407 ;;; (which just indirects the array header) or length (if it's simple and a
408 ;;; vector).
409 ;;;
410 (deftransform array-dimension ((array axis)
411 (array index))
412 (unless (constant-continuation-p axis)
413 (give-up (intl:gettext "Axis not constant.")))
414 (let ((array-type (continuation-type array))
415 (axis (continuation-value axis)))
416 (unless (array-type-p array-type)
417 (give-up))
418 (let ((dims (array-type-dimensions array-type)))
419 (unless (listp dims)
420 (give-up
421 (intl:gettext "Array dimensions unknown, must call array-dimension at runtime.")))
422 (unless (> (length dims) axis)
423 (abort-transform (intl:gettext "Array has dimensions ~S, ~D is too large.")
424 dims axis))
425 (let ((dim (nth axis dims)))
426 (cond ((integerp dim)
427 dim)
428 ((= (length dims) 1)
429 (ecase (array-type-complexp array-type)
430 ((t)
431 '(%array-dimension array 0))
432 ((nil)
433 '(length array))
434 ((:maybe *)
435 (give-up (intl:gettext "Can't tell if array is simple.")))))
436 (t
437 '(%array-dimension array axis)))))))
438
439 ;;; LENGTH -- transform.
440 ;;;
441 ;;; If the length has been declared and it's simple, just return it.
442 ;;;
443 (deftransform length ((vector)
444 ((simple-array * (*))))
445 (let ((type (continuation-type vector)))
446 (unless (array-type-p type)
447 (give-up))
448 (let ((dims (array-type-dimensions type)))
449 (unless (and (listp dims) (integerp (car dims)))
450 (give-up (intl:gettext "Vector length unknown, must call length at runtime.")))
451 (car dims))))
452
453 ;;; LENGTH -- transform.
454 ;;;
455 ;;; All vectors can get their length by using vector-length. If it's simple,
456 ;;; it will extract the length slot from the vector. It it's complex, it will
457 ;;; extract the fill pointer slot from the array header.
458 ;;;
459 (deftransform length ((vector) (vector))
460 '(vector-length vector))
461
462
463 ;;; If a simple array with known dimensions, then vector-length is a
464 ;;; compile-time constant.
465 ;;;
466 (deftransform vector-length ((vector) ((simple-array * (*))))
467 (let ((vtype (continuation-type vector)))
468 (if (array-type-p vtype)
469 (let ((dim (first (array-type-dimensions vtype))))
470 (when (eq dim '*) (give-up))
471 dim)
472 (give-up))))
473
474
475 ;;; ARRAY-TOTAL-SIZE -- transform.
476 ;;;
477 ;;; Again, if we can tell the results from the type, just use it. Otherwise,
478 ;;; if we know the rank, convert into a computation based on array-dimension.
479 ;;; We can wrap a truly-the index around the multiplications because we know
480 ;;; that the total size must be an index.
481 ;;;
482 (deftransform array-total-size ((array)
483 (array))
484 (let ((array-type (continuation-type array)))
485 (unless (array-type-p array-type)
486 (give-up))
487 (let ((dims (array-type-dimensions array-type)))
488 (unless (listp dims)
489 (give-up (intl:gettext "Can't tell the rank at compile time.")))
490 (if (member '* dims)
491 (do ((form 1 `(truly-the index
492 (* (array-dimension array ,i) ,form)))
493 (i 0 (1+ i)))
494 ((= i (length dims)) form))
495 (reduce #'* dims)))))
496
497 ;;; ARRAY-HAS-FILL-POINTER-P -- transform.
498 ;;;
499 ;;; Only complex vectors have fill pointers.
500 ;;;
501 (deftransform array-has-fill-pointer-p ((array))
502 (let ((array-type (continuation-type array)))
503 (unless (array-type-p array-type)
504 (give-up))
505 (let ((dims (array-type-dimensions array-type)))
506 (if (and (listp dims) (not (= (length dims) 1)))
507 nil
508 (ecase (array-type-complexp array-type)
509 ((t)
510 t)
511 ((nil)
512 nil)
513 (:maybe
514 (give-up (intl:gettext "Array type ambiguous; must call ~
515 array-has-fill-pointer-p at runtime."))))))))
516
517 ;;; %CHECK-BOUND -- transform.
518 ;;;
519 ;;; Primitive used to verify indicies into arrays. If we can tell at
520 ;;; compile-time or we are generating unsafe code, don't bother with the VOP.
521 ;;;
522 (deftransform %check-bound ((array dimension index))
523 (unless (constant-continuation-p dimension)
524 (give-up))
525 (let ((dim (continuation-value dimension)))
526 `(the (integer 0 (,dim)) index)))
527 ;;;
528 (deftransform %check-bound ((array dimension index) * *
529 :policy (and (> speed safety) (= safety 0)))
530 'index)
531
532
533 ;;; WITH-ROW-MAJOR-INDEX -- internal.
534 ;;;
535 ;;; Handy macro for computing the row-major index given a set of indices. We
536 ;;; wrap each index with a call to %check-bound to assure that everything
537 ;;; works out correctly. We can wrap all the interior arith with truly-the
538 ;;; index because we know the the resultant row-major index must be an index.
539 ;;;
540 (eval-when (compile eval)
541 ;;;
542 (defmacro with-row-major-index ((array indices index &optional new-value)
543 &rest body)
544 `(let (n-indices dims)
545 (dotimes (i (length ,indices))
546 (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
547 (push (make-symbol (format nil "DIM-~D" i)) dims))
548 (setf n-indices (nreverse n-indices))
549 (setf dims (nreverse dims))
550 `(lambda (,',array ,@n-indices ,@',(when new-value (list new-value)))
551 (let* (,@(let ((,index -1))
552 (mapcar #'(lambda (name)
553 `(,name (array-dimension ,',array
554 ,(incf ,index))))
555 dims))
556 (,',index
557 ,(if (null dims)
558 0
559 (do* ((dims dims (cdr dims))
560 (indices n-indices (cdr indices))
561 (last-dim nil (car dims))
562 (form `(%check-bound ,',array
563 ,(car dims)
564 ,(car indices))
565 `(truly-the index
566 (+ (truly-the index
567 (* ,form
568 ,last-dim))
569 (%check-bound
570 ,',array
571 ,(car dims)
572 ,(car indices))))))
573 ((null (cdr dims)) form)))))
574 ,',@body))))
575 ;;;
576 ); eval-when
577
578 ;;; ARRAY-ROW-MAJOR-INDEX -- transform.
579 ;;;
580 ;;; Just return the index after computing it.
581 ;;;
582 (deftransform array-row-major-index ((array &rest indices))
583 (with-row-major-index (array indices index)
584 index))
585
586
587
588 ;;;; Array accessors:
589
590 ;;; SVREF, %SVSET, SCHAR, %SCHARSET, CHAR,
591 ;;; %CHARSET, SBIT, %SBITSET, BIT, %BITSET
592 ;;; -- source transforms.
593 ;;;
594 ;;; We convert all typed array accessors into aref and %aset with type
595 ;;; assertions on the array.
596 ;;;
597 (macrolet ((frob (reffer setter type)
598 `(progn
599 (def-source-transform ,reffer (a &rest i)
600 (if (byte-compiling)
601 (values nil t)
602 `(aref (the ,',type ,a) ,@i)))
603 (def-source-transform ,setter (a &rest i)
604 (if (byte-compiling)
605 (values nil t)
606 `(%aset (the ,',type ,a) ,@i))))))
607 (frob sbit %sbitset (simple-array bit))
608 (frob bit %bitset (array bit)))
609
610 (macrolet ((frob (reffer setter type)
611 `(progn
612 (def-source-transform ,reffer (a i)
613 (if (byte-compiling)
614 (values nil t)
615 `(aref (the ,',type ,a) ,i)))
616 (def-source-transform ,setter (a i v)
617 (if (byte-compiling)
618 (values nil t)
619 `(%aset (the ,',type ,a) ,i ,v))))))
620 (frob svref %svset simple-vector)
621 (frob schar %scharset simple-string)
622 (frob char %charset string))
623
624 ;;; AREF, %ASET -- transform.
625 ;;;
626 ;;; Convert into a data-vector-ref (or set) with the set of indices replaced
627 ;;; with the an expression for the row major index.
628 ;;;
629 (deftransform aref ((array &rest indices))
630 (with-row-major-index (array indices index)
631 (data-vector-ref array index)))
632 ;;;
633 (deftransform %aset ((array &rest stuff))
634 (let ((indices (butlast stuff)))
635 (with-row-major-index (array indices index new-value)
636 (data-vector-set array index new-value))))
637
638 ;;; ROW-MAJOR-AREF, %SET-ROW-MAJOR-AREF -- transform.
639 ;;;
640 ;;; Just convert into a data-vector-ref (or set) after checking that the
641 ;;; index is inside the array total size.
642 ;;;
643 (deftransform row-major-aref ((array index))
644 `(data-vector-ref array (%check-bound array (array-total-size array) index)))
645 ;;;
646 (deftransform %set-row-major-aref ((array index new-value))
647 `(data-vector-set array
648 (%check-bound array (array-total-size array) index)
649 new-value))
650
651
652 ;;;; Bit-vector array operation canonicalization:
653 ;;;
654 ;;; We convert all bit-vector operations to have the result array specified.
655 ;;; This allows any result allocation to be open-coded, and eliminates the need
656 ;;; for any VM-dependent transforms to handle these cases.
657
658 (dolist (fun '(bit-and bit-ior bit-xor bit-eqv bit-nand bit-nor bit-andc1
659 bit-andc2 bit-orc1 bit-orc2))
660 ;;
661 ;; Make a result array if result is NIL or unsupplied.
662 (deftransform fun ((bit-array-1 bit-array-2 &optional result-bit-array)
663 '(bit-vector bit-vector &optional null) '*
664 :eval-name t :policy (>= speed space))
665 `(,fun bit-array-1 bit-array-2
666 (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
667 ;;
668 ;; If result its T, make it the first arg.
669 (deftransform fun ((bit-array-1 bit-array-2 result-bit-array)
670 '(bit-vector bit-vector (member t)) '*
671 :eval-name t)
672 `(,fun bit-array-1 bit-array-2 bit-array-1)))
673
674 ;;; Similar for BIT-NOT, but there is only one arg...
675 ;;;
676 (deftransform bit-not ((bit-array-1 &optional result-bit-array)
677 (bit-vector &optional null) *
678 :policy (>= speed space))
679 '(bit-not bit-array-1
680 (make-array (length bit-array-1) :element-type 'bit)))
681 ;;;
682 (deftransform bit-not ((bit-array-1 result-bit-array)
683 (bit-vector (constant-argument t)))
684 '(bit-not bit-array-1 bit-array-1))
685
686
687 ;;; ARRAY-HEADER-P -- transform.
688 ;;;
689 ;;; Pick off some constant cases.
690 ;;;
691 (deftransform array-header-p ((array) (array))
692 (let ((type (continuation-type array)))
693 (declare (optimize (safety 3)))
694 (unless (array-type-p type)
695 (give-up))
696 (let ((dims (array-type-dimensions type)))
697 (cond ((csubtypep type (specifier-type '(simple-array * (*))))
698 ;; No array header.
699 nil)
700 ((and (listp dims) (> (length dims) 1))
701 ;; Multi-dimensional array, will have a header.
702 t)
703 (t
704 (give-up))))))

  ViewVC Help
Powered by ViewVC 1.1.5