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