1 ;;;; functions to implement arrays
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.
12 (in-package "SB!IMPL")
15 (declaim (inline adjustable-array-p
18 ;;;; miscellaneous accessor functions
20 ;;; These functions are only needed by the interpreter, 'cause the
21 ;;; compiler inlines them.
22 (macrolet ((def (name)
26 (defun (setf ,name
) (value array
)
27 (setf (,name array
) value
)))))
28 (def %array-fill-pointer
)
29 (def %array-fill-pointer-p
)
30 (def %array-available-elements
)
31 (def %array-data-vector
)
32 (def %array-displacement
)
33 (def %array-displaced-p
)
34 (def %array-displaced-from
))
36 (defun %array-rank
(array)
39 (defun %array-dimension
(array axis
)
40 (%array-dimension array axis
))
42 (defun %set-array-dimension
(array axis value
)
43 (%set-array-dimension array axis value
))
45 (defun %check-bound
(array bound index
)
46 (declare (type index bound
)
48 (%check-bound array bound index
))
50 (defun check-bound (array bound index
)
51 (declare (type index bound
)
53 (%check-bound array bound index
)
56 (defun %with-array-data
/fp
(array start end
)
57 (%with-array-data-macro array start end
:check-bounds t
:check-fill-pointer t
))
59 (defun %with-array-data
(array start end
)
60 (%with-array-data-macro array start end
:check-bounds t
:check-fill-pointer nil
))
62 (defun %data-vector-and-index
(array index
)
63 (if (array-header-p array
)
64 (multiple-value-bind (vector index
)
65 (%with-array-data array index nil
)
66 (values vector index
))
67 (values array index
)))
70 (defun %integer-vector-widetag-and-n-bits
(signed high
)
72 #.
(let ((map (make-array (1+ sb
!vm
:n-word-bits
))))
73 (loop for saetp across
74 (reverse sb
!vm
:*specialized-array-element-type-properties
*)
75 for ctype
= (sb!vm
:saetp-ctype saetp
)
76 when
(and (numeric-type-p ctype
)
77 (eq (numeric-type-class ctype
) 'integer
)
78 (zerop (numeric-type-low ctype
)))
79 do
(fill map
(cons (sb!vm
:saetp-typecode saetp
)
80 (sb!vm
:saetp-n-bits saetp
))
81 :end
(1+ (integer-length (numeric-type-high ctype
)))))
84 #.
(let ((map (make-array (1+ sb
!vm
:n-word-bits
))))
85 (loop for saetp across
86 (reverse sb
!vm
:*specialized-array-element-type-properties
*)
87 for ctype
= (sb!vm
:saetp-ctype saetp
)
88 when
(and (numeric-type-p ctype
)
89 (eq (numeric-type-class ctype
) 'integer
)
90 (minusp (numeric-type-low ctype
)))
91 do
(fill map
(cons (sb!vm
:saetp-typecode saetp
)
92 (sb!vm
:saetp-n-bits saetp
))
93 :end
(+ (integer-length (numeric-type-high ctype
)) 2)))
95 (cond ((> high sb
!vm
:n-word-bits
)
96 (values #.sb
!vm
:simple-vector-widetag
#.sb
!vm
:n-word-bits
))
98 (let ((x (aref signed-table high
)))
99 (values (car x
) (cdr x
))))
101 (let ((x (aref unsigned-table high
)))
102 (values (car x
) (cdr x
)))))))
104 ;;; This is a bit complicated, but calling subtypep over all
105 ;;; specialized types is exceedingly slow
106 (defun %vector-widetag-and-n-bits
(type)
107 (macrolet ((with-parameters ((arg-type &key intervals
)
108 (&rest args
) &body body
)
109 (let ((type-sym (gensym)))
110 `(let (,@(loop for arg in args
112 (declare (ignorable ,@args
))
114 (let ((,type-sym
(cdr type
)))
116 ,@(loop for arg in args
118 `(cond ((consp ,type-sym
)
119 (let ((value (pop ,type-sym
)))
120 (if (or (eq value
'*)
121 (typep value
',arg-type
)
137 (let ((value (symbol-value widetag
)))
141 sb
!vm
:*specialized-array-element-type-properties
*
142 :key
#'sb
!vm
:saetp-typecode
))))))
144 (error "Invalid type specifier: ~s" type
))
145 (integer-interval-widetag (low high
)
147 (%integer-vector-widetag-and-n-bits
149 (1+ (max (integer-length low
) (integer-length high
))))
150 (%integer-vector-widetag-and-n-bits
152 (max (integer-length low
) (integer-length high
))))))
153 (let* ((consp (consp type
))
161 (result sb
!vm
:simple-vector-widetag
))
162 ((base-char standard-char
#!-sb-unicode character
)
165 (result sb
!vm
:simple-base-string-widetag
))
167 ((character extended-char
)
170 (result sb
!vm
:simple-character-string-widetag
))
174 (result sb
!vm
:simple-bit-vector-widetag
))
178 (result sb
!vm
:simple-array-fixnum-widetag
))
180 (with-parameters ((integer 1)) (high)
182 (result sb
!vm
:simple-vector-widetag
)
183 (%integer-vector-widetag-and-n-bits nil high
))))
185 (with-parameters ((integer 1)) (high)
187 (result sb
!vm
:simple-vector-widetag
)
188 (%integer-vector-widetag-and-n-bits t high
))))
190 (with-parameters (double-float :intervals t
) (low high
)
191 (if (and (not (eq low
'*))
193 (if (or (consp low
) (consp high
))
194 (>= (type-bound-number low
) (type-bound-number high
))
196 (result sb
!vm
:simple-array-nil-widetag
)
197 (result sb
!vm
:simple-array-double-float-widetag
))))
199 (with-parameters (single-float :intervals t
) (low high
)
200 (if (and (not (eq low
'*))
202 (if (or (consp low
) (consp high
))
203 (>= (type-bound-number low
) (type-bound-number high
))
205 (result sb
!vm
:simple-array-nil-widetag
)
206 (result sb
!vm
:simple-array-single-float-widetag
))))
208 (if (and (consp type
)
211 (typep (cadr type
) '(integer 1)))
212 (%integer-vector-widetag-and-n-bits
213 nil
(integer-length (1- (cadr type
))))
217 (with-parameters (long-float :intervals t
) (low high
)
218 (if (and (not (eq low
'*))
220 (if (or (consp low
) (consp high
))
221 (>= (type-bound-number low
) (type-bound-number high
))
223 (result sb
!vm
:simple-array-nil-widetag
)
224 (result sb
!vm
:simple-array-long-float-widetag
))))
226 (with-parameters (integer :intervals t
) (low high
)
227 (let ((low (if (consp low
)
230 (high (if (consp high
)
233 (cond ((or (eq high
'*)
235 (result sb
!vm
:simple-vector-widetag
))
237 (result sb
!vm
:simple-array-nil-widetag
))
239 (integer-interval-widetag low high
))))))
241 (with-parameters (t) (subtype)
243 (result sb
!vm
:simple-vector-widetag
)
244 (let ((ctype (specifier-type type
)))
245 (cond ((eq ctype
*empty-type
*)
246 (result sb
!vm
:simple-array-nil-widetag
))
247 ((union-type-p ctype
)
248 (cond ((csubtypep ctype
(specifier-type '(complex double-float
)))
250 sb
!vm
:simple-array-complex-double-float-widetag
))
251 ((csubtypep ctype
(specifier-type '(complex single-float
)))
253 sb
!vm
:simple-array-complex-single-float-widetag
))
255 ((csubtypep ctype
(specifier-type '(complex long-float
)))
257 sb
!vm
:simple-array-complex-long-float-widetag
))
259 (result sb
!vm
:simple-vector-widetag
))))
261 (case (numeric-type-format ctype
)
264 sb
!vm
:simple-array-complex-double-float-widetag
))
267 sb
!vm
:simple-array-complex-single-float-widetag
))
271 sb
!vm
:simple-array-complex-long-float-widetag
))
273 (result sb
!vm
:simple-vector-widetag
)))))))))
275 (result sb
!vm
:simple-array-nil-widetag
))
278 (let ((ctype (type-or-nil-if-unknown type
)))
280 (return (result sb
!vm
:simple-vector-widetag
)))
283 (let ((types (union-type-types ctype
)))
284 (cond ((not (every #'numeric-type-p types
))
285 (result sb
!vm
:simple-vector-widetag
))
286 ((csubtypep ctype
(specifier-type 'integer
))
287 (integer-interval-widetag
288 (reduce #'min types
:key
#'numeric-type-low
)
289 (reduce #'max types
:key
#'numeric-type-high
)))
290 ((csubtypep ctype
(specifier-type 'double-float
))
291 (result sb
!vm
:simple-array-double-float-widetag
))
292 ((csubtypep ctype
(specifier-type 'single-float
))
293 (result sb
!vm
:simple-array-single-float-widetag
))
295 ((csubtypep ctype
(specifier-type 'long-float
))
296 (result sb
!vm
:simple-array-long-float-widetag
))
298 (result sb
!vm
:simple-vector-widetag
)))))
300 #!-sb-unicode
(result sb
!vm
:simple-base-string-widetag
)
302 (if (loop for
(start . end
)
303 in
(character-set-type-pairs ctype
)
304 always
(and (< start base-char-code-limit
)
305 (< end base-char-code-limit
)))
306 (result sb
!vm
:simple-base-string-widetag
)
307 (result sb
!vm
:simple-character-string-widetag
)))
309 (let ((expansion (type-specifier ctype
)))
310 (if (equal expansion type
)
311 (result sb
!vm
:simple-vector-widetag
)
312 (%vector-widetag-and-n-bits expansion
)))))))))))))
314 (defun %complex-vector-widetag
(widetag)
315 (macrolet ((make-case ()
317 ,@(loop for saetp across sb
!vm
:*specialized-array-element-type-properties
*
318 for complex
= (sb!vm
:saetp-complex-typecode saetp
)
320 collect
(list (sb!vm
:saetp-typecode saetp
) complex
))
322 #.sb
!vm
:complex-vector-widetag
))))
325 (defglobal %%simple-array-n-bits%%
(make-array (1+ sb
!vm
:widetag-mask
)))
326 #.
(loop for info across sb
!vm
:*specialized-array-element-type-properties
*
327 collect
`(setf (aref %%simple-array-n-bits%%
,(sb!vm
:saetp-typecode info
))
328 ,(sb!vm
:saetp-n-bits info
)) into forms
329 finally
(return `(progn ,@forms
)))
331 (declaim (type (simple-vector #.
(1+ sb
!vm
:widetag-mask
)) %%simple-array-n-bits%%
))
333 (defun allocate-vector-with-widetag (widetag length
&optional n-bits
)
334 (declare (type (unsigned-byte 8) widetag
)
336 (let ((n-bits (or n-bits
(aref %%simple-array-n-bits%% widetag
))))
337 (declare (type (integer 0 256) n-bits
))
338 (allocate-vector widetag length
340 (* (if (or (= widetag sb
!vm
:simple-base-string-widetag
)
343 sb
!vm
:simple-character-string-widetag
))
347 sb
!vm
:n-word-bits
))))
349 (defun array-underlying-widetag (array)
350 (macrolet ((make-case ()
352 ,@(loop for saetp across sb
!vm
:*specialized-array-element-type-properties
*
353 for complex
= (sb!vm
:saetp-complex-typecode saetp
)
355 collect
(list complex
(sb!vm
:saetp-typecode saetp
)))
356 ((,sb
!vm
:simple-array-widetag
357 ,sb
!vm
:complex-vector-widetag
358 ,sb
!vm
:complex-array-widetag
)
359 (with-array-data ((array array
) (start) (end))
360 (declare (ignore start end
))
361 (%other-pointer-widetag array
)))
364 (let ((widetag (%other-pointer-widetag array
)))
367 (defun make-vector-like (vector length
)
368 (allocate-vector-with-widetag (array-underlying-widetag vector
) length
))
370 ;; Complain in various ways about wrong :INITIAL-foo arguments,
371 ;; returning the two initialization arguments needed for DATA-VECTOR-FROM-INITS.
372 (defun validate-array-initargs (element-p element contents-p contents displaced
)
373 (cond ((and displaced
(or element-p contents-p
))
374 (if (and element-p contents-p
)
375 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
376 may be specified with the :DISPLACED-TO option")
377 (error "~S may not be specified with the :DISPLACED-TO option"
378 (if element-p
:initial-element
:initial-contents
))))
379 ((and element-p contents-p
)
380 (error "Can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
381 (element-p (values :initial-element element
))
382 (contents-p (values :initial-contents contents
))
383 (t (values nil nil
))))
385 (declaim (inline %save-displaced-array-backpointer
))
386 (defun %save-displaced-array-backpointer
(array data
)
387 (flet ((purge (pointers)
388 (remove-if (lambda (value)
389 (or (not value
) (eq array value
)))
391 :key
#'weak-pointer-value
)))
392 ;; Add backpointer to the new data vector if it has a header.
393 (when (array-header-p data
)
394 (setf (%array-displaced-from data
)
395 (cons (make-weak-pointer array
)
396 (purge (%array-displaced-from data
)))))
397 ;; Remove old backpointer, if any.
398 (let ((old-data (%array-data-vector array
)))
399 (when (and (neq data old-data
) (array-header-p old-data
))
400 (setf (%array-displaced-from old-data
)
401 (purge (%array-displaced-from old-data
)))))))
403 ;;; Widetag is the widetag of the underlying vector,
404 ;;; it'll be the same as the resulting array widetag only for simple vectors
405 (defun %make-array
(dimensions widetag n-bits
408 (initial-element nil initial-element-p
)
409 (initial-contents nil initial-contents-p
)
410 adjustable fill-pointer
411 displaced-to displaced-index-offset
)
412 (declare (ignore element-type
))
413 (binding* (((array-rank dimension-0
)
414 (if (listp dimensions
)
415 (values (length dimensions
)
416 (if dimensions
(car dimensions
) 1))
417 (values 1 dimensions
)))
418 ((initialize initial-data
)
419 (validate-array-initargs initial-element-p initial-element
420 initial-contents-p initial-contents
422 (simple (and (null fill-pointer
)
424 (null displaced-to
))))
425 (declare (type array-rank array-rank
))
426 (declare (type index dimension-0
))
427 (cond ((and displaced-index-offset
(null displaced-to
))
428 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
429 ((and simple
(= array-rank
1))
430 (let ((vector ; a (SIMPLE-ARRAY * (*))
431 (allocate-vector-with-widetag widetag dimension-0 n-bits
)))
432 ;; presence of at most one :INITIAL-thing keyword was ensured above
433 (cond (initial-element-p
434 (fill vector initial-element
))
436 (let ((content-length (length initial-contents
)))
437 (unless (= dimension-0 content-length
)
438 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
439 the vector length is ~W."
440 content-length dimension-0
)))
441 (replace vector initial-contents
)))
443 ((and (arrayp displaced-to
)
444 (/= (array-underlying-widetag displaced-to
) widetag
))
445 (error "Array element type of :DISPLACED-TO array does not match specified element type"))
447 ;; it's non-simple or multidimensional, or both.
449 (unless (= array-rank
1)
450 (error "Only vectors can have fill pointers."))
451 (when (and (integerp fill-pointer
) (> fill-pointer dimension-0
))
452 ;; FIXME: should be TYPE-ERROR?
453 (error "invalid fill-pointer ~W" fill-pointer
)))
455 (if (consp dimensions
)
456 (the index
(reduce (lambda (a b
) (* a
(the index b
)))
458 ;; () is considered to have dimension-0 = 1.
459 ;; It avoids the REDUCE lambda being called with no args.
461 (data (or displaced-to
462 (data-vector-from-inits
463 dimensions total-size nil widetag n-bits
464 initialize initial-data
)))
465 (array (make-array-header
466 (cond ((= array-rank
1)
467 (%complex-vector-widetag widetag
))
468 (simple sb
!vm
:simple-array-widetag
)
469 (t sb
!vm
:complex-array-widetag
))
472 (setf (%array-fill-pointer-p array
) t
473 (%array-fill-pointer array
)
474 (if (eq fill-pointer t
) dimension-0 fill-pointer
))
475 (setf (%array-fill-pointer-p array
) nil
476 (%array-fill-pointer array
) total-size
))
477 (setf (%array-available-elements array
) total-size
)
478 ;; Terrible name for this slot - we displace to the
479 ;; target array's header, if any, not the "ultimate"
480 ;; vector in the chain of displacements.
481 (setf (%array-data-vector array
) data
)
482 (setf (%array-displaced-from array
) nil
)
484 (let ((offset (or displaced-index-offset
0)))
485 (when (> (+ offset total-size
)
486 (array-total-size displaced-to
))
487 (error "~S doesn't have enough elements." displaced-to
))
488 (setf (%array-displacement array
) offset
)
489 (setf (%array-displaced-p array
) t
)
490 (%save-displaced-array-backpointer array data
)))
492 (setf (%array-displaced-p array
) nil
)))
493 (if (listp dimensions
)
494 (let ((dims dimensions
)) ; avoid "prevents use of assertion"
495 (dotimes (axis array-rank
)
496 (setf (%array-dimension array axis
) (pop dims
))))
497 (setf (%array-dimension array
0) dimension-0
))
500 (defun make-array (dimensions &rest args
501 &key
(element-type t
)
502 initial-element initial-contents
506 displaced-index-offset
)
507 (declare (ignore initial-element
508 initial-contents adjustable
509 fill-pointer displaced-to displaced-index-offset
))
510 (declare (explicit-check))
511 (multiple-value-bind (widetag n-bits
) (%vector-widetag-and-n-bits element-type
)
512 (apply #'%make-array dimensions widetag n-bits args
)))
514 (defun make-static-vector (length &key
515 (element-type '(unsigned-byte 8))
516 (initial-contents nil initial-contents-p
)
517 (initial-element nil initial-element-p
))
519 "Allocate vector of LENGTH elements in static space. Only allocation
520 of specialized arrays is supported."
521 ;; STEP 1: check inputs fully
523 ;; This way of doing explicit checks before the vector is allocated
524 ;; is expensive, but probably worth the trouble as once we've allocated
525 ;; the vector we have no way to get rid of it anymore...
526 (when (eq t
(upgraded-array-element-type element-type
))
527 (error "Static arrays of type ~S not supported."
529 (validate-array-initargs initial-element-p initial-element
530 initial-contents-p initial-contents nil
) ; for effect
531 (when initial-contents-p
532 (unless (= length
(length initial-contents
))
533 (error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
534 vector length is ~W."
535 (length initial-contents
)
537 (unless (every (lambda (x) (typep x element-type
)) initial-contents
)
538 (error ":INITIAL-CONTENTS contains elements not of type ~S."
540 (when initial-element-p
541 (unless (typep initial-element element-type
)
542 (error ":INITIAL-ELEMENT ~S is not of type ~S."
543 initial-element element-type
)))
546 ;; Allocate and possibly initialize the vector.
547 (multiple-value-bind (type n-bits
)
548 (%vector-widetag-and-n-bits element-type
)
550 (allocate-static-vector type length
551 (ceiling (* length n-bits
)
552 sb
!vm
:n-word-bits
))))
553 (cond (initial-element-p
554 (fill vector initial-element
))
556 (replace vector initial-contents
))
560 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
561 ;;; specified array characteristics. Dimensions is only used to pass
562 ;;; to FILL-DATA-VECTOR for error checking on the structure of
563 ;;; initial-contents.
564 (defun data-vector-from-inits (dimensions total-size
565 element-type widetag n-bits
566 initialize initial-data
)
567 ;; FIXME: element-type can be NIL when widetag is non-nil,
568 ;; and FILL will check the type, although the error will be not as nice.
569 ;; (cond (typep initial-element element-type)
570 ;; (error "~S cannot be used to initialize an array of type ~S."
571 ;; initial-element element-type))
572 (let ((data (if widetag
573 (allocate-vector-with-widetag widetag total-size n-bits
)
574 (make-array total-size
:element-type element-type
))))
577 (fill (the vector data
) initial-data
))
579 ;; DIMENSIONS can be supplied as a list or integer now
580 (dx-let ((list-of-dims (list dimensions
))) ; ok if already a list
581 (fill-data-vector data
582 (if (listp dimensions
) dimensions list-of-dims
)
587 (defun vector (&rest objects
)
589 "Construct a SIMPLE-VECTOR from the given objects."
590 (let ((v (make-array (length objects
))))
591 (do-rest-arg ((x i
) objects
0 v
)
592 (setf (aref v i
) x
))))
595 ;;;; accessor/setter functions
597 ;;; Dispatch to an optimized routine the data vector accessors for
598 ;;; each different specialized vector type. Do dispatching by looking
599 ;;; up the widetag in the array rather than with the typecases, which
600 ;;; as of 1.0.5 compiles to a naive sequence of linear TYPEPs. Also
601 ;;; provide separate versions where bounds checking has been moved
602 ;;; from the callee to the caller, since it's much cheaper to do once
603 ;;; the type information is available. Finally, for each of these
604 ;;; routines also provide a slow path, taken for arrays that are not
605 ;;; vectors or not simple.
606 (macrolet ((def (name table-name
)
608 (defglobal ,table-name
(make-array ,(1+ sb
!vm
:widetag-mask
)))
609 (declaim (type (simple-array function
(,(1+ sb
!vm
:widetag-mask
)))
611 (defmacro ,name
(array-var)
614 (when (sb!vm
::%other-pointer-p
,array-var
)
615 (setf tag
(%other-pointer-widetag
,array-var
)))
616 (svref ,',table-name tag
)))))))
617 (def !find-data-vector-setter %%data-vector-setters%%
)
618 (def !find-data-vector-setter
/check-bounds %%data-vector-setters
/check-bounds%%
)
619 ;; Used by DO-VECTOR-DATA -- which in turn appears in DOSEQUENCE expansion,
620 ;; meaning we can have post-build dependences on this.
621 (def %find-data-vector-reffer %%data-vector-reffers%%
)
622 (def !find-data-vector-reffer
/check-bounds %%data-vector-reffers
/check-bounds%%
))
624 ;;; Like DOVECTOR, but more magical -- can't use this on host.
625 (defmacro do-vector-data
((elt vector
&optional result
) &body body
)
626 (multiple-value-bind (forms decls
) (parse-body body nil
)
627 (with-unique-names (index vec start end ref
)
628 `(with-array-data ((,vec
,vector
)
631 :check-fill-pointer t
)
632 (let ((,ref
(%find-data-vector-reffer
,vec
)))
633 (declare (function ,ref
))
634 (do ((,index
,start
(1+ ,index
)))
637 ,@(filter-dolist-declarations decls
)
640 (let ((,elt
(funcall ,ref
,vec
,index
)))
642 (tagbody ,@forms
))))))))
644 (macrolet ((%ref
(accessor-getter extra-params
)
645 `(funcall (,accessor-getter array
) array index
,@extra-params
))
646 (define (accessor-name slow-accessor-name accessor-getter
647 extra-params check-bounds
)
649 (defun ,accessor-name
(array index
,@extra-params
)
650 (declare (explicit-check))
651 (declare (optimize speed
652 ;; (SAFETY 0) is ok. All calls to
653 ;; these functions are generated by
654 ;; the compiler, so argument count
655 ;; checking isn't needed. Type checking
656 ;; is done implicitly via the widetag
659 (%ref
,accessor-getter
,extra-params
))
660 (defun ,slow-accessor-name
(array index
,@extra-params
)
661 (declare (optimize speed
(safety 0)))
662 (if (not (%array-displaced-p array
))
663 ;; The reasonably quick path of non-displaced complex
665 (let ((array (%array-data-vector array
)))
666 (%ref
,accessor-getter
,extra-params
))
667 ;; The real slow path.
671 (declare (optimize (speed 1) (safety 1)))
672 (,@check-bounds index
)))
675 (declare (ignore end
))
676 (,accessor-name vector index
,@extra-params
)))))))
677 (define hairy-data-vector-ref slow-hairy-data-vector-ref
678 %find-data-vector-reffer
680 (define hairy-data-vector-set slow-hairy-data-vector-set
681 !find-data-vector-setter
683 (define hairy-data-vector-ref
/check-bounds
684 slow-hairy-data-vector-ref
/check-bounds
685 !find-data-vector-reffer
/check-bounds
686 nil
(check-bound array
(array-dimension array
0)))
687 (define hairy-data-vector-set
/check-bounds
688 slow-hairy-data-vector-set
/check-bounds
689 !find-data-vector-setter
/check-bounds
690 (new-value) (check-bound array
(array-dimension array
0))))
692 (defun hairy-ref-error (array index
&optional new-value
)
693 (declare (ignore index new-value
))
696 :expected-type
'vector
))
698 (macrolet ((define-reffer (saetp check-form
)
699 (let* ((type (sb!vm
:saetp-specifier saetp
))
700 (atype `(simple-array ,type
(*))))
701 `(named-lambda (optimized-data-vector-ref ,type
) (vector index
)
702 (declare (optimize speed
(safety 0))
703 ;; Obviously these all coerce raw words to lispobjs
704 ;; so don't keep spewing notes about it.
705 (muffle-conditions compiler-note
)
708 `(data-vector-ref (the ,atype vector
)
710 (declare (optimize (safety 1)))
712 (,@check-form index
))))
713 `(data-nil-vector-ref (the ,atype vector
) index
)))))
714 (define-setter (saetp check-form
)
715 (let* ((type (sb!vm
:saetp-specifier saetp
))
716 (atype `(simple-array ,type
(*))))
717 `(named-lambda (optimized-data-vector-set ,type
) (vector index new-value
)
718 (declare (optimize speed
(safety 0)))
719 ;; Impossibly setting an elt of an (ARRAY NIL)
720 ;; returns no value. And nobody cares.
721 (declare (muffle-conditions compiler-note
))
722 (data-vector-set (the ,atype vector
)
724 (declare (optimize (safety 1)))
726 (,@check-form index
)))
728 ;; SPEED 1 needed to avoid the compiler
729 ;; from downgrading the type check to
731 (declare (optimize (speed 1)
733 (the ,type new-value
)))
734 ;; For specialized arrays, the return from
735 ;; data-vector-set would have to be reboxed to be a
736 ;; (Lisp) return value; instead, we use the
737 ;; already-boxed value as the return.
739 (define-reffers (symbol deffer check-form slow-path
)
741 ;; FIXME/KLUDGE: can't just FILL here, because genesis doesn't
742 ;; preserve the binding, so re-initiaize as NS doesn't have
743 ;; the energy to figure out to change that right now.
744 (setf ,symbol
(make-array (1+ sb
!vm
::widetag-mask
)
745 :initial-element
#'hairy-ref-error
))
746 ,@(loop for widetag in
'(sb!vm
:complex-vector-widetag
747 sb
!vm
:complex-vector-nil-widetag
748 sb
!vm
:complex-bit-vector-widetag
749 #!+sb-unicode sb
!vm
:complex-character-string-widetag
750 sb
!vm
:complex-base-string-widetag
751 sb
!vm
:simple-array-widetag
752 sb
!vm
:complex-array-widetag
)
753 collect
`(setf (svref ,symbol
,widetag
) ,slow-path
))
754 ,@(loop for saetp across sb
!vm
:*specialized-array-element-type-properties
*
755 for widetag
= (sb!vm
:saetp-typecode saetp
)
756 collect
`(setf (svref ,symbol
,widetag
)
757 (,deffer
,saetp
,check-form
))))))
758 (defun !hairy-data-vector-reffer-init
()
759 (define-reffers %%data-vector-reffers%% define-reffer
761 #'slow-hairy-data-vector-ref
)
762 (define-reffers %%data-vector-setters%% define-setter
764 #'slow-hairy-data-vector-set
)
765 (define-reffers %%data-vector-reffers
/check-bounds%% define-reffer
766 (check-bound vector
(length vector
))
767 #'slow-hairy-data-vector-ref
/check-bounds
)
768 (define-reffers %%data-vector-setters
/check-bounds%% define-setter
769 (check-bound vector
(length vector
))
770 #'slow-hairy-data-vector-set
/check-bounds
)))
772 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
773 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
774 ;;; definition is needed for the compiler to use in constant folding.)
775 (defun data-vector-ref (array index
)
776 (declare (explicit-check))
777 (hairy-data-vector-ref array index
))
779 (defun data-vector-ref-with-offset (array index offset
)
780 (declare (explicit-check))
781 (hairy-data-vector-ref array
(+ index offset
)))
783 (defun invalid-array-p (array)
784 (and (array-header-p array
)
785 (consp (%array-displaced-p array
))))
787 (declaim (ftype (function (array) nil
) invalid-array-error
))
788 (defun invalid-array-error (array)
789 (aver (array-header-p array
))
790 ;; Array invalidation stashes the original dimensions here...
791 (let ((dims (%array-displaced-p array
))
792 (et (array-element-type array
)))
793 (error 'invalid-array-error
798 `(vector ,et
,@dims
)))))
800 (declaim (ftype (function (array t integer
&optional t
) nil
)
801 invalid-array-index-error
))
802 (defun invalid-array-index-error (array index bound
&optional axis
)
803 (if (invalid-array-p array
)
804 (invalid-array-error array
)
805 (error 'invalid-array-index-error
809 :expected-type
`(integer 0 (,bound
)))))
811 ;;; SUBSCRIPTS has a dynamic-extent list structure and is destroyed
812 (defun %array-row-major-index
(array &rest subscripts
)
813 (declare (truly-dynamic-extent subscripts
)
815 (let ((length (length subscripts
)))
816 (cond ((array-header-p array
)
817 (let ((rank (%array-rank array
)))
818 (unless (= rank length
)
819 (error "wrong number of subscripts, ~W, for array of rank ~W."
821 (do ((axis (1- rank
) (1- axis
))
824 ((minusp axis
) result
)
825 (declare (fixnum axis chunk-size result
))
826 (let ((index (fast-&rest-nth axis subscripts
))
827 (dim (%array-dimension array axis
)))
828 (unless (and (fixnump index
) (< -
1 index dim
))
829 (invalid-array-index-error array index dim axis
))
833 (truly-the fixnum
(* chunk-size index
))))
834 chunk-size
(truly-the fixnum
(* chunk-size dim
)))))))
836 (error "Wrong number of subscripts, ~W, for array of rank 1."
839 (let ((index (fast-&rest-nth
0 subscripts
))
840 (length (length (the (simple-array * (*)) array
))))
841 (unless (and (fixnump index
) (< -
1 index length
))
842 (invalid-array-index-error array index length
))
845 (defun array-in-bounds-p (array &rest subscripts
)
847 "Return T if the SUBSCRIPTS are in bounds for the ARRAY, NIL otherwise."
848 (declare (truly-dynamic-extent subscripts
))
849 (let ((length (length subscripts
)))
850 (cond ((array-header-p array
)
851 (let ((rank (%array-rank array
)))
852 (unless (= rank length
)
853 (error "Wrong number of subscripts, ~W, for array of rank ~W."
855 (loop for i below length
856 for s
= (fast-&rest-nth i subscripts
)
857 always
(and (typep s
'(and fixnum unsigned-byte
))
858 (< s
(%array-dimension array i
))))))
860 (error "Wrong number of subscripts, ~W, for array of rank 1."
863 (let ((subscript (fast-&rest-nth
0 subscripts
)))
864 (and (typep subscript
'(and fixnum unsigned-byte
))
866 (length (truly-the (simple-array * (*)) array
)))))))))
868 (defun array-row-major-index (array &rest subscripts
)
869 (declare (truly-dynamic-extent subscripts
))
870 (apply #'%array-row-major-index array subscripts
))
872 (defun aref (array &rest subscripts
)
874 "Return the element of the ARRAY specified by the SUBSCRIPTS."
875 (declare (truly-dynamic-extent subscripts
))
876 (row-major-aref array
(apply #'%array-row-major-index array subscripts
)))
878 ;;; (setf aref/bit/sbit) are implemented using setf-functions,
879 ;;; because they have to work with (setf (apply #'aref array subscripts))
880 ;;; All other setfs can be done using setf-functions too, but I
881 ;;; haven't found technical advantages or disadvantages for either
883 (defun (setf aref
) (new-value array
&rest subscripts
)
884 (declare (truly-dynamic-extent subscripts
)
886 (setf (row-major-aref array
(apply #'%array-row-major-index array subscripts
))
889 (defun row-major-aref (array index
)
891 "Return the element of array corresponding to the row-major index. This is
893 (declare (optimize (safety 1)))
894 (row-major-aref array index
))
896 (defun %set-row-major-aref
(array index new-value
)
897 (declare (optimize (safety 1)))
898 (setf (row-major-aref array index
) new-value
))
900 (defun svref (simple-vector index
)
902 "Return the INDEXth element of the given Simple-Vector."
903 (declare (optimize (safety 1)))
904 (aref simple-vector index
))
906 (defun %svset
(simple-vector index new
)
907 (declare (optimize (safety 1)))
908 (setf (aref simple-vector index
) new
))
910 (defun bit (bit-array &rest subscripts
)
912 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
913 (declare (type (array bit
) bit-array
)
914 (truly-dynamic-extent subscripts
)
915 (optimize (safety 1)))
916 (row-major-aref bit-array
(apply #'%array-row-major-index bit-array subscripts
)))
918 (defun (setf bit
) (new-value bit-array
&rest subscripts
)
919 (declare (type (array bit
) bit-array
)
921 (truly-dynamic-extent subscripts
)
922 (optimize (safety 1)))
923 (setf (row-major-aref bit-array
924 (apply #'%array-row-major-index bit-array subscripts
))
927 (defun sbit (simple-bit-array &rest subscripts
)
929 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
930 (declare (type (simple-array bit
) simple-bit-array
)
931 (truly-dynamic-extent subscripts
)
932 (optimize (safety 1)))
933 (row-major-aref simple-bit-array
934 (apply #'%array-row-major-index simple-bit-array subscripts
)))
936 (defun (setf sbit
) (new-value bit-array
&rest subscripts
)
937 (declare (type (simple-array bit
) bit-array
)
939 (truly-dynamic-extent subscripts
)
940 (optimize (safety 1)))
941 (setf (row-major-aref bit-array
942 (apply #'%array-row-major-index bit-array subscripts
))
945 ;;;; miscellaneous array properties
947 (defun array-element-type (array)
949 "Return the type of the elements of the array"
950 (let ((widetag (%other-pointer-widetag array
))
951 (table (load-time-value
952 (let ((table (make-array 256 :initial-element nil
)))
953 (dotimes (i (length sb
!vm
:*specialized-array-element-type-properties
*) table
)
954 (let* ((saetp (aref sb
!vm
:*specialized-array-element-type-properties
* i
))
955 (typecode (sb!vm
:saetp-typecode saetp
))
956 (complex-typecode (sb!vm
:saetp-complex-typecode saetp
))
957 (specifier (sb!vm
:saetp-specifier saetp
)))
958 (aver (typep specifier
'(or list symbol
)))
959 (setf (aref table typecode
) specifier
)
960 (when complex-typecode
961 (setf (aref table complex-typecode
) specifier
)))))
963 (let ((result (aref table widetag
)))
965 (truly-the (or list symbol
) result
)
966 ;; (MAKE-ARRAY :ELEMENT-TYPE NIL) goes to this branch, but
967 ;; gets the right answer in the end
968 (with-array-data ((array array
) (start) (end))
969 (declare (ignore start end
))
970 (truly-the (or list symbol
) (aref table
(%other-pointer-widetag array
))))))))
972 (defun array-rank (array)
974 "Return the number of dimensions of ARRAY."
975 (if (array-header-p array
)
979 (defun array-dimension (array axis-number
)
981 "Return the length of dimension AXIS-NUMBER of ARRAY."
982 (declare (array array
) (type index axis-number
))
983 (cond ((not (array-header-p array
))
984 (unless (= axis-number
0)
985 (error "Vector axis is not zero: ~S" axis-number
))
986 (length (the (simple-array * (*)) array
)))
987 ((>= axis-number
(%array-rank array
))
988 (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
989 axis-number array
(%array-rank array
)))
991 (%array-dimension array axis-number
))))
993 (defun array-dimensions (array)
995 "Return a list whose elements are the dimensions of the array"
996 (declare (array array
))
997 (if (array-header-p array
)
998 (do ((results nil
(cons (array-dimension array index
) results
))
999 (index (1- (array-rank array
)) (1- index
)))
1000 ((minusp index
) results
))
1001 (list (array-dimension array
0))))
1003 (defun array-total-size (array)
1005 "Return the total number of elements in the Array."
1006 (declare (array array
))
1007 (if (array-header-p array
)
1008 (%array-available-elements array
)
1009 (length (the vector array
))))
1011 (defun array-displacement (array)
1013 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
1014 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
1015 (declare (type array array
))
1016 (if (and (array-header-p array
) ; if unsimple and
1017 (%array-displaced-p array
)) ; displaced
1018 (values (%array-data-vector array
) (%array-displacement array
))
1021 (defun adjustable-array-p (array)
1023 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
1024 to the argument, this happens for complex arrays."
1025 (declare (array array
))
1026 ;; Note that this appears not to be a fundamental limitation.
1027 ;; non-vector SIMPLE-ARRAYs are in fact capable of being adjusted,
1028 ;; but in practice we test using ADJUSTABLE-ARRAY-P in ADJUST-ARRAY.
1029 ;; -- CSR, 2004-03-01.
1030 (not (typep array
'simple-array
)))
1032 ;;;; fill pointer frobbing stuff
1034 (declaim (inline array-has-fill-pointer-p
))
1035 (defun array-has-fill-pointer-p (array)
1037 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
1038 (declare (array array
))
1039 (and (array-header-p array
) (%array-fill-pointer-p array
)))
1041 (defun fill-pointer-error (vector &optional arg
)
1043 (aver (array-has-fill-pointer-p vector
))
1044 (let ((max (%array-available-elements vector
)))
1045 (error 'simple-type-error
1047 :expected-type
(list 'integer
0 max
)
1048 :format-control
"The new fill pointer, ~S, is larger than the length of the vector (~S.)"
1049 :format-arguments
(list arg max
))))
1051 (error 'simple-type-error
1053 :expected-type
'(and vector
(satisfies array-has-fill-pointer-p
))
1054 :format-control
"~S is not an array with a fill pointer."
1055 :format-arguments
(list vector
)))))
1057 (declaim (inline fill-pointer
))
1058 (defun fill-pointer (vector)
1060 "Return the FILL-POINTER of the given VECTOR."
1061 (declare (explicit-check))
1062 (if (array-has-fill-pointer-p vector
)
1063 (%array-fill-pointer vector
)
1064 (fill-pointer-error vector
)))
1066 (defun %set-fill-pointer
(vector new
)
1067 (declare (explicit-check))
1069 (fill-pointer-error vector x
)))
1070 (cond ((not (array-has-fill-pointer-p vector
))
1072 ((> new
(%array-available-elements vector
))
1075 (setf (%array-fill-pointer vector
) new
)))))
1077 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
1078 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
1079 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
1080 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
1081 ;;; back to CMU CL).
1082 (defun vector-push (new-element array
)
1084 "Attempt to set the element of ARRAY designated by its fill pointer
1085 to NEW-ELEMENT, and increment the fill pointer by one. If the fill pointer is
1086 too large, NIL is returned, otherwise the index of the pushed element is
1088 (declare (explicit-check))
1089 (let ((fill-pointer (fill-pointer array
)))
1090 (cond ((= fill-pointer
(%array-available-elements array
))
1093 (locally (declare (optimize (safety 0)))
1094 (setf (aref array fill-pointer
) new-element
))
1095 (setf (%array-fill-pointer array
) (1+ fill-pointer
))
1098 (defun vector-push-extend (new-element vector
&optional min-extension
)
1099 (declare (type (or null
(and index
(integer 1))) min-extension
))
1100 (declare (explicit-check))
1101 (let ((fill-pointer (fill-pointer vector
)))
1102 (when (= fill-pointer
(%array-available-elements vector
))
1103 (let ((min-extension
1105 (let ((length (length vector
)))
1107 (- array-dimension-limit length
))))))
1108 (adjust-array vector
(+ fill-pointer
(max 1 min-extension
)))))
1109 ;; disable bounds checking
1110 (locally (declare (optimize (safety 0)))
1111 (setf (aref vector fill-pointer
) new-element
))
1112 (setf (%array-fill-pointer vector
) (1+ fill-pointer
))
1115 (defun vector-pop (array)
1117 "Decrease the fill pointer by 1 and return the element pointed to by the
1119 (declare (explicit-check))
1120 (let ((fill-pointer (fill-pointer array
)))
1121 (if (zerop fill-pointer
)
1122 (error "There is nothing left to pop.")
1123 ;; disable bounds checking (and any fixnum test)
1124 (locally (declare (optimize (safety 0)))
1126 (setf (%array-fill-pointer array
)
1127 (1- fill-pointer
)))))))
1132 (defun adjust-array (array dimensions
&key
1133 (element-type (array-element-type array
) element-type-p
)
1134 (initial-element nil initial-element-p
)
1135 (initial-contents nil initial-contents-p
)
1137 displaced-to displaced-index-offset
)
1139 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
1140 (when (invalid-array-p array
)
1141 (invalid-array-error array
))
1142 (binding* ((dimensions (ensure-list dimensions
))
1143 (array-rank (array-rank array
))
1145 (unless (= (length dimensions
) array-rank
)
1146 (error "The number of dimensions not equal to rank of array.")))
1147 ((initialize initial-data
)
1148 (validate-array-initargs initial-element-p initial-element
1149 initial-contents-p initial-contents
1151 (cond ((and element-type-p
1152 (not (subtypep element-type
(array-element-type array
))))
1153 ;; This is weird. Should check upgraded type against actual
1154 ;; array element type I think. See lp#1331299. CLHS says that
1155 ;; "consequences are unspecified" so current behavior isn't wrong.
1156 (error "The new element type, ~S, is incompatible with old type."
1158 ((and fill-pointer
(/= array-rank
1))
1159 (error "Only vectors can have fill pointers."))
1160 ((and fill-pointer
(not (array-has-fill-pointer-p array
)))
1161 ;; This case always struck me as odd. It seems like it might mean
1162 ;; that the user asks that the array gain a fill-pointer if it didn't
1163 ;; have one, yet CLHS is clear that the argument array must have a
1164 ;; fill-pointer or else signal a type-error.
1165 (fill-pointer-error array
)))
1166 (cond (initial-contents-p
1167 ;; array former contents replaced by INITIAL-CONTENTS
1168 (let* ((array-size (apply #'* dimensions
))
1169 (array-data (data-vector-from-inits
1170 dimensions array-size element-type nil nil
1171 initialize initial-data
)))
1172 (cond ((adjustable-array-p array
)
1173 (set-array-header array array-data array-size
1174 (get-new-fill-pointer array array-size
1176 0 dimensions nil nil
))
1177 ((array-header-p array
)
1178 ;; simple multidimensional or single dimensional array
1179 (make-array dimensions
1180 :element-type element-type
1181 :initial-contents initial-contents
))
1185 ;; We already established that no INITIAL-CONTENTS was supplied.
1186 (unless (or (eql element-type
(array-element-type displaced-to
))
1187 (subtypep element-type
(array-element-type displaced-to
)))
1188 ;; See lp#1331299 again. Require exact match on upgraded type?
1189 (error "can't displace an array of type ~S into another of ~
1191 element-type
(array-element-type displaced-to
)))
1192 (let ((displacement (or displaced-index-offset
0))
1193 (array-size (apply #'* dimensions
)))
1194 (declare (fixnum displacement array-size
))
1195 (if (< (the fixnum
(array-total-size displaced-to
))
1196 (the fixnum
(+ displacement array-size
)))
1197 (error "The :DISPLACED-TO array is too small."))
1198 (if (adjustable-array-p array
)
1199 ;; None of the original contents appear in adjusted array.
1200 (set-array-header array displaced-to array-size
1201 (get-new-fill-pointer array array-size
1203 displacement dimensions t nil
)
1204 ;; simple multidimensional or single dimensional array
1205 (make-array dimensions
1206 :element-type element-type
1207 :displaced-to displaced-to
1208 :displaced-index-offset
1209 displaced-index-offset
))))
1211 (let ((old-length (array-total-size array
))
1212 (new-length (car dimensions
))
1214 (declare (fixnum old-length new-length
))
1215 (with-array-data ((old-data array
) (old-start)
1216 (old-end old-length
))
1217 (cond ((or (and (array-header-p array
)
1218 (%array-displaced-p array
))
1219 (< old-length new-length
))
1221 (data-vector-from-inits
1222 dimensions new-length element-type
1223 (%other-pointer-widetag old-data
) nil
1224 initialize initial-data
))
1225 ;; Provide :END1 to avoid full call to LENGTH
1227 (replace new-data old-data
1229 :start2 old-start
:end2 old-end
))
1231 (shrink-vector old-data new-length
))))
1232 (if (adjustable-array-p array
)
1233 (set-array-header array new-data new-length
1234 (get-new-fill-pointer array new-length
1236 0 dimensions nil nil
)
1239 (let ((old-length (%array-available-elements array
))
1240 (new-length (apply #'* dimensions
)))
1241 (declare (fixnum old-length new-length
))
1242 (with-array-data ((old-data array
) (old-start)
1243 (old-end old-length
))
1244 (declare (ignore old-end
))
1245 (let ((new-data (if (or (and (array-header-p array
)
1246 (%array-displaced-p array
))
1247 (> new-length old-length
)
1248 (not (adjustable-array-p array
)))
1249 (data-vector-from-inits
1250 dimensions new-length
1252 (%other-pointer-widetag old-data
) nil
1253 (if initial-element-p
:initial-element
)
1256 (if (or (zerop old-length
) (zerop new-length
))
1257 (when initial-element-p
(fill new-data initial-element
))
1258 (zap-array-data old-data
(array-dimensions array
)
1260 new-data dimensions new-length
1261 element-type initial-element
1263 (if (adjustable-array-p array
)
1264 (set-array-header array new-data new-length
1265 nil
0 dimensions nil nil
)
1268 sb
!vm
:simple-array-widetag array-rank
)))
1269 (set-array-header new-array new-data new-length
1270 nil
0 dimensions nil t
))))))))))
1273 (defun get-new-fill-pointer (old-array new-array-size fill-pointer
)
1274 (declare (fixnum new-array-size
))
1275 (typecase fill-pointer
1277 ;; "The consequences are unspecified if array is adjusted to a
1278 ;; size smaller than its fill pointer ..."
1279 (when (array-has-fill-pointer-p old-array
)
1280 (when (> (%array-fill-pointer old-array
) new-array-size
)
1281 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
1282 smaller than its fill pointer (~S)"
1283 old-array new-array-size
(fill-pointer old-array
)))
1284 (%array-fill-pointer old-array
)))
1288 (when (> fill-pointer new-array-size
)
1289 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
1290 than the new length of the vector (~S)"
1291 fill-pointer new-array-size
))
1294 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
1295 ;;; which must be less than or equal to its current length. This can
1296 ;;; be called on vectors without a fill pointer but it is extremely
1297 ;;; dangerous to do so: shrinking the size of an object (as viewed by
1298 ;;; the gc) makes bounds checking unreliable in the face of interrupts
1299 ;;; or multi-threading. Call it only on provably local vectors.
1300 (defun %shrink-vector
(vector new-length
)
1301 (declare (vector vector
))
1302 (unless (array-header-p vector
)
1303 (macrolet ((frob (name &rest things
)
1305 ((simple-array nil
(*)) (error 'nil-array-accessed-error
))
1306 ,@(mapcar (lambda (thing)
1307 (destructuring-bind (type-spec fill-value
)
1310 (fill (truly-the ,type-spec
,name
)
1312 :start new-length
))))
1314 ;; Set the 'tail' of the vector to the appropriate type of zero,
1315 ;; "because in some cases we'll scavenge larger areas in one go,
1316 ;; like groups of pages that had triggered the write barrier, or
1317 ;; the whole static space" according to jsnell.
1321 `((simple-array ,(sb!vm
:saetp-specifier saetp
) (*))
1322 ,(if (or (eq (sb!vm
:saetp-specifier saetp
) 'character
)
1324 (eq (sb!vm
:saetp-specifier saetp
) 'base-char
))
1325 *default-init-char-form
*
1326 (sb!vm
:saetp-initial-element-default saetp
))))
1328 #'sb
!vm
:saetp-specifier
1329 sb
!vm
:*specialized-array-element-type-properties
*)))))
1330 ;; Only arrays have fill-pointers, but vectors have their length
1331 ;; parameter in the same place.
1332 (setf (%array-fill-pointer vector
) new-length
)
1335 (defun shrink-vector (vector new-length
)
1336 (declare (vector vector
))
1338 ((eq (length vector
) new-length
)
1340 ((array-has-fill-pointer-p vector
)
1341 (setf (%array-fill-pointer vector
) new-length
)
1343 (t (subseq vector
0 new-length
))))
1345 ;;; BIG THREAD SAFETY NOTE
1347 ;;; ADJUST-ARRAY/SET-ARRAY-HEADER, and its callees are very
1348 ;;; thread unsafe. They are nonatomic, and can mess with parallel
1349 ;;; code using the same arrays.
1351 ;;; A likely seeming fix is an additional level of indirection:
1352 ;;; ARRAY-HEADER -> ARRAY-INFO -> ... where ARRAY-HEADER would
1353 ;;; hold nothing but the pointer to ARRAY-INFO, and ARRAY-INFO
1354 ;;; would hold everything ARRAY-HEADER now holds. This allows
1355 ;;; consing up a new ARRAY-INFO and replacing it atomically in
1356 ;;; the ARRAY-HEADER.
1358 ;;; %WALK-DISPLACED-ARRAY-BACKPOINTERS is an especially nasty
1359 ;;; one: not only is it needed extremely rarely, which makes
1360 ;;; any thread safety bugs involving it look like rare random
1361 ;;; corruption, but because it walks the chain *upwards*, which
1362 ;;; may violate user expectations.
1364 ;;; Fill in array header with the provided information, and return the array.
1365 (defun set-array-header (array data length fill-pointer displacement dimensions
1367 (labels ((%walk-displaced-array-backpointers
(array new-length
)
1368 (dolist (p (%array-displaced-from array
))
1369 (let ((from (weak-pointer-value p
)))
1370 (when (and from
(eq array
(%array-data-vector from
)))
1371 (let ((requires (+ (%array-available-elements from
)
1372 (%array-displacement from
))))
1373 (unless (>= new-length requires
)
1374 ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
1376 ;; "If A is displaced to B, the consequences are unspecified if B is
1377 ;; adjusted in such a way that it no longer has enough elements to
1380 ;; since we're hanging on a weak pointer here, we can't signal an
1381 ;; error right now: the array that we're looking at might be
1382 ;; garbage. Instead, we set all dimensions to zero so that next
1383 ;; safe access to the displaced array will trap. Additionally, we
1384 ;; save the original dimensions, so we can signal a more
1385 ;; understandable error when the time comes.
1386 (%walk-displaced-array-backpointers from
0)
1387 (setf (%array-fill-pointer from
) 0
1388 (%array-available-elements from
) 0
1389 (%array-displaced-p from
) (array-dimensions array
))
1390 (dotimes (i (%array-rank from
))
1391 (setf (%array-dimension from i
) 0)))))))))
1393 (setf (%array-displaced-from array
) nil
)
1394 (%walk-displaced-array-backpointers array length
))
1396 (%save-displaced-array-backpointer array data
))
1397 (setf (%array-data-vector array
) data
)
1398 (setf (%array-available-elements array
) length
)
1400 (setf (%array-fill-pointer array
) fill-pointer
)
1401 (setf (%array-fill-pointer-p array
) t
))
1403 (setf (%array-fill-pointer array
) length
)
1404 (setf (%array-fill-pointer-p array
) nil
)))
1405 (setf (%array-displacement array
) displacement
)
1406 (if (listp dimensions
)
1407 (dotimes (axis (array-rank array
))
1408 (declare (type index axis
))
1409 (setf (%array-dimension array axis
) (pop dimensions
)))
1410 (setf (%array-dimension array
0) dimensions
))
1411 (setf (%array-displaced-p array
) displacedp
)
1414 ;;; User visible extension
1415 (declaim (ftype (sfunction (array) (simple-array * (*))) array-storage-vector
))
1416 (defun array-storage-vector (array)
1418 "Returns the underlying storage vector of ARRAY, which must be a non-displaced array.
1420 In SBCL, if ARRAY is a of type \(SIMPLE-ARRAY * \(*)), it is its own storage
1421 vector. Multidimensional arrays, arrays with fill pointers, and adjustable
1422 arrays have an underlying storage vector with the same ARRAY-ELEMENT-TYPE as
1423 ARRAY, which this function returns.
1425 Important note: the underlying vector is an implementation detail. Even though
1426 this function exposes it, changes in the implementation may cause this
1427 function to be removed without further warning."
1428 ;; KLUDGE: Without TRULY-THE the system is not smart enough to figure out that
1429 ;; the return value is always of the known type.
1430 (truly-the (simple-array * (*))
1431 (cond ((not (array-header-p array
))
1433 ((%array-displaced-p array
)
1434 (error "~S cannot be used with displaced arrays. Use ~S instead."
1435 'array-storage-vector
'array-displacement
))
1437 (%array-data-vector array
)))))
1440 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
1442 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
1443 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
1444 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
1445 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
1446 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
1447 element-type initial-element initial-element-p
)
1448 (declare (list old-dims new-dims
)
1449 (fixnum new-length
))
1450 ;; OLD-DIMS comes from array-dimensions, which returns a fresh list
1451 ;; at least in SBCL.
1452 ;; NEW-DIMS comes from the user.
1453 (setf old-dims
(nreverse old-dims
)
1454 new-dims
(reverse new-dims
))
1455 (cond ((eq old-data new-data
)
1456 ;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and
1457 ;; INITIAL-ELEMENT-P are used when OLD-DATA and NEW-DATA are
1458 ;; EQ; in this case, a temporary must be used and filled
1459 ;; appropriately. specified initial-element.
1460 ;; FIXME: transforming this TYPEP to someting a bit faster
1461 ;; would be a win...
1462 (unless (or (not initial-element-p
)
1463 (typep initial-element element-type
))
1464 (error "~S can't be used to initialize an array of type ~S."
1465 initial-element element-type
))
1466 (let ((temp (if initial-element-p
1467 (make-array new-length
:initial-element initial-element
)
1468 (make-array new-length
))))
1469 (declare (simple-vector temp
))
1470 (zap-array-data-aux old-data old-dims offset temp new-dims
)
1471 (dotimes (i new-length
)
1472 (setf (aref new-data i
) (aref temp i
)))
1473 ;; Kill the temporary vector to prevent garbage retention.
1474 (%shrink-vector temp
0)))
1476 ;; When OLD-DATA and NEW-DATA are not EQ, NEW-DATA has
1477 ;; already been filled with any
1478 (zap-array-data-aux old-data old-dims offset new-data new-dims
))))
1480 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims
)
1481 (declare (fixnum offset
))
1482 (let ((limits (mapcar (lambda (x y
)
1483 (declare (fixnum x y
))
1484 (1- (the fixnum
(min x y
))))
1485 old-dims new-dims
)))
1486 (macrolet ((bump-index-list (index limits
)
1487 `(do ((subscripts ,index
(cdr subscripts
))
1488 (limits ,limits
(cdr limits
)))
1489 ((null subscripts
) :eof
)
1490 (cond ((< (the fixnum
(car subscripts
))
1491 (the fixnum
(car limits
)))
1493 (1+ (the fixnum
(car subscripts
))))
1495 (t (rplaca subscripts
0))))))
1496 (do ((index (make-list (length old-dims
) :initial-element
0)
1497 (bump-index-list index limits
)))
1499 (setf (aref new-data
(row-major-index-from-dims index new-dims
))
1501 (+ (the fixnum
(row-major-index-from-dims index old-dims
))
1504 ;;; Figure out the row-major-order index of an array reference from a
1505 ;;; list of subscripts and a list of dimensions. This is for internal
1506 ;;; calls only, and the subscripts and dim-list variables are assumed
1507 ;;; to be reversed from what the user supplied.
1508 (defun row-major-index-from-dims (rev-subscripts rev-dim-list
)
1509 (do ((rev-subscripts rev-subscripts
(cdr rev-subscripts
))
1510 (rev-dim-list rev-dim-list
(cdr rev-dim-list
))
1513 ((null rev-dim-list
) result
)
1514 (declare (fixnum chunk-size result
))
1515 (setq result
(+ result
1516 (the fixnum
(* (the fixnum
(car rev-subscripts
))
1518 (setq chunk-size
(* chunk-size
(the fixnum
(car rev-dim-list
))))))
1522 (defun bit-array-same-dimensions-p (array1 array2
)
1523 (declare (type (array bit
) array1 array2
))
1524 (and (= (array-rank array1
)
1525 (array-rank array2
))
1526 (dotimes (index (array-rank array1
) t
)
1527 (when (/= (array-dimension array1 index
)
1528 (array-dimension array2 index
))
1531 (defun pick-result-array (result-bit-array bit-array-1
)
1532 (case result-bit-array
1534 ((nil) (make-array (array-dimensions bit-array-1
)
1536 :initial-element
0))
1538 (unless (bit-array-same-dimensions-p bit-array-1
1540 (error "~S and ~S don't have the same dimensions."
1541 bit-array-1 result-bit-array
))
1544 (defmacro def-bit-array-op
(name function
)
1545 `(defun ,name
(bit-array-1 bit-array-2
&optional result-bit-array
)
1548 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1549 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1550 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1551 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1552 All the arrays must have the same rank and dimensions."
1553 (symbol-name function
))
1554 (declare (type (array bit
) bit-array-1 bit-array-2
)
1555 (type (or (array bit
) (member t nil
)) result-bit-array
))
1556 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2
)
1557 (error "~S and ~S don't have the same dimensions."
1558 bit-array-1 bit-array-2
))
1559 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1
)))
1560 (if (and (simple-bit-vector-p bit-array-1
)
1561 (simple-bit-vector-p bit-array-2
)
1562 (simple-bit-vector-p result-bit-array
))
1563 (locally (declare (optimize (speed 3) (safety 0)))
1564 (,name bit-array-1 bit-array-2 result-bit-array
))
1565 (with-array-data ((data1 bit-array-1
) (start1) (end1))
1566 (declare (ignore end1
))
1567 (with-array-data ((data2 bit-array-2
) (start2) (end2))
1568 (declare (ignore end2
))
1569 (with-array-data ((data3 result-bit-array
) (start3) (end3))
1570 (do ((index-1 start1
(1+ index-1
))
1571 (index-2 start2
(1+ index-2
))
1572 (index-3 start3
(1+ index-3
)))
1573 ((>= index-3 end3
) result-bit-array
)
1574 (declare (type index index-1 index-2 index-3
))
1575 (setf (sbit data3 index-3
)
1576 (logand (,function
(sbit data1 index-1
)
1577 (sbit data2 index-2
))
1580 (def-bit-array-op bit-and logand
)
1581 (def-bit-array-op bit-ior logior
)
1582 (def-bit-array-op bit-xor logxor
)
1583 (def-bit-array-op bit-eqv logeqv
)
1584 (def-bit-array-op bit-nand lognand
)
1585 (def-bit-array-op bit-nor lognor
)
1586 (def-bit-array-op bit-andc1 logandc1
)
1587 (def-bit-array-op bit-andc2 logandc2
)
1588 (def-bit-array-op bit-orc1 logorc1
)
1589 (def-bit-array-op bit-orc2 logorc2
)
1591 (defun bit-not (bit-array &optional result-bit-array
)
1593 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1594 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1595 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1596 created. Both arrays must have the same rank and dimensions."
1597 (declare (type (array bit
) bit-array
)
1598 (type (or (array bit
) (member t nil
)) result-bit-array
))
1599 (let ((result-bit-array (pick-result-array result-bit-array bit-array
)))
1600 (if (and (simple-bit-vector-p bit-array
)
1601 (simple-bit-vector-p result-bit-array
))
1602 (locally (declare (optimize (speed 3) (safety 0)))
1603 (bit-not bit-array result-bit-array
))
1604 (with-array-data ((src bit-array
) (src-start) (src-end))
1605 (declare (ignore src-end
))
1606 (with-array-data ((dst result-bit-array
) (dst-start) (dst-end))
1607 (do ((src-index src-start
(1+ src-index
))
1608 (dst-index dst-start
(1+ dst-index
)))
1609 ((>= dst-index dst-end
) result-bit-array
)
1610 (declare (type index src-index dst-index
))
1611 (setf (sbit dst dst-index
)
1612 (logxor (sbit src src-index
) 1))))))))
1614 ;;;; array type dispatching
1616 ;;; Given DISPATCH-FOO as the DISPATCH-NAME argument (unevaluated),
1617 ;;; defines the functions
1619 ;;; DISPATCH-FOO/SIMPLE-BASE-STRING
1620 ;;; DISPATCH-FOO/SIMPLE-CHARACTER-STRING
1621 ;;; DISPATCH-FOO/SIMPLE-ARRAY-SINGLE-FLOAT
1624 ;;; PARAMS are the function parameters in the definition of each
1625 ;;; specializer function. The array being specialized must be the
1626 ;;; first parameter in PARAMS. A type declaration for this parameter
1627 ;;; is automatically inserted into the body of each function.
1629 ;;; The dispatch table %%FOO-FUNS%% is defined and populated by these
1630 ;;; functions. The table is padded by the function
1631 ;;; HAIRY-FOO-DISPATCH-ERROR, also defined by DEFINE-ARRAY-DISPATCH.
1633 ;;; Finally, the DISPATCH-FOO macro is defined which does the actual
1634 ;;; dispatching when called. It expects arguments that match PARAMS.
1636 (defmacro !define-array-dispatch
(dispatch-name params
&body body
)
1637 (let ((table-name (symbolicate "%%" dispatch-name
"-FUNS%%"))
1638 (error-name (symbolicate "HAIRY-" dispatch-name
"-ERROR")))
1640 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
1641 (defun ,error-name
(&rest args
)
1644 :expected-type
'(simple-array * (*)))))
1645 (!defglobal
,table-name
(make-array ,(1+ sb
!vm
:widetag-mask
)
1646 :initial-element
#',error-name
))
1647 ,@(loop for info across sb
!vm
:*specialized-array-element-type-properties
*
1648 for typecode
= (sb!vm
:saetp-typecode info
)
1649 for specifier
= (sb!vm
:saetp-specifier info
)
1650 for primitive-type-name
= (sb!vm
:saetp-primitive-type-name info
)
1651 collect
(let ((fun-name (symbolicate (string dispatch-name
)
1652 "/" primitive-type-name
)))
1654 (defun ,fun-name
,params
1655 (declare (type (simple-array ,specifier
(*))
1658 (setf (svref ,table-name
,typecode
) #',fun-name
))))
1659 (defmacro ,dispatch-name
(&rest args
)
1660 (check-type (first args
) symbol
)
1661 (let ((tag (gensym "TAG")))
1665 (when (sb!vm
::%other-pointer-p
,(first args
))
1666 (setf ,tag
(%other-pointer-widetag
,(first args
))))
1667 (svref ,',table-name
,tag
)))