/[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.42.2.2 - (hide annotations)
Sun May 18 22:55:24 2008 UTC (5 years, 11 months ago) by rtoy
Branch: unicode-utf16-branch
CVS Tags: unicode-utf16-string-support
Changes since 1.42.2.1: +1 -2 lines
MAKE-STRING deftransform is ok.
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.42.2.2 "$Header: /tiger/var/lib/cvsroots/cmucl/src/compiler/array-tran.lisp,v 1.42.2.2 2008/05/18 22:55:24 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 rtoy 1.42 ;; Can't derive the actual dimensions lest someone do
200     ;; an ADJUST-ARRAY, but we can try to get the rank
201     ;; correct.
202     (cond ((constant-continuation-p dims)
203     (let ((val (continuation-value dims)))
204     (if (listp val)
205     (make-list (length val) :initial-element '*)
206     '(*))))
207     ((csubtypep (continuation-type dims)
208     (specifier-type 'integer))
209     '(*))
210     (t
211     '*)))
212 wlott 1.11 ((constant-continuation-p dims)
213     (let ((val (continuation-value dims)))
214     (if (listp val) val (list val))))
215     ((csubtypep (continuation-type dims)
216     (specifier-type 'integer))
217     '(*))
218     (t
219     '*))))))
220 wlott 1.1
221    
222     ;;;; Constructors.
223    
224     ;;; VECTOR -- source-transform.
225     ;;;
226     ;;; Convert VECTOR into a make-array followed by setfs of all the elements.
227     ;;;
228     (def-source-transform vector (&rest elements)
229 ram 1.16 (if (byte-compiling)
230 ram 1.15 (values nil t)
231     (let ((len (length elements))
232     (n -1))
233     (once-only ((n-vec `(make-array ,len)))
234     `(progn
235     ,@(mapcar #'(lambda (el)
236     (once-only ((n-val el))
237     `(locally (declare (optimize (safety 0)))
238     (setf (svref ,n-vec ,(incf n))
239     ,n-val))))
240     elements)
241     ,n-vec)))))
242 wlott 1.1
243    
244     ;;; MAKE-STRING -- source-transform.
245     ;;;
246     ;;; Just convert it into a make-array.
247     ;;;
248 gerd 1.31 (deftransform make-string ((length &key (element-type 'base-char)
249 gerd 1.30 (initial-element #\NULL)))
250     `(make-array (the (values index &rest t) length)
251     :element-type element-type
252     :initial-element initial-element))
253 wlott 1.1
254     (defconstant array-info
255 rtoy 1.42.2.1 '((base-char #\NULL #-unicode 8 #+unicode 16 vm:simple-string-type)
256 rtoy 1.36 (single-float 0.0f0 32 vm:simple-array-single-float-type)
257 wlott 1.1 (double-float 0.0d0 64 vm:simple-array-double-float-type)
258 dtc 1.26 #+long-float (long-float 0.0l0 #+x86 96 #+sparc 128
259     vm:simple-array-long-float-type)
260 rtoy 1.41 #+double-double
261     (double-double-float 0w0 128
262     vm::simple-array-double-double-float-type)
263 wlott 1.1 (bit 0 1 vm:simple-bit-vector-type)
264     ((unsigned-byte 2) 0 2 vm:simple-array-unsigned-byte-2-type)
265     ((unsigned-byte 4) 0 4 vm:simple-array-unsigned-byte-4-type)
266     ((unsigned-byte 8) 0 8 vm:simple-array-unsigned-byte-8-type)
267     ((unsigned-byte 16) 0 16 vm:simple-array-unsigned-byte-16-type)
268     ((unsigned-byte 32) 0 32 vm:simple-array-unsigned-byte-32-type)
269 dtc 1.27 ((signed-byte 8) 0 8 vm:simple-array-signed-byte-8-type)
270     ((signed-byte 16) 0 16 vm:simple-array-signed-byte-16-type)
271     ((signed-byte 30) 0 32 vm:simple-array-signed-byte-30-type)
272     ((signed-byte 32) 0 32 vm:simple-array-signed-byte-32-type)
273 rtoy 1.36 ((complex single-float) #C(0.0f0 0.0f0) 64
274 dtc 1.27 vm:simple-array-complex-single-float-type)
275     ((complex double-float) #C(0.0d0 0.0d0) 128
276     vm:simple-array-complex-double-float-type)
277     #+long-float
278 dtc 1.26 ((complex long-float) #C(0.0l0 0.0l0) #+x86 192 #+sparc 256
279     vm:simple-array-complex-long-float-type)
280 rtoy 1.41 #+double-double
281     ((complex double-double-float) #C(0.0w0 0.0w0) 256
282     vm::simple-array-complex-double-double-float-type)
283 wlott 1.1 (t 0 32 vm:simple-vector-type)))
284    
285     ;;; MAKE-ARRAY -- source-transform.
286     ;;;
287     ;;; The integer type restriction on the length assures that it will be a
288     ;;; vector. The lack of adjustable, fill-pointer, and displaced-to keywords
289     ;;; assures that it will be simple.
290     ;;;
291     (deftransform make-array ((length &key initial-element element-type)
292     (integer &rest *))
293     (let* ((eltype (cond ((not element-type) t)
294     ((not (constant-continuation-p element-type))
295     (give-up "Element-Type is not constant."))
296     (t
297     (continuation-value element-type))))
298     (len (if (constant-continuation-p length)
299     (continuation-value length)
300     '*))
301     (spec `(simple-array ,eltype (,len)))
302     (eltype-type (specifier-type eltype)))
303     (multiple-value-bind
304     (default-initial-element element-size typecode)
305     (dolist (info array-info
306     (give-up "Cannot open-code creation of ~S" spec))
307     (when (csubtypep eltype-type (specifier-type (car info)))
308     (return (values-list (cdr info)))))
309 wlott 1.6 (let* ((nwords-form
310     (if (>= element-size vm:word-bits)
311     `(* length ,(/ element-size vm:word-bits))
312     (let ((elements-per-word (/ 32 element-size)))
313     `(truncate (+ length
314     ,(if (eq 'vm:simple-string-type typecode)
315     elements-per-word
316     (1- elements-per-word)))
317     ,elements-per-word))))
318     (constructor
319     `(truly-the ,spec
320     (allocate-vector ,typecode length ,nwords-form))))
321 wlott 1.1 (values
322 dtc 1.22 (cond ((and default-initial-element
323     (or (null initial-element)
324     (and (constant-continuation-p initial-element)
325     (eql (continuation-value initial-element)
326     default-initial-element))))
327     (unless (csubtypep (ctype-of default-initial-element)
328     eltype-type)
329     (compiler-note "Default initial element ~s is not a ~s."
330     default-initial-element eltype))
331     constructor)
332     (t
333     `(truly-the ,spec (fill ,constructor initial-element))))
334 wlott 1.1 '((declare (type index length))))))))
335    
336     ;;; MAKE-ARRAY -- transform.
337     ;;;
338     ;;; The list type restriction does not assure that the result will be a
339     ;;; multi-dimensional array. But the lack of
340     ;;;
341     (deftransform make-array ((dims &key initial-element element-type)
342     (list &rest *))
343     (unless (or (null element-type) (constant-continuation-p element-type))
344     (give-up "Element-type not constant; cannot open code array creation"))
345     (unless (constant-continuation-p dims)
346     (give-up "Dimension list not constant; cannot open code array creation"))
347     (let ((dims (continuation-value dims)))
348     (unless (every #'integerp dims)
349 wlott 1.6 (give-up "Dimension list contains something other than an integer: ~S"
350 wlott 1.1 dims))
351     (if (= (length dims) 1)
352     `(make-array ',(car dims)
353     ,@(when initial-element
354     '(:initial-element initial-element))
355     ,@(when element-type
356     '(:element-type element-type)))
357     (let* ((total-size (reduce #'* dims))
358     (rank (length dims))
359     (spec `(simple-array
360     ,(cond ((null element-type) t)
361     ((constant-continuation-p element-type)
362     (continuation-value element-type))
363     (t '*))
364     ,(make-list rank :initial-element '*))))
365     `(let ((header (make-array-header vm:simple-array-type ,rank)))
366     (setf (%array-fill-pointer header) ,total-size)
367     (setf (%array-fill-pointer-p header) nil)
368     (setf (%array-available-elements header) ,total-size)
369     (setf (%array-data-vector header)
370     (make-array ,total-size
371     ,@(when element-type
372     '(:element-type element-type))
373     ,@(when initial-element
374     '(:initial-element initial-element))))
375     (setf (%array-displaced-p header) nil)
376     ,@(let ((axis -1))
377     (mapcar #'(lambda (dim)
378     `(setf (%array-dimension header ,(incf axis))
379     ,dim))
380     dims))
381     (truly-the ,spec header))))))
382    
383    
384     ;;;; Random properties of arrays.
385    
386     ;;; Transforms for various random array properties. If the property is know
387     ;;; at compile time because of a type spec, use that constant value.
388    
389     ;;; ARRAY-RANK -- transform.
390     ;;;
391     ;;; If we can tell the rank from the type info, use it instead.
392     ;;;
393     (deftransform array-rank ((array))
394     (let ((array-type (continuation-type array)))
395     (unless (array-type-p array-type)
396     (give-up))
397     (let ((dims (array-type-dimensions array-type)))
398     (if (not (listp dims))
399     (give-up "Array rank not known at compile time: ~S" dims)
400     (length dims)))))
401    
402     ;;; ARRAY-DIMENSION -- transform.
403     ;;;
404     ;;; If we know the dimensions at compile time, just use it. Otherwise, if
405     ;;; we can tell that the axis is in bounds, convert to %array-dimension
406     ;;; (which just indirects the array header) or length (if it's simple and a
407     ;;; vector).
408     ;;;
409     (deftransform array-dimension ((array axis)
410     (array index))
411     (unless (constant-continuation-p axis)
412     (give-up "Axis not constant."))
413     (let ((array-type (continuation-type array))
414     (axis (continuation-value axis)))
415     (unless (array-type-p array-type)
416     (give-up))
417     (let ((dims (array-type-dimensions array-type)))
418     (unless (listp dims)
419     (give-up
420     "Array dimensions unknown, must call array-dimension at runtime."))
421     (unless (> (length dims) axis)
422     (abort-transform "Array has dimensions ~S, ~D is too large."
423     dims axis))
424     (let ((dim (nth axis dims)))
425     (cond ((integerp dim)
426     dim)
427     ((= (length dims) 1)
428     (ecase (array-type-complexp array-type)
429     ((t)
430     '(%array-dimension array 0))
431     ((nil)
432     '(length array))
433 gerd 1.32 ((:maybe *)
434 wlott 1.1 (give-up "Can't tell if array is simple."))))
435     (t
436     '(%array-dimension array axis)))))))
437    
438     ;;; LENGTH -- transform.
439     ;;;
440     ;;; If the length has been declared and it's simple, just return it.
441     ;;;
442     (deftransform length ((vector)
443     ((simple-array * (*))))
444     (let ((type (continuation-type vector)))
445     (unless (array-type-p type)
446     (give-up))
447     (let ((dims (array-type-dimensions type)))
448     (unless (and (listp dims) (integerp (car dims)))
449     (give-up "Vector length unknown, must call length at runtime."))
450     (car dims))))
451    
452     ;;; LENGTH -- transform.
453     ;;;
454     ;;; All vectors can get their length by using vector-length. If it's simple,
455     ;;; it will extract the length slot from the vector. It it's complex, it will
456     ;;; extract the fill pointer slot from the array header.
457     ;;;
458     (deftransform length ((vector) (vector))
459     '(vector-length vector))
460    
461 ram 1.7
462     ;;; If a simple array with known dimensions, then vector-length is a
463     ;;; compile-time constant.
464     ;;;
465     (deftransform vector-length ((vector) ((simple-array * (*))))
466     (let ((vtype (continuation-type vector)))
467     (if (array-type-p vtype)
468     (let ((dim (first (array-type-dimensions vtype))))
469     (when (eq dim '*) (give-up))
470     dim)
471     (give-up))))
472    
473    
474 wlott 1.1 ;;; ARRAY-TOTAL-SIZE -- transform.
475     ;;;
476     ;;; Again, if we can tell the results from the type, just use it. Otherwise,
477     ;;; if we know the rank, convert into a computation based on array-dimension.
478     ;;; We can wrap a truly-the index around the multiplications because we know
479     ;;; that the total size must be an index.
480     ;;;
481     (deftransform array-total-size ((array)
482     (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     (unless (listp dims)
488 wlott 1.2 (give-up "Can't tell the rank at compile time."))
489     (if (member '* dims)
490     (do ((form 1 `(truly-the index
491     (* (array-dimension array ,i) ,form)))
492     (i 0 (1+ i)))
493     ((= i (length dims)) form))
494     (reduce #'* dims)))))
495 wlott 1.1
496     ;;; ARRAY-HAS-FILL-POINTER-P -- transform.
497     ;;;
498     ;;; Only complex vectors have fill pointers.
499     ;;;
500     (deftransform array-has-fill-pointer-p ((array))
501     (let ((array-type (continuation-type array)))
502     (unless (array-type-p array-type)
503     (give-up))
504     (let ((dims (array-type-dimensions array-type)))
505     (if (and (listp dims) (not (= (length dims) 1)))
506     nil
507     (ecase (array-type-complexp array-type)
508     ((t)
509     t)
510     ((nil)
511     nil)
512 rtoy 1.39 (:maybe
513 wlott 1.1 (give-up "Array type ambiguous; must call ~
514     array-has-fill-pointer-p at runtime.")))))))
515    
516     ;;; %CHECK-BOUND -- transform.
517     ;;;
518     ;;; Primitive used to verify indicies into arrays. If we can tell at
519     ;;; compile-time or we are generating unsafe code, don't bother with the VOP.
520     ;;;
521     (deftransform %check-bound ((array dimension index))
522     (unless (constant-continuation-p dimension)
523     (give-up))
524     (let ((dim (continuation-value dimension)))
525 gerd 1.34 `(the (integer 0 (,dim)) index)))
526 wlott 1.1 ;;;
527     (deftransform %check-bound ((array dimension index) * *
528     :policy (and (> speed safety) (= safety 0)))
529     'index)
530    
531    
532     ;;; WITH-ROW-MAJOR-INDEX -- internal.
533     ;;;
534     ;;; Handy macro for computing the row-major index given a set of indices. We
535     ;;; wrap each index with a call to %check-bound to assure that everything
536     ;;; works out correctly. We can wrap all the interior arith with truly-the
537     ;;; index because we know the the resultant row-major index must be an index.
538     ;;;
539     (eval-when (compile eval)
540     ;;;
541     (defmacro with-row-major-index ((array indices index &optional new-value)
542     &rest body)
543     `(let (n-indices dims)
544     (dotimes (i (length ,indices))
545     (push (make-symbol (format nil "INDEX-~D" i)) n-indices)
546     (push (make-symbol (format nil "DIM-~D" i)) dims))
547     (setf n-indices (nreverse n-indices))
548     (setf dims (nreverse dims))
549     `(lambda (,',array ,@n-indices ,@',(when new-value (list new-value)))
550     (let* (,@(let ((,index -1))
551     (mapcar #'(lambda (name)
552     `(,name (array-dimension ,',array
553     ,(incf ,index))))
554     dims))
555     (,',index
556     ,(if (null dims)
557     0
558     (do* ((dims dims (cdr dims))
559     (indices n-indices (cdr indices))
560     (last-dim nil (car dims))
561     (form `(%check-bound ,',array
562     ,(car dims)
563     ,(car indices))
564     `(truly-the index
565     (+ (truly-the index
566     (* ,form
567     ,last-dim))
568     (%check-bound
569     ,',array
570     ,(car dims)
571     ,(car indices))))))
572     ((null (cdr dims)) form)))))
573     ,',@body))))
574     ;;;
575     ); eval-when
576    
577     ;;; ARRAY-ROW-MAJOR-INDEX -- transform.
578     ;;;
579     ;;; Just return the index after computing it.
580     ;;;
581     (deftransform array-row-major-index ((array &rest indices))
582     (with-row-major-index (array indices index)
583     index))
584    
585    
586    
587     ;;;; Array accessors:
588    
589     ;;; SVREF, %SVSET, SCHAR, %SCHARSET, CHAR,
590     ;;; %CHARSET, SBIT, %SBITSET, BIT, %BITSET
591     ;;; -- source transforms.
592     ;;;
593     ;;; We convert all typed array accessors into aref and %aset with type
594     ;;; assertions on the array.
595     ;;;
596     (macrolet ((frob (reffer setter type)
597     `(progn
598     (def-source-transform ,reffer (a &rest i)
599 ram 1.16 (if (byte-compiling)
600 ram 1.15 (values nil t)
601     `(aref (the ,',type ,a) ,@i)))
602 wlott 1.1 (def-source-transform ,setter (a &rest i)
603 ram 1.16 (if (byte-compiling)
604 ram 1.15 (values nil t)
605     `(%aset (the ,',type ,a) ,@i))))))
606 gerd 1.33 (frob sbit %sbitset (simple-array bit))
607     (frob bit %bitset (array bit)))
608    
609     (macrolet ((frob (reffer setter type)
610     `(progn
611     (def-source-transform ,reffer (a i)
612     (if (byte-compiling)
613     (values nil t)
614     `(aref (the ,',type ,a) ,i)))
615     (def-source-transform ,setter (a i v)
616     (if (byte-compiling)
617     (values nil t)
618     `(%aset (the ,',type ,a) ,i ,v))))))
619 wlott 1.1 (frob svref %svset simple-vector)
620     (frob schar %scharset simple-string)
621 gerd 1.33 (frob char %charset string))
622 wlott 1.1
623     ;;; AREF, %ASET -- transform.
624     ;;;
625     ;;; Convert into a data-vector-ref (or set) with the set of indices replaced
626     ;;; with the an expression for the row major index.
627     ;;;
628     (deftransform aref ((array &rest indices))
629     (with-row-major-index (array indices index)
630     (data-vector-ref array index)))
631     ;;;
632     (deftransform %aset ((array &rest stuff))
633     (let ((indices (butlast stuff)))
634     (with-row-major-index (array indices index new-value)
635     (data-vector-set array index new-value))))
636    
637     ;;; ROW-MAJOR-AREF, %SET-ROW-MAJOR-AREF -- transform.
638     ;;;
639     ;;; Just convert into a data-vector-ref (or set) after checking that the
640     ;;; index is inside the array total size.
641     ;;;
642     (deftransform row-major-aref ((array index))
643     `(data-vector-ref array (%check-bound array (array-total-size array) index)))
644     ;;;
645     (deftransform %set-row-major-aref ((array index new-value))
646     `(data-vector-set array
647     (%check-bound array (array-total-size array) index)
648     new-value))
649 ram 1.7
650    
651     ;;;; Bit-vector array operation canonicalization:
652     ;;;
653     ;;; We convert all bit-vector operations to have the result array specified.
654     ;;; This allows any result allocation to be open-coded, and eliminates the need
655     ;;; for any VM-dependent transforms to handle these cases.
656    
657     (dolist (fun '(bit-and bit-ior bit-xor bit-eqv bit-nand bit-nor bit-andc1
658     bit-andc2 bit-orc1 bit-orc2))
659     ;;
660     ;; Make a result array if result is NIL or unsupplied.
661     (deftransform fun ((bit-array-1 bit-array-2 &optional result-bit-array)
662 ram 1.14 '(bit-vector bit-vector &optional null) '*
663 ram 1.7 :eval-name t :policy (>= speed space))
664     `(,fun bit-array-1 bit-array-2
665 rtoy 1.40 (make-array (array-dimension bit-array-1 0) :element-type 'bit)))
666 ram 1.7 ;;
667     ;; If result its T, make it the first arg.
668     (deftransform fun ((bit-array-1 bit-array-2 result-bit-array)
669 ram 1.14 '(bit-vector bit-vector (member t)) '*
670 ram 1.7 :eval-name t)
671     `(,fun bit-array-1 bit-array-2 bit-array-1)))
672    
673     ;;; Similar for BIT-NOT, but there is only one arg...
674     ;;;
675     (deftransform bit-not ((bit-array-1 &optional result-bit-array)
676     (bit-vector &optional null) *
677     :policy (>= speed space))
678     '(bit-not bit-array-1
679     (make-array (length bit-array-1) :element-type 'bit)))
680     ;;;
681     (deftransform bit-not ((bit-array-1 result-bit-array)
682     (bit-vector (constant-argument t)))
683 ram 1.17 '(bit-not bit-array-1 bit-array-1))
684 dtc 1.20
685    
686     ;;; ARRAY-HEADER-P -- transform.
687     ;;;
688     ;;; Pick off some constant cases.
689     ;;;
690     (deftransform array-header-p ((array) (array))
691     (let ((type (continuation-type array)))
692     (declare (optimize (safety 3)))
693     (unless (array-type-p type)
694     (give-up))
695     (let ((dims (array-type-dimensions type)))
696     (cond ((csubtypep type (specifier-type '(simple-array * (*))))
697     ;; No array header.
698     nil)
699     ((and (listp dims) (> (length dims) 1))
700     ;; Multi-dimensional array, will have a header.
701     t)
702     (t
703     (give-up))))))

  ViewVC Help
Powered by ViewVC 1.1.5