Better type declarations for fill-pointer related code.
[sbcl.git] / src / code / array.lisp
blobb9f880731340a17531babfaca54a2e455d370821
1 ;;;; functions to implement arrays
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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")
14 #!-sb-fluid
15 (declaim (inline adjustable-array-p
16 array-displacement))
18 ;;;; miscellaneous accessor functions
20 ;;; These functions are only needed by the interpreter, 'cause the
21 ;;; compiler inlines them.
22 (macrolet ((def (name)
23 `(progn
24 (defun ,name (array)
25 (,name array))
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)
37 (%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)
47 (fixnum index))
48 (%check-bound array bound index))
50 (defun check-bound (array bound index)
51 (declare (type index bound)
52 (fixnum index))
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)))
68 ;;;; MAKE-ARRAY
69 (defun %integer-vector-widetag-and-n-bits (signed high)
70 (let ((unsigned-table
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)))))
81 map))
82 (signed-table
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)))
93 map)))
94 (cond ((> high sb!vm:n-word-bits)
95 (values #.sb!vm:simple-vector-widetag #.sb!vm:n-word-bits))
96 (signed
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
110 collect `(,arg '*)))
111 (declare (ignorable ,@args))
112 (when (consp type)
113 (let ((,type-sym (cdr type)))
114 (block nil
115 ,@(loop for arg in args
116 collect
117 `(cond ((consp ,type-sym)
118 (let ((value (pop ,type-sym)))
119 (if (or (eq value '*)
120 (typep value ',arg-type)
121 ,(if intervals
122 `(and (consp value)
123 (null (cdr value))
124 (typep (car value)
125 ',arg-type))))
126 (setf ,arg value)
127 (ill-type))))
128 ((null ,type-sym)
129 (return))
131 (ill-type)))))
132 (when ,type-sym
133 (ill-type))))
134 ,@body)))
135 (result (widetag)
136 (let ((value (symbol-value widetag)))
137 `(values ,value
138 ,(sb!vm:saetp-n-bits
139 (find value
140 sb!vm:*specialized-array-element-type-properties*
141 :key #'sb!vm:saetp-typecode))))))
142 (flet ((ill-type ()
143 (error "Invalid type specifier: ~s" type))
144 (integer-interval-widetag (low high)
145 (if (minusp low)
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))
153 (type-name (if consp
154 (car type)
155 type)))
156 (case type-name
157 ((t)
158 (when consp
159 (ill-type))
160 (result sb!vm:simple-vector-widetag))
161 ((base-char standard-char #!-sb-unicode character)
162 (when consp
163 (ill-type))
164 (result sb!vm:simple-base-string-widetag))
165 #!+sb-unicode
166 ((character extended-char)
167 (when consp
168 (ill-type))
169 (result sb!vm:simple-character-string-widetag))
170 (bit
171 (when consp
172 (ill-type))
173 (result sb!vm:simple-bit-vector-widetag))
174 (fixnum
175 (when consp
176 (ill-type))
177 (result sb!vm:simple-array-fixnum-widetag))
178 (unsigned-byte
179 (with-parameters ((integer 1)) (high)
180 (if (eq high '*)
181 (result sb!vm:simple-vector-widetag)
182 (%integer-vector-widetag-and-n-bits nil high))))
183 (signed-byte
184 (with-parameters ((integer 1)) (high)
185 (if (eq high '*)
186 (result sb!vm:simple-vector-widetag)
187 (%integer-vector-widetag-and-n-bits t high))))
188 (double-float
189 (with-parameters (double-float :intervals t) (low high)
190 (if (and (not (eq low '*))
191 (not (eq high '*))
192 (if (or (consp low) (consp high))
193 (>= (type-bound-number low) (type-bound-number high))
194 (> low high)))
195 (result sb!vm:simple-array-nil-widetag)
196 (result sb!vm:simple-array-double-float-widetag))))
197 (single-float
198 (with-parameters (single-float :intervals t) (low high)
199 (if (and (not (eq low '*))
200 (not (eq high '*))
201 (if (or (consp low) (consp high))
202 (>= (type-bound-number low) (type-bound-number high))
203 (> low high)))
204 (result sb!vm:simple-array-nil-widetag)
205 (result sb!vm:simple-array-single-float-widetag))))
206 (mod
207 (if (and (consp type)
208 (consp (cdr type))
209 (null (cddr type))
210 (typep (cadr type) '(integer 1)))
211 (%integer-vector-widetag-and-n-bits
212 nil (integer-length (1- (cadr type))))
213 (ill-type)))
214 #!+long-float
215 (long-float
216 (with-parameters (long-float :intervals t) (low high)
217 (if (and (not (eq low '*))
218 (not (eq high '*))
219 (if (or (consp low) (consp high))
220 (>= (type-bound-number low) (type-bound-number high))
221 (> low high)))
222 (result sb!vm:simple-array-nil-widetag)
223 (result sb!vm:simple-array-long-float-widetag))))
224 (integer
225 (with-parameters (integer :intervals t) (low high)
226 (let ((low (if (consp low)
227 (1+ (car low))
228 low))
229 (high (if (consp high)
230 (1- (car high))
231 high)))
232 (cond ((or (eq high '*)
233 (eq low '*))
234 (result sb!vm:simple-vector-widetag))
235 ((> low high)
236 (result sb!vm:simple-array-nil-widetag))
238 (integer-interval-widetag low high))))))
239 (complex
240 (with-parameters (t) (subtype)
241 (if (eq 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)))
248 (result
249 sb!vm:simple-array-complex-double-float-widetag))
250 ((csubtypep ctype (specifier-type '(complex single-float)))
251 (result
252 sb!vm:simple-array-complex-single-float-widetag))
253 #!+long-float
254 ((csubtypep ctype (specifier-type '(complex long-float)))
255 (result
256 sb!vm:simple-array-complex-long-float-widetag))
258 (result sb!vm:simple-vector-widetag))))
260 (case (numeric-type-format ctype)
261 (double-float
262 (result
263 sb!vm:simple-array-complex-double-float-widetag))
264 (single-float
265 (result
266 sb!vm:simple-array-complex-single-float-widetag))
267 #!+long-float
268 (long-float
269 (result
270 sb!vm:simple-array-complex-long-float-widetag))
272 (result sb!vm:simple-vector-widetag)))))))))
273 ((nil)
274 (result sb!vm:simple-array-nil-widetag))
276 (block nil
277 (let ((ctype
278 (handler-case (specifier-type type)
279 (parse-unknown-type ()
280 (return (result sb!vm:simple-vector-widetag))))))
281 (typecase ctype
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))
294 #!+long-float
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)
301 #!+sb-unicode
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 ()
316 `(case widetag
317 ,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
318 for complex = (sb!vm:saetp-complex-typecode saetp)
319 when complex
320 collect (list (sb!vm:saetp-typecode saetp) complex))
322 #.sb!vm:complex-vector-widetag))))
323 (make-case)))
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)
335 (type index length))
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
339 (ceiling
340 (* (if (or (= widetag sb!vm:simple-base-string-widetag)
341 #!+sb-unicode
342 (= widetag
343 sb!vm:simple-character-string-widetag))
344 (1+ length)
345 length)
346 n-bits)
347 sb!vm:n-word-bits))))
349 (defun array-underlying-widetag (array)
350 (macrolet ((make-case ()
351 `(case widetag
352 ,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
353 for complex = (sb!vm:saetp-complex-typecode saetp)
354 when complex
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)))
363 widetag))))
364 (let ((widetag (%other-pointer-widetag array)))
365 (make-case))))
367 (defun make-vector-like (vector length)
368 (let ((widetag (array-underlying-widetag vector)))
369 (%make-array length widetag (aref %%simple-array-n-bits%% widetag))))
371 ;; Complain in various ways about wrong :INITIAL-foo arguments,
372 ;; returning the two initialization arguments needed for DATA-VECTOR-FROM-INITS.
373 (defun validate-array-initargs (element-p element contents-p contents displaced)
374 (cond ((and displaced (or element-p contents-p))
375 (if (and element-p contents-p)
376 (error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
377 may be specified with the :DISPLACED-TO option")
378 (error "~S may not be specified with the :DISPLACED-TO option"
379 (if element-p :initial-element :initial-contents))))
380 ((and element-p contents-p)
381 (error "Can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
382 (element-p (values :initial-element element))
383 (contents-p (values :initial-contents contents))
384 (t (values nil nil))))
386 (declaim (inline %save-displaced-array-backpointer))
387 (defun %save-displaced-array-backpointer (array data)
388 (flet ((purge (pointers)
389 (remove-if (lambda (value)
390 (or (not value) (eq array value)))
391 pointers
392 :key #'weak-pointer-value)))
393 ;; Add backpointer to the new data vector if it has a header.
394 (when (array-header-p data)
395 (setf (%array-displaced-from data)
396 (cons (make-weak-pointer array)
397 (purge (%array-displaced-from data)))))
398 ;; Remove old backpointer, if any.
399 (let ((old-data (%array-data-vector array)))
400 (when (and (neq data old-data) (array-header-p old-data))
401 (setf (%array-displaced-from old-data)
402 (purge (%array-displaced-from old-data)))))))
404 ;;; Widetag is the widetag of the underlying vector,
405 ;;; it'll be the same as the resulting array widetag only for simple vectors
406 (defun %make-array (dimensions widetag n-bits
407 &key
408 element-type
409 (initial-element nil initial-element-p)
410 (initial-contents nil initial-contents-p)
411 adjustable fill-pointer
412 displaced-to displaced-index-offset)
413 (declare (ignore element-type))
414 (binding* (((array-rank dimension-0)
415 (if (listp dimensions)
416 (values (length dimensions)
417 (if dimensions (car dimensions) 1))
418 (values 1 dimensions)))
419 ((initialize initial-data)
420 (validate-array-initargs initial-element-p initial-element
421 initial-contents-p initial-contents
422 displaced-to))
423 (simple (and (null fill-pointer)
424 (not adjustable)
425 (null displaced-to))))
426 (declare (type array-rank array-rank))
427 (declare (type index dimension-0))
428 (cond ((and displaced-index-offset (null displaced-to))
429 (error "can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
430 ((and simple (= array-rank 1))
431 (let ((vector ; a (SIMPLE-ARRAY * (*))
432 (allocate-vector-with-widetag widetag dimension-0 n-bits)))
433 ;; presence of at most one :INITIAL-thing keyword was ensured above
434 (cond (initial-element-p
435 (fill vector initial-element))
436 (initial-contents-p
437 (let ((content-length (length initial-contents)))
438 (unless (= dimension-0 content-length)
439 (error "There are ~W elements in the :INITIAL-CONTENTS, but ~
440 the vector length is ~W."
441 content-length dimension-0)))
442 (replace vector initial-contents)))
443 vector))
444 ((and (arrayp displaced-to)
445 (/= (array-underlying-widetag displaced-to) widetag))
446 (error "Array element type of :DISPLACED-TO array does not match specified element type"))
448 ;; it's non-simple or multidimensional, or both.
449 (when fill-pointer
450 (unless (= array-rank 1)
451 (error "Only vectors can have fill pointers."))
452 (when (and (integerp fill-pointer) (> fill-pointer dimension-0))
453 ;; FIXME: should be TYPE-ERROR?
454 (error "invalid fill-pointer ~W" fill-pointer)))
455 (let* ((total-size
456 (if (consp dimensions)
457 (the index (reduce (lambda (a b) (* a (the index b)))
458 dimensions))
459 ;; () is considered to have dimension-0 = 1.
460 ;; It avoids the REDUCE lambda being called with no args.
461 dimension-0))
462 (data (or displaced-to
463 (data-vector-from-inits
464 dimensions total-size nil widetag n-bits
465 initialize initial-data)))
466 (array (make-array-header
467 (cond ((= array-rank 1)
468 (%complex-vector-widetag widetag))
469 (simple sb!vm:simple-array-widetag)
470 (t sb!vm:complex-array-widetag))
471 array-rank)))
472 (if fill-pointer
473 (setf (%array-fill-pointer-p array) t
474 (%array-fill-pointer array)
475 (if (eq fill-pointer t) dimension-0 fill-pointer))
476 (setf (%array-fill-pointer-p array) nil
477 (%array-fill-pointer array) total-size))
478 (setf (%array-available-elements array) total-size)
479 ;; Terrible name for this slot - we displace to the
480 ;; target array's header, if any, not the "ultimate"
481 ;; vector in the chain of displacements.
482 (setf (%array-data-vector array) data)
483 (setf (%array-displaced-from array) nil)
484 (cond (displaced-to
485 (let ((offset (or displaced-index-offset 0)))
486 (when (> (+ offset total-size)
487 (array-total-size displaced-to))
488 (error "~S doesn't have enough elements." displaced-to))
489 (setf (%array-displacement array) offset)
490 (setf (%array-displaced-p array) t)
491 (%save-displaced-array-backpointer array data)))
493 (setf (%array-displaced-p array) nil)))
494 (if (listp dimensions)
495 (let ((dims dimensions)) ; avoid "prevents use of assertion"
496 (dotimes (axis array-rank)
497 (setf (%array-dimension array axis) (pop dims))))
498 (setf (%array-dimension array 0) dimension-0))
499 array)))))
501 (defun make-array (dimensions &rest args
502 &key (element-type t)
503 initial-element initial-contents
504 adjustable
505 fill-pointer
506 displaced-to
507 displaced-index-offset)
508 (declare (ignore initial-element
509 initial-contents adjustable
510 fill-pointer displaced-to displaced-index-offset))
511 (declare (explicit-check))
512 (multiple-value-bind (widetag n-bits) (%vector-widetag-and-n-bits element-type)
513 (apply #'%make-array dimensions widetag n-bits args)))
515 (defun make-static-vector (length &key
516 (element-type '(unsigned-byte 8))
517 (initial-contents nil initial-contents-p)
518 (initial-element nil initial-element-p))
519 #!+sb-doc
520 "Allocate vector of LENGTH elements in static space. Only allocation
521 of specialized arrays is supported."
522 ;; STEP 1: check inputs fully
524 ;; This way of doing explicit checks before the vector is allocated
525 ;; is expensive, but probably worth the trouble as once we've allocated
526 ;; the vector we have no way to get rid of it anymore...
527 (when (eq t (upgraded-array-element-type element-type))
528 (error "Static arrays of type ~S not supported."
529 element-type))
530 (validate-array-initargs initial-element-p initial-element
531 initial-contents-p initial-contents nil) ; for effect
532 (when initial-contents-p
533 (unless (= length (length initial-contents))
534 (error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
535 vector length is ~W."
536 (length initial-contents)
537 length))
538 (unless (every (lambda (x) (typep x element-type)) initial-contents)
539 (error ":INITIAL-CONTENTS contains elements not of type ~S."
540 element-type)))
541 (when initial-element-p
542 (unless (typep initial-element element-type)
543 (error ":INITIAL-ELEMENT ~S is not of type ~S."
544 initial-element element-type)))
545 ;; STEP 2
547 ;; Allocate and possibly initialize the vector.
548 (multiple-value-bind (type n-bits)
549 (%vector-widetag-and-n-bits element-type)
550 (let ((vector
551 (allocate-static-vector type length
552 (ceiling (* length n-bits)
553 sb!vm:n-word-bits))))
554 (cond (initial-element-p
555 (fill vector initial-element))
556 (initial-contents-p
557 (replace vector initial-contents))
559 vector)))))
561 ;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
562 ;;; specified array characteristics. Dimensions is only used to pass
563 ;;; to FILL-DATA-VECTOR for error checking on the structure of
564 ;;; initial-contents.
565 (defun data-vector-from-inits (dimensions total-size
566 element-type widetag n-bits
567 initialize initial-data)
568 ;; FIXME: element-type can be NIL when widetag is non-nil,
569 ;; and FILL will check the type, although the error will be not as nice.
570 ;; (cond (typep initial-element element-type)
571 ;; (error "~S cannot be used to initialize an array of type ~S."
572 ;; initial-element element-type))
573 (let ((data (if widetag
574 (allocate-vector-with-widetag widetag total-size n-bits)
575 (make-array total-size :element-type element-type))))
576 (ecase initialize
577 (:initial-element
578 (fill (the vector data) initial-data))
579 (:initial-contents
580 ;; DIMENSIONS can be supplied as a list or integer now
581 (dx-let ((list-of-dims (list dimensions))) ; ok if already a list
582 (fill-data-vector data
583 (if (listp dimensions) dimensions list-of-dims)
584 initial-data)))
585 ((nil)))
586 data))
588 (defun vector (&rest objects)
589 #!+sb-doc
590 "Construct a SIMPLE-VECTOR from the given objects."
591 (let ((v (make-array (length objects))))
592 (do-rest-arg ((x i) objects 0 v)
593 (setf (aref v i) x))))
596 ;;;; accessor/setter functions
598 ;;; Dispatch to an optimized routine the data vector accessors for
599 ;;; each different specialized vector type. Do dispatching by looking
600 ;;; up the widetag in the array rather than with the typecases, which
601 ;;; as of 1.0.5 compiles to a naive sequence of linear TYPEPs. Also
602 ;;; provide separate versions where bounds checking has been moved
603 ;;; from the callee to the caller, since it's much cheaper to do once
604 ;;; the type information is available. Finally, for each of these
605 ;;; routines also provide a slow path, taken for arrays that are not
606 ;;; vectors or not simple.
607 (macrolet ((def (name table-name)
608 `(progn
609 (defglobal ,table-name (make-array ,(1+ sb!vm:widetag-mask)))
610 (declaim (type (simple-array function (,(1+ sb!vm:widetag-mask)))
611 ,table-name))
612 (defmacro ,name (array-var)
613 `(the function
614 (let ((tag 0))
615 (when (sb!vm::%other-pointer-p ,array-var)
616 (setf tag (%other-pointer-widetag ,array-var)))
617 (svref ,',table-name tag)))))))
618 (def !find-data-vector-setter %%data-vector-setters%%)
619 (def !find-data-vector-setter/check-bounds %%data-vector-setters/check-bounds%%)
620 ;; Used by DO-VECTOR-DATA -- which in turn appears in DOSEQUENCE expansion,
621 ;; meaning we can have post-build dependences on this.
622 (def %find-data-vector-reffer %%data-vector-reffers%%)
623 (def !find-data-vector-reffer/check-bounds %%data-vector-reffers/check-bounds%%))
625 ;;; Like DOVECTOR, but more magical -- can't use this on host.
626 (defmacro do-vector-data ((elt vector &optional result) &body body)
627 (multiple-value-bind (forms decls) (parse-body body nil)
628 (with-unique-names (index vec start end ref)
629 `(with-array-data ((,vec ,vector)
630 (,start)
631 (,end)
632 :check-fill-pointer t)
633 (let ((,ref (%find-data-vector-reffer ,vec)))
634 (do ((,index ,start (1+ ,index)))
635 ((>= ,index ,end)
636 (let ((,elt nil))
637 ,@(filter-dolist-declarations decls)
638 ,elt
639 ,result))
640 (let ((,elt (funcall ,ref ,vec ,index)))
641 ,@decls
642 (tagbody ,@forms))))))))
644 (macrolet ((%ref (accessor-getter extra-params)
645 `(funcall (,accessor-getter array) array index ,@extra-params))
646 (define (accessor-name slow-accessor-name accessor-getter
647 extra-params check-bounds)
648 `(progn
649 (defun ,accessor-name (array index ,@extra-params)
650 (declare (explicit-check))
651 (declare (optimize speed
652 ;; (SAFETY 0) is ok. All calls to
653 ;; these functions are generated by
654 ;; the compiler, so argument count
655 ;; checking isn't needed. Type checking
656 ;; is done implicitly via the widetag
657 ;; dispatch.
658 (safety 0)))
659 (%ref ,accessor-getter ,extra-params))
660 (defun ,slow-accessor-name (array index ,@extra-params)
661 (declare (optimize speed (safety 0)))
662 (if (not (%array-displaced-p array))
663 ;; The reasonably quick path of non-displaced complex
664 ;; arrays.
665 (let ((array (%array-data-vector array)))
666 (%ref ,accessor-getter ,extra-params))
667 ;; The real slow path.
668 (with-array-data
669 ((vector array)
670 (index (locally
671 (declare (optimize (speed 1) (safety 1)))
672 (,@check-bounds index)))
673 (end)
674 :force-inline t)
675 (declare (ignore end))
676 (,accessor-name vector index ,@extra-params)))))))
677 (define hairy-data-vector-ref slow-hairy-data-vector-ref
678 %find-data-vector-reffer
679 nil (progn))
680 (define hairy-data-vector-set slow-hairy-data-vector-set
681 !find-data-vector-setter
682 (new-value) (progn))
683 (define hairy-data-vector-ref/check-bounds
684 slow-hairy-data-vector-ref/check-bounds
685 !find-data-vector-reffer/check-bounds
686 nil (check-bound array (array-dimension array 0)))
687 (define hairy-data-vector-set/check-bounds
688 slow-hairy-data-vector-set/check-bounds
689 !find-data-vector-setter/check-bounds
690 (new-value) (check-bound array (array-dimension array 0))))
692 (defun hairy-ref-error (array index &optional new-value)
693 (declare (ignore index new-value))
694 (error 'type-error
695 :datum array
696 :expected-type 'vector))
698 (macrolet ((define-reffer (saetp check-form)
699 (let* ((type (sb!vm:saetp-specifier saetp))
700 (atype `(simple-array ,type (*))))
701 `(named-lambda (optimized-data-vector-ref ,type) (vector index)
702 (declare (optimize speed (safety 0))
703 ;; Obviously these all coerce raw words to lispobjs
704 ;; so don't keep spewing notes about it.
705 (muffle-conditions compiler-note)
706 (ignorable index))
707 ,(if type
708 `(data-vector-ref (the ,atype vector)
709 (locally
710 (declare (optimize (safety 1)))
711 (the index
712 (,@check-form index))))
713 `(data-nil-vector-ref (the ,atype vector) index)))))
714 (define-setter (saetp check-form)
715 (let* ((type (sb!vm:saetp-specifier saetp))
716 (atype `(simple-array ,type (*))))
717 `(named-lambda (optimized-data-vector-set ,type) (vector index new-value)
718 (declare (optimize speed (safety 0)))
719 ;; Impossibly setting an elt of an (ARRAY NIL)
720 ;; returns no value. And nobody cares.
721 (declare (muffle-conditions compiler-note))
722 (data-vector-set (the ,atype vector)
723 (locally
724 (declare (optimize (safety 1)))
725 (the index
726 (,@check-form index)))
727 (locally
728 ;; SPEED 1 needed to avoid the compiler
729 ;; from downgrading the type check to
730 ;; a cheaper one.
731 (declare (optimize (speed 1)
732 (safety 1)))
733 (the ,type new-value)))
734 ;; For specialized arrays, the return from
735 ;; data-vector-set would have to be reboxed to be a
736 ;; (Lisp) return value; instead, we use the
737 ;; already-boxed value as the return.
738 new-value)))
739 (define-reffers (symbol deffer check-form slow-path)
740 `(progn
741 ;; FIXME/KLUDGE: can't just FILL here, because genesis doesn't
742 ;; preserve the binding, so re-initiaize as NS doesn't have
743 ;; the energy to figure out to change that right now.
744 (setf ,symbol (make-array (1+ sb!vm::widetag-mask)
745 :initial-element #'hairy-ref-error))
746 ,@(loop for widetag in '(sb!vm:complex-vector-widetag
747 sb!vm:complex-vector-nil-widetag
748 sb!vm:complex-bit-vector-widetag
749 #!+sb-unicode sb!vm:complex-character-string-widetag
750 sb!vm:complex-base-string-widetag
751 sb!vm:simple-array-widetag
752 sb!vm:complex-array-widetag)
753 collect `(setf (svref ,symbol ,widetag) ,slow-path))
754 ,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
755 for widetag = (sb!vm:saetp-typecode saetp)
756 collect `(setf (svref ,symbol ,widetag)
757 (,deffer ,saetp ,check-form))))))
758 (defun !hairy-data-vector-reffer-init ()
759 (define-reffers %%data-vector-reffers%% define-reffer
760 (progn)
761 #'slow-hairy-data-vector-ref)
762 (define-reffers %%data-vector-setters%% define-setter
763 (progn)
764 #'slow-hairy-data-vector-set)
765 (define-reffers %%data-vector-reffers/check-bounds%% define-reffer
766 (check-bound vector (length vector))
767 #'slow-hairy-data-vector-ref/check-bounds)
768 (define-reffers %%data-vector-setters/check-bounds%% define-setter
769 (check-bound vector (length vector))
770 #'slow-hairy-data-vector-set/check-bounds)))
772 ;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
773 ;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
774 ;;; definition is needed for the compiler to use in constant folding.)
775 (defun data-vector-ref (array index)
776 (declare (explicit-check))
777 (hairy-data-vector-ref array index))
779 (defun data-vector-ref-with-offset (array index offset)
780 (declare (explicit-check))
781 (hairy-data-vector-ref array (+ index offset)))
783 (defun invalid-array-p (array)
784 (and (array-header-p array)
785 (consp (%array-displaced-p array))))
787 (declaim (ftype (function (array) nil) invalid-array-error))
788 (defun invalid-array-error (array)
789 (aver (array-header-p array))
790 ;; Array invalidation stashes the original dimensions here...
791 (let ((dims (%array-displaced-p array))
792 (et (array-element-type array)))
793 (error 'invalid-array-error
794 :datum array
795 :expected-type
796 (if (cdr dims)
797 `(array ,et ,dims)
798 `(vector ,et ,@dims)))))
800 (declaim (ftype (function (array t integer &optional t) nil)
801 invalid-array-index-error))
802 (defun invalid-array-index-error (array index bound &optional axis)
803 (if (invalid-array-p array)
804 (invalid-array-error array)
805 (error 'invalid-array-index-error
806 :array array
807 :axis axis
808 :datum index
809 :expected-type `(integer 0 (,bound)))))
811 ;;; SUBSCRIPTS has a dynamic-extent list structure and is destroyed
812 (defun %array-row-major-index (array &rest subscripts)
813 (declare (truly-dynamic-extent subscripts)
814 (array array))
815 (let ((length (length subscripts)))
816 (cond ((array-header-p array)
817 (let ((rank (%array-rank array)))
818 (unless (= rank length)
819 (error "wrong number of subscripts, ~W, for array of rank ~W."
820 length rank))
821 (do ((axis (1- rank) (1- axis))
822 (chunk-size 1)
823 (result 0))
824 ((minusp axis) result)
825 (declare (fixnum axis chunk-size result))
826 (let ((index (fast-&rest-nth axis subscripts))
827 (dim (%array-dimension array axis)))
828 (unless (and (fixnump index) (< -1 index dim))
829 (invalid-array-index-error array index dim axis))
830 (setf result
831 (truly-the fixnum
832 (+ result
833 (truly-the fixnum (* chunk-size index))))
834 chunk-size (truly-the fixnum (* chunk-size dim)))))))
835 ((/= length 1)
836 (error "Wrong number of subscripts, ~W, for array of rank 1."
837 length))
839 (let ((index (fast-&rest-nth 0 subscripts))
840 (length (length (the (simple-array * (*)) array))))
841 (unless (and (fixnump index) (< -1 index length))
842 (invalid-array-index-error array index length))
843 index)))))
845 (defun array-in-bounds-p (array &rest subscripts)
846 #!+sb-doc
847 "Return T if the SUBSCRIPTS are in bounds for the ARRAY, NIL otherwise."
848 (declare (truly-dynamic-extent subscripts))
849 (let ((length (length subscripts)))
850 (cond ((array-header-p array)
851 (let ((rank (%array-rank array)))
852 (unless (= rank length)
853 (error "Wrong number of subscripts, ~W, for array of rank ~W."
854 length rank))
855 (loop for i below length
856 for s = (fast-&rest-nth i subscripts)
857 always (and (typep s '(and fixnum unsigned-byte))
858 (< s (%array-dimension array i))))))
859 ((/= length 1)
860 (error "Wrong number of subscripts, ~W, for array of rank 1."
861 length))
863 (let ((subscript (fast-&rest-nth 0 subscripts)))
864 (and (typep subscript '(and fixnum unsigned-byte))
865 (< subscript
866 (length (truly-the (simple-array * (*)) array)))))))))
868 (defun array-row-major-index (array &rest subscripts)
869 (declare (truly-dynamic-extent subscripts))
870 (apply #'%array-row-major-index array subscripts))
872 (defun aref (array &rest subscripts)
873 #!+sb-doc
874 "Return the element of the ARRAY specified by the SUBSCRIPTS."
875 (declare (truly-dynamic-extent subscripts))
876 (row-major-aref array (apply #'%array-row-major-index array subscripts)))
878 ;;; (setf aref/bit/sbit) are implemented using setf-functions,
879 ;;; because they have to work with (setf (apply #'aref array subscripts))
880 ;;; All other setfs can be done using setf-functions too, but I
881 ;;; haven't found technical advantages or disadvantages for either
882 ;;; scheme.
883 (defun (setf aref) (new-value array &rest subscripts)
884 (declare (truly-dynamic-extent subscripts)
885 (type array array))
886 (setf (row-major-aref array (apply #'%array-row-major-index array subscripts))
887 new-value))
889 (defun row-major-aref (array index)
890 #!+sb-doc
891 "Return the element of array corresponding to the row-major index. This is
892 SETFable."
893 (declare (optimize (safety 1)))
894 (row-major-aref array index))
896 (defun %set-row-major-aref (array index new-value)
897 (declare (optimize (safety 1)))
898 (setf (row-major-aref array index) new-value))
900 (defun svref (simple-vector index)
901 #!+sb-doc
902 "Return the INDEXth element of the given Simple-Vector."
903 (declare (optimize (safety 1)))
904 (aref simple-vector index))
906 (defun %svset (simple-vector index new)
907 (declare (optimize (safety 1)))
908 (setf (aref simple-vector index) new))
910 (defun bit (bit-array &rest subscripts)
911 #!+sb-doc
912 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
913 (declare (type (array bit) bit-array)
914 (truly-dynamic-extent subscripts)
915 (optimize (safety 1)))
916 (row-major-aref bit-array (apply #'%array-row-major-index bit-array subscripts)))
918 (defun (setf bit) (new-value bit-array &rest subscripts)
919 (declare (type (array bit) bit-array)
920 (type bit new-value)
921 (truly-dynamic-extent subscripts)
922 (optimize (safety 1)))
923 (setf (row-major-aref bit-array
924 (apply #'%array-row-major-index bit-array subscripts))
925 new-value))
927 (defun sbit (simple-bit-array &rest subscripts)
928 #!+sb-doc
929 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
930 (declare (type (simple-array bit) simple-bit-array)
931 (truly-dynamic-extent subscripts)
932 (optimize (safety 1)))
933 (row-major-aref simple-bit-array
934 (apply #'%array-row-major-index simple-bit-array subscripts)))
936 (defun (setf sbit) (new-value bit-array &rest subscripts)
937 (declare (type (simple-array bit) bit-array)
938 (type bit new-value)
939 (truly-dynamic-extent subscripts)
940 (optimize (safety 1)))
941 (setf (row-major-aref bit-array
942 (apply #'%array-row-major-index bit-array subscripts))
943 new-value))
945 ;;;; miscellaneous array properties
947 (defun array-element-type (array)
948 #!+sb-doc
949 "Return the type of the elements of the array"
950 (let ((widetag (%other-pointer-widetag array))
951 (table (load-time-value
952 (let ((table (make-array 256 :initial-element nil)))
953 (dotimes (i (length sb!vm:*specialized-array-element-type-properties*) table)
954 (let* ((saetp (aref sb!vm:*specialized-array-element-type-properties* i))
955 (typecode (sb!vm:saetp-typecode saetp))
956 (complex-typecode (sb!vm:saetp-complex-typecode saetp))
957 (specifier (sb!vm:saetp-specifier saetp)))
958 (aver (typep specifier '(or list symbol)))
959 (setf (aref table typecode) specifier)
960 (when complex-typecode
961 (setf (aref table complex-typecode) specifier)))))
962 t)))
963 (let ((result (aref table widetag)))
964 (if result
965 (truly-the (or list symbol) result)
966 ;; (MAKE-ARRAY :ELEMENT-TYPE NIL) goes to this branch, but
967 ;; gets the right answer in the end
968 (with-array-data ((array array) (start) (end))
969 (declare (ignore start end))
970 (truly-the (or list symbol) (aref table (%other-pointer-widetag array))))))))
972 (defun array-rank (array)
973 #!+sb-doc
974 "Return the number of dimensions of ARRAY."
975 (if (array-header-p array)
976 (%array-rank array)
979 (defun array-dimension (array axis-number)
980 #!+sb-doc
981 "Return the length of dimension AXIS-NUMBER of ARRAY."
982 (declare (array array) (type index axis-number))
983 (cond ((not (array-header-p array))
984 (unless (= axis-number 0)
985 (error "Vector axis is not zero: ~S" axis-number))
986 (length (the (simple-array * (*)) array)))
987 ((>= axis-number (%array-rank array))
988 (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
989 axis-number array (%array-rank array)))
991 (%array-dimension array axis-number))))
993 (defun array-dimensions (array)
994 #!+sb-doc
995 "Return a list whose elements are the dimensions of the array"
996 (declare (array array))
997 (if (array-header-p array)
998 (do ((results nil (cons (array-dimension array index) results))
999 (index (1- (array-rank array)) (1- index)))
1000 ((minusp index) results))
1001 (list (array-dimension array 0))))
1003 (defun array-total-size (array)
1004 #!+sb-doc
1005 "Return the total number of elements in the Array."
1006 (declare (array array))
1007 (if (array-header-p array)
1008 (%array-available-elements array)
1009 (length (the vector array))))
1011 (defun array-displacement (array)
1012 #!+sb-doc
1013 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
1014 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
1015 (declare (type array array))
1016 (if (and (array-header-p array) ; if unsimple and
1017 (%array-displaced-p array)) ; displaced
1018 (values (%array-data-vector array) (%array-displacement array))
1019 (values nil 0)))
1021 (defun adjustable-array-p (array)
1022 #!+sb-doc
1023 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
1024 to the argument, this happens for complex arrays."
1025 (declare (array array))
1026 ;; Note that this appears not to be a fundamental limitation.
1027 ;; non-vector SIMPLE-ARRAYs are in fact capable of being adjusted,
1028 ;; but in practice we test using ADJUSTABLE-ARRAY-P in ADJUST-ARRAY.
1029 ;; -- CSR, 2004-03-01.
1030 (not (typep array 'simple-array)))
1032 ;;;; fill pointer frobbing stuff
1034 (declaim (inline array-has-fill-pointer-p))
1035 (defun array-has-fill-pointer-p (array)
1036 #!+sb-doc
1037 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
1038 (declare (array array))
1039 (and (array-header-p array) (%array-fill-pointer-p array)))
1041 (defun fill-pointer-error (vector &optional arg)
1042 (cond (arg
1043 (aver (array-has-fill-pointer-p vector))
1044 (let ((max (%array-available-elements vector)))
1045 (error 'simple-type-error
1046 :datum arg
1047 :expected-type (list 'integer 0 max)
1048 :format-control "The new fill pointer, ~S, is larger than the length of the vector (~S.)"
1049 :format-arguments (list arg max))))
1051 (error 'simple-type-error
1052 :datum vector
1053 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
1054 :format-control "~S is not an array with a fill pointer."
1055 :format-arguments (list vector)))))
1057 (declaim (inline fill-pointer))
1058 (defun fill-pointer (vector)
1059 #!+sb-doc
1060 "Return the FILL-POINTER of the given VECTOR."
1061 (declare (explicit-check))
1062 (if (array-has-fill-pointer-p vector)
1063 (%array-fill-pointer vector)
1064 (fill-pointer-error vector)))
1066 (defun %set-fill-pointer (vector new)
1067 (declare (explicit-check))
1068 (flet ((oops (x)
1069 (fill-pointer-error vector x)))
1070 (if (array-has-fill-pointer-p vector)
1071 (if (> new (%array-available-elements vector))
1072 (oops new)
1073 (setf (%array-fill-pointer vector) new))
1074 (oops nil))))
1076 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
1077 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
1078 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
1079 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
1080 ;;; back to CMU CL).
1081 (defun vector-push (new-element array)
1082 #!+sb-doc
1083 "Attempt to set the element of ARRAY designated by its fill pointer
1084 to NEW-ELEMENT, and increment the fill pointer by one. If the fill pointer is
1085 too large, NIL is returned, otherwise the index of the pushed element is
1086 returned."
1087 (declare (explicit-check))
1088 (let ((fill-pointer (fill-pointer array)))
1089 (cond ((= fill-pointer (%array-available-elements array))
1090 nil)
1092 (locally (declare (optimize (safety 0)))
1093 (setf (aref array fill-pointer) new-element))
1094 (setf (%array-fill-pointer array) (1+ fill-pointer))
1095 fill-pointer))))
1097 (defun vector-push-extend (new-element vector &optional min-extension)
1098 (declare (type (or null (and index (integer 1))) min-extension))
1099 (declare (explicit-check))
1100 (let ((fill-pointer (fill-pointer vector)))
1101 (when (= fill-pointer (%array-available-elements vector))
1102 (let ((min-extension
1103 (or min-extension
1104 (let ((length (length vector)))
1105 (min length
1106 (- array-dimension-limit length))))))
1107 (adjust-array vector (+ fill-pointer (max 1 min-extension)))))
1108 ;; disable bounds checking
1109 (locally (declare (optimize (safety 0)))
1110 (setf (aref vector fill-pointer) new-element))
1111 (setf (%array-fill-pointer vector) (1+ fill-pointer))
1112 fill-pointer))
1114 (defun vector-pop (array)
1115 #!+sb-doc
1116 "Decrease the fill pointer by 1 and return the element pointed to by the
1117 new fill pointer."
1118 (declare (explicit-check))
1119 (let ((fill-pointer (fill-pointer array)))
1120 (if (zerop fill-pointer)
1121 (error "There is nothing left to pop.")
1122 ;; disable bounds checking (and any fixnum test)
1123 (locally (declare (optimize (safety 0)))
1124 (aref array
1125 (setf (%array-fill-pointer array)
1126 (1- fill-pointer)))))))
1129 ;;;; ADJUST-ARRAY
1131 (defun adjust-array (array dimensions &key
1132 (element-type (array-element-type array) element-type-p)
1133 (initial-element nil initial-element-p)
1134 (initial-contents nil initial-contents-p)
1135 fill-pointer
1136 displaced-to displaced-index-offset)
1137 #!+sb-doc
1138 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
1139 (when (invalid-array-p array)
1140 (invalid-array-error array))
1141 (binding* ((dimensions (ensure-list dimensions))
1142 (array-rank (array-rank array))
1144 (unless (= (length dimensions) array-rank)
1145 (error "The number of dimensions not equal to rank of array.")))
1146 ((initialize initial-data)
1147 (validate-array-initargs initial-element-p initial-element
1148 initial-contents-p initial-contents
1149 displaced-to)))
1150 (cond ((and element-type-p
1151 (not (subtypep element-type (array-element-type array))))
1152 ;; This is weird. Should check upgraded type against actual
1153 ;; array element type I think. See lp#1331299. CLHS says that
1154 ;; "consequences are unspecified" so current behavior isn't wrong.
1155 (error "The new element type, ~S, is incompatible with old type."
1156 element-type))
1157 ((and fill-pointer (/= array-rank 1))
1158 (error "Only vectors can have fill pointers."))
1159 ((and fill-pointer (not (array-has-fill-pointer-p array)))
1160 ;; This case always struck me as odd. It seems like it might mean
1161 ;; that the user asks that the array gain a fill-pointer if it didn't
1162 ;; have one, yet CLHS is clear that the argument array must have a
1163 ;; fill-pointer or else signal a type-error.
1164 (fill-pointer-error array)))
1165 (cond (initial-contents-p
1166 ;; array former contents replaced by INITIAL-CONTENTS
1167 (let* ((array-size (apply #'* dimensions))
1168 (array-data (data-vector-from-inits
1169 dimensions array-size element-type nil nil
1170 initialize initial-data)))
1171 (if (adjustable-array-p array)
1172 (set-array-header array array-data array-size
1173 (get-new-fill-pointer array array-size
1174 fill-pointer)
1175 0 dimensions nil nil)
1176 (if (array-header-p array)
1177 ;; simple multidimensional or single dimensional array
1178 (make-array dimensions
1179 :element-type element-type
1180 :initial-contents initial-contents)
1181 array-data))))
1182 (displaced-to
1183 ;; We already established that no INITIAL-CONTENTS was supplied.
1184 (unless (or (eql element-type (array-element-type displaced-to))
1185 (subtypep element-type (array-element-type displaced-to)))
1186 ;; See lp#1331299 again. Require exact match on upgraded type?
1187 (error "can't displace an array of type ~S into another of ~
1188 type ~S"
1189 element-type (array-element-type displaced-to)))
1190 (let ((displacement (or displaced-index-offset 0))
1191 (array-size (apply #'* dimensions)))
1192 (declare (fixnum displacement array-size))
1193 (if (< (the fixnum (array-total-size displaced-to))
1194 (the fixnum (+ displacement array-size)))
1195 (error "The :DISPLACED-TO array is too small."))
1196 (if (adjustable-array-p array)
1197 ;; None of the original contents appear in adjusted array.
1198 (set-array-header array displaced-to array-size
1199 (get-new-fill-pointer array array-size
1200 fill-pointer)
1201 displacement dimensions t nil)
1202 ;; simple multidimensional or single dimensional array
1203 (make-array dimensions
1204 :element-type element-type
1205 :displaced-to displaced-to
1206 :displaced-index-offset
1207 displaced-index-offset))))
1208 ((= array-rank 1)
1209 (let ((old-length (array-total-size array))
1210 (new-length (car dimensions))
1211 new-data)
1212 (declare (fixnum old-length new-length))
1213 (with-array-data ((old-data array) (old-start)
1214 (old-end old-length))
1215 (cond ((or (and (array-header-p array)
1216 (%array-displaced-p array))
1217 (< old-length new-length))
1218 (setf new-data
1219 (data-vector-from-inits
1220 dimensions new-length element-type
1221 (%other-pointer-widetag old-data) nil
1222 initialize initial-data))
1223 ;; Provide :END1 to avoid full call to LENGTH
1224 ;; inside REPLACE.
1225 (replace new-data old-data
1226 :end1 new-length
1227 :start2 old-start :end2 old-end))
1228 (t (setf new-data
1229 (shrink-vector old-data new-length))))
1230 (if (adjustable-array-p array)
1231 (set-array-header array new-data new-length
1232 (get-new-fill-pointer array new-length
1233 fill-pointer)
1234 0 dimensions nil nil)
1235 new-data))))
1237 (let ((old-length (%array-available-elements array))
1238 (new-length (apply #'* dimensions)))
1239 (declare (fixnum old-length new-length))
1240 (with-array-data ((old-data array) (old-start)
1241 (old-end old-length))
1242 (declare (ignore old-end))
1243 (let ((new-data (if (or (and (array-header-p array)
1244 (%array-displaced-p array))
1245 (> new-length old-length)
1246 (not (adjustable-array-p array)))
1247 (data-vector-from-inits
1248 dimensions new-length
1249 element-type
1250 (%other-pointer-widetag old-data) nil
1251 (if initial-element-p :initial-element)
1252 initial-element)
1253 old-data)))
1254 (if (or (zerop old-length) (zerop new-length))
1255 (when initial-element-p (fill new-data initial-element))
1256 (zap-array-data old-data (array-dimensions array)
1257 old-start
1258 new-data dimensions new-length
1259 element-type initial-element
1260 initial-element-p))
1261 (if (adjustable-array-p array)
1262 (set-array-header array new-data new-length
1263 nil 0 dimensions nil nil)
1264 (let ((new-array
1265 (make-array-header
1266 sb!vm:simple-array-widetag array-rank)))
1267 (set-array-header new-array new-data new-length
1268 nil 0 dimensions nil t))))))))))
1271 (defun get-new-fill-pointer (old-array new-array-size fill-pointer)
1272 (declare (fixnum new-array-size))
1273 (typecase fill-pointer
1274 (null
1275 ;; "The consequences are unspecified if array is adjusted to a
1276 ;; size smaller than its fill pointer ..."
1277 (when (array-has-fill-pointer-p old-array)
1278 (when (> (%array-fill-pointer old-array) new-array-size)
1279 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
1280 smaller than its fill pointer (~S)"
1281 old-array new-array-size (fill-pointer old-array)))
1282 (%array-fill-pointer old-array)))
1283 ((eql t)
1284 new-array-size)
1285 (fixnum
1286 (when (> fill-pointer new-array-size)
1287 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
1288 than the new length of the vector (~S)"
1289 fill-pointer new-array-size)))))
1291 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
1292 ;;; which must be less than or equal to its current length. This can
1293 ;;; be called on vectors without a fill pointer but it is extremely
1294 ;;; dangerous to do so: shrinking the size of an object (as viewed by
1295 ;;; the gc) makes bounds checking unreliable in the face of interrupts
1296 ;;; or multi-threading. Call it only on provably local vectors.
1297 (defun %shrink-vector (vector new-length)
1298 (declare (vector vector))
1299 (unless (array-header-p vector)
1300 (macrolet ((frob (name &rest things)
1301 `(etypecase ,name
1302 ((simple-array nil (*)) (error 'nil-array-accessed-error))
1303 ,@(mapcar (lambda (thing)
1304 (destructuring-bind (type-spec fill-value)
1305 thing
1306 `(,type-spec
1307 (fill (truly-the ,type-spec ,name)
1308 ,fill-value
1309 :start new-length))))
1310 things))))
1311 ;; Set the 'tail' of the vector to the appropriate type of zero,
1312 ;; "because in some cases we'll scavenge larger areas in one go,
1313 ;; like groups of pages that had triggered the write barrier, or
1314 ;; the whole static space" according to jsnell.
1315 #.`(frob vector
1316 ,@(map 'list
1317 (lambda (saetp)
1318 `((simple-array ,(sb!vm:saetp-specifier saetp) (*))
1319 ,(if (or (eq (sb!vm:saetp-specifier saetp) 'character)
1320 #!+sb-unicode
1321 (eq (sb!vm:saetp-specifier saetp) 'base-char))
1322 *default-init-char-form*
1323 (sb!vm:saetp-initial-element-default saetp))))
1324 (remove-if-not
1325 #'sb!vm:saetp-specifier
1326 sb!vm:*specialized-array-element-type-properties*)))))
1327 ;; Only arrays have fill-pointers, but vectors have their length
1328 ;; parameter in the same place.
1329 (setf (%array-fill-pointer vector) new-length)
1330 vector)
1332 (defun shrink-vector (vector new-length)
1333 (declare (vector vector))
1334 (cond
1335 ((eq (length vector) new-length)
1336 vector)
1337 ((array-has-fill-pointer-p vector)
1338 (setf (%array-fill-pointer vector) new-length)
1339 vector)
1340 (t (subseq vector 0 new-length))))
1342 ;;; BIG THREAD SAFETY NOTE
1344 ;;; ADJUST-ARRAY/SET-ARRAY-HEADER, and its callees are very
1345 ;;; thread unsafe. They are nonatomic, and can mess with parallel
1346 ;;; code using the same arrays.
1348 ;;; A likely seeming fix is an additional level of indirection:
1349 ;;; ARRAY-HEADER -> ARRAY-INFO -> ... where ARRAY-HEADER would
1350 ;;; hold nothing but the pointer to ARRAY-INFO, and ARRAY-INFO
1351 ;;; would hold everything ARRAY-HEADER now holds. This allows
1352 ;;; consing up a new ARRAY-INFO and replacing it atomically in
1353 ;;; the ARRAY-HEADER.
1355 ;;; %WALK-DISPLACED-ARRAY-BACKPOINTERS is an especially nasty
1356 ;;; one: not only is it needed extremely rarely, which makes
1357 ;;; any thread safety bugs involving it look like rare random
1358 ;;; corruption, but because it walks the chain *upwards*, which
1359 ;;; may violate user expectations.
1361 ;;; Fill in array header with the provided information, and return the array.
1362 (defun set-array-header (array data length fill-pointer displacement dimensions
1363 displacedp newp)
1364 (labels ((%walk-displaced-array-backpointers (array new-length)
1365 (dolist (p (%array-displaced-from array))
1366 (let ((from (weak-pointer-value p)))
1367 (when (and from (eq array (%array-data-vector from)))
1368 (let ((requires (+ (%array-available-elements from)
1369 (%array-displacement from))))
1370 (unless (>= new-length requires)
1371 ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
1373 ;; "If A is displaced to B, the consequences are unspecified if B is
1374 ;; adjusted in such a way that it no longer has enough elements to
1375 ;; satisfy A.
1377 ;; since we're hanging on a weak pointer here, we can't signal an
1378 ;; error right now: the array that we're looking at might be
1379 ;; garbage. Instead, we set all dimensions to zero so that next
1380 ;; safe access to the displaced array will trap. Additionally, we
1381 ;; save the original dimensions, so we can signal a more
1382 ;; understandable error when the time comes.
1383 (%walk-displaced-array-backpointers from 0)
1384 (setf (%array-fill-pointer from) 0
1385 (%array-available-elements from) 0
1386 (%array-displaced-p from) (array-dimensions array))
1387 (dotimes (i (%array-rank from))
1388 (setf (%array-dimension from i) 0)))))))))
1389 (if newp
1390 (setf (%array-displaced-from array) nil)
1391 (%walk-displaced-array-backpointers array length))
1392 (when displacedp
1393 (%save-displaced-array-backpointer array data))
1394 (setf (%array-data-vector array) data)
1395 (setf (%array-available-elements array) length)
1396 (cond (fill-pointer
1397 (setf (%array-fill-pointer array) fill-pointer)
1398 (setf (%array-fill-pointer-p array) t))
1400 (setf (%array-fill-pointer array) length)
1401 (setf (%array-fill-pointer-p array) nil)))
1402 (setf (%array-displacement array) displacement)
1403 (if (listp dimensions)
1404 (dotimes (axis (array-rank array))
1405 (declare (type index axis))
1406 (setf (%array-dimension array axis) (pop dimensions)))
1407 (setf (%array-dimension array 0) dimensions))
1408 (setf (%array-displaced-p array) displacedp)
1409 array))
1411 ;;; User visible extension
1412 (declaim (ftype (function (array) (values (simple-array * (*)) &optional))
1413 array-storage-vector))
1414 (defun array-storage-vector (array)
1415 #!+sb-doc
1416 "Returns the underlying storage vector of ARRAY, which must be a non-displaced array.
1418 In SBCL, if ARRAY is a of type \(SIMPLE-ARRAY * \(*)), it is its own storage
1419 vector. Multidimensional arrays, arrays with fill pointers, and adjustable
1420 arrays have an underlying storage vector with the same ARRAY-ELEMENT-TYPE as
1421 ARRAY, which this function returns.
1423 Important note: the underlying vector is an implementation detail. Even though
1424 this function exposes it, changes in the implementation may cause this
1425 function to be removed without further warning."
1426 ;; KLUDGE: Without TRULY-THE the system is not smart enough to figure out that
1427 ;; the return value is always of the known type.
1428 (truly-the (simple-array * (*))
1429 (if (array-header-p array)
1430 (if (%array-displaced-p array)
1431 (error "~S cannot be used with displaced arrays. Use ~S instead."
1432 'array-storage-vector 'array-displacement)
1433 (%array-data-vector array))
1434 array)))
1437 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
1439 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
1440 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
1441 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
1442 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
1443 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
1444 element-type initial-element initial-element-p)
1445 (declare (list old-dims new-dims)
1446 (fixnum new-length))
1447 ;; OLD-DIMS comes from array-dimensions, which returns a fresh list
1448 ;; at least in SBCL.
1449 ;; NEW-DIMS comes from the user.
1450 (setf old-dims (nreverse old-dims)
1451 new-dims (reverse new-dims))
1452 (cond ((eq old-data new-data)
1453 ;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and
1454 ;; INITIAL-ELEMENT-P are used when OLD-DATA and NEW-DATA are
1455 ;; EQ; in this case, a temporary must be used and filled
1456 ;; appropriately. specified initial-element.
1457 (when initial-element-p
1458 ;; FIXME: transforming this TYPEP to someting a bit faster
1459 ;; would be a win...
1460 (unless (typep initial-element element-type)
1461 (error "~S can't be used to initialize an array of type ~S."
1462 initial-element element-type)))
1463 (let ((temp (if initial-element-p
1464 (make-array new-length :initial-element initial-element)
1465 (make-array new-length))))
1466 (declare (simple-vector temp))
1467 (zap-array-data-aux old-data old-dims offset temp new-dims)
1468 (dotimes (i new-length)
1469 (setf (aref new-data i) (aref temp i)))
1470 ;; Kill the temporary vector to prevent garbage retention.
1471 (%shrink-vector temp 0)))
1473 ;; When OLD-DATA and NEW-DATA are not EQ, NEW-DATA has
1474 ;; already been filled with any
1475 (zap-array-data-aux old-data old-dims offset new-data new-dims))))
1477 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims)
1478 (declare (fixnum offset))
1479 (let ((limits (mapcar (lambda (x y)
1480 (declare (fixnum x y))
1481 (1- (the fixnum (min x y))))
1482 old-dims new-dims)))
1483 (macrolet ((bump-index-list (index limits)
1484 `(do ((subscripts ,index (cdr subscripts))
1485 (limits ,limits (cdr limits)))
1486 ((null subscripts) :eof)
1487 (cond ((< (the fixnum (car subscripts))
1488 (the fixnum (car limits)))
1489 (rplaca subscripts
1490 (1+ (the fixnum (car subscripts))))
1491 (return ,index))
1492 (t (rplaca subscripts 0))))))
1493 (do ((index (make-list (length old-dims) :initial-element 0)
1494 (bump-index-list index limits)))
1495 ((eq index :eof))
1496 (setf (aref new-data (row-major-index-from-dims index new-dims))
1497 (aref old-data
1498 (+ (the fixnum (row-major-index-from-dims index old-dims))
1499 offset)))))))
1501 ;;; Figure out the row-major-order index of an array reference from a
1502 ;;; list of subscripts and a list of dimensions. This is for internal
1503 ;;; calls only, and the subscripts and dim-list variables are assumed
1504 ;;; to be reversed from what the user supplied.
1505 (defun row-major-index-from-dims (rev-subscripts rev-dim-list)
1506 (do ((rev-subscripts rev-subscripts (cdr rev-subscripts))
1507 (rev-dim-list rev-dim-list (cdr rev-dim-list))
1508 (chunk-size 1)
1509 (result 0))
1510 ((null rev-dim-list) result)
1511 (declare (fixnum chunk-size result))
1512 (setq result (+ result
1513 (the fixnum (* (the fixnum (car rev-subscripts))
1514 chunk-size))))
1515 (setq chunk-size (* chunk-size (the fixnum (car rev-dim-list))))))
1517 ;;;; some bit stuff
1519 (defun bit-array-same-dimensions-p (array1 array2)
1520 (declare (type (array bit) array1 array2))
1521 (and (= (array-rank array1)
1522 (array-rank array2))
1523 (dotimes (index (array-rank array1) t)
1524 (when (/= (array-dimension array1 index)
1525 (array-dimension array2 index))
1526 (return nil)))))
1528 (defun pick-result-array (result-bit-array bit-array-1)
1529 (case result-bit-array
1530 ((t) bit-array-1)
1531 ((nil) (make-array (array-dimensions bit-array-1)
1532 :element-type 'bit
1533 :initial-element 0))
1535 (unless (bit-array-same-dimensions-p bit-array-1
1536 result-bit-array)
1537 (error "~S and ~S don't have the same dimensions."
1538 bit-array-1 result-bit-array))
1539 result-bit-array)))
1541 (defmacro def-bit-array-op (name function)
1542 `(defun ,name (bit-array-1 bit-array-2 &optional result-bit-array)
1543 #!+sb-doc
1544 ,(format nil
1545 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1546 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1547 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1548 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1549 All the arrays must have the same rank and dimensions."
1550 (symbol-name function))
1551 (declare (type (array bit) bit-array-1 bit-array-2)
1552 (type (or (array bit) (member t nil)) result-bit-array))
1553 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2)
1554 (error "~S and ~S don't have the same dimensions."
1555 bit-array-1 bit-array-2))
1556 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1)))
1557 (if (and (simple-bit-vector-p bit-array-1)
1558 (simple-bit-vector-p bit-array-2)
1559 (simple-bit-vector-p result-bit-array))
1560 (locally (declare (optimize (speed 3) (safety 0)))
1561 (,name bit-array-1 bit-array-2 result-bit-array))
1562 (with-array-data ((data1 bit-array-1) (start1) (end1))
1563 (declare (ignore end1))
1564 (with-array-data ((data2 bit-array-2) (start2) (end2))
1565 (declare (ignore end2))
1566 (with-array-data ((data3 result-bit-array) (start3) (end3))
1567 (do ((index-1 start1 (1+ index-1))
1568 (index-2 start2 (1+ index-2))
1569 (index-3 start3 (1+ index-3)))
1570 ((>= index-3 end3) result-bit-array)
1571 (declare (type index index-1 index-2 index-3))
1572 (setf (sbit data3 index-3)
1573 (logand (,function (sbit data1 index-1)
1574 (sbit data2 index-2))
1575 1))))))))))
1577 (def-bit-array-op bit-and logand)
1578 (def-bit-array-op bit-ior logior)
1579 (def-bit-array-op bit-xor logxor)
1580 (def-bit-array-op bit-eqv logeqv)
1581 (def-bit-array-op bit-nand lognand)
1582 (def-bit-array-op bit-nor lognor)
1583 (def-bit-array-op bit-andc1 logandc1)
1584 (def-bit-array-op bit-andc2 logandc2)
1585 (def-bit-array-op bit-orc1 logorc1)
1586 (def-bit-array-op bit-orc2 logorc2)
1588 (defun bit-not (bit-array &optional result-bit-array)
1589 #!+sb-doc
1590 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1591 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1592 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1593 created. Both arrays must have the same rank and dimensions."
1594 (declare (type (array bit) bit-array)
1595 (type (or (array bit) (member t nil)) result-bit-array))
1596 (let ((result-bit-array (pick-result-array result-bit-array bit-array)))
1597 (if (and (simple-bit-vector-p bit-array)
1598 (simple-bit-vector-p result-bit-array))
1599 (locally (declare (optimize (speed 3) (safety 0)))
1600 (bit-not bit-array result-bit-array))
1601 (with-array-data ((src bit-array) (src-start) (src-end))
1602 (declare (ignore src-end))
1603 (with-array-data ((dst result-bit-array) (dst-start) (dst-end))
1604 (do ((src-index src-start (1+ src-index))
1605 (dst-index dst-start (1+ dst-index)))
1606 ((>= dst-index dst-end) result-bit-array)
1607 (declare (type index src-index dst-index))
1608 (setf (sbit dst dst-index)
1609 (logxor (sbit src src-index) 1))))))))
1611 ;;;; array type dispatching
1613 ;;; Given DISPATCH-FOO as the DISPATCH-NAME argument (unevaluated),
1614 ;;; defines the functions
1616 ;;; DISPATCH-FOO/SIMPLE-BASE-STRING
1617 ;;; DISPATCH-FOO/SIMPLE-CHARACTER-STRING
1618 ;;; DISPATCH-FOO/SIMPLE-ARRAY-SINGLE-FLOAT
1619 ;;; ...
1621 ;;; PARAMS are the function parameters in the definition of each
1622 ;;; specializer function. The array being specialized must be the
1623 ;;; first parameter in PARAMS. A type declaration for this parameter
1624 ;;; is automatically inserted into the body of each function.
1626 ;;; The dispatch table %%FOO-FUNS%% is defined and populated by these
1627 ;;; functions. The table is padded by the function
1628 ;;; HAIRY-FOO-DISPATCH-ERROR, also defined by DEFINE-ARRAY-DISPATCH.
1630 ;;; Finally, the DISPATCH-FOO macro is defined which does the actual
1631 ;;; dispatching when called. It expects arguments that match PARAMS.
1633 (defmacro !define-array-dispatch (dispatch-name params &body body)
1634 (let ((table-name (symbolicate "%%" dispatch-name "-FUNS%%"))
1635 (error-name (symbolicate "HAIRY-" dispatch-name "-ERROR")))
1636 `(progn
1637 (eval-when (:compile-toplevel :load-toplevel :execute)
1638 (defun ,error-name (&rest args)
1639 (error 'type-error
1640 :datum (first args)
1641 :expected-type '(simple-array * (*)))))
1642 (!defglobal ,table-name (make-array ,(1+ sb!vm:widetag-mask)
1643 :initial-element #',error-name))
1644 ,@(loop for info across sb!vm:*specialized-array-element-type-properties*
1645 for typecode = (sb!vm:saetp-typecode info)
1646 for specifier = (sb!vm:saetp-specifier info)
1647 for primitive-type-name = (sb!vm:saetp-primitive-type-name info)
1648 collect (let ((fun-name (symbolicate (string dispatch-name)
1649 "/" primitive-type-name)))
1650 `(progn
1651 (defun ,fun-name ,params
1652 (declare (type (simple-array ,specifier (*))
1653 ,(first params)))
1654 ,@body)
1655 (setf (svref ,table-name ,typecode) #',fun-name))))
1656 (defmacro ,dispatch-name (&rest args)
1657 (check-type (first args) symbol)
1658 (let ((tag (gensym "TAG")))
1659 `(funcall
1660 (the function
1661 (let ((,tag 0))
1662 (when (sb!vm::%other-pointer-p ,(first args))
1663 (setf ,tag (%other-pointer-widetag ,(first args))))
1664 (svref ,',table-name ,tag)))
1665 ,@args))))))