-
Notifications
You must be signed in to change notification settings - Fork 313
/
type-vops.lisp
1170 lines (1102 loc) · 48 KB
/
type-vops.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
;;;; type testing and checking VOPs for the x86-64 VM
;;;; 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")
;;;; test generation utilities
(defun generate-fixnum-test (value)
"Set the Z flag if VALUE is fixnum"
(inst test :byte
(cond ((ea-p value) value) ; merged a memory load + fixnump vop
((sc-is value control-stack)
(ea (frame-byte-offset (tn-offset value)) rbp-tn))
(t
;; Don't check for (ANY-REG DESCRIPTOR-REG) because VALUE
;; can have a "weird" SC that would not have tag bits.
;; Sometimes a vop temp is specified as UNSIGNED-REG, e.g.
value))
fixnum-tag-mask))
(defun %test-fixnum (value temp target not-p)
(declare (ignore temp))
(generate-fixnum-test value)
(inst jmp (if not-p :nz :z) target))
(defun %lea-for-lowtag-test (temp value lowtag &optional (operand-size :dword))
;; If OPERAND-SIZE is :QWORD, then instead of discarding the upper 32 bits
;; in a lowtag test, preserve the full word which becomes an untagged pointer.
;; The REX byte can't be avoided if VALUE is R8 or higher, so it costs nothing
;; to widen the LEA operand. In this way, we avoid emitting a displacement to
;; load the widetag. The total number of bytes emitted is reduced by 1,
;; or by nothing. ASSUMPTION: tagged pointer causes conservative pinning,
;; hence an untagged pointer remains valid.
(inst lea operand-size temp (ea (- lowtag) value)))
;; Numerics including fixnum, excluding short-float. (INTEGER,RATIONAL)
(defun %test-fixnum-and-headers (value temp target not-p headers
&key value-tn-ref immediate-tested)
(let ((drop-through (gen-label)))
(case n-fixnum-tag-bits
(1
(%lea-for-lowtag-test temp value other-pointer-lowtag :qword)
(when (types-equal-or-intersect (tn-ref-type value-tn-ref)
(specifier-type 'fixnum))
(inst test :byte temp 1)
(inst jmp :nz (if not-p drop-through target))) ; inverted
(%test-headers value temp target not-p nil headers
:drop-through drop-through :compute-temp nil
:value-tn-ref value-tn-ref
:immediate-tested immediate-tested))
(t
(generate-fixnum-test value)
(inst jmp :z (if not-p drop-through target))
(%test-headers value temp target not-p nil headers
:drop-through drop-through
:immediate-tested immediate-tested)))))
;; Numerics
(defun %test-fixnum-immediate-and-headers (value temp target not-p immediate headers
&key value-tn-ref immediate-tested)
(let ((drop-through (gen-label)))
(case n-fixnum-tag-bits
(1
(%lea-for-lowtag-test temp value other-pointer-lowtag :qword)
(when (types-equal-or-intersect (tn-ref-type value-tn-ref)
(specifier-type 'fixnum))
(inst test :byte temp 1)
(inst jmp :nz (if not-p drop-through target))) ; inverted
(when (or (/= immediate single-float-widetag)
(types-equal-or-intersect (tn-ref-type value-tn-ref)
(specifier-type 'single-float)))
(inst cmp :byte temp (- immediate other-pointer-lowtag))
(inst jmp :e (if not-p drop-through target)))
(%test-headers value temp target not-p nil headers
:drop-through drop-through :compute-temp nil
:value-tn-ref value-tn-ref
:immediate-tested immediate-tested))
(t (generate-fixnum-test value)
(inst jmp :z (if not-p drop-through target))
(%test-immediate-and-headers value temp target not-p immediate headers
:drop-through drop-through
:immediate-tested immediate-tested)))))
(defun %test-immediate (value temp target not-p immediate
&key value-tn-ref)
(declare (ignore temp value-tn-ref))
(inst cmp :byte value immediate)
(inst jmp (if not-p :ne :e) target))
;; Numerics including short-float, excluding fixnum
(defun %test-immediate-and-headers (value temp target not-p immediate headers
&key (drop-through (gen-label))
value-tn-ref
immediate-tested)
;; Code a single instruction byte test if possible.
(cond ((sc-is value any-reg descriptor-reg)
(inst cmp :byte value immediate))
(t
(move temp value) ; FIXME - why load?
(inst cmp :byte temp immediate)))
(inst jmp :e (if not-p drop-through target))
(%test-headers value temp target not-p nil headers
:drop-through drop-through
:value-tn-ref value-tn-ref
:immediate-tested immediate-tested))
(defun %test-lowtag (value temp target not-p lowtag &key value-tn-ref)
(declare (ignore value-tn-ref))
(%lea-for-lowtag-test temp value lowtag)
(inst test :byte temp lowtag-mask)
(inst jmp (if not-p :nz :z) target))
(defun %test-headers (value temp target not-p function-p headers
&key except
(drop-through (gen-label))
(load-widetag t)
(compute-temp load-widetag)
value-tn-ref
immediate-tested)
(let* ((lowtag (if function-p fun-pointer-lowtag other-pointer-lowtag))
;; It is preferable (smaller and faster code) to directly
;; compare the value in memory instead of loading it into
;; a register first. Find out if this is possible and set
;; WIDETAG-TN accordingly. If impossible, generate the
;; register load.
(widetag-tn (if (and load-widetag
(null (cdr headers))
(not except)
(or (atom (car headers))
(= (caar headers) bignum-widetag)
(= (cdar headers) complex-array-widetag)))
(ea (- lowtag) value)
temp))
(first (car headers))
(second (cadr headers))
(untagged))
(multiple-value-bind (equal less-or-equal greater-or-equal when-true
when-false)
;; EQUAL, LESS-OR-EQUAL, and GREATER-OR-EQUAL are the conditions
;; for branching to TARGET. WHEN-TRUE and WHEN-FALSE are the
;; labels to branch to when we know it's true and when we know
;; it's false respectively.
(if not-p
(values :ne :a :b drop-through target)
(values :e :na :nb target drop-through))
(cond ((not load-widetag))
((and value-tn-ref
(eq lowtag other-pointer-lowtag)
(other-pointer-tn-ref-p value-tn-ref t immediate-tested))) ; best case: lowtag is right
((and value-tn-ref
;; If HEADERS contains a range, then list pointers have to be
;; disallowed - consider a list whose CAR has a fixnum that
;; spuriously matches the range test.
(if (some #'listp headers)
(headered-object-pointer-tn-ref-p value-tn-ref)
(pointer-tn-ref-p value-tn-ref)))
;; Emit one fewer conditional jump than the general case,
(inst mov temp value)
(inst and temp (lognot lowtag-mask))
(if (ea-p widetag-tn)
(setq widetag-tn (ea temp))
(setq untagged (ea temp))))
(t
;; Regardless of whether :COMPUTE-TEMP is T or NIL, it will hold
;; an untagged ptr to VALUE if the lowtag test passes.
(setq untagged (ea temp))
(when (ea-p widetag-tn)
(setq widetag-tn untagged))
(when compute-temp
(%lea-for-lowtag-test temp value lowtag :qword))
(inst test :byte temp lowtag-mask)
(inst jmp :nz when-false)))
(when (and load-widetag
(eq widetag-tn temp))
(inst mov :dword temp (or untagged (ea (- lowtag) value))))
(dolist (widetag except)
(inst cmp :byte temp widetag)
(inst jmp :e when-false))
(cond
((and (fixnump first)
(fixnump second)
(not (cddr headers))
(= (logcount (logxor first second)) 1))
;; Two widetags differing at one bit. Use one cmp and branch.
;; Start by ORing in the bit that they differ on.
(let ((diff-bit (logxor first second)))
(aver (not (ea-p widetag-tn))) ; can't clobber a header
(inst or :byte widetag-tn diff-bit)
(inst cmp :byte widetag-tn (logior first diff-bit))
(if not-p (inst jmp :ne target) (inst jmp :eq target))))
(t
;; Compared to x86 we additionally optimize the cases of a
;; range starting with BIGNUM-WIDETAG (= min widetag)
;; or ending with COMPLEX-ARRAY-WIDETAG (= max widetag)
(do ((remaining headers (cdr remaining)))
((null remaining))
(let ((header (car remaining))
(last (null (cdr remaining))))
(cond
((and (eql header simple-array-widetag)
value-tn-ref
(csubtypep (tn-ref-type value-tn-ref) (specifier-type 'string))))
((atom header)
(inst cmp :byte widetag-tn header)
(if last
(inst jmp equal target)
(inst jmp :e when-true)))
(t
(let ((start (car header))
(end (cdr header)))
(cond
((= start bignum-widetag)
(inst cmp :byte widetag-tn end)
(if last
(inst jmp less-or-equal target)
(inst jmp :be when-true)))
((= end complex-array-widetag)
(inst cmp :byte widetag-tn start)
(if last
(inst jmp greater-or-equal target)
(inst jmp :b when-false)))
((not last)
(inst cmp :byte temp start)
(inst jmp :b when-false)
(inst cmp :byte temp end)
(inst jmp :be when-true))
(t
(inst sub :byte temp start)
(inst cmp :byte temp (- end start))
(inst jmp less-or-equal target))))))))))
(emit-label drop-through))))
;;;; other integer ranges
(define-vop (simple-type-predicate)
(:args (value :scs (any-reg descriptor-reg control-stack)))
(:conditional)
(:arg-refs args)
(:policy :fast-safe))
(define-vop (fixnump/unsigned-byte-64 simple-type-predicate)
(:args (value :scs (unsigned-reg)))
(:arg-types unsigned-num)
(:translate fixnump)
(:temporary (:sc unsigned-reg :from (:argument 0)) tmp)
(:conditional :z)
(:generator 3
(move tmp value)
(inst shr tmp n-positive-fixnum-bits)))
#-#.(cl:if (cl:= sb-vm:n-fixnum-tag-bits 1) '(:and) '(:or))
(define-vop (fixnump/signed-byte-64 simple-type-predicate)
(:args (value :scs (signed-reg)))
(:conditional :z)
(:temporary (:sc unsigned-reg) temp)
(:arg-types signed-num)
(:translate fixnump)
(:generator 3
;; Hackers Delight, p. 53: signed
;; a <= x <= a + 2^n - 1
;; is equivalent to unsigned
;; ((x-a) >> n) = 0
(inst mov temp #.(- most-negative-fixnum))
(inst add temp value)
(inst shr temp n-fixnum-bits)))
#+#.(cl:if (cl:= sb-vm:n-fixnum-tag-bits 1) '(:and) '(:or))
(define-vop (fixnump/signed-byte-64 simple-type-predicate)
(:args (value :scs (signed-reg) :target temp))
(:conditional :no)
(:temporary (:sc unsigned-reg :from (:argument 0)) temp)
(:arg-types signed-num)
(:translate fixnump)
(:generator 3
(move temp value)
;; The overflow flag will be set if the reg's sign bit changes.
(inst shl temp 1)))
;;; A (SIGNED-BYTE 64) can be represented with either fixnum or a bignum with
;;; exactly one digit.
(define-vop (pointerp)
(:args (value :scs (any-reg descriptor-reg)))
(:temporary (:sc unsigned-reg :from (:argument 0)) temp)
(:conditional :z)
(:policy :fast-safe)
(:translate pointerp)
(:generator 3
(if (location= temp value) (inst sub :dword value 3) (inst lea :dword temp (ea -3 value)))
(inst test :byte temp #b11)))
;; A fixnum or single-digit bignum satisfies signed-byte-64-p
(define-vop (signed-byte-64-p pointerp)
(:translate signed-byte-64-p)
(:conditional :z)
(:arg-refs arg-ref)
(:generator 6
(when (types-equal-or-intersect (tn-ref-type arg-ref) (specifier-type 'fixnum))
(inst test :byte value fixnum-tag-mask)
(inst jmp :z out)) ; good
(let ((ea (cond ((fixnum-or-other-pointer-tn-ref-p arg-ref t)
(ea (- other-pointer-lowtag) value))
(t
(%lea-for-lowtag-test temp value other-pointer-lowtag :qword)
(inst test :byte temp lowtag-mask)
(inst jmp :nz out)
(ea temp)))))
(inst cmp :qword ea (bignum-header-for-length 1)))
OUT))
(define-vop (signed-byte-64-p/unsigned)
(:args (value :scs (unsigned-reg)))
(:arg-types unsigned-num)
(:conditional :ns)
(:policy :fast-safe)
(:translate signed-byte-64-p)
(:generator 5
(inst test value value)))
(macrolet ((define (name src-size)
`(define-vop (,(symbolicate name "/SIGNED"))
(:translate ,name)
(:args (value :scs (signed-reg)))
(:arg-types signed-num)
(:conditional :z)
(:policy :fast-safe)
(:temporary (:sc unsigned-reg) temp)
(:generator 2
(inst movsx '(,src-size :qword) temp value)
(inst cmp temp value)))))
(define signed-byte-8-p :byte)
(define signed-byte-16-p :word)
(define signed-byte-32-p :dword))
(define-vop (signed-byte-8-p)
(:translate signed-byte-8-p)
(:args (value :scs (any-reg descriptor-reg)))
(:conditional :z)
(:arg-refs arg-ref)
(:policy :fast-safe)
(:temporary (:sc unsigned-reg) temp)
(:generator 6
(inst lea temp (ea (ash (expt 2 7) n-fixnum-tag-bits) value))
(inst test temp (lognot (fixnumize (1- (expt 2 8)))))))
(define-vop (signed-byte-16-p)
(:translate signed-byte-16-p)
(:args (value :scs (any-reg descriptor-reg)))
(:conditional :z)
(:arg-refs arg-ref)
(:policy :fast-safe)
(:temporary (:sc unsigned-reg) temp)
(:generator 6
(inst lea temp (ea (ash (expt 2 15) n-fixnum-tag-bits) value))
(inst test temp (lognot (fixnumize (1- (expt 2 16)))))))
(define-vop (signed-byte-32-p)
(:translate signed-byte-32-p)
(:args (value :scs (any-reg descriptor-reg)))
(:conditional :z)
(:arg-refs arg-ref)
(:policy :fast-safe)
(:temporary (:sc unsigned-reg) temp temp2)
(:generator 6
(move temp value)
(inst sar temp n-fixnum-tag-bits)
(inst movsx '(:dword :qword) temp2 temp)
(inst cmp temp2 temp)
(unless (csubtypep (tn-ref-type arg-ref) (specifier-type 'fixnum))
(inst jmp :nz out)
(inst test :byte value fixnum-tag-mask))
out))
;;; Sign bit and fixnum tag bit.
(defconstant non-negative-fixnum-mask-constant
#x8000000000000001)
(defconstant non-negative-fixnum-mask-constant-wired-address
(+ static-space-start (* 12 n-word-bytes)))
;; the preceding constant is embedded in an array,
;; the header of which must not overlap the static alloc regions
#-sb-thread
(aver (>= (- non-negative-fixnum-mask-constant-wired-address (* 2 n-word-bytes))
(+ static-space-start
(max boxed-region-offset
cons-region-offset
mixed-region-offset)
(* 3 n-word-bytes))))
;;; An (unsigned-byte 64) can be represented with either a positive
;;; fixnum, a bignum with exactly one positive digit, or a bignum with
;;; exactly two digits and the second digit all zeros.
(define-vop (unsigned-byte-64-p type-predicate)
(:translate unsigned-byte-64-p)
(:generator 10
(let* ((not-target (gen-label))
(single-word (gen-label))
(fixnum-p (types-equal-or-intersect (tn-ref-type args) (specifier-type 'fixnum)))
(not-signed-byte-64-p (not (types-equal-or-intersect (tn-ref-type args) (specifier-type 'signed-word))))
(unsigned-p (or not-signed-byte-64-p
(not (types-equal-or-intersect (tn-ref-type args) (specifier-type '(integer * -1)))))))
(multiple-value-bind (yep nope)
(if not-p
(values not-target target)
(values target not-target))
(when fixnum-p
(cond (unsigned-p
(inst test :byte value fixnum-tag-mask)
(inst jmp :z yep))
(t ;; Is it a fixnum with the sign bit clear?
(inst test (ea non-negative-fixnum-mask-constant-wired-address) value)
(inst jmp :z yep))))
(cond ((fixnum-or-other-pointer-tn-ref-p args t)
(when (and fixnum-p
(not unsigned-p))
(inst test :byte value fixnum-tag-mask)
(inst jmp :z nope)))
(t
(%lea-for-lowtag-test temp value other-pointer-lowtag)
(inst test :byte temp lowtag-mask)
(inst jmp :ne nope)))
;; Get the header.
(loadw temp value 0 other-pointer-lowtag)
(unless not-signed-byte-64-p
;; Is it one?
(inst cmp temp (bignum-header-for-length 1))
(inst jmp :e (if unsigned-p
yep
single-word)))
;; If it's other than two, we can't be an (unsigned-byte 64)
;: Leave TEMP holding 0 in the affirmative case.
(inst sub temp (bignum-header-for-length 2))
(inst jmp :ne nope)
;; Compare the second digit to zero (in TEMP).
(inst cmp (object-slot-ea value (1+ bignum-digits-offset) other-pointer-lowtag)
temp)
(cond (unsigned-p
(inst jmp (if not-p :nz :z) target))
(t
(inst jmp :z yep) ; All zeros, its an (unsigned-byte 64).
(inst jmp nope)))
(unless unsigned-p
(emit-label single-word)
;; Get the single digit.
(loadw temp value bignum-digits-offset other-pointer-lowtag)
;; positive implies (unsigned-byte 64).
(inst test temp temp)
(inst jmp (if not-p :s :ns) target))
(emit-label not-target)))))
(define-vop (unsigned-byte-x-p type-predicate)
(:arg-types * (:constant (integer 1)))
(:translate sb-c::unsigned-byte-x-p)
(:info target not-p x)
(:temporary (:sc unsigned-reg) last-digit)
(:generator 10
(let* ((type (tn-ref-type args))
(fixnum-p (types-equal-or-intersect type (specifier-type 'fixnum)))
(integer-p (csubtypep type (specifier-type 'integer)))
(unsigned-p (not (types-equal-or-intersect type (specifier-type '(integer * -1))))))
(multiple-value-bind (yep nope)
(if not-p
(values not-target target)
(values target not-target))
(assemble ()
(when fixnum-p
(cond (unsigned-p
(inst test :byte value fixnum-tag-mask)
(inst jmp :z yep))
(t ;; Is it a fixnum with the sign bit clear?
(inst test (ea non-negative-fixnum-mask-constant-wired-address) value)
(inst jmp :z yep))))
(cond ((fixnum-or-other-pointer-tn-ref-p args t)
(when (and fixnum-p
(not unsigned-p))
(inst test :byte value fixnum-tag-mask)
(inst jmp :z nope)))
(t
(%lea-for-lowtag-test temp value other-pointer-lowtag)
(inst test :byte temp lowtag-mask)
(inst jmp :ne nope)))
;; Get the header.
(loadw temp value 0 other-pointer-lowtag)
(unless integer-p
(inst cmp :byte temp bignum-widetag)
(inst jmp :ne nope))
(inst shr temp n-widetag-bits)
(inst cmp :dword temp (1+ (/ x n-word-bits)))
(inst jmp :g nope)
;; Is it a sign-extended sign bit
(cond (unsigned-p
(inst jmp :l yep)
(inst cmp :dword (ea (+ (- other-pointer-lowtag) (/ n-word-bytes 2))
value temp n-word-bytes)
0)
(inst jmp (if not-p :nz :z) target))
(t
(inst mov last-digit (ea (- other-pointer-lowtag) value temp n-word-bytes))
(inst jmp :l fixnum)
(inst test last-digit last-digit)
(inst jmp :nz nope)))
fixnum
(unless unsigned-p
(inst test last-digit last-digit)
(inst jmp (if not-p :s :ns) target)))))
not-target))
;;; SINGLE-FLOAT-P, CHARACTERP, UNBOUND-MARKER-P produce a flag result
;;; and never need a temporary.
(macrolet ((define (name widetag)
`(define-vop (,name simple-type-predicate)
(:translate ,name)
(:conditional :z)
(:generator 1 (inst cmp :byte value ,widetag)))))
(define single-float-p single-float-widetag)
(define characterp character-widetag)
(define unbound-marker-p unbound-marker-widetag))
;;; FUNCTIONP, LISTP, %INSTANCEP, %OTHER-POINTER-P produce a flag result
(macrolet ((define (name lowtag)
`(define-vop (,name pointerp)
(:translate ,name)
(:generator 2
(if (location= temp value)
(inst sub :dword value ,lowtag)
(inst lea :dword temp (ea (- ,lowtag) value)))
(inst test :byte temp lowtag-mask)))))
(define functionp fun-pointer-lowtag)
(define listp list-pointer-lowtag)
(define %instancep instance-pointer-lowtag)
(define %other-pointer-p other-pointer-lowtag))
;;; Function subtypes produce a flag result
(macrolet ((define (name widetag)
`(define-vop (,name type-predicate)
(:translate ,name)
(:info) ; nullify the info
(:conditional :z)
(:generator 4
(inst lea temp (ea (- fun-pointer-lowtag) value))
(inst test :byte temp lowtag-mask)
(inst jmp :ne out)
(inst cmp :byte (ea temp) ,widetag)
out))))
(define closurep closure-widetag)
(define simple-fun-p simple-fun-widetag)
(define funcallable-instance-p funcallable-instance-widetag))
;;; Various OTHER-POINTER objects produce a flag result.
;;; The parens around widetag are from copy&paste of generic/type-vops
(macrolet ((define (name (widetag))
`(define-vop (,name type-predicate)
(:translate ,name)
(:info) ; nullify the info
(:conditional :z)
(:generator 4
(test-other-ptr value args ,widetag temp out)
out))))
(define bignump (bignum-widetag))
(define ratiop (ratio-widetag))
(define complex-rational-p (complex-rational-widetag))
(define complex-single-float-p (complex-single-float-widetag))
(define complex-double-float-p (complex-double-float-widetag))
(define double-float-p (double-float-widetag))
(define system-area-pointer-p (sap-widetag))
(define weak-pointer-p (weak-pointer-widetag))
(define code-component-p (code-header-widetag))
(define fdefn-p (fdefn-widetag))
(define simple-array-header-p (simple-array-widetag))
(define complex-vector-p (complex-vector-widetag)))
(macrolet ((fail-if-not-otherptr ()
`(cond ((other-pointer-tn-ref-p value-tn-ref t)
(inst mov :byte temp (ea (- other-pointer-lowtag) value)))
(t
(inst lea temp (ea (- other-pointer-lowtag) value))
(inst test :byte temp lowtag-mask)
;; TEST clears the Carry, so if this jump occurs,
;; it returns the correct answer for the vops that return
;; their result in Z or C.
(inst jmp :nz out)
(inst mov :byte temp (ea temp))))))
#+sb-unicode
(macrolet ((define (name (simple nonsimple))
(aver (= (logior 8 (symbol-value simple)) (symbol-value nonsimple)))
`(define-vop (,name type-predicate)
(:translate ,name)
(:info) ; nullify the info
(:conditional :z)
(:arg-refs value-tn-ref)
(:generator 4
(fail-if-not-otherptr)
(inst or :byte temp 8)
(inst cmp :byte temp ,nonsimple)
out))))
(define base-string-p (simple-base-string-widetag complex-base-string-widetag))
(define character-string-p
(simple-character-string-widetag complex-character-string-widetag)))
(macrolet ((define (name widetags)
(let* ((widetags (symbol-value widetags))
(min (reduce #'min widetags))
(max (reduce #'max widetags)))
`(define-vop (,name type-predicate)
(:translate ,name)
(:info)
(:conditional :c) ; Carry flag = "below" (unsigned)
(:arg-refs value-tn-ref)
(:generator 4
(fail-if-not-otherptr)
(inst sub :byte temp ,min)
(inst cmp :byte temp ,(1+ (- max min)))
OUT)))))
(define simple-rank-1-array-*-p +simple-rank-1-array-widetags+)
(define vectorp +vector-widetags+)
(define simple-array-p +simple-array-widetags+)
#+sb-unicode (define stringp +string-widetags+)))
;;;; list/symbol types
;;;
;;; symbolp (or symbol (eq nil))
;;; consp (and list (not (eq nil)))
;;; Test whether ARG is an other-pointer to WIDETAG, setting the Z flag if so
(defun test-other-ptr (arg arg-ref widetag temp label &optional (permit-nil t))
(inst cmp :byte
(cond ((other-pointer-tn-ref-p arg-ref permit-nil)
(ea (- other-pointer-lowtag) arg))
(t
(%lea-for-lowtag-test temp arg other-pointer-lowtag :qword)
(inst test :byte temp lowtag-mask)
(inst jmp :ne label)
(ea temp)))
widetag))
(define-vop (symbolp type-predicate)
(:translate symbolp)
(:info)
(:conditional :z)
(:generator 5
;; If the VALUE were known to be an OTHER-POINTER, the IR1 lvar type
;; can't intersect NULL. So the IR1 should have been transformed to NON-NULL-SYMBOL-P.
;; Hence this must *not* be known to be an OTHER-POINTER.
(aver (not (other-pointer-tn-ref-p args)))
(if (other-pointer-tn-ref-p args t) ; allow NIL
(inst cmp :byte (ea (- other-pointer-lowtag) value) symbol-widetag)
(assemble ()
(%lea-for-lowtag-test temp value other-pointer-lowtag :qword)
(inst test :byte temp lowtag-mask)
(inst jmp :e compare-widetag)
(inst cmp value nil-value)
(inst jmp out)
compare-widetag
(inst cmp :byte (ea temp) symbol-widetag)))
out))
(define-vop (non-null-symbol-p symbolp)
(:translate non-null-symbol-p)
(:generator 3
(test-other-ptr value args symbol-widetag temp out nil)
out))
;;; It would be far better if we could recognize the IR1 for
;;; (AND (CONSP X) (EQ (CAR X) 'FOO))
;;; rather than treating (TYPEP X '(CONS (EQL FOO))) as a special case,
;;; but hey at least this provides the IR2 support for it.
(define-vop (car-eq-if-listp)
(:args (value :scs (descriptor-reg))
(obj :scs (immediate any-reg descriptor-reg)))
(:temporary (:sc unsigned-reg) temp)
(:conditional :z)
(:policy :fast-safe)
(:translate car-eq-if-listp)
(:generator 3
(inst lea temp (ea (- list-pointer-lowtag) value))
(inst test :byte temp lowtag-mask)
(inst jmp :nz out)
(inst cmp :qword (ea temp) (encode-value-if-immediate obj))
out))
(eval-when (:compile-toplevel) (aver (= sb-impl::package-id-bits 16)))
(define-vop (keywordp symbolp)
(:translate keywordp)
(:generator 3
(cond ((csubtypep (tn-ref-type args) (specifier-type 'symbol))
(inst cmp :word (ea (+ (ash symbol-name-slot word-shift) 6
(- other-pointer-lowtag))
value)
sb-impl::+package-id-keyword+))
(t
(inst lea temp (ea (- other-pointer-lowtag) value))
(inst test :byte temp lowtag-mask)
(inst jmp :ne out)
(inst cmp :byte (ea temp) symbol-widetag)
(inst jmp :ne out)
(inst cmp :word (ea (+ (ash symbol-name-slot word-shift) 6) temp)
sb-impl::+package-id-keyword+)))
out))
(define-vop (consp type-predicate)
(:translate consp)
(:generator 8
(let ((is-not-cons-label (if not-p target DROP-THRU)))
(inst cmp value nil-value)
(inst jmp :e is-not-cons-label)
(test-type value temp target not-p (list-pointer-lowtag)))
DROP-THRU))
(define-vop (widetag=)
(:translate widetag=)
(:policy :fast-safe)
(:args (x :scs (descriptor-reg)))
(:info widetag)
(:arg-types * (:constant t))
(:conditional :e)
(:arg-refs args)
(:generator 2
(inst cmp :byte (ea (- other-pointer-lowtag) x) widetag)))
#+compact-instance-header
(progn
(define-vop ()
(:translate %instance-layout)
(:policy :fast-safe)
(:args (object :scs (descriptor-reg)))
(:results (res :scs (descriptor-reg)))
(:variant-vars lowtag)
(:variant instance-pointer-lowtag)
(:generator 1
(inst mov :dword res (ea (- 4 lowtag) object))))
(define-vop ()
(:translate %set-instance-layout)
(:policy :fast-safe)
(:args (object :scs (descriptor-reg))
(value :scs (any-reg descriptor-reg)))
(:vop-var vop)
(:temporary (:sc unsigned-reg) temp)
(:generator 1
(emit-gengc-barrier object nil temp (vop-nth-arg 1 vop) value)
(inst mov :dword (ea (- 4 instance-pointer-lowtag) object) value)))
(define-vop (%fun-layout %instance-layout)
(:translate %fun-layout)
(:variant fun-pointer-lowtag))
(define-vop (%set-fun-layout %set-instance-layout)
(:translate %set-fun-layout)
(:generator 1
(emit-gengc-barrier object nil temp (vop-nth-arg 1 vop) value)
(inst mov :dword (ea (- 4 fun-pointer-lowtag) object) value)))
(define-vop ()
(:translate sb-c::layout-eq)
(:policy :fast-safe)
(:conditional :e)
(:args (object :scs (descriptor-reg))
(layout :scs (descriptor-reg immediate)))
(:arg-types * * (:constant t))
(:info lowtag)
(:generator 1
(inst cmp :dword (ea (- 4 lowtag) object)
(if (sc-is layout immediate)
(make-fixup (tn-value layout) :layout)
layout)))))
;;; Return the DISP part of an EA based on MEM-OP,
;;; which is a memory access vop such as INSTANCE-REF.
;;; Return NIL if the access can't be absorbed into a following instruction.
(defun valid-memref-byte-disp (mem-op &aux (info (vop-codegen-info mem-op)))
(ecase (vop-name mem-op)
(instance-index-ref-c
;; for historical reasons, this has a "-C" variant which takes an info arg
(destructuring-bind (index) info
(- (ash (+ index instance-slots-offset) word-shift)
instance-pointer-lowtag)))
((%raw-instance-ref/word %raw-instance-ref/signed-word)
;; raw slot vops accept an immediate TN, not a codegen arg
(let ((index (tn-ref-tn (tn-ref-across (vop-args mem-op)))))
(when (sc-is index immediate)
(- (ash (+ (tn-value index) instance-slots-offset) word-shift)
instance-pointer-lowtag))))
(slot
(destructuring-bind (name index lowtag) info
(declare (ignore name))
(- (ash index word-shift) lowtag)))
(data-vector-ref-with-offset/simple-vector-c
(destructuring-bind (index offset) info
(let ((disp (- (ash (+ vector-data-offset index offset) word-shift)
other-pointer-lowtag)))
(if (typep disp '(signed-byte 32)) disp))))))
(define-vop (fixnump simple-type-predicate)
(:translate fixnump)
(:arg-refs arg-ref)
(:args (value :scs (any-reg descriptor-reg)))
(:conditional :z)
;; the compiler is very sensitive to this cost here as regards boxing. DON'T TOUCH !!!
(:generator 3
(awhen (tn-ref-memory-access arg-ref)
(setq value (ea (cdr it) value)))
(generate-fixnum-test value)))
(macrolet
((define-simple-array-type-vops ()
`(progn
,@(map 'list
(lambda (saetp &aux (primtype (saetp-primitive-type-name saetp))
(name (symbolicate primtype "-P")))
`(define-vop (,name symbolp)
(:translate ,name)
(:generator 4
(test-other-ptr value args ,(saetp-typecode saetp) temp out)
out)))
*specialized-array-element-type-properties*))))
(define-simple-array-type-vops))
;;; Try to absorb a memory load into FIXNUMP.
(defoptimizer (sb-c::vop-optimize fixnump) (vop)
;; Ensure that the fixnump vop does not try to absorb more than one memref.
;; That is, if the initial IR2 matches (fixnump (memref (memref))) which is simplified
;; to (memref+fixnump (memref x)), it would seem to allow matching of the pattern
;; again if this optimizer is reapplied, because the "new" fixnump vop is superficially
;; the same, except for the attachment of extra data to its input.
(unless (tn-ref-memory-access (vop-args vop))
(let ((prev (sb-c::previous-vop-is
vop
'(instance-index-ref-c slot
;; FIXME: could we also handle the non "-C" vop?
;; The problem is that because FIXNUMP only takes one arg,
;; an additional TN would have to be grafted into the data flow
;; to convey the INDEX arg. So it's not the same fixnump vop
;; any more - it's like a two-arg variant of fixnump.
data-vector-ref-with-offset/simple-vector-c))))
;; Inhibit the optimization on simple-vector if we need to trap uninitialized reads.
;; #+ubsan always inhibits, otherwise it's policy-based
(when (and prev
(eq (vop-name prev) 'data-vector-ref-with-offset/simple-vector-c)
#-ubsan (sb-c::policy (sb-c::vop-node vop) (= safety 3)))
(return-from vop-optimize-fixnump-optimizer nil))
(aver (not (vop-results vop))) ; is a :CONDITIONAL vop
(when (and prev (eq (vop-block prev) (vop-block vop)))
(let ((arg (vop-args vop)))
(when (and (eq (tn-ref-tn (vop-results prev)) (tn-ref-tn arg))
(sb-c::very-temporary-p (tn-ref-tn arg)))
(binding* ((disp (valid-memref-byte-disp prev) :exit-if-null)
(arg-ref
(sb-c:reference-tn (tn-ref-tn (vop-args prev)) nil))
(new (sb-c::emit-and-insert-vop
(sb-c::vop-node vop) (vop-block vop) (sb-c::vop-info vop)
arg-ref nil prev (vop-codegen-info vop))))
(setf (tn-ref-memory-access arg-ref) `(:read . ,disp))
(sb-c::delete-vop prev)
(sb-c::delete-vop vop)
new)))))))
(define-vop (>-integer-fixnum)
(:translate >)
(:args (integer :scs (descriptor-reg))
(fixnum :scs (immediate any-reg)))
(:arg-types (:or integer bignum) tagged-num)
(:temporary (:sc unsigned-reg) temp)
(:conditional)
(:info target not-p)
(:arg-refs integer-ref)
(:policy :fast-safe)
(:variant-vars comparison)
(:variant :g)
(:generator 8
(let* ((integer-p (csubtypep (tn-ref-type integer-ref) (specifier-type 'integer)))
(fixnum (if (sc-is fixnum immediate)
(let* ((value (fixnumize (tn-value fixnum)))
(one (fixnumize 1)))
(cond ((plausible-signed-imm32-operand-p value)
value)
((and (plausible-signed-imm32-operand-p (+ value one))
(or (and (eql comparison :le)
(setf comparison :l))
(and (eql comparison :g)
(setf comparison :ge))))
(setf value (+ value one)))
((and (plausible-signed-imm32-operand-p (- value one))
(or (and (eql comparison :ge)
(setf comparison :g))
(and (eql comparison :l)
(setf comparison :le))))
(setf value (- value one)))
(t
(inst mov temp value)
temp)))
fixnum)))
(multiple-value-bind (yep nope)
(if not-p
(values not-target target)
(values target not-target))
(assemble ()
(when (types-equal-or-intersect (tn-ref-type integer-ref) (specifier-type 'fixnum))
(generate-fixnum-test integer)
(cond
((and (eql fixnum 0)
(memq comparison '(:ge :l))
(csubtypep (type-intersection (tn-ref-type integer-ref)
(specifier-type 'fixnum))
(specifier-type '(integer * -1))))
(inst jmp :z (if (eq comparison :l)
yep
nope)))
((and (eql fixnum 0)
(memq comparison '(:ge :l))
(csubtypep (type-intersection (tn-ref-type integer-ref)
(specifier-type 'fixnum))
(specifier-type '(integer 0))))
(inst jmp :z (if (eq comparison :l)
nope
yep)))
(t
(inst jmp :nz BIGNUM)
(cond ((eql fixnum 0)
(inst test integer integer))
((or (and (eql fixnum #.(fixnumize -1))
(or (and (eql comparison :le)
(setf comparison :l))
(and (eql comparison :g)
(setf comparison :ge))))
(and (eql fixnum #.(fixnumize 1))
(or (and (eql comparison :ge)
(setf comparison :g))
(and (eql comparison :l)
(setf comparison :le)))))
(inst test integer integer))
(t
(inst cmp integer fixnum)))
(inst jmp comparison yep)
(inst jmp nope))))
bignum
(unless (fixnum-or-other-pointer-tn-ref-p integer-ref t)
(test-type integer temp nope t (other-pointer-lowtag)))
(loadw temp integer 0 other-pointer-lowtag)
(unless integer-p
(inst cmp :byte temp bignum-widetag)
(inst jmp :ne nope))
#.(assert (= (integer-length bignum-widetag) 5))
(inst shr temp 5)
(inst cmp :dword (ea (+ (- other-pointer-lowtag) (/ n-word-bytes 2)) integer temp) 0)
(inst jmp (case comparison
((:l :le) (if not-p :ge :l))
(t (if not-p :l :ge)))
target))))
not-target))
(define-vop (<-integer-fixnum >-integer-fixnum)
(:translate <)
(:variant :l))
(define-vop (>-fixnum-integer >-integer-fixnum)
(:translate >)
(:args (fixnum :scs (immediate any-reg))
(integer :scs (descriptor-reg)))
(:arg-types tagged-num (:or integer bignum))
(:arg-refs nil integer-ref)
(:variant :l))
(define-vop (<-fixnum-integer >-fixnum-integer)
(:translate <)
(:variant :g))
;;; For integerp+cmp
(define-vop (<=-integer-fixnum >-integer-fixnum)
(:translate)
(:variant :le))
(define-vop (>=-integer-fixnum <-integer-fixnum)
(:translate)
(:variant :ge))
(define-vop (<=-fixnum-integer >-fixnum-integer)
(:translate)
(:variant :ge))
(define-vop (>=-fixnum-integer <-fixnum-integer)
(:translate)
(:variant :le))
(define-vop (load-other-pointer-widetag)
(:args (value :scs (any-reg descriptor-reg)))
(:arg-refs value-ref)
(:info not-other-pointer-label null-label zero-extend)
(:results (r :scs (unsigned-reg)))
(:result-types unsigned-num)
(:generator 1
(when null-label
(if (types-equal-or-intersect
(type-difference (tn-ref-type value-ref) (specifier-type 'null))
(specifier-type 'cons))