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