x86-64 disentwingling of fixnums and words.
[sbcl.git] / src / compiler / x86-64 / values.lisp
1 ;;;; unknown-values VOPs for the x86 VM
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!VM")
13
14 (define-vop (reset-stack-pointer)
15   (:args (ptr :scs (any-reg)))
16   (:generator 1
17     (move rsp-tn ptr)))
18
19 (define-vop (%%nip-values)
20   (:args (last-nipped-ptr :scs (any-reg) :target rdi)
21          (last-preserved-ptr :scs (any-reg) :target rsi)
22          (moved-ptrs :scs (any-reg) :more t))
23   (:results (r-moved-ptrs :scs (any-reg) :more t)
24             ;; same as MOVED-PTRS
25             )
26   (:temporary (:sc any-reg :offset rsi-offset) rsi)
27   (:temporary (:sc any-reg :offset rdi-offset) rdi)
28   (:ignore r-moved-ptrs)
29   (:generator 1
30     (move rdi last-nipped-ptr)
31     (move rsi last-preserved-ptr)
32     (inst sub rsi n-word-bytes)
33     (inst sub rdi n-word-bytes)
34     (inst cmp rsp-tn rsi)
35     (inst jmp :a DONE)
36     (inst std)
37     LOOP
38     (inst movs :qword)
39     (inst cmp rsp-tn rsi)
40     (inst jmp :be LOOP)
41     (inst cld)
42     DONE
43     (inst lea rsp-tn (make-ea :qword :base rdi :disp n-word-bytes))
44     (inst sub rdi rsi)
45     (loop for moved = moved-ptrs then (tn-ref-across moved)
46           while moved
47           do (inst add (tn-ref-tn moved) rdi))))
48
49 ;;; Push some values onto the stack, returning the start and number of values
50 ;;; pushed as results. It is assumed that the Vals are wired to the standard
51 ;;; argument locations. Nvals is the number of values to push.
52 ;;;
53 ;;; The generator cost is pseudo-random. We could get it right by defining a
54 ;;; bogus SC that reflects the costs of the memory-to-memory moves for each
55 ;;; operand, but this seems unworthwhile.
56 (define-vop (push-values)
57   (:args (vals :more t))
58   (:temporary (:sc unsigned-reg :to (:result 0) :target start) temp)
59   (:results (start) (count))
60   (:info nvals)
61   (:generator 20
62     (move temp rsp-tn)                  ; WARN pointing 1 below
63     (do ((val vals (tn-ref-across val)))
64         ((null val))
65       (inst push (tn-ref-tn val)))
66     (move start temp)
67     (inst mov count (fixnumize nvals))))
68
69 ;;; Push a list of values on the stack, returning Start and Count as used in
70 ;;; unknown values continuations.
71 (define-vop (values-list)
72   (:args (arg :scs (descriptor-reg) :target list))
73   (:arg-types list)
74   (:policy :fast-safe)
75   (:results (start :scs (any-reg))
76             (count :scs (any-reg)))
77   (:temporary (:sc descriptor-reg :from (:argument 0) :to (:result 1)) list)
78   (:temporary (:sc descriptor-reg :to (:result 1)) nil-temp)
79   (:temporary (:sc unsigned-reg :offset rax-offset :to (:result 1)) rax)
80   (:vop-var vop)
81   (:save-p :compute-only)
82   (:generator 0
83     (move list arg)
84     (move start rsp-tn)                 ; WARN pointing 1 below
85     (inst mov nil-temp nil-value)
86
87     LOOP
88     (inst cmp list nil-temp)
89     (inst jmp :e DONE)
90     (pushw list cons-car-slot list-pointer-lowtag)
91     (loadw list list cons-cdr-slot list-pointer-lowtag)
92     (inst mov rax list)
93     (inst and al-tn lowtag-mask)
94     (inst cmp al-tn list-pointer-lowtag)
95     (inst jmp :e LOOP)
96     (error-call vop 'bogus-arg-to-values-list-error list)
97
98     DONE
99     (inst mov count start)              ; start is high address
100     (inst sub count rsp-tn)             ; stackp is low address
101     #!-#.(cl:if (cl:= sb!vm:word-shift sb!vm:n-fixnum-tag-bits) '(and) '(or))
102     (inst shr count (- word-shift n-fixnum-tag-bits))))
103
104 ;;; Copy the more arg block to the top of the stack so we can use them
105 ;;; as function arguments.
106 ;;;
107 ;;; Accepts a context as produced by more-arg-context; points to the first
108 ;;; value on the stack, not 4 bytes above as in other contexts.
109 ;;;
110 ;;; Return a context that is 4 bytes above the first value, suitable for
111 ;;; defining a new stack frame.
112 (define-vop (%more-arg-values)
113   (:args (context :scs (descriptor-reg any-reg) :target src)
114          (skip :scs (any-reg immediate))
115          (num :scs (any-reg) :target count))
116   (:arg-types * positive-fixnum positive-fixnum)
117   (:temporary (:sc any-reg :offset rsi-offset :from (:argument 0)) src)
118   (:temporary (:sc descriptor-reg :offset rax-offset) temp)
119   (:temporary (:sc unsigned-reg :offset rcx-offset) loop-index)
120   (:results (start :scs (any-reg))
121             (count :scs (any-reg)))
122   (:generator 20
123     (sc-case skip
124       (immediate
125        (cond ((zerop (tn-value skip))
126               (move src context)
127               (move count num))
128              (t
129               (inst lea src (make-ea :dword :base context
130                                      :disp (- (* (tn-value skip)
131                                                  n-word-bytes))))
132               (move count num)
133               (inst sub count (* (tn-value skip) n-word-bytes)))))
134
135       (any-reg
136        (move src context)
137        #!+#.(cl:if (cl:= sb!vm:word-shift sb!vm:n-fixnum-tag-bits) '(and) '(or))
138        (inst sub src skip)
139        #!-#.(cl:if (cl:= sb!vm:word-shift sb!vm:n-fixnum-tag-bits) '(and) '(or))
140        (progn
141          ;; FIXME: This can't be efficient, but LEA (my first choice)
142          ;; doesn't do subtraction.
143          (inst shl skip (- word-shift n-fixnum-tag-bits))
144          (inst sub src skip)
145          (inst shr skip (- word-shift n-fixnum-tag-bits)))
146        (move count num)
147        (inst sub count skip)))
148
149     (inst lea loop-index (make-ea :byte :index count
150                                   :scale (ash 1 (- word-shift n-fixnum-tag-bits))))
151     (inst mov start rsp-tn)
152     (inst jrcxz DONE)  ; check for 0 count?
153
154     (inst sub rsp-tn loop-index)
155     (inst sub src loop-index)
156
157     LOOP
158     (inst mov temp (make-ea :qword :base src :index loop-index))
159     (inst sub loop-index n-word-bytes)
160     (inst mov (make-ea :qword :base rsp-tn :index loop-index) temp)
161     (inst jmp :nz LOOP)
162
163     DONE))
164