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