/
array.lisp
1806 lines (1702 loc) · 84.2 KB
/
array.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;; functions to implement arrays
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; This software is derived from the CMU CL system, which was
;;;; written at Carnegie Mellon University and released into the
;;;; public domain. The software is in the public domain and is
;;;; provided with absolutely no warranty. See the COPYING and CREDITS
;;;; files for more information.
(in-package "SB!IMPL")
#!-sb-fluid
(declaim (inline adjustable-array-p
array-displacement))
;;;; miscellaneous accessor functions
;;; These functions are only needed by the interpreter, 'cause the
;;; compiler inlines them.
(macrolet ((def (name)
`(progn
(defun ,name (array)
(,name array))
(defun (setf ,name) (value array)
(setf (,name array) value)))))
(def %array-fill-pointer)
(def %array-fill-pointer-p)
(def %array-available-elements)
(def %array-data)
(def %array-displacement)
(def %array-displaced-p)
(def %array-displaced-from))
;;; For compatibility: DO NOT USE IN NEW CODE.
(defun %array-data-vector (array) (%array-data array))
(defun %array-rank (array)
(%array-rank array))
(defun %array-dimension (array axis)
(%array-dimension array axis))
(defun %set-array-dimension (array axis value)
(%set-array-dimension array axis value))
(defun %check-bound (array bound index)
(declare (type index bound)
(fixnum index))
(%check-bound array bound index))
(defun check-bound (array bound index)
(declare (type index bound)
(fixnum index))
(%check-bound array bound index)
index)
(defun %with-array-data/fp (array start end)
(%with-array-data-macro array start end :check-bounds t :check-fill-pointer t))
(defun %with-array-data (array start end)
(%with-array-data-macro array start end :check-bounds t :array-header-p t))
(defun %data-vector-and-index (array index)
(if (array-header-p array)
(multiple-value-bind (vector index)
(%with-array-data array index nil)
(values vector index))
(values (truly-the (simple-array * (*)) array) index)))
;;;; MAKE-ARRAY
(defun %integer-vector-widetag-and-n-bits-shift (signed high)
(let ((unsigned-table
#.(let ((map (make-array (1+ sb!vm:n-word-bits))))
(loop for saetp across
(reverse sb!vm:*specialized-array-element-type-properties*)
for ctype = (sb!vm:saetp-ctype saetp)
when (and (numeric-type-p ctype)
(eq (numeric-type-class ctype) 'integer)
(zerop (numeric-type-low ctype)))
do (fill map (cons (sb!vm:saetp-typecode saetp)
(sb!vm:saetp-n-bits-shift saetp))
:end (1+ (integer-length (numeric-type-high ctype)))))
map))
(signed-table
#.(let ((map (make-array (1+ sb!vm:n-word-bits))))
(loop for saetp across
(reverse sb!vm:*specialized-array-element-type-properties*)
for ctype = (sb!vm:saetp-ctype saetp)
when (and (numeric-type-p ctype)
(eq (numeric-type-class ctype) 'integer)
(minusp (numeric-type-low ctype)))
do (fill map (cons (sb!vm:saetp-typecode saetp)
(sb!vm:saetp-n-bits-shift saetp))
:end (+ (integer-length (numeric-type-high ctype)) 2)))
map)))
(cond ((> high sb!vm:n-word-bits)
(values #.sb!vm:simple-vector-widetag
#.(1- (integer-length sb!vm:n-word-bits))))
(signed
(let ((x (aref signed-table high)))
(values (car x) (cdr x))))
(t
(let ((x (aref unsigned-table high)))
(values (car x) (cdr x)))))))
;;; This is a bit complicated, but calling subtypep over all
;;; specialized types is exceedingly slow
(defun %vector-widetag-and-n-bits-shift (type)
(macrolet ((with-parameters ((arg-type &key intervals)
(&rest args) &body body)
(let ((type-sym (gensym)))
`(let (,@(loop for arg in args
collect `(,arg '*)))
(declare (ignorable ,@args))
(when (consp type)
(let ((,type-sym (cdr type)))
(block nil
,@(loop for arg in args
collect
`(cond ((consp ,type-sym)
(let ((value (pop ,type-sym)))
(if (or (eq value '*)
(typep value ',arg-type)
,(if intervals
`(and (consp value)
(null (cdr value))
(typep (car value)
',arg-type))))
(setf ,arg value)
(ill-type))))
((null ,type-sym)
(return))
(t
(ill-type)))))
(when ,type-sym
(ill-type))))
,@body)))
(result (widetag)
(let ((value (symbol-value widetag)))
`(values ,value
,(sb!vm:saetp-n-bits-shift
(find value
sb!vm:*specialized-array-element-type-properties*
:key #'sb!vm:saetp-typecode))))))
(flet ((ill-type ()
(declare (optimize allow-non-returning-tail-call))
(error "Invalid type specifier: ~/sb!impl:print-type-specifier/"
type))
(integer-interval-widetag (low high)
(if (minusp low)
(%integer-vector-widetag-and-n-bits-shift
t
(1+ (max (integer-length low) (integer-length high))))
(%integer-vector-widetag-and-n-bits-shift
nil
(max (integer-length low) (integer-length high))))))
(let* ((consp (consp type))
(type-name (if consp
(car type)
type)))
(case type-name
((t)
(when consp
(ill-type))
(result sb!vm:simple-vector-widetag))
((base-char standard-char #!-sb-unicode character)
(when consp
(ill-type))
(result sb!vm:simple-base-string-widetag))
#!+sb-unicode
((character extended-char)
(when consp
(ill-type))
(result sb!vm:simple-character-string-widetag))
(bit
(when consp
(ill-type))
(result sb!vm:simple-bit-vector-widetag))
(fixnum
(when consp
(ill-type))
(result sb!vm:simple-array-fixnum-widetag))
(unsigned-byte
(with-parameters ((integer 1)) (high)
(if (eq high '*)
(result sb!vm:simple-vector-widetag)
(%integer-vector-widetag-and-n-bits-shift nil high))))
(signed-byte
(with-parameters ((integer 1)) (high)
(if (eq high '*)
(result sb!vm:simple-vector-widetag)
(%integer-vector-widetag-and-n-bits-shift t high))))
(double-float
(with-parameters (double-float :intervals t) (low high)
(if (and (not (eq low '*))
(not (eq high '*))
(if (or (consp low) (consp high))
(>= (type-bound-number low) (type-bound-number high))
(> low high)))
(result sb!vm:simple-array-nil-widetag)
(result sb!vm:simple-array-double-float-widetag))))
(single-float
(with-parameters (single-float :intervals t) (low high)
(if (and (not (eq low '*))
(not (eq high '*))
(if (or (consp low) (consp high))
(>= (type-bound-number low) (type-bound-number high))
(> low high)))
(result sb!vm:simple-array-nil-widetag)
(result sb!vm:simple-array-single-float-widetag))))
(mod
(if (and (consp type)
(consp (cdr type))
(null (cddr type))
(typep (cadr type) '(integer 1)))
(%integer-vector-widetag-and-n-bits-shift
nil (integer-length (1- (cadr type))))
(ill-type)))
#!+long-float
(long-float
(with-parameters (long-float :intervals t) (low high)
(if (and (not (eq low '*))
(not (eq high '*))
(if (or (consp low) (consp high))
(>= (type-bound-number low) (type-bound-number high))
(> low high)))
(result sb!vm:simple-array-nil-widetag)
(result sb!vm:simple-array-long-float-widetag))))
(integer
(with-parameters (integer :intervals t) (low high)
(let ((low (if (consp low)
(1+ (car low))
low))
(high (if (consp high)
(1- (car high))
high)))
(cond ((or (eq high '*)
(eq low '*))
(result sb!vm:simple-vector-widetag))
((> low high)
(result sb!vm:simple-array-nil-widetag))
(t
(integer-interval-widetag low high))))))
(complex
(with-parameters (t) (subtype)
(if (eq subtype '*)
(result sb!vm:simple-vector-widetag)
(let ((ctype (specifier-type type)))
(cond ((eq ctype *empty-type*)
(result sb!vm:simple-array-nil-widetag))
((union-type-p ctype)
(cond ((csubtypep ctype (specifier-type '(complex double-float)))
(result
sb!vm:simple-array-complex-double-float-widetag))
((csubtypep ctype (specifier-type '(complex single-float)))
(result
sb!vm:simple-array-complex-single-float-widetag))
#!+long-float
((csubtypep ctype (specifier-type '(complex long-float)))
(result
sb!vm:simple-array-complex-long-float-widetag))
(t
(result sb!vm:simple-vector-widetag))))
(t
(case (numeric-type-format ctype)
(double-float
(result
sb!vm:simple-array-complex-double-float-widetag))
(single-float
(result
sb!vm:simple-array-complex-single-float-widetag))
#!+long-float
(long-float
(result
sb!vm:simple-array-complex-long-float-widetag))
(t
(result sb!vm:simple-vector-widetag)))))))))
((nil)
(result sb!vm:simple-array-nil-widetag))
(t
(block nil
(let ((ctype (type-or-nil-if-unknown type)))
(unless ctype
(return (result sb!vm:simple-vector-widetag)))
(typecase ctype
(union-type
(let ((types (union-type-types ctype)))
(cond ((not (every #'numeric-type-p types))
(result sb!vm:simple-vector-widetag))
((csubtypep ctype (specifier-type 'integer))
(integer-interval-widetag
(reduce #'min types :key #'numeric-type-low)
(reduce #'max types :key #'numeric-type-high)))
((csubtypep ctype (specifier-type 'double-float))
(result sb!vm:simple-array-double-float-widetag))
((csubtypep ctype (specifier-type 'single-float))
(result sb!vm:simple-array-single-float-widetag))
#!+long-float
((csubtypep ctype (specifier-type 'long-float))
(result sb!vm:simple-array-long-float-widetag))
(t
(result sb!vm:simple-vector-widetag)))))
(character-set-type
#!-sb-unicode (result sb!vm:simple-base-string-widetag)
#!+sb-unicode
(if (loop for (start . end)
in (character-set-type-pairs ctype)
always (and (< start base-char-code-limit)
(< end base-char-code-limit)))
(result sb!vm:simple-base-string-widetag)
(result sb!vm:simple-character-string-widetag)))
(t
(let ((expansion (type-specifier ctype)))
(if (equal expansion type)
(result sb!vm:simple-vector-widetag)
(%vector-widetag-and-n-bits-shift expansion)))))))))))))
(defun %complex-vector-widetag (widetag)
(macrolet ((make-case ()
`(case widetag
,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
for complex = (sb!vm:saetp-complex-typecode saetp)
when complex
collect (list (sb!vm:saetp-typecode saetp) complex))
(t
#.sb!vm:complex-vector-widetag))))
(make-case)))
(defglobal %%simple-array-n-bits-shifts%% (make-array (1+ sb!vm:widetag-mask)))
#.(loop for info across sb!vm:*specialized-array-element-type-properties*
collect `(setf (aref %%simple-array-n-bits-shifts%% ,(sb!vm:saetp-typecode info))
,(sb!vm:saetp-n-bits-shift info)) into forms
finally (return `(progn ,@forms)))
(declaim (type (simple-vector #.(1+ sb!vm:widetag-mask)) %%simple-array-n-bits-shifts%%))
(declaim (inline vector-length-in-words))
(defun vector-length-in-words (length n-bits-shift)
(declare (type (integer 0 7) n-bits-shift))
(let ((mask (ash (1- sb!vm:n-word-bits) (- n-bits-shift)))
(shift (- n-bits-shift
(1- (integer-length sb!vm:n-word-bits)))))
(ash (+ length mask) shift)))
;;; N-BITS-SHIFT is the shift amount needed to turn LENGTH into bits
;;; or NIL, %%simple-array-n-bits-shifts%% will be used in that case.
(defun allocate-vector-with-widetag (widetag length n-bits-shift)
(declare (type (unsigned-byte 8) widetag)
(type index length))
(let* ((n-bits-shift (or n-bits-shift
(aref %%simple-array-n-bits-shifts%% widetag)))
(full-length (if (or (= widetag sb!vm:simple-base-string-widetag)
#!+sb-unicode
(= widetag
sb!vm:simple-character-string-widetag))
(1+ length)
length)))
;; Be careful not to allocate backing storage for element type NIL.
;; Both it and type BIT have N-BITS-SHIFT = 0, so the determination
;; of true size can't be left up to VECTOR-LENGTH-IN-WORDS.
(allocate-vector widetag length
(if (/= widetag sb!vm:simple-array-nil-widetag)
(vector-length-in-words full-length n-bits-shift)
0))))
(defun array-underlying-widetag (array)
(macrolet ((make-case ()
`(case widetag
,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
for complex = (sb!vm:saetp-complex-typecode saetp)
when complex
collect (list complex (sb!vm:saetp-typecode saetp)))
((,sb!vm:simple-array-widetag
,sb!vm:complex-vector-widetag
,sb!vm:complex-array-widetag)
(with-array-data ((array array) (start) (end))
(declare (ignore start end))
(%other-pointer-widetag array)))
(t
widetag))))
(let ((widetag (%other-pointer-widetag array)))
(make-case))))
(defun make-vector-like (vector length)
(allocate-vector-with-widetag (array-underlying-widetag vector) length nil))
;; Complain in various ways about wrong :INITIAL-foo arguments,
;; returning the two initialization arguments needed for DATA-VECTOR-FROM-INITS.
(defun validate-array-initargs (element-p element contents-p contents displaced)
(cond ((and displaced (or element-p contents-p))
(if (and element-p contents-p)
(error "Neither :INITIAL-ELEMENT nor :INITIAL-CONTENTS ~
may be specified with the :DISPLACED-TO option")
(error "~S may not be specified with the :DISPLACED-TO option"
(if element-p :initial-element :initial-contents))))
((and element-p contents-p)
(error "Can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
(element-p (values :initial-element element))
(contents-p (values :initial-contents contents))
(t (values nil nil))))
(declaim (inline %save-displaced-array-backpointer))
(defun %save-displaced-array-backpointer (array data)
(flet ((purge (pointers)
(remove-if (lambda (value)
(or (not value) (eq array value)))
pointers
:key #'weak-pointer-value)))
;; Add backpointer to the new data vector if it has a header.
(when (array-header-p data)
(setf (%array-displaced-from data)
(cons (make-weak-pointer array)
(purge (%array-displaced-from data)))))
;; Remove old backpointer, if any.
(let ((old-data (%array-data array)))
(when (and (neq data old-data) (array-header-p old-data))
(setf (%array-displaced-from old-data)
(purge (%array-displaced-from old-data)))))))
;;; Widetag is the widetag of the underlying vector,
;;; it'll be the same as the resulting array widetag only for simple vectors
(defun %make-array (dimensions widetag n-bits
&key
element-type
(initial-element nil initial-element-p)
(initial-contents nil initial-contents-p)
adjustable fill-pointer
displaced-to displaced-index-offset)
(declare (ignore element-type))
(binding* (((array-rank dimension-0)
(if (listp dimensions)
(values (length dimensions)
(if dimensions (car dimensions) 1))
(values 1 dimensions)))
((initialize initial-data)
(validate-array-initargs initial-element-p initial-element
initial-contents-p initial-contents
displaced-to))
(simple (and (null fill-pointer)
(not adjustable)
(null displaced-to))))
(declare (type array-rank array-rank))
(declare (type index dimension-0))
(cond ((and displaced-index-offset (null displaced-to))
(error "Can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))
((and simple (= array-rank 1))
(let ((vector ; a (SIMPLE-ARRAY * (*))
(allocate-vector-with-widetag widetag dimension-0 n-bits)))
;; presence of at most one :INITIAL-thing keyword was ensured above
(cond (initial-element-p
(fill vector initial-element))
(initial-contents-p
(let ((content-length (length initial-contents)))
(unless (= dimension-0 content-length)
(error "There are ~W elements in the :INITIAL-CONTENTS, but ~
the vector length is ~W."
content-length dimension-0)))
(replace vector initial-contents)))
vector))
((and (arrayp displaced-to)
(/= (array-underlying-widetag displaced-to) widetag))
(error "Array element type of :DISPLACED-TO array does not match specified element type"))
(t
;; it's non-simple or multidimensional, or both.
(when fill-pointer
(unless (= array-rank 1)
(error "Only vectors can have fill pointers."))
(when (and (integerp fill-pointer) (> fill-pointer dimension-0))
;; FIXME: should be TYPE-ERROR?
(error "invalid fill-pointer ~W" fill-pointer)))
(let* ((total-size
(if (consp dimensions)
(the index (reduce (lambda (a b) (* a (the index b)))
dimensions))
;; () is considered to have dimension-0 = 1.
;; It avoids the REDUCE lambda being called with no args.
dimension-0))
(data (or displaced-to
(data-vector-from-inits
dimensions total-size nil widetag n-bits
initialize initial-data)))
(array (make-array-header
(cond ((= array-rank 1)
(%complex-vector-widetag widetag))
(simple sb!vm:simple-array-widetag)
(t sb!vm:complex-array-widetag))
array-rank)))
(if fill-pointer
(setf (%array-fill-pointer-p array) t
(%array-fill-pointer array)
(if (eq fill-pointer t) dimension-0 fill-pointer))
(setf (%array-fill-pointer-p array) nil
(%array-fill-pointer array) total-size))
(setf (%array-available-elements array) total-size)
;; Terrible name for this slot - we displace to the
;; target array's header, if any, not the "ultimate"
;; vector in the chain of displacements.
(setf (%array-data array) data)
(setf (%array-displaced-from array) nil)
(cond (displaced-to
(let ((offset (or displaced-index-offset 0)))
(when (> (+ offset total-size)
(array-total-size displaced-to))
(error "~S doesn't have enough elements." displaced-to))
(setf (%array-displacement array) offset)
(setf (%array-displaced-p array) t)
(%save-displaced-array-backpointer array data)))
(t
(setf (%array-displaced-p array) nil)))
(if (listp dimensions)
(let ((dims dimensions)) ; avoid "prevents use of assertion"
(dotimes (axis array-rank)
(setf (%array-dimension array axis) (pop dims))))
(setf (%array-dimension array 0) dimension-0))
array)))))
(defun make-array (dimensions &rest args
&key (element-type t)
initial-element initial-contents
adjustable
fill-pointer
displaced-to
displaced-index-offset)
(declare (ignore initial-element
initial-contents adjustable
fill-pointer displaced-to displaced-index-offset))
(declare (explicit-check))
(multiple-value-bind (widetag shift) (%vector-widetag-and-n-bits-shift element-type)
(apply #'%make-array dimensions widetag shift args)))
(defun make-static-vector (length &key
(element-type '(unsigned-byte 8))
(initial-contents nil initial-contents-p)
(initial-element nil initial-element-p))
"Allocate vector of LENGTH elements in static space. Only allocation
of specialized arrays is supported."
;; STEP 1: check inputs fully
;;
;; This way of doing explicit checks before the vector is allocated
;; is expensive, but probably worth the trouble as once we've allocated
;; the vector we have no way to get rid of it anymore...
(when (eq t (upgraded-array-element-type element-type))
(error "Static arrays of type ~/sb!impl:print-type-specifier/ not supported."
element-type))
(validate-array-initargs initial-element-p initial-element
initial-contents-p initial-contents nil) ; for effect
(when initial-contents-p
(unless (= length (length initial-contents))
(error "There are ~W elements in the :INITIAL-CONTENTS, but the ~
vector length is ~W."
(length initial-contents)
length))
(unless (every (lambda (x) (typep x element-type)) initial-contents)
(error ":INITIAL-CONTENTS contains elements not of type ~
~/sb!impl:print-type-specifier/."
element-type)))
(when initial-element-p
(unless (typep initial-element element-type)
(error ":INITIAL-ELEMENT ~S is not of type ~
~/sb!impl:print-type-specifier/."
initial-element element-type)))
;; STEP 2
;;
;; Allocate and possibly initialize the vector.
(multiple-value-bind (type n-bits-shift)
(%vector-widetag-and-n-bits-shift element-type)
(let ((vector
(allocate-static-vector type length
(vector-length-in-words length
n-bits-shift))))
(cond (initial-element-p
(fill vector initial-element))
(initial-contents-p
(replace vector initial-contents))
(t
vector)))))
;;; DATA-VECTOR-FROM-INITS returns a simple vector that has the
;;; specified array characteristics. Dimensions is only used to pass
;;; to FILL-DATA-VECTOR for error checking on the structure of
;;; initial-contents.
(defun data-vector-from-inits (dimensions total-size
element-type widetag n-bits
initialize initial-data)
;; FIXME: element-type can be NIL when widetag is non-nil,
;; and FILL will check the type, although the error will be not as nice.
;; (cond (typep initial-element element-type)
;; (error "~S cannot be used to initialize an array of type ~S."
;; initial-element element-type))
(let ((data (if widetag
(allocate-vector-with-widetag widetag total-size n-bits)
(make-array total-size :element-type element-type))))
(ecase initialize
(:initial-element
(fill (the vector data) initial-data))
(:initial-contents
;; DIMENSIONS can be supplied as a list or integer now
(dx-let ((list-of-dims (list dimensions))) ; ok if already a list
(fill-data-vector data
(if (listp dimensions) dimensions list-of-dims)
initial-data)))
((nil)))
data))
(defun vector (&rest objects)
"Construct a SIMPLE-VECTOR from the given objects."
(let ((v (make-array (length objects))))
(do-rest-arg ((x i) objects 0 v)
(setf (aref v i) x))))
;;;; accessor/setter functions
;;; Dispatch to an optimized routine the data vector accessors for
;;; each different specialized vector type. Do dispatching by looking
;;; up the widetag in the array rather than with the typecases, which
;;; as of 1.0.5 compiles to a naive sequence of linear TYPEPs. Also
;;; provide separate versions where bounds checking has been moved
;;; from the callee to the caller, since it's much cheaper to do once
;;; the type information is available. Finally, for each of these
;;; routines also provide a slow path, taken for arrays that are not
;;; vectors or not simple.
(macrolet ((def (name table-name)
`(progn
(defglobal ,table-name (make-array ,(1+ sb!vm:widetag-mask)))
(declaim (type (simple-array function (,(1+ sb!vm:widetag-mask)))
,table-name))
(defmacro ,name (array-var)
`(the function
(let ((tag 0))
(when (sb!vm::%other-pointer-p ,array-var)
(setf tag (%other-pointer-widetag ,array-var)))
(svref ,',table-name tag)))))))
(def !find-data-vector-setter %%data-vector-setters%%)
(def !find-data-vector-setter/check-bounds %%data-vector-setters/check-bounds%%)
;; Used by DO-VECTOR-DATA -- which in turn appears in DOSEQUENCE expansion,
;; meaning we can have post-build dependences on this.
(def %find-data-vector-reffer %%data-vector-reffers%%)
(def !find-data-vector-reffer/check-bounds %%data-vector-reffers/check-bounds%%))
;;; Like DOVECTOR, but more magical -- can't use this on host.
(defmacro do-vector-data ((elt vector &optional result) &body body)
(multiple-value-bind (forms decls) (parse-body body nil)
(with-unique-names (index vec start end ref)
`(with-array-data ((,vec ,vector)
(,start)
(,end)
:check-fill-pointer t)
(let ((,ref (%find-data-vector-reffer ,vec)))
(declare (function ,ref))
(do ((,index ,start (1+ ,index)))
((>= ,index ,end)
(let ((,elt nil))
,@(filter-dolist-declarations decls)
,elt
,result))
(let ((,elt (funcall ,ref ,vec ,index)))
,@decls
(tagbody ,@forms))))))))
(macrolet ((%ref (accessor-getter extra-params)
`(funcall (,accessor-getter array) array index ,@extra-params))
(define (accessor-name slow-accessor-name accessor-getter
extra-params check-bounds)
`(progn
(defun ,accessor-name (array index ,@extra-params)
(declare (explicit-check))
(declare (optimize speed
;; (SAFETY 0) is ok. All calls to
;; these functions are generated by
;; the compiler, so argument count
;; checking isn't needed. Type checking
;; is done implicitly via the widetag
;; dispatch.
(safety 0)))
(%ref ,accessor-getter ,extra-params))
(defun ,slow-accessor-name (array index ,@extra-params)
(declare (optimize speed (safety 0)))
(if (not (%array-displaced-p array))
;; The reasonably quick path of non-displaced complex
;; arrays.
(let ((array (%array-data array)))
(%ref ,accessor-getter ,extra-params))
;; The real slow path.
(with-array-data
((vector array)
(index (locally
(declare (optimize (speed 1) (safety 1)))
(,@check-bounds index)))
(end)
:force-inline t)
(declare (ignore end))
(,accessor-name vector index ,@extra-params)))))))
(define hairy-data-vector-ref slow-hairy-data-vector-ref
%find-data-vector-reffer
nil (progn))
(define hairy-data-vector-set slow-hairy-data-vector-set
!find-data-vector-setter
(new-value) (progn))
(define hairy-data-vector-ref/check-bounds
slow-hairy-data-vector-ref/check-bounds
!find-data-vector-reffer/check-bounds
nil (check-bound array (%array-dimension array 0)))
(define hairy-data-vector-set/check-bounds
slow-hairy-data-vector-set/check-bounds
!find-data-vector-setter/check-bounds
(new-value) (check-bound array (%array-dimension array 0))))
(defun hairy-ref-error (array index &optional new-value)
(declare (ignore index new-value))
(error 'type-error
:datum array
:expected-type 'vector))
(macrolet ((define-reffer (saetp check-form)
(let* ((type (sb!vm:saetp-specifier saetp))
(atype `(simple-array ,type (*))))
`(named-lambda (optimized-data-vector-ref ,type) (vector index)
(declare (optimize speed (safety 0))
;; Obviously these all coerce raw words to lispobjs
;; so don't keep spewing notes about it.
(muffle-conditions compiler-note)
(ignorable index))
,(if type
`(data-vector-ref (the ,atype vector)
(locally
(declare (optimize (safety 1)))
(the index
(,@check-form index))))
`(data-nil-vector-ref (the ,atype vector) index)))))
(define-setter (saetp check-form)
(let* ((type (sb!vm:saetp-specifier saetp))
(atype `(simple-array ,type (*))))
`(named-lambda (optimized-data-vector-set ,type) (vector index new-value)
(declare (optimize speed (safety 0)))
;; Impossibly setting an elt of an (ARRAY NIL)
;; returns no value. And nobody cares.
(declare (muffle-conditions compiler-note))
(data-vector-set (the ,atype vector)
(locally
(declare (optimize (safety 1)))
(the index
(,@check-form index)))
(locally
;; SPEED 1 needed to avoid the compiler
;; from downgrading the type check to
;; a cheaper one.
(declare (optimize (speed 1)
(safety 1)))
(the* (,type :context :aref) new-value)))
;; For specialized arrays, the return from
;; data-vector-set would have to be reboxed to be a
;; (Lisp) return value; instead, we use the
;; already-boxed value as the return.
new-value)))
(define-reffers (symbol deffer check-form slow-path)
`(progn
;; FIXME/KLUDGE: can't just FILL here, because genesis doesn't
;; preserve the binding, so re-initiaize as NS doesn't have
;; the energy to figure out to change that right now.
(setf ,symbol (make-array (1+ sb!vm::widetag-mask)
:initial-element #'hairy-ref-error))
,@(loop for widetag in '(sb!vm:complex-vector-widetag
sb!vm:complex-vector-nil-widetag
sb!vm:complex-bit-vector-widetag
#!+sb-unicode sb!vm:complex-character-string-widetag
sb!vm:complex-base-string-widetag
sb!vm:simple-array-widetag
sb!vm:complex-array-widetag)
collect `(setf (svref ,symbol ,widetag) ,slow-path))
,@(loop for saetp across sb!vm:*specialized-array-element-type-properties*
for widetag = (sb!vm:saetp-typecode saetp)
collect `(setf (svref ,symbol ,widetag)
(,deffer ,saetp ,check-form))))))
(defun !hairy-data-vector-reffer-init ()
(define-reffers %%data-vector-reffers%% define-reffer
(progn)
#'slow-hairy-data-vector-ref)
(define-reffers %%data-vector-setters%% define-setter
(progn)
#'slow-hairy-data-vector-set)
(define-reffers %%data-vector-reffers/check-bounds%% define-reffer
(check-bound vector (length vector))
#'slow-hairy-data-vector-ref/check-bounds)
(define-reffers %%data-vector-setters/check-bounds%% define-setter
(check-bound vector (length vector))
#'slow-hairy-data-vector-set/check-bounds)))
;;; (Ordinary DATA-VECTOR-REF usage compiles into a vop, but
;;; DATA-VECTOR-REF is also FOLDABLE, and this ordinary function
;;; definition is needed for the compiler to use in constant folding.)
(defun data-vector-ref (array index)
(declare (explicit-check))
(hairy-data-vector-ref array index))
(defun data-vector-ref-with-offset (array index offset)
(declare (explicit-check))
(hairy-data-vector-ref array (+ index offset)))
(defun invalid-array-p (array)
(and (array-header-p array)
(consp (%array-displaced-p array))))
(declaim (ftype (function (array) nil) invalid-array-error))
(defun invalid-array-error (array)
(declare (optimize allow-non-returning-tail-call))
(aver (array-header-p array))
;; Array invalidation stashes the original dimensions here...
(let ((dims (%array-displaced-p array))
(et (array-element-type array)))
(error 'invalid-array-error
:datum array
:expected-type
(if (cdr dims)
`(array ,et ,dims)
`(vector ,et ,@dims)))))
(declaim (ftype (function (array t integer &optional t) nil)
invalid-array-index-error))
(defun invalid-array-index-error (array index bound &optional axis)
(declare (optimize allow-non-returning-tail-call))
(if (invalid-array-p array)
(invalid-array-error array)
(error 'invalid-array-index-error
:array array
:axis axis
:datum index
:expected-type `(integer 0 (,bound)))))
;;; SUBSCRIPTS has a dynamic-extent list structure and is destroyed
(defun %array-row-major-index (array &rest subscripts)
(declare (truly-dynamic-extent subscripts)
(array array))
(let ((length (length subscripts)))
(cond ((array-header-p array)
(let ((rank (%array-rank array)))
(unless (= rank length)
(error "Wrong number of subscripts, ~W, for array of rank ~W."
length rank))
(do ((axis (1- rank) (1- axis))
(chunk-size 1)
(result 0))
((minusp axis) result)
(declare (fixnum axis chunk-size result))
(let ((index (fast-&rest-nth axis subscripts))
(dim (%array-dimension array axis)))
(unless (and (fixnump index) (< -1 index dim))
(invalid-array-index-error array index dim axis))
(setf result
(truly-the fixnum
(+ result
(truly-the fixnum (* chunk-size index))))
chunk-size (truly-the fixnum (* chunk-size dim)))))))
((/= length 1)
(error "Wrong number of subscripts, ~W, for array of rank 1."
length))
(t
(let ((index (fast-&rest-nth 0 subscripts))
(length (length (the (simple-array * (*)) array))))
(unless (and (fixnump index) (< -1 index length))
(invalid-array-index-error array index length))
index)))))
(defun array-in-bounds-p (array &rest subscripts)
"Return T if the SUBSCRIPTS are in bounds for the ARRAY, NIL otherwise."
(declare (truly-dynamic-extent subscripts))
(let ((length (length subscripts)))
(cond ((array-header-p array)
(let ((rank (%array-rank array)))
(unless (= rank length)
(error "Wrong number of subscripts, ~W, for array of rank ~W."
length rank))
(loop for i below length
for s = (fast-&rest-nth i subscripts)
always (and (typep s '(and fixnum unsigned-byte))
(< s (%array-dimension array i))))))
((/= length 1)
(error "Wrong number of subscripts, ~W, for array of rank 1."
length))
(t
(let ((subscript (fast-&rest-nth 0 subscripts)))
(and (typep subscript '(and fixnum unsigned-byte))
(< subscript
(length (truly-the (simple-array * (*)) array)))))))))
(defun array-row-major-index (array &rest subscripts)
(declare (truly-dynamic-extent subscripts))
(apply #'%array-row-major-index array subscripts))
(defun aref (array &rest subscripts)
"Return the element of the ARRAY specified by the SUBSCRIPTS."
(declare (truly-dynamic-extent subscripts))
(row-major-aref array (apply #'%array-row-major-index array subscripts)))
;;; (setf aref/bit/sbit) are implemented using setf-functions,
;;; because they have to work with (setf (apply #'aref array subscripts))
;;; All other setfs can be done using setf-functions too, but I
;;; haven't found technical advantages or disadvantages for either
;;; scheme.
(defun (setf aref) (new-value array &rest subscripts)
(declare (truly-dynamic-extent subscripts)
(type array array))
(setf (row-major-aref array (apply #'%array-row-major-index array subscripts))
new-value))
(defun row-major-aref (array index)
"Return the element of array corresponding to the row-major index. This is
SETFable."
(declare (optimize (safety 1)))
(row-major-aref array index))
(defun %set-row-major-aref (array index new-value)
(declare (optimize (safety 1)))
(setf (row-major-aref array index) new-value))
(defun svref (simple-vector index)
"Return the INDEXth element of the given Simple-Vector."
(declare (optimize (safety 1)))
(aref simple-vector index))
(defun %svset (simple-vector index new)
(declare (optimize (safety 1)))
(setf (aref simple-vector index) new))
(defun bit (bit-array &rest subscripts)
"Return the bit from the BIT-ARRAY at the specified SUBSCRIPTS."
(declare (type (array bit) bit-array)
(truly-dynamic-extent subscripts)
(optimize (safety 1)))
(row-major-aref bit-array (apply #'%array-row-major-index bit-array subscripts)))
(defun (setf bit) (new-value bit-array &rest subscripts)
(declare (type (array bit) bit-array)
(type bit new-value)
(truly-dynamic-extent subscripts)
(optimize (safety 1)))
(setf (row-major-aref bit-array
(apply #'%array-row-major-index bit-array subscripts))
new-value))
(defun sbit (simple-bit-array &rest subscripts)
"Return the bit from SIMPLE-BIT-ARRAY at the specified SUBSCRIPTS."
(declare (type (simple-array bit) simple-bit-array)
(truly-dynamic-extent subscripts)
(optimize (safety 1)))
(row-major-aref simple-bit-array
(apply #'%array-row-major-index simple-bit-array subscripts)))
(defun (setf sbit) (new-value bit-array &rest subscripts)
(declare (type (simple-array bit) bit-array)
(type bit new-value)
(truly-dynamic-extent subscripts)
(optimize (safety 1)))
(setf (row-major-aref bit-array
(apply #'%array-row-major-index bit-array subscripts))
new-value))
;;;; miscellaneous array properties
(defun array-element-type (array)
"Return the type of the elements of the array"
(let ((widetag (%other-pointer-widetag array))
(table (load-time-value
(let ((table (make-array 256 :initial-element :invalid)))
(dotimes (i (length sb!vm:*specialized-array-element-type-properties*) table)
(let* ((saetp (aref sb!vm:*specialized-array-element-type-properties* i))
(typecode (sb!vm:saetp-typecode saetp))
(complex-typecode (sb!vm:saetp-complex-typecode saetp))
(specifier (sb!vm:saetp-specifier saetp)))
(aver (typep specifier '(or list symbol)))
(setf (aref table typecode) specifier)
(when complex-typecode
(setf (aref table complex-typecode) specifier))))
(setf (aref table sb!vm:simple-array-widetag) nil
(aref table sb!vm:complex-vector-widetag) nil
(aref table sb!vm:complex-array-widetag) nil)
table)
t)))
(let ((result (aref table widetag)))
(if result
(truly-the (or list symbol) result)
;; (MAKE-ARRAY :ELEMENT-TYPE NIL) goes to this branch, but
;; gets the right answer in the end
(with-array-data ((array array) (start) (end))
(declare (ignore start end))
(truly-the (or list symbol) (aref table (%other-pointer-widetag array))))))))
(defun array-rank (array)
"Return the number of dimensions of ARRAY."
(if (array-header-p array)
(%array-rank array)
1))
(defun array-dimension (array axis-number)
"Return the length of dimension AXIS-NUMBER of ARRAY."
(declare (array array) (type index axis-number))
(cond ((not (array-header-p array))