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