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
))
55 (defun %with-array-data
/fp
(array start end
)
56 (%with-array-data-macro array start end
:check-bounds t
:check-fill-pointer t
))
58 (defun %with-array-data
(array start end
)
59 (%with-array-data-macro array start end
:check-bounds t
:check-fill-pointer nil
))
61 (defun %data-vector-and-index
(array index
)
62 (if (array-header-p array
)
63 (multiple-value-bind (vector index
)
64 (%with-array-data array index nil
)
65 (values vector index
))
66 (values array index
)))
69 (defun %integer-vector-widetag-and-n-bits
(signed high
)
71 #.
(let ((map (make-array (1+ sb
!vm
:n-word-bits
))))
72 (loop for saetp across
73 (reverse sb
!vm
:*specialized-array-element-type-properties
*)
74 for ctype
= (sb!vm
:saetp-ctype saetp
)
75 when
(and (numeric-type-p ctype
)
76 (eq (numeric-type-class ctype
) 'integer
)
77 (zerop (numeric-type-low ctype
)))
78 do
(fill map
(cons (sb!vm
:saetp-typecode saetp
)
79 (sb!vm
:saetp-n-bits saetp
))
80 :end
(1+ (integer-length (numeric-type-high ctype
)))))
83 #.
(let ((map (make-array (1+ sb
!vm
:n-word-bits
))))
84 (loop for saetp across
85 (reverse sb
!vm
:*specialized-array-element-type-properties
*)
86 for ctype
= (sb!vm
:saetp-ctype saetp
)
87 when
(and (numeric-type-p ctype
)
88 (eq (numeric-type-class ctype
) 'integer
)
89 (minusp (numeric-type-low ctype
)))
90 do
(fill map
(cons (sb!vm
:saetp-typecode saetp
)
91 (sb!vm
:saetp-n-bits saetp
))
92 :end
(+ (integer-length (numeric-type-high ctype
)) 2)))
94 (cond ((> high sb
!vm
:n-word-bits
)
95 (values #.sb
!vm
:simple-vector-widetag
#.sb
!vm
:n-word-bits
))
97 (let ((x (aref signed-table high
)))
98 (values (car x
) (cdr x
))))
100 (let ((x (aref unsigned-table high
)))
101 (values (car x
) (cdr x
)))))))
103 ;;; This is a bit complicated, but calling subtypep over all
104 ;;; specialized types is exceedingly slow
105 (defun %vector-widetag-and-n-bits
(type)
106 (macrolet ((with-parameters ((arg-type &key intervals
)
107 (&rest args
) &body body
)
108 (let ((type-sym (gensym)))
109 `(let (,@(loop for arg in args
111 (declare (ignorable ,@args
))
113 (let ((,type-sym
(cdr type
)))
115 ,@(loop for arg in args
117 `(cond ((consp ,type-sym
)
118 (let ((value (pop ,type-sym
)))
119 (if (or (eq value
'*)
120 (typep value
',arg-type
)
136 (let ((value (symbol-value widetag
)))
140 sb
!vm
:*specialized-array-element-type-properties
*
141 :key
#'sb
!vm
:saetp-typecode
))))))
143 (error "Invalid type specifier: ~s" type
))
144 (integer-interval-widetag (low high
)
146 (%integer-vector-widetag-and-n-bits
148 (1+ (max (integer-length low
) (integer-length high
))))
149 (%integer-vector-widetag-and-n-bits
151 (max (integer-length low
) (integer-length high
))))))
152 (let* ((consp (consp type
))
160 (result sb
!vm
:simple-vector-widetag
))
161 ((base-char standard-char
#!-sb-unicode character
)
164 (result sb
!vm
:simple-base-string-widetag
))
166 ((character extended-char
)
169 (result sb
!vm
:simple-character-string-widetag
))
173 (result sb
!vm
:simple-bit-vector-widetag
))
177 (result sb
!vm
:simple-array-fixnum-widetag
))
179 (with-parameters ((integer 1)) (high)
181 (result sb
!vm
:simple-vector-widetag
)
182 (%integer-vector-widetag-and-n-bits nil high
))))
184 (with-parameters ((integer 1)) (high)
186 (result sb
!vm
:simple-vector-widetag
)
187 (%integer-vector-widetag-and-n-bits t high
))))
189 (with-parameters (double-float :intervals t
) (low high
)
190 (if (and (not (eq low
'*))
192 (if (or (consp low
) (consp high
))
193 (>= (type-bound-number low
) (type-bound-number high
))
195 (result sb
!vm
:simple-array-nil-widetag
)
196 (result sb
!vm
:simple-array-double-float-widetag
))))
198 (with-parameters (single-float :intervals t
) (low high
)
199 (if (and (not (eq low
'*))
201 (if (or (consp low
) (consp high
))
202 (>= (type-bound-number low
) (type-bound-number high
))
204 (result sb
!vm
:simple-array-nil-widetag
)
205 (result sb
!vm
:simple-array-single-float-widetag
))))
207 (if (and (consp type
)
210 (typep (cadr type
) '(integer 1)))
211 (%integer-vector-widetag-and-n-bits
212 nil
(integer-length (1- (cadr type
))))
216 (with-parameters (long-float :intervals t
) (low high
)
217 (if (and (not (eq low
'*))
219 (if (or (consp low
) (consp high
))
220 (>= (type-bound-number low
) (type-bound-number high
))
222 (result sb
!vm
:simple-array-nil-widetag
)
223 (result sb
!vm
:simple-array-long-float-widetag
))))
225 (with-parameters (integer :intervals t
) (low high
)
226 (let ((low (if (consp low
)
229 (high (if (consp high
)
232 (cond ((or (eq high
'*)
234 (result sb
!vm
:simple-vector-widetag
))
236 (result sb
!vm
:simple-array-nil-widetag
))
238 (integer-interval-widetag low high
))))))
240 (with-parameters (t) (subtype)
242 (result sb
!vm
:simple-vector-widetag
)
243 (let ((ctype (specifier-type type
)))
244 (cond ((eq ctype
*empty-type
*)
245 (result sb
!vm
:simple-array-nil-widetag
))
246 ((union-type-p ctype
)
247 (cond ((csubtypep ctype
(specifier-type '(complex double-float
)))
249 sb
!vm
:simple-array-complex-double-float-widetag
))
250 ((csubtypep ctype
(specifier-type '(complex single-float
)))
252 sb
!vm
:simple-array-complex-single-float-widetag
))
254 ((csubtypep ctype
(specifier-type '(complex long-float
)))
256 sb
!vm
:simple-array-complex-long-float-widetag
))
258 (result sb
!vm
:simple-vector-widetag
))))
260 (case (numeric-type-format ctype
)
263 sb
!vm
:simple-array-complex-double-float-widetag
))
266 sb
!vm
:simple-array-complex-single-float-widetag
))
270 sb
!vm
:simple-array-complex-long-float-widetag
))
272 (result sb
!vm
:simple-vector-widetag
)))))))))
274 (result sb
!vm
:simple-array-nil-widetag
))
277 (let ((ctype (type-or-nil-if-unknown type
)))
279 (return (result sb
!vm
:simple-vector-widetag
)))
282 (let ((types (union-type-types ctype
)))
283 (cond ((not (every #'numeric-type-p types
))
284 (result sb
!vm
:simple-vector-widetag
))
285 ((csubtypep ctype
(specifier-type 'integer
))
286 (integer-interval-widetag
287 (reduce #'min types
:key
#'numeric-type-low
)
288 (reduce #'max types
:key
#'numeric-type-high
)))
289 ((csubtypep ctype
(specifier-type 'double-float
))
290 (result sb
!vm
:simple-array-double-float-widetag
))
291 ((csubtypep ctype
(specifier-type 'single-float
))
292 (result sb
!vm
:simple-array-single-float-widetag
))
294 ((csubtypep ctype
(specifier-type 'long-float
))
295 (result sb
!vm
:simple-array-long-float-widetag
))
297 (result sb
!vm
:simple-vector-widetag
)))))
299 #!-sb-unicode
(result sb
!vm
:simple-base-string-widetag
)
301 (if (loop for
(start . end
)
302 in
(character-set-type-pairs ctype
)
303 always
(and (< start base-char-code-limit
)
304 (< end base-char-code-limit
)))
305 (result sb
!vm
:simple-base-string-widetag
)
306 (result sb
!vm
:simple-character-string-widetag
)))
308 (let ((expansion (type-specifier ctype
)))
309 (if (equal expansion type
)
310 (result sb
!vm
:simple-vector-widetag
)
311 (%vector-widetag-and-n-bits expansion
)))))))))))))
313 (defun %complex-vector-widetag
(widetag)
314 (macrolet ((make-case ()
316 ,@(loop for saetp across sb
!vm
:*specialized-array-element-type-properties
*
317 for complex
= (sb!vm
:saetp-complex-typecode saetp
)
319 collect
(list (sb!vm
:saetp-typecode saetp
) complex
))
321 #.sb
!vm
:complex-vector-widetag
))))
324 (defglobal %%simple-array-n-bits%%
(make-array (1+ sb
!vm
:widetag-mask
)))
325 #.
(loop for info across sb
!vm
:*specialized-array-element-type-properties
*
326 collect
`(setf (aref %%simple-array-n-bits%%
,(sb!vm
:saetp-typecode info
))
327 ,(sb!vm
:saetp-n-bits info
)) into forms
328 finally
(return `(progn ,@forms
)))
330 (declaim (type (simple-vector #.
(1+ sb
!vm
:widetag-mask
)) %%simple-array-n-bits%%
))
332 (defun allocate-vector-with-widetag (widetag length
&optional n-bits
)
333 (declare (type (unsigned-byte 8) widetag
)
335 (let ((n-bits (or n-bits
(aref %%simple-array-n-bits%% widetag
))))
336 (declare (type (integer 0 256) n-bits
))
337 (allocate-vector widetag length
339 (* (if (or (= widetag sb
!vm
:simple-base-string-widetag
)
342 sb
!vm
:simple-character-string-widetag
))
346 sb
!vm
:n-word-bits
))))
348 (defun array-underlying-widetag (array)
349 (macrolet ((make-case ()
351 ,@(loop for saetp across sb
!vm
:*specialized-array-element-type-properties
*
352 for complex
= (sb!vm
:saetp-complex-typecode saetp
)
354 collect
(list complex
(sb!vm
:saetp-typecode saetp
)))
355 ((,sb
!vm
:simple-array-widetag
356 ,sb
!vm
:complex-vector-widetag
357 ,sb
!vm
:complex-array-widetag
)
358 (with-array-data ((array array
) (start) (end))
359 (declare (ignore start end
))
360 (%other-pointer-widetag array
)))
363 (let ((widetag (%other-pointer-widetag array
)))
366 (defun make-vector-like (vector length
)
367 (allocate-vector-with-widetag (array-underlying-widetag vector
) length
))
369 ;; Complain in various ways about wrong :INITIAL-foo arguments,
370 ;; returning the two initialization arguments needed for DATA-VECTOR-FROM-INITS.
371 (defun validate-array-initargs (element-p element contents-p contents displaced
)
372 (cond ((and displaced
(or element-p contents-p
))
373 (if (and element-p contents-p
)
374 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
375 may be specified with the :DISPLACED-TO option")
376 (error "~S may not be specified with the :DISPLACED-TO option"
377 (if element-p
:initial-element
:initial-contents
))))
378 ((and element-p contents-p
)
379 (error "Can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
380 (element-p (values :initial-element element
))
381 (contents-p (values :initial-contents contents
))
382 (t (values nil nil
))))
384 (declaim (inline %save-displaced-array-backpointer
))
385 (defun %save-displaced-array-backpointer
(array data
)
386 (flet ((purge (pointers)
387 (remove-if (lambda (value)
388 (or (not value
) (eq array value
)))
390 :key
#'weak-pointer-value
)))
391 ;; Add backpointer to the new data vector if it has a header.
392 (when (array-header-p data
)
393 (setf (%array-displaced-from data
)
394 (cons (make-weak-pointer array
)
395 (purge (%array-displaced-from data
)))))
396 ;; Remove old backpointer, if any.
397 (let ((old-data (%array-data-vector array
)))
398 (when (and (neq data old-data
) (array-header-p old-data
))
399 (setf (%array-displaced-from old-data
)
400 (purge (%array-displaced-from old-data
)))))))
402 ;;; Widetag is the widetag of the underlying vector,
403 ;;; it'll be the same as the resulting array widetag only for simple vectors
404 (defun %make-array
(dimensions widetag n-bits
407 (initial-element nil initial-element-p
)
408 (initial-contents nil initial-contents-p
)
409 adjustable fill-pointer
410 displaced-to displaced-index-offset
)
411 (declare (ignore element-type
))
412 (binding* (((array-rank dimension-0
)
413 (if (listp dimensions
)
414 (values (length dimensions
)
415 (if dimensions
(car dimensions
) 1))
416 (values 1 dimensions
)))
417 ((initialize initial-data
)
418 (validate-array-initargs initial-element-p initial-element
419 initial-contents-p initial-contents
421 (simple (and (null fill-pointer
)
423 (null displaced-to
))))
424 (declare (type array-rank array-rank
))
425 (declare (type index dimension-0
))
426 (cond ((and displaced-index-offset
(null displaced-to
))
427 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
428 ((and simple
(= array-rank
1))
429 (let ((vector ; a (SIMPLE-ARRAY * (*))
430 (allocate-vector-with-widetag widetag dimension-0 n-bits
)))
431 ;; presence of at most one :INITIAL-thing keyword was ensured above
432 (cond (initial-element-p
433 (fill vector initial-element
))
435 (let ((content-length (length initial-contents
)))
436 (unless (= dimension-0 content-length
)
437 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
438 the vector length is ~W."
439 content-length dimension-0
)))
440 (replace vector initial-contents
)))
442 ((and (arrayp displaced-to
)
443 (/= (array-underlying-widetag displaced-to
) widetag
))
444 (error "Array element type of :DISPLACED-TO array does not match specified element type"))
446 ;; it's non-simple or multidimensional, or both.
448 (unless (= array-rank
1)
449 (error "Only vectors can have fill pointers."))
450 (when (and (integerp fill-pointer
) (> fill-pointer dimension-0
))
451 ;; FIXME: should be TYPE-ERROR?
452 (error "invalid fill-pointer ~W" fill-pointer
)))
454 (if (consp dimensions
)
455 (the index
(reduce (lambda (a b
) (* a
(the index b
)))
457 ;; () is considered to have dimension-0 = 1.
458 ;; It avoids the REDUCE lambda being called with no args.
460 (data (or displaced-to
461 (data-vector-from-inits
462 dimensions total-size nil widetag n-bits
463 initialize initial-data
)))
464 (array (make-array-header
465 (cond ((= array-rank
1)
466 (%complex-vector-widetag widetag
))
467 (simple sb
!vm
:simple-array-widetag
)
468 (t sb
!vm
:complex-array-widetag
))
471 (setf (%array-fill-pointer-p array
) t
472 (%array-fill-pointer array
)
473 (if (eq fill-pointer t
) dimension-0 fill-pointer
))
474 (setf (%array-fill-pointer-p array
) nil
475 (%array-fill-pointer array
) total-size
))
476 (setf (%array-available-elements array
) total-size
)
477 ;; Terrible name for this slot - we displace to the
478 ;; target array's header, if any, not the "ultimate"
479 ;; vector in the chain of displacements.
480 (setf (%array-data-vector array
) data
)
481 (setf (%array-displaced-from array
) nil
)
483 (let ((offset (or displaced-index-offset
0)))
484 (when (> (+ offset total-size
)
485 (array-total-size displaced-to
))
486 (error "~S doesn't have enough elements." displaced-to
))
487 (setf (%array-displacement array
) offset
)
488 (setf (%array-displaced-p array
) t
)
489 (%save-displaced-array-backpointer array data
)))
491 (setf (%array-displaced-p array
) nil
)))
492 (if (listp dimensions
)
493 (let ((dims dimensions
)) ; avoid "prevents use of assertion"
494 (dotimes (axis array-rank
)
495 (setf (%array-dimension array axis
) (pop dims
))))
496 (setf (%array-dimension array
0) dimension-0
))
499 (defun make-array (dimensions &rest args
500 &key
(element-type t
)
501 initial-element initial-contents
505 displaced-index-offset
)
506 (declare (ignore initial-element
507 initial-contents adjustable
508 fill-pointer displaced-to displaced-index-offset
))
509 (declare (explicit-check))
510 (multiple-value-bind (widetag n-bits
) (%vector-widetag-and-n-bits element-type
)
511 (apply #'%make-array dimensions widetag n-bits args
)))
513 (defun make-static-vector (length &key
514 (element-type '(unsigned-byte 8))
515 (initial-contents nil initial-contents-p
)
516 (initial-element nil initial-element-p
))
518 "Allocate vector of LENGTH elements in static space. Only allocation
519 of specialized arrays is supported."
520 ;; STEP 1: check inputs fully
522 ;; This way of doing explicit checks before the vector is allocated
523 ;; is expensive, but probably worth the trouble as once we've allocated
524 ;; the vector we have no way to get rid of it anymore...
525 (when (eq t
(upgraded-array-element-type element-type
))
526 (error "Static arrays of type ~S not supported."
528 (validate-array-initargs initial-element-p initial-element
529 initial-contents-p initial-contents nil
) ; for effect
530 (when initial-contents-p
531 (unless (= length
(length initial-contents
))
532 (error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
533 vector length is ~W."
534 (length initial-contents
)
536 (unless (every (lambda (x) (typep x element-type
)) initial-contents
)
537 (error ":INITIAL-CONTENTS contains elements not of type ~S."
539 (when initial-element-p
540 (unless (typep initial-element element-type
)
541 (error ":INITIAL-ELEMENT ~S is not of type ~S."
542 initial-element element-type
)))
545 ;; Allocate and possibly initialize the vector.
546 (multiple-value-bind (type n-bits
)
547 (%vector-widetag-and-n-bits element-type
)
549 (allocate-static-vector type length
550 (ceiling (* length n-bits
)
551 sb
!vm
:n-word-bits
))))
552 (cond (initial-element-p
553 (fill vector initial-element
))
555 (replace vector initial-contents
))
559 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
560 ;;; specified array characteristics. Dimensions is only used to pass
561 ;;; to FILL-DATA-VECTOR for error checking on the structure of
562 ;;; initial-contents.
563 (defun data-vector-from-inits (dimensions total-size
564 element-type widetag n-bits
565 initialize initial-data
)
566 ;; FIXME: element-type can be NIL when widetag is non-nil,
567 ;; and FILL will check the type, although the error will be not as nice.
568 ;; (cond (typep initial-element element-type)
569 ;; (error "~S cannot be used to initialize an array of type ~S."
570 ;; initial-element element-type))
571 (let ((data (if widetag
572 (allocate-vector-with-widetag widetag total-size n-bits
)
573 (make-array total-size
:element-type element-type
))))
576 (fill (the vector data
) initial-data
))
578 ;; DIMENSIONS can be supplied as a list or integer now
579 (dx-let ((list-of-dims (list dimensions
))) ; ok if already a list
580 (fill-data-vector data
581 (if (listp dimensions
) dimensions list-of-dims
)
586 (defun vector (&rest objects
)
588 "Construct a SIMPLE-VECTOR from the given objects."
589 (let ((v (make-array (length objects
))))
590 (do-rest-arg ((x i
) objects
0 v
)
591 (setf (aref v i
) x
))))
594 ;;;; accessor/setter functions
596 ;;; Dispatch to an optimized routine the data vector accessors for
597 ;;; each different specialized vector type. Do dispatching by looking
598 ;;; up the widetag in the array rather than with the typecases, which
599 ;;; as of 1.0.5 compiles to a naive sequence of linear TYPEPs. Also
600 ;;; provide separate versions where bounds checking has been moved
601 ;;; from the callee to the caller, since it's much cheaper to do once
602 ;;; the type information is available. Finally, for each of these
603 ;;; routines also provide a slow path, taken for arrays that are not
604 ;;; vectors or not simple.
605 (macrolet ((def (name table-name
)
607 (defglobal ,table-name
(make-array ,(1+ sb
!vm
:widetag-mask
)))
608 (declaim (type (simple-array function
(,(1+ sb
!vm
:widetag-mask
)))
610 (defmacro ,name
(array-var)
613 (when (sb!vm
::%other-pointer-p
,array-var
)
614 (setf tag
(%other-pointer-widetag
,array-var
)))
615 (svref ,',table-name tag
)))))))
616 (def !find-data-vector-setter %%data-vector-setters%%
)
617 (def !find-data-vector-setter
/check-bounds %%data-vector-setters
/check-bounds%%
)
618 ;; Used by DO-VECTOR-DATA -- which in turn appears in DOSEQUENCE expansion,
619 ;; meaning we can have post-build dependences on this.
620 (def %find-data-vector-reffer %%data-vector-reffers%%
)
621 (def !find-data-vector-reffer
/check-bounds %%data-vector-reffers
/check-bounds%%
))
623 ;;; Like DOVECTOR, but more magical -- can't use this on host.
624 (defmacro do-vector-data
((elt vector
&optional result
) &body body
)
625 (multiple-value-bind (forms decls
) (parse-body body nil
)
626 (with-unique-names (index vec start end ref
)
627 `(with-array-data ((,vec
,vector
)
630 :check-fill-pointer t
)
631 (let ((,ref
(%find-data-vector-reffer
,vec
)))
632 (declare (function ,ref
))
633 (do ((,index
,start
(1+ ,index
)))
636 ,@(filter-dolist-declarations decls
)
639 (let ((,elt
(funcall ,ref
,vec
,index
)))
641 (tagbody ,@forms
))))))))
643 (macrolet ((%ref
(accessor-getter extra-params
)
644 `(funcall (,accessor-getter array
) array index
,@extra-params
))
645 (define (accessor-name slow-accessor-name accessor-getter
646 extra-params check-bounds
)
648 (defun ,accessor-name
(array index
,@extra-params
)
649 (declare (explicit-check))
650 (declare (optimize speed
651 ;; (SAFETY 0) is ok. All calls to
652 ;; these functions are generated by
653 ;; the compiler, so argument count
654 ;; checking isn't needed. Type checking
655 ;; is done implicitly via the widetag
658 (%ref
,accessor-getter
,extra-params
))
659 (defun ,slow-accessor-name
(array index
,@extra-params
)
660 (declare (optimize speed
(safety 0)))
661 (if (not (%array-displaced-p array
))
662 ;; The reasonably quick path of non-displaced complex
664 (let ((array (%array-data-vector array
)))
665 (%ref
,accessor-getter
,extra-params
))
666 ;; The real slow path.
670 (declare (optimize (speed 1) (safety 1)))
671 (,@check-bounds index
)))
674 (declare (ignore end
))
675 (,accessor-name vector index
,@extra-params
)))))))
676 (define hairy-data-vector-ref slow-hairy-data-vector-ref
677 %find-data-vector-reffer
679 (define hairy-data-vector-set slow-hairy-data-vector-set
680 !find-data-vector-setter
682 (define hairy-data-vector-ref
/check-bounds
683 slow-hairy-data-vector-ref
/check-bounds
684 !find-data-vector-reffer
/check-bounds
685 nil
(check-bound array
(array-dimension array
0)))
686 (define hairy-data-vector-set
/check-bounds
687 slow-hairy-data-vector-set
/check-bounds
688 !find-data-vector-setter
/check-bounds
689 (new-value) (check-bound array
(array-dimension array
0))))
691 (defun hairy-ref-error (array index
&optional new-value
)
692 (declare (ignore index new-value
))
695 :expected-type
'vector
))
697 (macrolet ((define-reffer (saetp check-form
)
698 (let* ((type (sb!vm
:saetp-specifier saetp
))
699 (atype `(simple-array ,type
(*))))
700 `(named-lambda (optimized-data-vector-ref ,type
) (vector index
)
701 (declare (optimize speed
(safety 0))
702 ;; Obviously these all coerce raw words to lispobjs
703 ;; so don't keep spewing notes about it.
704 (muffle-conditions compiler-note
)
707 `(data-vector-ref (the ,atype vector
)
709 (declare (optimize (safety 1)))
711 (,@check-form index
))))
712 `(data-nil-vector-ref (the ,atype vector
) index
)))))
713 (define-setter (saetp check-form
)
714 (let* ((type (sb!vm
:saetp-specifier saetp
))
715 (atype `(simple-array ,type
(*))))
716 `(named-lambda (optimized-data-vector-set ,type
) (vector index new-value
)
717 (declare (optimize speed
(safety 0)))
718 ;; Impossibly setting an elt of an (ARRAY NIL)
719 ;; returns no value. And nobody cares.
720 (declare (muffle-conditions compiler-note
))
721 (data-vector-set (the ,atype vector
)
723 (declare (optimize (safety 1)))
725 (,@check-form index
)))
727 ;; SPEED 1 needed to avoid the compiler
728 ;; from downgrading the type check to
730 (declare (optimize (speed 1)
732 (the ,type new-value
)))
733 ;; For specialized arrays, the return from
734 ;; data-vector-set would have to be reboxed to be a
735 ;; (Lisp) return value; instead, we use the
736 ;; already-boxed value as the return.
738 (define-reffers (symbol deffer check-form slow-path
)
740 ;; FIXME/KLUDGE: can't just FILL here, because genesis doesn't
741 ;; preserve the binding, so re-initiaize as NS doesn't have
742 ;; the energy to figure out to change that right now.
743 (setf ,symbol
(make-array (1+ sb
!vm
::widetag-mask
)
744 :initial-element
#'hairy-ref-error
))
745 ,@(loop for widetag in
'(sb!vm
:complex-vector-widetag
746 sb
!vm
:complex-vector-nil-widetag
747 sb
!vm
:complex-bit-vector-widetag
748 #!+sb-unicode sb
!vm
:complex-character-string-widetag
749 sb
!vm
:complex-base-string-widetag
750 sb
!vm
:simple-array-widetag
751 sb
!vm
:complex-array-widetag
)
752 collect
`(setf (svref ,symbol
,widetag
) ,slow-path
))
753 ,@(loop for saetp across sb
!vm
:*specialized-array-element-type-properties
*
754 for widetag
= (sb!vm
:saetp-typecode saetp
)
755 collect
`(setf (svref ,symbol
,widetag
)
756 (,deffer
,saetp
,check-form
))))))
757 (defun !hairy-data-vector-reffer-init
()
758 (define-reffers %%data-vector-reffers%% define-reffer
760 #'slow-hairy-data-vector-ref
)
761 (define-reffers %%data-vector-setters%% define-setter
763 #'slow-hairy-data-vector-set
)
764 (define-reffers %%data-vector-reffers
/check-bounds%% define-reffer
765 (check-bound vector
(length vector
))
766 #'slow-hairy-data-vector-ref
/check-bounds
)
767 (define-reffers %%data-vector-setters
/check-bounds%% define-setter
768 (check-bound vector
(length vector
))
769 #'slow-hairy-data-vector-set
/check-bounds
)))
771 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
772 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
773 ;;; definition is needed for the compiler to use in constant folding.)
774 (defun data-vector-ref (array index
)
775 (declare (explicit-check))
776 (hairy-data-vector-ref array index
))
778 (defun data-vector-ref-with-offset (array index offset
)
779 (declare (explicit-check))
780 (hairy-data-vector-ref array
(+ index offset
)))
782 (defun invalid-array-p (array)
783 (and (array-header-p array
)
784 (consp (%array-displaced-p array
))))
786 (declaim (ftype (function (array) nil
) invalid-array-error
))
787 (defun invalid-array-error (array)
788 (aver (array-header-p array
))
789 ;; Array invalidation stashes the original dimensions here...
790 (let ((dims (%array-displaced-p array
))
791 (et (array-element-type array
)))
792 (error 'invalid-array-error
797 `(vector ,et
,@dims
)))))
799 (declaim (ftype (function (array t integer
&optional t
) nil
)
800 invalid-array-index-error
))
801 (defun invalid-array-index-error (array index bound
&optional axis
)
802 (if (invalid-array-p array
)
803 (invalid-array-error array
)
804 (error 'invalid-array-index-error
808 :expected-type
`(integer 0 (,bound
)))))
810 ;;; SUBSCRIPTS has a dynamic-extent list structure and is destroyed
811 (defun %array-row-major-index
(array &rest subscripts
)
812 (declare (truly-dynamic-extent subscripts
)
814 (let ((length (length subscripts
)))
815 (cond ((array-header-p array
)
816 (let ((rank (%array-rank array
)))
817 (unless (= rank length
)
818 (error "wrong number of subscripts, ~W, for array of rank ~W."
820 (do ((axis (1- rank
) (1- axis
))
823 ((minusp axis
) result
)
824 (declare (fixnum axis chunk-size result
))
825 (let ((index (fast-&rest-nth axis subscripts
))
826 (dim (%array-dimension array axis
)))
827 (unless (and (fixnump index
) (< -
1 index dim
))
828 (invalid-array-index-error array index dim axis
))
832 (truly-the fixnum
(* chunk-size index
))))
833 chunk-size
(truly-the fixnum
(* chunk-size dim
)))))))
835 (error "Wrong number of subscripts, ~W, for array of rank 1."
838 (let ((index (fast-&rest-nth
0 subscripts
))
839 (length (length (the (simple-array * (*)) array
))))
840 (unless (and (fixnump index
) (< -
1 index length
))
841 (invalid-array-index-error array index length
))
844 (defun array-in-bounds-p (array &rest subscripts
)
846 "Return T if the SUBSCRIPTS are in bounds for the ARRAY, NIL otherwise."
847 (declare (truly-dynamic-extent subscripts
))
848 (let ((length (length subscripts
)))
849 (cond ((array-header-p array
)
850 (let ((rank (%array-rank array
)))
851 (unless (= rank length
)
852 (error "Wrong number of subscripts, ~W, for array of rank ~W."
854 (loop for i below length
855 for s
= (fast-&rest-nth i subscripts
)
856 always
(and (typep s
'(and fixnum unsigned-byte
))
857 (< s
(%array-dimension array i
))))))
859 (error "Wrong number of subscripts, ~W, for array of rank 1."
862 (let ((subscript (fast-&rest-nth
0 subscripts
)))
863 (and (typep subscript
'(and fixnum unsigned-byte
))
865 (length (truly-the (simple-array * (*)) array
)))))))))
867 (defun array-row-major-index (array &rest subscripts
)
868 (declare (truly-dynamic-extent subscripts
))
869 (apply #'%array-row-major-index array subscripts
))
871 (defun aref (array &rest subscripts
)
873 "Return the element of the ARRAY specified by the SUBSCRIPTS."
874 (declare (truly-dynamic-extent subscripts
))
875 (row-major-aref array
(apply #'%array-row-major-index array subscripts
)))
877 ;;; (setf aref/bit/sbit) are implemented using setf-functions,
878 ;;; because they have to work with (setf (apply #'aref array subscripts))
879 ;;; All other setfs can be done using setf-functions too, but I
880 ;;; haven't found technical advantages or disadvantages for either
882 (defun (setf aref
) (new-value array
&rest subscripts
)
883 (declare (truly-dynamic-extent subscripts
)
885 (setf (row-major-aref array
(apply #'%array-row-major-index array subscripts
))
888 (defun row-major-aref (array index
)
890 "Return the element of array corresponding to the row-major index. This is
892 (declare (optimize (safety 1)))
893 (row-major-aref array index
))
895 (defun %set-row-major-aref
(array index new-value
)
896 (declare (optimize (safety 1)))
897 (setf (row-major-aref array index
) new-value
))
899 (defun svref (simple-vector index
)
901 "Return the INDEXth element of the given Simple-Vector."
902 (declare (optimize (safety 1)))
903 (aref simple-vector index
))
905 (defun %svset
(simple-vector index new
)
906 (declare (optimize (safety 1)))
907 (setf (aref simple-vector index
) new
))
909 (defun bit (bit-array &rest subscripts
)
911 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
912 (declare (type (array bit
) bit-array
)
913 (truly-dynamic-extent subscripts
)
914 (optimize (safety 1)))
915 (row-major-aref bit-array
(apply #'%array-row-major-index bit-array subscripts
)))
917 (defun (setf bit
) (new-value bit-array
&rest subscripts
)
918 (declare (type (array bit
) bit-array
)
920 (truly-dynamic-extent subscripts
)
921 (optimize (safety 1)))
922 (setf (row-major-aref bit-array
923 (apply #'%array-row-major-index bit-array subscripts
))
926 (defun sbit (simple-bit-array &rest subscripts
)
928 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
929 (declare (type (simple-array bit
) simple-bit-array
)
930 (truly-dynamic-extent subscripts
)
931 (optimize (safety 1)))
932 (row-major-aref simple-bit-array
933 (apply #'%array-row-major-index simple-bit-array subscripts
)))
935 (defun (setf sbit
) (new-value bit-array
&rest subscripts
)
936 (declare (type (simple-array bit
) bit-array
)
938 (truly-dynamic-extent subscripts
)
939 (optimize (safety 1)))
940 (setf (row-major-aref bit-array
941 (apply #'%array-row-major-index bit-array subscripts
))
944 ;;;; miscellaneous array properties
946 (defun array-element-type (array)
948 "Return the type of the elements of the array"
949 (let ((widetag (%other-pointer-widetag array
))
950 (table (load-time-value
951 (let ((table (make-array 256 :initial-element nil
)))
952 (dotimes (i (length sb
!vm
:*specialized-array-element-type-properties
*) table
)
953 (let* ((saetp (aref sb
!vm
:*specialized-array-element-type-properties
* i
))
954 (typecode (sb!vm
:saetp-typecode saetp
))
955 (complex-typecode (sb!vm
:saetp-complex-typecode saetp
))
956 (specifier (sb!vm
:saetp-specifier saetp
)))
957 (aver (typep specifier
'(or list symbol
)))
958 (setf (aref table typecode
) specifier
)
959 (when complex-typecode
960 (setf (aref table complex-typecode
) specifier
)))))
962 (let ((result (aref table widetag
)))
964 (truly-the (or list symbol
) result
)
965 ;; (MAKE-ARRAY :ELEMENT-TYPE NIL) goes to this branch, but
966 ;; gets the right answer in the end
967 (with-array-data ((array array
) (start) (end))
968 (declare (ignore start end
))
969 (truly-the (or list symbol
) (aref table
(%other-pointer-widetag array
))))))))
971 (defun array-rank (array)
973 "Return the number of dimensions of ARRAY."
974 (if (array-header-p array
)
978 (defun array-dimension (array axis-number
)
980 "Return the length of dimension AXIS-NUMBER of ARRAY."
981 (declare (array array
) (type index axis-number
))
982 (cond ((not (array-header-p array
))
983 (unless (= axis-number
0)
984 (error "Vector axis is not zero: ~S" axis-number
))
985 (length (the (simple-array * (*)) array
)))
986 ((>= axis-number
(%array-rank array
))
987 (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
988 axis-number array
(%array-rank array
)))
990 (%array-dimension array axis-number
))))
992 (defun array-dimensions (array)
994 "Return a list whose elements are the dimensions of the array"
995 (declare (array array
))
996 (if (array-header-p array
)
997 (do ((results nil
(cons (array-dimension array index
) results
))
998 (index (1- (array-rank array
)) (1- index
)))
999 ((minusp index
) results
))
1000 (list (array-dimension array
0))))
1002 (defun array-total-size (array)
1004 "Return the total number of elements in the Array."
1005 (declare (array array
))
1006 (if (array-header-p array
)
1007 (%array-available-elements array
)
1008 (length (the vector array
))))
1010 (defun array-displacement (array)
1012 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
1013 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
1014 (declare (type array array
))
1015 (if (and (array-header-p array
) ; if unsimple and
1016 (%array-displaced-p array
)) ; displaced
1017 (values (%array-data-vector array
) (%array-displacement array
))
1020 (defun adjustable-array-p (array)
1022 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
1023 to the argument, this happens for complex arrays."
1024 (declare (array array
))
1025 ;; Note that this appears not to be a fundamental limitation.
1026 ;; non-vector SIMPLE-ARRAYs are in fact capable of being adjusted,
1027 ;; but in practice we test using ADJUSTABLE-ARRAY-P in ADJUST-ARRAY.
1028 ;; -- CSR, 2004-03-01.
1029 (not (typep array
'simple-array
)))
1031 ;;;; fill pointer frobbing stuff
1033 (declaim (inline array-has-fill-pointer-p
))
1034 (defun array-has-fill-pointer-p (array)
1036 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
1037 (declare (array array
))
1038 (and (array-header-p array
) (%array-fill-pointer-p array
)))
1040 (defun fill-pointer-error (vector &optional arg
)
1042 (aver (array-has-fill-pointer-p vector
))
1043 (let ((max (%array-available-elements vector
)))
1044 (error 'simple-type-error
1046 :expected-type
(list 'integer
0 max
)
1047 :format-control
"The new fill pointer, ~S, is larger than the length of the vector (~S.)"
1048 :format-arguments
(list arg max
))))
1050 (error 'simple-type-error
1052 :expected-type
'(and vector
(satisfies array-has-fill-pointer-p
))
1053 :format-control
"~S is not an array with a fill pointer."
1054 :format-arguments
(list vector
)))))
1056 (declaim (inline fill-pointer
))
1057 (defun fill-pointer (vector)
1059 "Return the FILL-POINTER of the given VECTOR."
1060 (declare (explicit-check))
1061 (if (array-has-fill-pointer-p vector
)
1062 (%array-fill-pointer vector
)
1063 (fill-pointer-error vector
)))
1065 (defun %set-fill-pointer
(vector new
)
1066 (declare (explicit-check))
1068 (fill-pointer-error vector x
)))
1069 (cond ((not (array-has-fill-pointer-p vector
))
1071 ((> new
(%array-available-elements vector
))
1074 (setf (%array-fill-pointer vector
) new
)))))
1076 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
1077 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
1078 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
1079 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
1080 ;;; back to CMU CL).
1081 (defun vector-push (new-element array
)
1083 "Attempt to set the element of ARRAY designated by its fill pointer
1084 to NEW-ELEMENT, and increment the fill pointer by one. If the fill pointer is
1085 too large, NIL is returned, otherwise the index of the pushed element is
1087 (declare (explicit-check))
1088 (let ((fill-pointer (fill-pointer array
)))
1089 (cond ((= fill-pointer
(%array-available-elements array
))
1092 (locally (declare (optimize (safety 0)))
1093 (setf (aref array fill-pointer
) new-element
))
1094 (setf (%array-fill-pointer array
) (1+ fill-pointer
))
1097 (defun vector-push-extend (new-element vector
&optional min-extension
)
1098 (declare (type (or null
(and index
(integer 1))) min-extension
))
1099 (declare (explicit-check))
1100 (let ((fill-pointer (fill-pointer vector
)))
1101 (when (= fill-pointer
(%array-available-elements vector
))
1102 (let ((min-extension
1104 (let ((length (length vector
)))
1106 (- array-dimension-limit length
))))))
1107 (adjust-array vector
(+ fill-pointer
(max 1 min-extension
)))))
1108 ;; disable bounds checking
1109 (locally (declare (optimize (safety 0)))
1110 (setf (aref vector fill-pointer
) new-element
))
1111 (setf (%array-fill-pointer vector
) (1+ fill-pointer
))
1114 (defun vector-pop (array)
1116 "Decrease the fill pointer by 1 and return the element pointed to by the
1118 (declare (explicit-check))
1119 (let ((fill-pointer (fill-pointer array
)))
1120 (if (zerop fill-pointer
)
1121 (error "There is nothing left to pop.")
1122 ;; disable bounds checking (and any fixnum test)
1123 (locally (declare (optimize (safety 0)))
1125 (setf (%array-fill-pointer array
)
1126 (1- fill-pointer
)))))))
1131 (defun adjust-array (array dimensions
&key
1132 (element-type (array-element-type array
) element-type-p
)
1133 (initial-element nil initial-element-p
)
1134 (initial-contents nil initial-contents-p
)
1136 displaced-to displaced-index-offset
)
1138 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
1139 (when (invalid-array-p array
)
1140 (invalid-array-error array
))
1141 (binding* ((dimensions (ensure-list dimensions
))
1142 (array-rank (array-rank array
))
1144 (unless (= (length dimensions
) array-rank
)
1145 (error "The number of dimensions not equal to rank of array.")))
1146 ((initialize initial-data
)
1147 (validate-array-initargs initial-element-p initial-element
1148 initial-contents-p initial-contents
1150 (cond ((and element-type-p
1151 (not (subtypep element-type
(array-element-type array
))))
1152 ;; This is weird. Should check upgraded type against actual
1153 ;; array element type I think. See lp#1331299. CLHS says that
1154 ;; "consequences are unspecified" so current behavior isn't wrong.
1155 (error "The new element type, ~S, is incompatible with old type."
1157 ((and fill-pointer
(/= array-rank
1))
1158 (error "Only vectors can have fill pointers."))
1159 ((and fill-pointer
(not (array-has-fill-pointer-p array
)))
1160 ;; This case always struck me as odd. It seems like it might mean
1161 ;; that the user asks that the array gain a fill-pointer if it didn't
1162 ;; have one, yet CLHS is clear that the argument array must have a
1163 ;; fill-pointer or else signal a type-error.
1164 (fill-pointer-error array
)))
1165 (cond (initial-contents-p
1166 ;; array former contents replaced by INITIAL-CONTENTS
1167 (let* ((array-size (apply #'* dimensions
))
1168 (array-data (data-vector-from-inits
1169 dimensions array-size element-type nil nil
1170 initialize initial-data
)))
1171 (cond ((adjustable-array-p array
)
1172 (set-array-header array array-data array-size
1173 (get-new-fill-pointer array array-size
1175 0 dimensions nil nil
))
1176 ((array-header-p array
)
1177 ;; simple multidimensional or single dimensional array
1178 (make-array dimensions
1179 :element-type element-type
1180 :initial-contents initial-contents
))
1184 ;; We already established that no INITIAL-CONTENTS was supplied.
1185 (unless (or (eql element-type
(array-element-type displaced-to
))
1186 (subtypep element-type
(array-element-type displaced-to
)))
1187 ;; See lp#1331299 again. Require exact match on upgraded type?
1188 (error "can't displace an array of type ~S into another of ~
1190 element-type
(array-element-type displaced-to
)))
1191 (let ((displacement (or displaced-index-offset
0))
1192 (array-size (apply #'* dimensions
)))
1193 (declare (fixnum displacement array-size
))
1194 (if (< (the fixnum
(array-total-size displaced-to
))
1195 (the fixnum
(+ displacement array-size
)))
1196 (error "The :DISPLACED-TO array is too small."))
1197 (if (adjustable-array-p array
)
1198 ;; None of the original contents appear in adjusted array.
1199 (set-array-header array displaced-to array-size
1200 (get-new-fill-pointer array array-size
1202 displacement dimensions t nil
)
1203 ;; simple multidimensional or single dimensional array
1204 (make-array dimensions
1205 :element-type element-type
1206 :displaced-to displaced-to
1207 :displaced-index-offset
1208 displaced-index-offset
))))
1210 (let ((old-length (array-total-size array
))
1211 (new-length (car dimensions
))
1213 (declare (fixnum old-length new-length
))
1214 (with-array-data ((old-data array
) (old-start)
1215 (old-end old-length
))
1216 (cond ((or (and (array-header-p array
)
1217 (%array-displaced-p array
))
1218 (< old-length new-length
))
1220 (data-vector-from-inits
1221 dimensions new-length element-type
1222 (%other-pointer-widetag old-data
) nil
1223 initialize initial-data
))
1224 ;; Provide :END1 to avoid full call to LENGTH
1226 (replace new-data old-data
1228 :start2 old-start
:end2 old-end
))
1230 (shrink-vector old-data new-length
))))
1231 (if (adjustable-array-p array
)
1232 (set-array-header array new-data new-length
1233 (get-new-fill-pointer array new-length
1235 0 dimensions nil nil
)
1238 (let ((old-length (%array-available-elements array
))
1239 (new-length (apply #'* dimensions
)))
1240 (declare (fixnum old-length new-length
))
1241 (with-array-data ((old-data array
) (old-start)
1242 (old-end old-length
))
1243 (declare (ignore old-end
))
1244 (let ((new-data (if (or (and (array-header-p array
)
1245 (%array-displaced-p array
))
1246 (> new-length old-length
)
1247 (not (adjustable-array-p array
)))
1248 (data-vector-from-inits
1249 dimensions new-length
1251 (%other-pointer-widetag old-data
) nil
1252 (if initial-element-p
:initial-element
)
1255 (if (or (zerop old-length
) (zerop new-length
))
1256 (when initial-element-p
(fill new-data initial-element
))
1257 (zap-array-data old-data
(array-dimensions array
)
1259 new-data dimensions new-length
1260 element-type initial-element
1262 (if (adjustable-array-p array
)
1263 (set-array-header array new-data new-length
1264 nil
0 dimensions nil nil
)
1267 sb
!vm
:simple-array-widetag array-rank
)))
1268 (set-array-header new-array new-data new-length
1269 nil
0 dimensions nil t
))))))))))
1272 (defun get-new-fill-pointer (old-array new-array-size fill-pointer
)
1273 (declare (fixnum new-array-size
))
1274 (typecase fill-pointer
1276 ;; "The consequences are unspecified if array is adjusted to a
1277 ;; size smaller than its fill pointer ..."
1278 (when (array-has-fill-pointer-p old-array
)
1279 (when (> (%array-fill-pointer old-array
) new-array-size
)
1280 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
1281 smaller than its fill pointer (~S)"
1282 old-array new-array-size
(fill-pointer old-array
)))
1283 (%array-fill-pointer old-array
)))
1287 (when (> fill-pointer new-array-size
)
1288 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
1289 than the new length of the vector (~S)"
1290 fill-pointer new-array-size
))
1293 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
1294 ;;; which must be less than or equal to its current length. This can
1295 ;;; be called on vectors without a fill pointer but it is extremely
1296 ;;; dangerous to do so: shrinking the size of an object (as viewed by
1297 ;;; the gc) makes bounds checking unreliable in the face of interrupts
1298 ;;; or multi-threading. Call it only on provably local vectors.
1299 (defun %shrink-vector
(vector new-length
)
1300 (declare (vector vector
))
1301 (unless (array-header-p vector
)
1302 (macrolet ((frob (name &rest things
)
1304 ((simple-array nil
(*)) (error 'nil-array-accessed-error
))
1305 ,@(mapcar (lambda (thing)
1306 (destructuring-bind (type-spec fill-value
)
1309 (fill (truly-the ,type-spec
,name
)
1311 :start new-length
))))
1313 ;; Set the 'tail' of the vector to the appropriate type of zero,
1314 ;; "because in some cases we'll scavenge larger areas in one go,
1315 ;; like groups of pages that had triggered the write barrier, or
1316 ;; the whole static space" according to jsnell.
1320 `((simple-array ,(sb!vm
:saetp-specifier saetp
) (*))
1321 ,(if (or (eq (sb!vm
:saetp-specifier saetp
) 'character
)
1323 (eq (sb!vm
:saetp-specifier saetp
) 'base-char
))
1324 *default-init-char-form
*
1325 (sb!vm
:saetp-initial-element-default saetp
))))
1327 #'sb
!vm
:saetp-specifier
1328 sb
!vm
:*specialized-array-element-type-properties
*)))))
1329 ;; Only arrays have fill-pointers, but vectors have their length
1330 ;; parameter in the same place.
1331 (setf (%array-fill-pointer vector
) new-length
)
1334 (defun shrink-vector (vector new-length
)
1335 (declare (vector vector
))
1337 ((eq (length vector
) new-length
)
1339 ((array-has-fill-pointer-p vector
)
1340 (setf (%array-fill-pointer vector
) new-length
)
1342 (t (subseq vector
0 new-length
))))
1344 ;;; BIG THREAD SAFETY NOTE
1346 ;;; ADJUST-ARRAY/SET-ARRAY-HEADER, and its callees are very
1347 ;;; thread unsafe. They are nonatomic, and can mess with parallel
1348 ;;; code using the same arrays.
1350 ;;; A likely seeming fix is an additional level of indirection:
1351 ;;; ARRAY-HEADER -> ARRAY-INFO -> ... where ARRAY-HEADER would
1352 ;;; hold nothing but the pointer to ARRAY-INFO, and ARRAY-INFO
1353 ;;; would hold everything ARRAY-HEADER now holds. This allows
1354 ;;; consing up a new ARRAY-INFO and replacing it atomically in
1355 ;;; the ARRAY-HEADER.
1357 ;;; %WALK-DISPLACED-ARRAY-BACKPOINTERS is an especially nasty
1358 ;;; one: not only is it needed extremely rarely, which makes
1359 ;;; any thread safety bugs involving it look like rare random
1360 ;;; corruption, but because it walks the chain *upwards*, which
1361 ;;; may violate user expectations.
1363 ;;; Fill in array header with the provided information, and return the array.
1364 (defun set-array-header (array data length fill-pointer displacement dimensions
1366 (labels ((%walk-displaced-array-backpointers
(array new-length
)
1367 (dolist (p (%array-displaced-from array
))
1368 (let ((from (weak-pointer-value p
)))
1369 (when (and from
(eq array
(%array-data-vector from
)))
1370 (let ((requires (+ (%array-available-elements from
)
1371 (%array-displacement from
))))
1372 (unless (>= new-length requires
)
1373 ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
1375 ;; "If A is displaced to B, the consequences are unspecified if B is
1376 ;; adjusted in such a way that it no longer has enough elements to
1379 ;; since we're hanging on a weak pointer here, we can't signal an
1380 ;; error right now: the array that we're looking at might be
1381 ;; garbage. Instead, we set all dimensions to zero so that next
1382 ;; safe access to the displaced array will trap. Additionally, we
1383 ;; save the original dimensions, so we can signal a more
1384 ;; understandable error when the time comes.
1385 (%walk-displaced-array-backpointers from
0)
1386 (setf (%array-fill-pointer from
) 0
1387 (%array-available-elements from
) 0
1388 (%array-displaced-p from
) (array-dimensions array
))
1389 (dotimes (i (%array-rank from
))
1390 (setf (%array-dimension from i
) 0)))))))))
1392 (setf (%array-displaced-from array
) nil
)
1393 (%walk-displaced-array-backpointers array length
))
1395 (%save-displaced-array-backpointer array data
))
1396 (setf (%array-data-vector array
) data
)
1397 (setf (%array-available-elements array
) length
)
1399 (setf (%array-fill-pointer array
) fill-pointer
)
1400 (setf (%array-fill-pointer-p array
) t
))
1402 (setf (%array-fill-pointer array
) length
)
1403 (setf (%array-fill-pointer-p array
) nil
)))
1404 (setf (%array-displacement array
) displacement
)
1405 (if (listp dimensions
)
1406 (dotimes (axis (array-rank array
))
1407 (declare (type index axis
))
1408 (setf (%array-dimension array axis
) (pop dimensions
)))
1409 (setf (%array-dimension array
0) dimensions
))
1410 (setf (%array-displaced-p array
) displacedp
)
1413 ;;; User visible extension
1414 (declaim (ftype (sfunction (array) (simple-array * (*))) array-storage-vector
))
1415 (defun array-storage-vector (array)
1417 "Returns the underlying storage vector of ARRAY, which must be a non-displaced array.
1419 In SBCL, if ARRAY is a of type \(SIMPLE-ARRAY * \(*)), it is its own storage
1420 vector. Multidimensional arrays, arrays with fill pointers, and adjustable
1421 arrays have an underlying storage vector with the same ARRAY-ELEMENT-TYPE as
1422 ARRAY, which this function returns.
1424 Important note: the underlying vector is an implementation detail. Even though
1425 this function exposes it, changes in the implementation may cause this
1426 function to be removed without further warning."
1427 ;; KLUDGE: Without TRULY-THE the system is not smart enough to figure out that
1428 ;; the return value is always of the known type.
1429 (truly-the (simple-array * (*))
1430 (cond ((not (array-header-p array
))
1432 ((%array-displaced-p array
)
1433 (error "~S cannot be used with displaced arrays. Use ~S instead."
1434 'array-storage-vector
'array-displacement
))
1436 (%array-data-vector array
)))))
1439 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
1441 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
1442 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
1443 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
1444 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
1445 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
1446 element-type initial-element initial-element-p
)
1447 (declare (list old-dims new-dims
)
1448 (fixnum new-length
))
1449 ;; OLD-DIMS comes from array-dimensions, which returns a fresh list
1450 ;; at least in SBCL.
1451 ;; NEW-DIMS comes from the user.
1452 (setf old-dims
(nreverse old-dims
)
1453 new-dims
(reverse new-dims
))
1454 (cond ((eq old-data new-data
)
1455 ;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and
1456 ;; INITIAL-ELEMENT-P are used when OLD-DATA and NEW-DATA are
1457 ;; EQ; in this case, a temporary must be used and filled
1458 ;; appropriately. specified initial-element.
1459 ;; FIXME: transforming this TYPEP to someting a bit faster
1460 ;; would be a win...
1461 (unless (or (not initial-element-p
)
1462 (typep initial-element element-type
))
1463 (error "~S can't be used to initialize an array of type ~S."
1464 initial-element element-type
))
1465 (let ((temp (if initial-element-p
1466 (make-array new-length
:initial-element initial-element
)
1467 (make-array new-length
))))
1468 (declare (simple-vector temp
))
1469 (zap-array-data-aux old-data old-dims offset temp new-dims
)
1470 (dotimes (i new-length
)
1471 (setf (aref new-data i
) (aref temp i
)))
1472 ;; Kill the temporary vector to prevent garbage retention.
1473 (%shrink-vector temp
0)))
1475 ;; When OLD-DATA and NEW-DATA are not EQ, NEW-DATA has
1476 ;; already been filled with any
1477 (zap-array-data-aux old-data old-dims offset new-data new-dims
))))
1479 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims
)
1480 (declare (fixnum offset
))
1481 (let ((limits (mapcar (lambda (x y
)
1482 (declare (fixnum x y
))
1483 (1- (the fixnum
(min x y
))))
1484 old-dims new-dims
)))
1485 (macrolet ((bump-index-list (index limits
)
1486 `(do ((subscripts ,index
(cdr subscripts
))
1487 (limits ,limits
(cdr limits
)))
1488 ((null subscripts
) :eof
)
1489 (cond ((< (the fixnum
(car subscripts
))
1490 (the fixnum
(car limits
)))
1492 (1+ (the fixnum
(car subscripts
))))
1494 (t (rplaca subscripts
0))))))
1495 (do ((index (make-list (length old-dims
) :initial-element
0)
1496 (bump-index-list index limits
)))
1498 (setf (aref new-data
(row-major-index-from-dims index new-dims
))
1500 (+ (the fixnum
(row-major-index-from-dims index old-dims
))
1503 ;;; Figure out the row-major-order index of an array reference from a
1504 ;;; list of subscripts and a list of dimensions. This is for internal
1505 ;;; calls only, and the subscripts and dim-list variables are assumed
1506 ;;; to be reversed from what the user supplied.
1507 (defun row-major-index-from-dims (rev-subscripts rev-dim-list
)
1508 (do ((rev-subscripts rev-subscripts
(cdr rev-subscripts
))
1509 (rev-dim-list rev-dim-list
(cdr rev-dim-list
))
1512 ((null rev-dim-list
) result
)
1513 (declare (fixnum chunk-size result
))
1514 (setq result
(+ result
1515 (the fixnum
(* (the fixnum
(car rev-subscripts
))
1517 (setq chunk-size
(* chunk-size
(the fixnum
(car rev-dim-list
))))))
1521 (defun bit-array-same-dimensions-p (array1 array2
)
1522 (declare (type (array bit
) array1 array2
))
1523 (and (= (array-rank array1
)
1524 (array-rank array2
))
1525 (dotimes (index (array-rank array1
) t
)
1526 (when (/= (array-dimension array1 index
)
1527 (array-dimension array2 index
))
1530 (defun pick-result-array (result-bit-array bit-array-1
)
1531 (case result-bit-array
1533 ((nil) (make-array (array-dimensions bit-array-1
)
1535 :initial-element
0))
1537 (unless (bit-array-same-dimensions-p bit-array-1
1539 (error "~S and ~S don't have the same dimensions."
1540 bit-array-1 result-bit-array
))
1543 (defmacro def-bit-array-op
(name function
)
1544 `(defun ,name
(bit-array-1 bit-array-2
&optional result-bit-array
)
1547 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1548 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1549 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1550 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1551 All the arrays must have the same rank and dimensions."
1552 (symbol-name function
))
1553 (declare (type (array bit
) bit-array-1 bit-array-2
)
1554 (type (or (array bit
) (member t nil
)) result-bit-array
))
1555 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2
)
1556 (error "~S and ~S don't have the same dimensions."
1557 bit-array-1 bit-array-2
))
1558 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1
)))
1559 (if (and (simple-bit-vector-p bit-array-1
)
1560 (simple-bit-vector-p bit-array-2
)
1561 (simple-bit-vector-p result-bit-array
))
1562 (locally (declare (optimize (speed 3) (safety 0)))
1563 (,name bit-array-1 bit-array-2 result-bit-array
))
1564 (with-array-data ((data1 bit-array-1
) (start1) (end1))
1565 (declare (ignore end1
))
1566 (with-array-data ((data2 bit-array-2
) (start2) (end2))
1567 (declare (ignore end2
))
1568 (with-array-data ((data3 result-bit-array
) (start3) (end3))
1569 (do ((index-1 start1
(1+ index-1
))
1570 (index-2 start2
(1+ index-2
))
1571 (index-3 start3
(1+ index-3
)))
1572 ((>= index-3 end3
) result-bit-array
)
1573 (declare (type index index-1 index-2 index-3
))
1574 (setf (sbit data3 index-3
)
1575 (logand (,function
(sbit data1 index-1
)
1576 (sbit data2 index-2
))
1579 (def-bit-array-op bit-and logand
)
1580 (def-bit-array-op bit-ior logior
)
1581 (def-bit-array-op bit-xor logxor
)
1582 (def-bit-array-op bit-eqv logeqv
)
1583 (def-bit-array-op bit-nand lognand
)
1584 (def-bit-array-op bit-nor lognor
)
1585 (def-bit-array-op bit-andc1 logandc1
)
1586 (def-bit-array-op bit-andc2 logandc2
)
1587 (def-bit-array-op bit-orc1 logorc1
)
1588 (def-bit-array-op bit-orc2 logorc2
)
1590 (defun bit-not (bit-array &optional result-bit-array
)
1592 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1593 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1594 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1595 created. Both arrays must have the same rank and dimensions."
1596 (declare (type (array bit
) bit-array
)
1597 (type (or (array bit
) (member t nil
)) result-bit-array
))
1598 (let ((result-bit-array (pick-result-array result-bit-array bit-array
)))
1599 (if (and (simple-bit-vector-p bit-array
)
1600 (simple-bit-vector-p result-bit-array
))
1601 (locally (declare (optimize (speed 3) (safety 0)))
1602 (bit-not bit-array result-bit-array
))
1603 (with-array-data ((src bit-array
) (src-start) (src-end))
1604 (declare (ignore src-end
))
1605 (with-array-data ((dst result-bit-array
) (dst-start) (dst-end))
1606 (do ((src-index src-start
(1+ src-index
))
1607 (dst-index dst-start
(1+ dst-index
)))
1608 ((>= dst-index dst-end
) result-bit-array
)
1609 (declare (type index src-index dst-index
))
1610 (setf (sbit dst dst-index
)
1611 (logxor (sbit src src-index
) 1))))))))
1613 ;;;; array type dispatching
1615 ;;; Given DISPATCH-FOO as the DISPATCH-NAME argument (unevaluated),
1616 ;;; defines the functions
1618 ;;; DISPATCH-FOO/SIMPLE-BASE-STRING
1619 ;;; DISPATCH-FOO/SIMPLE-CHARACTER-STRING
1620 ;;; DISPATCH-FOO/SIMPLE-ARRAY-SINGLE-FLOAT
1623 ;;; PARAMS are the function parameters in the definition of each
1624 ;;; specializer function. The array being specialized must be the
1625 ;;; first parameter in PARAMS. A type declaration for this parameter
1626 ;;; is automatically inserted into the body of each function.
1628 ;;; The dispatch table %%FOO-FUNS%% is defined and populated by these
1629 ;;; functions. The table is padded by the function
1630 ;;; HAIRY-FOO-DISPATCH-ERROR, also defined by DEFINE-ARRAY-DISPATCH.
1632 ;;; Finally, the DISPATCH-FOO macro is defined which does the actual
1633 ;;; dispatching when called. It expects arguments that match PARAMS.
1635 (defmacro !define-array-dispatch
(dispatch-name params
&body body
)
1636 (let ((table-name (symbolicate "%%" dispatch-name
"-FUNS%%"))
1637 (error-name (symbolicate "HAIRY-" dispatch-name
"-ERROR")))
1639 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
1640 (defun ,error-name
(&rest args
)
1643 :expected-type
'(simple-array * (*)))))
1644 (!defglobal
,table-name
(make-array ,(1+ sb
!vm
:widetag-mask
)
1645 :initial-element
#',error-name
))
1646 ,@(loop for info across sb
!vm
:*specialized-array-element-type-properties
*
1647 for typecode
= (sb!vm
:saetp-typecode info
)
1648 for specifier
= (sb!vm
:saetp-specifier info
)
1649 for primitive-type-name
= (sb!vm
:saetp-primitive-type-name info
)
1650 collect
(let ((fun-name (symbolicate (string dispatch-name
)
1651 "/" primitive-type-name
)))
1653 (defun ,fun-name
,params
1654 (declare (type (simple-array ,specifier
(*))
1657 (setf (svref ,table-name
,typecode
) #',fun-name
))))
1658 (defmacro ,dispatch-name
(&rest args
)
1659 (check-type (first args
) symbol
)
1660 (let ((tag (gensym "TAG")))
1664 (when (sb!vm
::%other-pointer-p
,(first args
))
1665 (setf ,tag
(%other-pointer-widetag
,(first args
))))
1666 (svref ,',table-name
,tag
)))