Fix build with CLL and CLisp hosts
[sbcl.git] / src / code / array.lisp
blob8dfa801430d0be9bd51e70f4dcfb294ca9f98d1e
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 subscripts
774 &optional (invalid-index-error-p t))
775 (declare (array array)
776 (list subscripts))
777 (let ((rank (array-rank array)))
778 (unless (= rank (length subscripts))
779 (error "wrong number of subscripts, ~W, for array of rank ~W"
780 (length subscripts) rank))
781 (if (array-header-p array)
782 (do ((subs (nreverse subscripts) (cdr subs))
783 (axis (1- (array-rank array)) (1- axis))
784 (chunk-size 1)
785 (result 0))
786 ((null subs) result)
787 (declare (list subs) (fixnum axis chunk-size result))
788 (let ((index (car subs))
789 (dim (%array-dimension array axis)))
790 (declare (fixnum dim))
791 (unless (and (fixnump index) (< -1 index dim))
792 (if invalid-index-error-p
793 (invalid-array-index-error array index dim axis)
794 (return-from %array-row-major-index nil)))
795 (incf result (* chunk-size (the fixnum index)))
796 (setf chunk-size (* chunk-size dim))))
797 (let ((index (first subscripts))
798 (length (length (the (simple-array * (*)) array))))
799 (unless (and (fixnump index) (< -1 index length))
800 (if invalid-index-error-p
801 (invalid-array-index-error array index length)
802 (return-from %array-row-major-index nil)))
803 index))))
805 (defun array-in-bounds-p (array &rest subscripts)
806 #!+sb-doc
807 "Return T if the SUBSCRIPTS are in bounds for the ARRAY, NIL otherwise."
808 (if (%array-row-major-index array subscripts nil)
811 (defun array-row-major-index (array &rest subscripts)
812 (declare (truly-dynamic-extent subscripts))
813 (%array-row-major-index array subscripts))
815 (defun aref (array &rest subscripts)
816 #!+sb-doc
817 "Return the element of the ARRAY specified by the SUBSCRIPTS."
818 (declare (truly-dynamic-extent subscripts))
819 (row-major-aref array (%array-row-major-index array subscripts)))
821 ;;; (setf aref/bit/sbit) are implemented using setf-functions,
822 ;;; because they have to work with (setf (apply #'aref array subscripts))
823 ;;; All other setfs can be done using setf-functions too, but I
824 ;;; haven't found technical advantages or disatvantages for either
825 ;;; scheme.
826 (defun (setf aref) (new-value array &rest subscripts)
827 (declare (truly-dynamic-extent subscripts)
828 (type array array))
829 (setf (row-major-aref array (%array-row-major-index array subscripts))
830 new-value))
832 (defun row-major-aref (array index)
833 #!+sb-doc
834 "Return the element of array corresponding to the row-major index. This is
835 SETFable."
836 (declare (optimize (safety 1)))
837 (row-major-aref array index))
839 (defun %set-row-major-aref (array index new-value)
840 (declare (optimize (safety 1)))
841 (setf (row-major-aref array index) new-value))
843 (defun svref (simple-vector index)
844 #!+sb-doc
845 "Return the INDEXth element of the given Simple-Vector."
846 (declare (optimize (safety 1)))
847 (aref simple-vector index))
849 (defun %svset (simple-vector index new)
850 (declare (optimize (safety 1)))
851 (setf (aref simple-vector index) new))
853 (defun bit (bit-array &rest subscripts)
854 #!+sb-doc
855 "Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
856 (declare (type (array bit) bit-array)
857 (truly-dynamic-extent subscripts)
858 (optimize (safety 1)))
859 (row-major-aref bit-array (%array-row-major-index bit-array subscripts)))
861 (defun (setf bit) (new-value bit-array &rest subscripts)
862 (declare (type (array bit) bit-array)
863 (type bit new-value)
864 (truly-dynamic-extent subscripts)
865 (optimize (safety 1)))
866 (setf (row-major-aref bit-array
867 (%array-row-major-index bit-array subscripts))
868 new-value))
870 (defun sbit (simple-bit-array &rest subscripts)
871 #!+sb-doc
872 "Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
873 (declare (type (simple-array bit) simple-bit-array)
874 (truly-dynamic-extent subscripts)
875 (optimize (safety 1)))
876 (row-major-aref simple-bit-array
877 (%array-row-major-index simple-bit-array subscripts)))
879 (defun (setf sbit) (new-value bit-array &rest subscripts)
880 (declare (type (simple-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 (%array-row-major-index bit-array subscripts))
886 new-value))
888 ;;;; miscellaneous array properties
890 (defun array-element-type (array)
891 #!+sb-doc
892 "Return the type of the elements of the array"
893 (let ((widetag (%other-pointer-widetag array)))
894 (macrolet ((pick-element-type (&rest stuff)
895 `(cond ,@(mapcar (lambda (stuff)
896 (cons
897 (let ((item (car stuff)))
898 (cond ((eq item t)
900 ((listp item)
901 (cons 'or
902 (mapcar (lambda (x)
903 `(= widetag ,x))
904 item)))
906 `(= widetag ,item))))
907 (cdr stuff)))
908 stuff))))
909 #.`(pick-element-type
910 ,@(map 'list
911 (lambda (saetp)
912 `(,(if (sb!vm:saetp-complex-typecode saetp)
913 (list (sb!vm:saetp-typecode saetp)
914 (sb!vm:saetp-complex-typecode saetp))
915 (sb!vm:saetp-typecode saetp))
916 ',(sb!vm:saetp-specifier saetp)))
917 sb!vm:*specialized-array-element-type-properties*)
918 ((sb!vm:simple-array-widetag
919 sb!vm:complex-vector-widetag
920 sb!vm:complex-array-widetag)
921 (with-array-data ((array array) (start) (end))
922 (declare (ignore start end))
923 (array-element-type array)))
925 (error 'type-error :datum array :expected-type 'array))))))
927 (defun array-rank (array)
928 #!+sb-doc
929 "Return the number of dimensions of ARRAY."
930 (if (array-header-p array)
931 (%array-rank array)
934 (defun array-dimension (array axis-number)
935 #!+sb-doc
936 "Return the length of dimension AXIS-NUMBER of ARRAY."
937 (declare (array array) (type index axis-number))
938 (cond ((not (array-header-p array))
939 (unless (= axis-number 0)
940 (error "Vector axis is not zero: ~S" axis-number))
941 (length (the (simple-array * (*)) array)))
942 ((>= axis-number (%array-rank array))
943 (error "Axis number ~W is too big; ~S only has ~D dimension~:P."
944 axis-number array (%array-rank array)))
946 (%array-dimension array axis-number))))
948 (defun array-dimensions (array)
949 #!+sb-doc
950 "Return a list whose elements are the dimensions of the array"
951 (declare (array array))
952 (if (array-header-p array)
953 (do ((results nil (cons (array-dimension array index) results))
954 (index (1- (array-rank array)) (1- index)))
955 ((minusp index) results))
956 (list (array-dimension array 0))))
958 (defun array-total-size (array)
959 #!+sb-doc
960 "Return the total number of elements in the Array."
961 (declare (array array))
962 (if (array-header-p array)
963 (%array-available-elements array)
964 (length (the vector array))))
966 (defun array-displacement (array)
967 #!+sb-doc
968 "Return the values of :DISPLACED-TO and :DISPLACED-INDEX-offset
969 options to MAKE-ARRAY, or NIL and 0 if not a displaced array."
970 (declare (type array array))
971 (if (and (array-header-p array) ; if unsimple and
972 (%array-displaced-p array)) ; displaced
973 (values (%array-data-vector array) (%array-displacement array))
974 (values nil 0)))
976 (defun adjustable-array-p (array)
977 #!+sb-doc
978 "Return T if (ADJUST-ARRAY ARRAY...) would return an array identical
979 to the argument, this happens for complex arrays."
980 (declare (array array))
981 ;; Note that this appears not to be a fundamental limitation.
982 ;; non-vector SIMPLE-ARRAYs are in fact capable of being adjusted,
983 ;; but in practice we test using ADJUSTABLE-ARRAY-P in ADJUST-ARRAY.
984 ;; -- CSR, 2004-03-01.
985 (not (typep array 'simple-array)))
987 ;;;; fill pointer frobbing stuff
989 (declaim (inline array-has-fill-pointer-p))
990 (defun array-has-fill-pointer-p (array)
991 #!+sb-doc
992 "Return T if the given ARRAY has a fill pointer, or NIL otherwise."
993 (declare (array array))
994 (and (array-header-p array) (%array-fill-pointer-p array)))
996 (defun fill-pointer-error (vector &optional arg)
997 (cond (arg
998 (aver (array-has-fill-pointer-p vector))
999 (let ((max (%array-available-elements vector)))
1000 (error 'simple-type-error
1001 :datum arg
1002 :expected-type (list 'integer 0 max)
1003 :format-control "The new fill pointer, ~S, is larger than the length of the vector (~S.)"
1004 :format-arguments (list arg max))))
1006 (error 'simple-type-error
1007 :datum vector
1008 :expected-type '(and vector (satisfies array-has-fill-pointer-p))
1009 :format-control "~S is not an array with a fill pointer."
1010 :format-arguments (list vector)))))
1012 (declaim (inline fill-pointer))
1013 (defun fill-pointer (vector)
1014 #!+sb-doc
1015 "Return the FILL-POINTER of the given VECTOR."
1016 (if (array-has-fill-pointer-p vector)
1017 (%array-fill-pointer vector)
1018 (fill-pointer-error vector)))
1020 (defun %set-fill-pointer (vector new)
1021 (flet ((oops (x)
1022 (fill-pointer-error vector x)))
1023 (if (array-has-fill-pointer-p vector)
1024 (if (> new (%array-available-elements vector))
1025 (oops new)
1026 (setf (%array-fill-pointer vector) new))
1027 (oops nil))))
1029 ;;; FIXME: It'd probably make sense to use a MACROLET to share the
1030 ;;; guts of VECTOR-PUSH between VECTOR-PUSH-EXTEND. Such a macro
1031 ;;; should probably be based on the VECTOR-PUSH-EXTEND code (which is
1032 ;;; new ca. sbcl-0.7.0) rather than the VECTOR-PUSH code (which dates
1033 ;;; back to CMU CL).
1034 (defun vector-push (new-element array)
1035 #!+sb-doc
1036 "Attempt to set the element of ARRAY designated by its fill pointer
1037 to NEW-ELEMENT, and increment the fill pointer by one. If the fill pointer is
1038 too large, NIL is returned, otherwise the index of the pushed element is
1039 returned."
1040 (let ((fill-pointer (fill-pointer array)))
1041 (declare (fixnum fill-pointer))
1042 (cond ((= fill-pointer (%array-available-elements array))
1043 nil)
1045 (locally (declare (optimize (safety 0)))
1046 (setf (aref array fill-pointer) new-element))
1047 (setf (%array-fill-pointer array) (1+ fill-pointer))
1048 fill-pointer))))
1050 (defun vector-push-extend (new-element vector &optional min-extension)
1051 (declare (type (or null fixnum) min-extension))
1052 (let ((fill-pointer (fill-pointer vector)))
1053 (declare (fixnum fill-pointer))
1054 (when (= fill-pointer (%array-available-elements vector))
1055 (let ((min-extension
1056 (or min-extension
1057 (let ((length (length vector)))
1058 (min (1+ length)
1059 (- array-dimension-limit length))))))
1060 (adjust-array vector (+ fill-pointer (max 1 min-extension)))))
1061 ;; disable bounds checking
1062 (locally (declare (optimize (safety 0)))
1063 (setf (aref vector fill-pointer) new-element))
1064 (setf (%array-fill-pointer vector) (1+ fill-pointer))
1065 fill-pointer))
1067 (defun vector-pop (array)
1068 #!+sb-doc
1069 "Decrease the fill pointer by 1 and return the element pointed to by the
1070 new fill pointer."
1071 (let ((fill-pointer (fill-pointer array)))
1072 (declare (fixnum fill-pointer))
1073 (if (zerop fill-pointer)
1074 (error "There is nothing left to pop.")
1075 ;; disable bounds checking (and any fixnum test)
1076 (locally (declare (optimize (safety 0)))
1077 (aref array
1078 (setf (%array-fill-pointer array)
1079 (1- fill-pointer)))))))
1082 ;;;; ADJUST-ARRAY
1084 (defun adjust-array (array dimensions &key
1085 (element-type (array-element-type array) element-type-p)
1086 (initial-element nil initial-element-p)
1087 (initial-contents nil initial-contents-p)
1088 fill-pointer
1089 displaced-to displaced-index-offset)
1090 #!+sb-doc
1091 "Adjust ARRAY's dimensions to the given DIMENSIONS and stuff."
1092 (when (invalid-array-p array)
1093 (invalid-array-error array))
1094 (binding* ((dimensions (if (listp dimensions) dimensions (list dimensions)))
1095 (array-rank (array-rank array))
1097 (unless (= (length dimensions) array-rank)
1098 (error "The number of dimensions not equal to rank of array.")))
1099 ((initialize initial-data)
1100 (validate-array-initargs initial-element-p initial-element
1101 initial-contents-p initial-contents
1102 displaced-to)))
1103 (cond ((and element-type-p
1104 (not (subtypep element-type (array-element-type array))))
1105 ;; This is weird. Should check upgraded type against actual
1106 ;; array element type I think. See lp#1331299. CLHS says that
1107 ;; "consequences are unspecified" so current behavior isn't wrong.
1108 (error "The new element type, ~S, is incompatible with old type."
1109 element-type))
1110 ((and fill-pointer (/= array-rank 1))
1111 (error "Only vectors can have fill pointers."))
1112 ((and fill-pointer (not (array-has-fill-pointer-p array)))
1113 ;; This case always struck me as odd. It seems like it might mean
1114 ;; that the user asks that the array gain a fill-pointer if it didn't
1115 ;; have one, yet CLHS is clear that the argument array must have a
1116 ;; fill-pointer or else signal a type-error.
1117 (fill-pointer-error array)))
1118 (cond (initial-contents-p
1119 ;; array former contents replaced by INITIAL-CONTENTS
1120 (let* ((array-size (apply #'* dimensions))
1121 (array-data (data-vector-from-inits
1122 dimensions array-size element-type nil nil
1123 initialize initial-data)))
1124 (if (adjustable-array-p array)
1125 (set-array-header array array-data array-size
1126 (get-new-fill-pointer array array-size
1127 fill-pointer)
1128 0 dimensions nil nil)
1129 (if (array-header-p array)
1130 ;; simple multidimensional or single dimensional array
1131 (make-array dimensions
1132 :element-type element-type
1133 :initial-contents initial-contents)
1134 array-data))))
1135 (displaced-to
1136 ;; We already established that no INITIAL-CONTENTS was supplied.
1137 (unless (subtypep element-type (array-element-type displaced-to))
1138 ;; See lp#1331299 again. Require exact match on upgraded type?
1139 (error "can't displace an array of type ~S into another of ~
1140 type ~S"
1141 element-type (array-element-type displaced-to)))
1142 (let ((displacement (or displaced-index-offset 0))
1143 (array-size (apply #'* dimensions)))
1144 (declare (fixnum displacement array-size))
1145 (if (< (the fixnum (array-total-size displaced-to))
1146 (the fixnum (+ displacement array-size)))
1147 (error "The :DISPLACED-TO array is too small."))
1148 (if (adjustable-array-p array)
1149 ;; None of the original contents appear in adjusted array.
1150 (set-array-header array displaced-to array-size
1151 (get-new-fill-pointer array array-size
1152 fill-pointer)
1153 displacement dimensions t nil)
1154 ;; simple multidimensional or single dimensional array
1155 (make-array dimensions
1156 :element-type element-type
1157 :displaced-to displaced-to
1158 :displaced-index-offset
1159 displaced-index-offset))))
1160 ((= array-rank 1)
1161 (let ((old-length (array-total-size array))
1162 (new-length (car dimensions))
1163 new-data)
1164 (declare (fixnum old-length new-length))
1165 (with-array-data ((old-data array) (old-start)
1166 (old-end old-length))
1167 (cond ((or (and (array-header-p array)
1168 (%array-displaced-p array))
1169 (< old-length new-length))
1170 (setf new-data
1171 (data-vector-from-inits
1172 dimensions new-length element-type
1173 (%other-pointer-widetag old-data) nil
1174 initialize initial-data))
1175 ;; Provide :END1 to avoid full call to LENGTH
1176 ;; inside REPLACE.
1177 (replace new-data old-data
1178 :end1 new-length
1179 :start2 old-start :end2 old-end))
1180 (t (setf new-data
1181 (shrink-vector old-data new-length))))
1182 (if (adjustable-array-p array)
1183 (set-array-header array new-data new-length
1184 (get-new-fill-pointer array new-length
1185 fill-pointer)
1186 0 dimensions nil nil)
1187 new-data))))
1189 (let ((old-length (%array-available-elements array))
1190 (new-length (apply #'* dimensions)))
1191 (declare (fixnum old-length new-length))
1192 (with-array-data ((old-data array) (old-start)
1193 (old-end old-length))
1194 (declare (ignore old-end))
1195 (let ((new-data (if (or (and (array-header-p array)
1196 (%array-displaced-p array))
1197 (> new-length old-length)
1198 (not (adjustable-array-p array)))
1199 (data-vector-from-inits
1200 dimensions new-length
1201 element-type
1202 (%other-pointer-widetag old-data) nil
1203 (if initial-element-p :initial-element)
1204 initial-element)
1205 old-data)))
1206 (if (or (zerop old-length) (zerop new-length))
1207 (when initial-element-p (fill new-data initial-element))
1208 (zap-array-data old-data (array-dimensions array)
1209 old-start
1210 new-data dimensions new-length
1211 element-type initial-element
1212 initial-element-p))
1213 (if (adjustable-array-p array)
1214 (set-array-header array new-data new-length
1215 nil 0 dimensions nil nil)
1216 (let ((new-array
1217 (make-array-header
1218 sb!vm:simple-array-widetag array-rank)))
1219 (set-array-header new-array new-data new-length
1220 nil 0 dimensions nil t))))))))))
1223 (defun get-new-fill-pointer (old-array new-array-size fill-pointer)
1224 (cond ((not fill-pointer)
1225 ;; "The consequences are unspecified if array is adjusted to a
1226 ;; size smaller than its fill pointer ..."
1227 (when (array-has-fill-pointer-p old-array)
1228 (when (> (%array-fill-pointer old-array) new-array-size)
1229 (error "cannot ADJUST-ARRAY an array (~S) to a size (~S) that is ~
1230 smaller than its fill pointer (~S)"
1231 old-array new-array-size (fill-pointer old-array)))
1232 (%array-fill-pointer old-array)))
1233 ((numberp fill-pointer)
1234 (when (> fill-pointer new-array-size)
1235 (error "can't supply a value for :FILL-POINTER (~S) that is larger ~
1236 than the new length of the vector (~S)"
1237 fill-pointer new-array-size))
1238 fill-pointer)
1239 ((eq fill-pointer t)
1240 new-array-size)))
1242 ;;; Destructively alter VECTOR, changing its length to NEW-LENGTH,
1243 ;;; which must be less than or equal to its current length. This can
1244 ;;; be called on vectors without a fill pointer but it is extremely
1245 ;;; dangerous to do so: shrinking the size of an object (as viewed by
1246 ;;; the gc) makes bounds checking unreliable in the face of interrupts
1247 ;;; or multi-threading. Call it only on provably local vectors.
1248 (defun %shrink-vector (vector new-length)
1249 (declare (vector vector))
1250 (unless (array-header-p vector)
1251 (macrolet ((frob (name &rest things)
1252 `(etypecase ,name
1253 ((simple-array nil (*)) (error 'nil-array-accessed-error))
1254 ,@(mapcar (lambda (thing)
1255 (destructuring-bind (type-spec fill-value)
1256 thing
1257 `(,type-spec
1258 (fill (truly-the ,type-spec ,name)
1259 ,fill-value
1260 :start new-length))))
1261 things))))
1262 ;; Set the 'tail' of the vector to the appropriate type of zero,
1263 ;; "because in some cases we'll scavenge larger areas in one go,
1264 ;; like groups of pages that had triggered the write barrier, or
1265 ;; the whole static space" according to jsnell.
1266 #.`(frob vector
1267 ,@(map 'list
1268 (lambda (saetp)
1269 `((simple-array ,(sb!vm:saetp-specifier saetp) (*))
1270 ,(if (or (eq (sb!vm:saetp-specifier saetp) 'character)
1271 #!+sb-unicode
1272 (eq (sb!vm:saetp-specifier saetp) 'base-char))
1273 *default-init-char-form*
1274 (sb!vm:saetp-initial-element-default saetp))))
1275 (remove-if-not
1276 #'sb!vm:saetp-specifier
1277 sb!vm:*specialized-array-element-type-properties*)))))
1278 ;; Only arrays have fill-pointers, but vectors have their length
1279 ;; parameter in the same place.
1280 (setf (%array-fill-pointer vector) new-length)
1281 vector)
1283 (defun shrink-vector (vector new-length)
1284 (declare (vector vector))
1285 (cond
1286 ((eq (length vector) new-length)
1287 vector)
1288 ((array-has-fill-pointer-p vector)
1289 (setf (%array-fill-pointer vector) new-length)
1290 vector)
1291 (t (subseq vector 0 new-length))))
1293 ;;; BIG THREAD SAFETY NOTE
1295 ;;; ADJUST-ARRAY/SET-ARRAY-HEADER, and its callees are very
1296 ;;; thread unsafe. They are nonatomic, and can mess with parallel
1297 ;;; code using the same arrays.
1299 ;;; A likely seeming fix is an additional level of indirection:
1300 ;;; ARRAY-HEADER -> ARRAY-INFO -> ... where ARRAY-HEADER would
1301 ;;; hold nothing but the pointer to ARRAY-INFO, and ARRAY-INFO
1302 ;;; would hold everything ARRAY-HEADER now holds. This allows
1303 ;;; consing up a new ARRAY-INFO and replacing it atomically in
1304 ;;; the ARRAY-HEADER.
1306 ;;; %WALK-DISPLACED-ARRAY-BACKPOINTERS is an especially nasty
1307 ;;; one: not only is it needed extremely rarely, which makes
1308 ;;; any thread safety bugs involving it look like rare random
1309 ;;; corruption, but because it walks the chain *upwards*, which
1310 ;;; may violate user expectations.
1312 (defun %save-displaced-array-backpointer (array data)
1313 (flet ((purge (pointers)
1314 (remove-if (lambda (value)
1315 (or (not value) (eq array value)))
1316 pointers
1317 :key #'weak-pointer-value)))
1318 ;; Add backpointer to the new data vector if it has a header.
1319 (when (array-header-p data)
1320 (setf (%array-displaced-from data)
1321 (cons (make-weak-pointer array)
1322 (purge (%array-displaced-from data)))))
1323 ;; Remove old backpointer, if any.
1324 (let ((old-data (%array-data-vector array)))
1325 (when (and (neq data old-data) (array-header-p old-data))
1326 (setf (%array-displaced-from old-data)
1327 (purge (%array-displaced-from old-data)))))))
1329 (defun %walk-displaced-array-backpointers (array new-length)
1330 (dolist (p (%array-displaced-from array))
1331 (let ((from (weak-pointer-value p)))
1332 (when (and from (eq array (%array-data-vector from)))
1333 (let ((requires (+ (%array-available-elements from)
1334 (%array-displacement from))))
1335 (unless (>= new-length requires)
1336 ;; ANSI sayeth (ADJUST-ARRAY dictionary entry):
1338 ;; "If A is displaced to B, the consequences are unspecified if B is
1339 ;; adjusted in such a way that it no longer has enough elements to
1340 ;; satisfy A.
1342 ;; since we're hanging on a weak pointer here, we can't signal an
1343 ;; error right now: the array that we're looking at might be
1344 ;; garbage. Instead, we set all dimensions to zero so that next
1345 ;; safe access to the displaced array will trap. Additionally, we
1346 ;; save the original dimensions, so we can signal a more
1347 ;; understandable error when the time comes.
1348 (%walk-displaced-array-backpointers from 0)
1349 (setf (%array-fill-pointer from) 0
1350 (%array-available-elements from) 0
1351 (%array-displaced-p from) (array-dimensions array))
1352 (dotimes (i (%array-rank from))
1353 (setf (%array-dimension from i) 0))))))))
1355 ;;; Fill in array header with the provided information, and return the array.
1356 (defun set-array-header (array data length fill-pointer displacement dimensions
1357 displacedp newp)
1358 (if newp
1359 (setf (%array-displaced-from array) nil)
1360 (%walk-displaced-array-backpointers array length))
1361 (when displacedp
1362 (%save-displaced-array-backpointer array data))
1363 (setf (%array-data-vector array) data)
1364 (setf (%array-available-elements array) length)
1365 (cond (fill-pointer
1366 (setf (%array-fill-pointer array) fill-pointer)
1367 (setf (%array-fill-pointer-p array) t))
1369 (setf (%array-fill-pointer array) length)
1370 (setf (%array-fill-pointer-p array) nil)))
1371 (setf (%array-displacement array) displacement)
1372 (if (listp dimensions)
1373 (dotimes (axis (array-rank array))
1374 (declare (type index axis))
1375 (setf (%array-dimension array axis) (pop dimensions)))
1376 (setf (%array-dimension array 0) dimensions))
1377 (setf (%array-displaced-p array) displacedp)
1378 array)
1380 ;;; User visible extension
1381 (declaim (ftype (function (array) (values (simple-array * (*)) &optional))
1382 array-storage-vector))
1383 (defun array-storage-vector (array)
1384 #!+sb-doc
1385 "Returns the underlying storage vector of ARRAY, which must be a non-displaced array.
1387 In SBCL, if ARRAY is a of type \(SIMPLE-ARRAY * \(*)), it is its own storage
1388 vector. Multidimensional arrays, arrays with fill pointers, and adjustable
1389 arrays have an underlying storage vector with the same ARRAY-ELEMENT-TYPE as
1390 ARRAY, which this function returns.
1392 Important note: the underlying vector is an implementation detail. Even though
1393 this function exposes it, changes in the implementation may cause this
1394 function to be removed without further warning."
1395 ;; KLUDGE: Without TRULY-THE the system is not smart enough to figure out that
1396 ;; the return value is always of the known type.
1397 (truly-the (simple-array * (*))
1398 (if (array-header-p array)
1399 (if (%array-displaced-p array)
1400 (error "~S cannot be used with displaced arrays. Use ~S instead."
1401 'array-storage-vector 'array-displacement)
1402 (%array-data-vector array))
1403 array)))
1406 ;;;; ZAP-ARRAY-DATA for ADJUST-ARRAY
1408 ;;; This does the grinding work for ADJUST-ARRAY. It zaps the data
1409 ;;; from the OLD-DATA in an arrangement specified by the OLD-DIMS to
1410 ;;; the NEW-DATA in an arrangement specified by the NEW-DIMS. OFFSET
1411 ;;; is a displaced offset to be added to computed indices of OLD-DATA.
1412 (defun zap-array-data (old-data old-dims offset new-data new-dims new-length
1413 element-type initial-element initial-element-p)
1414 (declare (list old-dims new-dims)
1415 (fixnum new-length))
1416 ;; OLD-DIMS comes from array-dimensions, which returns a fresh list
1417 ;; at least in SBCL.
1418 ;; NEW-DIMS comes from the user.
1419 (setf old-dims (nreverse old-dims)
1420 new-dims (reverse new-dims))
1421 (cond ((eq old-data new-data)
1422 ;; NEW-LENGTH, ELEMENT-TYPE, INITIAL-ELEMENT, and
1423 ;; INITIAL-ELEMENT-P are used when OLD-DATA and NEW-DATA are
1424 ;; EQ; in this case, a temporary must be used and filled
1425 ;; appropriately. specified initial-element.
1426 (when initial-element-p
1427 ;; FIXME: transforming this TYPEP to someting a bit faster
1428 ;; would be a win...
1429 (unless (typep initial-element element-type)
1430 (error "~S can't be used to initialize an array of type ~S."
1431 initial-element element-type)))
1432 (let ((temp (if initial-element-p
1433 (make-array new-length :initial-element initial-element)
1434 (make-array new-length))))
1435 (declare (simple-vector temp))
1436 (zap-array-data-aux old-data old-dims offset temp new-dims)
1437 (dotimes (i new-length)
1438 (setf (aref new-data i) (aref temp i)))
1439 ;; Kill the temporary vector to prevent garbage retention.
1440 (%shrink-vector temp 0)))
1442 ;; When OLD-DATA and NEW-DATA are not EQ, NEW-DATA has
1443 ;; already been filled with any
1444 (zap-array-data-aux old-data old-dims offset new-data new-dims))))
1446 (defun zap-array-data-aux (old-data old-dims offset new-data new-dims)
1447 (declare (fixnum offset))
1448 (let ((limits (mapcar (lambda (x y)
1449 (declare (fixnum x y))
1450 (1- (the fixnum (min x y))))
1451 old-dims new-dims)))
1452 (macrolet ((bump-index-list (index limits)
1453 `(do ((subscripts ,index (cdr subscripts))
1454 (limits ,limits (cdr limits)))
1455 ((null subscripts) :eof)
1456 (cond ((< (the fixnum (car subscripts))
1457 (the fixnum (car limits)))
1458 (rplaca subscripts
1459 (1+ (the fixnum (car subscripts))))
1460 (return ,index))
1461 (t (rplaca subscripts 0))))))
1462 (do ((index (make-list (length old-dims) :initial-element 0)
1463 (bump-index-list index limits)))
1464 ((eq index :eof))
1465 (setf (aref new-data (row-major-index-from-dims index new-dims))
1466 (aref old-data
1467 (+ (the fixnum (row-major-index-from-dims index old-dims))
1468 offset)))))))
1470 ;;; Figure out the row-major-order index of an array reference from a
1471 ;;; list of subscripts and a list of dimensions. This is for internal
1472 ;;; calls only, and the subscripts and dim-list variables are assumed
1473 ;;; to be reversed from what the user supplied.
1474 (defun row-major-index-from-dims (rev-subscripts rev-dim-list)
1475 (do ((rev-subscripts rev-subscripts (cdr rev-subscripts))
1476 (rev-dim-list rev-dim-list (cdr rev-dim-list))
1477 (chunk-size 1)
1478 (result 0))
1479 ((null rev-dim-list) result)
1480 (declare (fixnum chunk-size result))
1481 (setq result (+ result
1482 (the fixnum (* (the fixnum (car rev-subscripts))
1483 chunk-size))))
1484 (setq chunk-size (* chunk-size (the fixnum (car rev-dim-list))))))
1486 ;;;; some bit stuff
1488 (defun bit-array-same-dimensions-p (array1 array2)
1489 (declare (type (array bit) array1 array2))
1490 (and (= (array-rank array1)
1491 (array-rank array2))
1492 (dotimes (index (array-rank array1) t)
1493 (when (/= (array-dimension array1 index)
1494 (array-dimension array2 index))
1495 (return nil)))))
1497 (defun pick-result-array (result-bit-array bit-array-1)
1498 (case result-bit-array
1499 ((t) bit-array-1)
1500 ((nil) (make-array (array-dimensions bit-array-1)
1501 :element-type 'bit
1502 :initial-element 0))
1504 (unless (bit-array-same-dimensions-p bit-array-1
1505 result-bit-array)
1506 (error "~S and ~S don't have the same dimensions."
1507 bit-array-1 result-bit-array))
1508 result-bit-array)))
1510 (defmacro def-bit-array-op (name function)
1511 `(defun ,name (bit-array-1 bit-array-2 &optional result-bit-array)
1512 #!+sb-doc
1513 ,(format nil
1514 "Perform a bit-wise ~A on the elements of BIT-ARRAY-1 and ~
1515 BIT-ARRAY-2,~% putting the results in RESULT-BIT-ARRAY. ~
1516 If RESULT-BIT-ARRAY is T,~% BIT-ARRAY-1 is used. If ~
1517 RESULT-BIT-ARRAY is NIL or omitted, a new array is~% created. ~
1518 All the arrays must have the same rank and dimensions."
1519 (symbol-name function))
1520 (declare (type (array bit) bit-array-1 bit-array-2)
1521 (type (or (array bit) (member t nil)) result-bit-array))
1522 (unless (bit-array-same-dimensions-p bit-array-1 bit-array-2)
1523 (error "~S and ~S don't have the same dimensions."
1524 bit-array-1 bit-array-2))
1525 (let ((result-bit-array (pick-result-array result-bit-array bit-array-1)))
1526 (if (and (simple-bit-vector-p bit-array-1)
1527 (simple-bit-vector-p bit-array-2)
1528 (simple-bit-vector-p result-bit-array))
1529 (locally (declare (optimize (speed 3) (safety 0)))
1530 (,name bit-array-1 bit-array-2 result-bit-array))
1531 (with-array-data ((data1 bit-array-1) (start1) (end1))
1532 (declare (ignore end1))
1533 (with-array-data ((data2 bit-array-2) (start2) (end2))
1534 (declare (ignore end2))
1535 (with-array-data ((data3 result-bit-array) (start3) (end3))
1536 (do ((index-1 start1 (1+ index-1))
1537 (index-2 start2 (1+ index-2))
1538 (index-3 start3 (1+ index-3)))
1539 ((>= index-3 end3) result-bit-array)
1540 (declare (type index index-1 index-2 index-3))
1541 (setf (sbit data3 index-3)
1542 (logand (,function (sbit data1 index-1)
1543 (sbit data2 index-2))
1544 1))))))))))
1546 (def-bit-array-op bit-and logand)
1547 (def-bit-array-op bit-ior logior)
1548 (def-bit-array-op bit-xor logxor)
1549 (def-bit-array-op bit-eqv logeqv)
1550 (def-bit-array-op bit-nand lognand)
1551 (def-bit-array-op bit-nor lognor)
1552 (def-bit-array-op bit-andc1 logandc1)
1553 (def-bit-array-op bit-andc2 logandc2)
1554 (def-bit-array-op bit-orc1 logorc1)
1555 (def-bit-array-op bit-orc2 logorc2)
1557 (defun bit-not (bit-array &optional result-bit-array)
1558 #!+sb-doc
1559 "Performs a bit-wise logical NOT on the elements of BIT-ARRAY,
1560 putting the results in RESULT-BIT-ARRAY. If RESULT-BIT-ARRAY is T,
1561 BIT-ARRAY is used. If RESULT-BIT-ARRAY is NIL or omitted, a new array is
1562 created. Both arrays must have the same rank and dimensions."
1563 (declare (type (array bit) bit-array)
1564 (type (or (array bit) (member t nil)) result-bit-array))
1565 (let ((result-bit-array (pick-result-array result-bit-array bit-array)))
1566 (if (and (simple-bit-vector-p bit-array)
1567 (simple-bit-vector-p result-bit-array))
1568 (locally (declare (optimize (speed 3) (safety 0)))
1569 (bit-not bit-array result-bit-array))
1570 (with-array-data ((src bit-array) (src-start) (src-end))
1571 (declare (ignore src-end))
1572 (with-array-data ((dst result-bit-array) (dst-start) (dst-end))
1573 (do ((src-index src-start (1+ src-index))
1574 (dst-index dst-start (1+ dst-index)))
1575 ((>= dst-index dst-end) result-bit-array)
1576 (declare (type index src-index dst-index))
1577 (setf (sbit dst dst-index)
1578 (logxor (sbit src src-index) 1))))))))
1580 ;;;; array type dispatching
1582 ;;; Given DISPATCH-FOO as the DISPATCH-NAME argument (unevaluated),
1583 ;;; defines the functions
1585 ;;; DISPATCH-FOO/SIMPLE-BASE-STRING
1586 ;;; DISPATCH-FOO/SIMPLE-CHARACTER-STRING
1587 ;;; DISPATCH-FOO/SIMPLE-ARRAY-SINGLE-FLOAT
1588 ;;; ...
1590 ;;; PARAMS are the function parameters in the definition of each
1591 ;;; specializer function. The array being specialized must be the
1592 ;;; first parameter in PARAMS. A type declaration for this parameter
1593 ;;; is automatically inserted into the body of each function.
1595 ;;; The dispatch table %%FOO-FUNS%% is defined and populated by these
1596 ;;; functions. The table is padded by the function
1597 ;;; HAIRY-FOO-DISPATCH-ERROR, also defined by DEFINE-ARRAY-DISPATCH.
1599 ;;; Finally, the DISPATCH-FOO macro is defined which does the actual
1600 ;;; dispatching when called. It expects arguments that match PARAMS.
1602 (defmacro define-array-dispatch (dispatch-name params &body body)
1603 (let ((table-name (symbolicate "%%" dispatch-name "-FUNS%%"))
1604 (error-name (symbolicate "HAIRY-" dispatch-name "-ERROR")))
1605 `(progn
1606 (eval-when (:compile-toplevel :load-toplevel :execute)
1607 (defun ,error-name (&rest args)
1608 (error 'type-error
1609 :datum (first args)
1610 :expected-type '(simple-array * (*)))))
1611 (defglobal ,table-name (make-array ,(1+ sb!vm:widetag-mask)
1612 :initial-element #',error-name))
1613 ,@(loop for info across sb!vm:*specialized-array-element-type-properties*
1614 for typecode = (sb!vm:saetp-typecode info)
1615 for specifier = (sb!vm:saetp-specifier info)
1616 for primitive-type-name = (sb!vm:saetp-primitive-type-name info)
1617 collect (let ((fun-name (symbolicate (string dispatch-name)
1618 "/" primitive-type-name)))
1619 `(progn
1620 (defun ,fun-name ,params
1621 (declare (type (simple-array ,specifier (*))
1622 ,(first params)))
1623 ,@body)
1624 (setf (svref ,table-name ,typecode) #',fun-name))))
1625 (defmacro ,dispatch-name (&rest args)
1626 (check-type (first args) symbol)
1627 (let ((tag (gensym "TAG")))
1628 `(funcall
1629 (the function
1630 (let ((,tag 0))
1631 (when (sb!vm::%other-pointer-p ,(first args))
1632 (setf ,tag (%other-pointer-widetag ,(first args))))
1633 (svref ,',table-name ,tag)))
1634 ,@args))))))