/
array.lisp
1917 lines (1803 loc) · 92.5 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-VM")
(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-available-elements)
(def %array-data)
(def %array-displacement)
(def %array-displaced-p)
(def %array-displaced-from))
(defun %array-rank (array)
(%array-rank array))
(defun %array-dimension (array axis)
(%array-dimension array axis))
(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+ n-word-bits))))
(loop for saetp across
(reverse *specialized-array-element-type-properties*)
for ctype = (saetp-ctype saetp)
when (and (numeric-type-p ctype)
(eq (numeric-type-class ctype) 'integer)
(zerop (numeric-type-low ctype)))
do (fill map (cons (saetp-typecode saetp)
(saetp-n-bits-shift saetp))
:end (1+ (integer-length (numeric-type-high ctype)))))
map))
(signed-table
#.(let ((map (make-array (1+ n-word-bits))))
(loop for saetp across
(reverse *specialized-array-element-type-properties*)
for ctype = (saetp-ctype saetp)
when (and (numeric-type-p ctype)
(eq (numeric-type-class ctype) 'integer)
(minusp (numeric-type-low ctype)))
do (fill map (cons (saetp-typecode saetp)
(saetp-n-bits-shift saetp))
:end (+ (integer-length (numeric-type-high ctype)) 2)))
map)))
(cond ((> high n-word-bits)
(values #.simple-vector-widetag
#.(1- (integer-length 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)))
(ill-type ()
`(go fastidiously-parse))
(result (widetag)
(let ((value (symbol-value widetag)))
`(values ,value
,(saetp-n-bits-shift
(find value
*specialized-array-element-type-properties*
:key #'saetp-typecode))))))
(flet ((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))))))
(tagbody
(binding*
((consp (consp type))
(type-name (if consp (car type) type))
((widetag n-bits-shift)
(case type-name
((t)
(when consp
(ill-type))
(result simple-vector-widetag))
((base-char standard-char #-sb-unicode character)
(when consp
(ill-type))
(result simple-base-string-widetag))
#+sb-unicode
((character extended-char)
(when consp
(ill-type))
(result simple-character-string-widetag))
(bit
(when consp
(ill-type))
(result simple-bit-vector-widetag))
(fixnum
(when consp
(ill-type))
(result simple-array-fixnum-widetag))
(unsigned-byte
(with-parameters ((integer 1)) (high)
(if (eq high '*)
(result simple-vector-widetag)
(%integer-vector-widetag-and-n-bits-shift nil high))))
(signed-byte
(with-parameters ((integer 1)) (high)
(if (eq high '*)
(result 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 simple-array-nil-widetag)
(result 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 simple-array-nil-widetag)
(result 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 simple-array-nil-widetag)
(result 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 simple-vector-widetag))
((> low high)
(result simple-array-nil-widetag))
(t
(integer-interval-widetag low high))))))
(complex
(with-parameters (t) (subtype)
(if (eq subtype '*)
(result simple-vector-widetag)
(let ((ctype (specifier-type type)))
(cond ((eq ctype *empty-type*)
(result simple-array-nil-widetag))
((union-type-p ctype)
(cond ((csubtypep ctype (specifier-type '(complex double-float)))
(result
simple-array-complex-double-float-widetag))
((csubtypep ctype (specifier-type '(complex single-float)))
(result
simple-array-complex-single-float-widetag))
#+long-float
((csubtypep ctype (specifier-type '(complex long-float)))
(result
simple-array-complex-long-float-widetag))
(t
(result simple-vector-widetag))))
(t
(case (numeric-type-format ctype)
(double-float
(result
simple-array-complex-double-float-widetag))
(single-float
(result
simple-array-complex-single-float-widetag))
#+long-float
(long-float
(result
simple-array-complex-long-float-widetag))
(t
(result simple-vector-widetag)))))))))
((nil)
(result simple-array-nil-widetag))
(t
(go fastidiously-parse)))))
(return-from %vector-widetag-and-n-bits-shift
(values widetag n-bits-shift)))
fastidiously-parse)
;; Do things the hard way after falling through the tagbody.
(let* ((ctype (type-or-nil-if-unknown type))
(ctype (and ctype
(sb-kernel::replace-hairy-type ctype))))
(typecase ctype
(null (result simple-vector-widetag))
(union-type
(let ((types (union-type-types ctype)))
(cond ((not (every #'numeric-type-p types))
(result simple-vector-widetag))
((csubtypep ctype (specifier-type 'integer))
(block nil
(integer-interval-widetag
(dx-flet ((low (x)
(or (numeric-type-low x)
(return (result simple-vector-widetag)))))
(reduce #'min types :key #'low))
(dx-flet ((high (x)
(or (numeric-type-high x)
(return (result simple-vector-widetag)))))
(reduce #'max types :key #'high)))))
((csubtypep ctype (specifier-type 'double-float))
(result simple-array-double-float-widetag))
((csubtypep ctype (specifier-type 'single-float))
(result simple-array-single-float-widetag))
#+long-float
((csubtypep ctype (specifier-type 'long-float))
(result simple-array-long-float-widetag))
((csubtypep ctype (specifier-type 'complex-double-float))
(result simple-array-complex-double-float-widetag))
((csubtypep ctype (specifier-type 'complex-single-float))
(result simple-array-complex-single-float-widetag))
(t
(result simple-vector-widetag)))))
(intersection-type
(let ((types (intersection-type-types ctype)))
(loop for type in types
unless (hairy-type-p type)
return (%vector-widetag-and-n-bits-shift (type-specifier type)))))
(character-set-type
#-sb-unicode (result 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 simple-base-string-widetag)
(result simple-character-string-widetag)))
(t
(let ((expansion (type-specifier ctype)))
(if (equal expansion type)
(result simple-vector-widetag)
(%vector-widetag-and-n-bits-shift expansion)))))))))
(defun %complex-vector-widetag (widetag)
(macrolet ((make-case ()
`(case widetag
,@(loop for saetp across *specialized-array-element-type-properties*
for complex = (saetp-complex-typecode saetp)
when complex
collect (list (saetp-typecode saetp) complex))
(t
#.complex-vector-widetag))))
(make-case)))
(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- n-word-bits) (- n-bits-shift)))
(shift (- n-bits-shift
(1- (integer-length n-word-bits)))))
(ash (+ length mask) shift)))
;;; N-BITS-SHIFT is the shift amount needed to turn LENGTH into array-size-in-bits,
;;; i.e. log(2,bits-per-elt)
(defun allocate-vector-with-widetag (#+ubsan poisoned widetag length n-bits-shift)
(declare (type (unsigned-byte 8) widetag)
(type index length))
(let* ( ;; KLUDGE: add SAETP-N-PAD-ELEMENTS "by hand" since there is
;; but a single case involving it now.
(full-length (+ length (if (= widetag simple-base-string-widetag) 1 0)))
;; 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.
;; VECTOR-LENGTH-IN-WORDS potentially returns a machine-word-sized
;; integer, so it doesn't match the primitive type restriction of
;; POSITIVE-FIXNUM for the last argument of the vector alloc vops.
(nwords (the fixnum
(if (/= widetag simple-array-nil-widetag)
(vector-length-in-words full-length n-bits-shift)
0))))
#+ubsan (if poisoned ; first arg to allocate-vector must be a constant
(allocate-vector t widetag length nwords)
(allocate-vector nil widetag length nwords))
#-ubsan (allocate-vector widetag length nwords)))
(declaim (ftype (sfunction (array) (integer 128 255)) array-underlying-widetag))
(defun array-underlying-widetag (array)
(macrolet ((generate-table ()
(macrolet ((to-index (x) `(ash ,x -2)))
(let ((table (sb-xc:make-array 64 :initial-element 0
:element-type '(unsigned-byte 8))))
(dovector (saetp *specialized-array-element-type-properties*)
(let* ((typecode (saetp-typecode saetp))
(complex-typecode (saetp-complex-typecode saetp)))
(setf (aref table (to-index typecode)) typecode)
(when complex-typecode
(setf (aref table (to-index complex-typecode)) typecode))))
(setf (aref table (to-index simple-array-widetag)) 0
(aref table (to-index complex-vector-widetag)) 0
(aref table (to-index complex-array-widetag)) 0)
table)))
(to-index (x) `(ash ,x -2)))
(named-let recurse ((x array))
(let ((result (aref (generate-table)
(to-index (%other-pointer-widetag x)))))
(if (= 0 result)
(recurse (%array-data x))
(truly-the (integer 128 255) result))))))
(declaim (ftype (sfunction (array) (values (integer 128 255) (unsigned-byte 8)))
array-underlying-widetag-and-shift))
(defun array-underlying-widetag-and-shift (array)
(declare (explicit-check))
(let ((widetag (array-underlying-widetag array)))
(values widetag
(truly-the (unsigned-byte 8)
(aref %%simple-array-n-bits-shifts%% widetag)))))
;; Complain in various ways about wrong MAKE-ARRAY and ADJUST-ARRAY arguments,
;; returning the two initialization arguments needed for DATA-VECTOR-FROM-INITS.
;; This is an unhygienic macro which would be a MACROLET other than for
;; doing so would entail moving toplevel defuns around for no good reason.
(defmacro check-make-array-initargs (displaceable &optional element-type size)
`(cond ,@(when displaceable
`((displaced-to
(when (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))))
(unless (= (array-underlying-widetag displaced-to) widetag)
;; Require exact match on upgraded type (lp#1331299)
(error "Can't displace an array of type ~/sb-impl:print-type-specifier/ ~
into another of type ~/sb-impl:print-type-specifier/"
,element-type (array-element-type displaced-to)))
(when (< (array-total-size displaced-to)
(+ displaced-index-offset ,size))
(error "The :DISPLACED-TO array is too small.")))
(offset-p
(error "Can't specify :DISPLACED-INDEX-OFFSET without :DISPLACED-TO"))))
((and element-p contents-p)
(error "Can't specify both :INITIAL-ELEMENT and :INITIAL-CONTENTS"))
(element-p (values :initial-element initial-element))
(contents-p (values :initial-contents initial-contents))))
(defmacro make-array-bad-fill-pointer (actual max adjective)
;; There was a comment implying that this should be TYPE-ERROR
;; but I don't see that as a spec requirement.
`(error "Can't supply a value for :FILL-POINTER (~S) that is larger ~
than the~A size of the vector (~S)" ,actual ,adjective ,max))
(declaim (inline %save-displaced-array-backpointer
%save-displaced-new-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)))
(let ((old-data (%array-data array)))
(unless (eq old-data data)
;; 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.
(when (array-header-p old-data)
(setf (%array-displaced-from old-data)
(purge (%array-displaced-from old-data))))))))
(defun %save-displaced-new-array-backpointer (array data)
(flet ((purge (pointers)
(remove-if-not #'weak-pointer-value pointers)))
(setf (%array-displaced-from data)
(cons (make-weak-pointer array)
(purge (%array-displaced-from data))))))
(defmacro populate-dimensions (header list-or-index rank)
`(if (listp ,list-or-index)
(let ((dims ,list-or-index))
(dotimes (axis ,rank)
(declare ((integer 0 ,array-rank-limit) axis))
(%set-array-dimension ,header axis (pop dims))))
(%set-array-dimension ,header 0 ,list-or-index)))
(declaim (inline rank-and-total-size-from-dims))
(defun rank-and-total-size-from-dims (dims)
(cond ((not (listp dims)) (values 1 (the index dims)))
((not dims) (values 0 1))
(t (let ((rank 1) (product (car dims)))
(declare (%array-rank rank) (index product))
(dolist (dim (cdr dims) (values rank product))
(setq product (* product (the index dim)))
(incf rank))))))
(declaim (inline widetag->element-type))
(defun widetag->element-type (widetag)
(svref #.(let ((a (make-array 32 :initial-element 0)))
(dovector (saetp *specialized-array-element-type-properties* a)
(let ((tag (saetp-typecode saetp)))
(setf (aref a (ash (- tag #x80) -2)) (saetp-specifier saetp)))))
(- (ash widetag -2) 32)))
(defun initial-contents-error (content-length length)
(error "There are ~W elements in the :INITIAL-CONTENTS, but ~
the vector length is ~W."
content-length length))
;;; 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 element-p)
(initial-contents nil contents-p)
adjustable fill-pointer
displaced-to
(displaced-index-offset 0 offset-p))
(declare (ignore element-type))
(binding* (((array-rank total-size) (rank-and-total-size-from-dims dimensions))
((initialize initial-data)
;; element-type might not be supplied, but widetag->element is always good
(check-make-array-initargs t (widetag->element-type widetag) total-size))
(simple (and (null fill-pointer)
(not adjustable)
(null displaced-to))))
(cond ((and simple (= array-rank 1))
(let ((vector ; a (SIMPLE-ARRAY * (*))
(allocate-vector-with-widetag #+ubsan (not (or element-p contents-p))
widetag total-size n-bits)))
;; presence of at most one :INITIAL-thing keyword was ensured above
(cond (element-p
(fill vector initial-element))
(contents-p
(let ((content-length (length initial-contents)))
(unless (= total-size content-length)
(initial-contents-error content-length total-size)))
(replace vector initial-contents))
#+ubsan
(t
;; store the function which bears responsibility for creation of this
;; array in case we need to blame it for not initializing.
(set-vector-extra-data (if (= widetag simple-vector-widetag) ; no shadow bits.
vector ; use the LENGTH slot directly
(vector-extra-data vector))
(ash (sap-ref-word (current-fp) n-word-bytes) 3)) ; XXX: magic
(cond ((= widetag simple-vector-widetag)
(fill vector (%make-lisp-obj unwritten-vector-element-marker)))
((array-may-contain-random-bits-p widetag)
;; Leave the last word alone for base-string,
;; in case the mandatory trailing null is part of a data word.
(dotimes (i (- (vector-length-in-words total-size n-bits)
(if (= widetag simple-base-string-widetag) 1 0)))
(setf (%vector-raw-bits vector i) sb-ext:most-positive-word))))))
vector))
(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 total-size))
(make-array-bad-fill-pointer fill-pointer total-size "")))
(let* ((data (or displaced-to
(data-vector-from-inits dimensions total-size widetag n-bits
initialize initial-data)))
(array (make-array-header
(cond ((= array-rank 1)
(%complex-vector-widetag widetag))
(simple simple-array-widetag)
(t complex-array-widetag))
array-rank)))
(cond (fill-pointer
(logior-array-flags array +array-fill-pointer-p+)
(setf (%array-fill-pointer array)
(if (eq fill-pointer t) total-size fill-pointer)))
(t
(reset-array-flags array +array-fill-pointer-p+)
(setf (%array-fill-pointer array) total-size)))
(setf (%array-available-elements array) total-size)
(setf (%array-data array) data)
(setf (%array-displaced-from array) nil)
(cond (displaced-to
(setf (%array-displacement array) (or displaced-index-offset 0))
(setf (%array-displaced-p array) t)
(when (adjustable-array-p data)
(%save-displaced-new-array-backpointer array data)))
(t
(setf (%array-displaced-p array) nil)))
(populate-dimensions array dimensions array-rank)
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 contents-p)
(initial-element nil 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))
(check-make-array-initargs nil) ; for effect
(when 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 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* ((full-length
;; KLUDGE: add SAETP-N-PAD-ELEMENTS "by hand" since there is
;; but a single case involving it now.
(+ length (if (= type simple-base-string-widetag) 1 0)))
(vector
(allocate-static-vector type length
(vector-length-in-words full-length
n-bits-shift))))
(cond (element-p
(fill vector initial-element))
(contents-p
(replace vector initial-contents))
(t
vector)))))
#+darwin-jit
(defun make-static-code-vector (length initial-contents)
"Allocate vector of LENGTH elements in static space. Only allocation
of specialized arrays is supported."
(let ((vector (allocate-static-code-vector simple-array-unsigned-byte-8-widetag
length
(* length n-word-bytes))))
(with-pinned-objects (initial-contents)
(jit-memcpy (vector-sap vector) (vector-sap initial-contents) length))
vector))
;;; DATA-VECTOR-FROM-INITS returns a simple rank-1 array 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 widetag n-bits initialize initial-data)
(declare (fixnum widetag n-bits)) ; really just that they're non-nil
(let ((data (allocate-vector-with-widetag #+ubsan (not initialize) widetag total-size n-bits)))
(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.
;;; FIXME: how is this not redundant with DEFINE-ARRAY-DISPATCH?
;;; Which is to say, why did DEFINE-ARRAY-DISPATCH decide to do
;;; something different instead of figuring out how to unify the ways
;;; that we call an element of an array accessed by widetag?
(macrolet ((def (name table-name)
`(progn
(define-load-time-global ,table-name
(make-array ,(1+ widetag-mask)))
(declaim (type (simple-array function (,(1+ widetag-mask)))
,table-name))
(defmacro ,name (array-var &optional vector-check)
(if vector-check
`(the function
(svref ,',table-name (%other-pointer-widetag
(locally (declare (optimize (safety 1)))
(the vector ,array-var)))))
`(the function
;; Assigning TAG to 0 initially produces slightly better
;; code than would be generated by the more natural expression
;; (let ((tag (if (%other-ptr ...) (widetag ...) 0)))
;; but either way is suboptimal. As expressed, if the array-var
;; is known to satisfy %other-pointer-p, then it performs a
;; move-immediate-to-register which is clobbered right away
;; by a zero-extending load. A peephole pass could eliminate
;; the first move as effectless. If expressed the other way,
;; it would produce a jump around a jump because the compiler
;; is unwilling to *unconditionally* assign 0 into a register
;; to begin with. It actually wants to guard an immediate load
;; when it doesn't need to, as if both consequents of the IF
;; have side-effects that should not happen.
(let ((tag 0))
(when (%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 sb-impl::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))
,@(sb-impl::filter-dolist-declarations decls)
,elt
,result))
(let ((,elt (funcall ,ref ,vec ,index)))
,@decls
(tagbody ,@forms))))))))
(macrolet ((%ref (accessor-getter extra-params &optional vector-check)
`(sb-c::%funcall-no-nargs (,accessor-getter array ,vector-check) 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 ,(symbolicate 'vector- accessor-name) (array index ,@extra-params)
(declare (explicit-check)
(optimize speed (safety 0)))
(%ref ,accessor-getter ,extra-params t))
(defun ,slow-accessor-name (array index ,@extra-params)
(declare (optimize speed (safety 0))
(array array))
(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
((array array)
(index (locally
(declare (optimize (speed 1) (safety 1)))
(,@check-bounds index)))
(end)
:force-inline t)
(declare (ignore end))
(%ref ,accessor-getter ,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)
(optimize (sb-c:verify-arg-count 0)))
(error 'type-error
:datum array
:expected-type 'vector))
(macrolet ((define-reffer (saetp check-form)
(let* ((type (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)
(the index
(locally
(declare (optimize (safety 1)))
(,@check-form index))))
`(data-nil-vector-ref (the ,atype vector) index)))))
(define-setter (saetp check-form)
(let* ((type (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 sb-c::aref-context) new-value)))
;; Low-level setters return no value
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+ widetag-mask)
:initial-element #'hairy-ref-error))
,@(loop for widetag in '(complex-vector-widetag
complex-bit-vector-widetag
#+sb-unicode complex-character-string-widetag
complex-base-string-widetag
simple-array-widetag
complex-array-widetag)
collect `(setf (svref ,symbol ,widetag) ,slow-path))
,@(loop for saetp across *specialized-array-element-type-properties*
for widetag = (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))
(define-error-wrapper invalid-array-error (array)
(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))
(define-error-wrapper invalid-array-index-error (array index bound &optional axis)
(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 (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 (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 (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 (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 (dynamic-extent subscripts)
(type array array))
(setf (row-major-aref array (apply #'%array-row-major-index array subscripts))
new-value))