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-diplaced-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 %with-array-data
/fp
(array start end
)
51 (%with-array-data-macro array start end
:check-bounds t
:check-fill-pointer t
))
53 (defun %with-array-data
(array start end
)
54 (%with-array-data-macro array start end
:check-bounds t
:check-fill-pointer nil
))
56 (defun %data-vector-and-index
(array index
)
57 (if (array-header-p array
)
58 (multiple-value-bind (vector index
)
59 (%with-array-data array index nil
)
60 (values vector index
))
61 (values array index
)))
64 (defun %integer-vector-widetag-and-n-bits
(signed high
)
66 #.
(let ((map (make-array (1+ sb
!vm
:n-word-bits
))))
67 (loop for saetp across
68 (reverse sb
!vm
:*specialized-array-element-type-properties
*)
69 for ctype
= (sb!vm
:saetp-ctype saetp
)
70 when
(and (numeric-type-p ctype
)
71 (eq (numeric-type-class ctype
) 'integer
)
72 (zerop (numeric-type-low ctype
)))
73 do
(fill map
(cons (sb!vm
:saetp-typecode saetp
)
74 (sb!vm
:saetp-n-bits saetp
))
75 :end
(1+ (integer-length (numeric-type-high ctype
)))))
78 #.
(let ((map (make-array (1+ sb
!vm
:n-word-bits
))))
79 (loop for saetp across
80 (reverse sb
!vm
:*specialized-array-element-type-properties
*)
81 for ctype
= (sb!vm
:saetp-ctype saetp
)
82 when
(and (numeric-type-p ctype
)
83 (eq (numeric-type-class ctype
) 'integer
)
84 (minusp (numeric-type-low ctype
)))
85 do
(fill map
(cons (sb!vm
:saetp-typecode saetp
)
86 (sb!vm
:saetp-n-bits saetp
))
87 :end
(+ (integer-length (numeric-type-high ctype
)) 2)))
89 (cond ((> high sb
!vm
:n-word-bits
)
90 (values #.sb
!vm
:simple-vector-widetag
#.sb
!vm
:n-word-bits
))
92 (let ((x (aref signed-table high
)))
93 (values (car x
) (cdr x
))))
95 (let ((x (aref unsigned-table high
)))
96 (values (car x
) (cdr x
)))))))
98 ;;; This is a bit complicated, but calling subtypep over all
99 ;;; specialized types is exceedingly slow
100 (defun %vector-widetag-and-n-bits
(type)
101 (macrolet ((with-parameters ((arg-type &key intervals
)
102 (&rest args
) &body body
)
103 (let ((type-sym (gensym)))
104 `(let (,@(loop for arg in args
106 (declare (ignorable ,@args
))
108 (let ((,type-sym
(cdr type
)))
110 ,@(loop for arg in args
112 `(cond ((consp ,type-sym
)
113 (let ((value (pop ,type-sym
)))
114 (if (or (eq value
'*)
115 (typep value
',arg-type
)
131 (let ((value (symbol-value widetag
)))
135 sb
!vm
:*specialized-array-element-type-properties
*
136 :key
#'sb
!vm
:saetp-typecode
))))))
138 (error "Invalid type specifier: ~s" type
))
139 (integer-interval-widetag (low high
)
141 (%integer-vector-widetag-and-n-bits
143 (1+ (max (integer-length low
) (integer-length high
))))
144 (%integer-vector-widetag-and-n-bits
146 (max (integer-length low
) (integer-length high
))))))
147 (let* ((consp (consp type
))
155 (result sb
!vm
:simple-vector-widetag
))
156 ((base-char standard-char
#!-sb-unicode character
)
159 (result sb
!vm
:simple-base-string-widetag
))
161 ((character extended-char
)
164 (result sb
!vm
:simple-character-string-widetag
))
168 (result sb
!vm
:simple-bit-vector-widetag
))
172 (result sb
!vm
:simple-array-fixnum-widetag
))
174 (with-parameters ((integer 1)) (high)
176 (result sb
!vm
:simple-vector-widetag
)
177 (%integer-vector-widetag-and-n-bits nil high
))))
179 (with-parameters ((integer 1)) (high)
181 (result sb
!vm
:simple-vector-widetag
)
182 (%integer-vector-widetag-and-n-bits t high
))))
184 (with-parameters (double-float :intervals t
) (low high
)
185 (if (and (not (eq low
'*))
187 (if (or (consp low
) (consp high
))
188 (>= (type-bound-number low
) (type-bound-number high
))
190 (result sb
!vm
:simple-array-nil-widetag
)
191 (result sb
!vm
:simple-array-double-float-widetag
))))
193 (with-parameters (single-float :intervals t
) (low high
)
194 (if (and (not (eq low
'*))
196 (if (or (consp low
) (consp high
))
197 (>= (type-bound-number low
) (type-bound-number high
))
199 (result sb
!vm
:simple-array-nil-widetag
)
200 (result sb
!vm
:simple-array-single-float-widetag
))))
202 (if (and (consp type
)
205 (typep (cadr type
) '(integer 1)))
206 (%integer-vector-widetag-and-n-bits
207 nil
(integer-length (1- (cadr type
))))
211 (with-parameters (long-float :intervals t
) (low high
)
212 (if (and (not (eq low
'*))
214 (if (or (consp low
) (consp high
))
215 (>= (type-bound-number low
) (type-bound-number high
))
217 (result sb
!vm
:simple-array-nil-widetag
)
218 (result sb
!vm
:simple-array-long-float-widetag
))))
220 (with-parameters (integer :intervals t
) (low high
)
221 (let ((low (if (consp low
)
224 (high (if (consp high
)
227 (cond ((or (eq high
'*)
229 (result sb
!vm
:simple-vector-widetag
))
231 (result sb
!vm
:simple-array-nil-widetag
))
233 (integer-interval-widetag low high
))))))
235 (with-parameters (t) (subtype)
237 (result sb
!vm
:simple-vector-widetag
)
238 (let ((ctype (specifier-type type
)))
239 (cond ((eq ctype
*empty-type
*)
240 (result sb
!vm
:simple-array-nil-widetag
))
241 ((union-type-p ctype
)
242 (cond ((csubtypep ctype
(specifier-type '(complex double-float
)))
244 sb
!vm
:simple-array-complex-double-float-widetag
))
245 ((csubtypep ctype
(specifier-type '(complex single-float
)))
247 sb
!vm
:simple-array-complex-single-float-widetag
))
249 ((csubtypep ctype
(specifier-type '(complex long-float
)))
251 sb
!vm
:simple-array-complex-long-float-widetag
))
253 (result sb
!vm
:simple-vector-widetag
))))
255 (case (numeric-type-format ctype
)
258 sb
!vm
:simple-array-complex-double-float-widetag
))
261 sb
!vm
:simple-array-complex-single-float-widetag
))
265 sb
!vm
:simple-array-complex-long-float-widetag
))
267 (result sb
!vm
:simple-vector-widetag
)))))))))
269 (result sb
!vm
:simple-array-nil-widetag
))
273 (handler-case (specifier-type type
)
274 (parse-unknown-type ()
275 (return (result sb
!vm
:simple-vector-widetag
))))))
277 (union-type ; FIXME: forward ref
278 (let ((types (union-type-types ctype
)))
279 (cond ((not (every #'numeric-type-p types
))
280 (result sb
!vm
:simple-vector-widetag
))
281 ((csubtypep ctype
(specifier-type 'integer
))
282 (integer-interval-widetag
283 (reduce #'min types
:key
#'numeric-type-low
)
284 (reduce #'max types
:key
#'numeric-type-high
)))
285 ((csubtypep ctype
(specifier-type 'double-float
))
286 (result sb
!vm
:simple-array-double-float-widetag
))
287 ((csubtypep ctype
(specifier-type 'single-float
))
288 (result sb
!vm
:simple-array-single-float-widetag
))
290 ((csubtypep ctype
(specifier-type 'long-float
))
291 (result sb
!vm
:simple-array-long-float-widetag
))
293 (result sb
!vm
:simple-vector-widetag
)))))
294 (character-set-type ; FIXME: forward ref
295 #!-sb-unicode
(result sb
!vm
:simple-base-string-widetag
)
297 (if (loop for
(start . end
)
298 in
(character-set-type-pairs ctype
)
299 always
(and (< start base-char-code-limit
)
300 (< end base-char-code-limit
)))
301 (result sb
!vm
:simple-base-string-widetag
)
302 (result sb
!vm
:simple-character-string-widetag
)))
304 (let ((expansion (type-specifier ctype
)))
305 (if (equal expansion type
)
306 (result sb
!vm
:simple-vector-widetag
)
307 (%vector-widetag-and-n-bits expansion
)))))))))))))
309 (defun %complex-vector-widetag
(widetag)
310 (macrolet ((make-case ()
312 ,@(loop for saetp across sb
!vm
:*specialized-array-element-type-properties
*
313 for complex
= (sb!vm
:saetp-complex-typecode saetp
)
315 collect
(list (sb!vm
:saetp-typecode saetp
) complex
))
317 #.sb
!vm
:complex-vector-widetag
))))
320 (defglobal %%simple-array-n-bits%%
(make-array (1+ sb
!vm
:widetag-mask
)))
321 #.
(loop for info across sb
!vm
:*specialized-array-element-type-properties
*
322 collect
`(setf (aref %%simple-array-n-bits%%
,(sb!vm
:saetp-typecode info
))
323 ,(sb!vm
:saetp-n-bits info
)) into forms
324 finally
(return `(progn ,@forms
)))
326 (declaim (type (simple-vector #.
(1+ sb
!vm
:widetag-mask
)) %%simple-array-n-bits%%
))
328 (defun allocate-vector-with-widetag (widetag length
&optional n-bits
)
329 (declare (type (unsigned-byte 8) widetag
)
331 (let ((n-bits (or n-bits
(aref %%simple-array-n-bits%% widetag
))))
332 (declare (type (integer 0 256) n-bits
))
333 (allocate-vector widetag length
335 (* (if (or (= widetag sb
!vm
:simple-base-string-widetag
)
338 sb
!vm
:simple-character-string-widetag
))
342 sb
!vm
:n-word-bits
))))
344 (defun array-underlying-widetag (array)
345 (macrolet ((make-case ()
347 ,@(loop for saetp across sb
!vm
:*specialized-array-element-type-properties
*
348 for complex
= (sb!vm
:saetp-complex-typecode saetp
)
350 collect
(list complex
(sb!vm
:saetp-typecode saetp
)))
351 ((,sb
!vm
:simple-array-widetag
352 ,sb
!vm
:complex-vector-widetag
353 ,sb
!vm
:complex-array-widetag
)
354 (with-array-data ((array array
) (start) (end))
355 (declare (ignore start end
))
356 (%other-pointer-widetag array
)))
359 (let ((widetag (%other-pointer-widetag array
)))
362 ;; Complain in various ways about wrong :INITIAL-foo arguments,
363 ;; returning the two initialization arguments needed for DATA-VECTOR-FROM-INITS.
364 (defun validate-array-initargs (element-p element contents-p contents displaced
)
365 (cond ((and displaced
(or element-p contents-p
))
366 (if (and element-p contents-p
)
367 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
368 may be specified with the :DISPLACED-TO option")
369 (error "~S may not be specified with the :DISPLACED-TO option"
370 (if element-p
:initial-element
:initial-contents
))))
371 ((and element-p contents-p
)
372 (error "Can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
373 (element-p (values :initial-element element
))
374 (contents-p (values :initial-contents contents
))
375 (t (values nil nil
))))
377 ;;; Widetag is the widetag of the underlying vector,
378 ;;; it'll be the same as the resulting array widetag only for simple vectors
379 (defun %make-array
(dimensions widetag n-bits
382 (initial-element nil initial-element-p
)
383 (initial-contents nil initial-contents-p
)
384 adjustable fill-pointer
385 displaced-to displaced-index-offset
)
386 (declare (ignore element-type
))
387 (binding* (((array-rank dimension-0
)
388 (if (listp dimensions
)
389 (values (length dimensions
)
390 (if dimensions
(car dimensions
) 1))
391 (values 1 dimensions
)))
392 ((initialize initial-data
)
393 (validate-array-initargs initial-element-p initial-element
394 initial-contents-p initial-contents
396 (simple (and (null fill-pointer
)
398 (null displaced-to
))))
399 (declare (type array-rank array-rank
))
400 (declare (type index dimension-0
))
401 (cond ((and displaced-index-offset
(null displaced-to
))
402 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
403 ((and simple
(= array-rank
1))
404 (let ((vector ; a (SIMPLE-ARRAY * (*))
405 (allocate-vector-with-widetag widetag dimension-0 n-bits
)))
406 ;; presence of at most one :INITIAL-thing keyword was ensured above
407 (cond (initial-element-p
408 (fill vector initial-element
))
410 (let ((content-length (length initial-contents
)))
411 (unless (= dimension-0 content-length
)
412 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
413 the vector length is ~W."
414 content-length dimension-0
)))
415 (replace vector initial-contents
)))
417 ((and (arrayp displaced-to
)
418 (/= (array-underlying-widetag displaced-to
) widetag
))
419 (error "Array element type of :DISPLACED-TO array does not match specified element type"))
421 ;; it's non-simple or multidimensional, or both.
423 (unless (= array-rank
1)
424 (error "Only vectors can have fill pointers."))
425 (when (and (integerp fill-pointer
) (> fill-pointer dimension-0
))
426 ;; FIXME: should be TYPE-ERROR?
427 (error "invalid fill-pointer ~W" fill-pointer
)))
429 (if (consp dimensions
)
430 (the index
(reduce (lambda (a b
) (* a
(the index b
)))
432 ;; () is considered to have dimension-0 = 1.
433 ;; It avoids the REDUCE lambda being called with no args.
435 (data (or displaced-to
436 (data-vector-from-inits
437 dimensions total-size nil widetag n-bits
438 initialize initial-data
)))
439 (array (make-array-header
440 (cond ((= array-rank
1)
441 (%complex-vector-widetag widetag
))
442 (simple sb
!vm
:simple-array-widetag
)
443 (t sb
!vm
:complex-array-widetag
))
446 (setf (%array-fill-pointer-p array
) t
447 (%array-fill-pointer array
)
448 (if (eq fill-pointer t
) dimension-0 fill-pointer
))
449 (setf (%array-fill-pointer-p array
) nil
450 (%array-fill-pointer array
) total-size
))
451 (setf (%array-available-elements array
) total-size
)
452 ;; Terrible name for this slot - we displace to the
453 ;; target array's header, if any, not the "ultimate"
454 ;; vector in the chain of displacements.
455 (setf (%array-data-vector array
) data
)
456 (setf (%array-displaced-from array
) nil
)
458 (let ((offset (or displaced-index-offset
0)))
459 (when (> (+ offset total-size
)
460 (array-total-size displaced-to
))
461 (error "~S doesn't have enough elements." displaced-to
))
462 (setf (%array-displacement array
) offset
)
463 (setf (%array-displaced-p array
) t
)
464 (%save-displaced-array-backpointer array data
)))
466 (setf (%array-displaced-p array
) nil
)))
467 (if (listp dimensions
)
468 (let ((dims dimensions
)) ; avoid "prevents use of assertion"
469 (dotimes (axis array-rank
)
470 (setf (%array-dimension array axis
) (pop dims
))))
471 (setf (%array-dimension array
0) dimension-0
))
474 (defun make-array (dimensions &rest args
475 &key
(element-type t
)
476 initial-element initial-contents
480 displaced-index-offset
)
481 (declare (ignore initial-element
482 initial-contents adjustable
483 fill-pointer displaced-to displaced-index-offset
))
484 (multiple-value-bind (widetag n-bits
) (%vector-widetag-and-n-bits element-type
)
485 (apply #'%make-array dimensions widetag n-bits args
)))
487 (defun make-static-vector (length &key
488 (element-type '(unsigned-byte 8))
489 (initial-contents nil initial-contents-p
)
490 (initial-element nil initial-element-p
))
492 "Allocate vector of LENGTH elements in static space. Only allocation
493 of specialized arrays is supported."
494 ;; STEP 1: check inputs fully
496 ;; This way of doing explicit checks before the vector is allocated
497 ;; is expensive, but probably worth the trouble as once we've allocated
498 ;; the vector we have no way to get rid of it anymore...
499 (when (eq t
(upgraded-array-element-type element-type
))
500 (error "Static arrays of type ~S not supported."
502 (validate-array-initargs initial-element-p initial-element
503 initial-contents-p initial-contents nil
) ; for effect
504 (when initial-contents-p
505 (unless (= length
(length initial-contents
))
506 (error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
507 vector length is ~W."
508 (length initial-contents
)
510 (unless (every (lambda (x) (typep x element-type
)) initial-contents
)
511 (error ":INITIAL-CONTENTS contains elements not of type ~S."
513 (when initial-element-p
514 (unless (typep initial-element element-type
)
515 (error ":INITIAL-ELEMENT ~S is not of type ~S."
516 initial-element element-type
)))
519 ;; Allocate and possibly initialize the vector.
520 (multiple-value-bind (type n-bits
)
521 (sb!impl
::%vector-widetag-and-n-bits element-type
)
523 (allocate-static-vector type length
524 (ceiling (* length n-bits
)
525 sb
!vm
:n-word-bits
))))
526 (cond (initial-element-p
527 (fill vector initial-element
))
529 (replace vector initial-contents
))
533 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
534 ;;; specified array characteristics. Dimensions is only used to pass
535 ;;; to FILL-DATA-VECTOR for error checking on the structure of
536 ;;; initial-contents.
537 (defun data-vector-from-inits (dimensions total-size
538 element-type widetag n-bits
539 initialize initial-data
)
540 ;; FIXME: element-type can be NIL when widetag is non-nil,
541 ;; and FILL will check the type, although the error will be not as nice.
542 ;; (cond (typep initial-element element-type)
543 ;; (error "~S cannot be used to initialize an array of type ~S."
544 ;; initial-element element-type))
545 (let ((data (if widetag
546 (allocate-vector-with-widetag widetag total-size n-bits
)
547 (make-array total-size
:element-type element-type
))))
550 (fill (the vector data
) initial-data
))
552 ;; DIMENSIONS can be supplied as a list or integer now
553 (dx-let ((list-of-dims (list dimensions
))) ; ok if already a list
554 (fill-data-vector data
555 (if (listp dimensions
) dimensions list-of-dims
)
560 (defun vector (&rest objects
)
562 "Construct a SIMPLE-VECTOR from the given objects."
563 (let ((v (make-array (length objects
))))
564 (do-rest-arg ((x i
) objects
0 v
)
565 (setf (aref v i
) x
))))
568 ;;;; accessor/setter functions
570 ;;; Dispatch to an optimized routine the data vector accessors for
571 ;;; each different specialized vector type. Do dispatching by looking
572 ;;; up the widetag in the array rather than with the typecases, which
573 ;;; as of 1.0.5 compiles to a naive sequence of linear TYPEPs. Also
574 ;;; provide separate versions where bounds checking has been moved
575 ;;; from the callee to the caller, since it's much cheaper to do once
576 ;;; the type information is available. Finally, for each of these
577 ;;; routines also provide a slow path, taken for arrays that are not
578 ;;; vectors or not simple.
579 (macrolet ((def (name table-name
)
581 (defglobal ,table-name
(make-array ,(1+ sb
!vm
:widetag-mask
)))
582 (defmacro ,name
(array-var)
585 (when (sb!vm
::%other-pointer-p
,array-var
)
586 (setf tag
(%other-pointer-widetag
,array-var
)))
587 (svref ,',table-name tag
)))))))
588 (def !find-data-vector-setter %%data-vector-setters%%
)
589 (def !find-data-vector-setter
/check-bounds %%data-vector-setters
/check-bounds%%
)
590 ;; Used by DO-VECTOR-DATA -- which in turn appears in DOSEQUENCE expansion,
591 ;; meaning we can have post-build dependences on this.
592 (def %find-data-vector-reffer %%data-vector-reffers%%
)
593 (def !find-data-vector-reffer
/check-bounds %%data-vector-reffers
/check-bounds%%
))
595 ;;; Like DOVECTOR, but more magical -- can't use this on host.
596 (defmacro do-vector-data
((elt vector
&optional result
) &body body
)
597 (multiple-value-bind (forms decls
) (parse-body body
:doc-string-allowed nil
)
598 (with-unique-names (index vec start end ref
)
599 `(with-array-data ((,vec
,vector
)
602 :check-fill-pointer t
)
603 (let ((,ref
(%find-data-vector-reffer
,vec
)))
604 (do ((,index
,start
(1+ ,index
)))
607 ,@(filter-dolist-declarations decls
)
610 (let ((,elt
(funcall ,ref
,vec
,index
)))
612 (tagbody ,@forms
))))))))
614 (macrolet ((%ref
(accessor-getter extra-params
)
615 `(funcall (,accessor-getter array
) array index
,@extra-params
))
616 (define (accessor-name slow-accessor-name accessor-getter
617 extra-params check-bounds
)
619 (defun ,accessor-name
(array index
,@extra-params
)
620 (declare (optimize speed
621 ;; (SAFETY 0) is ok. All calls to
622 ;; these functions are generated by
623 ;; the compiler, so argument count
624 ;; checking isn't needed. Type checking
625 ;; is done implicitly via the widetag
628 (%ref
,accessor-getter
,extra-params
))
629 (defun ,slow-accessor-name
(array index
,@extra-params
)
630 (declare (optimize speed
(safety 0)))
631 (if (not (%array-displaced-p array
))
632 ;; The reasonably quick path of non-displaced complex
634 (let ((array (%array-data-vector array
)))
635 (%ref
,accessor-getter
,extra-params
))
636 ;; The real slow path.
640 (declare (optimize (speed 1) (safety 1)))
641 (,@check-bounds index
)))
644 (declare (ignore end
))
645 (,accessor-name vector index
,@extra-params
)))))))
646 (define hairy-data-vector-ref slow-hairy-data-vector-ref
647 %find-data-vector-reffer
649 (define hairy-data-vector-set slow-hairy-data-vector-set
650 !find-data-vector-setter
652 (define hairy-data-vector-ref
/check-bounds
653 slow-hairy-data-vector-ref
/check-bounds
654 !find-data-vector-reffer
/check-bounds
655 nil
(%check-bound array
(array-dimension array
0)))
656 (define hairy-data-vector-set
/check-bounds
657 slow-hairy-data-vector-set
/check-bounds
658 !find-data-vector-setter
/check-bounds
659 (new-value) (%check-bound array
(array-dimension array
0))))
661 (defun hairy-ref-error (array index
&optional new-value
)
662 (declare (ignore index new-value
))
665 :expected-type
'vector
))
667 (macrolet ((define-reffer (saetp check-form
)
668 (let* ((type (sb!vm
:saetp-specifier saetp
))
669 (atype `(simple-array ,type
(*))))
670 `(named-lambda (optimized-data-vector-ref ,type
) (vector index
)
671 (declare (optimize speed
(safety 0))
672 ;; Obviously these all coerce raw words to lispobjs
673 ;; so don't keep spewing notes about it.
674 (muffle-conditions compiler-note
)
677 `(data-vector-ref (the ,atype vector
)
679 (declare (optimize (safety 1)))
681 (,@check-form index
))))
682 `(data-nil-vector-ref (the ,atype vector
) index
)))))
683 (define-setter (saetp check-form
)
684 (let* ((type (sb!vm
:saetp-specifier saetp
))
685 (atype `(simple-array ,type
(*))))
686 `(named-lambda (optimized-data-vector-set ,type
) (vector index new-value
)
687 (declare (optimize speed
(safety 0)))
688 ;; Impossibly setting an elt of an (ARRAY NIL)
689 ;; returns no value. And nobody cares.
690 (declare (muffle-conditions compiler-note
))
691 (data-vector-set (the ,atype vector
)
693 (declare (optimize (safety 1)))
695 (,@check-form index
)))
697 ;; SPEED 1 needed to avoid the compiler
698 ;; from downgrading the type check to
700 (declare (optimize (speed 1)
702 (the ,type new-value
)))
703 ;; For specialized arrays, the return from
704 ;; data-vector-set would have to be reboxed to be a
705 ;; (Lisp) return value; instead, we use the
706 ;; already-boxed value as the return.
708 (define-reffers (symbol deffer check-form slow-path
)
710 ;; FIXME/KLUDGE: can't just FILL here, because genesis doesn't
711 ;; preserve the binding, so re-initiaize as NS doesn't have
712 ;; the energy to figure out to change that right now.
713 (setf ,symbol
(make-array (1+ sb
!vm
::widetag-mask
)
714 :initial-element
#'hairy-ref-error
))
715 ,@(loop for widetag in
'(sb!vm
:complex-vector-widetag
716 sb
!vm
:complex-vector-nil-widetag
717 sb
!vm
:complex-bit-vector-widetag
718 #!+sb-unicode sb
!vm
:complex-character-string-widetag
719 sb
!vm
:complex-base-string-widetag
720 sb
!vm
:simple-array-widetag
721 sb
!vm
:complex-array-widetag
)
722 collect
`(setf (svref ,symbol
,widetag
) ,slow-path
))
723 ,@(loop for saetp across sb
!vm
:*specialized-array-element-type-properties
*
724 for widetag
= (sb!vm
:saetp-typecode saetp
)
725 collect
`(setf (svref ,symbol
,widetag
)
726 (,deffer
,saetp
,check-form
))))))
727 (defun !hairy-data-vector-reffer-init
()
728 (define-reffers %%data-vector-reffers%% define-reffer
730 #'slow-hairy-data-vector-ref
)
731 (define-reffers %%data-vector-setters%% define-setter
733 #'slow-hairy-data-vector-set
)
734 (define-reffers %%data-vector-reffers
/check-bounds%% define-reffer
735 (%check-bound vector
(length vector
))
736 #'slow-hairy-data-vector-ref
/check-bounds
)
737 (define-reffers %%data-vector-setters
/check-bounds%% define-setter
738 (%check-bound vector
(length vector
))
739 #'slow-hairy-data-vector-set
/check-bounds
)))
741 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
742 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
743 ;;; definition is needed for the compiler to use in constant folding.)
744 (defun data-vector-ref (array index
)
745 (hairy-data-vector-ref array index
))
747 (defun data-vector-ref-with-offset (array index offset
)
748 (hairy-data-vector-ref array
(+ index offset
)))
750 (defun invalid-array-p (array)
751 (and (array-header-p array
)
752 (consp (%array-displaced-p array
))))
754 (declaim (ftype (function (array) nil
) invalid-array-error
))
755 (defun invalid-array-error (array)
756 (aver (array-header-p array
))
757 ;; Array invalidation stashes the original dimensions here...
758 (let ((dims (%array-displaced-p array
))
759 (et (array-element-type array
)))
760 (error 'invalid-array-error
765 `(vector ,et
,@dims
)))))
767 (declaim (ftype (function (array integer integer
&optional t
) nil
)
768 invalid-array-index-error
))
769 (defun invalid-array-index-error (array index bound
&optional axis
)
770 (if (invalid-array-p array
)
771 (invalid-array-error array
)
772 (error 'invalid-array-index-error
776 :expected-type
`(integer 0 (,bound
)))))
778 ;;; SUBSCRIPTS has a dynamic-extent list structure and is destroyed
779 (defun %array-row-major-index
(array &rest subscripts
)
780 (declare (truly-dynamic-extent subscripts
)
782 (let ((length (length subscripts
)))
783 (cond ((array-header-p array
)
784 (let ((rank (%array-rank array
)))
785 (unless (= rank length
)
786 (error "wrong number of subscripts, ~W, for array of rank ~W."
788 (do ((axis (1- rank
) (1- axis
))
791 ((minusp axis
) result
)
792 (declare (fixnum axis chunk-size result
))
793 (let ((index (fast-&rest-nth axis subscripts
))
794 (dim (%array-dimension array axis
)))
795 (unless (and (fixnump index
) (< -
1 index dim
))
796 (invalid-array-index-error array index dim axis
))
800 (truly-the fixnum
(* chunk-size index
))))
801 chunk-size
(truly-the fixnum
(* chunk-size dim
)))))))
803 (error "Wrong number of subscripts, ~W, for array of rank 1."
806 (let ((index (fast-&rest-nth
0 subscripts
))
807 (length (length (the (simple-array * (*)) array
))))
808 (unless (and (fixnump index
) (< -
1 index length
))
809 (invalid-array-index-error array index length
))
812 (defun array-in-bounds-p (array &rest subscripts
)
814 "Return T if the SUBSCRIPTS are in bounds for the ARRAY, NIL otherwise."
815 (declare (truly-dynamic-extent subscripts
))
816 (let ((length (length subscripts
)))
817 (cond ((array-header-p array
)
818 (let ((rank (%array-rank array
)))
819 (unless (= rank length
)
820 (error "Wrong number of subscripts, ~W, for array of rank ~W."
822 (loop for i below length
823 for s
= (fast-&rest-nth i subscripts
)
824 always
(and (typep s
'(and fixnum unsigned-byte
))
825 (< s
(%array-dimension array i
))))))
827 (error "Wrong number of subscripts, ~W, for array of rank 1."
830 (let ((subscript (fast-&rest-nth
0 subscripts
)))
831 (and (typep subscript
'(and fixnum unsigned-byte
))
833 (length (truly-the (simple-array * (*)) array
)))))))))
835 (defun array-row-major-index (array &rest subscripts
)
836 (declare (truly-dynamic-extent subscripts
))
837 (apply #'%array-row-major-index array subscripts
))
839 (defun aref (array &rest subscripts
)
841 "Return the element of the ARRAY specified by the SUBSCRIPTS."
842 (declare (truly-dynamic-extent subscripts
))
843 (row-major-aref array
(apply #'%array-row-major-index array subscripts
)))
845 ;;; (setf aref/bit/sbit) are implemented using setf-functions,
846 ;;; because they have to work with (setf (apply #'aref array subscripts))
847 ;;; All other setfs can be done using setf-functions too, but I
848 ;;; haven't found technical advantages or disadvantages for either
850 (defun (setf aref
) (new-value array
&rest subscripts
)
851 (declare (truly-dynamic-extent subscripts
)
853 (setf (row-major-aref array
(apply #'%array-row-major-index array subscripts
))
856 (defun row-major-aref (array index
)
858 "Return the element of array corresponding to the row-major index. This is
860 (declare (optimize (safety 1)))
861 (row-major-aref array index
))
863 (defun %set-row-major-aref
(array index new-value
)
864 (declare (optimize (safety 1)))
865 (setf (row-major-aref array index
) new-value
))
867 (defun svref (simple-vector index
)
869 "Return the INDEXth element of the given Simple-Vector."
870 (declare (optimize (safety 1)))
871 (aref simple-vector index
))
873 (defun %svset
(simple-vector index new
)
874 (declare (optimize (safety 1)))
875 (setf (aref simple-vector index
) new
))
877 (defun bit (bit-array &rest subscripts
)
879 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
880 (declare (type (array bit
) bit-array
)
881 (truly-dynamic-extent subscripts
)
882 (optimize (safety 1)))
883 (row-major-aref bit-array
(apply #'%array-row-major-index bit-array subscripts
)))
885 (defun (setf bit
) (new-value bit-array
&rest subscripts
)
886 (declare (type (array bit
) bit-array
)
888 (truly-dynamic-extent subscripts
)
889 (optimize (safety 1)))
890 (setf (row-major-aref bit-array
891 (apply #'%array-row-major-index bit-array subscripts
))
894 (defun sbit (simple-bit-array &rest subscripts
)
896 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
897 (declare (type (simple-array bit
) simple-bit-array
)
898 (truly-dynamic-extent subscripts
)
899 (optimize (safety 1)))
900 (row-major-aref simple-bit-array
901 (apply #'%array-row-major-index simple-bit-array subscripts
)))
903 (defun (setf sbit
) (new-value bit-array
&rest subscripts
)
904 (declare (type (simple-array bit
) bit-array
)
906 (truly-dynamic-extent subscripts
)
907 (optimize (safety 1)))
908 (setf (row-major-aref bit-array
909 (apply #'%array-row-major-index bit-array subscripts
))
912 ;;;; miscellaneous array properties
914 (defun array-element-type (array)
916 "Return the type of the elements of the array"
917 (let ((widetag (%other-pointer-widetag array
)))
918 (macrolet ((pick-element-type (&rest stuff
)
919 `(cond ,@(mapcar (lambda (stuff)
921 (let ((item (car stuff
)))
930 `(= widetag
,item
))))
933 #.
`(pick-element-type
936 `(,(if (sb!vm
:saetp-complex-typecode saetp
)
937 (list (sb!vm
:saetp-typecode saetp
)
938 (sb!vm
:saetp-complex-typecode saetp
))
939 (sb!vm
:saetp-typecode saetp
))
940 ',(sb!vm
:saetp-specifier saetp
)))
941 sb
!vm
:*specialized-array-element-type-properties
*)
942 ((sb!vm
:simple-array-widetag
943 sb
!vm
:complex-vector-widetag
944 sb
!vm
:complex-array-widetag
)
945 (with-array-data ((array array
) (start) (end))
946 (declare (ignore start end
))
947 (array-element-type array
)))
949 (error 'type-error
:datum array
:expected-type
'array
))))))
951 (defun array-rank (array)
953 "Return the number of dimensions of ARRAY."
954 (if (array-header-p array
)
958 (defun array-dimension (array axis-number
)
960 "Return the length of dimension AXIS-NUMBER of ARRAY."
961 (declare (array array
) (type index axis-number
))
962 (cond ((not (array-header-p array
))
963 (unless (= axis-number
0)
964 (error "Vector axis is not zero: ~S" axis-number
))
965 (length (the (simple-array * (*)) array
)))
966 ((>= axis-number
(%array-rank array
))
967 (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
968 axis-number array
(%array-rank array
)))
970 (%array-dimension array axis-number
))))
972 (defun array-dimensions (array)
974 "Return a list whose elements are the dimensions of the array"
975 (declare (array array
))
976 (if (array-header-p array
)
977 (do ((results nil
(cons (array-dimension array index
) results
))
978 (index (1- (array-rank array
)) (1- index
)))
979 ((minusp index
) results
))
980 (list (array-dimension array
0))))
982 (defun array-total-size (array)
984 "Return the total number of elements in the Array."
985 (declare (array array
))
986 (if (array-header-p array
)
987 (%array-available-elements array
)
988 (length (the vector array
))))
990 (defun array-displacement (array)
992 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
993 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
994 (declare (type array array
))
995 (if (and (array-header-p array
) ; if unsimple and
996 (%array-displaced-p array
)) ; displaced
997 (values (%array-data-vector array
) (%array-displacement array
))
1000 (defun adjustable-array-p (array)
1002 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
1003 to the argument, this happens for complex arrays."
1004 (declare (array array
))
1005 ;; Note that this appears not to be a fundamental limitation.
1006 ;; non-vector SIMPLE-ARRAYs are in fact capable of being adjusted,
1007 ;; but in practice we test using ADJUSTABLE-ARRAY-P in ADJUST-ARRAY.
1008 ;; -- CSR, 2004-03-01.
1009 (not (typep array
'simple-array
)))
1011 ;;;; fill pointer frobbing stuff
1013 (declaim (inline array-has-fill-pointer-p
))
1014 (defun array-has-fill-pointer-p (array)
1016 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
1017 (declare (array array
))
1018 (and (array-header-p array
) (%array-fill-pointer-p array
)))
1020 (defun fill-pointer-error (vector &optional arg
)
1022 (aver (array-has-fill-pointer-p vector
))
1023 (let ((max (%array-available-elements vector
)))
1024 (error 'simple-type-error
1026 :expected-type
(list 'integer
0 max
)
1027 :format-control
"The new fill pointer, ~S, is larger than the length of the vector (~S.)"
1028 :format-arguments
(list arg max
))))
1030 (error 'simple-type-error
1032 :expected-type
'(and vector
(satisfies array-has-fill-pointer-p
))
1033 :format-control
"~S is not an array with a fill pointer."
1034 :format-arguments
(list vector
)))))
1036 (declaim (inline fill-pointer
))
1037 (defun fill-pointer (vector)
1039 "Return the FILL-POINTER of the given VECTOR."
1040 (if (array-has-fill-pointer-p vector
)
1041 (%array-fill-pointer vector
)
1042 (fill-pointer-error vector
)))
1044 (defun %set-fill-pointer
(vector new
)
1046 (fill-pointer-error vector x
)))
1047 (if (array-has-fill-pointer-p vector
)
1048 (if (> new
(%array-available-elements vector
))
1050 (setf (%array-fill-pointer vector
) new
))
1053 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
1054 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
1055 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
1056 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
1057 ;;; back to CMU CL).
1058 (defun vector-push (new-element array
)
1060 "Attempt to set the element of ARRAY designated by its fill pointer
1061 to NEW-ELEMENT, and increment the fill pointer by one. If the fill pointer is
1062 too large, NIL is returned, otherwise the index of the pushed element is
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 (let ((fill-pointer (fill-pointer vector
)))
1077 (declare (fixnum fill-pointer
))
1078 (when (= fill-pointer
(%array-available-elements vector
))
1079 (let ((min-extension
1081 (let ((length (length vector
)))
1083 (- array-dimension-limit length
))))))
1084 (adjust-array vector
(+ fill-pointer
(max 1 min-extension
)))))
1085 ;; disable bounds checking
1086 (locally (declare (optimize (safety 0)))
1087 (setf (aref vector fill-pointer
) new-element
))
1088 (setf (%array-fill-pointer vector
) (1+ fill-pointer
))
1091 (defun vector-pop (array)
1093 "Decrease the fill pointer by 1 and return the element pointed to by the
1095 (let ((fill-pointer (fill-pointer array
)))
1096 (declare (fixnum fill-pointer
))
1097 (if (zerop fill-pointer
)
1098 (error "There is nothing left to pop.")
1099 ;; disable bounds checking (and any fixnum test)
1100 (locally (declare (optimize (safety 0)))
1102 (setf (%array-fill-pointer array
)
1103 (1- fill-pointer
)))))))
1108 (defun adjust-array (array dimensions
&key
1109 (element-type (array-element-type array
) element-type-p
)
1110 (initial-element nil initial-element-p
)
1111 (initial-contents nil initial-contents-p
)
1113 displaced-to displaced-index-offset
)
1115 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
1116 (when (invalid-array-p array
)
1117 (invalid-array-error array
))
1118 (binding* ((dimensions (ensure-list dimensions
))
1119 (array-rank (array-rank array
))
1121 (unless (= (length dimensions
) array-rank
)
1122 (error "The number of dimensions not equal to rank of array.")))
1123 ((initialize initial-data
)
1124 (validate-array-initargs initial-element-p initial-element
1125 initial-contents-p initial-contents
1127 (cond ((and element-type-p
1128 (not (subtypep element-type
(array-element-type array
))))
1129 ;; This is weird. Should check upgraded type against actual
1130 ;; array element type I think. See lp#1331299. CLHS says that
1131 ;; "consequences are unspecified" so current behavior isn't wrong.
1132 (error "The new element type, ~S, is incompatible with old type."
1134 ((and fill-pointer
(/= array-rank
1))
1135 (error "Only vectors can have fill pointers."))
1136 ((and fill-pointer
(not (array-has-fill-pointer-p array
)))
1137 ;; This case always struck me as odd. It seems like it might mean
1138 ;; that the user asks that the array gain a fill-pointer if it didn't
1139 ;; have one, yet CLHS is clear that the argument array must have a
1140 ;; fill-pointer or else signal a type-error.
1141 (fill-pointer-error array
)))
1142 (cond (initial-contents-p
1143 ;; array former contents replaced by INITIAL-CONTENTS
1144 (let* ((array-size (apply #'* dimensions
))
1145 (array-data (data-vector-from-inits
1146 dimensions array-size element-type nil nil
1147 initialize initial-data
)))
1148 (if (adjustable-array-p array
)
1149 (set-array-header array array-data array-size
1150 (get-new-fill-pointer array array-size
1152 0 dimensions nil nil
)
1153 (if (array-header-p array
)
1154 ;; simple multidimensional or single dimensional array
1155 (make-array dimensions
1156 :element-type element-type
1157 :initial-contents initial-contents
)
1160 ;; We already established that no INITIAL-CONTENTS was supplied.
1161 (unless (subtypep element-type
(array-element-type displaced-to
))
1162 ;; See lp#1331299 again. Require exact match on upgraded type?
1163 (error "can't displace an array of type ~S into another of ~
1165 element-type
(array-element-type displaced-to
)))
1166 (let ((displacement (or displaced-index-offset
0))
1167 (array-size (apply #'* dimensions
)))
1168 (declare (fixnum displacement array-size
))
1169 (if (< (the fixnum
(array-total-size displaced-to
))
1170 (the fixnum
(+ displacement array-size
)))
1171 (error "The :DISPLACED-TO array is too small."))
1172 (if (adjustable-array-p array
)
1173 ;; None of the original contents appear in adjusted array.
1174 (set-array-header array displaced-to array-size
1175 (get-new-fill-pointer array array-size
1177 displacement dimensions t nil
)
1178 ;; simple multidimensional or single dimensional array
1179 (make-array dimensions
1180 :element-type element-type
1181 :displaced-to displaced-to
1182 :displaced-index-offset
1183 displaced-index-offset
))))
1185 (let ((old-length (array-total-size array
))
1186 (new-length (car dimensions
))
1188 (declare (fixnum old-length new-length
))
1189 (with-array-data ((old-data array
) (old-start)
1190 (old-end old-length
))
1191 (cond ((or (and (array-header-p array
)
1192 (%array-displaced-p array
))
1193 (< old-length new-length
))
1195 (data-vector-from-inits
1196 dimensions new-length element-type
1197 (%other-pointer-widetag old-data
) nil
1198 initialize initial-data
))
1199 ;; Provide :END1 to avoid full call to LENGTH
1201 (replace new-data old-data
1203 :start2 old-start
:end2 old-end
))
1205 (shrink-vector old-data new-length
))))
1206 (if (adjustable-array-p array
)
1207 (set-array-header array new-data new-length
1208 (get-new-fill-pointer array new-length
1210 0 dimensions nil nil
)
1213 (let ((old-length (%array-available-elements array
))
1214 (new-length (apply #'* dimensions
)))
1215 (declare (fixnum old-length new-length
))
1216 (with-array-data ((old-data array
) (old-start)
1217 (old-end old-length
))
1218 (declare (ignore old-end
))
1219 (let ((new-data (if (or (and (array-header-p array
)
1220 (%array-displaced-p array
))
1221 (> new-length old-length
)
1222 (not (adjustable-array-p array
)))
1223 (data-vector-from-inits
1224 dimensions new-length
1226 (%other-pointer-widetag old-data
) nil
1227 (if initial-element-p
:initial-element
)
1230 (if (or (zerop old-length
) (zerop new-length
))
1231 (when initial-element-p
(fill new-data initial-element
))
1232 (zap-array-data old-data
(array-dimensions array
)
1234 new-data dimensions new-length
1235 element-type initial-element
1237 (if (adjustable-array-p array
)
1238 (set-array-header array new-data new-length
1239 nil
0 dimensions nil nil
)
1242 sb
!vm
:simple-array-widetag array-rank
)))
1243 (set-array-header new-array new-data new-length
1244 nil
0 dimensions nil t
))))))))))
1247 (defun get-new-fill-pointer (old-array new-array-size fill-pointer
)
1248 (cond ((not fill-pointer
)
1249 ;; "The consequences are unspecified if array is adjusted to a
1250 ;; size smaller than its fill pointer ..."
1251 (when (array-has-fill-pointer-p old-array
)
1252 (when (> (%array-fill-pointer old-array
) new-array-size
)
1253 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
1254 smaller than its fill pointer (~S)"
1255 old-array new-array-size
(fill-pointer old-array
)))
1256 (%array-fill-pointer old-array
)))
1257 ((numberp fill-pointer
)
1258 (when (> fill-pointer new-array-size
)
1259 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
1260 than the new length of the vector (~S)"
1261 fill-pointer new-array-size
))
1263 ((eq fill-pointer t
)
1266 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
1267 ;;; which must be less than or equal to its current length. This can
1268 ;;; be called on vectors without a fill pointer but it is extremely
1269 ;;; dangerous to do so: shrinking the size of an object (as viewed by
1270 ;;; the gc) makes bounds checking unreliable in the face of interrupts
1271 ;;; or multi-threading. Call it only on provably local vectors.
1272 (defun %shrink-vector
(vector new-length
)
1273 (declare (vector vector
))
1274 (unless (array-header-p vector
)
1275 (macrolet ((frob (name &rest things
)
1277 ((simple-array nil
(*)) (error 'nil-array-accessed-error
))
1278 ,@(mapcar (lambda (thing)
1279 (destructuring-bind (type-spec fill-value
)
1282 (fill (truly-the ,type-spec
,name
)
1284 :start new-length
))))
1286 ;; Set the 'tail' of the vector to the appropriate type of zero,
1287 ;; "because in some cases we'll scavenge larger areas in one go,
1288 ;; like groups of pages that had triggered the write barrier, or
1289 ;; the whole static space" according to jsnell.
1293 `((simple-array ,(sb!vm
:saetp-specifier saetp
) (*))
1294 ,(if (or (eq (sb!vm
:saetp-specifier saetp
) 'character
)
1296 (eq (sb!vm
:saetp-specifier saetp
) 'base-char
))
1297 *default-init-char-form
*
1298 (sb!vm
:saetp-initial-element-default saetp
))))
1300 #'sb
!vm
:saetp-specifier
1301 sb
!vm
:*specialized-array-element-type-properties
*)))))
1302 ;; Only arrays have fill-pointers, but vectors have their length
1303 ;; parameter in the same place.
1304 (setf (%array-fill-pointer vector
) new-length
)
1307 (defun shrink-vector (vector new-length
)
1308 (declare (vector vector
))
1310 ((eq (length vector
) new-length
)
1312 ((array-has-fill-pointer-p vector
)
1313 (setf (%array-fill-pointer vector
) new-length
)
1315 (t (subseq vector
0 new-length
))))
1317 ;;; BIG THREAD SAFETY NOTE
1319 ;;; ADJUST-ARRAY/SET-ARRAY-HEADER, and its callees are very
1320 ;;; thread unsafe. They are nonatomic, and can mess with parallel
1321 ;;; code using the same arrays.
1323 ;;; A likely seeming fix is an additional level of indirection:
1324 ;;; ARRAY-HEADER -> ARRAY-INFO -> ... where ARRAY-HEADER would
1325 ;;; hold nothing but the pointer to ARRAY-INFO, and ARRAY-INFO
1326 ;;; would hold everything ARRAY-HEADER now holds. This allows
1327 ;;; consing up a new ARRAY-INFO and replacing it atomically in
1328 ;;; the ARRAY-HEADER.
1330 ;;; %WALK-DISPLACED-ARRAY-BACKPOINTERS is an especially nasty
1331 ;;; one: not only is it needed extremely rarely, which makes
1332 ;;; any thread safety bugs involving it look like rare random
1333 ;;; corruption, but because it walks the chain *upwards*, which
1334 ;;; may violate user expectations.
1336 (defun %save-displaced-array-backpointer
(array data
)
1337 (flet ((purge (pointers)
1338 (remove-if (lambda (value)
1339 (or (not value
) (eq array value
)))
1341 :key
#'weak-pointer-value
)))
1342 ;; Add backpointer to the new data vector if it has a header.
1343 (when (array-header-p data
)
1344 (setf (%array-displaced-from data
)
1345 (cons (make-weak-pointer array
)
1346 (purge (%array-displaced-from data
)))))
1347 ;; Remove old backpointer, if any.
1348 (let ((old-data (%array-data-vector array
)))
1349 (when (and (neq data old-data
) (array-header-p old-data
))
1350 (setf (%array-displaced-from old-data
)
1351 (purge (%array-displaced-from old-data
)))))))
1353 (defun %walk-displaced-array-backpointers
(array new-length
)
1354 (dolist (p (%array-displaced-from array
))
1355 (let ((from (weak-pointer-value p
)))
1356 (when (and from
(eq array
(%array-data-vector from
)))
1357 (let ((requires (+ (%array-available-elements from
)
1358 (%array-displacement from
))))
1359 (unless (>= new-length requires
)
1360 ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
1362 ;; "If A is displaced to B, the consequences are unspecified if B is
1363 ;; adjusted in such a way that it no longer has enough elements to
1366 ;; since we're hanging on a weak pointer here, we can't signal an
1367 ;; error right now: the array that we're looking at might be
1368 ;; garbage. Instead, we set all dimensions to zero so that next
1369 ;; safe access to the displaced array will trap. Additionally, we
1370 ;; save the original dimensions, so we can signal a more
1371 ;; understandable error when the time comes.
1372 (%walk-displaced-array-backpointers from
0)
1373 (setf (%array-fill-pointer from
) 0
1374 (%array-available-elements from
) 0
1375 (%array-displaced-p from
) (array-dimensions array
))
1376 (dotimes (i (%array-rank from
))
1377 (setf (%array-dimension from i
) 0))))))))
1379 ;;; Fill in array header with the provided information, and return the array.
1380 (defun set-array-header (array data length fill-pointer displacement dimensions
1383 (setf (%array-displaced-from array
) nil
)
1384 (%walk-displaced-array-backpointers array length
))
1386 (%save-displaced-array-backpointer array data
))
1387 (setf (%array-data-vector array
) data
)
1388 (setf (%array-available-elements array
) length
)
1390 (setf (%array-fill-pointer array
) fill-pointer
)
1391 (setf (%array-fill-pointer-p array
) t
))
1393 (setf (%array-fill-pointer array
) length
)
1394 (setf (%array-fill-pointer-p array
) nil
)))
1395 (setf (%array-displacement array
) displacement
)
1396 (if (listp dimensions
)
1397 (dotimes (axis (array-rank array
))
1398 (declare (type index axis
))
1399 (setf (%array-dimension array axis
) (pop dimensions
)))
1400 (setf (%array-dimension array
0) dimensions
))
1401 (setf (%array-displaced-p array
) displacedp
)
1404 ;;; User visible extension
1405 (declaim (ftype (function (array) (values (simple-array * (*)) &optional
))
1406 array-storage-vector
))
1407 (defun array-storage-vector (array)
1409 "Returns the underlying storage vector of ARRAY, which must be a non-displaced array.
1411 In SBCL, if ARRAY is a of type \(SIMPLE-ARRAY * \(*)), it is its own storage
1412 vector. Multidimensional arrays, arrays with fill pointers, and adjustable
1413 arrays have an underlying storage vector with the same ARRAY-ELEMENT-TYPE as
1414 ARRAY, which this function returns.
1416 Important note: the underlying vector is an implementation detail. Even though
1417 this function exposes it, changes in the implementation may cause this
1418 function to be removed without further warning."
1419 ;; KLUDGE: Without TRULY-THE the system is not smart enough to figure out that
1420 ;; the return value is always of the known type.
1421 (truly-the (simple-array * (*))
1422 (if (array-header-p array
)
1423 (if (%array-displaced-p array
)
1424 (error "~S cannot be used with displaced arrays. Use ~S instead."
1425 'array-storage-vector
'array-displacement
)
1426 (%array-data-vector array
))
1430 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
1432 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
1433 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
1434 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
1435 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
1436 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
1437 element-type initial-element initial-element-p
)
1438 (declare (list old-dims new-dims
)
1439 (fixnum new-length
))
1440 ;; OLD-DIMS comes from array-dimensions, which returns a fresh list
1441 ;; at least in SBCL.
1442 ;; NEW-DIMS comes from the user.
1443 (setf old-dims
(nreverse old-dims
)
1444 new-dims
(reverse new-dims
))
1445 (cond ((eq old-data new-data
)
1446 ;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and
1447 ;; INITIAL-ELEMENT-P are used when OLD-DATA and NEW-DATA are
1448 ;; EQ; in this case, a temporary must be used and filled
1449 ;; appropriately. specified initial-element.
1450 (when initial-element-p
1451 ;; FIXME: transforming this TYPEP to someting a bit faster
1452 ;; would be a win...
1453 (unless (typep initial-element element-type
)
1454 (error "~S can't be used to initialize an array of type ~S."
1455 initial-element element-type
)))
1456 (let ((temp (if initial-element-p
1457 (make-array new-length
:initial-element initial-element
)
1458 (make-array new-length
))))
1459 (declare (simple-vector temp
))
1460 (zap-array-data-aux old-data old-dims offset temp new-dims
)
1461 (dotimes (i new-length
)
1462 (setf (aref new-data i
) (aref temp i
)))
1463 ;; Kill the temporary vector to prevent garbage retention.
1464 (%shrink-vector temp
0)))
1466 ;; When OLD-DATA and NEW-DATA are not EQ, NEW-DATA has
1467 ;; already been filled with any
1468 (zap-array-data-aux old-data old-dims offset new-data new-dims
))))
1470 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims
)
1471 (declare (fixnum offset
))
1472 (let ((limits (mapcar (lambda (x y
)
1473 (declare (fixnum x y
))
1474 (1- (the fixnum
(min x y
))))
1475 old-dims new-dims
)))
1476 (macrolet ((bump-index-list (index limits
)
1477 `(do ((subscripts ,index
(cdr subscripts
))
1478 (limits ,limits
(cdr limits
)))
1479 ((null subscripts
) :eof
)
1480 (cond ((< (the fixnum
(car subscripts
))
1481 (the fixnum
(car limits
)))
1483 (1+ (the fixnum
(car subscripts
))))
1485 (t (rplaca subscripts
0))))))
1486 (do ((index (make-list (length old-dims
) :initial-element
0)
1487 (bump-index-list index limits
)))
1489 (setf (aref new-data
(row-major-index-from-dims index new-dims
))
1491 (+ (the fixnum
(row-major-index-from-dims index old-dims
))
1494 ;;; Figure out the row-major-order index of an array reference from a
1495 ;;; list of subscripts and a list of dimensions. This is for internal
1496 ;;; calls only, and the subscripts and dim-list variables are assumed
1497 ;;; to be reversed from what the user supplied.
1498 (defun row-major-index-from-dims (rev-subscripts rev-dim-list
)
1499 (do ((rev-subscripts rev-subscripts
(cdr rev-subscripts
))
1500 (rev-dim-list rev-dim-list
(cdr rev-dim-list
))
1503 ((null rev-dim-list
) result
)
1504 (declare (fixnum chunk-size result
))
1505 (setq result
(+ result
1506 (the fixnum
(* (the fixnum
(car rev-subscripts
))
1508 (setq chunk-size
(* chunk-size
(the fixnum
(car rev-dim-list
))))))
1512 (defun bit-array-same-dimensions-p (array1 array2
)
1513 (declare (type (array bit
) array1 array2
))
1514 (and (= (array-rank array1
)
1515 (array-rank array2
))
1516 (dotimes (index (array-rank array1
) t
)
1517 (when (/= (array-dimension array1 index
)
1518 (array-dimension array2 index
))
1521 (defun pick-result-array (result-bit-array bit-array-1
)
1522 (case result-bit-array
1524 ((nil) (make-array (array-dimensions bit-array-1
)
1526 :initial-element
0))
1528 (unless (bit-array-same-dimensions-p bit-array-1
1530 (error "~S and ~S don't have the same dimensions."
1531 bit-array-1 result-bit-array
))
1534 (defmacro def-bit-array-op
(name function
)
1535 `(defun ,name
(bit-array-1 bit-array-2
&optional result-bit-array
)
1538 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1539 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1540 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1541 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1542 All the arrays must have the same rank and dimensions."
1543 (symbol-name function
))
1544 (declare (type (array bit
) bit-array-1 bit-array-2
)
1545 (type (or (array bit
) (member t nil
)) result-bit-array
))
1546 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2
)
1547 (error "~S and ~S don't have the same dimensions."
1548 bit-array-1 bit-array-2
))
1549 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1
)))
1550 (if (and (simple-bit-vector-p bit-array-1
)
1551 (simple-bit-vector-p bit-array-2
)
1552 (simple-bit-vector-p result-bit-array
))
1553 (locally (declare (optimize (speed 3) (safety 0)))
1554 (,name bit-array-1 bit-array-2 result-bit-array
))
1555 (with-array-data ((data1 bit-array-1
) (start1) (end1))
1556 (declare (ignore end1
))
1557 (with-array-data ((data2 bit-array-2
) (start2) (end2))
1558 (declare (ignore end2
))
1559 (with-array-data ((data3 result-bit-array
) (start3) (end3))
1560 (do ((index-1 start1
(1+ index-1
))
1561 (index-2 start2
(1+ index-2
))
1562 (index-3 start3
(1+ index-3
)))
1563 ((>= index-3 end3
) result-bit-array
)
1564 (declare (type index index-1 index-2 index-3
))
1565 (setf (sbit data3 index-3
)
1566 (logand (,function
(sbit data1 index-1
)
1567 (sbit data2 index-2
))
1570 (def-bit-array-op bit-and logand
)
1571 (def-bit-array-op bit-ior logior
)
1572 (def-bit-array-op bit-xor logxor
)
1573 (def-bit-array-op bit-eqv logeqv
)
1574 (def-bit-array-op bit-nand lognand
)
1575 (def-bit-array-op bit-nor lognor
)
1576 (def-bit-array-op bit-andc1 logandc1
)
1577 (def-bit-array-op bit-andc2 logandc2
)
1578 (def-bit-array-op bit-orc1 logorc1
)
1579 (def-bit-array-op bit-orc2 logorc2
)
1581 (defun bit-not (bit-array &optional result-bit-array
)
1583 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1584 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1585 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1586 created. Both arrays must have the same rank and dimensions."
1587 (declare (type (array bit
) bit-array
)
1588 (type (or (array bit
) (member t nil
)) result-bit-array
))
1589 (let ((result-bit-array (pick-result-array result-bit-array bit-array
)))
1590 (if (and (simple-bit-vector-p bit-array
)
1591 (simple-bit-vector-p result-bit-array
))
1592 (locally (declare (optimize (speed 3) (safety 0)))
1593 (bit-not bit-array result-bit-array
))
1594 (with-array-data ((src bit-array
) (src-start) (src-end))
1595 (declare (ignore src-end
))
1596 (with-array-data ((dst result-bit-array
) (dst-start) (dst-end))
1597 (do ((src-index src-start
(1+ src-index
))
1598 (dst-index dst-start
(1+ dst-index
)))
1599 ((>= dst-index dst-end
) result-bit-array
)
1600 (declare (type index src-index dst-index
))
1601 (setf (sbit dst dst-index
)
1602 (logxor (sbit src src-index
) 1))))))))
1604 ;;;; array type dispatching
1606 ;;; Given DISPATCH-FOO as the DISPATCH-NAME argument (unevaluated),
1607 ;;; defines the functions
1609 ;;; DISPATCH-FOO/SIMPLE-BASE-STRING
1610 ;;; DISPATCH-FOO/SIMPLE-CHARACTER-STRING
1611 ;;; DISPATCH-FOO/SIMPLE-ARRAY-SINGLE-FLOAT
1614 ;;; PARAMS are the function parameters in the definition of each
1615 ;;; specializer function. The array being specialized must be the
1616 ;;; first parameter in PARAMS. A type declaration for this parameter
1617 ;;; is automatically inserted into the body of each function.
1619 ;;; The dispatch table %%FOO-FUNS%% is defined and populated by these
1620 ;;; functions. The table is padded by the function
1621 ;;; HAIRY-FOO-DISPATCH-ERROR, also defined by DEFINE-ARRAY-DISPATCH.
1623 ;;; Finally, the DISPATCH-FOO macro is defined which does the actual
1624 ;;; dispatching when called. It expects arguments that match PARAMS.
1626 (defmacro define-array-dispatch
(dispatch-name params
&body body
)
1627 (let ((table-name (symbolicate "%%" dispatch-name
"-FUNS%%"))
1628 (error-name (symbolicate "HAIRY-" dispatch-name
"-ERROR")))
1630 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
1631 (defun ,error-name
(&rest args
)
1634 :expected-type
'(simple-array * (*)))))
1635 (!defglobal
,table-name
(make-array ,(1+ sb
!vm
:widetag-mask
)
1636 :initial-element
#',error-name
))
1637 ,@(loop for info across sb
!vm
:*specialized-array-element-type-properties
*
1638 for typecode
= (sb!vm
:saetp-typecode info
)
1639 for specifier
= (sb!vm
:saetp-specifier info
)
1640 for primitive-type-name
= (sb!vm
:saetp-primitive-type-name info
)
1641 collect
(let ((fun-name (symbolicate (string dispatch-name
)
1642 "/" primitive-type-name
)))
1644 (defun ,fun-name
,params
1645 (declare (type (simple-array ,specifier
(*))
1648 (setf (svref ,table-name
,typecode
) #',fun-name
))))
1649 (defmacro ,dispatch-name
(&rest args
)
1650 (check-type (first args
) symbol
)
1651 (let ((tag (gensym "TAG")))
1655 (when (sb!vm
::%other-pointer-p
,(first args
))
1656 (setf ,tag
(%other-pointer-widetag
,(first args
))))
1657 (svref ,',table-name
,tag
)))