0.9.3.63:
[sbcl.git] / src / compiler / x86 / 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 esp-tn ptr)))
18
19 (define-vop (%%nip-values)
20   (:args (last-nipped-ptr :scs (any-reg) :target edi)
21          (last-preserved-ptr :scs (any-reg) :target esi)
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 esi-offset) esi)
27   (:temporary (:sc any-reg :offset edi-offset) edi)
28   (:ignore r-moved-ptrs)
29   (:generator 1
30     (move edi last-nipped-ptr)
31     (move esi last-preserved-ptr)
32     (inst sub esi n-word-bytes)
33     (inst sub edi n-word-bytes)
34     (inst cmp esp-tn esi)
35     (inst jmp :a done)
36     (inst std)
37     LOOP
38     (inst movs :dword)
39     (inst cmp esp-tn esi)
40     (inst jmp :be loop)
41     DONE
42     ;; solaris requires DF being zero.
43     #!+sunos (inst cld)
44     (inst lea esp-tn (make-ea :dword :base edi :disp n-word-bytes))
45     (inst sub edi esi)
46     (loop for moved = moved-ptrs then (tn-ref-across moved)
47           while moved
48           do (inst add (tn-ref-tn moved) edi))))
49
50 ;;; Push some values onto the stack, returning the start and number of values
51 ;;; pushed as results. It is assumed that the Vals are wired to the standard
52 ;;; argument locations. Nvals is the number of values to push.
53 ;;;
54 ;;; The generator cost is pseudo-random. We could get it right by defining a
55 ;;; bogus SC that reflects the costs of the memory-to-memory moves for each
56 ;;; operand, but this seems unworthwhile.
57 (define-vop (push-values)
58   (:args (vals :more t))
59   (:temporary (:sc unsigned-reg :to (:result 0) :target start) temp)
60   (:results (start) (count))
61   (:info nvals)
62   (:generator 20
63     (move temp esp-tn)                  ; WARN pointing 1 below
64     (do ((val vals (tn-ref-across val)))
65         ((null val))
66       (inst push (tn-ref-tn val)))
67     (move start temp)
68     (inst mov count (fixnumize nvals))))
69
70 ;;; Push a list of values on the stack, returning Start and Count as used in
71 ;;; unknown values continuations.
72 (define-vop (values-list)
73   (:args (arg :scs (descriptor-reg) :target list))
74   (:arg-types list)
75   (:policy :fast-safe)
76   (:results (start :scs (any-reg))
77             (count :scs (any-reg)))
78   (:temporary (:sc descriptor-reg :from (:argument 0) :to (:result 1)) list)
79   (:temporary (:sc descriptor-reg :to (:result 1)) nil-temp)
80   (:temporary (:sc unsigned-reg :offset eax-offset :to (:result 1)) eax)
81   (:vop-var vop)
82   (:save-p :compute-only)
83   (:generator 0
84     (move list arg)
85     (move start esp-tn)                 ; WARN pointing 1 below
86     (inst mov nil-temp nil-value)
87
88     LOOP
89     (inst cmp list nil-temp)
90     (inst jmp :e done)
91     (pushw list cons-car-slot list-pointer-lowtag)
92     (loadw list list cons-cdr-slot list-pointer-lowtag)
93     (inst mov eax list)
94     (inst and al-tn lowtag-mask)
95     (inst cmp al-tn list-pointer-lowtag)
96     (inst jmp :e loop)
97     (error-call vop bogus-arg-to-values-list-error list)
98
99     DONE
100     (inst mov count start)              ; start is high address
101     (inst sub count esp-tn)))           ; stackp is low address
102
103 ;;; Copy the more arg block to the top of the stack so we can use them
104 ;;; as function arguments.
105 ;;;
106 ;;; Accepts a context as produced by more-arg-context; points to the first
107 ;;; value on the stack, not 4 bytes above as in other contexts.
108 ;;;
109 ;;; Return a context that is 4 bytes above the first value, suitable for
110 ;;; defining a new stack frame.
111 (define-vop (%more-arg-values)
112   (:args (context :scs (descriptor-reg any-reg) :target src)
113          (skip :scs (any-reg immediate))
114          (num :scs (any-reg) :target count))
115   (:arg-types * positive-fixnum positive-fixnum)
116   (:temporary (:sc any-reg :offset esi-offset :from (:argument 0)) src)
117   (:temporary (:sc descriptor-reg :offset eax-offset) temp)
118   (:temporary (:sc unsigned-reg :offset ecx-offset) temp1)
119   (:results (start :scs (any-reg))
120             (count :scs (any-reg)))
121   (:generator 20
122     (sc-case skip
123       (immediate
124        (cond ((zerop (tn-value skip))
125               (move src context)
126               (move count num))
127              (t
128               (inst lea src (make-ea :dword :base context
129                                      :disp (- (* (tn-value skip)
130                                                  n-word-bytes))))
131               (move count num)
132               (inst sub count (* (tn-value skip) n-word-bytes)))))
133
134       (any-reg
135        (move src context)
136        (inst sub src skip)
137        (move count num)
138        (inst sub count skip)))
139
140     (move temp1 count)
141     (inst mov start esp-tn)
142     (inst jecxz done)  ; check for 0 count?
143
144     (inst shr temp1 word-shift) ; convert the fixnum to a count.
145
146     (inst std) ; move down the stack as more value are copied to the bottom.
147     LOOP
148     (inst lods temp)
149     (inst push temp)
150     (inst loop loop)
151
152     DONE
153     ;; solaris requires DF being zero.
154     #!+sunos (inst cld)))
155