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
))
278 (handler-case (specifier-type type
)
279 (parse-unknown-type ()
280 (return (result sb
!vm
:simple-vector-widetag
))))))
282 (union-type ; FIXME: forward ref
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
)))))
299 (character-set-type ; FIXME: forward ref
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 ;; Complain in various ways about wrong :INITIAL-foo arguments,
368 ;; returning the two initialization arguments needed for DATA-VECTOR-FROM-INITS.
369 (defun validate-array-initargs (element-p element contents-p contents displaced
)
370 (cond ((and displaced
(or element-p contents-p
))
371 (if (and element-p contents-p
)
372 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
373 may be specified with the :DISPLACED-TO option")
374 (error "~S may not be specified with the :DISPLACED-TO option"
375 (if element-p
:initial-element
:initial-contents
))))
376 ((and element-p contents-p
)
377 (error "Can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
378 (element-p (values :initial-element element
))
379 (contents-p (values :initial-contents contents
))
380 (t (values nil nil
))))
382 ;;; Widetag is the widetag of the underlying vector,
383 ;;; it'll be the same as the resulting array widetag only for simple vectors
384 (defun %make-array
(dimensions widetag n-bits
387 (initial-element nil initial-element-p
)
388 (initial-contents nil initial-contents-p
)
389 adjustable fill-pointer
390 displaced-to displaced-index-offset
)
391 (declare (ignore element-type
))
392 (binding* (((array-rank dimension-0
)
393 (if (listp dimensions
)
394 (values (length dimensions
)
395 (if dimensions
(car dimensions
) 1))
396 (values 1 dimensions
)))
397 ((initialize initial-data
)
398 (validate-array-initargs initial-element-p initial-element
399 initial-contents-p initial-contents
401 (simple (and (null fill-pointer
)
403 (null displaced-to
))))
404 (declare (type array-rank array-rank
))
405 (declare (type index dimension-0
))
406 (cond ((and displaced-index-offset
(null displaced-to
))
407 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
408 ((and simple
(= array-rank
1))
409 (let ((vector ; a (SIMPLE-ARRAY * (*))
410 (allocate-vector-with-widetag widetag dimension-0 n-bits
)))
411 ;; presence of at most one :INITIAL-thing keyword was ensured above
412 (cond (initial-element-p
413 (fill vector initial-element
))
415 (let ((content-length (length initial-contents
)))
416 (unless (= dimension-0 content-length
)
417 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
418 the vector length is ~W."
419 content-length dimension-0
)))
420 (replace vector initial-contents
)))
422 ((and (arrayp displaced-to
)
423 (/= (array-underlying-widetag displaced-to
) widetag
))
424 (error "Array element type of :DISPLACED-TO array does not match specified element type"))
426 ;; it's non-simple or multidimensional, or both.
428 (unless (= array-rank
1)
429 (error "Only vectors can have fill pointers."))
430 (when (and (integerp fill-pointer
) (> fill-pointer dimension-0
))
431 ;; FIXME: should be TYPE-ERROR?
432 (error "invalid fill-pointer ~W" fill-pointer
)))
434 (if (consp dimensions
)
435 (the index
(reduce (lambda (a b
) (* a
(the index b
)))
437 ;; () is considered to have dimension-0 = 1.
438 ;; It avoids the REDUCE lambda being called with no args.
440 (data (or displaced-to
441 (data-vector-from-inits
442 dimensions total-size nil widetag n-bits
443 initialize initial-data
)))
444 (array (make-array-header
445 (cond ((= array-rank
1)
446 (%complex-vector-widetag widetag
))
447 (simple sb
!vm
:simple-array-widetag
)
448 (t sb
!vm
:complex-array-widetag
))
451 (setf (%array-fill-pointer-p array
) t
452 (%array-fill-pointer array
)
453 (if (eq fill-pointer t
) dimension-0 fill-pointer
))
454 (setf (%array-fill-pointer-p array
) nil
455 (%array-fill-pointer array
) total-size
))
456 (setf (%array-available-elements array
) total-size
)
457 ;; Terrible name for this slot - we displace to the
458 ;; target array's header, if any, not the "ultimate"
459 ;; vector in the chain of displacements.
460 (setf (%array-data-vector array
) data
)
461 (setf (%array-displaced-from array
) nil
)
463 (let ((offset (or displaced-index-offset
0)))
464 (when (> (+ offset total-size
)
465 (array-total-size displaced-to
))
466 (error "~S doesn't have enough elements." displaced-to
))
467 (setf (%array-displacement array
) offset
)
468 (setf (%array-displaced-p array
) t
)
469 (%save-displaced-array-backpointer array data
)))
471 (setf (%array-displaced-p array
) nil
)))
472 (if (listp dimensions
)
473 (let ((dims dimensions
)) ; avoid "prevents use of assertion"
474 (dotimes (axis array-rank
)
475 (setf (%array-dimension array axis
) (pop dims
))))
476 (setf (%array-dimension array
0) dimension-0
))
479 (defun make-array (dimensions &rest args
480 &key
(element-type t
)
481 initial-element initial-contents
485 displaced-index-offset
)
486 (declare (ignore initial-element
487 initial-contents adjustable
488 fill-pointer displaced-to displaced-index-offset
))
489 (declare (explicit-check))
490 (multiple-value-bind (widetag n-bits
) (%vector-widetag-and-n-bits element-type
)
491 (apply #'%make-array dimensions widetag n-bits args
)))
493 (defun make-static-vector (length &key
494 (element-type '(unsigned-byte 8))
495 (initial-contents nil initial-contents-p
)
496 (initial-element nil initial-element-p
))
498 "Allocate vector of LENGTH elements in static space. Only allocation
499 of specialized arrays is supported."
500 ;; STEP 1: check inputs fully
502 ;; This way of doing explicit checks before the vector is allocated
503 ;; is expensive, but probably worth the trouble as once we've allocated
504 ;; the vector we have no way to get rid of it anymore...
505 (when (eq t
(upgraded-array-element-type element-type
))
506 (error "Static arrays of type ~S not supported."
508 (validate-array-initargs initial-element-p initial-element
509 initial-contents-p initial-contents nil
) ; for effect
510 (when initial-contents-p
511 (unless (= length
(length initial-contents
))
512 (error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
513 vector length is ~W."
514 (length initial-contents
)
516 (unless (every (lambda (x) (typep x element-type
)) initial-contents
)
517 (error ":INITIAL-CONTENTS contains elements not of type ~S."
519 (when initial-element-p
520 (unless (typep initial-element element-type
)
521 (error ":INITIAL-ELEMENT ~S is not of type ~S."
522 initial-element element-type
)))
525 ;; Allocate and possibly initialize the vector.
526 (multiple-value-bind (type n-bits
)
527 (sb!impl
::%vector-widetag-and-n-bits element-type
)
529 (allocate-static-vector type length
530 (ceiling (* length n-bits
)
531 sb
!vm
:n-word-bits
))))
532 (cond (initial-element-p
533 (fill vector initial-element
))
535 (replace vector initial-contents
))
539 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
540 ;;; specified array characteristics. Dimensions is only used to pass
541 ;;; to FILL-DATA-VECTOR for error checking on the structure of
542 ;;; initial-contents.
543 (defun data-vector-from-inits (dimensions total-size
544 element-type widetag n-bits
545 initialize initial-data
)
546 ;; FIXME: element-type can be NIL when widetag is non-nil,
547 ;; and FILL will check the type, although the error will be not as nice.
548 ;; (cond (typep initial-element element-type)
549 ;; (error "~S cannot be used to initialize an array of type ~S."
550 ;; initial-element element-type))
551 (let ((data (if widetag
552 (allocate-vector-with-widetag widetag total-size n-bits
)
553 (make-array total-size
:element-type element-type
))))
556 (fill (the vector data
) initial-data
))
558 ;; DIMENSIONS can be supplied as a list or integer now
559 (dx-let ((list-of-dims (list dimensions
))) ; ok if already a list
560 (fill-data-vector data
561 (if (listp dimensions
) dimensions list-of-dims
)
566 (defun vector (&rest objects
)
568 "Construct a SIMPLE-VECTOR from the given objects."
569 (let ((v (make-array (length objects
))))
570 (do-rest-arg ((x i
) objects
0 v
)
571 (setf (aref v i
) x
))))
574 ;;;; accessor/setter functions
576 ;;; Dispatch to an optimized routine the data vector accessors for
577 ;;; each different specialized vector type. Do dispatching by looking
578 ;;; up the widetag in the array rather than with the typecases, which
579 ;;; as of 1.0.5 compiles to a naive sequence of linear TYPEPs. Also
580 ;;; provide separate versions where bounds checking has been moved
581 ;;; from the callee to the caller, since it's much cheaper to do once
582 ;;; the type information is available. Finally, for each of these
583 ;;; routines also provide a slow path, taken for arrays that are not
584 ;;; vectors or not simple.
585 (macrolet ((def (name table-name
)
587 (defglobal ,table-name
(make-array ,(1+ sb
!vm
:widetag-mask
)))
588 (defmacro ,name
(array-var)
591 (when (sb!vm
::%other-pointer-p
,array-var
)
592 (setf tag
(%other-pointer-widetag
,array-var
)))
593 (svref ,',table-name tag
)))))))
594 (def !find-data-vector-setter %%data-vector-setters%%
)
595 (def !find-data-vector-setter
/check-bounds %%data-vector-setters
/check-bounds%%
)
596 ;; Used by DO-VECTOR-DATA -- which in turn appears in DOSEQUENCE expansion,
597 ;; meaning we can have post-build dependences on this.
598 (def %find-data-vector-reffer %%data-vector-reffers%%
)
599 (def !find-data-vector-reffer
/check-bounds %%data-vector-reffers
/check-bounds%%
))
601 ;;; Like DOVECTOR, but more magical -- can't use this on host.
602 (defmacro do-vector-data
((elt vector
&optional result
) &body body
)
603 (multiple-value-bind (forms decls
) (parse-body body nil
)
604 (with-unique-names (index vec start end ref
)
605 `(with-array-data ((,vec
,vector
)
608 :check-fill-pointer t
)
609 (let ((,ref
(%find-data-vector-reffer
,vec
)))
610 (do ((,index
,start
(1+ ,index
)))
613 ,@(filter-dolist-declarations decls
)
616 (let ((,elt
(funcall ,ref
,vec
,index
)))
618 (tagbody ,@forms
))))))))
620 (macrolet ((%ref
(accessor-getter extra-params
)
621 `(funcall (,accessor-getter array
) array index
,@extra-params
))
622 (define (accessor-name slow-accessor-name accessor-getter
623 extra-params check-bounds
)
625 (defun ,accessor-name
(array index
,@extra-params
)
626 (declare (explicit-check))
627 (declare (optimize speed
628 ;; (SAFETY 0) is ok. All calls to
629 ;; these functions are generated by
630 ;; the compiler, so argument count
631 ;; checking isn't needed. Type checking
632 ;; is done implicitly via the widetag
635 (%ref
,accessor-getter
,extra-params
))
636 (defun ,slow-accessor-name
(array index
,@extra-params
)
637 (declare (optimize speed
(safety 0)))
638 (if (not (%array-displaced-p array
))
639 ;; The reasonably quick path of non-displaced complex
641 (let ((array (%array-data-vector array
)))
642 (%ref
,accessor-getter
,extra-params
))
643 ;; The real slow path.
647 (declare (optimize (speed 1) (safety 1)))
648 (,@check-bounds index
)))
651 (declare (ignore end
))
652 (,accessor-name vector index
,@extra-params
)))))))
653 (define hairy-data-vector-ref slow-hairy-data-vector-ref
654 %find-data-vector-reffer
656 (define hairy-data-vector-set slow-hairy-data-vector-set
657 !find-data-vector-setter
659 (define hairy-data-vector-ref
/check-bounds
660 slow-hairy-data-vector-ref
/check-bounds
661 !find-data-vector-reffer
/check-bounds
662 nil
(check-bound array
(array-dimension array
0)))
663 (define hairy-data-vector-set
/check-bounds
664 slow-hairy-data-vector-set
/check-bounds
665 !find-data-vector-setter
/check-bounds
666 (new-value) (check-bound array
(array-dimension array
0))))
668 (defun hairy-ref-error (array index
&optional new-value
)
669 (declare (ignore index new-value
))
672 :expected-type
'vector
))
674 (macrolet ((define-reffer (saetp check-form
)
675 (let* ((type (sb!vm
:saetp-specifier saetp
))
676 (atype `(simple-array ,type
(*))))
677 `(named-lambda (optimized-data-vector-ref ,type
) (vector index
)
678 (declare (optimize speed
(safety 0))
679 ;; Obviously these all coerce raw words to lispobjs
680 ;; so don't keep spewing notes about it.
681 (muffle-conditions compiler-note
)
684 `(data-vector-ref (the ,atype vector
)
686 (declare (optimize (safety 1)))
688 (,@check-form index
))))
689 `(data-nil-vector-ref (the ,atype vector
) index
)))))
690 (define-setter (saetp check-form
)
691 (let* ((type (sb!vm
:saetp-specifier saetp
))
692 (atype `(simple-array ,type
(*))))
693 `(named-lambda (optimized-data-vector-set ,type
) (vector index new-value
)
694 (declare (optimize speed
(safety 0)))
695 ;; Impossibly setting an elt of an (ARRAY NIL)
696 ;; returns no value. And nobody cares.
697 (declare (muffle-conditions compiler-note
))
698 (data-vector-set (the ,atype vector
)
700 (declare (optimize (safety 1)))
702 (,@check-form index
)))
704 ;; SPEED 1 needed to avoid the compiler
705 ;; from downgrading the type check to
707 (declare (optimize (speed 1)
709 (the ,type new-value
)))
710 ;; For specialized arrays, the return from
711 ;; data-vector-set would have to be reboxed to be a
712 ;; (Lisp) return value; instead, we use the
713 ;; already-boxed value as the return.
715 (define-reffers (symbol deffer check-form slow-path
)
717 ;; FIXME/KLUDGE: can't just FILL here, because genesis doesn't
718 ;; preserve the binding, so re-initiaize as NS doesn't have
719 ;; the energy to figure out to change that right now.
720 (setf ,symbol
(make-array (1+ sb
!vm
::widetag-mask
)
721 :initial-element
#'hairy-ref-error
))
722 ,@(loop for widetag in
'(sb!vm
:complex-vector-widetag
723 sb
!vm
:complex-vector-nil-widetag
724 sb
!vm
:complex-bit-vector-widetag
725 #!+sb-unicode sb
!vm
:complex-character-string-widetag
726 sb
!vm
:complex-base-string-widetag
727 sb
!vm
:simple-array-widetag
728 sb
!vm
:complex-array-widetag
)
729 collect
`(setf (svref ,symbol
,widetag
) ,slow-path
))
730 ,@(loop for saetp across sb
!vm
:*specialized-array-element-type-properties
*
731 for widetag
= (sb!vm
:saetp-typecode saetp
)
732 collect
`(setf (svref ,symbol
,widetag
)
733 (,deffer
,saetp
,check-form
))))))
734 (defun !hairy-data-vector-reffer-init
()
735 (define-reffers %%data-vector-reffers%% define-reffer
737 #'slow-hairy-data-vector-ref
)
738 (define-reffers %%data-vector-setters%% define-setter
740 #'slow-hairy-data-vector-set
)
741 (define-reffers %%data-vector-reffers
/check-bounds%% define-reffer
742 (check-bound vector
(length vector
))
743 #'slow-hairy-data-vector-ref
/check-bounds
)
744 (define-reffers %%data-vector-setters
/check-bounds%% define-setter
745 (check-bound vector
(length vector
))
746 #'slow-hairy-data-vector-set
/check-bounds
)))
748 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
749 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
750 ;;; definition is needed for the compiler to use in constant folding.)
751 (defun data-vector-ref (array index
)
752 (declare (explicit-check))
753 (hairy-data-vector-ref array index
))
755 (defun data-vector-ref-with-offset (array index offset
)
756 (declare (explicit-check))
757 (hairy-data-vector-ref array
(+ index offset
)))
759 (defun invalid-array-p (array)
760 (and (array-header-p array
)
761 (consp (%array-displaced-p array
))))
763 (declaim (ftype (function (array) nil
) invalid-array-error
))
764 (defun invalid-array-error (array)
765 (aver (array-header-p array
))
766 ;; Array invalidation stashes the original dimensions here...
767 (let ((dims (%array-displaced-p array
))
768 (et (array-element-type array
)))
769 (error 'invalid-array-error
774 `(vector ,et
,@dims
)))))
776 (declaim (ftype (function (array t integer
&optional t
) nil
)
777 invalid-array-index-error
))
778 (defun invalid-array-index-error (array index bound
&optional axis
)
779 (if (invalid-array-p array
)
780 (invalid-array-error array
)
781 (error 'invalid-array-index-error
785 :expected-type
`(integer 0 (,bound
)))))
787 ;;; SUBSCRIPTS has a dynamic-extent list structure and is destroyed
788 (defun %array-row-major-index
(array &rest subscripts
)
789 (declare (truly-dynamic-extent subscripts
)
791 (let ((length (length subscripts
)))
792 (cond ((array-header-p array
)
793 (let ((rank (%array-rank array
)))
794 (unless (= rank length
)
795 (error "wrong number of subscripts, ~W, for array of rank ~W."
797 (do ((axis (1- rank
) (1- axis
))
800 ((minusp axis
) result
)
801 (declare (fixnum axis chunk-size result
))
802 (let ((index (fast-&rest-nth axis subscripts
))
803 (dim (%array-dimension array axis
)))
804 (unless (and (fixnump index
) (< -
1 index dim
))
805 (invalid-array-index-error array index dim axis
))
809 (truly-the fixnum
(* chunk-size index
))))
810 chunk-size
(truly-the fixnum
(* chunk-size dim
)))))))
812 (error "Wrong number of subscripts, ~W, for array of rank 1."
815 (let ((index (fast-&rest-nth
0 subscripts
))
816 (length (length (the (simple-array * (*)) array
))))
817 (unless (and (fixnump index
) (< -
1 index length
))
818 (invalid-array-index-error array index length
))
821 (defun array-in-bounds-p (array &rest subscripts
)
823 "Return T if the SUBSCRIPTS are in bounds for the ARRAY, NIL otherwise."
824 (declare (truly-dynamic-extent subscripts
))
825 (let ((length (length subscripts
)))
826 (cond ((array-header-p array
)
827 (let ((rank (%array-rank array
)))
828 (unless (= rank length
)
829 (error "Wrong number of subscripts, ~W, for array of rank ~W."
831 (loop for i below length
832 for s
= (fast-&rest-nth i subscripts
)
833 always
(and (typep s
'(and fixnum unsigned-byte
))
834 (< s
(%array-dimension array i
))))))
836 (error "Wrong number of subscripts, ~W, for array of rank 1."
839 (let ((subscript (fast-&rest-nth
0 subscripts
)))
840 (and (typep subscript
'(and fixnum unsigned-byte
))
842 (length (truly-the (simple-array * (*)) array
)))))))))
844 (defun array-row-major-index (array &rest subscripts
)
845 (declare (truly-dynamic-extent subscripts
))
846 (apply #'%array-row-major-index array subscripts
))
848 (defun aref (array &rest subscripts
)
850 "Return the element of the ARRAY specified by the SUBSCRIPTS."
851 (declare (truly-dynamic-extent subscripts
))
852 (row-major-aref array
(apply #'%array-row-major-index array subscripts
)))
854 ;;; (setf aref/bit/sbit) are implemented using setf-functions,
855 ;;; because they have to work with (setf (apply #'aref array subscripts))
856 ;;; All other setfs can be done using setf-functions too, but I
857 ;;; haven't found technical advantages or disadvantages for either
859 (defun (setf aref
) (new-value array
&rest subscripts
)
860 (declare (truly-dynamic-extent subscripts
)
862 (setf (row-major-aref array
(apply #'%array-row-major-index array subscripts
))
865 (defun row-major-aref (array index
)
867 "Return the element of array corresponding to the row-major index. This is
869 (declare (optimize (safety 1)))
870 (row-major-aref array index
))
872 (defun %set-row-major-aref
(array index new-value
)
873 (declare (optimize (safety 1)))
874 (setf (row-major-aref array index
) new-value
))
876 (defun svref (simple-vector index
)
878 "Return the INDEXth element of the given Simple-Vector."
879 (declare (optimize (safety 1)))
880 (aref simple-vector index
))
882 (defun %svset
(simple-vector index new
)
883 (declare (optimize (safety 1)))
884 (setf (aref simple-vector index
) new
))
886 (defun bit (bit-array &rest subscripts
)
888 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
889 (declare (type (array bit
) bit-array
)
890 (truly-dynamic-extent subscripts
)
891 (optimize (safety 1)))
892 (row-major-aref bit-array
(apply #'%array-row-major-index bit-array subscripts
)))
894 (defun (setf bit
) (new-value bit-array
&rest subscripts
)
895 (declare (type (array bit
) bit-array
)
897 (truly-dynamic-extent subscripts
)
898 (optimize (safety 1)))
899 (setf (row-major-aref bit-array
900 (apply #'%array-row-major-index bit-array subscripts
))
903 (defun sbit (simple-bit-array &rest subscripts
)
905 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
906 (declare (type (simple-array bit
) simple-bit-array
)
907 (truly-dynamic-extent subscripts
)
908 (optimize (safety 1)))
909 (row-major-aref simple-bit-array
910 (apply #'%array-row-major-index simple-bit-array subscripts
)))
912 (defun (setf sbit
) (new-value bit-array
&rest subscripts
)
913 (declare (type (simple-array bit
) bit-array
)
915 (truly-dynamic-extent subscripts
)
916 (optimize (safety 1)))
917 (setf (row-major-aref bit-array
918 (apply #'%array-row-major-index bit-array subscripts
))
921 ;;;; miscellaneous array properties
923 (defun array-element-type (array)
925 "Return the type of the elements of the array"
926 (let ((widetag (%other-pointer-widetag array
))
927 (table (load-time-value
928 (let ((table (make-array 256 :initial-element nil
)))
929 (dotimes (i (length sb
!vm
:*specialized-array-element-type-properties
*) table
)
930 (let* ((saetp (aref sb
!vm
:*specialized-array-element-type-properties
* i
))
931 (typecode (sb!vm
:saetp-typecode saetp
))
932 (complex-typecode (sb!vm
:saetp-complex-typecode saetp
))
933 (specifier (sb!vm
:saetp-specifier saetp
)))
934 (aver (typep specifier
'(or list symbol
)))
935 (setf (aref table typecode
) specifier
)
936 (when complex-typecode
937 (setf (aref table complex-typecode
) specifier
)))))
939 (let ((result (aref table widetag
)))
941 (truly-the (or list symbol
) result
)
942 ;; (MAKE-ARRAY :ELEMENT-TYPE NIL) goes to this branch, but
943 ;; gets the right answer in the end
944 (with-array-data ((array array
) (start) (end))
945 (declare (ignore start end
))
946 (truly-the (or list symbol
) (aref table
(%other-pointer-widetag array
))))))))
948 (defun array-rank (array)
950 "Return the number of dimensions of ARRAY."
951 (if (array-header-p array
)
955 (defun array-dimension (array axis-number
)
957 "Return the length of dimension AXIS-NUMBER of ARRAY."
958 (declare (array array
) (type index axis-number
))
959 (cond ((not (array-header-p array
))
960 (unless (= axis-number
0)
961 (error "Vector axis is not zero: ~S" axis-number
))
962 (length (the (simple-array * (*)) array
)))
963 ((>= axis-number
(%array-rank array
))
964 (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
965 axis-number array
(%array-rank array
)))
967 (%array-dimension array axis-number
))))
969 (defun array-dimensions (array)
971 "Return a list whose elements are the dimensions of the array"
972 (declare (array array
))
973 (if (array-header-p array
)
974 (do ((results nil
(cons (array-dimension array index
) results
))
975 (index (1- (array-rank array
)) (1- index
)))
976 ((minusp index
) results
))
977 (list (array-dimension array
0))))
979 (defun array-total-size (array)
981 "Return the total number of elements in the Array."
982 (declare (array array
))
983 (if (array-header-p array
)
984 (%array-available-elements array
)
985 (length (the vector array
))))
987 (defun array-displacement (array)
989 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
990 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
991 (declare (type array array
))
992 (if (and (array-header-p array
) ; if unsimple and
993 (%array-displaced-p array
)) ; displaced
994 (values (%array-data-vector array
) (%array-displacement array
))
997 (defun adjustable-array-p (array)
999 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
1000 to the argument, this happens for complex arrays."
1001 (declare (array array
))
1002 ;; Note that this appears not to be a fundamental limitation.
1003 ;; non-vector SIMPLE-ARRAYs are in fact capable of being adjusted,
1004 ;; but in practice we test using ADJUSTABLE-ARRAY-P in ADJUST-ARRAY.
1005 ;; -- CSR, 2004-03-01.
1006 (not (typep array
'simple-array
)))
1008 ;;;; fill pointer frobbing stuff
1010 (declaim (inline array-has-fill-pointer-p
))
1011 (defun array-has-fill-pointer-p (array)
1013 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
1014 (declare (array array
))
1015 (and (array-header-p array
) (%array-fill-pointer-p array
)))
1017 (defun fill-pointer-error (vector &optional arg
)
1019 (aver (array-has-fill-pointer-p vector
))
1020 (let ((max (%array-available-elements vector
)))
1021 (error 'simple-type-error
1023 :expected-type
(list 'integer
0 max
)
1024 :format-control
"The new fill pointer, ~S, is larger than the length of the vector (~S.)"
1025 :format-arguments
(list arg max
))))
1027 (error 'simple-type-error
1029 :expected-type
'(and vector
(satisfies array-has-fill-pointer-p
))
1030 :format-control
"~S is not an array with a fill pointer."
1031 :format-arguments
(list vector
)))))
1033 (declaim (inline fill-pointer
))
1034 (defun fill-pointer (vector)
1036 "Return the FILL-POINTER of the given VECTOR."
1037 (declare (explicit-check))
1038 (if (array-has-fill-pointer-p vector
)
1039 (%array-fill-pointer vector
)
1040 (fill-pointer-error vector
)))
1042 (defun %set-fill-pointer
(vector new
)
1043 (declare (explicit-check))
1045 (fill-pointer-error vector x
)))
1046 (if (array-has-fill-pointer-p vector
)
1047 (if (> new
(%array-available-elements vector
))
1049 (setf (%array-fill-pointer vector
) new
))
1052 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
1053 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
1054 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
1055 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
1056 ;;; back to CMU CL).
1057 (defun vector-push (new-element array
)
1059 "Attempt to set the element of ARRAY designated by its fill pointer
1060 to NEW-ELEMENT, and increment the fill pointer by one. If the fill pointer is
1061 too large, NIL is returned, otherwise the index of the pushed element is
1063 (declare (explicit-check))
1064 (let ((fill-pointer (fill-pointer array
)))
1065 (declare (fixnum fill-pointer
))
1066 (cond ((= fill-pointer
(%array-available-elements array
))
1069 (locally (declare (optimize (safety 0)))
1070 (setf (aref array fill-pointer
) new-element
))
1071 (setf (%array-fill-pointer array
) (1+ fill-pointer
))
1074 (defun vector-push-extend (new-element vector
&optional min-extension
)
1075 (declare (type (or null fixnum
) min-extension
))
1076 (declare (explicit-check))
1077 (let ((fill-pointer (fill-pointer vector
)))
1078 (declare (fixnum fill-pointer
))
1079 (when (= fill-pointer
(%array-available-elements vector
))
1080 (let ((min-extension
1082 (let ((length (length vector
)))
1084 (- array-dimension-limit length
))))))
1085 (adjust-array vector
(+ fill-pointer
(max 1 min-extension
)))))
1086 ;; disable bounds checking
1087 (locally (declare (optimize (safety 0)))
1088 (setf (aref vector fill-pointer
) new-element
))
1089 (setf (%array-fill-pointer vector
) (1+ fill-pointer
))
1092 (defun vector-pop (array)
1094 "Decrease the fill pointer by 1 and return the element pointed to by the
1096 (declare (explicit-check))
1097 (let ((fill-pointer (fill-pointer array
)))
1098 (declare (fixnum fill-pointer
))
1099 (if (zerop fill-pointer
)
1100 (error "There is nothing left to pop.")
1101 ;; disable bounds checking (and any fixnum test)
1102 (locally (declare (optimize (safety 0)))
1104 (setf (%array-fill-pointer array
)
1105 (1- fill-pointer
)))))))
1110 (defun adjust-array (array dimensions
&key
1111 (element-type (array-element-type array
) element-type-p
)
1112 (initial-element nil initial-element-p
)
1113 (initial-contents nil initial-contents-p
)
1115 displaced-to displaced-index-offset
)
1117 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
1118 (when (invalid-array-p array
)
1119 (invalid-array-error array
))
1120 (binding* ((dimensions (ensure-list dimensions
))
1121 (array-rank (array-rank array
))
1123 (unless (= (length dimensions
) array-rank
)
1124 (error "The number of dimensions not equal to rank of array.")))
1125 ((initialize initial-data
)
1126 (validate-array-initargs initial-element-p initial-element
1127 initial-contents-p initial-contents
1129 (cond ((and element-type-p
1130 (not (subtypep element-type
(array-element-type array
))))
1131 ;; This is weird. Should check upgraded type against actual
1132 ;; array element type I think. See lp#1331299. CLHS says that
1133 ;; "consequences are unspecified" so current behavior isn't wrong.
1134 (error "The new element type, ~S, is incompatible with old type."
1136 ((and fill-pointer
(/= array-rank
1))
1137 (error "Only vectors can have fill pointers."))
1138 ((and fill-pointer
(not (array-has-fill-pointer-p array
)))
1139 ;; This case always struck me as odd. It seems like it might mean
1140 ;; that the user asks that the array gain a fill-pointer if it didn't
1141 ;; have one, yet CLHS is clear that the argument array must have a
1142 ;; fill-pointer or else signal a type-error.
1143 (fill-pointer-error array
)))
1144 (cond (initial-contents-p
1145 ;; array former contents replaced by INITIAL-CONTENTS
1146 (let* ((array-size (apply #'* dimensions
))
1147 (array-data (data-vector-from-inits
1148 dimensions array-size element-type nil nil
1149 initialize initial-data
)))
1150 (if (adjustable-array-p array
)
1151 (set-array-header array array-data array-size
1152 (get-new-fill-pointer array array-size
1154 0 dimensions nil nil
)
1155 (if (array-header-p array
)
1156 ;; simple multidimensional or single dimensional array
1157 (make-array dimensions
1158 :element-type element-type
1159 :initial-contents initial-contents
)
1162 ;; We already established that no INITIAL-CONTENTS was supplied.
1163 (unless (subtypep element-type
(array-element-type displaced-to
))
1164 ;; See lp#1331299 again. Require exact match on upgraded type?
1165 (error "can't displace an array of type ~S into another of ~
1167 element-type
(array-element-type displaced-to
)))
1168 (let ((displacement (or displaced-index-offset
0))
1169 (array-size (apply #'* dimensions
)))
1170 (declare (fixnum displacement array-size
))
1171 (if (< (the fixnum
(array-total-size displaced-to
))
1172 (the fixnum
(+ displacement array-size
)))
1173 (error "The :DISPLACED-TO array is too small."))
1174 (if (adjustable-array-p array
)
1175 ;; None of the original contents appear in adjusted array.
1176 (set-array-header array displaced-to array-size
1177 (get-new-fill-pointer array array-size
1179 displacement dimensions t nil
)
1180 ;; simple multidimensional or single dimensional array
1181 (make-array dimensions
1182 :element-type element-type
1183 :displaced-to displaced-to
1184 :displaced-index-offset
1185 displaced-index-offset
))))
1187 (let ((old-length (array-total-size array
))
1188 (new-length (car dimensions
))
1190 (declare (fixnum old-length new-length
))
1191 (with-array-data ((old-data array
) (old-start)
1192 (old-end old-length
))
1193 (cond ((or (and (array-header-p array
)
1194 (%array-displaced-p array
))
1195 (< old-length new-length
))
1197 (data-vector-from-inits
1198 dimensions new-length element-type
1199 (%other-pointer-widetag old-data
) nil
1200 initialize initial-data
))
1201 ;; Provide :END1 to avoid full call to LENGTH
1203 (replace new-data old-data
1205 :start2 old-start
:end2 old-end
))
1207 (shrink-vector old-data new-length
))))
1208 (if (adjustable-array-p array
)
1209 (set-array-header array new-data new-length
1210 (get-new-fill-pointer array new-length
1212 0 dimensions nil nil
)
1215 (let ((old-length (%array-available-elements array
))
1216 (new-length (apply #'* dimensions
)))
1217 (declare (fixnum old-length new-length
))
1218 (with-array-data ((old-data array
) (old-start)
1219 (old-end old-length
))
1220 (declare (ignore old-end
))
1221 (let ((new-data (if (or (and (array-header-p array
)
1222 (%array-displaced-p array
))
1223 (> new-length old-length
)
1224 (not (adjustable-array-p array
)))
1225 (data-vector-from-inits
1226 dimensions new-length
1228 (%other-pointer-widetag old-data
) nil
1229 (if initial-element-p
:initial-element
)
1232 (if (or (zerop old-length
) (zerop new-length
))
1233 (when initial-element-p
(fill new-data initial-element
))
1234 (zap-array-data old-data
(array-dimensions array
)
1236 new-data dimensions new-length
1237 element-type initial-element
1239 (if (adjustable-array-p array
)
1240 (set-array-header array new-data new-length
1241 nil
0 dimensions nil nil
)
1244 sb
!vm
:simple-array-widetag array-rank
)))
1245 (set-array-header new-array new-data new-length
1246 nil
0 dimensions nil t
))))))))))
1249 (defun get-new-fill-pointer (old-array new-array-size fill-pointer
)
1250 (cond ((not fill-pointer
)
1251 ;; "The consequences are unspecified if array is adjusted to a
1252 ;; size smaller than its fill pointer ..."
1253 (when (array-has-fill-pointer-p old-array
)
1254 (when (> (%array-fill-pointer old-array
) new-array-size
)
1255 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
1256 smaller than its fill pointer (~S)"
1257 old-array new-array-size
(fill-pointer old-array
)))
1258 (%array-fill-pointer old-array
)))
1259 ((numberp fill-pointer
)
1260 (when (> fill-pointer new-array-size
)
1261 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
1262 than the new length of the vector (~S)"
1263 fill-pointer new-array-size
))
1265 ((eq fill-pointer t
)
1268 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
1269 ;;; which must be less than or equal to its current length. This can
1270 ;;; be called on vectors without a fill pointer but it is extremely
1271 ;;; dangerous to do so: shrinking the size of an object (as viewed by
1272 ;;; the gc) makes bounds checking unreliable in the face of interrupts
1273 ;;; or multi-threading. Call it only on provably local vectors.
1274 (defun %shrink-vector
(vector new-length
)
1275 (declare (vector vector
))
1276 (unless (array-header-p vector
)
1277 (macrolet ((frob (name &rest things
)
1279 ((simple-array nil
(*)) (error 'nil-array-accessed-error
))
1280 ,@(mapcar (lambda (thing)
1281 (destructuring-bind (type-spec fill-value
)
1284 (fill (truly-the ,type-spec
,name
)
1286 :start new-length
))))
1288 ;; Set the 'tail' of the vector to the appropriate type of zero,
1289 ;; "because in some cases we'll scavenge larger areas in one go,
1290 ;; like groups of pages that had triggered the write barrier, or
1291 ;; the whole static space" according to jsnell.
1295 `((simple-array ,(sb!vm
:saetp-specifier saetp
) (*))
1296 ,(if (or (eq (sb!vm
:saetp-specifier saetp
) 'character
)
1298 (eq (sb!vm
:saetp-specifier saetp
) 'base-char
))
1299 *default-init-char-form
*
1300 (sb!vm
:saetp-initial-element-default saetp
))))
1302 #'sb
!vm
:saetp-specifier
1303 sb
!vm
:*specialized-array-element-type-properties
*)))))
1304 ;; Only arrays have fill-pointers, but vectors have their length
1305 ;; parameter in the same place.
1306 (setf (%array-fill-pointer vector
) new-length
)
1309 (defun shrink-vector (vector new-length
)
1310 (declare (vector vector
))
1312 ((eq (length vector
) new-length
)
1314 ((array-has-fill-pointer-p vector
)
1315 (setf (%array-fill-pointer vector
) new-length
)
1317 (t (subseq vector
0 new-length
))))
1319 ;;; BIG THREAD SAFETY NOTE
1321 ;;; ADJUST-ARRAY/SET-ARRAY-HEADER, and its callees are very
1322 ;;; thread unsafe. They are nonatomic, and can mess with parallel
1323 ;;; code using the same arrays.
1325 ;;; A likely seeming fix is an additional level of indirection:
1326 ;;; ARRAY-HEADER -> ARRAY-INFO -> ... where ARRAY-HEADER would
1327 ;;; hold nothing but the pointer to ARRAY-INFO, and ARRAY-INFO
1328 ;;; would hold everything ARRAY-HEADER now holds. This allows
1329 ;;; consing up a new ARRAY-INFO and replacing it atomically in
1330 ;;; the ARRAY-HEADER.
1332 ;;; %WALK-DISPLACED-ARRAY-BACKPOINTERS is an especially nasty
1333 ;;; one: not only is it needed extremely rarely, which makes
1334 ;;; any thread safety bugs involving it look like rare random
1335 ;;; corruption, but because it walks the chain *upwards*, which
1336 ;;; may violate user expectations.
1338 (defun %save-displaced-array-backpointer
(array data
)
1339 (flet ((purge (pointers)
1340 (remove-if (lambda (value)
1341 (or (not value
) (eq array value
)))
1343 :key
#'weak-pointer-value
)))
1344 ;; Add backpointer to the new data vector if it has a header.
1345 (when (array-header-p data
)
1346 (setf (%array-displaced-from data
)
1347 (cons (make-weak-pointer array
)
1348 (purge (%array-displaced-from data
)))))
1349 ;; Remove old backpointer, if any.
1350 (let ((old-data (%array-data-vector array
)))
1351 (when (and (neq data old-data
) (array-header-p old-data
))
1352 (setf (%array-displaced-from old-data
)
1353 (purge (%array-displaced-from old-data
)))))))
1355 (defun %walk-displaced-array-backpointers
(array new-length
)
1356 (dolist (p (%array-displaced-from array
))
1357 (let ((from (weak-pointer-value p
)))
1358 (when (and from
(eq array
(%array-data-vector from
)))
1359 (let ((requires (+ (%array-available-elements from
)
1360 (%array-displacement from
))))
1361 (unless (>= new-length requires
)
1362 ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
1364 ;; "If A is displaced to B, the consequences are unspecified if B is
1365 ;; adjusted in such a way that it no longer has enough elements to
1368 ;; since we're hanging on a weak pointer here, we can't signal an
1369 ;; error right now: the array that we're looking at might be
1370 ;; garbage. Instead, we set all dimensions to zero so that next
1371 ;; safe access to the displaced array will trap. Additionally, we
1372 ;; save the original dimensions, so we can signal a more
1373 ;; understandable error when the time comes.
1374 (%walk-displaced-array-backpointers from
0)
1375 (setf (%array-fill-pointer from
) 0
1376 (%array-available-elements from
) 0
1377 (%array-displaced-p from
) (array-dimensions array
))
1378 (dotimes (i (%array-rank from
))
1379 (setf (%array-dimension from i
) 0))))))))
1381 ;;; Fill in array header with the provided information, and return the array.
1382 (defun set-array-header (array data length fill-pointer displacement dimensions
1385 (setf (%array-displaced-from array
) nil
)
1386 (%walk-displaced-array-backpointers array length
))
1388 (%save-displaced-array-backpointer array data
))
1389 (setf (%array-data-vector array
) data
)
1390 (setf (%array-available-elements array
) length
)
1392 (setf (%array-fill-pointer array
) fill-pointer
)
1393 (setf (%array-fill-pointer-p array
) t
))
1395 (setf (%array-fill-pointer array
) length
)
1396 (setf (%array-fill-pointer-p array
) nil
)))
1397 (setf (%array-displacement array
) displacement
)
1398 (if (listp dimensions
)
1399 (dotimes (axis (array-rank array
))
1400 (declare (type index axis
))
1401 (setf (%array-dimension array axis
) (pop dimensions
)))
1402 (setf (%array-dimension array
0) dimensions
))
1403 (setf (%array-displaced-p array
) displacedp
)
1406 ;;; User visible extension
1407 (declaim (ftype (function (array) (values (simple-array * (*)) &optional
))
1408 array-storage-vector
))
1409 (defun array-storage-vector (array)
1411 "Returns the underlying storage vector of ARRAY, which must be a non-displaced array.
1413 In SBCL, if ARRAY is a of type \(SIMPLE-ARRAY * \(*)), it is its own storage
1414 vector. Multidimensional arrays, arrays with fill pointers, and adjustable
1415 arrays have an underlying storage vector with the same ARRAY-ELEMENT-TYPE as
1416 ARRAY, which this function returns.
1418 Important note: the underlying vector is an implementation detail. Even though
1419 this function exposes it, changes in the implementation may cause this
1420 function to be removed without further warning."
1421 ;; KLUDGE: Without TRULY-THE the system is not smart enough to figure out that
1422 ;; the return value is always of the known type.
1423 (truly-the (simple-array * (*))
1424 (if (array-header-p array
)
1425 (if (%array-displaced-p array
)
1426 (error "~S cannot be used with displaced arrays. Use ~S instead."
1427 'array-storage-vector
'array-displacement
)
1428 (%array-data-vector array
))
1432 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
1434 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
1435 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
1436 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
1437 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
1438 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
1439 element-type initial-element initial-element-p
)
1440 (declare (list old-dims new-dims
)
1441 (fixnum new-length
))
1442 ;; OLD-DIMS comes from array-dimensions, which returns a fresh list
1443 ;; at least in SBCL.
1444 ;; NEW-DIMS comes from the user.
1445 (setf old-dims
(nreverse old-dims
)
1446 new-dims
(reverse new-dims
))
1447 (cond ((eq old-data new-data
)
1448 ;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and
1449 ;; INITIAL-ELEMENT-P are used when OLD-DATA and NEW-DATA are
1450 ;; EQ; in this case, a temporary must be used and filled
1451 ;; appropriately. specified initial-element.
1452 (when initial-element-p
1453 ;; FIXME: transforming this TYPEP to someting a bit faster
1454 ;; would be a win...
1455 (unless (typep initial-element element-type
)
1456 (error "~S can't be used to initialize an array of type ~S."
1457 initial-element element-type
)))
1458 (let ((temp (if initial-element-p
1459 (make-array new-length
:initial-element initial-element
)
1460 (make-array new-length
))))
1461 (declare (simple-vector temp
))
1462 (zap-array-data-aux old-data old-dims offset temp new-dims
)
1463 (dotimes (i new-length
)
1464 (setf (aref new-data i
) (aref temp i
)))
1465 ;; Kill the temporary vector to prevent garbage retention.
1466 (%shrink-vector temp
0)))
1468 ;; When OLD-DATA and NEW-DATA are not EQ, NEW-DATA has
1469 ;; already been filled with any
1470 (zap-array-data-aux old-data old-dims offset new-data new-dims
))))
1472 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims
)
1473 (declare (fixnum offset
))
1474 (let ((limits (mapcar (lambda (x y
)
1475 (declare (fixnum x y
))
1476 (1- (the fixnum
(min x y
))))
1477 old-dims new-dims
)))
1478 (macrolet ((bump-index-list (index limits
)
1479 `(do ((subscripts ,index
(cdr subscripts
))
1480 (limits ,limits
(cdr limits
)))
1481 ((null subscripts
) :eof
)
1482 (cond ((< (the fixnum
(car subscripts
))
1483 (the fixnum
(car limits
)))
1485 (1+ (the fixnum
(car subscripts
))))
1487 (t (rplaca subscripts
0))))))
1488 (do ((index (make-list (length old-dims
) :initial-element
0)
1489 (bump-index-list index limits
)))
1491 (setf (aref new-data
(row-major-index-from-dims index new-dims
))
1493 (+ (the fixnum
(row-major-index-from-dims index old-dims
))
1496 ;;; Figure out the row-major-order index of an array reference from a
1497 ;;; list of subscripts and a list of dimensions. This is for internal
1498 ;;; calls only, and the subscripts and dim-list variables are assumed
1499 ;;; to be reversed from what the user supplied.
1500 (defun row-major-index-from-dims (rev-subscripts rev-dim-list
)
1501 (do ((rev-subscripts rev-subscripts
(cdr rev-subscripts
))
1502 (rev-dim-list rev-dim-list
(cdr rev-dim-list
))
1505 ((null rev-dim-list
) result
)
1506 (declare (fixnum chunk-size result
))
1507 (setq result
(+ result
1508 (the fixnum
(* (the fixnum
(car rev-subscripts
))
1510 (setq chunk-size
(* chunk-size
(the fixnum
(car rev-dim-list
))))))
1514 (defun bit-array-same-dimensions-p (array1 array2
)
1515 (declare (type (array bit
) array1 array2
))
1516 (and (= (array-rank array1
)
1517 (array-rank array2
))
1518 (dotimes (index (array-rank array1
) t
)
1519 (when (/= (array-dimension array1 index
)
1520 (array-dimension array2 index
))
1523 (defun pick-result-array (result-bit-array bit-array-1
)
1524 (case result-bit-array
1526 ((nil) (make-array (array-dimensions bit-array-1
)
1528 :initial-element
0))
1530 (unless (bit-array-same-dimensions-p bit-array-1
1532 (error "~S and ~S don't have the same dimensions."
1533 bit-array-1 result-bit-array
))
1536 (defmacro def-bit-array-op
(name function
)
1537 `(defun ,name
(bit-array-1 bit-array-2
&optional result-bit-array
)
1540 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1541 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1542 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1543 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1544 All the arrays must have the same rank and dimensions."
1545 (symbol-name function
))
1546 (declare (type (array bit
) bit-array-1 bit-array-2
)
1547 (type (or (array bit
) (member t nil
)) result-bit-array
))
1548 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2
)
1549 (error "~S and ~S don't have the same dimensions."
1550 bit-array-1 bit-array-2
))
1551 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1
)))
1552 (if (and (simple-bit-vector-p bit-array-1
)
1553 (simple-bit-vector-p bit-array-2
)
1554 (simple-bit-vector-p result-bit-array
))
1555 (locally (declare (optimize (speed 3) (safety 0)))
1556 (,name bit-array-1 bit-array-2 result-bit-array
))
1557 (with-array-data ((data1 bit-array-1
) (start1) (end1))
1558 (declare (ignore end1
))
1559 (with-array-data ((data2 bit-array-2
) (start2) (end2))
1560 (declare (ignore end2
))
1561 (with-array-data ((data3 result-bit-array
) (start3) (end3))
1562 (do ((index-1 start1
(1+ index-1
))
1563 (index-2 start2
(1+ index-2
))
1564 (index-3 start3
(1+ index-3
)))
1565 ((>= index-3 end3
) result-bit-array
)
1566 (declare (type index index-1 index-2 index-3
))
1567 (setf (sbit data3 index-3
)
1568 (logand (,function
(sbit data1 index-1
)
1569 (sbit data2 index-2
))
1572 (def-bit-array-op bit-and logand
)
1573 (def-bit-array-op bit-ior logior
)
1574 (def-bit-array-op bit-xor logxor
)
1575 (def-bit-array-op bit-eqv logeqv
)
1576 (def-bit-array-op bit-nand lognand
)
1577 (def-bit-array-op bit-nor lognor
)
1578 (def-bit-array-op bit-andc1 logandc1
)
1579 (def-bit-array-op bit-andc2 logandc2
)
1580 (def-bit-array-op bit-orc1 logorc1
)
1581 (def-bit-array-op bit-orc2 logorc2
)
1583 (defun bit-not (bit-array &optional result-bit-array
)
1585 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1586 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1587 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1588 created. Both arrays must have the same rank and dimensions."
1589 (declare (type (array bit
) bit-array
)
1590 (type (or (array bit
) (member t nil
)) result-bit-array
))
1591 (let ((result-bit-array (pick-result-array result-bit-array bit-array
)))
1592 (if (and (simple-bit-vector-p bit-array
)
1593 (simple-bit-vector-p result-bit-array
))
1594 (locally (declare (optimize (speed 3) (safety 0)))
1595 (bit-not bit-array result-bit-array
))
1596 (with-array-data ((src bit-array
) (src-start) (src-end))
1597 (declare (ignore src-end
))
1598 (with-array-data ((dst result-bit-array
) (dst-start) (dst-end))
1599 (do ((src-index src-start
(1+ src-index
))
1600 (dst-index dst-start
(1+ dst-index
)))
1601 ((>= dst-index dst-end
) result-bit-array
)
1602 (declare (type index src-index dst-index
))
1603 (setf (sbit dst dst-index
)
1604 (logxor (sbit src src-index
) 1))))))))
1606 ;;;; array type dispatching
1608 ;;; Given DISPATCH-FOO as the DISPATCH-NAME argument (unevaluated),
1609 ;;; defines the functions
1611 ;;; DISPATCH-FOO/SIMPLE-BASE-STRING
1612 ;;; DISPATCH-FOO/SIMPLE-CHARACTER-STRING
1613 ;;; DISPATCH-FOO/SIMPLE-ARRAY-SINGLE-FLOAT
1616 ;;; PARAMS are the function parameters in the definition of each
1617 ;;; specializer function. The array being specialized must be the
1618 ;;; first parameter in PARAMS. A type declaration for this parameter
1619 ;;; is automatically inserted into the body of each function.
1621 ;;; The dispatch table %%FOO-FUNS%% is defined and populated by these
1622 ;;; functions. The table is padded by the function
1623 ;;; HAIRY-FOO-DISPATCH-ERROR, also defined by DEFINE-ARRAY-DISPATCH.
1625 ;;; Finally, the DISPATCH-FOO macro is defined which does the actual
1626 ;;; dispatching when called. It expects arguments that match PARAMS.
1628 (defmacro define-array-dispatch
(dispatch-name params
&body body
)
1629 (let ((table-name (symbolicate "%%" dispatch-name
"-FUNS%%"))
1630 (error-name (symbolicate "HAIRY-" dispatch-name
"-ERROR")))
1632 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
1633 (defun ,error-name
(&rest args
)
1636 :expected-type
'(simple-array * (*)))))
1637 (!defglobal
,table-name
(make-array ,(1+ sb
!vm
:widetag-mask
)
1638 :initial-element
#',error-name
))
1639 ,@(loop for info across sb
!vm
:*specialized-array-element-type-properties
*
1640 for typecode
= (sb!vm
:saetp-typecode info
)
1641 for specifier
= (sb!vm
:saetp-specifier info
)
1642 for primitive-type-name
= (sb!vm
:saetp-primitive-type-name info
)
1643 collect
(let ((fun-name (symbolicate (string dispatch-name
)
1644 "/" primitive-type-name
)))
1646 (defun ,fun-name
,params
1647 (declare (type (simple-array ,specifier
(*))
1650 (setf (svref ,table-name
,typecode
) #',fun-name
))))
1651 (defmacro ,dispatch-name
(&rest args
)
1652 (check-type (first args
) symbol
)
1653 (let ((tag (gensym "TAG")))
1657 (when (sb!vm
::%other-pointer-p
,(first args
))
1658 (setf ,tag
(%other-pointer-widetag
,(first args
))))
1659 (svref ,',table-name
,tag
)))