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

  ViewVC Help
Powered by ViewVC 1.1.5